mirror of
https://github.com/MoonlitJolteon/crasher-sidescroller.git
synced 2025-11-02 21:25:03 +00:00
Changed movement to scroll level instead of player, test level now loops. Player also now has weight based inertia
This commit is contained in:
@ -6,36 +6,103 @@ extends CharacterBody2D
|
||||
@export var base_weight: float = 1
|
||||
@export var min_weight: float = 0.5
|
||||
@export var max_weight: float = 2.0
|
||||
|
||||
@export var weight_change_speed: float = 1000
|
||||
@export var vertical_momentum_factor: float = 5000.0
|
||||
@export var horizontal_momentum_factor: float = 500.0
|
||||
@export var current_speed: float = 0.0
|
||||
@export var ground_speed: Vector2 = Vector2.ZERO
|
||||
|
||||
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
|
||||
@onready var weight_label = $WeightLabel
|
||||
|
||||
var animation_locked: bool = false
|
||||
var direction: Vector2 = Vector2.ZERO
|
||||
var was_in_air: bool = false
|
||||
var current_speed: float = 0.0
|
||||
var current_weight: float = base_weight
|
||||
var is_jumping: bool = false
|
||||
var jump_start_weight: float = base_weight
|
||||
|
||||
func _process(_delta):
|
||||
# Update the label's text with the current weight
|
||||
weight_label.text = (
|
||||
"Weight: " + str(current_weight) +
|
||||
"\nDelta Weight: " + str(current_weight - jump_start_weight) +
|
||||
"\nVelocity X: " + str(ground_speed.x)
|
||||
)
|
||||
|
||||
# Position the label above the character's head
|
||||
weight_label.global_position = global_position + Vector2(-20, -75) # Adjust the Y offset as needed
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if ground_speed == null:
|
||||
return
|
||||
|
||||
# Weight changing
|
||||
if Input.is_action_pressed("decrease_weight"):
|
||||
current_weight = max(current_weight - (weight_change_speed * delta), min_weight)
|
||||
elif Input.is_action_pressed("increase_weight"):
|
||||
current_weight = min(current_weight + (weight_change_speed * delta), max_weight)
|
||||
elif Input.is_action_pressed("reset_weight"):
|
||||
current_weight = base_weight
|
||||
|
||||
if was_in_air == true and is_on_floor():
|
||||
current_speed = ground_speed.x
|
||||
|
||||
ground_speed.x = current_speed
|
||||
|
||||
# Gravity and inertia
|
||||
if not is_on_floor():
|
||||
velocity += get_gravity() * delta
|
||||
was_in_air = true
|
||||
|
||||
if is_jumping:
|
||||
# Apply inertia on weight change
|
||||
var weight_change = current_weight - jump_start_weight
|
||||
ground_speed.x += -weight_change * vertical_momentum_factor * delta
|
||||
velocity.y += weight_change * horizontal_momentum_factor * delta
|
||||
else:
|
||||
if was_in_air == true:
|
||||
land()
|
||||
was_in_air = false
|
||||
is_jumping = false
|
||||
jump_start_weight = current_weight
|
||||
|
||||
# Constant forward movement
|
||||
if abs(current_speed - max_speed) < 1:
|
||||
current_speed = max_speed
|
||||
|
||||
if current_speed < max_speed:
|
||||
current_speed += acceleration * delta
|
||||
elif current_speed > max_speed:
|
||||
current_speed -= acceleration * delta * ((current_speed - max_speed) / 30)
|
||||
|
||||
# Handle Jumping
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
jump()
|
||||
|
||||
update_animation()
|
||||
move_and_slide()
|
||||
update_facing_direction()
|
||||
|
||||
func update_animation():
|
||||
if not animation_locked:
|
||||
if direction.x != 0:
|
||||
if ground_speed.x != 0:
|
||||
animated_sprite.play("run")
|
||||
else:
|
||||
animated_sprite.play("idle")
|
||||
|
||||
func update_facing_direction():
|
||||
if direction.x > 0:
|
||||
if ground_speed.x > 0:
|
||||
animated_sprite.flip_h = false
|
||||
elif direction.x < 0:
|
||||
elif ground_speed.x < 0:
|
||||
animated_sprite.flip_h = true
|
||||
|
||||
func jump():
|
||||
velocity.y = jump_force
|
||||
animated_sprite.play("jump_start")
|
||||
animation_locked = true
|
||||
is_jumping = true
|
||||
|
||||
func land():
|
||||
animated_sprite.play("jump_end")
|
||||
|
||||
Reference in New Issue
Block a user