DEADSOFTWARE

Add player head rotation
[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.Sprite;
4 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
5 import com.badlogic.gdx.math.MathUtils;
6 import com.badlogic.gdx.math.Vector2;
7 import ru.deadsoftware.cavedroid.game.world.GameWorld;
8 import ru.deadsoftware.cavedroid.misc.Assets;
9 import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
11 public class Player extends Mob {
13 public final int[] inventory;
14 public int slot;
15 public final int gameMode;
16 public boolean swim;
17 public float headRotation = 0f;
19 public Player() {
20 super(0, 0, 4, 30, randomDir(), Type.MOB);
21 this.gameMode = 1;
22 inventory = new int[9];
23 swim = false;
24 }
26 public void respawn(GameWorld gameWorld) {
27 Vector2 pos = getSpawnPoint(gameWorld);
28 this.x = pos.x;
29 this.y = pos.y;
30 mVelocity.setZero();
31 }
33 private Vector2 getSpawnPoint(GameWorld gameWorld) {
34 int y;
35 for (y = 0; y < gameWorld.getHeight(); y++) {
36 if (y == gameWorld.getHeight() - 1) {
37 y = 60;
38 gameWorld.setForeMap(0, y, 1);
39 break;
40 }
41 if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
42 break;
43 }
44 }
45 return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
46 }
48 private boolean isAnimationIncreasing() {
49 return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0;
50 }
52 private void updateAnimation(float delta) {
53 if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) {
54 mAnim += mAnimDelta * delta;
55 } else {
56 mAnim = 0;
57 }
59 if (mAnim > 60f) {
60 mAnim = 60f;
61 mAnimDelta = -ANIMATION_SPEED;
62 } else if (mAnim < -60f) {
63 mAnim = -60f;
64 mAnimDelta = ANIMATION_SPEED;
65 }
67 if (mVelocity.x == 0f && isAnimationIncreasing()) {
68 mAnimDelta = -mAnimDelta;
69 }
70 }
72 public void setDir(Direction dir) {
73 if (dir != getDirection()) {
74 switchDir();
75 }
76 }
78 @Override
79 public void ai(GameWorld gameWorld, float delta) {
80 }
82 @Override
83 public void changeDir() {
84 }
86 @Override
87 public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
88 updateAnimation(delta);
90 final Sprite backHand = Assets.playerSprite[1][2];
91 final Sprite backLeg = Assets.playerSprite[1][3];
92 final Sprite frontLeg = Assets.playerSprite[0][3];
93 final Sprite head = Assets.playerSprite[dirMultiplier()][0];
94 final Sprite body = Assets.playerSprite[dirMultiplier()][1];
95 final Sprite frontHand = Assets.playerSprite[0][2];
97 SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim);
98 SpriteUtilsKt.draw(spriteBatch, backLeg, x + 2, y + 20, mAnim);
99 SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 2, y + 20, -mAnim);
100 SpriteUtilsKt.draw(spriteBatch, head, x, y, headRotation);
101 SpriteUtilsKt.draw(spriteBatch, body, x + 2, y + 8);
102 SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim);