From 9af18466c072e538b263dae8c0781d02c7c65a05 Mon Sep 17 00:00:00 2001 From: Martin Schmidt Date: Tue, 18 Jun 2024 13:18:07 +0200 Subject: [PATCH] feat: sprinting functionality --- .../de/dhbw/tinf22b6/gameobject/Player.java | 24 ++++++++++++++++++- .../dhbw/tinf22b6/util/PlayerStatistics.java | 4 ++++ .../dhbw/tinf22b6/world/WorldController.java | 10 ++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/game/core/src/main/de/dhbw/tinf22b6/gameobject/Player.java b/game/core/src/main/de/dhbw/tinf22b6/gameobject/Player.java index df965a6..cdee79c 100644 --- a/game/core/src/main/de/dhbw/tinf22b6/gameobject/Player.java +++ b/game/core/src/main/de/dhbw/tinf22b6/gameobject/Player.java @@ -32,6 +32,8 @@ public class Player extends MobGameObject { private float dodgeStateTime; private boolean movedDuringDash; private GameObject interactionTarget; + private boolean isSprinting; + private float timeToRegenStamina; public Player(Vector2 position, Camera camera) { super("c1", position, Constants.PLAYER_BIT); @@ -39,6 +41,7 @@ public Player(Vector2 position, Camera camera) { this.dodgeAnimation = new Animation<>(0.1f, Assets.instance.getAnimationAtlasRegion("priest1_dash")); this.speed = 75; this.motionVector = new Vector2(); + this.timeToRegenStamina = 3; // create Body BodyDef bodyDef = new BodyDef(); @@ -151,6 +154,12 @@ public void tick(float delta) { setDirection(Direction.DOWN); } + if (!isSprinting) timeToRegenStamina -= delta; + + if (!isSprinting && timeToRegenStamina <= 0) { + PlayerStatistics.instance.regenerateStamina(delta); + } + dodgeStateTime += delta; if (!dodging) { pos.x = body.getPosition().x - (float) TILE_SIZE / 2; @@ -177,7 +186,7 @@ public void applyForce(Vector2 motionVector) { }) .start(); } else { - body.setLinearVelocity(motionVector.x * speed, motionVector.y * speed); + body.setLinearVelocity(motionVector.x * getSpeed(), motionVector.y * getSpeed()); pos.x = body.getPosition().x - (float) TILE_SIZE / 2; pos.y = body.getPosition().y - (float) TILE_SIZE / 4; } @@ -236,4 +245,17 @@ public Vector2 getMotionVector() { public void pickupWeapon() { PlayerStatistics.instance.pickupWeapon(); } + + public void setSprinting() { + isSprinting = true; + } + + public void stopSprinting() { + isSprinting = false; + timeToRegenStamina = 3; + } + + public float getSpeed() { + return isSprinting ? speed * 2 : speed; + } } diff --git a/game/core/src/main/de/dhbw/tinf22b6/util/PlayerStatistics.java b/game/core/src/main/de/dhbw/tinf22b6/util/PlayerStatistics.java index 46852e4..7ddf330 100644 --- a/game/core/src/main/de/dhbw/tinf22b6/util/PlayerStatistics.java +++ b/game/core/src/main/de/dhbw/tinf22b6/util/PlayerStatistics.java @@ -173,4 +173,8 @@ public float getStamina() { public void decreaseStamina(float delta) { this.stamina -= delta; } + + public void regenerateStamina(float delta) { + if (stamina <= 100) stamina += delta * 5; + } } diff --git a/game/core/src/main/de/dhbw/tinf22b6/world/WorldController.java b/game/core/src/main/de/dhbw/tinf22b6/world/WorldController.java index 499521f..30dd337 100644 --- a/game/core/src/main/de/dhbw/tinf22b6/world/WorldController.java +++ b/game/core/src/main/de/dhbw/tinf22b6/world/WorldController.java @@ -30,6 +30,7 @@ public class WorldController extends InputAdapter { private final int inventory = prefs.getInteger("inventory", Input.Keys.I); private final int interact = prefs.getInteger("interact", Input.Keys.E); private final int dodge = prefs.getInteger("dodge", Input.Keys.SPACE); + private final int run = prefs.getInteger("run", Input.Keys.SHIFT_LEFT); public CameraHelper cameraHelper; public boolean debugBox2D = false; private Game game; @@ -134,6 +135,12 @@ public void update(float deltaTime) { } private void handleInput(float deltaTime) { + if (Gdx.input.isKeyPressed(run) && PlayerStatistics.instance.canSprint()) { + PlayerStatistics.instance.decreaseStamina(deltaTime * 20); + } + if (Gdx.input.isKeyPressed(run) && !PlayerStatistics.instance.canSprint()) { + EntitySystem.instance.getPlayer().stopSprinting(); + } // Camera Controls (move) float camMoveSpeed = 32 * deltaTime; float camMoveSpeedAccelerationFactor = 5; @@ -161,6 +168,7 @@ public boolean touchDown(int screenX, int screenY, int pointer, int button) { @Override public boolean keyDown(int keycode) { + if (keycode == run) EntitySystem.instance.getPlayer().setSprinting(); // Set Motion Vector pressedKeys.add(keycode); if (keycode == interact) player.interact(player); @@ -183,6 +191,8 @@ public boolean scrolled(float amountX, float amountY) { @Override public boolean keyUp(int keycode) { + if (keycode == run) EntitySystem.instance.getPlayer().stopSprinting(); + // Reset Motion Vector pressedKeys.remove(keycode); if (keycode == Input.Keys.ENTER) {