X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fmobs%2FPlayer.java;h=ebdd9c0dbed6281204809267f6ed0997f270925b;hb=9f899928ab57fc6c2c6151023190ef8d7c3eac03;hp=580665ef8950f1daa3d6ab59faf1776412e7d5ab;hpb=e7a1e15a93abaafa8e2e0435336483a198cc697c;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 580665e..ebdd9c0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -4,12 +4,26 @@ 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 javax.annotation.CheckForNull; + public class Player extends Mob { + private static final float SPEED = 69.072f; + private static final float JUMP_VELOCITY = -133.332f; + private static final int MAX_HEALTH = 20; + + private boolean hitting = false; + private float hitAnim = 0f; + private float hitAnimDelta = ANIMATION_SPEED; + public final int[] inventory; public int slot; public final int gameMode; @@ -17,7 +31,7 @@ public class Player extends Mob { public float headRotation = 0f; public Player() { - super(0, 0, 4, 30, randomDir(), Type.MOB); + super(0, 0, 4, 30, randomDir(), Type.MOB, MAX_HEALTH); this.gameMode = 1; inventory = new int[9]; swim = false; @@ -28,6 +42,18 @@ public class Player extends Mob { this.x = pos.x; this.y = pos.y; mVelocity.setZero(); + mDead = false; + heal(MAX_HEALTH); + } + + 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) { @@ -45,42 +71,124 @@ public class Player extends Mob { return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight()); } - private boolean isAnimationIncreasing() { - return mAnim > 0 && mAnimDelta > 0 || mAnim < 0 && mAnimDelta < 0; + public void setDir(Direction dir) { + if (dir != getDirection()) { + switchDir(); + } + } + + @Override + public float getSpeed() { + return SPEED; } - private void updateAnimation(float delta) { - if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) { - mAnim += mAnimDelta * delta; - } else { - mAnim = 0; + @Override + public void jump() { + mVelocity.y = JUMP_VELOCITY; + } + + @Override + public void ai(GameWorld gameWorld, float delta) { + } + + @Override + public void changeDir() { + } + + @Override + public void damage(int damage) { + if (gameMode == 1) { + return; + } + super.damage(damage); + } + + @Override + public void heal(int heal) { + if (gameMode == 1) { + return; + } + super.heal(heal); + } + + private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) { + 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 (mAnim > 60f) { - mAnim = 60f; - mAnimDelta = -ANIMATION_SPEED; - } else if (mAnim < -60f) { - mAnim = -60f; - mAnimDelta = ANIMATION_SPEED; + if (!item.isTool()) { + sprite.setSize(Drop.DROP_SIZE, Drop.DROP_SIZE); } - if (mVelocity.x == 0f && isAnimationIncreasing()) { - mAnimDelta = -mAnimDelta; + final float handLength = Assets.playerSprite[0][2].getHeight(); + + final SpriteOrigin spriteOrigin = item.getDefaultOrigin(); + final int handMultiplier = -getDirection().getBasis(); + final float xOffset = (-1 + getDirection().getIndex()) * sprite.getWidth() + 4 + handMultiplier * (sprite.getWidth() * spriteOrigin.getX()); + final float yOffset = item.isTool() ? -sprite.getHeight() / 2 : 0; + + float rotate = anim + 30; + + final float itemX = x + handLength * MathUtils.sin(handMultiplier * anim * MathUtils.degRad) + xOffset; + final float itemY = y + handLength * MathUtils.cos(handMultiplier * anim * MathUtils.degRad) + yOffset; + + if (looksLeft()) { + sprite.setFlip(true, sprite.isFlipY()); + SpriteUtilsKt.applyOrigin(sprite, spriteOrigin.getFlipped(true, false)); + } else { + sprite.setFlip(false, sprite.isFlipY()); + SpriteUtilsKt.applyOrigin(sprite, spriteOrigin); } + + SpriteUtilsKt.drawSprite(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate); + + // dont forget to reset + sprite.setFlip(false, sprite.isFlipY()); + sprite.setRotation(0); + sprite.setOriginCenter(); } - public void setDir(Direction dir) { - if (dir != getDirection()) { - switchDir(); + public void startHitting() { + if (hitting) { + return; } + + hitting = true; + hitAnim = 90f; + hitAnimDelta = ANIMATION_SPEED; } - @Override - public void ai(GameWorld gameWorld, float delta) { + public void stopHitting() { + hitting = false; } - @Override - public void changeDir() { + private float getRightHandAnim(float delta) { + hitAnim -= hitAnimDelta * delta; + + if (hitAnim < 30f || hitAnim > 90f) { + if (hitting) { + hitAnimDelta = -hitAnimDelta; + } else { + hitAnimDelta = ANIMATION_SPEED; + } + } + + if (!hitting) { + if (hitAnim < hitAnimDelta * delta) { + hitAnim = 0; + hitAnimDelta = 0; + return -mAnim; + } + } + + return hitAnim; } @Override @@ -90,16 +198,38 @@ public class Player extends Mob { 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 head = Assets.playerSprite[getDirection().getIndex()][0]; + final Sprite body = Assets.playerSprite[getDirection().getIndex()][1]; final Sprite frontHand = Assets.playerSprite[0][2]; - SpriteUtilsKt.draw(spriteBatch, backHand, x + 2, y + 8, -mAnim); - 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); - SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim); + float backHandAnim, frontHandAnim; + + final float rightHandAnim = getRightHandAnim(delta); + + if (looksLeft()) { + backHandAnim = rightHandAnim; + frontHandAnim = mAnim; + } else { + backHandAnim = -mAnim; + frontHandAnim = -rightHandAnim; + } + + SpriteUtilsKt.drawSprite(spriteBatch, backHand, x + 2, y + 8, backHandAnim); + + if (looksLeft()) { + drawItem(spriteBatch, x, y, -backHandAnim); + } + + SpriteUtilsKt.drawSprite(spriteBatch, backLeg, x + 2, y + 20, mAnim); + SpriteUtilsKt.drawSprite(spriteBatch, frontLeg, x + 2, y + 20, -mAnim); + SpriteUtilsKt.drawSprite(spriteBatch, head, x, y, headRotation); + SpriteUtilsKt.drawSprite(spriteBatch, body, x + 2, y + 8); + + if (looksRight()) { + drawItem(spriteBatch, x, y, frontHandAnim); + } + + SpriteUtilsKt.drawSprite(spriteBatch, frontHand, x + 2, y + 8, frontHandAnim); } }