phs-galaxy/Player.gd

60 lines
1.7 KiB
GDScript

extends KinematicBody
var acceleration := Vector3(0.0, -9.81, 0.0)
var velocity := Vector3(0.0, 0.0, 0.0)
var move_speed = 5.0
var jump_accel = 300.0
export(NodePath) var solar_system
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
var move_velocity := Vector3.ZERO
var move_acceleration := Vector3.ZERO
# Movement and rotation
if Input.is_action_pressed("ui_up"):
move_velocity.z -= move_speed
if Input.is_action_pressed("ui_down"):
move_velocity.z += move_speed
if Input.is_action_pressed("ui_left"):
rotate(transform.basis.y, delta)
if Input.is_action_pressed("ui_right"):
rotate(transform.basis.y, -delta)
# Make movement local
move_velocity = transform.basis * move_velocity
# Jumping
if Input.is_action_just_pressed("ui_select"):
move_acceleration.y += jump_accel
# Gravity
var gravity_acceleration = get_node(solar_system).get_gravitation_acceleration(transform.origin)
velocity += (gravity_acceleration * 9.81 - gravity_acceleration * move_acceleration.y) * delta
var movement = (move_velocity + velocity) * delta
# Apply movement to position
var collision = move_and_collide(movement)
# Reset the velocity if the body is colliding
if collision:
velocity = Vector3.ZERO
# Rotate down vector to face center of gravity
var down = gravity_acceleration
var local_down = transform.basis * Vector3.DOWN
var angle = local_down.angle_to(down)
var axis = local_down.cross(down).normalized()
if axis != Vector3.ZERO: # Happens if we're perfectly aligned already (local_down and down are equal)
rotate(axis, angle)