From a20ea5299dfd8969766c3302bf876621e57c75fe Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 4 Nov 2025 14:02:23 +0100 Subject: [PATCH] afegit player.md --- source/game/entities/player.md | 132 +++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 source/game/entities/player.md diff --git a/source/game/entities/player.md b/source/game/entities/player.md new file mode 100644 index 00000000..eef5115d --- /dev/null +++ b/source/game/entities/player.md @@ -0,0 +1,132 @@ +handleInput() +{ + if (checkAction(LEFT)) + { + wannaGo::LEFT; + } + else if (checkAction(RIGHT)) + { + wannaGo::RIGHT; + } + else + { + wannaGo::STAY; + } + + wannaJUMP = (checkAction(JUMP)); +} + +move() +{ + handleHorizontalMovement(); + handleVerticalMovement(); +} + +handleHorizontalMovement() +{ + if (State!=STANDING) + { + return; + } + + if (automovement) + { + vx = FIXED_BY_ROOM; + } + else + { + vx = wannaGo::DIRECTION; + } + + moveAndCollide(); + +} + +handleVerticalMovement() +{ + if (State==STANDING) + { + return; + } + + if (State==JUMPING) + { + applyGravity(); + } + + moveAndCollide(); + +} + + +handleConveyorBelts() +{ + if (!automovement and isOnConveyorBelt and wannaGo::STAY) + { + automovement = true; + } + + if (automovement and !isOnConveyorBelt) + { + automovement = false; + } +} + +handleShouldFall() +{ + if (!isOnFloor and State::STANDING) + { + transitionToState(FALLING); + } +} + +transitionToState(state) +{ + prev = state_; + state_ = state; + + switch (state) + { + case STANDING: + vy = 0; + case JUMPING: + if (prev==STANDING) + { + vy = -MAX_VY; + last_ground_position = y; + } + case FALLING: + last_ground_position = y; + vy = MAX_VY; + vx = 0; + } +} + +updateState() +{ + switch (state) + { + case STANDING: + handleConveyorBelts(); + handleShouldFall(); + if (wannaJump) + { + transitionToState(JUMPING); + } + case JUMPING: + automovement = false; + playJumpingSound(); + handleEndJump(); + case FALLING: + automovement = false; + playFallingSound(); + } +} + +update() +{ + hanldeInput(); + updateState(); + move(); + animate(); +} \ No newline at end of file