added more tunable basic movement
This commit is contained in:
parent
b726bcf4aa
commit
0f41e0d017
@ -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")
|
||||
|
@ -14,3 +14,21 @@ config/name="lakewood"
|
||||
run/main_scene="res://world.tscn"
|
||||
config/features=PackedStringArray("4.3", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[input]
|
||||
|
||||
jump={
|
||||
"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":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_left={
|
||||
"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":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
move_right={
|
||||
"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":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
@ -11,19 +11,24 @@
|
||||
physics_interpolation_mode = 1
|
||||
|
||||
[node name="Tellick" parent="." instance=ExtResource("1_dgu6h")]
|
||||
position = Vector2(-7, -130)
|
||||
position = Vector2(-39, 47)
|
||||
|
||||
[node name="Camera2D" type="Camera2D" parent="."]
|
||||
zoom = Vector2(0.5, 0.5)
|
||||
|
||||
[node name="StaticBody2D" type="StaticBody2D" parent="."]
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"]
|
||||
position = Vector2(1, 140)
|
||||
shape = SubResource("WorldBoundaryShape2D_5e4u7")
|
||||
debug_color = Color(0.994873, 0.0645888, 1.15514e-06, 0.42)
|
||||
|
||||
[node name="Ground1" parent="." instance=ExtResource("2_gboim")]
|
||||
position = Vector2(-333, 114)
|
||||
position = Vector2(-279, 114)
|
||||
|
||||
[node name="TileMap" type="TileMap" parent="."]
|
||||
tile_set = SubResource("TileSet_bke3u")
|
||||
format = 2
|
||||
|
||||
[node name="Ground2" parent="." instance=ExtResource("2_gboim")]
|
||||
position = Vector2(-245, -72)
|
||||
|
Loading…
Reference in New Issue
Block a user