godot-shooter / starfield-background / files / Enemy.gd
Branch starfield-background · default branch · all branches
Enemy.gd
b856550a 2023-10-30 Pavel Safronov | 1 | extends KinematicBody2D |
| 2 | ||
| 3 | onready var ray_cast_2d: RayCast2D = $RayCast2D | |
| 4 | onready var scene_tree: SceneTree = get_tree() | |
614e7fb1 2023-10-31 Pavel Safronov | 5 | const utils = preload("res://Utils.gd") |
c6faad15 2023-10-31 Pavel Safronov | 6 | var Explosion = preload("res://Explosion.tscn") |
b856550a 2023-10-30 Pavel Safronov | 7 | |
| 8 | export var move_speed = 100 | |
| 9 | export var health: int = randi() % 8 + 2 | |
| 10 | export var max_health = 10 | |
| 11 | ||
| 12 | var velocity = Vector2.ZERO | |
| 13 | ||
| 14 | var dead = false | |
| 15 | ||
614e7fb1 2023-10-31 Pavel Safronov | 16 | func _process(delta: float) -> void: |
| 17 | $Graphics/Alive/Container/HealthLabel.text = "%s / %s" % [health, max_health] | |
| 18 | $Graphics/Alive/Container.global_rotation = 0 | |
| 19 | ||
b856550a 2023-10-30 Pavel Safronov | 20 | func _physics_process(delta: float) -> void: |
| 21 | if dead: | |
| 22 | return | |
| 23 | var players = scene_tree.get_nodes_in_group("player") | |
| 24 | var player: KinematicBody2D = players[0] | |
| 25 | var dist_to_player = global_position.distance_to(player.global_position) | |
| 26 | ||
| 27 | var dir_to_player = global_position.direction_to(player.global_position) | |
| 28 | var angle_to_player = dir_to_player.angle() + PI / 2.0 | |
614e7fb1 2023-10-31 Pavel Safronov | 29 | var vel = dir_to_player * move_speed |
b856550a 2023-10-30 Pavel Safronov | 30 | global_rotation = angle_to_player |
d9bff51f 2023-10-31 Pavel Safronov | 31 | velocity = move_and_slide(vel) |
b856550a 2023-10-30 Pavel Safronov | 32 | |
| 33 | var ray_length = ray_cast_2d.cast_to.length() | |
| 34 | if ray_cast_2d.is_colliding() and ray_cast_2d.get_collider() == player and dist_to_player < (ray_length + 5): | |
| 35 | player.hurt() | |
| 36 | var push = to_global(ray_cast_2d.cast_to) - to_global(Vector2.ZERO) | |
| 37 | player.position += (push * 2) | |
| 38 | player.velocity = player.move_and_slide(player.velocity) | |
| 39 | ||
| 40 | func hurt(): | |
| 41 | if dead: | |
| 42 | return | |
614e7fb1 2023-10-31 Pavel Safronov | 43 | utils.log("zombie is hurt, health was ", health) |
b856550a 2023-10-30 Pavel Safronov | 44 | health -= 1 |
| 45 | if health <= 0: | |
| 46 | kill() | |
| 47 | ||
| 48 | func kill(): | |
| 49 | if dead: | |
| 50 | return | |
614e7fb1 2023-10-31 Pavel Safronov | 51 | utils.log("zombie is dead") |
b856550a 2023-10-30 Pavel Safronov | 52 | dead = true |
| 53 | $Graphics/Alive.hide() | |
c6faad15 2023-10-31 Pavel Safronov | 54 | var explosion = Explosion.instance() |
| 55 | explosion.connect("explosion_done", self, "_on_death_explosion_finished") | |
| 56 | $Graphics.add_child(explosion) | |
b856550a 2023-10-30 Pavel Safronov | 57 | $CollisionPolygon2D.disabled = true |
| 58 | z_index = -1 | |
c6faad15 2023-10-31 Pavel Safronov | 59 | |
| 60 | func _on_death_explosion_finished(): | |
| 61 | utils.log("removing zombie after explosion finished") | |
b856550a 2023-10-30 Pavel Safronov | 62 | queue_free() |