Compare commits
5 commits
a2a78bcc29
...
e151d2be84
Author | SHA1 | Date | |
---|---|---|---|
e151d2be84 | |||
fe8bebd875 | |||
a44accebe4 | |||
5b22a39a9d | |||
9e3f0fc34a |
5 changed files with 91 additions and 59 deletions
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=8 format=3 uid="uid://yitiyfwr4txh"]
|
[gd_scene load_steps=6 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,27 +6,6 @@
|
||||||
[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="."]
|
||||||
|
@ -41,11 +20,14 @@ 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
|
||||||
|
@ -54,20 +36,24 @@ drag_vertical_enabled = true
|
||||||
editor_draw_limits = true
|
editor_draw_limits = true
|
||||||
editor_draw_drag_margin = true
|
editor_draw_drag_margin = true
|
||||||
|
|
||||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player"]
|
[node name="Enemy" type="RigidBody2D" parent="." node_paths=PackedStringArray("ray")]
|
||||||
shape = SubResource("RectangleShape2D_m15tu")
|
position = Vector2(298, 42)
|
||||||
|
rotation = 2.93722
|
||||||
[node name="Enemy" type="Sprite2D" parent="." node_paths=PackedStringArray("ray")]
|
gravity_scale = 0.0
|
||||||
z_index = 1
|
linear_damp = 5.0
|
||||||
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,33 +1,35 @@
|
||||||
extends Node2D
|
extends RigidBody2D
|
||||||
|
|
||||||
@export_group("Movement Properties")
|
@export_group("Movement Properties")
|
||||||
@export var speed = 25
|
@export var speed := 100.0
|
||||||
@export var turn_speed = 25
|
@export var turn_speed := 25.0
|
||||||
|
|
||||||
@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 := 50
|
@export var max_view_distance := 120.0
|
||||||
@export_range(0.0, 360.0) var angle_cone_of_vision : float = 120
|
@export_range(0.0, 360.0) var angle_cone_of_vision := 120.0
|
||||||
@export_range(0, 72) var sweeping_steps : int = 24
|
@export_range(0, 72) var sweeping_steps := 34.0
|
||||||
@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
|
||||||
# Only used for Debugging purposes right now
|
var target_distance : float
|
||||||
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():
|
func swipe_view() -> Array:
|
||||||
# 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
|
||||||
|
|
||||||
|
@ -35,7 +37,9 @@ func swipe_view():
|
||||||
var hit_angles : Array
|
var hit_angles : Array
|
||||||
|
|
||||||
# Clear Player Positions Array
|
# Clear Player Positions Array
|
||||||
player_positions.clear()
|
target_positons.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
|
||||||
|
@ -53,22 +57,27 @@ func swipe_view():
|
||||||
else:
|
else:
|
||||||
collision_layer = collider.get_collision_layer()
|
collision_layer = collider.get_collision_layer()
|
||||||
|
|
||||||
# Checking Collision Layer
|
# Break if No relevant Collision
|
||||||
if collision_layer & player_collision_layer > 0:
|
if collision_layer & player_collision_layer > 0:
|
||||||
|
# Checking Collision Layer
|
||||||
hit_angles.append(ray.rotation_degrees)
|
hit_angles.append(ray.rotation_degrees)
|
||||||
player_positions.append(to_local(ray.get_collision_point()))
|
target_positons.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):
|
func look_at_player(delta : float) -> 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
|
return 0
|
||||||
state = STATES.TARGETING
|
state = STATES.TARGETING
|
||||||
|
|
||||||
# Calculate Average Angle Of Player
|
# Calculate Average Angle Of Player
|
||||||
|
@ -79,10 +88,27 @@ func look_at_player(delta : 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):
|
|
||||||
look_at_player(delta)
|
func _physics_process(delta: float):
|
||||||
|
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():
|
||||||
|
@ -99,9 +125,8 @@ 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
|
||||||
if state == STATES.TARGETING:
|
for pos in target_positons:
|
||||||
for pos in player_positions:
|
self.draw_circle(pos, 1, Color.RED)
|
||||||
self.draw_circle(pos, 4, Color.ORANGE_RED)
|
|
||||||
queue_redraw()
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
9
Scripts/GameManager.gd
Normal file
9
Scripts/GameManager.gd
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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,16 +1,24 @@
|
||||||
extends CharacterBody2D
|
extends CharacterBody2D
|
||||||
|
|
||||||
@export var speed = 50
|
@export var speed = 100
|
||||||
|
@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
|
||||||
|
|
||||||
func _physics_process(delta):
|
# Update The Players Physics Calculations
|
||||||
# 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,6 +14,10 @@ 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