2017-08-12 17:02:52 +01:00
|
|
|
extends KinematicBody2D
|
|
|
|
|
|
|
|
const km = preload("Kinematic.gd")
|
|
|
|
var MissileImpact = load("res://MissileImpact.tscn")
|
|
|
|
|
2017-08-12 18:45:11 +01:00
|
|
|
onready var sprite = get_node("Sprite")
|
|
|
|
onready var ps = get_node("Particles2D")
|
2017-08-12 19:44:05 +01:00
|
|
|
onready var cs = get_node("CollisionShape2D")
|
2017-08-12 18:45:11 +01:00
|
|
|
|
2017-08-12 17:02:52 +01:00
|
|
|
var missile_max_speed = 384 # pixels per second
|
|
|
|
var missile_arrive_radius = 64
|
|
|
|
var missile_arrive_speed = 1
|
|
|
|
var missile_rot_speed = deg2rad(22.5) # degrees per second
|
|
|
|
var missile_dmg = 100
|
|
|
|
|
|
|
|
var cur_kinematic
|
|
|
|
var steering
|
|
|
|
var s_seek
|
|
|
|
var s_arrive
|
|
|
|
|
2017-08-12 18:45:11 +01:00
|
|
|
# is dead?
|
|
|
|
var is_dead = false
|
|
|
|
|
|
|
|
# time to die
|
|
|
|
var death_time = 1
|
|
|
|
|
|
|
|
# current timer
|
|
|
|
var death_timer = 0
|
|
|
|
|
2017-08-12 17:02:52 +01:00
|
|
|
func _ready():
|
|
|
|
set_fixed_process(true)
|
|
|
|
|
|
|
|
cur_kinematic = km.Kinematic.new(self, missile_max_speed)
|
|
|
|
steering = km.Steering.new()
|
|
|
|
|
|
|
|
s_seek = km.Seek.new(self)
|
|
|
|
s_arrive = km.Arrive.new(self, missile_arrive_radius, missile_arrive_speed)
|
|
|
|
|
|
|
|
var player = get_tree().get_root().get_node("Game/Player")
|
|
|
|
s_seek.set_target(player)
|
|
|
|
s_arrive.set_target(player)
|
|
|
|
|
2017-08-13 00:32:41 +01:00
|
|
|
player.on_enemy_missile_lock(self)
|
|
|
|
|
2017-08-12 18:45:11 +01:00
|
|
|
# base death timer on lifetime
|
|
|
|
death_time = ps.get_lifetime()
|
|
|
|
|
2017-08-13 00:32:41 +01:00
|
|
|
func set_velocity(vel):
|
|
|
|
cur_kinematic.velocity = vel
|
|
|
|
|
2017-08-12 18:23:11 +01:00
|
|
|
func on_impact(e, is_bullet):
|
2017-08-12 17:02:52 +01:00
|
|
|
|
|
|
|
# create bullet impact
|
|
|
|
var new_mi = MissileImpact.instance()
|
|
|
|
# e.add_child(new_mi)
|
|
|
|
get_tree().get_root().add_child(new_mi)
|
|
|
|
#new_bi.set_global_pos(get_collision_pos() + velocity/2)
|
|
|
|
new_mi.set_scale(Vector2(1.5, 1.5)) # when missile, larger effect
|
|
|
|
new_mi.set_global_pos(get_collision_pos())
|
|
|
|
|
2017-08-12 18:23:11 +01:00
|
|
|
if not is_bullet:
|
|
|
|
# align along "hit normal, the lazy way"
|
|
|
|
new_mi.set_global_rot((-get_collision_normal()).angle())
|
|
|
|
else:
|
|
|
|
new_mi.set_global_rot(get_global_rot())
|
|
|
|
new_mi.rotate(deg2rad(180))
|
2017-08-12 17:02:52 +01:00
|
|
|
|
|
|
|
# damage and free
|
|
|
|
e.do_damage(missile_dmg)
|
2017-08-12 18:45:11 +01:00
|
|
|
|
|
|
|
# hide missile, keep particles until dedness
|
|
|
|
queue_death()
|
|
|
|
|
|
|
|
func queue_death():
|
|
|
|
sprite.hide()
|
|
|
|
ps.set_emitting(false)
|
2017-08-12 19:44:05 +01:00
|
|
|
cs.queue_free() # kill it
|
2017-08-13 00:43:46 +01:00
|
|
|
get_tree().get_root().get_node("Game/Player").on_enemy_missile_lock_lost(self)
|
2017-08-12 18:45:11 +01:00
|
|
|
is_dead = true
|
2017-08-12 17:02:52 +01:00
|
|
|
|
|
|
|
func _fixed_process(delta):
|
2017-08-12 18:45:11 +01:00
|
|
|
|
|
|
|
if is_dead:
|
|
|
|
death_timer += delta
|
|
|
|
if death_timer >= death_time:
|
|
|
|
queue_free()
|
|
|
|
return
|
|
|
|
|
2017-08-12 17:02:52 +01:00
|
|
|
s_seek.get_steering(steering)
|
|
|
|
s_arrive.get_steering(steering)
|
|
|
|
cur_kinematic.update(steering, delta)
|
|
|
|
|
|
|
|
if is_colliding():
|
|
|
|
var e = get_collider()
|
2017-08-12 18:23:11 +01:00
|
|
|
var e_t = e.type()
|
|
|
|
if e_t == "Bullet":
|
|
|
|
on_impact(e, true)
|
|
|
|
elif e_t != "Missile":
|
|
|
|
on_impact(e, false)
|
2017-08-12 17:02:52 +01:00
|
|
|
|
|
|
|
func type():
|
|
|
|
return "Missile"
|