DEADSOFTWARE

Move items to JSON
[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.MathUtils;
5 import com.badlogic.gdx.math.Rectangle;
6 import com.badlogic.gdx.math.Vector2;
7 import ru.deadsoftware.cavedroid.game.GameItems;
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[] inv;
18 public int invSlot;
19 public int gameMode;
20 public boolean swim;
22 public Player(int gameMode) {
23 super(0, 0, 4, 30, 1, true);
24 this.gameMode = gameMode;
25 inv = new int[9];
26 pos = getSpawnPoint().cpy();
27 swim = false;
28 }
30 public void respawn() {
31 pos.set(getSpawnPoint());
32 mov.setZero();
33 }
35 private Vector2 getSpawnPoint() {
36 int x = 0, y;
37 for (y = 0; y < GP.world.getHeight(); y++) {
38 if (y == GP.world.getHeight() - 1) {
39 y = 60;
40 GP.world.setForeMap(x, y, 1);
41 break;
42 }
43 if (GP.world.getForeMap(x, y) > 0 && GameItems.getBlock(GP.world.getForeMap(x, y)).hasCollision()) break;
44 }
45 return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight());
46 }
48 public void setDir(int dir) {
49 if (dir != getDir()) switchDir();
50 }
52 @Override
53 public void ai() {
54 }
56 @Override
57 public void changeDir() {
58 }
60 @Override
61 public void draw(SpriteBatch spriteBatch, float x, float y) {
62 if (mov.x != 0 || Assets.plSprite[0][2].getRotation() != 0) {
63 Assets.plSprite[0][2].rotate(animDelta);
64 Assets.plSprite[1][2].rotate(-animDelta);
65 Assets.plSprite[0][3].rotate(-animDelta);
66 Assets.plSprite[1][3].rotate(animDelta);
67 } else {
68 Assets.plSprite[0][2].setRotation(0);
69 Assets.plSprite[1][2].setRotation(0);
70 Assets.plSprite[0][3].setRotation(0);
71 Assets.plSprite[1][3].setRotation(0);
72 }
73 if (Assets.plSprite[0][2].getRotation() >= 60 || Assets.plSprite[0][2].getRotation() <= -60)
74 animDelta = -animDelta;
76 //back hand
77 Assets.plSprite[1][2].setPosition(x - 6, y);
78 Assets.plSprite[1][2].draw(spriteBatch);
79 //back leg
80 Assets.plSprite[1][3].setPosition(x - 6, y + 10);
81 Assets.plSprite[1][3].draw(spriteBatch);
82 //front leg
83 Assets.plSprite[0][3].setPosition(x - 6, y + 10);
84 Assets.plSprite[0][3].draw(spriteBatch);
85 //head
86 spriteBatch.draw(Assets.plSprite[getDir()][0], x - 2, y - 2);
87 //body
88 spriteBatch.draw(Assets.plSprite[getDir()][1], x - 2, y + 8);
89 //front hand
90 Assets.plSprite[0][2].setPosition(x - 6, y);
91 Assets.plSprite[0][2].draw(spriteBatch);
92 }
94 @Override
95 public int getType() {
96 return 0;
97 }
99 @Override
100 public Rectangle getRect() {
101 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());