Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package com.benberi.cadesim.game.scene.impl.battle;
  2.  
  3.  
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.OrthographicCamera;
  6. import com.badlogic.gdx.graphics.Texture;
  7. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  9. import com.benberi.cadesim.GameContext;
  10. import com.benberi.cadesim.game.scene.GameScene;
  11. import com.benberi.cadesim.game.scene.impl.battle.tile.GameTile;
  12. import com.benberi.cadesim.util.OrientationLocation;
  13. import com.benberi.cadesim.util.PackedShipOrientation;
  14. import com.benberi.cadesim.util.SimpleFileUtils;
  15.  
  16. public class SeaBattleScene implements GameScene {
  17.  
  18.     /**
  19.      * The main game context
  20.      */
  21.     private GameContext context;
  22.  
  23.     /**
  24.      * The sprite batch renderer
  25.      */
  26.     private SpriteBatch batch;
  27.  
  28.     /**
  29.      * The battle map
  30.      */
  31.     private SeaMap map;
  32.  
  33.     /**
  34.      * The camera view of the scene
  35.      */
  36.     private OrthographicCamera camera;
  37.  
  38.     public SeaBattleScene(GameContext context) {
  39.         this.context = context;
  40.     }
  41.  
  42.     @Override
  43.     public void create() {
  44.         this.batch = new SpriteBatch();
  45.         map = new SeaMap();
  46.         camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  47.     }
  48.  
  49.     @Override
  50.     public void update() {
  51.         camera.update();
  52.     }
  53.  
  54.     @Override
  55.     public void render() {
  56.         batch.setProjectionMatrix(camera.combined);
  57.         batch.begin();
  58.        
  59.         // Render the map
  60.         renderSeaBattle();
  61.        
  62.         // Render ships
  63.         renderEntities();
  64.        
  65.         batch.end();
  66.     }
  67.    
  68.     @Override
  69.     public void dispose() {
  70.  
  71.     }
  72.  
  73.     @Override
  74.     public void handleDrag(float x, float y) {
  75.         camera.translate(-x, y);
  76.     }
  77.  
  78.     @Override
  79.     public void handleClick(float x, float y) {
  80.     }
  81.  
  82.     private int shipRotationIndex = 14; // this is a temporary for testing, ship rotation
  83.    
  84.    
  85.     private void renderEntities() {
  86.         Texture texture = new Texture("core/assets/vessel/wf/spritesheet.png");
  87.  
  88.         PackedShipOrientation o = context.getTools().getGson().fromJson(SimpleFileUtils.readStringFromFile("core/assets/vessel/wf/properties.json"), PackedShipOrientation.class);
  89.  
  90.         OrientationLocation location = o.getOrienation(shipRotationIndex);
  91.  
  92.         TextureRegion region = new TextureRegion(texture, location.getX(), location.getY(), location.getWidth(), location.getHeight());
  93.        
  94.         int i = 1;
  95.         int j = 1;
  96.  
  97.         int x = getIsometricX(i,j,region);
  98.         int y = getIsometricY(i,j,region);
  99.        
  100.         batch.draw(region, x + location.getOffsetx(), y + location.getOffsety());
  101.  
  102.  
  103.     }
  104.    
  105.     private void processShipRotation() {
  106.         shipRotationIndex++;
  107.         if (shipRotationIndex > 15) {
  108.             shipRotationIndex = 0;
  109.         }
  110.     }
  111.  
  112.     public int getIsometricX(int x, int y, TextureRegion region) {
  113.         return (x * GameTile.TILE_WIDTH / 2) - (y * GameTile.TILE_WIDTH / 2) - (region.getRegionWidth() / 2);
  114.     }
  115.  
  116.     public int getIsometricY(int x, int y, TextureRegion region) {
  117.         return (x * GameTile.TILE_HEIGHT / 2) + (y * GameTile.TILE_HEIGHT / 2) - (region.getRegionHeight() / 2);
  118.     }
  119.  
  120.     private void renderSeaBattle() {
  121.  
  122.         // The map tiles
  123.         GameTile[][] tiles = map.getTiles();
  124.  
  125.         for (int i = 0; i < tiles.length; i++) {
  126.             for(int j = 0; j < tiles[i].length; j++) {
  127.  
  128.                 GameTile tile = tiles[i][j];
  129.                 Texture texture = tile.getTexture();
  130.  
  131.                 int x = (i * GameTile.TILE_WIDTH / 2) - (j * GameTile.TILE_WIDTH / 2) -texture.getWidth() / 2;
  132.                 int y = (i * GameTile.TILE_HEIGHT / 2) + (j * GameTile.TILE_HEIGHT / 2) -texture.getHeight() / 2;
  133.  
  134.                 batch.draw(texture, x, y);
  135.  
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement