ld-39-jam/EnemyGrunt.gd

90 lines
2.1 KiB
GDScript3
Raw Normal View History

2017-07-30 20:57:41 +01:00
extends KinematicBody2D
onready var gun = get_node("Ship/Gun")
2017-08-13 00:32:41 +01:00
onready var launcher = get_node("Ship/MissileLauncher")
2017-08-15 13:47:41 +01:00
onready var firing_area = get_node("FiringCone")
onready var avoid_area = get_node("AvoidCone")
2017-08-12 17:02:52 +01:00
var ExplosionEffect = load("res://ExplosionEffect.tscn")
const km = preload("Kinematic.gd")
2017-08-10 23:26:45 +01:00
var grunt_max_speed = 256 # pixels per second
var grunt_arrive_radius = 64
2017-08-10 20:17:39 +01:00
var grunt_arrive_speed = 1
2017-07-31 17:23:46 +01:00
var grunt_rot_speed = deg2rad(22.5) # degrees per second
2017-08-12 17:02:52 +01:00
var health = 100
var cur_kinematic
2017-08-10 20:17:39 +01:00
var steering
var s_seek
var s_arrive
2017-08-15 13:47:41 +01:00
var s_avoid
2017-07-31 17:23:46 +01:00
var player_in_cone = false
2017-07-30 20:57:41 +01:00
func _ready():
var t = get_tree().get_root().get_node("Game/MinimapControl").register_entity(self)
set_fixed_process(true)
gun.set_owner(type())
cur_kinematic = km.Kinematic.new(self, grunt_max_speed)
2017-08-10 20:17:39 +01:00
steering = km.Steering.new()
s_seek = km.Seek.new(self)
2017-08-10 20:17:39 +01:00
s_arrive = km.Arrive.new(self, grunt_arrive_radius, grunt_arrive_speed)
2017-08-15 13:47:41 +01:00
s_avoid = km.Avoid.new(self, avoid_area)
var player = get_tree().get_root().get_node("Game/Player")
s_seek.set_target(player)
s_arrive.set_target(player)
2017-08-12 15:45:22 +01:00
# firing cone!
2017-08-15 13:47:41 +01:00
firing_area.connect("body_enter", self, "on_body_enter_firing_cone")
firing_area.connect("body_exit", self, "on_body_exit_firing_cone")
2017-08-15 13:47:41 +01:00
func on_body_enter_firing_cone(b):
if b.type() == "Player":
player_in_cone = true
2017-08-15 13:47:41 +01:00
func on_body_exit_firing_cone(b):
if b.type() == "Player":
player_in_cone = false
2017-08-12 17:02:52 +01:00
func on_explode():
# create explosion effect
var new_ee = ExplosionEffect.instance()
get_tree().get_root().add_child(new_ee)
new_ee.set_global_pos(get_global_pos())
queue_free()
2017-08-12 15:45:22 +01:00
func do_damage(v):
2017-08-12 17:02:52 +01:00
health -= v
2017-07-31 17:23:46 +01:00
2017-07-30 20:57:41 +01:00
func type():
2017-08-12 15:45:22 +01:00
return "Enemy"
2017-07-31 17:23:46 +01:00
2017-08-15 13:47:41 +01:00
func get_kinematic_position():
return get_global_pos() + cur_kinematic.velocity
func _fixed_process(delta):
2017-08-15 13:47:41 +01:00
2017-08-10 20:17:39 +01:00
s_seek.get_steering(steering)
s_arrive.get_steering(steering)
2017-08-15 13:47:41 +01:00
s_avoid.get_steering(steering)
2017-08-10 20:17:39 +01:00
cur_kinematic.update(steering, delta)
2017-08-13 00:32:41 +01:00
# pass on velocity
launcher.set_velocity(cur_kinematic.velocity)
if player_in_cone:
gun.fire(delta, cur_kinematic.velocity * delta, cur_kinematic.get_orientation())
2017-08-12 17:02:52 +01:00
if health <= 0:
on_explode()
2017-07-31 17:23:46 +01:00
func _process(delta):
pass