Player movement and animation
This commit is contained in:
parent
f456725cf2
commit
0dd3f24030
9 changed files with 700 additions and 38 deletions
|
@ -1,5 +1,7 @@
|
|||
extends KinematicBody2D
|
||||
|
||||
const DustEffect = preload("res://DustEffect.tscn")
|
||||
|
||||
export (int) var acceleration = 512
|
||||
export (int) var max_speed = 64
|
||||
export (float) var friction = 0.25
|
||||
|
@ -9,11 +11,14 @@ export (int) var max_slope = 46
|
|||
|
||||
var motion = Vector2.ZERO
|
||||
var snap_vector = Vector2.ZERO
|
||||
var just_jumped= false
|
||||
|
||||
onready var sprite = $Sprite
|
||||
onready var animation = $Animation
|
||||
onready var coyoteJumpTimer = $CoyoteJumpTimer
|
||||
|
||||
func _physics_process(delta):
|
||||
just_jumped = false
|
||||
var input_vector = get_input_vector()
|
||||
apply_horizontal_force(input_vector, delta)
|
||||
apply_friction(input_vector)
|
||||
|
@ -23,6 +28,13 @@ func _physics_process(delta):
|
|||
update_animations(input_vector)
|
||||
move()
|
||||
|
||||
func create_dust_effect():
|
||||
var dustPosition = global_position
|
||||
dustPosition.x += rand_range(-4, 4)
|
||||
var dustEffect = DustEffect.instance()
|
||||
dustEffect.global_position = dustPosition
|
||||
get_tree().current_scene.add_child(dustEffect)
|
||||
|
||||
func get_input_vector():
|
||||
var input_vector = Vector2.ZERO
|
||||
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
|
||||
|
@ -42,17 +54,19 @@ func update_snap_vector():
|
|||
snap_vector = Vector2.DOWN
|
||||
|
||||
func jump_check():
|
||||
if is_on_floor():
|
||||
if is_on_floor() or coyoteJumpTimer.time_left > 0:
|
||||
if Input.is_action_just_pressed("ui_select"):
|
||||
motion.y = -jump_force
|
||||
snap_vector = Vector2.ZERO
|
||||
just_jumped = true
|
||||
else:
|
||||
if Input.is_action_just_released("ui_select"):# and motion.y < -jump_force/2:
|
||||
if Input.is_action_just_released("ui_select") and motion.y < -jump_force/2:
|
||||
motion.y = motion.y/2
|
||||
|
||||
func apply_gravity(delta):
|
||||
motion.y += gravity * delta
|
||||
motion.y = min(motion.y, jump_force)
|
||||
if not is_on_floor():
|
||||
motion.y += gravity * delta
|
||||
motion.y = min(motion.y, jump_force)
|
||||
|
||||
func update_animations(input_vector):
|
||||
if input_vector.x != 0:
|
||||
|
@ -66,5 +80,28 @@ func update_animations(input_vector):
|
|||
animation.play("Jump")
|
||||
|
||||
func move():
|
||||
var was_on_air = not is_on_floor()
|
||||
var was_on_floor = is_on_floor()
|
||||
var last_position = position
|
||||
var last_motion = motion
|
||||
|
||||
motion = move_and_slide_with_snap(motion, snap_vector * 4, Vector2.UP, true, 4, deg2rad(max_slope), false)
|
||||
|
||||
# Just landed
|
||||
if was_on_air and is_on_floor():
|
||||
# Keep previous momentum when landing on slopes
|
||||
motion.x = last_motion.x
|
||||
create_dust_effect()
|
||||
|
||||
# Just left ground
|
||||
if was_on_floor and not is_on_floor() and not just_jumped:
|
||||
# Fixes "gap" when jumping off a cliff [TODO]
|
||||
motion.y = 0
|
||||
position.y = last_position.y
|
||||
coyoteJumpTimer.start()
|
||||
|
||||
# Prevent Sliding (hack) [NOT WORKING]
|
||||
# If we are on floor, the floor isn't moving and our motion is really tiny
|
||||
if is_on_floor() and get_floor_velocity().length() == 0 and abs(motion.x) < 1:
|
||||
position.x = last_position.x
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue