ld-39-jam/Player.gd

46 lines
1.2 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")
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?
var ship_mass = 61
2017-07-29 15:49:55 +01:00
2017-07-29 22:28:46 +01:00
var ship_dir = Vector2(1, 0)
2017-07-29 15:49:55 +01:00
func _ready():
set_fixed_process(true)
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)
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"):
mov_delta += -ship_dir
elif Input.is_action_pressed("player_move_backwards"):
mov_delta += ship_dir
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)