add decay (THE THEME OF THE JAM)

This commit is contained in:
James 2024-04-21 12:40:56 +01:00
parent 8c5a4f0bea
commit 1779f3d448
7 changed files with 112 additions and 38 deletions

View file

@ -0,0 +1,15 @@
package com.monjaro.gamejam.main;
import java.util.List;
public abstract class Decay {
protected String description;
public abstract List<Die> getDecayed(List<Die> dice);
public String getDescription() {
return description;
}
}

View file

@ -61,7 +61,8 @@ public class Die extends Actor {
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) {
List<Face.Pip> pips = face.getPips(); List<Face.Pip> pips = face.getPips();
Face.Pip decayed = pips.get(random.nextInt()); if (pips.isEmpty()) continue;
Face.Pip decayed = pips.get(random.nextInt(pips.size()));
face.removePip(decayed); face.removePip(decayed);
} }
} }

View file

@ -6,7 +6,6 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils; import com.badlogic.gdx.utils.ScreenUtils;
import com.monjaro.gamejam.segment.DualSegment; import com.monjaro.gamejam.segment.DualSegment;
import com.monjaro.gamejam.segment.KinSegment; import com.monjaro.gamejam.segment.KinSegment;
@ -19,7 +18,6 @@ import java.util.List;
public class Game extends ApplicationAdapter { public class Game extends ApplicationAdapter {
private final List<Die> dice = new ArrayList<>(); private final List<Die> dice = new ArrayList<>();
private final List<Segment> segments = new ArrayList<>();
private SpriteBatch batch; private SpriteBatch batch;
private BitmapFont font; private BitmapFont font;
@ -38,54 +36,65 @@ public class Game extends ApplicationAdapter {
font.getData().markupEnabled = true; font.getData().markupEnabled = true;
img = new Texture("badlogic.jpg"); img = new Texture("badlogic.jpg");
ui = new UI(50, 280, 10); ui = new UI(this, 50, 280);
round = new Round(new ArrayList<>(), 10); round = new Round(List.of(new OlympicSegment(1), new OlympicSegment(3), new KinSegment(3), new DualSegment(false)), List.of(new ParityDecay(true)), 5);
Face.setBlankFaceSprite(new Texture("blank_die_face.png")); Face.setBlankFaceSprite(new Texture("blank_die_face.png"));
Face.setPipSprite(new Texture("pip.png")); Face.setPipSprite(new Texture("pip.png"));
Die.setLockedSprite(new Texture("locked_die_border.png")); Die.setLockedSprite(new Texture("locked_die_border.png"));
UI.setRerollTexture(new Texture("reroll_symbol.png")); UI.setRerollTexture(new Texture("reroll_symbol.png"));
Vector2 dieSize = new Vector2();
float divide = Gdx.graphics.getWidth() / 6f; float divide = Gdx.graphics.getWidth() / 6f;
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
dice.add(new Die(divide * (i + 1), 350, 64, 64)); dice.add(new Die(divide * (i + 1), 350, 64, 64));
} }
for (int i = 1; i <= 5; i++) {
segments.add(new KinSegment(i));
}
for (int i = 2; i <= 5; i++) {
segments.add(new OlympicSegment(i));
}
segments.add(new DualSegment(false));
segments.add(new DualSegment(true));
} }
public void tick() { public void tick() {
processInput(); processInput();
ui.setRemainingRerolls(round.getRerolls()); ui.setRerolls(round.getRerolls());
} }
public void processInput() { public void processInput() {
Input input = Gdx.input; Input input = Gdx.input;
if (input.isKeyJustPressed(Input.Keys.R) && round.getRerolls() > 0) { //reroll dice that aren't locked if (input.isKeyJustPressed(Input.Keys.R) && round.getRerolls() > 0) { //reroll dice that aren't locked
dice.stream().filter(d -> !d.isSelected()).forEach(Die::roll); reroll();
round.reduceRerolls(1); round.reduceRerolls(0);
} }
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode if (input.isKeyPressed(Input.Keys.SHIFT_LEFT)) {
Die die = dice.get(i); //die iterator is looking at for (int i = 0; i < round.getSegments().size(); i++) {
int keyCode = Input.Keys.NUM_1 + i; //keycode for the current die, 1, 2...9, 0 on keyboard int keyCode = Input.Keys.NUM_1 + i; //keycode for the current segment, shift + 1, 2...9, 0 on keyboard
if (input.isKeyJustPressed(keyCode)) { //if key corresponding to die has been pressed if (input.isKeyJustPressed(keyCode)) {
die.setSelected(!die.isSelected()); //flip lock state Segment segment = round.getSegments().get(i);
if (segment.isDestroyedBy(getSelectedDice())) { //if can be destroyed with selected
segment.destroy();
round.getDecays().forEach(d -> d.getDecayed(getSelectedDice()).forEach(Die::decay)); //apply all decay rules
dice.forEach(d -> d.setSelected(false));
// reroll();
}
}
}
} else {
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode
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 die = dice.get(i); //die iterator is looking at
die.setSelected(!die.isSelected()); //flip lock state
}
} }
} }
} }
private void reroll() {
dice.stream().filter(d -> !d.isSelected()).forEach(Die::roll);
}
@Override @Override
public void render() { public void render() {
tickProgress += Gdx.graphics.getDeltaTime() * TICKS_PER_SECOND; tickProgress += Gdx.graphics.getDeltaTime() * TICKS_PER_SECOND;
@ -103,20 +112,24 @@ public class Game extends ApplicationAdapter {
} }
int y = Gdx.graphics.getHeight() - 50; int y = Gdx.graphics.getHeight() - 50;
for (Segment segment : segments) { for (Segment segment : round.getSegments()) {
String prefix = "[#9E65A8]"; String prefix = "[#9E65A8]";
if (segment.isDestroyed()) prefix = "[#EBE5EC]"; if (segment.isDestroyed()) prefix = "[#EBE5EC]";
else if (segment.isDestroyedBy(getSelectedDice())) prefix = "[#528154]"; else if (segment.isDestroyedBy(getSelectedDice())) prefix = "[#528154]";
font.draw(batch, prefix + segment.getName(), 100, y -= 20); font.draw(batch, prefix + segment.getName(), 100, y -= 20);
} }
y -= 50;
for (Decay decay : round.getDecays()) {
font.draw(batch, "[#9E65A8]" + decay.getDescription(), 100, y -= 20);
}
//----- //-----
ui.render(batch); ui.render(batch);
batch.end(); batch.end();
} }
@Override @Override
public void dispose() { public void dispose() {
batch.dispose(); batch.dispose();
@ -129,4 +142,8 @@ public class Game extends ApplicationAdapter {
.toList(); .toList();
} }
public Round getRound() {
return round;
}
} }

View file

@ -0,0 +1,22 @@
package com.monjaro.gamejam.main;
import java.util.List;
public class ParityDecay extends Decay {
private final int parity;
public ParityDecay(boolean odd) {
parity = odd ? 1 : 0;
description = "All used dice with " + (odd ? "ODD" : "EVEN") + " value decay.";
}
@Override
public List<Die> getDecayed(List<Die> dice) {
return dice.stream()
.filter(d -> !d.isFaceBlank())
.filter(d -> d.getFaceValue() % 2 == parity)
.toList();
}
}

View file

@ -7,11 +7,27 @@ import java.util.List;
public class Round { public class Round {
private final List<Segment> segments; private final List<Segment> segments;
private final List<Decay> decays;
private final int maxRerolls;
private int rerolls; private int rerolls;
public Round(List<Segment> segments, int rerolls){ public Round(List<Segment> segments, List<Decay> decays, int rerolls) {
this.rerolls = rerolls;
this.segments = segments; this.segments = segments;
this.decays = decays;
maxRerolls = rerolls;
this.rerolls = rerolls;
}
public List<Segment> getSegments() {
return segments;
}
public List<Decay> getDecays() {
return decays;
}
public int getMaxRerolls() {
return maxRerolls;
} }
public int getRerolls() { public int getRerolls() {

View file

@ -5,17 +5,16 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class UI extends Actor{ public class UI extends Actor{
private Transform position;
private final Game game;
private final Transform position;
private static Texture rerollTexture; private static Texture rerollTexture;
private int rerolls; private int rerolls;
private int remainingRerolls;
public UI(int x, int y, int rerolls) public UI(Game game, int x, int y) {
{ this.game = game;
position = new Transform(x, y, 0, 0); position = new Transform(x, y, 0, 0);
this.rerolls = rerolls;
this.remainingRerolls = rerolls;
} }
public void setPosition(int x, int y){ public void setPosition(int x, int y){
@ -23,7 +22,9 @@ public class UI extends Actor{
position.y = y; position.y = y;
} }
public void setRemainingRerolls(int x){remainingRerolls = x;} public void setRerolls(int x){
rerolls = x;
}
public static void setRerollTexture(Texture texture){rerollTexture = texture;} public static void setRerollTexture(Texture texture){rerollTexture = texture;}
@ -34,8 +35,8 @@ public class UI extends Actor{
@Override @Override
public void render(SpriteBatch batch) { public void render(SpriteBatch batch) {
for (int i = 0; i < rerolls; i++) { for (int i = 0; i < game.getRound().getMaxRerolls(); i++) {
if (i > remainingRerolls-1) if (i >= game.getRound().getRerolls())
batch.setColor(Color.GRAY); batch.setColor(Color.GRAY);
batch.draw(rerollTexture, (position.x + (40f*i)), (position.y)); batch.draw(rerollTexture, (position.x + (40f*i)), (position.y));
batch.setColor(Color.WHITE); batch.setColor(Color.WHITE);

View file

@ -30,15 +30,17 @@ public class OlympicSegment extends Segment {
.sorted() .sorted()
.toList(); .toList();
int last = -1, run = 1, best = run; int last = -1, run = 1, best = 0;
for (int value : values) { for (int value : values) {
if (value == last + 1) { if (value == last + 1) {
if (++run > best) best = run; run++;
} else { } else {
run = 1; run = 1;
} }
if (run > best) best = run;
last = value; last = value;
} }