make falling pips despawn, remove redundancy

This commit is contained in:
James 2024-04-21 17:13:56 +01:00
parent 8d664684da
commit 28b2b68c46
3 changed files with 6 additions and 16 deletions

View file

@ -68,7 +68,7 @@ public class Die extends Actor {
Face.Pip decayed = pips.get(random.nextInt(pips.size())); Face.Pip decayed = pips.get(random.nextInt(pips.size()));
face.removePip(decayed); face.removePip(decayed);
game.addFallingPip(new FallingPip(new Transform(transform.getX(), transform.getY(), 0, 0), face.equals(getFace()))); game.addFallingPip(new FallingPip(new Transform(transform.getX(), transform.getY(), 0, 0)));
} }
} }

View file

@ -10,18 +10,12 @@ public class FallingPip extends Actor {
private final Transform transform; private final Transform transform;
private final Vector2 velocity; private final Vector2 velocity;
private final float rotationalVelocity;
private final boolean onTop; public FallingPip(Transform transform) {
public FallingPip(Transform transform, boolean onTop) {
Random random = new Random(); Random random = new Random();
this.transform = transform; this.transform = transform;
velocity = new Vector2(5 * (-0.5f + random.nextFloat()), 2 + random.nextFloat() * 3); velocity = new Vector2(5 * (-0.5f + random.nextFloat()), 2 + random.nextFloat() * 3);
rotationalVelocity = 40 * (-0.5f + random.nextFloat());
this.onTop = onTop;
} }
@Override @Override
@ -30,7 +24,6 @@ public class FallingPip extends Actor {
transform.x += velocity.x; transform.x += velocity.x;
transform.y += velocity.y; transform.y += velocity.y;
transform.rotation += rotationalVelocity;
} }
@Override @Override
@ -43,8 +36,4 @@ public class FallingPip extends Actor {
return transform.y <= 25; return transform.y <= 25;
} }
public boolean isOnTop() {
return onTop;
}
} }

View file

@ -79,6 +79,7 @@ public class Game extends ApplicationAdapter {
processInput(); processInput();
fallingPips.forEach(Actor::tick); fallingPips.forEach(Actor::tick);
fallingPips.removeIf(FallingPip::isOffScreen);
} }
public void processInput() { public void processInput() {
@ -153,13 +154,13 @@ public class Game extends ApplicationAdapter {
ScreenUtils.clear(0, 0, 0, 1); ScreenUtils.clear(0, 0, 0, 1);
batch.begin(); batch.begin();
for (FallingPip pip : fallingPips) if (!pip.isOnTop()) pip.render(batch);
for (Die die : dice) { for (Die die : dice) {
die.render(batch); die.render(batch);
} }
for (FallingPip pip : fallingPips) if (pip.isOnTop()) pip.render(batch); //on top for (FallingPip pip : fallingPips) { //on top
pip.render(batch);
}
int y = Gdx.graphics.getHeight() / 3 * 2 - 25; int y = Gdx.graphics.getHeight() / 3 * 2 - 25;
for (Decay decay : round.getDecays()) { for (Decay decay : round.getDecays()) {