Cleaning up the messy sections of my code and making the debug console output easier to read
55 lines
1.2 KiB
GDScript
55 lines
1.2 KiB
GDScript
extends CharacterBody2D
|
|
|
|
class_name Enemy1
|
|
enum States
|
|
{
|
|
IDLE,
|
|
ROAM,
|
|
CHASE,
|
|
ATTACK,
|
|
TAKING_DAMMAGE,
|
|
DEAD
|
|
}
|
|
const max_distance = 750
|
|
const SPEED = 300.0
|
|
var is_enemy_chase: bool
|
|
|
|
var health = 100
|
|
var health_max = 100
|
|
var health_min = 0
|
|
|
|
var attack_dammage = 10
|
|
|
|
var dir: Vector2
|
|
var knockback = 200
|
|
var current_state = States.IDLE
|
|
|
|
@onready var player: CharacterBody2D = get_tree().get_first_node_in_group("player")
|
|
@export var Projectile: PackedScene
|
|
|
|
func _on_timer_timeout() -> void:
|
|
$DirectionTimer.wait_time = chose([1,2,3])
|
|
if current_state !=States.CHASE:
|
|
dir = chose([Vector2.RIGHT, Vector2.LEFT])
|
|
var newProjectile = Projectile.instantiate() as Node2D
|
|
newProjectile.global_position=global_position
|
|
# newProjectile.global_rotation=global_rotation
|
|
get_tree().current_scene.add_child(newProjectile)
|
|
|
|
func chose(array):
|
|
array.shuffle()
|
|
return array.front()
|
|
|
|
func wraparound():
|
|
var direction = global_position.direction_to(player.global_position)
|
|
global_position += direction * max_distance*2
|
|
|
|
func _process(delta: float) -> void:
|
|
look_at(player.global_position)
|
|
#print(player.global_position)
|
|
var distance = global_position.distance_to(player.global_position)
|
|
if distance>max_distance:
|
|
wraparound()
|
|
|
|
|