DEADSOFTWARE

Some refactor and javadocs
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / objects / Player.java
1 package ru.deadsoftware.cavedroid.game.objects;
3 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
4 import com.badlogic.gdx.math.Rectangle;
5 import com.badlogic.gdx.math.Vector2;
6 import ru.deadsoftware.cavedroid.game.GameItems;
7 import ru.deadsoftware.cavedroid.game.GameWorld;
8 import ru.deadsoftware.cavedroid.game.mobs.Mob;
9 import ru.deadsoftware.cavedroid.misc.Assets;
11 import java.io.Serializable;
13 import static ru.deadsoftware.cavedroid.GameScreen.GP;
15 public class Player extends Mob implements Serializable {
17 public int[] inventory;
18 public int slot;
19 public int gameMode;
20 public boolean swim;
22 public Player(int gameMode) {
23 super(0, 0, 4, 30, 1);
24 this.gameMode = gameMode;
25 inventory = new int[9];
26 swim = false;
27 }
29 public void respawn() {
30 pos.set(getSpawnPoint());
31 mov.setZero();
32 }
34 private Vector2 getSpawnPoint() {
35 int x = 0, y;
36 for (y = 0; y < GP.world.getHeight(); y++) {
37 if (y == GP.world.getHeight() - 1) {
38 y = 60;
39 GP.world.setForeMap(x, y, 1);
40 break;
41 }
42 if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) break;
43 }
44 return new Vector2(x * 16 + 8 - getWidth() / 2, (float) y * 16 - getHeight());
45 }
47 public void setDir(int dir) {
48 if (dir != getDirection()) switchDir();
49 }
51 @Override
52 public void ai() {
53 }
55 @Override
56 public void changeDir() {
57 }
59 @Override
60 public void draw(SpriteBatch spriteBatch, float x, float y) {
61 if (mov.x != 0 || Assets.plSprite[0][2].getRotation() != 0) {
62 Assets.plSprite[0][2].rotate(animDelta);
63 Assets.plSprite[1][2].rotate(-animDelta);
64 Assets.plSprite[0][3].rotate(-animDelta);
65 Assets.plSprite[1][3].rotate(animDelta);
66 } else {
67 Assets.plSprite[0][2].setRotation(0);
68 Assets.plSprite[1][2].setRotation(0);
69 Assets.plSprite[0][3].setRotation(0);
70 Assets.plSprite[1][3].setRotation(0);
71 }
72 if (Assets.plSprite[0][2].getRotation() >= 60 || Assets.plSprite[0][2].getRotation() <= -60)
73 animDelta = -animDelta;
75 //back hand
76 Assets.plSprite[1][2].setPosition(x - 6, y);
77 Assets.plSprite[1][2].draw(spriteBatch);
78 //back leg
79 Assets.plSprite[1][3].setPosition(x - 6, y + 10);
80 Assets.plSprite[1][3].draw(spriteBatch);
81 //front leg
82 Assets.plSprite[0][3].setPosition(x - 6, y + 10);
83 Assets.plSprite[0][3].draw(spriteBatch);
84 //head
85 spriteBatch.draw(Assets.plSprite[getDirection()][0], x - 2, y - 2);
86 //body
87 spriteBatch.draw(Assets.plSprite[getDirection()][1], x - 2, y + 8);
88 //front hand
89 Assets.plSprite[0][2].setPosition(x - 6, y);
90 Assets.plSprite[0][2].draw(spriteBatch);
91 }
93 @Override
94 public int getType() {
95 return 0;
96 }
98 @Override
99 public Rectangle getRect() {
100 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());