Advertisement
dermetfan

digging animation test for Essa Rebano (EssaRebano.java)

Aug 4th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.45 KB | None | 0 0
  1. package net.dermetfan.someLibgdxTests.entities;
  2.  
  3. import java.util.Iterator;
  4.  
  5. import com.badlogic.gdx.Gdx;
  6. import com.badlogic.gdx.Input.Keys;
  7. import com.badlogic.gdx.InputProcessor;
  8. import com.badlogic.gdx.graphics.g2d.Sprite;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.maps.tiled.TiledMap;
  11. import com.badlogic.gdx.maps.tiled.TiledMapTile;
  12. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  13. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
  14. import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
  15. import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
  16. import com.badlogic.gdx.math.Vector2;
  17. import com.badlogic.gdx.utils.Array;
  18. import com.badlogic.gdx.utils.TimeUtils;
  19.  
  20. public class EssaRebano extends Sprite implements InputProcessor {
  21.  
  22.     /** the movement velocity */
  23.     private Vector2 velocity = new Vector2();
  24.  
  25.     private float speed = 60 * 2, gravity = 60 * 3, boundingTolerance = 1;
  26.  
  27.     private boolean canJump;
  28.  
  29.     private TiledMapTileLayer collisionLayer;
  30.  
  31.     private String blockedKey = "blocked", diggableKey = "diggable", beforeDiggingKey = "beforeDigging", afterDiggingKey = "afterDigging", diggingCellKey = "diggingCell", isDiggingKey = "isDigging", diggingSinceKey = "diggingSince", digTimeKey = "digTime", collisionLayerName = "background";
  32.  
  33.     private AnimatedTiledMapTile digging;
  34.  
  35.     public EssaRebano(Sprite sprite, TiledMap map) {
  36.         super(sprite);
  37.         this.collisionLayer = (TiledMapTileLayer) map.getLayers().get(collisionLayerName);
  38.  
  39.         Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>(2);
  40.         // ESSA REBANO: find tile that should be set after digging
  41.         StaticTiledMapTile afterDiggingTile = null;
  42.  
  43.         // get the frame tiles
  44.         Iterator<TiledMapTile> tiles = map.getTileSets().getTileSet("tiles").iterator();
  45.         while(tiles.hasNext()) {
  46.             TiledMapTile tile = tiles.next();
  47.             if(tile.getProperties().containsKey("animation") && tile.getProperties().get("animation", String.class).equals("digging"))
  48.                 frameTiles.add((StaticTiledMapTile) tile);
  49.             else if(tile.getProperties().containsKey("afterDigging")) // ESSA REBANO: found it, set it
  50.                 afterDiggingTile = (StaticTiledMapTile) tile;
  51.         }
  52.  
  53.         // ESSA REBANO: create the digging animated tile
  54.         digging = new AnimatedTiledMapTile(1 / 3f, frameTiles);
  55.         // ESSA REBANO: put all things needed for digging in the digging tile properties
  56.         digging.getProperties().put(afterDiggingKey, afterDiggingTile); // ESSA REBANO: set the tile that should be set after digging
  57.         digging.getProperties().put(blockedKey, null); // ESSA REBANO: the tile should be blocked while digging
  58.         digging.getProperties().put(isDiggingKey, false); // ESSA REBANO: set that we're currently not digging
  59.         digging.getProperties().put(digTimeKey, 1000); // ESSA REBANO: set the time that digging takes (in milliseconds)
  60.     }
  61.  
  62.     @Override
  63.     public void draw(SpriteBatch spriteBatch) {
  64.         update(Gdx.graphics.getDeltaTime());
  65.         super.draw(spriteBatch);
  66.     }
  67.  
  68.     public void update(float delta) {
  69.         // apply gravity
  70.         velocity.y -= gravity * delta;
  71.  
  72.         // clamp velocity
  73.         if(velocity.y > speed)
  74.             velocity.y = speed;
  75.         else if(velocity.y < -speed)
  76.             velocity.y = -speed;
  77.  
  78.         //Collision
  79.         //Save old position
  80.         float oldX = getX(), oldY = getY(), tileWidth = collisionLayer.getTileWidth(), tileHeight = collisionLayer.getTileHeight();
  81.         boolean collisionX = false, collisionY = false;
  82.         Cell tmpCell;
  83.  
  84.         // move on x
  85.         setX(getX() + velocity.x * delta);
  86.  
  87.         if(velocity.x < 0) { // going left
  88.             // top left
  89.             if((tmpCell = collisionLayer.getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + getHeight() - boundingTolerance) / tileHeight))).getTile() != null)
  90.                 collisionX = isCellBlocked(tmpCell);
  91.  
  92.             // middle left
  93.             if(!collisionX && (tmpCell = collisionLayer.getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight))).getTile() != null)
  94.                 collisionX = isCellBlocked(tmpCell);
  95.  
  96.             // bottom left
  97.             if(!collisionX && (tmpCell = collisionLayer.getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  98.                 collisionX = isCellBlocked(tmpCell);
  99.         } else if(velocity.x > 0) { // going right
  100.             // top right
  101.             if((tmpCell = collisionLayer.getCell((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight))).getTile() != null)
  102.                 collisionX = isCellBlocked(tmpCell);
  103.  
  104.             // middle right
  105.             if(!collisionX && (tmpCell = collisionLayer.getCell((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() + getHeight() / 2) / tileHeight))).getTile() != null)
  106.                 collisionX = isCellBlocked(tmpCell);
  107.  
  108.             // bottom right
  109.             if(!collisionX && (tmpCell = collisionLayer.getCell((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  110.                 collisionX = isCellBlocked(tmpCell);
  111.         }
  112.  
  113.         // react to x collision
  114.         if(collisionX) {
  115.             setX(oldX);
  116.             velocity.x = 0;
  117.         }
  118.  
  119.         // move on y
  120.         setY(getY() + velocity.y * delta);
  121.  
  122.         if(velocity.y < 0) { // going down
  123.             // bottom left
  124.             if((tmpCell = collisionLayer.getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  125.                 collisionY = isCellBlocked(tmpCell);
  126.  
  127.             // bottom middle
  128.             if(!collisionY && (tmpCell = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  129.                 collisionY = isCellBlocked(tmpCell);
  130.  
  131.             // bottom right
  132.             if(!collisionY && (tmpCell = collisionLayer.getCell((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight))).getTile() != null)
  133.                 collisionY = isCellBlocked(tmpCell);
  134.  
  135.             canJump = collisionY;
  136.         } else if(velocity.y > 0) { // going up
  137.             // top left
  138.             if((tmpCell = collisionLayer.getCell((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight))).getTile() != null)
  139.                 collisionY = isCellBlocked(tmpCell);
  140.  
  141.             // top middle
  142.             if(!collisionY && (tmpCell = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight))).getTile() != null)
  143.                 collisionY = isCellBlocked(tmpCell);
  144.  
  145.             // top right
  146.             if(!collisionY && (tmpCell = collisionLayer.getCell((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight))).getTile() != null)
  147.                 collisionY = isCellBlocked(tmpCell);
  148.         }
  149.  
  150.         // react to y collision
  151.         if(collisionY) {
  152.             setY(oldY);
  153.             velocity.y = 0;
  154.         }
  155.  
  156.         // ESSA REBANO: update the digging behaviour
  157.         if(digging.getProperties().get(isDiggingKey, Boolean.class)) // ESSA REBANO: if we're digging
  158.             // ESSA REBANO: set tmpCell to the cell under the character and see if it is NOT the same as the cell that we currently dig
  159.             if((tmpCell = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / collisionLayer.getTileWidth()), (int) (getY() / collisionLayer.getTileHeight()))) != digging.getProperties().get(diggingCellKey, Cell.class)) {
  160.                 // ESSA REBANO: if we're here, the cell under the character is not the cell that we began to dig in anymore (the character may have moved while digging)
  161.                 digging.getProperties().get(diggingCellKey, Cell.class).setTile(digging.getProperties().get(beforeDiggingKey, StaticTiledMapTile.class)); // ESSA REBANO: therefore, set the tile of the cell that we dug in back to what it previously was
  162.                 digging.getProperties().put(isDiggingKey, false); // ESSA REBANO: save that we're not digging anymore
  163.             } else if(TimeUtils.millis() - digging.getProperties().get(diggingSinceKey, Long.class) > digging.getProperties().get(digTimeKey, Integer.class)) {
  164.                 // ESSA REBANO: if we're here, digging is complete
  165.                 digging.getProperties().put(isDiggingKey, false); // therefore, set digging to false
  166.                 digging.getProperties().get(diggingCellKey, Cell.class).setTile(digging.getProperties().get(afterDiggingKey, StaticTiledMapTile.class)); // ESSA REBANO: and put the afterDigging tile in the cell that we dug
  167.             }
  168.     }
  169.  
  170.     private boolean isCellBlocked(Cell cell) {
  171.         return cell.getTile().getProperties().containsKey(blockedKey);
  172.     }
  173.  
  174.     @Override
  175.     public boolean keyDown(int keycode) {
  176.         switch(keycode) {
  177.         case Keys.W:
  178.             if(canJump) {
  179.                 velocity.y = speed;
  180.                 canJump = false;
  181.             }
  182.             break;
  183.         case Keys.A:
  184.             velocity.x = -speed;
  185.             break;
  186.         case Keys.D:
  187.             velocity.x = speed;
  188.             break;
  189.         case Keys.DOWN: // ESSA REBANO: start digging
  190.             Cell cell = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / collisionLayer.getTileWidth()), (int) (getY() / collisionLayer.getTileHeight())); // ESSA REBANO: get the cell under the player
  191.             if(cell.getTile() != null && cell.getTile().getProperties().containsKey(diggableKey)) { // ESSA REBANO: if the cell contains a tile which is diggable
  192.                 digging.getProperties().put(diggingCellKey, cell); // ESSA REBANO: store this cell somewhere
  193.                 digging.getProperties().put(beforeDiggingKey, cell.getTile()); // ESSA REBANO: save the tile that is in the cell
  194.                 cell.setTile(digging); // ESSA REBANO: put the digging animation tile in the cell
  195.                 digging.getProperties().put(isDiggingKey, true); // ESSA REBANO: set digging to true
  196.                 digging.getProperties().put(diggingSinceKey, TimeUtils.millis()); // ESSA REBANO: save the time we began to dig to decide if we're done later
  197.             }
  198.         }
  199.         return true;
  200.     }
  201.  
  202.     @Override
  203.     public boolean keyUp(int keycode) {
  204.         switch(keycode) {
  205.         case Keys.A:
  206.             if(velocity.x < 0)
  207.                 velocity.x = 0;
  208.             break;
  209.         case Keys.D:
  210.             if(velocity.x > 0)
  211.                 velocity.x = 0;
  212.             break;
  213.         case Keys.DOWN:
  214.             // ESSA REBANO: stop digging
  215.             Cell cell = collisionLayer.getCell((int) ((getX() + getWidth() / 2) / collisionLayer.getTileWidth()), (int) (getY() / collisionLayer.getTileHeight())); // ESSA REBANO: get the cell under the player
  216.             if(cell.getTile() == digging) { // ESSA REBANO: if the tile that the character is standing on is the digging animation tile
  217.                 digging.getProperties().put(isDiggingKey, false); // ESSA REBANO: set digging to false
  218.                 cell.setTile(digging.getProperties().get(beforeDiggingKey, StaticTiledMapTile.class)); // ESSA REBANO: change the tile back to what it was before digging
  219.             }
  220.         }
  221.         return true;
  222.     }
  223.  
  224.     public Vector2 getVelocity() {
  225.         return velocity;
  226.     }
  227.  
  228.     public void setVelocity(Vector2 velocity) {
  229.         this.velocity = velocity;
  230.     }
  231.  
  232.     public float getSpeed() {
  233.         return speed;
  234.     }
  235.  
  236.     public void setSpeed(float speed) {
  237.         this.speed = speed;
  238.     }
  239.  
  240.     public float getGravity() {
  241.         return gravity;
  242.     }
  243.  
  244.     public void setGravity(float gravity) {
  245.         this.gravity = gravity;
  246.     }
  247.  
  248.     public TiledMapTileLayer getCollisionLayer() {
  249.         return collisionLayer;
  250.     }
  251.  
  252.     public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
  253.         this.collisionLayer = collisionLayer;
  254.     }
  255.  
  256.     public float getBoundingTolerance() {
  257.         return boundingTolerance;
  258.     }
  259.  
  260.     public void setBoundingTolerance(float boundingTolerance) {
  261.         this.boundingTolerance = boundingTolerance;
  262.     }
  263.  
  264.     public String getBlockedKey() {
  265.         return blockedKey;
  266.     }
  267.  
  268.     public void setBlockedKey(String blockedKey) {
  269.         this.blockedKey = blockedKey;
  270.     }
  271.  
  272.     public String getDiggableKey() {
  273.         return diggableKey;
  274.     }
  275.  
  276.     public void setDiggableKey(String diggableKey) {
  277.         this.diggableKey = diggableKey;
  278.     }
  279.  
  280.     public String getCollisionLayerName() {
  281.         return collisionLayerName;
  282.     }
  283.  
  284.     public void setCollisionLayerName(String collisionLayerName) {
  285.         this.collisionLayerName = collisionLayerName;
  286.     }
  287.  
  288.     public AnimatedTiledMapTile getDigging() {
  289.         return digging;
  290.     }
  291.  
  292.     public void setDigging(AnimatedTiledMapTile digging) {
  293.         this.digging = digging;
  294.     }
  295.  
  296.     public boolean isCanJump() {
  297.         return canJump;
  298.     }
  299.  
  300.     @Override
  301.     public boolean keyTyped(char character) {
  302.         return false;
  303.     }
  304.  
  305.     @Override
  306.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  307.         return false;
  308.     }
  309.  
  310.     @Override
  311.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  312.         return false;
  313.     }
  314.  
  315.     @Override
  316.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  317.         return false;
  318.     }
  319.  
  320.     @Override
  321.     public boolean mouseMoved(int screenX, int screenY) {
  322.         return false;
  323.     }
  324.  
  325.     @Override
  326.     public boolean scrolled(int amount) {
  327.         return false;
  328.     }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement