Advertisement
Guest User

Untitled

a guest
Apr 26th, 2013
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Copyright (c) 2013 Katharina Sabel
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package bucket.game.client.actors;
  18.  
  19. // Imports from framework
  20. import map.SolarSystem;
  21.  
  22. // LibGDX imports
  23. import com.badlogic.gdx.Gdx;
  24. import com.badlogic.gdx.graphics.OrthographicCamera;
  25. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  26. import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  27. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  28. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  29. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  30. import com.badlogic.gdx.math.Vector2;
  31. import com.badlogic.gdx.scenes.scene2d.Actor;
  32. import com.badlogic.gdx.scenes.scene2d.Touchable;
  33. import com.badlogic.gdx.utils.Disposable;
  34.  
  35. /**
  36.  * HexMap implementation.
  37.  *
  38.  * @author Katharina
  39.  *
  40.  */
  41.  
  42. public class HexMap extends Actor implements Disposable {
  43.  
  44.     private float sizeX, sizeY; // Size of the map-actor on screen
  45.     private Vector2 tileID; // Tile ID on the global map.
  46.     private float tileX, tileY; // Tile size to let the view know when to stop drawing tiles
  47.     private float vTile, hTile; // Number of tiles possible on the screen
  48.  
  49.     /** Actor Stuff here */
  50.  
  51.     private TextureAtlas atlas; // Holds all Tile textures
  52.     private TextureRegion hosTile, friendTile, neuTile, playTile;
  53.     private ShapeRenderer shapeRenderer;
  54.  
  55.     /** New Render stuff here */
  56.     private OrthographicCamera camera;
  57.  
  58.     /**
  59.      * Will draw the map. At some point
  60.      */
  61.     public void draw(SpriteBatch batch, float parentAlpha) {
  62.  
  63.         batch.end();
  64.         shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());
  65.         shapeRenderer.setTransformMatrix(batch.getTransformMatrix());
  66.         shapeRenderer.translate(getX() / 2, getY() / 2, 0);
  67.  
  68.         shapeRenderer.begin(ShapeType.Rectangle);
  69.         shapeRenderer.rect(0, 0, sizeX + 20, sizeY);
  70.         shapeRenderer.end();
  71.         batch.begin();
  72.  
  73.         // Drawing a few tiles. Not mathematically correct but sue me!
  74.         for (int n = 0; n <= 3; n++) {
  75.  
  76.             for (int m = 0; m <= 3; m++)
  77.                 batch.draw(neuTile, (getX() / 2) + (n * tileX), (getY() / 2) + (m * tileY), 0, 0, 100, 100, 1, 1, 0);
  78.         }
  79.  
  80.         for (int n = 0; n <= 3; n++) {
  81.  
  82.             for (int m = 0; m <= 2; m++)
  83.                 batch.draw(neuTile, (getX() / 2) + (n * tileX + 75), (getY() / 2) + (m * tileY + 42.5f), 0, 0, 100, 100, 1, 1,
  84.                         0);
  85.         }
  86.  
  87.     }
  88.  
  89.     public HexMap(float x, float y) {
  90.         this.sizeX = x;
  91.         this.sizeY = y;
  92.         this.tileX = 150;
  93.         this.tileY = 85;
  94.  
  95.         shapeRenderer = new ShapeRenderer();
  96.  
  97.         loadTextures();
  98.     }
  99.  
  100.     /**
  101.      * Loads the Tile TextureRegions from the atlas. Moved to the @ResourcePacker in
  102.      * P-1.0.3.
  103.      */
  104.     @Deprecated
  105.     private void loadTextures() {
  106.  
  107.         this.atlas = new TextureAtlas(Gdx.files.internal("assets/map/prot-map-tiles.pack"));
  108.  
  109.         // See what I did there? ;)
  110.         hosTile = atlas.findRegion("prot-map-tile-hostile");
  111.         friendTile = atlas.findRegion("prot-map-tile-friend");
  112.         neuTile = atlas.findRegion("prot-map-tile-neutral");
  113.         playTile = atlas.findRegion("prot-map-tile-player");
  114.  
  115.     }
  116.  
  117.     /**
  118.      * I got this method from another class I found somewhere. Not really sure what it does
  119.      * and when it is being called :/
  120.      */
  121.     public Actor hit(float x, float y, boolean touchable) {
  122.         if (touchable && getTouchable() != Touchable.enabled)
  123.             return null;
  124.         return x >= 0 && x < getWidth() && y >= 0 && y < getHeight() ? this : null;
  125.     }
  126.  
  127.     /**
  128.      * Usually only called once when creating the application. Players with custom skins and
  129.      * graphic packs may end up changing these values. Checking for tile size is done
  130.      * outside this class.
  131.      *
  132.      * @param x
  133.      * @param y
  134.      */
  135.     public void setTileSize(float x, float y) {
  136.  
  137.         this.tileX = x;
  138.         this.tileY = y;
  139.  
  140.         this.hTile = sizeX / tileX;
  141.         this.vTile = sizeY / tileY;
  142.  
  143.     }
  144.  
  145.     /**
  146.      * Gets the size of a generic tile as a vector.
  147.      *
  148.      * @return Vector2 object with tile size as x and y paramenters
  149.      */
  150.     public Vector2 getTileSize() {
  151.  
  152.         Vector2 size = new Vector2();
  153.         size.x = tileX;
  154.         size.y = tileY;
  155.  
  156.         return size;
  157.     }
  158.  
  159.     /**
  160.      * Checks if the requested tile should even be displayed or if it is outside the
  161.      * requested view.
  162.      *
  163.      * @param tileID
  164.      *          Vector ID of the tile in question
  165.      *
  166.      * @return true: The tile is on screen and needs to be displayed. false: The tile is no
  167.      *         on screen and shouldn't be displayed.
  168.      */
  169.     public boolean isTileOnScreen(Vector2 tileID) {
  170.  
  171.         boolean result = false;
  172.  
  173.         // TODO: Get top left tile (Somehow? :s)
  174.  
  175.         // TODO: Then check
  176.         // if(dist(topleft --> tileID) > hTile || vTile)
  177.         // ### result --> false
  178.         // ### else result --> true
  179.  
  180.         return result;
  181.     }
  182.  
  183.     public SolarSystem getTileWithID(Vector2 tileID) {
  184.         return null;
  185.  
  186.         // TODO: This will check the QuadTree for tile info
  187.  
  188.     }
  189.  
  190.     /**
  191.      * Am I supposed to do something with this stub? :|
  192.      */
  193.     @Override
  194.     public void dispose() {
  195.  
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement