2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
extends RigidBody2D
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Member variables
|
2014-02-10 01:10:30 +00:00
|
|
|
const STATE_WALKING = 0
|
|
|
|
const STATE_DYING = 1
|
|
|
|
|
|
|
|
var state = STATE_WALKING
|
|
|
|
|
|
|
|
var direction = -1
|
2015-11-21 15:13:43 +00:00
|
|
|
var anim = ""
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var rc_left = null
|
|
|
|
var rc_right = null
|
2014-02-10 01:10:30 +00:00
|
|
|
var WALK_SPEED = 50
|
|
|
|
|
|
|
|
var bullet_class = preload("res://bullet.gd")
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
func _die():
|
|
|
|
queue_free()
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
func _pre_explode():
|
2015-11-21 15:13:43 +00:00
|
|
|
# Stay there
|
2014-02-10 01:10:30 +00:00
|
|
|
clear_shapes()
|
|
|
|
set_mode(MODE_STATIC)
|
|
|
|
get_node("sound").play("explode")
|
|
|
|
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
func _integrate_forces(s):
|
2014-02-10 01:10:30 +00:00
|
|
|
var lv = s.get_linear_velocity()
|
2015-11-21 15:13:43 +00:00
|
|
|
var new_anim = anim
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
if (state == STATE_DYING):
|
|
|
|
new_anim = "explode"
|
|
|
|
elif (state == STATE_WALKING):
|
|
|
|
new_anim = "walk"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var wall_side = 0.0
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
for i in range(s.get_contact_count()):
|
|
|
|
var cc = s.get_contact_collider_object(i)
|
|
|
|
var dp = s.get_contact_local_normal(i)
|
|
|
|
|
|
|
|
if (cc):
|
|
|
|
if (cc extends bullet_class and not cc.disabled):
|
|
|
|
set_mode(MODE_RIGID)
|
2015-11-21 15:13:43 +00:00
|
|
|
state = STATE_DYING
|
|
|
|
#lv = s.get_contact_local_normal(i)*400
|
2014-02-10 01:10:30 +00:00
|
|
|
s.set_angular_velocity(sign(dp.x)*33.0)
|
2015-05-04 21:30:57 +00:00
|
|
|
set_friction(1)
|
2014-02-10 01:10:30 +00:00
|
|
|
cc.disable()
|
|
|
|
get_node("sound").play("hit")
|
|
|
|
break
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
if (dp.x > 0.9):
|
|
|
|
wall_side = 1.0
|
|
|
|
elif (dp.x < -0.9):
|
|
|
|
wall_side = -1.0
|
|
|
|
|
|
|
|
if (wall_side != 0 and wall_side != direction):
|
|
|
|
direction = -direction
|
|
|
|
get_node("sprite").set_scale(Vector2(-direction, 1))
|
|
|
|
if (direction < 0 and not rc_left.is_colliding() and rc_right.is_colliding()):
|
|
|
|
direction = -direction
|
|
|
|
get_node("sprite").set_scale(Vector2(-direction, 1))
|
|
|
|
elif (direction > 0 and not rc_right.is_colliding() and rc_left.is_colliding()):
|
|
|
|
direction = -direction
|
|
|
|
get_node("sprite").set_scale(Vector2(-direction, 1))
|
|
|
|
|
|
|
|
lv.x = direction*WALK_SPEED
|
|
|
|
|
|
|
|
if(anim != new_anim):
|
|
|
|
anim = new_anim
|
2014-02-10 01:10:30 +00:00
|
|
|
get_node("anim").play(anim)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
s.set_linear_velocity(lv)
|
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
2015-11-21 15:13:43 +00:00
|
|
|
rc_left = get_node("raycast_left")
|
|
|
|
rc_right = get_node("raycast_right")
|