how do I sprite cranberry and why to people keep asking if I wanna?

This commit is contained in:
Rosia E Evans 2024-04-21 00:27:50 +01:00
commit dbb7868659
8 changed files with 161 additions and 30 deletions

View file

@ -1,4 +1,4 @@
sourceCompatibility = 1.8 sourceCompatibility = 1.20
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8' [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ] sourceSets.main.java.srcDirs = [ "src/" ]

View file

@ -1,24 +1,19 @@
package com.monjaro.gamejam; package com.monjaro.gamejam;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class Die extends Actor { public class Die extends Actor {
private Transform transform; private Transform transform;
private int rotation;
/* /*
0 0
1 2 3 4 1 2 3 4
5 5
*/ */
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 boolean locked = false;
@ -27,14 +22,6 @@ public class Die extends Actor {
private final Random random = new Random(); //TODO use central random private final Random random = new Random(); //TODO use central random
public Die() {
transform = new Transform();
int[] pips = {4, 6, 5, 1, 2, 3};
for (int i = 0; i < faces.length; i++) {
faces[i] = new Face(pips[i], transform);
}
}
public Die(float x, float y, float width, float height) { public Die(float x, float y, float width, float height) {
transform = new Transform(x, y, width, height); transform = new Transform(x, y, width, height);
@ -65,10 +52,8 @@ public class Die extends Actor {
} }
public void roll() { public void roll() {
if (!locked) {
faceIndex = random.nextInt(6); faceIndex = random.nextInt(6);
} }
}
public static void setLockedSprite(Texture sprite){ public static void setLockedSprite(Texture sprite){
lockedSprite = sprite; lockedSprite = sprite;
@ -90,6 +75,10 @@ public class Die extends Actor {
return getFace().getValue(); return getFace().getValue();
} }
public boolean isBlank() {
return getFaceValue() <= 0;
}
public boolean isLocked() { public boolean isLocked() {
return locked; return locked;
} }

View file

@ -1,9 +1,8 @@
package com.monjaro.gamejam; package com.monjaro.gamejam;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
@ -12,7 +11,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
public class Face extends Actor{ public class Face extends Actor{
private Transform transform; private Transform transform;
@ -22,7 +20,6 @@ public class Face extends Actor{
private static Texture blankFaceSprite; private static Texture blankFaceSprite;
private static Texture pipSprite; private static Texture pipSprite;
public Face(int pipCount, Transform transform) { public Face(int pipCount, Transform transform) {
addPipsForValue(pipCount); addPipsForValue(pipCount);
this.transform = transform; this.transform = transform;

View file

@ -3,12 +3,13 @@ package com.monjaro.gamejam;
import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
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.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2; 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.KinSegment;
import com.monjaro.gamejam.segment.Segment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -20,9 +21,9 @@ public class Game extends ApplicationAdapter {
private final Set<Actor> actors = new HashSet<>(); private final Set<Actor> actors = new HashSet<>();
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 Texture img; private Texture img;
private final static int TICKS_PER_SECOND = 60; private final static int TICKS_PER_SECOND = 60;
@ -31,7 +32,6 @@ public class Game extends ApplicationAdapter {
@Override @Override
public void create() { public void create() {
batch = new SpriteBatch(); batch = new SpriteBatch();
font = new BitmapFont();
img = new Texture("badlogic.jpg"); img = new Texture("badlogic.jpg");
Face.setBlankFaceSprite(new Texture("blank_die_face.png")); Face.setBlankFaceSprite(new Texture("blank_die_face.png"));
@ -43,6 +43,12 @@ public class Game extends ApplicationAdapter {
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));
}
segments.add(new DualSegment(false));
segments.add(new DualSegment(true));
} }
public void tick() { public void tick() {
@ -54,9 +60,15 @@ 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)) { //reroll dice if (input.isKeyJustPressed(Input.Keys.R)) { //reroll dice that aren't locked
dice.forEach(Die::roll); dice.stream().filter(d -> !d.isLocked()).forEach(Die::roll);
System.out.println("=".repeat(100));
for (Segment segment : segments) {
System.out.println(segment.getName() + ": " + segment.isDestroyedBy(dice));
} }
}
for (int i = 0; i < dice.size(); i++) { //lock dice, iterating over for each keycode 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 Die die = dice.get(i); //die iterator is looking at
@ -82,7 +94,6 @@ public class Game extends ApplicationAdapter {
actors.forEach(a -> a.render(batch)); actors.forEach(a -> a.render(batch));
//TODO debug //TODO debug
int x = 100;
for (Die die : dice) { for (Die die : dice) {
die.render(batch); die.render(batch);
} }

View file

@ -0,0 +1,58 @@
package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class DualSegment extends Segment {
private final boolean firstTrio;
public DualSegment(boolean firstTrio) { //first is a trio or duo, second is a duo
this.firstTrio = firstTrio;
if (firstTrio) {
Random random = new Random();
if (random.nextInt(10) != 0) {
name = "Full House";
} else {
String[] names = new String[]{
"Complete Household",
"Entire Residence",
"Occupied Home",
"Family Residence",
"Filled Dwelling",
"Packed Abode",
"Crowded Domicile",
"Busy Household",
"Well-occupied Home",
"Houseful"
}; //thank you ChatGPT for alternate names
name = names[random.nextInt(names.length)];
}
} else {
name = "Dual Duo";
}
}
@Override
public boolean isDestroyedBy(List<Die> dice) {
Map<Integer, Integer> counts = countValues(dice);
Map<Integer, Integer> countCounts = new HashMap<>(); //count of counts
for (int count : counts.values()) {
for (int i = count; i >= 1; i--) { //add from count to 1 - this is so a trio is also a pair, e.g. 11122 is a dual duo
int countCount = countCounts.getOrDefault(i, 0) + 1;
countCounts.put(i, countCount);
}
}
return countCounts.getOrDefault(2, 0) >= 2
&& (!firstTrio || countCounts.getOrDefault(3, 0) >= 1);
}
}

View file

@ -0,0 +1,38 @@
package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.List;
import java.util.Map;
public class KinSegment extends Segment { //multiple dice of the same value
private final int requirement;
public KinSegment(int requirement) {
this.requirement = requirement;
name = switch (requirement) {
case 1 -> "Any";
case 2 -> "Duo";
case 3 -> "Trio";
case 4 -> "Quartet";
case 5 -> "Quintet";
default -> requirement + " of a kind";
};
}
@Override
public boolean isDestroyedBy(List<Die> dice) {
Map<Integer, Integer> counts = countValues(dice);
for (int count : counts.values()) {
if (count >= requirement) {
return true;
}
}
return false;
}
}

View file

@ -0,0 +1,38 @@
package com.monjaro.gamejam.segment;
import com.monjaro.gamejam.Die;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class Segment {
private boolean destroyed = false;
protected String name;
public void destroy() {
destroyed = true;
}
public abstract boolean isDestroyedBy(List<Die> die);
public String getName() {
return name;
}
protected Map<Integer, Integer> countValues(List<Die> dice) {
Map<Integer, Integer> counts = new HashMap<>();
for (Die die : dice) {
if (die.isBlank()) continue;
int count = counts.getOrDefault(die.getFaceValue(), 0) + 1;
counts.put(die.getFaceValue(), count);
}
return counts;
}
}

View file

@ -1,4 +1,4 @@
sourceCompatibility = 1.8 sourceCompatibility = 1.20
sourceSets.main.java.srcDirs = [ "src/" ] sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = ["../assets"] sourceSets.main.resources.srcDirs = ["../assets"]