DEADSOFTWARE

Refactor
[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()) changeDir();
49 }
51 @Override
52 public void ai() {
53 }
55 @Override
56 public void changeDir() {
57 switchDir();
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 //item in hand
90 if (inv[invSlot] > 0) {
91 float handRotation = MathUtils.degRad * Assets.plSprite[0][2].getRotation();
92 switch (GameItems.getItem(inv[invSlot]).getType()) {
93 case 0:
94 Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition(
95 x - 8 * MathUtils.sin(handRotation),
96 y + 6 + 8 * MathUtils.cos(handRotation));
97 Assets.blockTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch);
98 break;
99 default:
100 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false);
101 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setRotation(
102 -45 + getDir() * 90 + Assets.plSprite[0][2].getRotation());
103 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].setPosition(
104 x - 10 + (12 * getDir()) - 8 * MathUtils.sin(handRotation),
105 y + 2 + 8 * MathUtils.cos(handRotation));
106 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].draw(spriteBatch);
107 Assets.itemTex[GameItems.getItem(inv[invSlot]).getTex()].flip((getDir() == 0), false);
108 break;
111 //front hand
112 Assets.plSprite[0][2].setPosition(x - 6, y);
113 Assets.plSprite[0][2].draw(spriteBatch);
116 @Override
117 public int getType() {
118 return 0;
121 @Override
122 public Rectangle getRect() {
123 return new Rectangle(pos.x, pos.y, getWidth(), getHeight());