Jump height control, walk level, adjustments

This commit is contained in:
Jakob Feldmann 2021-05-11 14:17:37 +02:00
parent 61cd57c253
commit 36595e81ec
5 changed files with 78 additions and 66 deletions

View File

@ -14,8 +14,8 @@ func _on_EnemyDetector_body_entered(body: Node) -> void:
die() die()
func handle_grounded_movement(delta: float, direction: Vector2, state: String) -> Vector2: func handle_grounded_movement(delta: float, direction: Vector2) -> Vector2:
return calculate_grounded_velocity(_velocity, delta, direction, state) return calculate_grounded_velocity(_velocity, delta, direction)
func handle_jump_movement(delta: float, direction: Vector2) -> Vector2: func handle_jump_movement(delta: float, direction: Vector2) -> Vector2:
@ -31,8 +31,9 @@ func handle_wallslide_movement(delta: float, direction: Vector2) -> Vector2:
func calculate_grounded_velocity( func calculate_grounded_velocity(
linear_velocity: Vector2, delta: float, direction: Vector2, state: String linear_velocity: Vector2, delta: float, direction: Vector2
) -> Vector2: ) -> Vector2:
var state = self.get_node("PlayerStateMachine").state
var out_vel := linear_velocity var out_vel := linear_velocity
var velocity_direction = 1.0 var velocity_direction = 1.0
if _velocity.x < 0: if _velocity.x < 0:
@ -41,7 +42,7 @@ func calculate_grounded_velocity(
# Stopping movement # Stopping movement
if direction.x == 0.0: if direction.x == 0.0:
var deceleration_force = calculate_deceleration_force( var deceleration_force = calculate_deceleration_force(
gravity, mass, delta _gravity, mass, delta
) )
# Translates velocity back to force and subtracts deceleration force # Translates velocity back to force and subtracts deceleration force
var result_force = ( var result_force = (
@ -62,7 +63,7 @@ func calculate_grounded_velocity(
if reverse_move: if reverse_move:
out_vel.x -= ( out_vel.x -= (
convert_force_to_velocity( convert_force_to_velocity(
calculate_deceleration_force(gravity, mass, delta), calculate_deceleration_force(_gravity, mass, delta),
mass, mass,
delta delta
) )
@ -85,20 +86,12 @@ func calculate_grounded_velocity(
) )
elif ! reverse_move: elif ! reverse_move:
out_vel.x = max_velocity[state] * direction.x out_vel.x = max_velocity[state] * direction.x
# TODO Is this the right place to determine this?
# Jumping when grounded # Jumping when grounded
if is_on_floor() && Input.is_action_just_pressed("jump"): if Input.is_action_just_pressed("jump"):
var additive_jump_force = ( return calculate_jump_velocity(_velocity, delta, direction)
velocity_jump_boost_ratio
* abs(_velocity.x)
* mass
)
out_vel.y = (
((acceleration_force[state].y + additive_jump_force) / mass)
* -1
)
else: else:
out_vel.y = gravity * delta out_vel.y = _gravity * delta
return out_vel return out_vel
@ -113,6 +106,7 @@ func is_reversing_horizontal_movement(direction: Vector2) -> bool:
# Being able to touch a vertical surface over this length also makes it a qualified "wall" # Being able to touch a vertical surface over this length also makes it a qualified "wall"
# Also sets wall_touch_direction # Also sets wall_touch_direction
# TODO Ugly side effect # TODO Ugly side effect
# TODO Walljumping is a bit to radical behaving
func is_touching_wall_completely() -> bool: func is_touching_wall_completely() -> bool:
for left_raycast in left_wall_raycasts.get_children(): for left_raycast in left_wall_raycasts.get_children():
wall_touch_direction = -1 wall_touch_direction = -1
@ -147,19 +141,43 @@ func get_ground_friction() -> float:
# TODO Comments for parameters # TODO Comments for parameters
func calculate_deceleration_force(gravity: float, mass: float, delta: float) -> float: func calculate_deceleration_force(_gravity: float, mass: float, delta: float) -> float:
return get_ground_friction() * gravity * mass * delta return get_ground_friction() * _gravity * mass * delta
func calculate_jump_velocity( func calculate_jump_velocity(
linear_velocity: Vector2, delta: float, direction: Vector2 linear_velocity: Vector2, delta: float, direction: Vector2
) -> Vector2: ) -> Vector2:
if is_touching_wall_completely() && is_correct_walljump_input(direction): var state = self.get_node("PlayerStateMachine").state
if Input.is_action_just_pressed("jump") && state != "jump":
var additive_jump_force = (
velocity_jump_boost_ratio
* abs(_velocity.x)
* mass
)
linear_velocity.y = (
((acceleration_force[state].y + additive_jump_force) / mass)
* -1
)
if (
is_touching_wall_completely()
&& is_correct_walljump_input(direction)
):
# The faster you are moving up the farther the walljump goes # The faster you are moving up the farther the walljump goes
linear_velocity.y = (acceleration_force["walljump"].y / mass) * -1 linear_velocity.y = (acceleration_force["walljump"].y / mass) * -1
linear_velocity.x += max_velocity["walljump"] * direction.x linear_velocity.x += max_velocity["walljump"] * direction.x
if ! Input.is_action_pressed("jump"):
# TODO This is so good not gonna lie
if _velocity.y > _gravity * delta * 10:
linear_velocity.y += _gravity * delta * 10
else: else:
linear_velocity.y += gravity * delta linear_velocity.y += (
max(abs(linear_velocity.y), _gravity * delta)
/ 2
)
else:
linear_velocity.y += _gravity * delta * 0.88
if _velocity.x == 0: if _velocity.x == 0:
linear_velocity.x += inair_velocity * direction.x linear_velocity.x += inair_velocity * direction.x
return linear_velocity return linear_velocity
@ -170,7 +188,7 @@ func calculate_fall_velocity(
linear_velocity: Vector2, delta: float, direction: Vector2 linear_velocity: Vector2, delta: float, direction: Vector2
) -> Vector2: ) -> Vector2:
if _velocity.y < max_velocity["fall"]: if _velocity.y < max_velocity["fall"]:
linear_velocity.y += gravity * delta linear_velocity.y += _gravity * delta
else: else:
linear_velocity.y = max_velocity["fall"] linear_velocity.y = max_velocity["fall"]
if _velocity.x == 0: if _velocity.x == 0:
@ -183,7 +201,8 @@ func calculate_wallslide_velocity(
) -> Vector2: ) -> Vector2:
# Walljump mechanics # Walljump mechanics
if is_correct_walljump_input(direction): if is_correct_walljump_input(direction):
var multiplicator = max(min(1, 1000 / _velocity.y), 0.5) # TODO This +0.01 indicates a larger problem with division through possible 0 values!!
var multiplicator = max(min(1, 1000 / (_velocity.y + 0.01)), 0.5)
linear_velocity.y += ( linear_velocity.y += (
(acceleration_force["walljump"].y / mass) (acceleration_force["walljump"].y / mass)
* -1 * -1
@ -191,7 +210,7 @@ func calculate_wallslide_velocity(
) )
linear_velocity.x += max_velocity["walljump"] * direction.x linear_velocity.x += max_velocity["walljump"] * direction.x
else: else:
linear_velocity.y += gravity * delta * 0.4 linear_velocity.y += _gravity * delta * 0.4
# linear_velocity.x += inair_velocity * direction.x # linear_velocity.x += inair_velocity * direction.x
return linear_velocity return linear_velocity
@ -204,6 +223,12 @@ func calculate_stomp_velocity(linear_velocity: Vector2, impulse: float) -> Vecto
func execute_movement() -> void: func execute_movement() -> void:
_velocity = move_and_slide(_velocity, FLOOR_NORMAL) _velocity = move_and_slide(_velocity, FLOOR_NORMAL)
# TODO Replace .get_nodes with $ and put them to file beginning if possible
# var camera = self.get_node("Camera2D")
# if _velocity.x < 0:
# camera.transform.origin.x = -100
# if _velocity.x > 0:
# camera.transform.origin.x = 100
func die() -> void: func die() -> void:

View File

@ -29,7 +29,6 @@ script = ExtResource( 3 )
script = ExtResource( 4 ) script = ExtResource( 4 )
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
position = Vector2( 0, -181 )
current = true current = true
limit_left = 0 limit_left = 0
limit_top = 0 limit_top = 0
@ -37,8 +36,6 @@ limit_smoothed = true
drag_margin_h_enabled = true drag_margin_h_enabled = true
drag_margin_v_enabled = true drag_margin_v_enabled = true
smoothing_enabled = true smoothing_enabled = true
drag_margin_left = 0.0
drag_margin_right = 0.0
[node name="EnemyDetector" type="Area2D" parent="."] [node name="EnemyDetector" type="Area2D" parent="."]
monitorable = false monitorable = false
@ -48,10 +45,10 @@ collision_mask = 2
modulate = Color( 0.2, 0, 0.494118, 1 ) modulate = Color( 0.2, 0, 0.494118, 1 )
shape = SubResource( 2 ) shape = SubResource( 2 )
[node name="StateMachine" type="Node" parent="."] [node name="PlayerStateMachine" type="Node" parent="."]
script = ExtResource( 2 ) script = ExtResource( 2 )
[node name="StateLable" type="Label" parent="."] [node name="StateLabel" type="Label" parent="."]
margin_left = -25.3386 margin_left = -25.3386
margin_top = -34.2836 margin_top = -34.2836
margin_right = 25.6614 margin_right = 25.6614
@ -93,5 +90,6 @@ position = Vector2( 10.6962, 14.8466 )
enabled = true enabled = true
cast_to = Vector2( 1, 0 ) cast_to = Vector2( 1, 0 )
collision_mask = 9 collision_mask = 9
[connection signal="area_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_area_entered"] [connection signal="area_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_area_entered"]
[connection signal="body_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_body_entered"] [connection signal="body_entered" from="EnemyDetector" to="." method="_on_EnemyDetector_body_entered"]

View File

@ -3,23 +3,24 @@ class_name Player
const FLOOR_NORMAL := Vector2.UP const FLOOR_NORMAL := Vector2.UP
export var stomp_feedback := 1000.0 var stomp_feedback := 1000.0
export var inair_velocity := 21 var inair_velocity := 21
export var wallslide_threshold := 1000 var wallslide_threshold := 1000
export var max_velocity := { var max_velocity := {
"walk": 130, "run": 180, "fall": 987, "walljump": 150 "walk": 130, "run": 180, "fall": 987, "walljump": 150, "idle": 130
} }
export var velocity_jump_boost_ratio := 0.4383 var velocity_jump_boost_ratio := 0.1967
# This is added to the acceleration force initially # This is added to the acceleration force initially
export var init_acceleration_force := {"walk": 4181, "run": 6765} var init_acceleration_force := {"walk": 4181, "run": 6765, "idle": 4181}
# newtonmeters is the unit # newtonmeters is the unit
export var acceleration_force := { var acceleration_force := {
"walk": Vector2(2584, 2000), "walk": Vector2(2584, 2000),
"idle": Vector2(2584, 2000),
"run": Vector2(2584, 2000), "run": Vector2(2584, 2000),
"walljump": Vector2(2548, 2000) "walljump": Vector2(2548, 2000)
} }
export var gravity := 1667.0 var _gravity := 1667.0
# Kilograms # Kilograms
export var mass := 6 var mass := 6
var _velocity := Vector2.ZERO var _velocity := Vector2.ZERO

View File

@ -48,17 +48,17 @@ func _state_logic(delta):
func handle_idle_input(delta, direction := get_horizontal_direction()) -> Vector2: func handle_idle_input(delta, direction := get_horizontal_direction()) -> Vector2:
if Input.is_action_pressed("boost_move"): if Input.is_action_pressed("boost_move"):
return parent.handle_grounded_movement(delta, direction, "run") return parent.handle_grounded_movement(delta, direction)
else: else:
return parent.handle_grounded_movement(delta, direction, "walk") return parent.handle_grounded_movement(delta, direction)
func handle_walk_input(delta, direction := get_horizontal_direction()) -> Vector2: func handle_walk_input(delta, direction := get_horizontal_direction()) -> Vector2:
return parent.handle_grounded_movement(delta, direction, state) return parent.handle_grounded_movement(delta, direction)
func handle_run_input(delta, direction := get_horizontal_direction()) -> Vector2: func handle_run_input(delta, direction := get_horizontal_direction()) -> Vector2:
return parent.handle_grounded_movement(delta, direction, state) return parent.handle_grounded_movement(delta, direction)
func handle_jump_input(delta, direction := get_horizontal_direction()) -> Vector2: func handle_jump_input(delta, direction := get_horizontal_direction()) -> Vector2:
@ -85,7 +85,7 @@ func get_horizontal_direction() -> Vector2:
# Determines which state should be active at the moment # Determines which state should be active at the moment
func _get_transition(delta): func _get_transition(delta):
parent.get_node("StateLable").text = ( parent.get_node("StateLabel").text = (
self.state self.state
+ " x vel:" + " x vel:"
+ String(round(parent._velocity.x)) + String(round(parent._velocity.x))

File diff suppressed because one or more lines are too long