DEADSOFTWARE

5810eb55a047e86b8977341e32256c3ecd491b3c
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / objects / Player.java
1 package ru.deadsoftware.cavecraft.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.cavecraft.game.GameItems;
8 import ru.deadsoftware.cavecraft.game.GameWorld;
9 import ru.deadsoftware.cavecraft.game.mobs.Mob;
10 import ru.deadsoftware.cavecraft.misc.Assets;
12 import java.io.Serializable;
14 public class Player extends Mob implements Serializable {
16 public int[] inv;
17 public int invSlot;
18 public int gameMode;
19 public boolean swim;
21 public Player(GameWorld world, int gameMode) {
22 super(0, 0, 4, 30, 1, true);
23 this.gameMode = gameMode;
24 inv = new int[9];
25 pos = getSpawnPoint(world).cpy();
26 swim = false;
27 }
29 public void respawn(GameWorld world) {
30 pos.set(getSpawnPoint(world));
31 mov.setZero();
32 }
34 private Vector2 getSpawnPoint(GameWorld world) {
35 int x = 0, y;
36 for (y = 0; y < world.getHeight(); y++) {
37 if (y == world.getHeight() - 1) {
38 y = 60;
39 world.setForeMap(x, y, 1);
40 break;
41 }
42 if (world.getForeMap(x, y) > 0 && GameItems.getBlock(world.getForeMap(x, y)).hasCollision()) break;
43 }
44 return new Vector2(x * 16 + 8 - (float) getWidth() / 2, (float) y * 16 - getHeight());
45 }
47 public void setDir(int dir) {
48 if (dir != getDir()) 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[getDir()][0], x - 2, y - 2);
86 //body
87 spriteBatch.draw(Assets.plSprite[getDir()][1], x - 2, y + 8);
88 //item in hand
89 if (inv[invSlot] > 0) {
90 float handRotation = MathUtils.degRad * Assets.plSprite[0][2].getRotation();
91 switch (GameItems.getItem(inv[invSlot]).getType()) {
92 case 0:
93 Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition(
94 x - 8 * MathUtils.sin(handRotation),
95 y + 6 + 8 * MathUtils.cos(handRotation));
96 Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch);
97 break;
98 default:
99 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false);
100 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setRotation(
101 -45 + getDir() * 90 + Assets.plSprite[0][2].getRotation());
102 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition(
103 x - 10 + (12 * getDir()) - 8 * MathUtils.sin(handRotation),
104 y + 2 + 8 * MathUtils.cos(handRotation));
105 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch);
106 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false);
107 break;
110 //front hand
111 Assets.plSprite[0][2].setPosition(x - 6, y);
112 Assets.plSprite[0][2].draw(spriteBatch);
115 @Override
116 public int getType() {
117 return 0;
120 @Override
121 public Rectangle getRect() {
122 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());