DEADSOFTWARE

Move game world to new package
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Player.java
1 package ru.deadsoftware.cavedroid.game.mobs;
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
4 import com.badlogic.gdx.math.Vector2;
5 import ru.deadsoftware.cavedroid.game.world.GameWorld;
6 import ru.deadsoftware.cavedroid.misc.Assets;
8 public class Player extends Mob {
10 public final int[] inventory;
11 public int slot;
12 public final int gameMode;
13 public boolean swim;
15 public Player() {
16 super(0, 0, 4, 30, randomDir(), Type.MOB);
17 this.gameMode = 1;
18 inventory = new int[9];
19 swim = false;
20 }
22 public void respawn(GameWorld gameWorld) {
23 Vector2 pos = getSpawnPoint(gameWorld);
24 this.x = pos.x;
25 this.y = pos.y;
26 mVelocity.setZero();
27 }
29 private Vector2 getSpawnPoint(GameWorld gameWorld) {
30 int y;
31 for (y = 0; y < gameWorld.getHeight(); y++) {
32 if (y == gameWorld.getHeight() - 1) {
33 y = 60;
34 gameWorld.setForeMap(0, y, 1);
35 break;
36 }
37 if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
38 break;
39 }
40 }
41 return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
42 }
44 public void setDir(Direction dir) {
45 if (dir != getDirection()) {
46 switchDir();
47 }
48 }
50 @Override
51 public void ai(GameWorld gameWorld, float delta) {
52 }
54 @Override
55 public void changeDir() {
56 }
58 @Override
59 public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
60 final float correctedAnimationDelta = mAnimDelta * delta;
62 if (mVelocity.x != 0 || Math.abs(Assets.playerSprite[0][2].getRotation()) > Math.abs(correctedAnimationDelta)) {
63 Assets.playerSprite[0][2].rotate(correctedAnimationDelta);
64 Assets.playerSprite[1][2].rotate(-correctedAnimationDelta);
65 Assets.playerSprite[0][3].rotate(-correctedAnimationDelta);
66 Assets.playerSprite[1][3].rotate(correctedAnimationDelta);
67 } else {
68 Assets.playerSprite[0][2].setRotation(0);
69 Assets.playerSprite[1][2].setRotation(0);
70 Assets.playerSprite[0][3].setRotation(0);
71 Assets.playerSprite[1][3].setRotation(0);
72 }
73 if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60) {
74 mAnimDelta = -mAnimDelta;
75 }
77 //back hand
78 Assets.playerSprite[1][2].setPosition(x + 2, y + 8);
79 Assets.playerSprite[1][2].draw(spriteBatch);
80 //back leg
81 Assets.playerSprite[1][3].setPosition(x + 2, y + 20);
82 Assets.playerSprite[1][3].draw(spriteBatch);
83 //front leg
84 Assets.playerSprite[0][3].setPosition(x + 2, y + 20);
85 Assets.playerSprite[0][3].draw(spriteBatch);
86 //head
87 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x, y);
88 //body
89 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x + 2, y + 8);
90 //front hand
91 Assets.playerSprite[0][2].setPosition(x + 2, y + 8);
92 Assets.playerSprite[0][2].draw(spriteBatch);
93 }
95 }