DEADSOFTWARE

b45b942697356a3b23450a355e8b5b5178dc9b09
[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 private static final float SPEED = 69.072f;
20 private static final float JUMP_VELOCITY = -133.332f;
22 public final int[] inventory;
23 public int slot;
24 public final int gameMode;
25 public boolean swim;
26 public float headRotation = 0f;
28 public Player() {
29 super(0, 0, 4, 30, randomDir(), Type.MOB);
30 this.gameMode = 1;
31 inventory = new int[9];
32 swim = false;
33 }
35 public void respawn(GameWorld gameWorld) {
36 Vector2 pos = getSpawnPoint(gameWorld);
37 this.x = pos.x;
38 this.y = pos.y;
39 mVelocity.setZero();
40 mDead = false;
41 }
43 public void pickUpDrop(Drop drop) {
44 for (int i = 0; i < inventory.length; i++) {
45 if (inventory[i] == 0 || inventory[i] == drop.getId()) {
46 inventory[i] = drop.getId();
47 drop.setPickedUp(true);
48 break;
49 }
50 }
51 }
53 private Vector2 getSpawnPoint(GameWorld gameWorld) {
54 int y;
55 for (y = 0; y < gameWorld.getHeight(); y++) {
56 if (y == gameWorld.getHeight() - 1) {
57 y = 60;
58 gameWorld.setForeMap(0, y, 1);
59 break;
60 }
61 if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
62 break;
63 }
64 }
65 return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
66 }
68 public void setDir(Direction dir) {
69 if (dir != getDirection()) {
70 switchDir();
71 }
72 }
74 @Override
75 public float getSpeed() {
76 return SPEED;
77 }
79 @Override
80 public void jump() {
81 mVelocity.y = JUMP_VELOCITY;
82 }
84 @Override
85 public void ai(GameWorld gameWorld, float delta) {
86 }
88 @Override
89 public void changeDir() {
90 }
92 private void drawItem(SpriteBatch spriteBatch, float x, float y) {
93 final int itemId = inventory[slot];
94 final Item item = GameItems.getItem(itemId);
96 @CheckForNull final Sprite sprite = item.isBlock()
97 ? item.toBlock().getTexture()
98 : item.getSprite();
100 if (sprite == null) {
101 return;
104 if (!item.isTool()) {
105 sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE);
108 final float handLength = Assets.playerSprite[0][2].getHeight();
110 final SpriteOrigin spriteOrigin = item.getDefaultOrigin();
111 final int handMultiplier = -getDirection().getBasis();
112 final float xOffset = (-1 + getDirection().getIndex()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX());
113 final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0;
115 float rotate = mAnim + 30;
117 final float itemX = x + handLength * MathUtils.sin(handMultiplier * mAnim * MathUtils.degRad) + xOffset;
118 final float itemY = y + handLength * MathUtils.cos(handMultiplier * mAnim * MathUtils.degRad) + yOffset;
120 if (looksLeft()) {
121 sprite.setFlip(true, sprite.isFlipY());
122 SpriteUtilsKt.applyOrigin(sprite, spriteOrigin.getFlipped(true, false));
123 } else {
124 sprite.setFlip(false, sprite.isFlipY());
125 SpriteUtilsKt.applyOrigin(sprite, spriteOrigin);
128 SpriteUtilsKt.drawSprite(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate);
130 // dont forget to reset
131 sprite.setFlip(false, sprite.isFlipY());
132 sprite.setRotation(0);
133 sprite.setOriginCenter();
136 @Override
137 public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
138 updateAnimation(delta);
140 final Sprite backHand = Assets.playerSprite[1][2];
141 final Sprite backLeg = Assets.playerSprite[1][3];
142 final Sprite frontLeg = Assets.playerSprite[0][3];
143 final Sprite head = Assets.playerSprite[getDirection().getIndex()][0];
144 final Sprite body = Assets.playerSprite[getDirection().getIndex()][1];
145 final Sprite frontHand = Assets.playerSprite[0][2];
147 SpriteUtilsKt.drawSprite(spriteBatch, backHand, x + 2, y + 8, -mAnim);
149 if (looksLeft()) {
150 drawItem(spriteBatch, x, y);
153 SpriteUtilsKt.drawSprite(spriteBatch, backLeg, x + 2, y + 20, mAnim);
154 SpriteUtilsKt.drawSprite(spriteBatch, frontLeg, x + 2, y + 20, -mAnim);
155 SpriteUtilsKt.drawSprite(spriteBatch, head, x, y, headRotation);
156 SpriteUtilsKt.drawSprite(spriteBatch, body, x + 2, y + 8);
158 if (looksRight()) {
159 drawItem(spriteBatch, x, y);
162 SpriteUtilsKt.drawSprite(spriteBatch, frontHand, x + 2, y + 8, mAnim);