gdot-test/Player.gd

51 lines
1.5 KiB
GDScript3
Raw Normal View History

2023-11-02 19:51:40 -04:00
extends CharacterBody2D
const SPEED = 150.0
const JUMP_VELOCITY = -200.0
const MAX_FALL = 200
var has_double_jumped: bool = false
var jump_released: bool = false
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
if self.get_position()[1] >= MAX_FALL:
get_tree().reload_current_scene()
if is_on_floor():
has_double_jumped = false
if Input.is_action_just_pressed("ui_jump") and not is_on_floor() and not has_double_jumped:
has_double_jumped = true
jump_released = false
velocity.y = 0
velocity.y += JUMP_VELOCITY
# Add the gravity.
if not is_on_floor():
velocity.y += gravity/2 * delta
# Handle Jump.
if Input.is_action_just_pressed("ui_jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
jump_released = false
if Input.is_action_just_released("ui_jump") and not jump_released:
velocity.y = velocity.y * 0.2 #Slow down verticle acceleration
jump_released = true
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if velocity.x < 0:
get_node("AnimatedSprite2D").set_flip_h(true)
if velocity.x > 0:
get_node("AnimatedSprite2D").set_flip_h(false)
move_and_slide()