added ability to lock dice

This commit is contained in:
James 2024-04-20 16:34:16 +01:00
parent 2544da0313
commit 40bcbee4e9
2 changed files with 22 additions and 3 deletions

View file

@ -17,10 +17,10 @@ public class Die extends Actor {
private final Face[] faces = new Face[6]; private final Face[] faces = new Face[6];
private int faceIndex = 3; private int faceIndex = 3;
private boolean locked = false;
private final Random random = new Random(); //TODO use central random private final Random random = new Random(); //TODO use central random
public Die() { public Die() {
int[] pips = {4, 6, 5, 1, 2, 3}; int[] pips = {4, 6, 5, 1, 2, 3};
for (int i = 0; i < faces.length; i++) { for (int i = 0; i < faces.length; i++) {
@ -49,8 +49,10 @@ public class Die extends Actor {
} }
public void roll() { public void roll() {
if (!locked) {
faceIndex = random.nextInt(6); faceIndex = random.nextInt(6);
} }
}
public void decay() { //remove a pip from all faces of this die public void decay() { //remove a pip from all faces of this die
for (Face face : faces) { for (Face face : faces) {
@ -68,4 +70,12 @@ public class Die extends Actor {
return getFace().getValue(); return getFace().getValue();
} }
public boolean isLocked() {
return locked;
}
public void setLocked(boolean locked) {
this.locked = locked;
}
} }

View file

@ -47,9 +47,18 @@ public class Game extends ApplicationAdapter {
public void processInput() { public void processInput() {
Input input = Gdx.input; Input input = Gdx.input;
if (input.isKeyJustPressed(Input.Keys.R)) { if (input.isKeyJustPressed(Input.Keys.R)) { //reroll dice
dice.forEach(Die::roll); dice.forEach(Die::roll);
} }
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode
Die die = dice.get(i); //die iterator is looking at
int keyCode = Input.Keys.NUM_1 + i; //keycode for the current die, 1, 2...9, 0 on keyboard
if (input.isKeyJustPressed(keyCode)) { //if key corresponding to die has been pressed
die.setLocked(!die.isLocked()); //flip lock state
}
}
} }
@Override @Override