1
Fork 0

Create a new general-purpose unit class, basic turn-ordering and arrows

This commit is contained in:
Joshua Goins 2022-05-01 17:13:11 -04:00
parent 970988a5eb
commit 41d1fa55ef
6 changed files with 207 additions and 39 deletions

48
assets/test.atlas Normal file
View file

@ -0,0 +1,48 @@
test.png
size: 128, 32
format: RGBA8888
filter: Nearest, Nearest
repeat: none
cross-down
rotate: false
xy: 56, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
down
rotate: false
xy: 20, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
up
rotate: false
xy: 92, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
left
rotate: false
xy: 38, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
cross-up
rotate: false
xy: 2, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1
right
rotate: false
xy: 74, 2
size: 16, 16
orig: 16, 16
offset: 0, 0
index: -1

BIN
assets/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

View file

@ -53,8 +53,6 @@ public class AStar {
current = current.parent;
}
System.out.println(path.toString());
return path;
}

View file

@ -1,6 +0,0 @@
package com.redstrate.watersymbol;
public class Player {
public int positionX;
public int positionY;
}

View file

@ -0,0 +1,14 @@
package com.redstrate.watersymbol;
public class Unit {
public int positionX;
public int positionY;
public enum Team {
Player,
Enemy,
Ally
}
public Team team;
}

View file

@ -5,6 +5,8 @@ import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
@ -13,10 +15,11 @@ import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.ScreenUtils;
import com.redstrate.watersymbol.AStar;
import com.redstrate.watersymbol.Player;
import com.redstrate.watersymbol.Unit;
import com.redstrate.watersymbol.WaterSymbol;
import java.util.ArrayList;
import java.util.Collections;
public class GameScreen implements Screen {
private final WaterSymbol game;
@ -24,11 +27,18 @@ public class GameScreen implements Screen {
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
Player player;
ArrayList<Unit> units = new ArrayList<>();
Unit.Team currentTurnTeam = Unit.Team.Player;
Unit currentUnit = null;
int currentUnitIndex = 0;
int currentTurn = 0;
Texture playerTexture;
Sprite playerSprite;
TextureAtlas arrowAtlas;
GameScreen(WaterSymbol game) {
this.game = game;
@ -38,11 +48,26 @@ public class GameScreen implements Screen {
tiledMap = new TmxMapLoader().load("test.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
player = new Player();
Unit player = new Unit();
player.positionX = 0;
player.positionY = 0;
player.team = Unit.Team.Player;
units.add(player);
Unit enemy = new Unit();
enemy.positionX = 5;
enemy.positionY = 0;
enemy.team = Unit.Team.Enemy;
units.add(enemy);
playerTexture = new Texture(Gdx.files.internal("player.png"));
playerSprite = new Sprite(playerTexture);
arrowAtlas = new TextureAtlas("test.atlas");
startNewTeamTurn(Unit.Team.Player);
}
@Override
@ -50,52 +75,141 @@ public class GameScreen implements Screen {
}
void getNextUnit() {
currentUnit = null;
for(int i = currentUnitIndex; i < units.size(); i++) {
if(units.get(currentUnitIndex).team == currentTurnTeam) {
currentUnit = units.get(currentUnitIndex);
}
}
}
void startNewTeamTurn(Unit.Team team) {
currentTurnTeam = team;
currentUnitIndex = 0;
getNextUnit();
}
void advanceTurn() {
currentUnitIndex++;
getNextUnit();
if(currentUnit == null) {
if(currentTurnTeam == Unit.Team.Player) {
startNewTeamTurn(Unit.Team.Enemy);
} else if(currentTurnTeam == Unit.Team.Enemy) {
currentTurn++;
startNewTeamTurn(Unit.Team.Player);
}
}
}
@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0.2f, 1);
camera.position.set(player.positionX * 32, player.positionY * 32, 0);
if(currentUnit != null)
camera.position.set(currentUnit.positionX * 32, currentUnit.positionY * 32, 0);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
game.batch.begin();
game.batch.setProjectionMatrix(camera.combined);
playerSprite.setPosition(player.positionX * 32, player.positionY * 32);
playerSprite.draw(game.batch);
// player is trying to move
if(Gdx.input.isButtonPressed(0)) {
hasStartedMoving = true;
for(Unit unit : units) {
playerSprite.setPosition(unit.positionX * 32, unit.positionY * 32);
playerSprite.draw(game.batch);
}
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
if(currentTurnTeam == Unit.Team.Player && currentUnit != null) {
// player is trying to move
if (Gdx.input.isButtonPressed(0)) {
hasStartedMoving = true;
camera.unproject(mousePos);
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y);
camera.unproject(mousePos);
int newFixedX = (int) (newX / 32.0);
int newFixedY = (int) (newY / 32.0);
double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y);
ArrayList<Vector2> path = AStar.path(new Vector2(player.positionX, player.positionY), new Vector2((float)newFixedX, (float)newFixedY));
if(path != null) {
for (Vector2 position : path) {
game.batch.draw(playerTexture, position.x * 32, position.y * 32);
int newFixedX = (int) (newX / 32.0);
int newFixedY = (int) (newY / 32.0);
ArrayList<Vector2> path = AStar.path(new Vector2(currentUnit.positionX, currentUnit.positionY), new Vector2((float) newFixedX, (float) newFixedY));
Collections.reverse(path);
if (path != null) {
// we start at index 1 since there's no point in drawing an arrow over the player
for (int i = 1; i < path.size(); i++) {
Vector2 previousPosition = path.get(Math.max(i - 1, 0));
Vector2 newPosition = path.get(i);
Vector2 d = new Vector2(newPosition.x - previousPosition.x, newPosition.y - previousPosition.y);
TextureRegion spriteRegion = null;
// going down
if (d.epsilonEquals(new Vector2(0, -1))) {
spriteRegion = arrowAtlas.findRegion("down");
}
if (d.epsilonEquals(new Vector2(0, 1))) {
spriteRegion = arrowAtlas.findRegion("up");
}
if (d.epsilonEquals(new Vector2(-1, 0))) {
spriteRegion = arrowAtlas.findRegion("right");
}
if (d.epsilonEquals(new Vector2(1, 0))) {
spriteRegion = arrowAtlas.findRegion("left");
}
if (d.epsilonEquals(new Vector2(1, 1))) {
spriteRegion = arrowAtlas.findRegion("cross-up");
}
if (d.epsilonEquals(new Vector2(-1, -1))) {
spriteRegion = arrowAtlas.findRegion("cross-up");
}
if (d.epsilonEquals(new Vector2(1, -1))) {
spriteRegion = arrowAtlas.findRegion("cross-down");
}
if (d.epsilonEquals(new Vector2(-1, 1))) {
spriteRegion = arrowAtlas.findRegion("cross-down");
}
if (spriteRegion != null) {
game.batch.draw(spriteRegion, newPosition.x * 32, newPosition.y * 32);
}
}
}
} else if (hasStartedMoving) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(mousePos);
double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y);
currentUnit.positionX = (int) (newX / 32.0);
currentUnit.positionY = (int) (newY / 32.0);
hasStartedMoving = false;
advanceTurn();
}
} else if(hasStartedMoving) {
Vector3 mousePos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
}
camera.unproject(mousePos);
double newX = Math.floor(mousePos.x);
double newY = Math.floor(mousePos.y);
player.positionX = (int) (newX / 32.0);
player.positionY = (int) (newY / 32.0);
hasStartedMoving = false;
if(currentTurnTeam == Unit.Team.Enemy) {
advanceTurn();
}
game.batch.end();