DEADSOFTWARE

3335b7f5ebc69322478b8e1eeb7cf82f043b2baa
[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.game.world.GameWorld;
6 import ru.deadsoftware.cavedroid.misc.Assets;
8 public class Player extends Mob {
10 public final int[] inventory;
11 public int slot;
12 public final int gameMode;
13 public boolean swim;
15 public Player() {
16 super(0, 0, 4, 30, randomDir(), Type.MOB);
17 this.gameMode = 1;
18 inventory = new int[9];
19 swim = false;
20 }
22 public void respawn(GameWorld gameWorld) {
23 Vector2 pos = getSpawnPoint(gameWorld);
24 this.x = pos.x;
25 this.y = pos.y;
26 mVelocity.setZero();
27 }
29 private Vector2 getSpawnPoint(GameWorld gameWorld) {
30 int y;
31 for (y = 0; y < gameWorld.getHeight(); y++) {
32 if (y == gameWorld.getHeight() - 1) {
33 y = 60;
34 gameWorld.setForeMap(0, y, 1);
35 break;
36 }
37 if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
38 break;
39 }
40 }
41 return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
42 }
44 private boolean isAnimationIncreasing() {
45 return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
46 }
48 public void setDir(Direction dir) {
49 if (dir != getDirection()) {
50 switchDir();
51 }
52 }
54 @Override
55 public void ai(GameWorld gameWorld, float delta) {
56 }
58 @Override
59 public void changeDir() {
60 }
62 @Override
63 public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
64 if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) {
65 mAnim += mAnimDelta * delta;
66 } else {
67 mAnim = 0;
68 }
70 Assets.playerSprite[0][2].setRotation(mAnim);
71 Assets.playerSprite[1][2].setRotation(-mAnim);
72 Assets.playerSprite[0][3].setRotation(-mAnim);
73 Assets.playerSprite[1][3].setRotation(mAnim);
75 if (mAnim > 60f) {
76 mAnim = 60f;
77 mAnimDelta = -ANIMATION_SPEED;
78 } else if (mAnim < -60f) {
79 mAnim = -60f;
80 mAnimDelta = ANIMATION_SPEED;
81 }
83 if (mVelocity.x == 0f && isAnimationIncreasing()) {
84 mAnimDelta = -mAnimDelta;
85 }
87 //back hand
88 Assets.playerSprite[1][2].setPosition(x + 2, y + 8);
89 Assets.playerSprite[1][2].draw(spriteBatch);
90 //back leg
91 Assets.playerSprite[1][3].setPosition(x + 2, y + 20);
92 Assets.playerSprite[1][3].draw(spriteBatch);
93 //front leg
94 Assets.playerSprite[0][3].setPosition(x + 2, y + 20);
95 Assets.playerSprite[0][3].draw(spriteBatch);
96 //head
97 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x, y);
98 //body
99 spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x + 2, y + 8);
100 //front hand
101 Assets.playerSprite[0][2].setPosition(x + 2, y + 8);
102 Assets.playerSprite[0][2].draw(spriteBatch);