DEADSOFTWARE

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