86 lines
2.6 KiB
GDScript
86 lines
2.6 KiB
GDScript
extends "res://objects/enemy/enemy.gd"
|
|
|
|
export var walk_speed = 25.0
|
|
export var left_boundary = 0.0
|
|
export var right_boundary = 0.0
|
|
export var direction = 1.0
|
|
export var idle_turns = 4
|
|
export var turn_time = 0.5
|
|
export var shoot_time = 1.0
|
|
|
|
onready var sprite = $AnimatedSprite
|
|
onready var shoot_position = $"%ShootPosition"
|
|
onready var shoot_cast = $"%ShootCast"
|
|
onready var graphics_cast = $"%GraphicsCast"
|
|
onready var shoot_line = $"%ShootLine"
|
|
onready var sparks = $SparkParticles
|
|
|
|
var shooting = false
|
|
var turns = 0
|
|
|
|
func _ready():
|
|
# convert boundaries into actual coordinate positions
|
|
left_boundary = position.x - left_boundary * 8.0
|
|
right_boundary = position.x + right_boundary * 8.0
|
|
# make facing match direction
|
|
sprite.scale.x = direction
|
|
# make animation speed sync to walk speed
|
|
sprite.speed_scale = inverse_lerp(0.0, 25.0, walk_speed)
|
|
|
|
func _physics_process(delta):
|
|
if !shooting:
|
|
# check for player in raycast
|
|
var collider = shoot_cast.get_collider()
|
|
if collider != null && collider.is_in_group("player"):
|
|
# kill player and enter shooting state temporarily
|
|
collider.get_parent().die()
|
|
shooting = true
|
|
get_tree().create_timer(0.05, false).connect("timeout", self, "_stop_shoot")
|
|
sprite.play("shoot")
|
|
# check other raycast to find collision passing through player
|
|
graphics_cast.force_raycast_update()
|
|
var hit_position = graphics_cast.to_global(Vector2(256.0, 0.0))
|
|
if graphics_cast.is_colliding():
|
|
hit_position = graphics_cast.get_collision_point()
|
|
# set line point to point of collision
|
|
var hit_x = shoot_line.to_local(hit_position).x
|
|
shoot_line.points[1].x = hit_x
|
|
# shoot_line.visible = true
|
|
# get_tree().create_timer(0.05, false).connect("timeout", shoot_line, "set_visible", [false], CONNECT_DEFERRED)
|
|
# move and play sparks
|
|
sparks.global_position = hit_position
|
|
sparks.emitting = true
|
|
return
|
|
|
|
# if there aren't turns, walk around
|
|
if turns == 0:
|
|
sprite.play("walk")
|
|
position.x += direction * walk_speed * delta
|
|
if position.x <= left_boundary:
|
|
position.x = left_boundary
|
|
direction = 1.0
|
|
_do_turn()
|
|
elif position.x >= right_boundary:
|
|
position.x = right_boundary
|
|
direction = -1.0
|
|
_do_turn()
|
|
|
|
func _do_turn():
|
|
if shooting:
|
|
get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn")
|
|
return
|
|
sprite.play("idle")
|
|
sprite.scale.x *= -1.0
|
|
if turns < idle_turns * 2:
|
|
turns += 1
|
|
get_tree().create_timer(turn_time, false).connect("timeout", self, "_do_turn")
|
|
else:
|
|
turns = 0
|
|
|
|
func _stop_shoot():
|
|
shooting = false
|
|
shoot_line.visible = false
|
|
if turns == 0:
|
|
sprite.play("walk")
|
|
else:
|
|
sprite.play("idle")
|