Skip to content

Instantly share code, notes, and snippets.

@Razzlegames
Last active August 29, 2015 14:18
Show Gist options
  • Save Razzlegames/7b7337fb3c98aaf979a1 to your computer and use it in GitHub Desktop.
Save Razzlegames/7b7337fb3c98aaf979a1 to your computer and use it in GitHub Desktop.
# The ship is from: <div xmlns:cc="http://creativecommons.org/ns#" about="http://millionthvector.blogspot.com/p/free-sprites.html"><a rel="cc:attributionURL" property="cc:attributionName" href="http://millionthvector.blogspot.com/p/free-sprites.html">MillionthVector</a> / <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div>
# The Background is from: http://www.jpl.nasa.gov/spaceimages/details.php?id=PIA16883
extends Node2D
export var ship_speed = 100
export var acceleration = 10
var current_speed = Vector2(0,0)
func _ready():
# Initalization here
set_fixed_process(true)
func move(speed_x, speed_y, acc, delta):
current_speed.x = lerp(current_speed.x, speed_x, acc * delta)
current_speed.y = lerp(current_speed.y, speed_y, acc * delta)
get_node("KinematicBody2D").move(Vector2(current_speed.x, current_speed.y))
func _fixed_process(delta):
var ship = get_node("KinematicBody2D")
# Check for key input and move ship
if (Input.is_action_pressed("ui_up")):
move(current_speed.x, -ship_speed, acceleration, delta)
elif (Input.is_action_pressed("ui_down")):
move(current_speed.x, ship_speed, acceleration, delta)
elif (Input.is_action_pressed("ui_left")):
move(-ship_speed, current_speed.y, acceleration, delta)
elif (Input.is_action_pressed("ui_right")):
move(ship_speed, current_speed.y, acceleration, delta)
else:
move(0, 0, acceleration, delta)
if (ship.is_colliding()):
var n = ship.get_collision_normal()
print("Collision Normal: " + str(n))
current_speed = n.slide( current_speed )
print("Slide value: " + str(current_speed))
# Move the ship away from the collision slightly to avoid penetrating
ship.move( current_speed + n*10)
print("Position: " + str(ship.get_pos()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment