Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.teamkwan.dugmanalpha.entity;
- import net.dermetfan.libgdx.maps.MapUtils;
- import net.dermetfan.libgdx.maps.TileAnimator;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputProcessor;
- import com.badlogic.gdx.graphics.g2d.Animation;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.maps.tiled.TiledMap;
- import com.badlogic.gdx.maps.tiled.TiledMapTile;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
- import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
- import com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile;
- import com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.utils.ObjectMap;
- import com.badlogic.gdx.utils.TimeUtils;
- import com.teamkwan.dugmanalpha.Assets;
- /**
- * based on net.dermetfan.someLibgdxTests.entities.EssaRebano from <a href="http://bitbucket.org/dermetfan/somelibgdxtests">SomeLibgdxTests</a>
- * @author dermetfan
- */
- public class DugMan2 extends Sprite implements InputProcessor, TileMapCollider {
- private Vector2 velocity = new Vector2(), previousVelocity = new Vector2();
- private float speed = 60 * 2, boundingTolerance = 2, animationTime, tileWidth, tileHeight;
- private boolean canJump, isDigging;
- private TiledMapTileLayer collisionLayer;
- private String collisionLayerName = "collision";
- private String blockedKey = "blocked", diggableKey = "diggable", animationKey = "animation", diggingAnimationKey = "digging", beforeDiggingKey = "beforeDigging", afterDiggingKey = "afterDigging";
- private TiledMapTile beforeDiggingTile, afterDiggingTile;
- private Cell diggingCell;
- private long diggingSince;
- private int diggingDuration, diggingCellRelativeX, diggingCellRelativeY;
- private AnimatedTiledMapTile digging;
- private Animation stillLeft, stillRight, stillUp, stillDown, left, right, up, down;
- private Cell tmpCell;
- public DugMan2(TiledMap map) {
- collisionLayer = (TiledMapTileLayer) map.getLayers().get("collision");
- tileWidth = collisionLayer.getTileWidth();
- tileHeight = collisionLayer.getTileHeight();
- // setup graphics
- Assets.inst();
- left = new Animation(.2f, Assets.hero.findRegion("Heroleft1"), Assets.hero.findRegion("Heroleft2"), Assets.hero.findRegion("Heroleft3"));
- right = new Animation(0.2f, Assets.hero.findRegion("Heroright1"), Assets.hero.findRegion("Heroright2"), Assets.hero.findRegion("Heroright3"));
- up = new Animation(0.2f, Assets.hero.findRegion("Heroup1"), Assets.hero.findRegion("Heroup2"), Assets.hero.findRegion("Heroup3"));
- down = new Animation(0.2f, Assets.hero.findRegion("Herodown1"), Assets.hero.findRegion("Herodown2"), Assets.hero.findRegion("Herodown3"));
- stillLeft = new Animation(0, Assets.hero.findRegion("Heroleft2"));
- stillRight = new Animation(0, Assets.hero.findRegion("Heroright2"));
- stillUp = new Animation(0, Assets.hero.findRegion("Heroup2"));
- stillDown = new Animation(0, Assets.hero.findRegion("Herodown2"));
- left.setPlayMode(Animation.LOOP);
- right.setPlayMode(Animation.LOOP);
- up.setPlayMode(Animation.LOOP);
- down.setPlayMode(Animation.LOOP);
- stillLeft.setPlayMode(Animation.LOOP);
- stillRight.setPlayMode(Animation.LOOP);
- stillUp.setPlayMode(Animation.LOOP);
- stillDown.setPlayMode(Animation.LOOP);
- // initially set the stillDown region
- setRegion(stillDown.getKeyFrame(animationTime));
- setSize(tileWidth, tileHeight * 1.75f);
- System.out.println(getWidth() + " x " + getHeight());
- /* setup digging tile graphics
- *
- * afterDigging tile
- * animation: afterDigging
- * digging tiles
- * animation: digging */
- ObjectMap<String, Array<StaticTiledMapTile>> frames = TileAnimator.filterFrames(MapUtils.toTiledMapTileArray(map.getTileSets().getTileSet(0)), animationKey);
- Array<StaticTiledMapTile> frameTiles = frames.get(diggingAnimationKey);
- afterDiggingTile = frames.get(afterDiggingKey).first();
- digging = new AnimatedTiledMapTile(1 / 12f, frameTiles);
- digging.getProperties().put(blockedKey, null); // ESSA REBANO: the tile should be blocked while digging
- diggingDuration = 1000;
- }
- @Override
- public void draw(SpriteBatch spriteBatch) {
- update(Gdx.graphics.getDeltaTime());
- super.draw(spriteBatch);
- }
- public void update(float delta) {
- // clamp velocity
- if(velocity.y > speed)
- velocity.y = speed;
- else if(velocity.y < -speed)
- velocity.y = -speed;
- //Collision
- //Save old position
- float oldX = getX(), oldY = getY();
- boolean collisionX = false, collisionY = false;
- // move on x
- setX(getX() + velocity.x * delta);
- if(velocity.x < 0) // going left
- collisionX = collidesLeft();
- else if(velocity.x > 0) // going right
- collisionX = collidesRight();
- // react to x collision
- if(collisionX) {
- setX(oldX);
- velocity.x = 0;
- }
- // move on y
- setY(getY() + velocity.y * delta);
- if(velocity.y < 0) // going down
- collisionY = collidesBottom();
- else if(velocity.y > 0) // going up
- collisionY = collidesTop();
- // react to y collision
- if(collisionY) {
- setY(oldY);
- velocity.y = 0;
- }
- // ESSA REBANO: update the digging behavior
- if(isDigging) // ESSA REBANO: if we're digging
- // 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
- if((tmpCell = collisionLayer.getCell((int) ((getX() + diggingCellRelativeX) / tileWidth), (int) ((getY() + diggingCellRelativeY) / tileHeight))) != diggingCell) {
- // 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)
- isDigging = false; // ESSA REBANO: save that we're not digging anymore
- diggingCell.setTile(beforeDiggingTile); // ESSA REBANO: therefore, set the tile of the cell that we dug in back to what it previously was
- } else if(TimeUtils.millis() - diggingSince > diggingDuration) {
- // ESSA REBANO: if we're here, digging is complete
- isDigging = false; // therefore, set digging to false
- diggingCell.setTile(afterDiggingTile); // ESSA REBANO: and put the afterDigging tile in the cell that we dug
- }
- // update animation
- animationTime += delta;
- float angle = velocity.angle();
- if(angle > 315 || angle < 45) { // right
- if(velocity.x == 0) {
- if(previousVelocity.x < 0)
- setRegion(stillLeft.getKeyFrame(animationTime));
- else if(previousVelocity.x > 0)
- setRegion(stillRight.getKeyFrame(animationTime));
- } else
- setRegion(right.getKeyFrame(animationTime));
- } else if(angle < 135 && angle > 45) { // up
- if(velocity.y == 0) {
- if(previousVelocity.y < 0)
- setRegion(stillDown.getKeyFrame(animationTime));
- else if(previousVelocity.y > 0)
- setRegion(stillUp.getKeyFrame(animationTime));
- } else
- setRegion(up.getKeyFrame(animationTime));
- } else if(angle < 225 && angle > 135) { // left
- if(velocity.x == 0) {
- if(previousVelocity.x < 0)
- setRegion(stillLeft.getKeyFrame(animationTime));
- else if(previousVelocity.x > 0)
- setRegion(stillRight.getKeyFrame(animationTime));
- } else
- setRegion(left.getKeyFrame(animationTime));
- } else if(angle < 315 && angle > 225) // down
- if(velocity.y == 0) {
- if(previousVelocity.y < 0)
- setRegion(stillDown.getKeyFrame(animationTime));
- else if(previousVelocity.y > 0)
- setRegion(stillUp.getKeyFrame(animationTime));
- } else
- setRegion(down.getKeyFrame(animationTime));
- }
- private boolean isCellBlocked(int x, int y) {
- return (tmpCell = collisionLayer.getCell(x, y)) != null && tmpCell.getTile() != null && tmpCell.getTile().getProperties().containsKey(blockedKey);
- }
- @Override
- public boolean collidesLeft() {
- boolean collides = false;
- for(float step = boundingTolerance; step < getHeight(); step += tileHeight / 2 - boundingTolerance) {
- if(collides)
- break;
- collides = isCellBlocked((int) ((getX() + boundingTolerance) / tileWidth), (int) ((getY() + step) / tileHeight));
- }
- return collides;
- }
- @Override
- public boolean collidesRight() {
- boolean collides = false;
- for(float step = boundingTolerance; step < getHeight(); step += tileHeight / 2 - boundingTolerance) {
- if(collides)
- break;
- collides = isCellBlocked((int) ((getX() - boundingTolerance + getWidth()) / tileWidth), (int) ((getY() + step) / tileHeight));
- }
- return collides;
- }
- @Override
- public boolean collidesTop() {
- boolean collides = false;
- for(float step = boundingTolerance; step < getWidth(); step += tileWidth / 2 - boundingTolerance) {
- if(collides)
- break;
- collides = isCellBlocked((int) ((getX() + step) / tileWidth), (int) ((getY() - boundingTolerance + getHeight()) / tileHeight));
- }
- return collides;
- }
- @Override
- public boolean collidesBottom() {
- boolean collides = false;
- for(float step = boundingTolerance; step < getWidth(); step += tileWidth / 2 - boundingTolerance) {
- if(collides)
- break;
- collides = isCellBlocked((int) ((getX() + step) / tileWidth), (int) ((getY() + boundingTolerance) / tileHeight));
- }
- return collides;
- }
- @Override
- public boolean keyDown(int keycode) {
- switch(keycode) {
- case Keys.W:
- previousVelocity.set(velocity);
- velocity.y = speed;
- break;
- case Keys.A:
- previousVelocity.set(velocity);
- velocity.x = -speed;
- break;
- case Keys.D:
- previousVelocity.set(velocity);
- velocity.x = speed;
- break;
- case Keys.S:
- previousVelocity.set(velocity);
- velocity.y = -speed;
- break;
- case Keys.UP:
- startDigging((int) (getWidth() / 2), (int) (getHeight() + tileHeight / 2)); // ESSA REBANO: get the cell over the player
- break;
- case Keys.DOWN:
- startDigging((int) (getWidth() / 2), (int) (-tileHeight / 2)); // ESSA REBANO: get the cell under the player
- break;
- case Keys.LEFT:
- startDigging((int) (-tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell left of the player
- break;
- case Keys.RIGHT:
- startDigging((int) (getWidth() + tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell right of the player
- }
- return true;
- }
- @Override
- public boolean keyUp(int keycode) {
- switch(keycode) {
- case Keys.A:
- if(velocity.x < 0) {
- previousVelocity.set(velocity);
- velocity.x = 0;
- }
- break;
- case Keys.D:
- if(velocity.x > 0) {
- previousVelocity.set(velocity);
- velocity.x = 0;
- }
- break;
- case Keys.W:
- if(velocity.y > 0) {
- previousVelocity.set(velocity);
- velocity.y = 0;
- }
- break;
- case Keys.S:
- if(velocity.y < 0) {
- previousVelocity.set(velocity);
- velocity.y = 0;
- }
- break;
- case Keys.UP:
- stopDigging((int) (getWidth() / 2), (int) (getHeight() + tileHeight / 2)); // ESSA REBANO: get the cell over the player
- break;
- case Keys.DOWN:
- stopDigging((int) (getWidth() / 2), (int) (-tileHeight / 2)); // ESSA REBANO: get the cell under the player
- break;
- case Keys.LEFT:
- stopDigging((int) (-tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell left of the player
- break;
- case Keys.RIGHT:
- stopDigging((int) (getWidth() + tileWidth / 2), (int) (getHeight() / 2)); // ESSA REBANO: get the cell right of the player
- }
- return true;
- }
- public void startDigging(int relativeX, int relativeY) {
- Cell cell = collisionLayer.getCell((int) ((getX() + relativeX) / tileWidth), (int) ((getY() + relativeY) / tileHeight));
- if(cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(diggableKey)) { // ESSA REBANO: if the cell contains a tile which is diggable
- diggingCellRelativeX = relativeX;
- diggingCellRelativeY = relativeY;
- diggingCell = cell; // ESSA REBANO: store this cell somewhere
- beforeDiggingTile = cell.getTile(); // ESSA REBANO: save the tile that is in the cell
- cell.setTile(digging); // ESSA REBANO: put the digging animation tile in the cell
- isDigging = true; // ESSA REBANO: set digging to true
- diggingSince = TimeUtils.millis(); // ESSA REBANO: save the time we began to dig to decide if we're done later
- }
- }
- public void stopDigging(int relativeX, int relativeY) {
- Cell cell = collisionLayer.getCell((int) ((getX() + relativeX) / tileWidth), (int) ((getY() + relativeY) / tileHeight));
- if(cell != null && cell.getTile() == digging) { // ESSA REBANO: if the tile that the character is standing on is the digging animation tile
- isDigging = false; // ESSA REBANO: set digging to false
- cell.setTile(beforeDiggingTile); // ESSA REBANO: change the tile back to what it was before digging
- }
- }
- @Override
- public boolean keyTyped(char character) {
- return false;
- }
- @Override
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- return false;
- }
- @Override
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- return false;
- }
- @Override
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- return false;
- }
- @Override
- public boolean mouseMoved(int screenX, int screenY) {
- return false;
- }
- @Override
- public boolean scrolled(int amount) {
- return false;
- }
- /** @return the velocity */
- public Vector2 getVelocity() {
- return velocity;
- }
- /** @param velocity the velocity to set */
- public void setVelocity(Vector2 velocity) {
- this.velocity = velocity;
- }
- /** @return the previousVelocity */
- public Vector2 getPreviousVelocity() {
- return previousVelocity;
- }
- /** @param previousVelocity the previousVelocity to set */
- public void setPreviousVelocity(Vector2 previousVelocity) {
- this.previousVelocity = previousVelocity;
- }
- /** @return the speed */
- public float getSpeed() {
- return speed;
- }
- /** @param speed the speed to set */
- public void setSpeed(float speed) {
- this.speed = speed;
- }
- /** @return the boundingTolerance */
- public float getBoundingTolerance() {
- return boundingTolerance;
- }
- /** @param boundingTolerance the boundingTolerance to set */
- public void setBoundingTolerance(float boundingTolerance) {
- this.boundingTolerance = boundingTolerance;
- }
- /** @return the canJump */
- public boolean isCanJump() {
- return canJump;
- }
- /** @param canJump the canJump to set */
- public void setCanJump(boolean canJump) {
- this.canJump = canJump;
- }
- /** @return the collisionLayer */
- public TiledMapTileLayer getCollisionLayer() {
- return collisionLayer;
- }
- /** @param collisionLayer the collisionLayer to set */
- public void setCollisionLayer(TiledMapTileLayer collisionLayer) {
- this.collisionLayer = collisionLayer;
- }
- /** @return the animationTime */
- public float getAnimationTime() {
- return animationTime;
- }
- /** @return the tileWidth */
- public float getTileWidth() {
- return tileWidth;
- }
- /** @return the tileHeight */
- public float getTileHeight() {
- return tileHeight;
- }
- /** @return the collisionLayerName */
- public String getCollisionLayerName() {
- return collisionLayerName;
- }
- /** @return the blockedKey */
- public String getBlockedKey() {
- return blockedKey;
- }
- /** @return the diggableKey */
- public String getDiggableKey() {
- return diggableKey;
- }
- /** @return the animationKey */
- public String getAnimationKey() {
- return animationKey;
- }
- /** @return the diggingAnimationKey */
- public String getDiggingAnimationKey() {
- return diggingAnimationKey;
- }
- /** @return the beforeDiggingKey */
- public String getBeforeDiggingKey() {
- return beforeDiggingKey;
- }
- /** @return the afterDiggingKey */
- public String getAfterDiggingKey() {
- return afterDiggingKey;
- }
- /** @return the stillLeft */
- public Animation getStillLeft() {
- return stillLeft;
- }
- /** @return the stillRight */
- public Animation getStillRight() {
- return stillRight;
- }
- /** @return the stillUp */
- public Animation getStillUp() {
- return stillUp;
- }
- /** @return the stillDown */
- public Animation getStillDown() {
- return stillDown;
- }
- /** @return the left */
- public Animation getLeft() {
- return left;
- }
- /** @return the right */
- public Animation getRight() {
- return right;
- }
- /** @return the up */
- public Animation getUp() {
- return up;
- }
- /** @return the down */
- public Animation getDown() {
- return down;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment