2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
extends KinematicBody2D
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# This is a simple collision demo showing how
|
|
|
|
# the kinematic controller works.
|
|
|
|
# move() will allow to move the node, and will
|
|
|
|
# always move it to a non-colliding spot,
|
|
|
|
# as long as it starts from a non-colliding spot too.
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Member variables
|
|
|
|
const GRAVITY = 500.0 # Pixels/second
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Angle in degrees towards either side that the player can consider "floor"
|
2014-02-22 23:28:19 +00:00
|
|
|
const FLOOR_ANGLE_TOLERANCE = 40
|
|
|
|
const WALK_FORCE = 600
|
2015-11-21 15:13:43 +00:00
|
|
|
const WALK_MIN_SPEED = 10
|
2014-02-22 23:28:19 +00:00
|
|
|
const WALK_MAX_SPEED = 200
|
|
|
|
const STOP_FORCE = 1300
|
|
|
|
const JUMP_SPEED = 200
|
2015-11-21 15:13:43 +00:00
|
|
|
const JUMP_MAX_AIRBORNE_TIME = 0.2
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
const SLIDE_STOP_VELOCITY = 1.0 # One pixel per second
|
|
|
|
const SLIDE_STOP_MIN_TRAVEL = 1.0 # One pixel
|
2015-05-04 13:17:24 +00:00
|
|
|
|
2014-02-22 23:28:19 +00:00
|
|
|
var velocity = Vector2()
|
2015-11-21 15:13:43 +00:00
|
|
|
var on_air_time = 100
|
|
|
|
var jumping = false
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
var prev_jump_pressed = false
|
2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
func _fixed_process(delta):
|
|
|
|
# Create forces
|
|
|
|
var force = Vector2(0, GRAVITY)
|
2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
var walk_left = Input.is_action_pressed("move_left")
|
|
|
|
var walk_right = Input.is_action_pressed("move_right")
|
|
|
|
var jump = Input.is_action_pressed("jump")
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
var stop = true
|
2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
if (walk_left):
|
2015-11-21 15:13:43 +00:00
|
|
|
if (velocity.x <= WALK_MIN_SPEED and velocity.x > -WALK_MAX_SPEED):
|
|
|
|
force.x -= WALK_FORCE
|
|
|
|
stop = false
|
2014-02-22 23:28:19 +00:00
|
|
|
elif (walk_right):
|
2015-11-21 15:13:43 +00:00
|
|
|
if (velocity.x >= -WALK_MIN_SPEED and velocity.x < WALK_MAX_SPEED):
|
|
|
|
force.x += WALK_FORCE
|
|
|
|
stop = false
|
2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
if (stop):
|
|
|
|
var vsign = sign(velocity.x)
|
|
|
|
var vlen = abs(velocity.x)
|
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
vlen -= STOP_FORCE*delta
|
|
|
|
if (vlen < 0):
|
|
|
|
vlen = 0
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
velocity.x = vlen*vsign
|
2014-02-22 23:28:19 +00:00
|
|
|
|
2015-11-21 15:13:43 +00:00
|
|
|
# Integrate forces to velocity
|
|
|
|
velocity += force*delta
|
|
|
|
|
|
|
|
# Integrate velocity into motion and move
|
|
|
|
var motion = velocity*delta
|
|
|
|
|
|
|
|
# Move and consume motion
|
2014-02-22 23:28:19 +00:00
|
|
|
motion = move(motion)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
var floor_velocity = Vector2()
|
|
|
|
|
2014-02-22 23:28:19 +00:00
|
|
|
if (is_colliding()):
|
2015-11-21 15:13:43 +00:00
|
|
|
# You can check which tile was collision against with this
|
2014-10-16 03:06:34 +00:00
|
|
|
# print(get_collider_metadata())
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
# Ran against something, is it the floor? Get normal
|
2014-02-22 23:28:19 +00:00
|
|
|
var n = get_collision_normal()
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
if (rad2deg(acos(n.dot(Vector2(0, -1)))) < FLOOR_ANGLE_TOLERANCE):
|
|
|
|
# If angle to the "up" vectors is < angle tolerance
|
|
|
|
# char is on floor
|
|
|
|
on_air_time = 0
|
|
|
|
floor_velocity = get_collider_velocity()
|
|
|
|
|
|
|
|
if (on_air_time == 0 and force.x == 0 and get_travel().length() < SLIDE_STOP_MIN_TRAVEL and abs(velocity.x) < SLIDE_STOP_VELOCITY and get_collider_velocity() == Vector2()):
|
|
|
|
# Since this formula will always slide the character around,
|
|
|
|
# a special case must be considered to to stop it from moving
|
|
|
|
# if standing on an inclined floor. Conditions are:
|
|
|
|
# 1) Standing on floor (on_air_time == 0)
|
2015-05-04 01:37:10 +00:00
|
|
|
# 2) Did not move more than one pixel (get_travel().length() < SLIDE_STOP_MIN_TRAVEL)
|
|
|
|
# 3) Not moving horizontally (abs(velocity.x) < SLIDE_STOP_VELOCITY)
|
|
|
|
# 4) Collider is not moving
|
2015-11-21 15:13:43 +00:00
|
|
|
|
2015-05-04 01:37:10 +00:00
|
|
|
revert_motion()
|
2015-11-21 15:13:43 +00:00
|
|
|
velocity.y = 0.0
|
2015-05-04 01:37:10 +00:00
|
|
|
else:
|
2015-11-21 15:13:43 +00:00
|
|
|
# For every other case of motion, our motion was interrupted.
|
|
|
|
# Try to complete the motion by "sliding" by the normal
|
2015-05-04 01:37:10 +00:00
|
|
|
motion = n.slide(motion)
|
2015-11-21 15:13:43 +00:00
|
|
|
velocity = n.slide(velocity)
|
|
|
|
# Then move again
|
2015-05-04 01:37:10 +00:00
|
|
|
move(motion)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
if (floor_velocity != Vector2()):
|
|
|
|
# If floor moves, move with floor
|
2014-02-22 23:28:19 +00:00
|
|
|
move(floor_velocity*delta)
|
2015-11-21 15:13:43 +00:00
|
|
|
|
|
|
|
if (jumping and velocity.y > 0):
|
|
|
|
# If falling, no longer jumping
|
|
|
|
jumping = false
|
|
|
|
|
|
|
|
if (on_air_time < JUMP_MAX_AIRBORNE_TIME and jump and not prev_jump_pressed and not jumping):
|
|
|
|
# Jump must also be allowed to happen if the character left the floor a little bit ago.
|
2015-05-04 13:17:24 +00:00
|
|
|
# Makes controls more snappy.
|
2015-11-21 15:13:43 +00:00
|
|
|
velocity.y = -JUMP_SPEED
|
|
|
|
jumping = true
|
|
|
|
|
|
|
|
on_air_time += delta
|
|
|
|
prev_jump_pressed = jump
|
|
|
|
|
2014-02-22 23:28:19 +00:00
|
|
|
|
|
|
|
func _ready():
|
|
|
|
set_fixed_process(true)
|