DEADSOFTWARE

Update README.md
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / mobs / Pig.java
index e36da71cdb37d855235287fe8c00a9ac6a4799d7..22903876fd393dd01469d1cad38355927e8c66a2 100644 (file)
@@ -1,58 +1,52 @@
 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.GamePhysics;
+import ru.deadsoftware.cavedroid.game.world.GameWorld;
 import ru.deadsoftware.cavedroid.misc.Assets;
+import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
+
+import static ru.deadsoftware.cavedroid.misc.Assets.pigSprite;
 
 public class Pig extends Mob {
 
     public Pig(float x, float y) {
-        super(x, y, 25, 18, MathUtils.random(1));
-        mov = new Vector2(looksLeft() ? -1 : 1, 0);
+        super(x, y, 25, 18, randomDir(), Type.MOB);
+        mVelocity = new Vector2((looksLeft() ? -1 : 1) * GamePhysics.PL_SPEED, 0);
     }
 
     @Override
     public void changeDir() {
         switchDir();
-        mov.x = -1 + 2 * getDirection();
+        mVelocity.x = (-1 + 2 * dirMultiplier()) * GamePhysics.PL_SPEED;
     }
 
     @Override
-    public void ai() {
-        if (MathUtils.randomBoolean(.0025f)) changeDir();
-        else if (MathUtils.randomBoolean(.0025f)) {
-            if (mov.x != 0f) mov.x = 0;
-            else mov.x = -1 + 2 * getDirection();
-        }
-        if (mov.x != 0f) anim += animDelta;
-        else anim = 0;
-        if (anim >= 60 || anim <= -60) {
-            animDelta = -animDelta;
+    public void ai(GameWorld gameWorld, float delta) {
+        if (MathUtils.randomBoolean(delta)) {
+            if (mVelocity.x != 0f) {
+                mVelocity.x = 0;
+            } else {
+                changeDir();
+            }
         }
     }
 
     @Override
-    public void draw(SpriteBatch spriteBatch, float x, float y) {
-        Assets.pigSprite[0][1].setRotation(anim);
-        Assets.pigSprite[1][1].setRotation(-anim);
-        //back legs
-        Assets.pigSprite[1][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6);
-        Assets.pigSprite[1][1].draw(spriteBatch);
-        Assets.pigSprite[1][1].setPosition(x + 17 - (9 * getDirection()), y + 6);
-        Assets.pigSprite[1][1].draw(spriteBatch);
-        //front legs
-        Assets.pigSprite[0][1].setPosition(x - 4 + (9 - getDirection() * 9), y + 6);
-        Assets.pigSprite[0][1].draw(spriteBatch);
-        Assets.pigSprite[0][1].setPosition(x + 17 - (9 * getDirection()), y + 6);
-        Assets.pigSprite[0][1].draw(spriteBatch);
-        //head & body
-        spriteBatch.draw(Assets.pigSprite[getDirection()][0], x, y);
-    }
-
-    @Override
-    public int getType() {
-        return 0;
+    public void draw(SpriteBatch spriteBatch, float x, float y, float delta) {
+        updateAnimation(delta);
+
+        final Sprite frontLeg = pigSprite[0][1];
+        final Sprite backLeg = pigSprite[1][1];
+        final Sprite body = pigSprite[dirMultiplier()][0];
+
+        SpriteUtilsKt.draw(spriteBatch, backLeg, x + (9 - dirMultiplier() * 9), y + 12, -mAnim);
+        SpriteUtilsKt.draw(spriteBatch, backLeg, x + 21 - (9 * dirMultiplier()), y + 12, -mAnim);
+        SpriteUtilsKt.draw(spriteBatch, body, x, y);
+        SpriteUtilsKt.draw(spriteBatch, frontLeg, x + (9 - dirMultiplier() * 9), y + 12, mAnim);
+        SpriteUtilsKt.draw(spriteBatch, frontLeg, x + 21 - (9 * dirMultiplier()), y + 12, mAnim);
     }
-
 }