DEADSOFTWARE

Refactor physics
[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.misc.Assets;
7 import static ru.deadsoftware.cavedroid.GameScreen.GP;
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(int gameMode) {
17 super(0, 0, 4, 30, randomDir(), Type.MOB);
18 this.gameMode = gameMode;
19 inventory = new int[9];
20 swim = false;
21 }
23 public void respawn() {
24 Vector2 pos = getSpawnPoint();
25 this.x = pos.x;
26 this.y = pos.y;
27 move.setZero();
28 }
30 private Vector2 getSpawnPoint() {
31 int x = 0, y;
32 for (y = 0; y < GP.world.getHeight(); y++) {
33 if (y == GP.world.getHeight() - 1) {
34 y = 60;
35 GP.world.setForeMap(x, y, 1);
36 break;
37 }
38 if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) {
39 break;
40 }
41 }
42 return new Vector2(x * 16 + 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() {
53 }
55 @Override
56 public void changeDir() {
57 }
59 @Override
60 public void draw(SpriteBatch spriteBatch, float x, float y) {
61 if (move.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
62 Assets.playerSprite[0][2].rotate(animDelta);
63 Assets.playerSprite[1][2].rotate(-animDelta);
64 Assets.playerSprite[0][3].rotate(-animDelta);
65 Assets.playerSprite[1][3].rotate(animDelta);
66 } else {
67 Assets.playerSprite[0][2].setRotation(0);
68 Assets.playerSprite[1][2].setRotation(0);
69 Assets.playerSprite[0][3].setRotation(0);
70 Assets.playerSprite[1][3].setRotation(0);
71 }
72 if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60)
73 animDelta = -animDelta;
75 //back hand
76 Assets.playerSprite[1][2].setPosition(x - 6, y);
77 Assets.playerSprite[1][2].draw(spriteBatch);
78 //back leg
79 Assets.playerSprite[1][3].setPosition(x - 6, y + 10);
80 Assets.playerSprite[1][3].draw(spriteBatch);
81 //front leg
82 Assets.playerSprite[0][3].setPosition(x - 6, y + 10);
83 Assets.playerSprite[0][3].draw(spriteBatch);
84 //head
85 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x - 2, y - 2);
86 //body
87 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x - 2, y + 8);
88 //front hand
89 Assets.playerSprite[0][2].setPosition(x - 6, y);
90 Assets.playerSprite[0][2].draw(spriteBatch);
91 }
93 }