DEADSOFTWARE

Add item in player hand
[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.GameItems;
8 import ru.deadsoftware.cavedroid.game.objects.Drop;
9 import ru.deadsoftware.cavedroid.game.objects.Item;
10 import ru.deadsoftware.cavedroid.game.world.GameWorld;
11 import ru.deadsoftware.cavedroid.misc.Assets;
12 import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
13 import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
15 import javax.annotation.CheckForNull;
17 public class Player extends Mob {
19 public final int[] inventory;
20 public int slot;
21 public final int gameMode;
22 public boolean swim;
23 public float headRotation = 0f;
25 public Player() {
26 super(0, 0, 4, 30, randomDir(), Type.MOB);
27 this.gameMode = 1;
28 inventory = new int[9];
29 swim = false;
30 }
32 public void respawn(GameWorld gameWorld) {
33 Vector2 pos = getSpawnPoint(gameWorld);
34 this.x = pos.x;
35 this.y = pos.y;
36 mVelocity.setZero();
37 mDead = false;
38 }
40 public void pickUpDrop(Drop drop) {
41 for (int i = 0; i < inventory.length; i++) {
42 if (inventory[i] == 0 || inventory[i] == drop.getId()) {
43 inventory[i] = drop.getId();
44 drop.setPickedUp(true);
45 break;
46 }
47 }
48 }
50 private Vector2 getSpawnPoint(GameWorld gameWorld) {
51 int y;
52 for (y = 0; y < gameWorld.getHeight(); y++) {
53 if (y == gameWorld.getHeight() - 1) {
54 y = 60;
55 gameWorld.setForeMap(0, y, 1);
56 break;
57 }
58 if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
59 break;
60 }
61 }
62 return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
63 }
65 public void setDir(Direction dir) {
66 if (dir != getDirection()) {
67 switchDir();
68 }
69 }
71 @Override
72 public void ai(GameWorld gameWorld, float delta) {
73 }
75 @Override
76 public void changeDir() {
77 }
79 private void drawItem(SpriteBatch spriteBatch, float x, float y) {
80 final int itemId = inventory[slot];
81 final Item item = GameItems.getItem(itemId);
83 @CheckForNull final Sprite sprite = item.isBlock()
84 ? item.toBlock().getTexture()
85 : item.getSprite();
87 if (sprite == null) {
88 return;
89 }
91 if (!item.isTool()) {
92 sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE);
93 }
95 final float handLength = Assets.playerSprite[0][2].getHeight();
97 final SpriteOrigin spriteOrigin = item.getDefaultOrigin();
98 final int handMultiplier = 1 + -2 * dirMultiplier();
99 final float xOffset = (-1 + dirMultiplier()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX());
100 final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0;
102 float rotate = mAnim + 30;
104 final float itemX = x + handLength * MathUtils.sin(handMultiplier * mAnim * MathUtils.degRad) + xOffset;
105 final float itemY = y + handLength * MathUtils.cos(handMultiplier * mAnim * MathUtils.degRad) + yOffset;
107 if (looksLeft()) {
108 sprite.setFlip(true, sprite.isFlipY());
109 SpriteUtilsKt.applyOrigin(sprite, spriteOrigin.getFlipped(true, false));
110 } else {
111 sprite.setFlip(false, sprite.isFlipY());
112 SpriteUtilsKt.applyOrigin(sprite, spriteOrigin);
115 SpriteUtilsKt.draw(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate);
117 // dont forget to reset
118 sprite.setFlip(false, sprite.isFlipY());
119 sprite.setRotation(0);
120 sprite.setOriginCenter();
123 @Override
124 public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
125 updateAnimation(delta);
127 final Sprite backHand = Assets.playerSprite[1][2];
128 final Sprite backLeg = Assets.playerSprite[1][3];
129 final Sprite frontLeg = Assets.playerSprite[0][3];
130 final Sprite head = Assets.playerSprite[dirMultiplier()][0];
131 final Sprite body = Assets.playerSprite[dirMultiplier()][1];
132 final Sprite frontHand = Assets.playerSprite[0][2];
134 SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim);
136 if (looksLeft()) {
137 drawItem(spriteBatch, x, y);
140 SpriteUtilsKt.draw(spriteBatch, backLeg, x + 2, y + 20, mAnim);
141 SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 2, y + 20, -mAnim);
142 SpriteUtilsKt.draw(spriteBatch, head, x, y, headRotation);
143 SpriteUtilsKt.draw(spriteBatch, body, x + 2, y + 8);
145 if (looksRight()) {
146 drawItem(spriteBatch, x, y);
149 SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim);