Files
Defensaria/Game/Mobs/enemy.gd

152 lines
4.2 KiB
GDScript

extends Node2D
var speed = 0
var health = 0
var shield = 0
var damage = 0
var current_health = 0
var current_shield = 0
var last_position: Vector2 = Vector2.ZERO # for previous position
static var NAMES = ["Forkman", "Cobold", "Ork"]
func _ready() -> void:
last_position = get_parent().position
get_node("CharacterBody2D/Area2D").connect("body_entered", Callable(self, "Collision_Handler"))
get_node("CharacterBody2D/AnimatedSprite2D").connect("animation_finished", Callable(self, "AnimatedSprite2D_animation_finished"))
set_character_data()
adjust_health_bar()
func _process(delta: float) -> void:
move_character(delta)
func move_character(delta: float) -> void:
var path_follow = get_parent()
# Update the progress and prevent rotation
path_follow.set_progress(path_follow.get_progress() + speed * delta)
path_follow.rotation = 0
#turn the character to to the way its headed
var current_position = path_follow.position
if current_position.x < last_position.x:
path_follow.scale.x = -abs(path_follow.scale.x)
else:
path_follow.scale.x = abs(path_follow.scale.x)
# Update the last_position for the next frame
last_position = current_position
#if reached the end of the path remove both the path, and the character
if path_follow.get_progress_ratio() >= 0.999:
get_parent().get_parent().get_parent().decrease_life(damage)
get_parent().queue_free()
func set_character_data():
match self.name:
"Forkman":
self.name = "Forkman-" + str(randi())
speed = 120
health = 100
shield = 100
damage = 2
"Ork":
self.name = "Ork-" + str(randi())
speed = 80
health = 150
shield = 200
damage = 5
"Cobold":
self.name = "Cobold-" + str(randi())
speed = 200
health = 50
shield = 0
damage = 1
get_node("CharacterBody2D/AnimatedSprite2D").speed_scale = float(speed) / 100 #adjust animation speed based on the character speed
current_health = health
current_shield = shield
func adjust_health_bar() -> void:
var health_bar = get_node("ProgressBar")
# Create new StyleBox resources for each instance
var fill_style = StyleBoxFlat.new()
fill_style.corner_radius_top_left = 20
fill_style.corner_radius_top_right = 20
fill_style.corner_radius_bottom_left = 20
fill_style.corner_radius_bottom_right = 20
var bg_style = StyleBoxFlat.new()
bg_style.corner_radius_top_left = 20
bg_style.corner_radius_top_right = 20
bg_style.corner_radius_bottom_left = 20
bg_style.corner_radius_bottom_right = 20
if current_shield > 0:
fill_style.bg_color = Color(0, 0, 1) # Blue for shield
bg_style.bg_color = Color(0, 1, 0) # Green background
health_bar.value = int((current_shield / float(shield)) * 100) # Use current_shield and shield
else:
fill_style.bg_color = Color(0, 1, 0) # Green for health
bg_style.bg_color = Color(1, 0, 0) # Red background
health_bar.value = int((current_health / float(health)) * 100)
# Apply the styles
health_bar.add_theme_stylebox_override("fill", fill_style)
health_bar.add_theme_stylebox_override("background", bg_style)
func enemy_hurt(amount) -> void:
if current_shield > 0:
if current_shield > amount:
current_shield -= amount
else:
amount -= current_shield
current_shield = 0
current_health -= amount
else:
current_health -= amount
if current_health <= 0:
get_parent().queue_free()
else:
adjust_health_bar()
func AnimatedSprite2D_animation_finished() -> void:
var animated_sprite = get_node("CharacterBody2D/AnimatedSprite2D")
if animated_sprite.animation == "hurt":
animated_sprite.play("walk")
func Collision_Handler(body: Node2D):
if body.get_parent().name.contains("StickTrap"):
if not body.get_parent().get_if_moving_state():
enemy_hurt(25)
get_node("CharacterBody2D/AnimatedSprite2D").play("hurt")
if body.get_parent().name.contains("Mine"):
if not body.get_parent().get_if_moving_state():
var surrounding_enemies = body.get_node("Area2D").get_overlapping_bodies()
for i in surrounding_enemies:
for j in NAMES:
if i.get_parent().name.contains(j):
enemy_hurt(100)
body.get_parent().Explode_Mine()
func get_progress():
return get_parent().get_progress()
func set_progress(amount) -> void:
get_parent().set_progress(amount)
func get_speed():
return speed
func set_speed(nspeed) -> void:
speed = nspeed