Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.benberi.cadesim.game.scene.impl.battle;
- import com.badlogic.gdx.Gdx;
- 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.benberi.cadesim.GameContext;
- import com.benberi.cadesim.game.scene.GameScene;
- import com.benberi.cadesim.game.scene.impl.battle.tile.GameTile;
- import com.benberi.cadesim.util.OrientationLocation;
- import com.benberi.cadesim.util.PackedShipOrientation;
- import com.benberi.cadesim.util.SimpleFileUtils;
- public class SeaBattleScene implements GameScene {
- /**
- * The main game context
- */
- private GameContext context;
- /**
- * The sprite batch renderer
- */
- private SpriteBatch batch;
- /**
- * The battle map
- */
- private SeaMap map;
- /**
- * The camera view of the scene
- */
- private OrthographicCamera camera;
- public SeaBattleScene(GameContext context) {
- this.context = context;
- }
- @Override
- public void create() {
- this.batch = new SpriteBatch();
- map = new SeaMap();
- camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- }
- @Override
- public void update() {
- camera.update();
- }
- @Override
- public void render() {
- batch.setProjectionMatrix(camera.combined);
- batch.begin();
- // Render the map
- renderSeaBattle();
- // Render ships
- renderEntities();
- batch.end();
- }
- @Override
- public void dispose() {
- }
- @Override
- public void handleDrag(float x, float y) {
- camera.translate(-x, y);
- }
- @Override
- public void handleClick(float x, float y) {
- }
- private int shipRotationIndex = 14; // this is a temporary for testing, ship rotation
- private void renderEntities() {
- Texture texture = new Texture("core/assets/vessel/wf/spritesheet.png");
- PackedShipOrientation o = context.getTools().getGson().fromJson(SimpleFileUtils.readStringFromFile("core/assets/vessel/wf/properties.json"), PackedShipOrientation.class);
- OrientationLocation location = o.getOrienation(shipRotationIndex);
- TextureRegion region = new TextureRegion(texture, location.getX(), location.getY(), location.getWidth(), location.getHeight());
- int i = 1;
- int j = 1;
- int x = getIsometricX(i,j,region);
- int y = getIsometricY(i,j,region);
- batch.draw(region, x + location.getOffsetx(), y + location.getOffsety());
- }
- private void processShipRotation() {
- shipRotationIndex++;
- if (shipRotationIndex > 15) {
- shipRotationIndex = 0;
- }
- }
- public int getIsometricX(int x, int y, TextureRegion region) {
- return (x * GameTile.TILE_WIDTH / 2) - (y * GameTile.TILE_WIDTH / 2) - (region.getRegionWidth() / 2);
- }
- public int getIsometricY(int x, int y, TextureRegion region) {
- return (x * GameTile.TILE_HEIGHT / 2) + (y * GameTile.TILE_HEIGHT / 2) - (region.getRegionHeight() / 2);
- }
- private void renderSeaBattle() {
- // The map tiles
- GameTile[][] tiles = map.getTiles();
- for (int i = 0; i < tiles.length; i++) {
- for(int j = 0; j < tiles[i].length; j++) {
- GameTile tile = tiles[i][j];
- Texture texture = tile.getTexture();
- int x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) -texture.getWidth() / 2;
- int y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) -texture.getHeight() / 2;
- batch.draw(texture, x, y);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement