ld-39-jam/Player.gd

83 lines
2.4 KiB
GDScript3
Raw Normal View History

2017-07-29 22:28:46 +01:00
extends KinematicBody2D
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
onready var sprite = get_node("Sprite")
onready var camera = get_node("Camera2D")
onready var left_particles = get_node("Sprite/LeftThruster")
onready var right_particles = get_node("Sprite/RightThruster")
onready var engine_particles = get_node("Sprite/Engine")
2017-07-30 21:25:42 +01:00
onready var left_gun = get_node("Sprite/LeftGun")
onready var right_gun = get_node("Sprite/RightGun")
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
var mov_speed = 64 # pixels per second
var ship_vel = Vector2(0, 0)
var ship_max_vel = 100 # pixels per second
var ship_accel = 25 # pixels per second.. per second?
var ship_turn_speed = deg2rad(360) # degrees per second.. i guess?
2017-07-30 20:57:41 +01:00
var ship_dir = Vector2(1, 0)
2017-07-29 22:28:46 +01:00
var ship_mass = 61
2017-07-29 15:49:55 +01:00
2017-07-30 20:57:41 +01:00
var is_moving = false
2017-07-29 15:49:55 +01:00
func _ready():
set_fixed_process(true)
left_particles.set_emitting(false)
right_particles.set_emitting(false)
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
func turn_towards(delta, pos):
2017-07-29 15:49:55 +01:00
var target_dir = (pos - get_global_pos()).normalized()
2017-07-29 22:28:46 +01:00
var cpd = target_dir.dot(ship_dir.rotated(deg2rad(90)))
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
var as = asin(cpd)
var ca = clamp(as, (-ship_turn_speed) * delta, (ship_turn_speed) * delta)
left_particles.set_param(Particles2D.PARAM_LINEAR_VELOCITY, 400 * abs(ca))
right_particles.set_param(Particles2D.PARAM_LINEAR_VELOCITY, 400 * abs(ca))
2017-07-30 22:31:47 +01:00
if ca < -0.01:
left_particles.set_emitting(false)
right_particles.set_emitting(true)
elif ca > 0.01:
left_particles.set_emitting(true)
right_particles.set_emitting(false)
else:
left_particles.set_emitting(false)
right_particles.set_emitting(false)
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
ship_dir = ship_dir.rotated(-ca)
func _fixed_process(delta):
var mov_delta = Vector2(0, 0)
turn_towards(delta, get_global_mouse_pos())
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
if Input.is_action_pressed("player_move_forwards"):
2017-07-30 20:57:41 +01:00
is_moving = true
2017-07-29 22:28:46 +01:00
mov_delta += -ship_dir
elif Input.is_action_pressed("player_move_backwards"):
mov_delta += ship_dir
2017-07-30 20:57:41 +01:00
else:
is_moving = false
2017-07-30 19:46:44 +01:00
if Input.is_action_pressed("player_attack_primary"):
2017-07-30 21:49:58 +01:00
left_gun.fire(delta, ship_vel, -ship_dir)
right_gun.fire(delta, ship_vel, -ship_dir)
2017-07-30 19:46:44 +01:00
elif Input.is_action_pressed("player_attack_secondary"):
pass
2017-07-30 21:49:58 +01:00
2017-07-30 19:46:44 +01:00
if Input.is_action_pressed("player_switch_up"):
pass
elif Input.is_action_pressed("player_switch_down"):
pass
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
sprite.set_rot(ship_dir.angle())
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
var new_vel = (ship_vel + (mov_delta * (delta * ship_accel))).clamped(ship_max_vel)
var diff = new_vel - ship_vel
ship_vel += diff
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
self.move(ship_vel)
ship_vel = ship_vel / (delta * ship_mass)
2017-07-30 20:57:41 +01:00
engine_particles.set_emitting(is_moving)