dermetfan

Essa Rebano digging player

Nov 9th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.34 KB | None | 0 0
  1. package com.teamkwan.dugmanalpha.entity;
  2.  
  3. import net.dermetfan.libgdx.maps.MapUtils;
  4. import net.dermetfan.libgdx.maps.TileAnimator;
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.InputProcessor;
  9. import com.badlogic.gdx.graphics.g2d.Animation;
  10. import com.badlogic.gdx.graphics.g2d.Sprite;
  11. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  12. import com.badlogic.gdx.maps.tiled.TiledMap;
  13. import com.badlogic.gdx.maps.tiled.TiledMapTile;
  14. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
  15. import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
  16. import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
  17. import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
  18. import com.badlogic.gdx.math.Vector2;
  19. import com.badlogic.gdx.utils.Array;
  20. import com.badlogic.gdx.utils.ObjectMap;
  21. import com.badlogic.gdx.utils.TimeUtils;
  22. import com.teamkwan.dugmanalpha.Assets;
  23.  
  24. /**
  25.  * based on net.dermetfan.someLibgdxTests.entities.EssaRebano from <a href="http://bitbucket.org/dermetfan/somelibgdxtests">SomeLibgdxTests</a>
  26.  * @author dermetfan
  27.  */
  28. public class DugMan2 extends Sprite implements InputProcessor, TileMapCollider {
  29.  
  30.     private Vector2 velocity = new Vector2(), previousVelocity = new Vector2();
  31.     private float speed = 60 * 2, boundingTolerance = 2, animationTime, tileWidth, tileHeight;
  32.     private boolean canJump, isDigging;
  33.  
  34.     private TiledMapTileLayer collisionLayer;
  35.  
  36.     private String collisionLayerName = "collision";
  37.     private String blockedKey = "blocked", diggableKey = "diggable", animationKey = "animation", diggingAnimationKey = "digging", beforeDiggingKey = "beforeDigging", afterDiggingKey = "afterDigging";
  38.  
  39.     private TiledMapTile beforeDiggingTile, afterDiggingTile;
  40.     private Cell diggingCell;
  41.     private long diggingSince;
  42.     private int diggingDuration, diggingCellRelativeX, diggingCellRelativeY;
  43.     private AnimatedTiledMapTile digging;
  44.  
  45.     private Animation stillLeft, stillRight, stillUp, stillDown, left, right, up, down;
  46.  
  47.     private Cell tmpCell;
  48.  
  49.     public DugMan2(TiledMap map) {
  50.         collisionLayer = (TiledMapTileLayer) map.getLayers().get("collision");
  51.         tileWidth = collisionLayer.getTileWidth();
  52.         tileHeight = collisionLayer.getTileHeight();
  53.  
  54.         // setup graphics
  55.         Assets.inst();
  56.         left = new Animation(.2f, Assets.hero.findRegion("Heroleft1"), Assets.hero.findRegion("Heroleft2"), Assets.hero.findRegion("Heroleft3"));
  57.         right = new Animation(0.2f, Assets.hero.findRegion("Heroright1"), Assets.hero.findRegion("Heroright2"), Assets.hero.findRegion("Heroright3"));
  58.         up = new Animation(0.2f, Assets.hero.findRegion("Heroup1"), Assets.hero.findRegion("Heroup2"), Assets.hero.findRegion("Heroup3"));
  59.         down = new Animation(0.2f, Assets.hero.findRegion("Herodown1"), Assets.hero.findRegion("Herodown2"), Assets.hero.findRegion("Herodown3"));
  60.  
  61.         stillLeft = new Animation(0, Assets.hero.findRegion("Heroleft2"));
  62.         stillRight = new Animation(0, Assets.hero.findRegion("Heroright2"));
  63.         stillUp = new Animation(0, Assets.hero.findRegion("Heroup2"));
  64.         stillDown = new Animation(0, Assets.hero.findRegion("Herodown2"));
  65.  
  66.         left.setPlayMode(Animation.LOOP);
  67.         right.setPlayMode(Animation.LOOP);
  68.         up.setPlayMode(Animation.LOOP);
  69.         down.setPlayMode(Animation.LOOP);
  70.  
  71.         stillLeft.setPlayMode(Animation.LOOP);
  72.         stillRight.setPlayMode(Animation.LOOP);
  73.         stillUp.setPlayMode(Animation.LOOP);
  74.         stillDown.setPlayMode(Animation.LOOP);
  75.  
  76.         // initially set the stillDown region
  77.         setRegion(stillDown.getKeyFrame(animationTime));
  78.  
  79.         setSize(tileWidth, tileHeight * 1.75f);
  80.         System.out.println(getWidth() + " x " + getHeight());
  81.  
  82.         /* setup digging tile graphics
  83.          *
  84.          * afterDigging tile
  85.          *      animation: afterDigging
  86.          * digging tiles
  87.          *      animation: digging */
  88.         ObjectMap<String, Array<StaticTiledMapTile>> frames = TileAnimator.filterFrames(MapUtils.toTiledMapTileArray(map.getTileSets().getTileSet(0)), animationKey);
  89.         Array<StaticTiledMapTile> frameTiles = frames.get(diggingAnimationKey);
  90.         afterDiggingTile = frames.get(afterDiggingKey).first();
  91.  
  92.         digging = new AnimatedTiledMapTile(1 / 12f, frameTiles);
  93.         digging.getProperties().put(blockedKey, null); // ESSA REBANO: the tile should be blocked while digging
  94.         diggingDuration = 1000;
  95.     }
  96.  
  97.     @Override
  98.     public void draw(SpriteBatch spriteBatch) {
  99.         update(Gdx.graphics.getDeltaTime());
  100.         super.draw(spriteBatch);
  101.     }
  102.  
  103.     public void update(float delta) {
  104.         // clamp velocity
  105.         if(velocity.y > speed)
  106.             velocity.y = speed;
  107.         else if(velocity.y < -speed)
  108.             velocity.y = -speed;
  109.  
  110.         //Collision
  111.         //Save old position
  112.         float oldX = getX(), oldY = getY();
  113.         boolean collisionX = false, collisionY = false;
  114.  
  115.         // move on x
  116.         setX(getX() + velocity.x * delta);
  117.  
  118.         if(velocity.x < 0) // going left
  119.             collisionX = collidesLeft();
  120.         else if(velocity.x > 0) // going right
  121.             collisionX = collidesRight();
  122.  
  123.         // react to x collision
  124.         if(collisionX) {
  125.             setX(oldX);
  126.             velocity.x = 0;
  127.         }
  128.  
  129.         // move on y
  130.         setY(getY() + velocity.y * delta);
  131.  
  132.         if(velocity.y < 0) // going down
  133.             collisionY = collidesBottom();
  134.         else if(velocity.y > 0) // going up
  135.             collisionY = collidesTop();
  136.  
  137.         // react to y collision
  138.         if(collisionY) {
  139.             setY(oldY);
  140.             velocity.y = 0;
  141.         }
  142.  
  143.         // ESSA REBANO: update the digging behavior
  144.         if(isDigging) // ESSA REBANO: if we're digging
  145.             // ESSA REBANO: set tmpCell to the cell relative to the character and see if it is NOT the same as the cell that we currently dig
  146.             if((tmpCell = collisionLayer.getCell((int) ((getX() + diggingCellRelativeX) / tileWidth), (int) ((getY() + diggingCellRelativeY) / tileHeight))) != diggingCell) {
  147.                 // ESSA REBANO: if we're here, the cell in the relative digging position is not the cell that we began to dig in anymore (the character may have moved while digging)
  148.                 isDigging = false; // ESSA REBANO: save that we're not digging anymore
  149.                 diggingCell.setTile(beforeDiggingTile); // ESSA REBANO: therefore, set the tile of the cell that we dug in back to what it previously was
  150.             } else if(TimeUtils.millis() - diggingSince > diggingDuration) {
  151.                 // ESSA REBANO: if we're here, digging is complete
  152.                 isDigging = false; // therefore, set digging to false
  153.                 diggingCell.setTile(afterDiggingTile); // ESSA REBANO: and put the afterDigging tile in the cell that we dug
  154.             }
  155.  
  156.         // update animation
  157.         animationTime += delta;
  158.         float angle = velocity.angle();
  159.         if(angle > 315 || angle < 45) { // right
  160.             if(velocity.x == 0) {
  161.                 if(previousVelocity.x < 0)
  162.                     setRegion(stillLeft.getKeyFrame(animationTime));
  163.                 else if(previousVelocity.x > 0)
  164.                     setRegion(stillRight.getKeyFrame(animationTime));
  165.             } else
  166.                 setRegion(right.getKeyFrame(animationTime));
  167.         } else if(angle < 135 && angle > 45) { // up
  168.             if(velocity.y == 0) {
  169.                 if(previousVelocity.y < 0)
  170.                     setRegion(stillDown.getKeyFrame(animationTime));
  171.                 else if(previousVelocity.y > 0)
  172.                     setRegion(stillUp.getKeyFrame(animationTime));
  173.             } else
  174.                 setRegion(up.getKeyFrame(animationTime));
  175.         } else if(angle < 225 && angle > 135) { // left
  176.             if(velocity.x == 0) {
  177.                 if(previousVelocity.x < 0)
  178.                     setRegion(stillLeft.getKeyFrame(animationTime));
  179.                 else if(previousVelocity.x > 0)
  180.                     setRegion(stillRight.getKeyFrame(animationTime));
  181.             } else
  182.                 setRegion(left.getKeyFrame(animationTime));
  183.         } else if(angle < 315 && angle > 225) // down
  184.             if(velocity.y == 0) {
  185.                 if(previousVelocity.y < 0)
  186.                     setRegion(stillDown.getKeyFrame(animationTime));
  187.                 else if(previousVelocity.y > 0)
  188.                     setRegion(stillUp.getKeyFrame(animationTime));
  189.             } else
  190.                 setRegion(down.getKeyFrame(animationTime));
  191.     }
  192.  
  193.     private boolean isCellBlocked(int x, int y) {
  194.         return (tmpCell = collisionLayer.getCell(x, y)) != null && tmpCell.getTile() != null && tmpCell.getTile().getProperties().containsKey(blockedKey);
  195.     }
  196.  
  197.     @Override
  198.     public boolean collidesLeft() {
  199.         boolean collides = false;
  200.  
  201.         for(float step = boundingTolerance; step < getHeight(); step += tileHeight / 2 - boundingTolerance) {
  202.             if(collides)
  203.                 break;
  204.             collides = isCellBlocked((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + step) / tileHeight));
  205.         }
  206.  
  207.         return collides;
  208.     }
  209.  
  210.     @Override
  211.     public boolean collidesRight() {
  212.         boolean collides = false;
  213.  
  214.         for(float step = boundingTolerance; step < getHeight(); step += tileHeight / 2 - boundingTolerance) {
  215.             if(collides)
  216.                 break;
  217.             collides = isCellBlocked((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() + step) / tileHeight));
  218.         }
  219.  
  220.         return collides;
  221.     }
  222.  
  223.     @Override
  224.     public boolean collidesTop() {
  225.         boolean collides = false;
  226.  
  227.         for(float step = boundingTolerance; step < getWidth(); step += tileWidth / 2 - boundingTolerance) {
  228.             if(collides)
  229.                 break;
  230.             collides = isCellBlocked((int) ((getX() + step) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight));
  231.         }
  232.  
  233.         return collides;
  234.     }
  235.  
  236.     @Override
  237.     public boolean collidesBottom() {
  238.         boolean collides = false;
  239.  
  240.         for(float step = boundingTolerance; step < getWidth(); step += tileWidth / 2 - boundingTolerance) {
  241.             if(collides)
  242.                 break;
  243.             collides = isCellBlocked((int) ((getX() + step) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight));
  244.         }
  245.  
  246.         return collides;
  247.     }
  248.  
  249.     @Override
  250.     public boolean keyDown(int keycode) {
  251.         switch(keycode) {
  252.         case Keys.W:
  253.             previousVelocity.set(velocity);
  254.             velocity.y = speed;
  255.             break;
  256.         case Keys.A:
  257.             previousVelocity.set(velocity);
  258.             velocity.x = -speed;
  259.             break;
  260.         case Keys.D:
  261.             previousVelocity.set(velocity);
  262.             velocity.x = speed;
  263.             break;
  264.         case Keys.S:
  265.             previousVelocity.set(velocity);
  266.             velocity.y = -speed;
  267.             break;
  268.         case Keys.UP:
  269.             startDigging((int) (getWidth() / 2), (int) (getHeight() + tileHeight / 2)); // ESSA REBANO: get the cell over the player
  270.             break;
  271.         case Keys.DOWN:
  272.             startDigging((int) (getWidth() / 2), (int) (-tileHeight / 2)); // ESSA REBANO: get the cell under the player
  273.             break;
  274.         case Keys.LEFT:
  275.             startDigging((int) (-tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell left of the player
  276.             break;
  277.         case Keys.RIGHT:
  278.             startDigging((int) (getWidth() + tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell right of the player
  279.         }
  280.         return true;
  281.     }
  282.  
  283.     @Override
  284.     public boolean keyUp(int keycode) {
  285.         switch(keycode) {
  286.         case Keys.A:
  287.             if(velocity.x < 0) {
  288.                 previousVelocity.set(velocity);
  289.                 velocity.x = 0;
  290.             }
  291.             break;
  292.         case Keys.D:
  293.             if(velocity.x > 0) {
  294.                 previousVelocity.set(velocity);
  295.                 velocity.x = 0;
  296.             }
  297.             break;
  298.         case Keys.W:
  299.             if(velocity.y > 0) {
  300.                 previousVelocity.set(velocity);
  301.                 velocity.y = 0;
  302.             }
  303.             break;
  304.         case Keys.S:
  305.             if(velocity.y < 0) {
  306.                 previousVelocity.set(velocity);
  307.                 velocity.y = 0;
  308.             }
  309.             break;
  310.         case Keys.UP:
  311.             stopDigging((int) (getWidth() / 2), (int) (getHeight() + tileHeight / 2)); // ESSA REBANO: get the cell over the player
  312.             break;
  313.         case Keys.DOWN:
  314.             stopDigging((int) (getWidth() / 2), (int) (-tileHeight / 2)); // ESSA REBANO: get the cell under the player
  315.             break;
  316.         case Keys.LEFT:
  317.             stopDigging((int) (-tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell left of the player
  318.             break;
  319.         case Keys.RIGHT:
  320.             stopDigging((int) (getWidth() + tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell right of the player
  321.         }
  322.         return true;
  323.     }
  324.  
  325.     public void startDigging(int relativeX, int relativeY) {
  326.         Cell cell = collisionLayer.getCell((int) ((getX() + relativeX) / tileWidth), (int) ((getY() + relativeY) / tileHeight));
  327.         if(cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(diggableKey)) { // ESSA REBANO: if the cell contains a tile which is diggable
  328.             diggingCellRelativeX = relativeX;
  329.             diggingCellRelativeY = relativeY;
  330.             diggingCell = cell; // ESSA REBANO: store this cell somewhere
  331.             beforeDiggingTile = cell.getTile(); // ESSA REBANO: save the tile that is in the cell
  332.             cell.setTile(digging); // ESSA REBANO: put the digging animation tile in the cell
  333.             isDigging = true; // ESSA REBANO: set digging to true
  334.             diggingSince = TimeUtils.millis(); // ESSA REBANO: save the time we began to dig to decide if we're done later
  335.         }
  336.     }
  337.  
  338.     public void stopDigging(int relativeX, int relativeY) {
  339.         Cell cell = collisionLayer.getCell((int) ((getX() + relativeX) / tileWidth), (int) ((getY() + relativeY) / tileHeight));
  340.         if(cell != null && cell.getTile() == digging) { // ESSA REBANO: if the tile that the character is standing on is the digging animation tile
  341.             isDigging = false; // ESSA REBANO: set digging to false
  342.             cell.setTile(beforeDiggingTile); // ESSA REBANO: change the tile back to what it was before digging
  343.         }
  344.     }
  345.  
  346.     @Override
  347.     public boolean keyTyped(char character) {
  348.         return false;
  349.     }
  350.  
  351.     @Override
  352.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  353.         return false;
  354.     }
  355.  
  356.     @Override
  357.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  358.         return false;
  359.     }
  360.  
  361.     @Override
  362.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  363.         return false;
  364.     }
  365.  
  366.     @Override
  367.     public boolean mouseMoved(int screenX, int screenY) {
  368.         return false;
  369.     }
  370.  
  371.     @Override
  372.     public boolean scrolled(int amount) {
  373.         return false;
  374.     }
  375.  
  376.     /** @return the velocity */
  377.     public Vector2 getVelocity() {
  378.         return velocity;
  379.     }
  380.  
  381.     /** @param velocity the velocity to set */
  382.     public void setVelocity(Vector2 velocity) {
  383.         this.velocity = velocity;
  384.     }
  385.  
  386.     /** @return the previousVelocity */
  387.     public Vector2 getPreviousVelocity() {
  388.         return previousVelocity;
  389.     }
  390.  
  391.     /** @param previousVelocity the previousVelocity to set */
  392.     public void setPreviousVelocity(Vector2 previousVelocity) {
  393.         this.previousVelocity = previousVelocity;
  394.     }
  395.  
  396.     /** @return the speed */
  397.     public float getSpeed() {
  398.         return speed;
  399.     }
  400.  
  401.     /** @param speed the speed to set */
  402.     public void setSpeed(float speed) {
  403.         this.speed = speed;
  404.     }
  405.  
  406.     /** @return the boundingTolerance */
  407.     public float getBoundingTolerance() {
  408.         return boundingTolerance;
  409.     }
  410.  
  411.     /** @param boundingTolerance the boundingTolerance to set */
  412.     public void setBoundingTolerance(float boundingTolerance) {
  413.         this.boundingTolerance = boundingTolerance;
  414.     }
  415.  
  416.     /** @return the canJump */
  417.     public boolean isCanJump() {
  418.         return canJump;
  419.     }
  420.  
  421.     /** @param canJump the canJump to set */
  422.     public void setCanJump(boolean canJump) {
  423.         this.canJump = canJump;
  424.     }
  425.  
  426.     /** @return the collisionLayer */
  427.     public TiledMapTileLayer getCollisionLayer() {
  428.         return collisionLayer;
  429.     }
  430.  
  431.     /** @param collisionLayer the collisionLayer to set */
  432.     public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
  433.         this.collisionLayer = collisionLayer;
  434.     }
  435.  
  436.     /** @return the animationTime */
  437.     public float getAnimationTime() {
  438.         return animationTime;
  439.     }
  440.  
  441.     /** @return the tileWidth */
  442.     public float getTileWidth() {
  443.         return tileWidth;
  444.     }
  445.  
  446.     /** @return the tileHeight */
  447.     public float getTileHeight() {
  448.         return tileHeight;
  449.     }
  450.  
  451.     /** @return the collisionLayerName */
  452.     public String getCollisionLayerName() {
  453.         return collisionLayerName;
  454.     }
  455.  
  456.     /** @return the blockedKey */
  457.     public String getBlockedKey() {
  458.         return blockedKey;
  459.     }
  460.  
  461.     /** @return the diggableKey */
  462.     public String getDiggableKey() {
  463.         return diggableKey;
  464.     }
  465.  
  466.     /** @return the animationKey */
  467.     public String getAnimationKey() {
  468.         return animationKey;
  469.     }
  470.  
  471.     /** @return the diggingAnimationKey */
  472.     public String getDiggingAnimationKey() {
  473.         return diggingAnimationKey;
  474.     }
  475.  
  476.     /** @return the beforeDiggingKey */
  477.     public String getBeforeDiggingKey() {
  478.         return beforeDiggingKey;
  479.     }
  480.  
  481.     /** @return the afterDiggingKey */
  482.     public String getAfterDiggingKey() {
  483.         return afterDiggingKey;
  484.     }
  485.  
  486.     /** @return the stillLeft */
  487.     public Animation getStillLeft() {
  488.         return stillLeft;
  489.     }
  490.  
  491.     /** @return the stillRight */
  492.     public Animation getStillRight() {
  493.         return stillRight;
  494.     }
  495.  
  496.     /** @return the stillUp */
  497.     public Animation getStillUp() {
  498.         return stillUp;
  499.     }
  500.  
  501.     /** @return the stillDown */
  502.     public Animation getStillDown() {
  503.         return stillDown;
  504.     }
  505.  
  506.     /** @return the left */
  507.     public Animation getLeft() {
  508.         return left;
  509.     }
  510.  
  511.     /** @return the right */
  512.     public Animation getRight() {
  513.         return right;
  514.     }
  515.  
  516.     /** @return the up */
  517.     public Animation getUp() {
  518.         return up;
  519.     }
  520.  
  521.     /** @return the down */
  522.     public Animation getDown() {
  523.         return down;
  524.     }
  525.  
  526. }
Advertisement
Add Comment
Please, Sign In to add comment