godot-shooter / meteor / files / Enemy.gd
Branch meteor · default branch · all branches
Enemy.gd
b856550a 2023-10-30 Pavel Safronov | 1 | extends KinematicBody2D |
f398dcb9 2023-11-02 Pavel Safronov | 2 | class_name Enemy |
b856550a 2023-10-30 Pavel Safronov | 3 | |
| 4 | onready var ray_cast_2d: RayCast2D = $RayCast2D | |
cf2013fa 2023-11-02 Pavel Safronov | 5 | onready var damage_points = $DamagePoints |
5b18ed86 2023-11-01 Pavel Safronov | 6 | const Utils = preload("res://Utils.gd") |
| 7 | const Explosion = preload("res://Explosion.tscn") | |
cf2013fa 2023-11-02 Pavel Safronov | 8 | const Smoke = preload("res://Smoke.tscn") |
b856550a 2023-10-30 Pavel Safronov | 9 | |
ad6f22a5 2023-11-02 Pavel Safronov | 10 | export var move_speed = 40 |
e9f8f8f2 2023-11-01 Pavel Safronov | 11 | export var health = -1 |
b856550a 2023-10-30 Pavel Safronov | 12 | export var max_health = 10 |
1d4ba5a6 2023-11-02 Pavel Safronov | 13 | export var is_targeted = false |
52220002 2023-11-06 Pavel Safronov | 14 | export var is_invincible = false |
5b18ed86 2023-11-01 Pavel Safronov | 15 | signal enemy_killed |
b856550a 2023-10-30 Pavel Safronov | 16 | |
| 17 | var velocity = Vector2.ZERO | |
| 18 | var dead = false | |
| 19 | ||
e9f8f8f2 2023-11-01 Pavel Safronov | 20 | func _ready() -> void: |
cf2013fa 2023-11-02 Pavel Safronov | 21 | if health < 0 or health > max_health: |
e9f8f8f2 2023-11-01 Pavel Safronov | 22 | health = max_health |
cf2013fa 2023-11-02 Pavel Safronov | 23 | else: |
| 24 | var hurt_points = max_health - health | |
| 25 | health = max_health | |
| 26 | for i in hurt_points: | |
| 27 | hurt() | |
e9f8f8f2 2023-11-01 Pavel Safronov | 28 | |
236d1677 2023-11-01 Pavel Safronov | 29 | func _process(_delta: float) -> void: |
614e7fb1 2023-10-31 Pavel Safronov | 30 | $Graphics/Alive/Container/HealthLabel.text = "%s / %s" % [health, max_health] |
| 31 | $Graphics/Alive/Container.global_rotation = 0 | |
1d4ba5a6 2023-11-02 Pavel Safronov | 32 | $Graphics/Alive/Target.visible = is_targeted |
614e7fb1 2023-10-31 Pavel Safronov | 33 | |
8c2ffd3f 2023-11-02 Pavel Safronov | 34 | func _physics_process(_delta: float) -> void: |
b856550a 2023-10-30 Pavel Safronov | 35 | if dead: |
| 36 | return | |
5b18ed86 2023-11-01 Pavel Safronov | 37 | var players = get_tree().get_nodes_in_group("player") |
f398dcb9 2023-11-02 Pavel Safronov | 38 | var player: Player = players[0] |
| 39 | if player.dead: | |
| 40 | return | |
b856550a 2023-10-30 Pavel Safronov | 41 | var dist_to_player = global_position.distance_to(player.global_position) |
| 42 | var dir_to_player = global_position.direction_to(player.global_position) | |
| 43 | var angle_to_player = dir_to_player.angle() + PI / 2.0 | |
614e7fb1 2023-10-31 Pavel Safronov | 44 | var vel = dir_to_player * move_speed |
b856550a 2023-10-30 Pavel Safronov | 45 | global_rotation = angle_to_player |
d9bff51f 2023-10-31 Pavel Safronov | 46 | velocity = move_and_slide(vel) |
b856550a 2023-10-30 Pavel Safronov | 47 | |
| 48 | var ray_length = ray_cast_2d.cast_to.length() | |
f398dcb9 2023-11-02 Pavel Safronov | 49 | if ray_cast_2d.is_colliding(): |
| 50 | if ray_cast_2d.get_collider() == player and dist_to_player < (ray_length + 5): | |
| 51 | player.hurt(2) | |
| 52 | var explosion = Explosion.instance() | |
| 53 | explosion.transform = transform | |
| 54 | get_parent().add_child(explosion) | |
| 55 | kill() | |
b856550a 2023-10-30 Pavel Safronov | 56 | |
| 57 | func hurt(): | |
52220002 2023-11-06 Pavel Safronov | 58 | if dead || is_invincible: |
b856550a 2023-10-30 Pavel Safronov | 59 | return |
| 60 | health -= 1 | |
cf2013fa 2023-11-02 Pavel Safronov | 61 | var smoke = Smoke.instance() |
| 62 | var smoke_pos = get_damage_point_on_body() | |
| 63 | smoke.position = smoke_pos | |
| 64 | add_child(smoke) | |
b856550a 2023-10-30 Pavel Safronov | 65 | if health <= 0: |
| 66 | kill() | |
| 67 | ||
| 68 | func kill(): | |
52220002 2023-11-06 Pavel Safronov | 69 | if dead || is_invincible: |
b856550a 2023-10-30 Pavel Safronov | 70 | return |
| 71 | dead = true | |
| 72 | $Graphics/Alive.hide() | |
e9f8f8f2 2023-11-01 Pavel Safronov | 73 | $CollisionPolygon2D.queue_free() |
c6faad15 2023-10-31 Pavel Safronov | 74 | var explosion = Explosion.instance() |
| 75 | explosion.connect("explosion_done", self, "_on_death_explosion_finished") | |
| 76 | $Graphics.add_child(explosion) | |
b856550a 2023-10-30 Pavel Safronov | 77 | z_index = -1 |
5b18ed86 2023-11-01 Pavel Safronov | 78 | emit_signal("enemy_killed") |
c6faad15 2023-10-31 Pavel Safronov | 79 | |
| 80 | func _on_death_explosion_finished(): | |
cf2013fa 2023-11-02 Pavel Safronov | 81 | $AnimationPlayer.play("fade_out") |
| 82 | ||
| 83 | func get_damage_point_on_body() -> Vector2: | |
| 84 | var child_count = damage_points.get_child_count() | |
| 85 | var random_index = randi() % child_count | |
| 86 | var random_child: Position2D = damage_points.get_child(random_index) | |
| 87 | return random_child.position |