dice now raise and twist

This commit is contained in:
Rosia E Evans 2024-04-21 00:24:27 +01:00
parent fe4a77b32a
commit bf25eaf84a
10 changed files with 87 additions and 29 deletions

View file

@ -1,14 +1,18 @@
package com.monjaro.gamejam;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
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.Random;
public class Die extends Actor {
private final Rectangle shape;
private Transform transform;
private int rotation;
/*
0
1 2 3 4
@ -19,34 +23,35 @@ public class Die extends Actor {
private int faceIndex = 3;
private boolean locked = false;
private static Texture lockedSprite;
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]);
faces[i] = new Face(pips[i], transform);
}
shape = new Rectangle();
}
public Die(float x, float y, float width, float height) {
transform = new Transform(x, y, width, height);
int[] pips = {4, 6, 5, 1, 2, 3};
for (int i = 0; i < faces.length; i++) {
faces[i] = new Face(pips[i]);
faces[i].setPosition(x, y);
faces[i].setSize(width, height);
faces[i] = new Face(pips[i], transform);
}
shape = new Rectangle(x, y, width, height);
}
public void setPosition(float x, float y){
shape.setX(x);
shape.setY(y);
transform.setX(x);
transform.setY(y);
}
public void setSize(float w, float h){
shape.setSize(w, h);
transform.setSize(w, h);
}
@Override
@ -65,6 +70,10 @@ public class Die extends Actor {
}
}
public static void setLockedSprite(Texture sprite){
lockedSprite = sprite;
}
public void decay() { //remove a pip from all faces of this die
for (Face face : faces) {
List<Face.Pip> pips = face.getPips();
@ -86,6 +95,18 @@ public class Die extends Actor {
}
public void setLocked(boolean locked) {
if (locked != this.locked)
{
if (locked) {
transform.y += 64;
transform.rotation = 20;
}
else{
transform.y -= 64;
transform.rotation = 0;
}
} // terrible
this.locked = locked;
}

View file

@ -1,6 +1,8 @@
package com.monjaro.gamejam;
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.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
@ -13,7 +15,7 @@ import java.util.Random;
public class Face extends Actor{
private Rectangle shape = new Rectangle();
private Transform transform;
private final List<Pip> pips = new ArrayList<>();
@ -21,15 +23,16 @@ public class Face extends Actor{
private static Texture pipSprite;
public Face(int pipCount) {
public Face(int pipCount, Transform transform) {
addPipsForValue(pipCount);
this.transform = transform;
}
private void addPipsForValue(int value){
int[][] positions = {{25, 25}, {75, 75}, {25, 75}, {75, 25}, {25, 50}, {75, 50}};
int[][] positions = {{-25, -25}, {25, 25}, {-25, 25}, {25, -25}, {-25, 0}, {25, 0}};
if (value % 2 == 1) {
pips.add(new Pip(50, 50));
pips.add(new Pip(0, 0));
value--;
}
@ -73,12 +76,12 @@ public class Face extends Actor{
}
public void setPosition(float x, float y){
shape.setX(x);
shape.setY(y);
transform.setX(x);
transform.setY(y);
}
public void setSize(float w, float h){
shape.setSize(w, h);
transform.setSize(w, h);
}
public static void setBlankFaceSprite(Texture sprite){
@ -89,13 +92,15 @@ public class Face extends Actor{
pipSprite = sprite;
}
public Vector2 getPipLocationFromPercentage(Vector2 percentages)
{
Vector2 position = new Vector2(
shape.x + (shape.width*percentages.x/100f) - (float)pipSprite.getWidth()/2,
shape.y + shape.width*percentages.y/100f - (float)pipSprite.getHeight()/2);
public Vector2 calculatePipLocation(Vector2 percentages) {
double radians = Math.toRadians(transform.rotation);
return position;
float x = (float) (percentages.x * Math.cos(radians) - percentages.y * Math.sin(radians));
float y = (float) (percentages.x * Math.sin(radians) + percentages.y * Math.cos(radians));
return new Vector2(
transform.x + transform.width*x/100f - (float)pipSprite.getWidth()/2,
transform.y + transform.height*y/100f - (float)pipSprite.getHeight()/2);
}
@Override
@ -105,9 +110,14 @@ public class Face extends Actor{
@Override
public void render(SpriteBatch batch) {
batch.draw(blankFaceSprite, shape.x, shape.y, shape.width, shape.height);
Sprite face = new Sprite(blankFaceSprite);
face.setOrigin(face.getWidth()/2, face.getHeight()/2);
face.rotate(transform.getRotation());
face.setPosition(transform.x-face.getWidth()/2, transform.y-face.getHeight()/2);
face.draw(batch);
for(Pip pip : pips){
Vector2 position = getPipLocationFromPercentage(pip.getVectorLocation());
Vector2 position = calculatePipLocation(pip.getVectorLocation());
batch.draw(pipSprite,
position.x,
position.y);

View file

@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
import java.util.ArrayList;
@ -35,9 +36,12 @@ public class Game extends ApplicationAdapter {
Face.setBlankFaceSprite(new Texture("blank_die_face.png"));
Face.setPipSprite(new Texture("pip.png"));
Die.setLockedSprite(new Texture("locked_die_border.png"));
for (int i = 1; i <= 5; i++) {
dice.add(new Die((i*80), 20, 64, 64));
Vector2 dieSize = new Vector2();
float divide = Gdx.graphics.getWidth() / 6f;
for (int i = 0; i < 5; i++) {
dice.add(new Die(divide * (i + 1), 350, 64, 64));
}
}

View file

@ -0,0 +1,23 @@
package com.monjaro.gamejam;
import com.badlogic.gdx.math.Rectangle;
public class Transform extends Rectangle {
float rotation;
public Transform(float x, float y, float width, float height){
super(x, y, width, height);
}
public Transform(){
super();
}
public float getRotation() {
return rotation;
}
public void setRotation(float rotation) {
this.rotation = rotation;
}
}