fight system and whatnot
This commit is contained in:
86
scripts/enemies/skull.gd
Normal file
86
scripts/enemies/skull.gd
Normal file
@@ -0,0 +1,86 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
@export var speed: int = 900
|
||||
@onready var navigation_agent_2d: NavigationAgent2D = $NavigationAgent2D
|
||||
@onready var follow_update_timer: Timer = $FollowUpdateTimer
|
||||
@onready var attack_timer: Timer = $AttackTimer
|
||||
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
|
||||
var idle: bool = true
|
||||
const DAMAGE: int = 1
|
||||
var health: int = 8
|
||||
var target: Player = null
|
||||
var in_attack_range: bool = false
|
||||
var stop_distance: int = 20
|
||||
var attack_cooldown_offset: float = 0.0
|
||||
var flicker_time: float = 0.2
|
||||
var flicker_timer: float = 0.0
|
||||
|
||||
func _ready() -> void:
|
||||
navigation_agent_2d.avoidance_priority = randf_range(0.5, 1.0)
|
||||
attack_cooldown_offset = randf_range(0.0, 0.5)
|
||||
animated_sprite.speed_scale = randf_range(0.4, 1.2)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if flicker_timer > 0.0:
|
||||
flicker_timer -= delta
|
||||
animated_sprite.modulate = Color(1, 0, 0)
|
||||
if flicker_timer <= 0.0:
|
||||
animated_sprite.modulate = Color(1, 1, 1)
|
||||
|
||||
func _on_area_body_entered(body: Node2D) -> void:
|
||||
if body is Player:
|
||||
idle = false
|
||||
target = body
|
||||
follow_update_timer.start()
|
||||
animated_sprite.play()
|
||||
|
||||
func _on_area_body_exited(body: Node2D) -> void:
|
||||
if body is Player:
|
||||
idle = true
|
||||
target = null
|
||||
follow_update_timer.stop()
|
||||
animated_sprite.frame = 0
|
||||
animated_sprite.stop()
|
||||
|
||||
func _on_attack_area_body_entered(body: Node2D) -> void:
|
||||
if body is Player:
|
||||
in_attack_range = true
|
||||
attack_timer.start(attack_timer.wait_time + attack_cooldown_offset)
|
||||
|
||||
func _on_attack_area_body_exited(body: Node2D) -> void:
|
||||
if body is Player:
|
||||
in_attack_range = false
|
||||
attack_timer.stop()
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if !idle && target:
|
||||
if !navigation_agent_2d.is_navigation_finished():
|
||||
var next_path_pos: Vector2 = navigation_agent_2d.get_next_path_position()
|
||||
var direction: Vector2 = (next_path_pos - global_position).normalized()
|
||||
var desired_velocity: Vector2 = direction * speed * delta
|
||||
navigation_agent_2d.set_velocity(desired_velocity)
|
||||
else:
|
||||
velocity = Vector2.ZERO
|
||||
move_and_slide()
|
||||
else:
|
||||
velocity = Vector2.ZERO
|
||||
move_and_slide()
|
||||
|
||||
func _on_follow_update_timer_timeout() -> void:
|
||||
if target && !idle:
|
||||
navigation_agent_2d.target_position = target.global_position
|
||||
|
||||
func _on_navigation_agent2d_velocity_computed(safe_velocity: Vector2) -> void:
|
||||
velocity = safe_velocity
|
||||
move_and_slide()
|
||||
|
||||
func _on_attack_timer_timeout() -> void:
|
||||
if target && in_attack_range:
|
||||
target.health -= DAMAGE
|
||||
|
||||
func _on_fightable_fought(player: Player) -> void:
|
||||
health -= player.damage
|
||||
flicker_timer = flicker_time
|
||||
if health <= 0:
|
||||
queue_free()
|
||||
1
scripts/enemies/skull.gd.uid
Normal file
1
scripts/enemies/skull.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cuc28u7op02gq
|
||||
Reference in New Issue
Block a user