diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
index fd7101c7dbd0dafbefab5675098e47cb014723f7..93150b6a5d3f4870187251071d4dfabf2bdeff2c 100644 (file)
package ru.deadsoftware.cavedroid.game.mobs;
+import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
+import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.GameItems;
+import ru.deadsoftware.cavedroid.game.objects.Drop;
+import ru.deadsoftware.cavedroid.game.objects.Item;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
import ru.deadsoftware.cavedroid.misc.Assets;
+import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin;
+import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+import javax.annotation.CheckForNull;
public class Player extends Mob {
public int slot;
public final int gameMode;
public boolean swim;
+ public float headRotation = 0f;
- public Player(int gameMode) {
+ public Player() {
super(0, 0, 4, 30, randomDir(), Type.MOB);
- this.gameMode = gameMode;
+ this.gameMode = 1;
inventory = new int[9];
swim = false;
}
- public void respawn() {
- Vector2 pos = getSpawnPoint();
+ public void respawn(GameWorld gameWorld) {
+ Vector2 pos = getSpawnPoint(gameWorld);
this.x = pos.x;
this.y = pos.y;
- move.setZero();
+ mVelocity.setZero();
+ mDead = false;
}
- private Vector2 getSpawnPoint() {
- int x = 0, y;
- for (y = 0; y < GP.world.getHeight(); y++) {
- if (y == GP.world.getHeight() - 1) {
+ public void pickUpDrop(Drop drop) {
+ for (int i = 0; i < inventory.length; i++) {
+ if (inventory[i] == 0 || inventory[i] == drop.getId()) {
+ inventory[i] = drop.getId();
+ drop.setPickedUp(true);
+ break;
+ }
+ }
+ }
+
+ private Vector2 getSpawnPoint(GameWorld gameWorld) {
+ int y;
+ for (y = 0; y < gameWorld.getHeight(); y++) {
+ if (y == gameWorld.getHeight() - 1) {
y = 60;
- GP.world.setForeMap(x, y, 1);
+ gameWorld.setForeMap(0, y, 1);
break;
}
- if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) {
+ if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
break;
}
}
- return new Vector2(x * 16 + 8 - getWidth() / 2, (float) y * 16 - getHeight());
+ return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
}
public void setDir(Direction dir) {
}
@Override
- public void ai() {
+ public void ai(GameWorld gameWorld, float delta) {
}
@Override
public void changeDir() {
}
- @Override
- public void draw(SpriteBatch spriteBatch, float x, float y) {
- if (move.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
- Assets.playerSprite[0][2].rotate(animDelta);
- Assets.playerSprite[1][2].rotate(-animDelta);
- Assets.playerSprite[0][3].rotate(-animDelta);
- Assets.playerSprite[1][3].rotate(animDelta);
+ private void drawItem(SpriteBatch spriteBatch, float x, float y) {
+ final int itemId = inventory[slot];
+ final Item item = GameItems.getItem(itemId);
+
+ @CheckForNull final Sprite sprite = item.isBlock()
+ ? item.toBlock().getTexture()
+ : item.getSprite();
+
+ if (sprite == null) {
+ return;
+ }
+
+ if (!item.isTool()) {
+ sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE);
+ }
+
+ final float handLength = Assets.playerSprite[0][2].getHeight();
+
+ final SpriteOrigin spriteOrigin = item.getDefaultOrigin();
+ final int handMultiplier = 1 + -2 * dirMultiplier();
+ final float xOffset = (-1 + dirMultiplier()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX());
+ final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0;
+
+ float rotate = mAnim + 30;
+
+ final float itemX = x + handLength * MathUtils.sin(handMultiplier * mAnim * MathUtils.degRad) + xOffset;
+ final float itemY = y + handLength * MathUtils.cos(handMultiplier * mAnim * MathUtils.degRad) + yOffset;
+
+ if (looksLeft()) {
+ sprite.setFlip(true, sprite.isFlipY());
+ SpriteUtilsKt.applyOrigin(sprite, spriteOrigin.getFlipped(true, false));
} else {
- Assets.playerSprite[0][2].setRotation(0);
- Assets.playerSprite[1][2].setRotation(0);
- Assets.playerSprite[0][3].setRotation(0);
- Assets.playerSprite[1][3].setRotation(0);
+ sprite.setFlip(false, sprite.isFlipY());
+ SpriteUtilsKt.applyOrigin(sprite, spriteOrigin);
}
- if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60) {
- animDelta = -animDelta;
+
+ SpriteUtilsKt.draw(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate);
+
+ // dont forget to reset
+ sprite.setFlip(false, sprite.isFlipY());
+ sprite.setRotation(0);
+ sprite.setOriginCenter();
+ }
+
+ @Override
+ public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
+ updateAnimation(delta);
+
+ final Sprite backHand = Assets.playerSprite[1][2];
+ final Sprite backLeg = Assets.playerSprite[1][3];
+ final Sprite frontLeg = Assets.playerSprite[0][3];
+ final Sprite head = Assets.playerSprite[dirMultiplier()][0];
+ final Sprite body = Assets.playerSprite[dirMultiplier()][1];
+ final Sprite frontHand = Assets.playerSprite[0][2];
+
+ SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim);
+
+ if (looksLeft()) {
+ drawItem(spriteBatch, x, y);
+ }
+
+ SpriteUtilsKt.draw(spriteBatch, backLeg, x + 2, y + 20, mAnim);
+ SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 2, y + 20, -mAnim);
+ SpriteUtilsKt.draw(spriteBatch, head, x, y, headRotation);
+ SpriteUtilsKt.draw(spriteBatch, body, x + 2, y + 8);
+
+ if (looksRight()) {
+ drawItem(spriteBatch, x, y);
}
- //back hand
- Assets.playerSprite[1][2].setPosition(x + 2, y + 8);
- Assets.playerSprite[1][2].draw(spriteBatch);
- //back leg
- Assets.playerSprite[1][3].setPosition(x + 2, y + 20);
- Assets.playerSprite[1][3].draw(spriteBatch);
- //front leg
- Assets.playerSprite[0][3].setPosition(x + 2, y + 20);
- Assets.playerSprite[0][3].draw(spriteBatch);
- //head
- spriteBatch.draw(Assets.playerSprite[dirMultiplier()][0], x, y);
- //body
- spriteBatch.draw(Assets.playerSprite[dirMultiplier()][1], x + 2, y + 8);
- //front hand
- Assets.playerSprite[0][2].setPosition(x + 2, y + 8);
- Assets.playerSprite[0][2].draw(spriteBatch);
+ SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim);
}
}