2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
extends KinematicBody2D
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
# Member variables
|
2015-03-09 06:34:56 +01:00
|
|
|
const MAX_SPEED = 300.0
|
|
|
|
const IDLE_SPEED = 10.0
|
2015-11-21 16:13:43 +01:00
|
|
|
const ACCEL = 5.0
|
|
|
|
const VSCALE = 0.5
|
|
|
|
const SHOOT_INTERVAL = 0.3
|
|
|
|
|
|
|
|
var speed = Vector2()
|
|
|
|
var current_anim = ""
|
|
|
|
var current_mirror = false
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var shoot_countdown = 0
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
func _input(event):
|
|
|
|
if (event.type == InputEvent.MOUSE_BUTTON and event.button_index == 1 and event.pressed and shoot_countdown <= 0):
|
|
|
|
var pos = get_canvas_transform().affine_inverse()*event.pos
|
|
|
|
var dir = (pos - get_global_pos()).normalized()
|
2015-03-09 06:34:56 +01:00
|
|
|
var bullet = preload("res://shoot.scn").instance()
|
2015-11-21 16:13:43 +01:00
|
|
|
bullet.advance_dir = dir
|
|
|
|
bullet.set_pos(get_global_pos() + dir*60)
|
2015-03-09 06:34:56 +01:00
|
|
|
get_parent().add_child(bullet)
|
2015-11-21 16:13:43 +01:00
|
|
|
shoot_countdown = SHOOT_INTERVAL
|
|
|
|
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
func _fixed_process(delta):
|
2015-11-21 16:13:43 +01:00
|
|
|
shoot_countdown -= delta
|
2015-03-09 06:34:56 +01:00
|
|
|
var dir = Vector2()
|
|
|
|
if (Input.is_action_pressed("up")):
|
2015-11-21 16:13:43 +01:00
|
|
|
dir += Vector2(0, -1)
|
2015-03-09 06:34:56 +01:00
|
|
|
if (Input.is_action_pressed("down")):
|
2015-11-21 16:13:43 +01:00
|
|
|
dir += Vector2(0, 1)
|
2015-03-09 06:34:56 +01:00
|
|
|
if (Input.is_action_pressed("left")):
|
2015-11-21 16:13:43 +01:00
|
|
|
dir += Vector2(-1, 0)
|
2015-03-09 06:34:56 +01:00
|
|
|
if (Input.is_action_pressed("right")):
|
2015-11-21 16:13:43 +01:00
|
|
|
dir += Vector2(1, 0)
|
|
|
|
|
|
|
|
if (dir != Vector2()):
|
|
|
|
dir = dir.normalized()
|
|
|
|
speed = speed.linear_interpolate(dir*MAX_SPEED, delta*ACCEL)
|
|
|
|
var motion = speed*delta
|
|
|
|
motion.y *= VSCALE
|
|
|
|
motion = move(motion)
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
if (is_colliding()):
|
|
|
|
var n = get_collision_normal()
|
2015-11-21 16:13:43 +01:00
|
|
|
motion = n.slide(motion)
|
2015-03-09 06:34:56 +01:00
|
|
|
move(motion)
|
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
var next_anim = ""
|
|
|
|
var next_mirror = false
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
if (dir == Vector2() and speed.length() < IDLE_SPEED):
|
|
|
|
next_anim = "idle"
|
|
|
|
next_mirror = false
|
|
|
|
elif (speed.length() > IDLE_SPEED*0.1):
|
|
|
|
var angle = atan2(abs(speed.x), speed.y)
|
2015-03-09 06:34:56 +01:00
|
|
|
|
2015-11-21 16:13:43 +01:00
|
|
|
next_mirror = speed.x > 0
|
|
|
|
if (angle < PI/8):
|
|
|
|
next_anim = "bottom"
|
|
|
|
next_mirror = false
|
|
|
|
elif (angle < PI/4 + PI/8):
|
|
|
|
next_anim = "bottom_left"
|
|
|
|
elif (angle < PI*2/4 + PI/8):
|
|
|
|
next_anim = "left"
|
|
|
|
elif (angle < PI*3/4 + PI/8):
|
|
|
|
next_anim = "top_left"
|
2015-03-09 06:34:56 +01:00
|
|
|
else:
|
2015-11-21 16:13:43 +01:00
|
|
|
next_anim = "top"
|
|
|
|
next_mirror = false
|
|
|
|
|
|
|
|
if (next_anim != current_anim or next_mirror != current_mirror):
|
2015-03-09 06:34:56 +01:00
|
|
|
get_node("frames").set_flip_h(next_mirror)
|
|
|
|
get_node("anim").play(next_anim)
|
2015-11-21 16:13:43 +01:00
|
|
|
current_anim = next_anim
|
|
|
|
current_mirror = next_mirror
|
2015-03-09 06:34:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
set_fixed_process(true)
|
|
|
|
set_process_input(true)
|