added ability to lock dice
This commit is contained in:
parent
2544da0313
commit
40bcbee4e9
2 changed files with 22 additions and 3 deletions
|
@ -17,10 +17,10 @@ public class Die extends Actor {
|
|||
|
||||
private final Face[] faces = new Face[6];
|
||||
private int faceIndex = 3;
|
||||
private boolean locked = false;
|
||||
|
||||
private final Random random = new Random(); //TODO use central random
|
||||
|
||||
|
||||
public Die() {
|
||||
int[] pips = {4, 6, 5, 1, 2, 3};
|
||||
for (int i = 0; i < faces.length; i++) {
|
||||
|
@ -49,8 +49,10 @@ public class Die extends Actor {
|
|||
}
|
||||
|
||||
public void roll() {
|
||||
if (!locked) {
|
||||
faceIndex = random.nextInt(6);
|
||||
}
|
||||
}
|
||||
|
||||
public void decay() { //remove a pip from all faces of this die
|
||||
for (Face face : faces) {
|
||||
|
@ -68,4 +70,12 @@ public class Die extends Actor {
|
|||
return getFace().getValue();
|
||||
}
|
||||
|
||||
public boolean isLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
public void setLocked(boolean locked) {
|
||||
this.locked = locked;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,9 +47,18 @@ public class Game extends ApplicationAdapter {
|
|||
public void processInput() {
|
||||
Input input = Gdx.input;
|
||||
|
||||
if (input.isKeyJustPressed(Input.Keys.R)) {
|
||||
if (input.isKeyJustPressed(Input.Keys.R)) { //reroll dice
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue