From b121e6a10d906a6af14ace4415c735bf8b8a29f1 Mon Sep 17 00:00:00 2001 From: Luna Brovchuk Date: Sun, 1 Oct 2023 12:24:43 +0200 Subject: [PATCH] Fix Target Selection --- src/models/enemies/ghost/ghost.gd | 7 +++---- src/models/player/player.gd | 17 +++++++++-------- src/scenes/game/Game.tscn | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/models/enemies/ghost/ghost.gd b/src/models/enemies/ghost/ghost.gd index be58284..046b94a 100644 --- a/src/models/enemies/ghost/ghost.gd +++ b/src/models/enemies/ghost/ghost.gd @@ -1,7 +1,6 @@ -extends KinematicBody2D +extends EnemyClass onready var player = $"../../Player" -var speed_multiplier = 1 var dash_target = Vector2.ZERO var dash_timer = .0 var dashing = false @@ -13,10 +12,10 @@ func _physics_process(delta): else: if dash_target == Vector2.ZERO: var angle = global_position.angle_to_point(player.global_position) + PI - dash_target = global_position + Vector2(70, 0).rotated(angle) + dash_target = global_position + Vector2(60, 0).rotated(angle) if dash_target != Vector2.ZERO: dash_timer += delta - if dash_timer >= 2.0: + if dash_timer >= 1.0: dash_timer = 0 dashing = true diff --git a/src/models/player/player.gd b/src/models/player/player.gd index 8c5ceef..5bb950b 100644 --- a/src/models/player/player.gd +++ b/src/models/player/player.gd @@ -3,8 +3,8 @@ extends KinematicBody2D var velocity = Vector2.ZERO var speed_multiplier: float = 1.0 var slowed = false +var target var speed = 100 - func get_input(): var input_direction = Input.get_vector("left", "right", "up", "down") velocity = input_direction * speed @@ -25,13 +25,14 @@ func _physics_process(_delta): speed_multiplier = 1.0 velocity = move_and_slide(velocity * speed_multiplier) - _choose_target() + choose_target() -func _choose_target(): +func choose_target(): + var least_dist = 4000 + var enemy_dist var enemies = get_tree().get_nodes_in_group("enemy") - enemies.sort_custom(self, "_sort") for enemy in enemies: - enemies.front().scale.x = 1 #TODO - -func _sort(a, b): - return a.position < b.position + enemy_dist = global_position.distance_to(enemy.global_position) + if enemy_dist < least_dist: + least_dist = enemy_dist + target = enemy diff --git a/src/scenes/game/Game.tscn b/src/scenes/game/Game.tscn index de2e68a..fbe842b 100644 --- a/src/scenes/game/Game.tscn +++ b/src/scenes/game/Game.tscn @@ -73,7 +73,7 @@ shape = SubResource( 3 ) [node name="Enemies" type="Node2D" parent="View"] -[node name="TrashCan" parent="View/Enemies" groups=["enemy"] instance=ExtResource( 13 )] +[node name="TrashCan" parent="View/Enemies" instance=ExtResource( 13 )] [node name="Bat" parent="View/Enemies" instance=ExtResource( 14 )] z_index = 1