166 lines
5.2 KiB
GDScript
166 lines
5.2 KiB
GDScript
tool
|
|
extends "res://objects/enemy/enemy.gd"
|
|
|
|
export var walk_speed = 25.0
|
|
export var left_boundary = 0.0 setget _set_left_boundary
|
|
export var right_boundary = 0.0 setget _set_right_boundary
|
|
export var direction = 1.0
|
|
export var idle_turns = 0
|
|
export var turn_time = 0.5
|
|
export var shoot_time = 1.0
|
|
|
|
export var bones_amount = 5
|
|
export var bones_speed = 50.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
|
|
onready var muzzle_flash = $"%MuzzleFlashParticles"
|
|
|
|
var shooting = false
|
|
var turns = 0
|
|
|
|
const BoneParticle = preload("res://objects/enemy/bone_particle.tscn")
|
|
const EasyBullet = preload("res://objects/enemy/roboturret_proj.tscn")
|
|
|
|
func _ready():
|
|
if Engine.editor_hint:
|
|
update()
|
|
return
|
|
# 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 Engine.editor_hint:
|
|
return
|
|
if !shooting:
|
|
# check for player in raycast
|
|
var collider = shoot_cast.get_collider()
|
|
if collider != null && (collider.is_in_group("player") or collider.is_in_group("msx")):
|
|
if Game.get_sector(global_position) == Game.get_sector(collider.global_position):
|
|
if Game.is_easy_mode:
|
|
var easy_bullet = EasyBullet.instance()
|
|
easy_bullet.target_group = "player"
|
|
easy_bullet.direction = sign(shoot_position.global_position.x - global_position.x)
|
|
easy_bullet.global_position = shoot_position.global_position
|
|
easy_bullet.speed = 100
|
|
get_parent().call_deferred("add_child", easy_bullet)
|
|
else:
|
|
# kill player and enter shooting state temporarily
|
|
collider.get_parent().die()
|
|
# play sound
|
|
Audio.play_sound(Audio.a_die_robot, Audio.ac_boss)
|
|
# muzzle flash
|
|
muzzle_flash.emitting = true
|
|
shooting = true
|
|
get_tree().create_timer(0.5, 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")
|
|
# still pause with zero "turns"
|
|
if idle_turns <= 0:
|
|
# set turns to -1 to stop walking
|
|
turns = -1
|
|
# wait for turn_tie
|
|
yield(get_tree().create_timer(turn_time, false), "timeout")
|
|
sprite.scale.x *= -1.0
|
|
# wait a bit after turning
|
|
get_tree().create_timer(turn_time, false).connect("timeout", self, "set", ["turns", 0])
|
|
return
|
|
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:
|
|
get_tree().create_timer(turn_time, false).connect("timeout", self, "set", ["turns", 0])
|
|
# turns = 0
|
|
|
|
func _stop_shoot():
|
|
shooting = false
|
|
shoot_line.visible = false
|
|
if turns == 0:
|
|
sprite.play("walk")
|
|
else:
|
|
sprite.play("idle")
|
|
|
|
# editor debug drawing
|
|
func _draw():
|
|
if Engine.editor_hint:
|
|
var left = -left_boundary * 8.0
|
|
var right = right_boundary * 8.0 + 8.0
|
|
draw_line(
|
|
Vector2(left, 0.0),
|
|
Vector2(right, 0.0),
|
|
Color(0.4, 0.2, 0.6, 0.75), 1.01, false
|
|
)
|
|
draw_line(
|
|
Vector2(left, 2.0),
|
|
Vector2(left, -2.0),
|
|
Color(0.4, 0.2, 0.6, 0.75), 1.01, false
|
|
)
|
|
draw_line(
|
|
Vector2(right, 2.0),
|
|
Vector2(right, -2.0),
|
|
Color(0.4, 0.2, 0.6, 0.75), 1.01, false
|
|
)
|
|
|
|
func _set_left_boundary(value):
|
|
left_boundary = value
|
|
if Engine.editor_hint:
|
|
update()
|
|
|
|
func _set_right_boundary(value):
|
|
right_boundary = value
|
|
if Engine.editor_hint:
|
|
update()
|
|
|
|
func die():
|
|
for n in bones_amount:
|
|
var bone = BoneParticle.instance()
|
|
bone.rotation = rand_range(0.0, 2.0*PI) # random rotation
|
|
bone.global_position = global_position
|
|
bone.linear_velocity = Vector2(bones_speed * rand_range(0.5, 1.5), 0.0).rotated(rand_range(-PI, 0.0))
|
|
bone.angular_velocity = rand_range(-PI*2.0, PI*2.0)
|
|
var timer = get_tree().create_timer(3.0 + rand_range(0.0, 1.0), false)
|
|
timer.connect("timeout", bone, "queue_free")
|
|
get_parent().call_deferred("add_child", bone)
|
|
.die() # call rest of enemy.gd die() funciton
|