mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-06-27 19:54:43 +02:00
Merge branch 'player-and-zoom'
This commit is contained in:
@ -2,11 +2,15 @@ package inf112.fiasko.roborally;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.InputProcessor;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import inf112.fiasko.roborally.game.Game;
|
||||
import inf112.fiasko.roborally.game.IDrawableGame;
|
||||
import inf112.fiasko.roborally.objects.IDrawableObject;
|
||||
@ -17,15 +21,23 @@ import java.util.List;
|
||||
/**
|
||||
* This class renders a game using libgdx
|
||||
*/
|
||||
public class GameLauncher extends ApplicationAdapter {
|
||||
public class GameLauncher extends ApplicationAdapter implements InputProcessor {
|
||||
private OrthographicCamera camera;
|
||||
private SpriteBatch batch;
|
||||
private IDrawableGame game;
|
||||
private IDrawableGame debugGame;
|
||||
|
||||
private Texture robotTexture;
|
||||
private Texture textureSheet;
|
||||
|
||||
private final int tileDimensions = 64;
|
||||
private float cameraZoom = 1;
|
||||
private int cameraX = 0;
|
||||
private int cameraY = 0;
|
||||
private Vector2 lastTouch;
|
||||
private final int viewPortWidth = 12 * tileDimensions;
|
||||
private final int viewPortHeight = 12 * tileDimensions;
|
||||
private boolean debugging = false;
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
@ -33,11 +45,16 @@ public class GameLauncher extends ApplicationAdapter {
|
||||
robotTexture = new Texture(Gdx.files.internal("assets/Robot.png"));
|
||||
textureSheet = new Texture(Gdx.files.internal("assets/tiles.png"));
|
||||
|
||||
game = new Game();
|
||||
debugGame = new Game(true);
|
||||
game = new Game(false);
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, game.getWidth() * tileDimensions,
|
||||
game.getHeight() * tileDimensions);
|
||||
camera.setToOrtho(false, viewPortWidth, viewPortHeight);
|
||||
camera.position.set(viewPortWidth/2f, viewPortHeight/2f, 0);
|
||||
batch = new SpriteBatch();
|
||||
/*MyTextInputListener listener = new MyTextInputListener();
|
||||
Gdx.input.getTextInput(listener, "Input name", "", "Name");*/
|
||||
Gdx.input.setInputProcessor(this);
|
||||
lastTouch = new Vector2();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,6 +66,10 @@ public class GameLauncher extends ApplicationAdapter {
|
||||
camera.update();
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
batch.begin();
|
||||
camera.translate(cameraX, cameraY);
|
||||
cameraX = 0;
|
||||
cameraY = 0;
|
||||
camera.zoom = cameraZoom;
|
||||
//Draws all elements the game wants to draw
|
||||
List<IDrawableObject> elementsToDraw = IOUtil.getDrawableObjectsFromGame(game, tileDimensions, tileDimensions);
|
||||
for (IDrawableObject object : elementsToDraw) {
|
||||
@ -69,4 +90,125 @@ public class GameLauncher extends ApplicationAdapter {
|
||||
textureSheet.dispose();
|
||||
batch.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyDown(int keycode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyUp(int keycode) {
|
||||
switch (keycode) {
|
||||
case Input.Keys.PLUS:
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
|
||||
cameraZoom -= 0.1;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Input.Keys.MINUS:
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.CONTROL_LEFT)) {
|
||||
cameraZoom += 0.1;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case Input.Keys.HOME:
|
||||
IDrawableGame temp = game;
|
||||
this.game = debugGame;
|
||||
this.debugGame = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean keyTyped(char character) {
|
||||
if (character == 'r') {
|
||||
//camera.rotate(-90);
|
||||
camera.rotateAround(
|
||||
new Vector3(viewPortWidth/2f, viewPortHeight/2f, 0),
|
||||
new Vector3(0, 0, 1), 90);
|
||||
return true;
|
||||
} else if (character == 'q') {
|
||||
camera.up.x = 0;
|
||||
camera.up.y = 1;
|
||||
cameraZoom = 1;
|
||||
System.out.print((game.getHeight() * tileDimensions) - viewPortHeight/2f);
|
||||
camera.position.set(viewPortWidth/2f, viewPortHeight/2f, 0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
|
||||
lastTouch = new Vector2(screenX, screenY);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
|
||||
lastTouch = new Vector2(screenX, screenY);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean touchDragged(int screenX, int screenY, int pointer) {
|
||||
Vector2 newTouch = new Vector2(screenX, screenY);
|
||||
Vector2 diff = newTouch.cpy().sub(lastTouch);
|
||||
lastTouch = newTouch;
|
||||
int[] positionChange = translateCoordinateAccountingForCameraRotation(diff.x, diff.y);
|
||||
cameraX = positionChange[0];
|
||||
cameraY = positionChange[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates x and y coordinates according to the camera's direction
|
||||
* @param x The x coordinate to translate
|
||||
* @param y The y coordinate to translate
|
||||
* @return A list containing the translated coordinates of x and y
|
||||
*/
|
||||
private int[] translateCoordinateAccountingForCameraRotation(float x, float y) {
|
||||
int outX = 0;
|
||||
int outY = 0;
|
||||
int cameraUpX = Math.round(camera.up.x);
|
||||
int cameraUpY = Math.round(camera.up.y);
|
||||
if (cameraUpX == 0 && Math.round(camera.up.y) == 1) {
|
||||
outX = (int)-x;
|
||||
outY = (int)y;
|
||||
} else if (cameraUpX == 0 && cameraUpY == -1) {
|
||||
outX = (int)x;
|
||||
outY = (int)-y;
|
||||
} else if (cameraUpX == -1 && cameraUpY == 0) {
|
||||
outX = (int)-y;
|
||||
outY = (int)-x;
|
||||
} else if (cameraUpX == 1 && cameraUpY == 0) {
|
||||
outX = (int)y;
|
||||
outY = (int)x;
|
||||
}
|
||||
return new int[]{outX, outY};
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mouseMoved(int screenX, int screenY) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean scrolled(int amount) {
|
||||
if (amount < 0 && cameraZoom > 0 || amount > 0 && cameraZoom < 2) {
|
||||
cameraZoom += amount / 10.0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*public static class MyTextInputListener implements Input.TextInputListener {
|
||||
@Override
|
||||
public void input (String text) {
|
||||
System.out.println(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void canceled () {
|
||||
}
|
||||
}*/
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package inf112.fiasko.roborally.element_properties;
|
||||
|
||||
/**
|
||||
* This enum represents an action on a programming card
|
||||
*/
|
||||
public enum Action {
|
||||
ROTATE_RIGHT,
|
||||
ROTATE_LEFT,
|
||||
U_TURN,
|
||||
MOVE_1,
|
||||
MOVE_2,
|
||||
MOVE_3,
|
||||
BACK_UP
|
||||
}
|
@ -26,7 +26,15 @@ public enum TileType {
|
||||
FLAG_4 (20),
|
||||
WRENCH (21),
|
||||
WRENCH_AND_HAMMER (22),
|
||||
DEATH_TILE (23);
|
||||
DEATH_TILE (23),
|
||||
ROBOT_SPAWN_1 (24),
|
||||
ROBOT_SPAWN_2 (25),
|
||||
ROBOT_SPAWN_3 (26),
|
||||
ROBOT_SPAWN_4 (27),
|
||||
ROBOT_SPAWN_5 (28),
|
||||
ROBOT_SPAWN_6 (29),
|
||||
ROBOT_SPAWN_7 (30),
|
||||
ROBOT_SPAWN_8 (31);
|
||||
|
||||
private final int tileTypeID;
|
||||
|
||||
|
@ -19,11 +19,33 @@ import java.util.concurrent.TimeUnit;
|
||||
public class Game implements IDrawableGame {
|
||||
private Board gameBoard;
|
||||
|
||||
public Game(boolean debug) {
|
||||
if (debug) {
|
||||
initializeDebugMode();
|
||||
} else {
|
||||
initializeGame();
|
||||
}
|
||||
}
|
||||
|
||||
public Game() {
|
||||
initializeGame();
|
||||
}
|
||||
|
||||
private void initializeDebugMode() {
|
||||
List<Robot> robots = new ArrayList<>();
|
||||
try {
|
||||
gameBoard = BoardLoaderUtil.loadBoard("boards/all_tiles_test_board.txt", robots);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeGame() {
|
||||
try {
|
||||
List<Robot> robots = new ArrayList<>();
|
||||
robots.add(new Robot(RobotID.ROBOT_1, new Position(1, 1)));
|
||||
robots.add(new Robot(RobotID.ROBOT_2, new Position(1, 2)));
|
||||
robots.add(new Robot(RobotID.ROBOT_3, new Position(1, 3)));
|
||||
gameBoard = BoardLoaderUtil.loadBoard("boards/Checkmate.txt", robots);
|
||||
new Thread(() -> {
|
||||
try {
|
||||
|
@ -0,0 +1,20 @@
|
||||
package inf112.fiasko.roborally.objects;
|
||||
|
||||
/**
|
||||
* This Interface describes a card without a card suit
|
||||
* @param <S> The value type
|
||||
* @param <T> The symbol type
|
||||
*/
|
||||
public interface ICardWithoutSuit<S,T> {
|
||||
/**
|
||||
* Gets the value of the card
|
||||
* @return The card value
|
||||
*/
|
||||
S getValue();
|
||||
|
||||
/**
|
||||
* Gets the symbol of the card
|
||||
* @return The card symbol
|
||||
*/
|
||||
T getSymbol();
|
||||
}
|
@ -25,6 +25,7 @@ public final class TextureConverterUtil {
|
||||
private static final Texture textureSheet = new Texture(Gdx.files.internal("assets/tiles.png"));
|
||||
private static final Texture robot1Texture = new Texture(Gdx.files.internal("assets/Robot.png"));
|
||||
private static final Texture robot2Texture = new Texture(Gdx.files.internal("assets/Robot2.png"));
|
||||
private static final Texture robot3Texture = new Texture(Gdx.files.internal("assets/Robot3.png"));
|
||||
private static Map<TileType, TextureConverterContainer> tileSheetTileTextureMappings;
|
||||
private static Map<TileType, Boolean> tileSheetTileHasRotatedTextureMappings;
|
||||
private static Map<WallType, TextureConverterContainer> tileSheetWallTextureMappings;
|
||||
@ -87,6 +88,8 @@ public final class TextureConverterUtil {
|
||||
return new TextureRegion(robot1Texture, 0, 0, 64, 64);
|
||||
} else if (robot.getRobotId() == RobotID.ROBOT_2) {
|
||||
return new TextureRegion(robot2Texture, 0, 0, 64, 64);
|
||||
} else if (robot.getRobotId() == RobotID.ROBOT_3) {
|
||||
return new TextureRegion(robot3Texture, 0, 0, 64, 64);
|
||||
}
|
||||
throw new IllegalArgumentException("Robot has no drawable texture.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user