Skip to content
This repository has been archived by the owner on Sep 29, 2024. It is now read-only.

Commit

Permalink
feat: sprinting functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
LPkkjHD committed Jun 18, 2024
1 parent d0dc629 commit 9af1846
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
24 changes: 23 additions & 1 deletion game/core/src/main/de/dhbw/tinf22b6/gameobject/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ 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);
this.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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
10 changes: 10 additions & 0 deletions game/core/src/main/de/dhbw/tinf22b6/world/WorldController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down

0 comments on commit 9af1846

Please sign in to comment.