Skip to content

Instantly share code, notes, and snippets.

@jedStevens
Created April 21, 2018 17:36
Show Gist options
  • Save jedStevens/640b787d14449ebbd1eba3b4b4caf3d2 to your computer and use it in GitHub Desktop.
Save jedStevens/640b787d14449ebbd1eba3b4b4caf3d2 to your computer and use it in GitHub Desktop.
LD41 Player
extends KinematicBody2D
const GRAVITY = 800.0
const PUSH_SPEED = 200
const MAX_SPEED = 500.0
const MIN_OLLIE_FORCE = 200.0
const MAX_OLLIE_FORCE = 350.0
const OLLIE_CHARGE_RATE = 600.0
var OLLIE_FORCE = 0.0
var air_time = 0.0
var velocity = Vector2()
var on_rail = false
var manual = false
var manual_attempt = false
var checkpoint = Vector2()
var trick = ""
var alphabet = "WESDXC"
var collect_word = "SKATE"
var states = {}
var current_state = "idle"
var trick_map = { "SC" : "kickflip", "SE" : "heelflip" , "SX" : "shuv"}
#, "SX" : "shuv-bs", "SW" : "shuv-fs" }
var anim_lock = false
func give_points(description, amount):
# Shows a floating ui text of the points and some txt
var floating_text = preload("res://floating_text.tscn").instance()
get_parent().add_child(floating_text)
floating_text.global_position = global_position
floating_text.get_node("Label").text = "+"+str(amount)+" "+description
func pickup(obj):
print("I collected: ", obj)
var letter = obj.get("letter")
if letter != null:
if letter in collect_word:
$Camera2D/ui/letters.text[collect_word.find(letter)] = letter
give_points("SECRET LETTER", 100)
obj.queue_free()
func _ready():
checkpoint = global_position
set_process_input(true)
$Sprite.connect("animation_finished", self, "unlock_anim")
func _input(event):
if event is InputEventKey:
if event.pressed:
if event.as_text() in alphabet:
trick = trick+event.as_text()
func unlock_anim():
anim_lock = false
print("unlocked")
func respawn():
global_position = checkpoint
velocity = Vector2()
anim_lock = false
func _physics_process(delta):
#self.call(states[current_state], delta)
if air_time > 3.5:
air_time = 0.0
respawn()
if Input.is_action_just_pressed("respawn"):
respawn()
velocity.y += delta * GRAVITY
var friction = 0.998
var can_ollie = is_on_floor() or air_time == 0
var can_push = can_ollie and not on_rail
if Input.is_action_pressed("ollie") and can_ollie:
OLLIE_FORCE += OLLIE_CHARGE_RATE * delta
OLLIE_FORCE = min(MAX_OLLIE_FORCE, OLLIE_FORCE)
if Input.is_action_just_released("ollie") and can_ollie:
OLLIE_FORCE = min(MAX_OLLIE_FORCE, OLLIE_FORCE + MIN_OLLIE_FORCE)
velocity.y = -(OLLIE_FORCE)
OLLIE_FORCE = 0.0
manual= false
manual_attempt = false
if trick in trick_map.keys():
$Sprite.play(trick_map[trick])
anim_lock = true
print(trick_map[trick])
trick = ""
$"ollie-o-meter".value = (OLLIE_FORCE / MAX_OLLIE_FORCE) * 100
if Input.is_key_pressed(KEY_Q) and ((not is_on_floor()) or air_time > 0) and not $anim.is_playing():
$anim.play("back_flip")
if Input.is_key_pressed(KEY_D) and air_time > 0:
manual_attempt = true
if Input.is_action_just_released("push")and can_push and not manual:
velocity.x += PUSH_SPEED
velocity.x = min(velocity.x, MAX_SPEED)
velocity = move_and_slide(velocity, Vector2(0, -1))
air_time += delta
if is_on_floor():
if manual_attempt:
manual = true
manual_attempt = false
if $anim.current_animation == "back_flip":
respawn()
air_time = 0
else:
if manual:
manual= false
manual_attempt = false
if get_slide_count() == 0:
on_rail = false
for i in range(get_slide_count()):
var col = get_slide_collision(i)
if col.collider.get_collision_layer_bit(1):
on_rail = true
friction = 1
break
elif abs(col.normal.angle_to(Vector2(0,-1))) > deg2rad(25):
$anim.play("bail")
velocity = Vector2()
anim_lock = true
velocity.x *= friction
if not anim_lock:
if on_rail:
$Sprite.play("grind")
elif not is_on_floor():
if manual or manual_attempt:
$Sprite.play("manual")
elif velocity.y > 0:
$Sprite.play("fall")
else:
$Sprite.play("air")
else:
if manual or manual_attempt:
$Sprite.play("manual")
else:
$Sprite.play("ride")
func idle_state(states, delta):
pass
func walking_state(states, delta):
pass
func riding_state(states, delta):
pass
func jumping_state(states, delta):
pass
func crouching_state(states, delta):
pass
func trick_state(states, delta):
pass
func air_state(states, delta):
pass
func manual_state(states, delta):
pass
func grab_state(states, delta):
pass
func grind_state(states, delta):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment