DEADSOFTWARE

Refactor
[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.mobs.Mob;
7 import ru.deadsoftware.cavedroid.misc.Assets;
9 import java.io.Serializable;
11 import static ru.deadsoftware.cavedroid.GameScreen.GP;
13 public class Player extends Mob implements Serializable {
15 public int[] inventory;
16 public int slot;
17 public int gameMode;
18 public boolean swim;
20 public Player(int gameMode) {
21 super(0, 0, 4, 30, 1);
22 this.gameMode = gameMode;
23 inventory = new int[9];
24 swim = false;
25 }
27 public void respawn() {
28 pos.set(getSpawnPoint());
29 mov.setZero();
30 }
32 private Vector2 getSpawnPoint() {
33 int x = 0, y;
34 for (y = 0; y < GP.world.getHeight(); y++) {
35 if (y == GP.world.getHeight() - 1) {
36 y = 60;
37 GP.world.setForeMap(x, y, 1);
38 break;
39 }
40 if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) break;
41 }
42 return new Vector2(x * 16 + 8 - getWidth() / 2, (float) y * 16 - getHeight());
43 }
45 public void setDir(int dir) {
46 if (dir != getDirection()) switchDir();
47 }
49 @Override
50 public void ai() {
51 }
53 @Override
54 public void changeDir() {
55 }
57 @Override
58 public void draw(SpriteBatch spriteBatch, float x, float y) {
59 if (mov.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
60 Assets.playerSprite[0][2].rotate(animDelta);
61 Assets.playerSprite[1][2].rotate(-animDelta);
62 Assets.playerSprite[0][3].rotate(-animDelta);
63 Assets.playerSprite[1][3].rotate(animDelta);
64 } else {
65 Assets.playerSprite[0][2].setRotation(0);
66 Assets.playerSprite[1][2].setRotation(0);
67 Assets.playerSprite[0][3].setRotation(0);
68 Assets.playerSprite[1][3].setRotation(0);
69 }
70 if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60)
71 animDelta = -animDelta;
73 //back hand
74 Assets.playerSprite[1][2].setPosition(x - 6, y);
75 Assets.playerSprite[1][2].draw(spriteBatch);
76 //back leg
77 Assets.playerSprite[1][3].setPosition(x - 6, y + 10);
78 Assets.playerSprite[1][3].draw(spriteBatch);
79 //front leg
80 Assets.playerSprite[0][3].setPosition(x - 6, y + 10);
81 Assets.playerSprite[0][3].draw(spriteBatch);
82 //head
83 spriteBatch.draw(Assets.playerSprite[getDirection()][0], x - 2, y - 2);
84 //body
85 spriteBatch.draw(Assets.playerSprite[getDirection()][1], x - 2, y + 8);
86 //front hand
87 Assets.playerSprite[0][2].setPosition(x - 6, y);
88 Assets.playerSprite[0][2].draw(spriteBatch);
89 }
91 @Override
92 public int getType() {
93 return 0;
94 }
96 @Override
97 public Rectangle getRect() {
98 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());
99 }