2023-10-06 14:25:13 +02:00
|
|
|
extends PathFollow2D
|
|
|
|
|
|
|
|
var config = null
|
|
|
|
var enemy_id: int = 0
|
|
|
|
var health: int = 0
|
2023-10-07 17:13:27 +02:00
|
|
|
var damage: int = 0
|
2023-10-06 14:25:13 +02:00
|
|
|
var speed: int = 0
|
2023-10-07 17:39:23 +02:00
|
|
|
var reward: int = 0
|
2023-10-06 19:02:54 +02:00
|
|
|
var direction_progress = []
|
|
|
|
var direction = []
|
2023-10-06 14:25:13 +02:00
|
|
|
|
2023-10-16 19:12:44 +02:00
|
|
|
var enemy_vol = Vector2(1,1)
|
|
|
|
var old_enemy_pos
|
|
|
|
|
2023-10-06 14:25:13 +02:00
|
|
|
func set_config(config_path):
|
|
|
|
config = load(config_path).new()
|
|
|
|
|
|
|
|
func set_enemy(id: int):
|
|
|
|
enemy_id = id
|
|
|
|
health = config.enemy[enemy_id]['health']
|
2023-10-07 17:13:27 +02:00
|
|
|
damage = config.enemy[enemy_id]['damage']
|
2023-10-06 14:25:13 +02:00
|
|
|
speed = config.enemy[enemy_id]['speed']
|
2023-10-07 17:39:23 +02:00
|
|
|
reward = config.enemy[enemy_id]['reward']
|
2023-10-06 19:02:54 +02:00
|
|
|
|
2023-10-14 15:16:53 +02:00
|
|
|
func set_direction(type: int):
|
|
|
|
var path_direction = config.game['path_direction'][type]
|
2023-10-06 19:02:54 +02:00
|
|
|
for x in path_direction:
|
|
|
|
direction_progress.append(x['progress'])
|
|
|
|
direction.append(x['direction'])
|
2023-10-06 14:25:13 +02:00
|
|
|
|
2023-10-16 19:12:44 +02:00
|
|
|
func _ready():
|
|
|
|
old_enemy_pos = global_position
|
|
|
|
|
2023-10-06 14:25:13 +02:00
|
|
|
func _process(delta):
|
|
|
|
update_healthbar()
|
2023-10-06 19:02:54 +02:00
|
|
|
change_direction(get_progress())
|
2023-10-07 17:39:23 +02:00
|
|
|
if health < 1:
|
|
|
|
get_tree().current_scene.on_enemy_reward(reward)
|
2023-10-06 14:25:13 +02:00
|
|
|
queue_free()
|
|
|
|
set_progress(get_progress() + speed * delta)
|
2023-10-16 19:12:44 +02:00
|
|
|
enemy_vol = global_position - old_enemy_pos
|
|
|
|
old_enemy_pos = global_position
|
2023-10-06 19:02:54 +02:00
|
|
|
|
|
|
|
func change_direction(current_progress):
|
|
|
|
if !direction_progress.is_empty():
|
|
|
|
if current_progress > direction_progress[0]:
|
|
|
|
$EnemyBody/AnimatedSprite2D.play(direction[0])
|
|
|
|
direction_progress.erase(direction_progress[0])
|
|
|
|
direction.erase(direction[0])
|
2023-10-06 14:25:13 +02:00
|
|
|
|
|
|
|
func update_healthbar():
|
2023-10-06 17:47:24 +02:00
|
|
|
$EnemyBody/HealthBar.value = health
|
2023-10-06 14:25:13 +02:00
|
|
|
|
|
|
|
if health >= 100:
|
2023-10-06 17:47:24 +02:00
|
|
|
$EnemyBody/HealthBar.visible = false
|
2023-10-06 14:25:13 +02:00
|
|
|
else:
|
2023-10-06 17:47:24 +02:00
|
|
|
$EnemyBody/HealthBar.visible = true
|
2023-10-06 14:25:13 +02:00
|
|
|
|
2023-10-08 12:51:55 +02:00
|
|
|
func hit(value : int):
|
|
|
|
health = health - value
|
2023-10-07 17:13:27 +02:00
|
|
|
|
|
|
|
func destroy():
|
|
|
|
queue_free()
|