diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-11-29 12:57:36 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-11-29 12:57:36 +0200 |
| commit | cddd0a496fcc6404f1024c4e515f1e20a31a2d6c (patch) | |
| tree | 5a31beeeeeb7428976b9902a1165394d1ca8633b | |
| parent | 3c3d0c3bfd9bac15b4406318c27a91954c3e7697 (diff) | |
Add momentum to swinging
| -rw-r--r-- | computer.gd | 2 | ||||
| -rw-r--r-- | docs/index.html | 2 | ||||
| -rw-r--r-- | docs/index.pck | bin | 3865528 -> 3865560 bytes | |||
| -rw-r--r-- | docs/index.service.worker.js | 2 | ||||
| -rw-r--r-- | player.gd | 24 | ||||
| -rw-r--r-- | ps_curve.gd | 2 | ||||
| -rw-r--r-- | ps_hanging.gd | 26 | ||||
| -rw-r--r-- | transition_player.gd | 6 |
8 files changed, 39 insertions, 25 deletions
diff --git a/computer.gd b/computer.gd index ffd1b4c..1e5bcb9 100644 --- a/computer.gd +++ b/computer.gd @@ -5,4 +5,4 @@ extends Sprite2D @export var reverse: bool func _on_area_2d_body_entered(player: Player) -> void: - player.set_movement_curve(path.curve, speed, reverse) + player.request_state_curve(path.curve, speed, reverse) diff --git a/docs/index.html b/docs/index.html index 3aca742..1e16f0c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -112,7 +112,7 @@ body { <script src="index.js"></script> <script> -const GODOT_CONFIG = {"args":[],"canvasResizePolicy":2,"emscriptenPoolSize":8,"ensureCrossOriginIsolationHeaders":true,"executable":"index","experimentalVK":false,"fileSizes":{"index.pck":3865528,"index.wasm":38034280},"focusCanvas":true,"gdextensionLibs":[],"godotPoolSize":4,"serviceWorker":"index.service.worker.js"}; +const GODOT_CONFIG = {"args":[],"canvasResizePolicy":2,"emscriptenPoolSize":8,"ensureCrossOriginIsolationHeaders":true,"executable":"index","experimentalVK":false,"fileSizes":{"index.pck":3865560,"index.wasm":38034280},"focusCanvas":true,"gdextensionLibs":[],"godotPoolSize":4,"serviceWorker":"index.service.worker.js"}; const GODOT_THREADS_ENABLED = false; const engine = new Engine(GODOT_CONFIG); diff --git a/docs/index.pck b/docs/index.pck Binary files differindex c669e15..2ef95fb 100644 --- a/docs/index.pck +++ b/docs/index.pck diff --git a/docs/index.service.worker.js b/docs/index.service.worker.js index c2f5364..362e564 100644 --- a/docs/index.service.worker.js +++ b/docs/index.service.worker.js @@ -4,7 +4,7 @@ // Incrementing CACHE_VERSION will kick off the install event and force // previously cached resources to be updated from the network. /** @type {string} */ -const CACHE_VERSION = '1764363858|27701075135'; +const CACHE_VERSION = '1764413754|33126432540'; /** @type {string} */ const CACHE_PREFIX = 'MobileMetroidvan-sw-cache-'; const CACHE_NAME = CACHE_PREFIX + CACHE_VERSION; @@ -12,12 +12,6 @@ var state: PlayerState = PlayerStateInit.new(self) var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") -func get_facing() -> int: - if $AnimatedSprite2D.flip_h: - return -1 - else: - return 1 - # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta: float) -> void: state._handle(delta) @@ -25,26 +19,30 @@ func _process(delta: float) -> void: func _physics_process(delta: float): state._handle_physics(delta) -#region movement type setters -func set_movement_normal(): +func request_state_normal(): state = PlayerStateNormal.new(self) -func set_movement_forced(dir: String): +func request_state_forced(dir: String): if state is PlayerStateForced: return state = PlayerStateForced.new(self, dir) -func set_movement_curve(curve: Curve2D, c_speed: float, c_reverse: bool): +func request_state_curve(curve: Curve2D, c_speed: float, c_reverse: bool): if state is PlayerStateCurve: return state = PlayerStateCurve.new(self, curve, c_speed, c_reverse) -func set_movement_hanging(target: Node2D, hang_dist: float): +func request_state_hanging(target: Node2D, hang_dist: float): if state is PlayerStateHanging: return if not PlayerStateHanging.can_hang_from(self, target): return state = PlayerStateHanging.new(self, target, hang_dist) -#endregion + +func get_facing() -> int: + if anim_sp.flip_h: + return -1 + else: + return 1 func handle_action_input(): var hooks = get_tree().get_nodes_in_group("hook_hanging") @@ -57,4 +55,4 @@ func handle_action_input(): var dist = (hook.global_position - global_position).length() print_debug("found hook: ", hook, ", dist: ", dist) if dist < hook_distance_max: - set_movement_hanging(hook, hook.hang_distance) + request_state_hanging(hook, hook.hang_distance) diff --git a/ps_curve.gd b/ps_curve.gd index ab5c327..c1c1567 100644 --- a/ps_curve.gd +++ b/ps_curve.gd @@ -36,7 +36,7 @@ func _handle_physics(delta: float): p.velocity = (new_position - p.position) / delta if finished: - p.set_movement_normal() + p.request_state_normal() return p.position = new_position diff --git a/ps_hanging.gd b/ps_hanging.gd index 0b3b80b..50bd6b1 100644 --- a/ps_hanging.gd +++ b/ps_hanging.gd @@ -15,6 +15,8 @@ var travel_to: Vector2 var T_mlem: float = 0.2 var T_travel: float = 0.1 +var swing_force: float = 0.2 + enum S { MLEMING, TRAVELING, HANGING } var state: S = S.MLEMING @@ -24,12 +26,15 @@ func _init(pl: Player, h_target: HookHanging, h_dist: float) -> void: t = 0 L = h_dist target = h_target - w = sqrt(p.gravity / L) * 0.8 + w = sqrt(p.gravity / L) * 0.7 + + var dx = target.global_position.x - pl.global_position.x + pl.anim_sp.flip_h = dx < 0 state = S.MLEMING func _to_string() -> String: - return "PlayerStateHanging %s, θ₀ = %f" % [S.keys()[state], theta0] + return "PlayerStateHanging %s, θ₀ = %f, θ = %f" % [S.keys()[state], theta0, last_theta] static func can_hang_from(pl: Player, c_target: HookHanging) -> bool: var d = c_target.global_position - pl.global_position @@ -100,12 +105,23 @@ func _hang(delta: float): if Input.is_action_just_pressed("jump"): p.velocity.y = -p.jump_strength tongue.queue_free() - p.set_movement_normal() + p.request_state_normal() var theta = theta0 * cos(t * w) + + # Allow player to add momentum by moving toward the swing direction + var dtheta = theta - last_theta + var momentum = Input.get_axis("move_right", "move_left") + var momentum_dir = sign(momentum) * sign(dtheta) + theta0 += momentum_dir * sign(theta0) * swing_force * delta + + # Limit max swinging height to horizontal by + # lerping theta0 to 90deg at the bottom of the swing + if abs(theta0) > PI / 2 and abs(theta) < PI / 8: + theta0 = lerp(theta0, sign(theta0) * PI / 2, 0.8) - if sign(theta) != sign(last_theta) and abs(theta0) > PI / 2: - theta0 = sign(theta0) * PI / 2 + #if sign(theta) != sign(last_theta) and abs(theta0) > PI / 2: + # theta0 = sign(theta0) * PI / 2 p.global_position.x = target.global_position.x + L * cos(theta + PI / 2) p.global_position.y = target.global_position.y + L * sin(theta + PI / 2) diff --git a/transition_player.gd b/transition_player.gd index a118b56..56fe756 100644 --- a/transition_player.gd +++ b/transition_player.gd @@ -15,7 +15,7 @@ func room_transition(from_door: Door, to_room_path: String, door_name: Variant): if from_door: var player: Player = top.get_node("ActiveRoom").find_child("Player") - player.set_movement_forced(from_door.enter_towards) + player.request_state_forced(from_door.enter_towards) # Start animation # The animation calls _change_room at a specific time @@ -48,7 +48,7 @@ func _on_room_changed(): if _transition_door_name: var door: Door = room.find_child(_transition_door_name) player.position = door.position - player.set_movement_forced(door.exit_towards) + player.request_state_forced(door.exit_towards) play("room_trans_end") top.game_save() @@ -62,4 +62,4 @@ func _transition_end(): var room = top.get_node("ActiveRoom") var player: Player = room.find_child("Player") if player: - player.set_movement_normal() + player.request_state_normal() |
