added more tunable basic movement
This commit is contained in:
@@ -1,25 +1,53 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
const JUMP_VELOCITY = -600.0
|
||||
const MAX_JUMP_DURATION = 0.3
|
||||
const MIN_JUMP_DURATION = 0.1
|
||||
const GRAVITY = 980
|
||||
const FALL_GRAVITY_MULTIPLIER = 1.5
|
||||
|
||||
var jump_timer = 0.0
|
||||
var is_jumping = false
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
func _physics_process(delta):
|
||||
# Apply gravity
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
var gravity_multiplier = FALL_GRAVITY_MULTIPLIER if velocity.y > 0 else 1.0
|
||||
velocity.y += GRAVITY * gravity_multiplier * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
# Handle Jump
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
start_jump()
|
||||
|
||||
if is_jumping:
|
||||
jump_timer += delta
|
||||
if Input.is_action_pressed("jump") and jump_timer < MAX_JUMP_DURATION:
|
||||
continue_jump(delta)
|
||||
else:
|
||||
end_jump()
|
||||
|
||||
# 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")
|
||||
# Handle horizontal movement
|
||||
var direction = Input.get_axis("move_left", "move_right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func start_jump():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
is_jumping = true
|
||||
jump_timer = 0.0
|
||||
|
||||
func continue_jump(delta):
|
||||
if jump_timer > MIN_JUMP_DURATION:
|
||||
var t = (jump_timer - MIN_JUMP_DURATION) / (MAX_JUMP_DURATION - MIN_JUMP_DURATION)
|
||||
var ease_factor = 1.0 - t * t # Quadratic easing
|
||||
velocity.y += JUMP_VELOCITY * ease_factor * delta
|
||||
|
||||
func end_jump():
|
||||
is_jumping = false
|
||||
if velocity.y < 0:
|
||||
velocity.y *= 0.5 # Cut the upward velocity to end the jump early if button is released
|
||||
|
@@ -7,14 +7,14 @@ size = Vector2(455, 51)
|
||||
|
||||
[node name="Ground1" type="Node2D"]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(2, 3)
|
||||
scale = Vector2(0.2, 0.2)
|
||||
texture = ExtResource("1_437ln")
|
||||
|
||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
|
||||
position = Vector2(-8.5, 1.5)
|
||||
shape = SubResource("RectangleShape2D_2rdbk")
|
||||
debug_color = Color(0, 0.645698, 0.254313, 0.42)
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(2, 3)
|
||||
scale = Vector2(0.2, 0.2)
|
||||
texture = ExtResource("1_437ln")
|
||||
|
Reference in New Issue
Block a user