DEADSOFTWARE

Refactor drop and its physics
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Player.java
index 5ea56d9bf8c9b30cbf1d72254b02497473fe8f57..1f42039bfec7bd2969ec002bdacc4c9ee8ae4386 100644 (file)
@@ -1,9 +1,13 @@
 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.objects.Drop;
 import ru.deadsoftware.cavedroid.game.world.GameWorld;
 import ru.deadsoftware.cavedroid.misc.Assets;
+import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
 
 public class Player extends Mob {
 
@@ -11,6 +15,7 @@ public class Player extends Mob {
     public int slot;
     public final int gameMode;
     public boolean swim;
+    public float headRotation = 0f;
 
     public Player() {
         super(0, 0, 4, 30, randomDir(), Type.MOB);
@@ -26,6 +31,16 @@ public class Player extends Mob {
         mVelocity.setZero();
     }
 
+    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++) {
@@ -41,10 +56,6 @@ 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();
@@ -61,37 +72,21 @@ public class Player extends Mob {
 
     @Override
     public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
-        if (mVelocity.x != 0f || Math.abs(mAnim) > 5f) {
-            mAnim += mAnimDelta * delta;
-        } else {
-            mAnim = 0;
-        }
+        updateAnimation(delta);
 
-        Assets.playerSprite[0][2].setRotation(mAnim);
-        Assets.playerSprite[1][2].setRotation(-mAnim);
-        Assets.playerSprite[0][3].setRotation(-mAnim);
-        Assets.playerSprite[1][3].setRotation(mAnim);
-
-        if (mAnim >= 60 || mAnim <= -60 ||(mVelocity.x == 0f && isAnimationIncreasing())) {
-            mAnimDelta = -mAnimDelta;
-        }
+        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];
 
-        //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, 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);
     }
 
 }