Changed movement to scroll level instead of player, test level now loops. Player also now has weight based inertia

This commit is contained in:
2025-02-17 09:40:31 -05:00
parent 94795e4c77
commit 1abf5b7bce
5 changed files with 124 additions and 9 deletions

View File

@ -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")

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=36 format=3 uid="uid://4qv7ygwuks70"]
[gd_scene load_steps=37 format=3 uid="uid://4qv7ygwuks70"]
[ext_resource type="Texture2D" uid="uid://ce5lrej8gpo3i" path="res://Art/Character/Idle/Idle-Sheet.png" id="1_caaw0"]
[ext_resource type="Script" path="res://Characters/player.gd" id="1_mhe8q"]
@ -223,6 +223,9 @@ animations = [{
radius = 8.0
height = 44.0
[sub_resource type="LabelSettings" id="LabelSettings_av1l4"]
font_size = 10
[node name="Player" type="CharacterBody2D"]
script = ExtResource("1_mhe8q")
@ -237,4 +240,12 @@ offset = Vector2(-32, -40)
position = Vector2(0, 2)
shape = SubResource("CapsuleShape2D_4mr7i")
[node name="WeightLabel" type="Label" parent="."]
offset_left = -40.0
offset_top = -41.0
offset_right = 27.0
offset_bottom = -27.0
text = "Weight: "
label_settings = SubResource("LabelSettings_av1l4")
[connection signal="animation_finished" from="AnimatedSprite2D" to="." method="_on_animated_sprite_2d_animation_finished"]

12
Levels/test_level.gd Normal file
View File

@ -0,0 +1,12 @@
extends Node2D
@onready var world: CanvasGroup = $CanvasGroup
@onready var player: CharacterBody2D = $Player
func _process(delta: float) -> void:
world.position.x -= player.ground_speed.x * delta
if world.position.x < -2000:
world.position.x = 0

File diff suppressed because one or more lines are too long

View File

@ -51,6 +51,21 @@ jump={
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}
increase_weight={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null)
]
}
decrease_weight={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":81,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null)
]
}
reset_weight={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null)
]
}
[rendering]