DEADSOFTWARE

Implement dependency injection for game classes #13
[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.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 mMove.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) {
52 }
54 @Override
55 public void changeDir() {
56 }
58 @Override
59 public void draw(SpriteBatch spriteBatch, float x, float y) {
60 if (mMove.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
61 Assets.playerSprite[0][2].rotate(mAnimDelta);
62 Assets.playerSprite[1][2].rotate(-mAnimDelta);
63 Assets.playerSprite[0][3].rotate(-mAnimDelta);
64 Assets.playerSprite[1][3].rotate(mAnimDelta);
65 } else {
66 Assets.playerSprite[0][2].setRotation(0);
67 Assets.playerSprite[1][2].setRotation(0);
68 Assets.playerSprite[0][3].setRotation(0);
69 Assets.playerSprite[1][3].setRotation(0);
70 }
71 if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60) {
72 mAnimDelta = -mAnimDelta;
73 }
75 //back hand
76 Assets.playerSprite[1][2].setPosition(x + 2, y + 8);
77 Assets.playerSprite[1][2].draw(spriteBatch);
78 //back leg
79 Assets.playerSprite[1][3].setPosition(x + 2, y + 20);
80 Assets.playerSprite[1][3].draw(spriteBatch);
81 //front leg
82 Assets.playerSprite[0][3].setPosition(x + 2, y + 20);
83 Assets.playerSprite[0][3].draw(spriteBatch);
84 //head
85 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x, y);
86 //body
87 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x + 2, y + 8);
88 //front hand
89 Assets.playerSprite[0][2].setPosition(x + 2, y + 8);
90 Assets.playerSprite[0][2].draw(spriteBatch);
91 }
93 }