Compare commits
No commits in common. "e151d2be84d17606027cb65cd569aa38375f2b6d" and "a2a78bcc291acc36b8ce9f3887aba4f0dfb74fed" have entirely different histories.
e151d2be84
...
a2a78bcc29
5 changed files with 59 additions and 91 deletions
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=6 format=3 uid="uid://yitiyfwr4txh"]
|
[gd_scene load_steps=8 format=3 uid="uid://yitiyfwr4txh"]
|
||||||
|
|
||||||
[ext_resource type="TileSet" uid="uid://bf1eb4aogv8ru" path="res://Assets/Tilesets/colony.tres" id="2"]
|
[ext_resource type="TileSet" uid="uid://bf1eb4aogv8ru" path="res://Assets/Tilesets/colony.tres" id="2"]
|
||||||
[ext_resource type="Texture2D" uid="uid://dp4tdg3fxe5bf" path="res://Assets/Textures/color_cube.png" id="2_0flm2"]
|
[ext_resource type="Texture2D" uid="uid://dp4tdg3fxe5bf" path="res://Assets/Textures/color_cube.png" id="2_0flm2"]
|
||||||
|
@ -6,6 +6,27 @@
|
||||||
[ext_resource type="Texture2D" uid="uid://dx5k5qa1pwbfl" path="res://Assets/Textures/basic_enemy.png" id="4_4sn7h"]
|
[ext_resource type="Texture2D" uid="uid://dx5k5qa1pwbfl" path="res://Assets/Textures/basic_enemy.png" id="4_4sn7h"]
|
||||||
[ext_resource type="Script" path="res://Scripts/Enemy/EnemyController.gd" id="5_5xgcv"]
|
[ext_resource type="Script" path="res://Scripts/Enemy/EnemyController.gd" id="5_5xgcv"]
|
||||||
|
|
||||||
|
[sub_resource type="RectangleShape2D" id="RectangleShape2D_m15tu"]
|
||||||
|
size = Vector2(5, 5)
|
||||||
|
|
||||||
|
[sub_resource type="GDScript" id="GDScript_xkf0t"]
|
||||||
|
script/source = "extends Node
|
||||||
|
enum STATES {
|
||||||
|
Partroling,
|
||||||
|
Targeting
|
||||||
|
}
|
||||||
|
@export var sees := STATES.Partroling
|
||||||
|
|
||||||
|
# Called when the node enters the scene tree for the first time.
|
||||||
|
func _ready():
|
||||||
|
pass # Replace with function body.
|
||||||
|
|
||||||
|
|
||||||
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||||
|
func _process(delta):
|
||||||
|
pass
|
||||||
|
"
|
||||||
|
|
||||||
[node name="Test Scene" type="Node2D"]
|
[node name="Test Scene" type="Node2D"]
|
||||||
|
|
||||||
[node name="TileMap" type="TileMap" parent="."]
|
[node name="TileMap" type="TileMap" parent="."]
|
||||||
|
@ -20,14 +41,11 @@ layer_0/tile_data = PackedInt32Array(1114138, 262144, 1, 1048602, 0, 1, 983066,
|
||||||
position = Vector2(203, 60)
|
position = Vector2(203, 60)
|
||||||
collision_layer = 2
|
collision_layer = 2
|
||||||
script = ExtResource("2_pk6k4")
|
script = ExtResource("2_pk6k4")
|
||||||
|
speed = 100
|
||||||
|
|
||||||
[node name="Sprite2D" type="Sprite2D" parent="Player"]
|
[node name="Sprite2D" type="Sprite2D" parent="Player"]
|
||||||
texture = ExtResource("2_0flm2")
|
texture = ExtResource("2_0flm2")
|
||||||
|
|
||||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Player"]
|
|
||||||
position = Vector2(-2, -2)
|
|
||||||
polygon = PackedVector2Array(-1, 0, 0, -1, 4, -1, 5, 0, 5, 4, 4, 5, 0, 5, -1, 4)
|
|
||||||
|
|
||||||
[node name="Camera2D" type="Camera2D" parent="Player"]
|
[node name="Camera2D" type="Camera2D" parent="Player"]
|
||||||
zoom = Vector2(8, 8)
|
zoom = Vector2(8, 8)
|
||||||
position_smoothing_enabled = true
|
position_smoothing_enabled = true
|
||||||
|
@ -36,24 +54,20 @@ drag_vertical_enabled = true
|
||||||
editor_draw_limits = true
|
editor_draw_limits = true
|
||||||
editor_draw_drag_margin = true
|
editor_draw_drag_margin = true
|
||||||
|
|
||||||
[node name="Enemy" type="RigidBody2D" parent="." node_paths=PackedStringArray("ray")]
|
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
|
||||||
position = Vector2(298, 42)
|
shape = SubResource("RectangleShape2D_m15tu")
|
||||||
rotation = 2.93722
|
|
||||||
gravity_scale = 0.0
|
[node name="Enemy" type="Sprite2D" parent="." node_paths=PackedStringArray("ray")]
|
||||||
linear_damp = 5.0
|
z_index = 1
|
||||||
|
position = Vector2(237, 46)
|
||||||
|
rotation = 2.04581
|
||||||
|
texture = ExtResource("4_4sn7h")
|
||||||
script = ExtResource("5_5xgcv")
|
script = ExtResource("5_5xgcv")
|
||||||
ray = NodePath("RayCast2D")
|
ray = NodePath("RayCast2D")
|
||||||
debug_vision = true
|
debug_vision = true
|
||||||
|
|
||||||
[node name="Sprite2D" type="Sprite2D" parent="Enemy"]
|
|
||||||
z_index = 1
|
|
||||||
texture = ExtResource("4_4sn7h")
|
|
||||||
|
|
||||||
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Enemy"]
|
|
||||||
position = Vector2(-2, -1.99998)
|
|
||||||
polygon = PackedVector2Array(-1, 0, 0, -1, 4, -1, 5, 0, 5, 4, 4, 5, 0, 5, -1, 4)
|
|
||||||
|
|
||||||
[node name="RayCast2D" type="RayCast2D" parent="Enemy"]
|
[node name="RayCast2D" type="RayCast2D" parent="Enemy"]
|
||||||
target_position = Vector2(15, 0)
|
target_position = Vector2(15, 0)
|
||||||
collision_mask = 3
|
collision_mask = 3
|
||||||
hit_from_inside = true
|
hit_from_inside = true
|
||||||
|
script = SubResource("GDScript_xkf0t")
|
||||||
|
|
|
@ -1,35 +1,33 @@
|
||||||
extends RigidBody2D
|
extends Node2D
|
||||||
|
|
||||||
@export_group("Movement Properties")
|
@export_group("Movement Properties")
|
||||||
@export var speed := 100.0
|
@export var speed = 25
|
||||||
@export var turn_speed := 25.0
|
@export var turn_speed = 25
|
||||||
|
|
||||||
@export_group("Vision Properties")
|
@export_group("Vision Properties")
|
||||||
@export var ray : RayCast2D
|
@export var ray : RayCast2D
|
||||||
@export_flags_2d_physics var player_collision_layer := 2
|
@export_flags_2d_physics var player_collision_layer := 2
|
||||||
@export var max_view_distance := 120.0
|
@export var max_view_distance := 50
|
||||||
@export_range(0.0, 360.0) var angle_cone_of_vision := 120.0
|
@export_range(0.0, 360.0) var angle_cone_of_vision : float = 120
|
||||||
@export_range(0, 72) var sweeping_steps := 34.0
|
@export_range(0, 72) var sweeping_steps : int = 24
|
||||||
@export var debug_vision := false
|
@export var debug_vision := false
|
||||||
|
|
||||||
@export_group("Behaviour Properties")
|
|
||||||
@export var follow_keep_distance := 30.0
|
|
||||||
|
|
||||||
enum STATES {
|
enum STATES {
|
||||||
IDLE,
|
IDLE,
|
||||||
TARGETING
|
TARGETING
|
||||||
}
|
}
|
||||||
|
|
||||||
var state : STATES
|
var state : STATES
|
||||||
var target_positons : Array
|
|
||||||
var target_distance : float
|
# Only used for Debugging purposes right now
|
||||||
|
var player_positions : Array
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
# Set Raycast into view Direction
|
# Set Raycast into view Direction
|
||||||
ray.target_position = Vector2(max_view_distance, 0)
|
ray.target_position = Vector2(max_view_distance, 0)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func swipe_view() -> Array:
|
func swipe_view():
|
||||||
# Set Raycast to starting position
|
# Set Raycast to starting position
|
||||||
ray.rotation_degrees = -angle_cone_of_vision / 2
|
ray.rotation_degrees = -angle_cone_of_vision / 2
|
||||||
|
|
||||||
|
@ -37,9 +35,7 @@ func swipe_view() -> Array:
|
||||||
var hit_angles : Array
|
var hit_angles : Array
|
||||||
|
|
||||||
# Clear Player Positions Array
|
# Clear Player Positions Array
|
||||||
target_positons.clear()
|
player_positions.clear()
|
||||||
# Reset Targets distance to Infinity
|
|
||||||
target_distance = INF
|
|
||||||
|
|
||||||
for step in sweeping_steps + 1:
|
for step in sweeping_steps + 1:
|
||||||
# Check Ray
|
# Check Ray
|
||||||
|
@ -57,27 +53,22 @@ func swipe_view() -> Array:
|
||||||
else:
|
else:
|
||||||
collision_layer = collider.get_collision_layer()
|
collision_layer = collider.get_collision_layer()
|
||||||
|
|
||||||
# Break if No relevant Collision
|
|
||||||
if collision_layer & player_collision_layer > 0:
|
|
||||||
# Checking Collision Layer
|
# Checking Collision Layer
|
||||||
|
if collision_layer & player_collision_layer > 0:
|
||||||
hit_angles.append(ray.rotation_degrees)
|
hit_angles.append(ray.rotation_degrees)
|
||||||
target_positons.append(to_local(ray.get_collision_point()))
|
player_positions.append(to_local(ray.get_collision_point()))
|
||||||
|
|
||||||
# Check distance
|
|
||||||
target_distance = min(target_distance, ray.get_collision_point().distance_to(self.global_position))
|
|
||||||
|
|
||||||
# Rotate Ray
|
# Rotate Ray
|
||||||
ray.rotation_degrees += angle_cone_of_vision / sweeping_steps
|
ray.rotation_degrees += angle_cone_of_vision / sweeping_steps
|
||||||
return hit_angles
|
return hit_angles
|
||||||
|
|
||||||
func look_at_player(delta : float) -> float:
|
func look_at_player(delta : float):
|
||||||
# Get All Angles at which the Player can be seen
|
# Get All Angles at which the Player can be seen
|
||||||
var hit_angles = swipe_view()
|
var hit_angles = swipe_view()
|
||||||
|
|
||||||
# If The Player cant be seen, return and do nothing
|
# If The Player cant be seen, return and do nothing
|
||||||
if hit_angles.size() <= 0:
|
if hit_angles.size() <= 0:
|
||||||
state = STATES.IDLE
|
state = STATES.IDLE
|
||||||
return 0
|
return
|
||||||
state = STATES.TARGETING
|
state = STATES.TARGETING
|
||||||
|
|
||||||
# Calculate Average Angle Of Player
|
# Calculate Average Angle Of Player
|
||||||
|
@ -88,27 +79,10 @@ func look_at_player(delta : float) -> float:
|
||||||
|
|
||||||
# Rotate towards Player, but limit it by rotation speed
|
# Rotate towards Player, but limit it by rotation speed
|
||||||
self.rotation_degrees += clampf(average_angle, -2 * PI * delta * turn_speed, 2 * PI * delta * turn_speed)
|
self.rotation_degrees += clampf(average_angle, -2 * PI * delta * turn_speed, 2 * PI * delta * turn_speed)
|
||||||
return average_angle
|
|
||||||
|
|
||||||
func follow_player(angle : float, delta : float):
|
|
||||||
if(target_distance > follow_keep_distance):
|
|
||||||
self.apply_central_force(Vector2.RIGHT.rotated(self.rotation).rotated(deg_to_rad(angle)) * speed * delta * 60)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
func _physics_process(delta):
|
||||||
func _physics_process(delta: float):
|
look_at_player(delta)
|
||||||
var angle := look_at_player(delta)
|
|
||||||
match state:
|
|
||||||
STATES.TARGETING:
|
|
||||||
follow_player(angle, delta)
|
|
||||||
STATES.IDLE:
|
|
||||||
# Do Idle Stuff
|
|
||||||
print("Dam, Nice a pause, time for a Coffee")
|
|
||||||
pass
|
|
||||||
|
|
||||||
func _process(delta: float):
|
|
||||||
if debug_vision:
|
|
||||||
queue_redraw()
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func _draw():
|
func _draw():
|
||||||
|
@ -125,8 +99,9 @@ func _draw():
|
||||||
self.draw_arc(Vector2(), max_view_distance, deg_to_rad(-angle_cone_of_vision / 2), deg_to_rad(angle_cone_of_vision / 2), sweeping_steps + 1, Color.RED, 0.5, false)
|
self.draw_arc(Vector2(), max_view_distance, deg_to_rad(-angle_cone_of_vision / 2), deg_to_rad(angle_cone_of_vision / 2), sweeping_steps + 1, Color.RED, 0.5, false)
|
||||||
|
|
||||||
# Draw A Circle Around the Player if hes seen
|
# Draw A Circle Around the Player if hes seen
|
||||||
for pos in target_positons:
|
if state == STATES.TARGETING:
|
||||||
self.draw_circle(pos, 1, Color.RED)
|
for pos in player_positions:
|
||||||
|
self.draw_circle(pos, 4, Color.ORANGE_RED)
|
||||||
|
queue_redraw()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
extends Node
|
|
||||||
|
|
||||||
# Called when the node enters the scene tree for the first time.
|
|
||||||
func _ready():
|
|
||||||
pass # Replace with function body.
|
|
||||||
|
|
||||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
||||||
func _process(delta):
|
|
||||||
pass
|
|
|
@ -1,24 +1,16 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
@export var speed = 100
|
@export var speed = 50
|
||||||
@export_range(0, 1) var damping_factor := 0.6
|
|
||||||
|
|
||||||
func get_move_vector():
|
func get_move_vector():
|
||||||
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
|
var input_direction = Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
|
||||||
return input_direction
|
return input_direction
|
||||||
|
|
||||||
func move_player(delta : float):
|
|
||||||
if get_move_vector() == Vector2.ZERO:
|
|
||||||
# Damp Players Velocity if no Input
|
|
||||||
self.velocity = self.velocity * (1 - min(1, damping_factor * 60 * delta))
|
|
||||||
else:
|
|
||||||
# Set Players Velocity to the Input Direction in the Players Speed
|
|
||||||
self.velocity = get_move_vector() * speed * delta * 60
|
|
||||||
|
|
||||||
# Update The Players Physics Calculations
|
func _physics_process(delta):
|
||||||
|
# Set Velcoity to Move Vector times speed
|
||||||
|
self.velocity = get_move_vector() * speed
|
||||||
|
# Do the Physics-Calculations of the Player
|
||||||
self.move_and_slide()
|
self.move_and_slide()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func _physics_process(delta : float):
|
|
||||||
move_player(delta)
|
|
||||||
pass
|
|
||||||
|
|
|
@ -14,10 +14,6 @@ config/name="2d Hack And Slay"
|
||||||
run/main_scene="res://Scenes/Test Scene.tscn"
|
run/main_scene="res://Scenes/Test Scene.tscn"
|
||||||
config/features=PackedStringArray("4.0")
|
config/features=PackedStringArray("4.0")
|
||||||
|
|
||||||
[autoload]
|
|
||||||
|
|
||||||
GameManager="*res://Scripts/GameManager.gd"
|
|
||||||
|
|
||||||
[display]
|
[display]
|
||||||
|
|
||||||
window/stretch/mode="viewport"
|
window/stretch/mode="viewport"
|
||||||
|
|
Reference in a new issue