You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.8 KiB
GDScript

# Extends
extends KinematicBody2D
class_name Player
enum { MOVE, CLIMB, TRANSITION }
export(Resource) var moveData
var velocity = Vector2.ZERO
var state = MOVE
onready var animatedSprite = $AnimatedSprite
onready var ladderCheck = $LadderCheck
onready var ladderCheckTwo = $LadderCheck2
func _ready():
animatedSprite.frames = load("res://PinkPlayerSkin.tres")
# Called every physics frame.
func _physics_process(delta):
var input = Vector2.ZERO
input.x = Input.get_axis("ui_left", "ui_right")
input.y = Input.get_axis("ui_up", "ui_down")
print(state)
match state:
MOVE: move_state(input)
CLIMB: climb_state(input)
TRANSITION: transition_state(input)
func is_on_ladder():
if not ladderCheck.is_colliding(): return false
var collider = ladderCheck.get_collider()
if not collider is Ladder and not collider is LadderTop: return false
return true
func is_ontop_ladder():
if ladderCheck.is_colliding():
if ladderCheck.get_collider() is LadderTop:
if not ladderCheckTwo.is_colliding():
return true
return false
func apply_gravity():
velocity.y += moveData.GRAVITY
velocity.y = min(velocity.y, 200)
func apply_friction():
velocity.x = move_toward(velocity.x, 0, moveData.FRICTION)
func apply_acceleration(amount):
velocity.x = move_toward(velocity.x, moveData.MAXIMUM_SPEED * amount, moveData.ACCELERATION)
pass
func move_state(input):
if not is_on_ladder() and not is_ontop_ladder(): state = MOVE
if is_on_ladder(): state = CLIMB
if is_ontop_ladder(): state = TRANSITION
apply_gravity()
if input.x > 0:
animatedSprite.flip_h = true
if input.x < 0:
animatedSprite.flip_h = false
if input.x == 0:
apply_friction()
animatedSprite.animation = "Idle"
else:
apply_acceleration(input.x)
animatedSprite.animation = "Run"
if is_on_floor():
if Input.is_action_just_pressed("ui_up"):
velocity.y = moveData.MAXIMUM_JUMP
else:
animatedSprite.animation = "Jump"
if Input.is_action_just_released("ui_up") and velocity.y < moveData.MINIMUM_JUMP:
velocity.y = moveData.MINIMUM_JUMP
if velocity.y > 0:
velocity.y += moveData.ADDITIONAL_FALL_GRAVITY
var was_in_air = not is_on_floor()
velocity = move_and_slide(velocity, Vector2.UP)
var just_landed = is_on_floor() and was_in_air
if just_landed:
animatedSprite.animation = "Run"
animatedSprite.frame = 1
func climb_state(input):
if not is_on_ladder() and not is_ontop_ladder(): state = MOVE
if is_on_ladder(): state = CLIMB
if is_ontop_ladder(): state = TRANSITION
velocity = input * 50
velocity = move_and_slide(velocity, Vector2.UP)
func transition_state(input):
if not is_on_ladder() and not is_ontop_ladder(): state = MOVE
if is_on_ladder(): state = CLIMB
if is_ontop_ladder(): state = TRANSITION
if not is_ontop_ladder():
velocity.y += moveData.GRAVITY
velocity.y = min(velocity.y, 200)
if input.x > 0:
animatedSprite.flip_h = true
if input.x < 0:
animatedSprite.flip_h = false
if input.x == 0:
apply_friction()
animatedSprite.animation = "Idle"
else:
apply_acceleration(input.x)
animatedSprite.animation = "Run"
if is_ontop_ladder():
if Input.is_action_just_pressed("ui_up"):
velocity.y = moveData.MAXIMUM_JUMP
else:
animatedSprite.animation = "Jump"
if Input.is_action_just_released("ui_up") and velocity.y < moveData.MINIMUM_JUMP:
velocity.y = moveData.MINIMUM_JUMP
if velocity.y > 0:
velocity.y += moveData.ADDITIONAL_FALL_GRAVITY
if Input.is_action_just_pressed("ui_down"):
var tr = ladderCheck.get_collider().get_node("CollisionShape2D").get_transform()
tr.origin = Vector2.ZERO
tr = tr.rotated(float(180))
ladderCheck.get_collider().get_node("CollisionShape2D").set_transform(tr)
velocity.y = 10
var was_in_air = not is_ontop_ladder()
velocity = move_and_slide(velocity, Vector2.UP)
var just_landed = is_ontop_ladder() and was_in_air
if just_landed:
animatedSprite.animation = "Run"
animatedSprite.frame = 1