DEADSOFTWARE

Some mobs refactor
authorfredboy <fredboy@protonmail.com>
Thu, 18 Apr 2024 05:15:14 +0000 (12:15 +0700)
committerfredboy <fredboy@protonmail.com>
Thu, 18 Apr 2024 14:16:48 +0000 (21:16 +0700)
14 files changed:
core/src/ru/deadsoftware/cavedroid/game/GameInput.java
core/src/ru/deadsoftware/cavedroid/game/GameItems.java
core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java
core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java [deleted file]
core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt
core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt
core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt
core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt
core/src/ru/deadsoftware/cavedroid/misc/utils/mobs/MobSprites.kt [new file with mode: 0644]

index 29c8188d9d6c44f07946a8f464913a01fab1e0ea..2bf6e7d096ab17912d6794841661a212841c1988 100644 (file)
@@ -69,12 +69,12 @@ public class GameInput {
         if (checkSwim()) {
             mPlayer.swim = true;
         } else if (mPlayer.canJump()) {
-            mPlayer.getVelocity().add(0, GamePhysics.PL_JUMP_VELOCITY);
+            mPlayer.jump();
         } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
             mPlayer.setFlyMode(true);
             mPlayer.getVelocity().y = 0;
         } else if (mPlayer.isFlyMode()) {
-            mPlayer.getVelocity().y = -GamePhysics.PL_SPEED;
+            mPlayer.getVelocity().y = -mPlayer.getSpeed();
         }
     }
 
@@ -91,14 +91,14 @@ public class GameInput {
         if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
             switch (keycode) {
                 case Input.Keys.A:
-                    mPlayer.getVelocity().x = -GamePhysics.PL_SPEED;
+                    mPlayer.getVelocity().x = -mPlayer.getSpeed();
                     mPlayer.setDir(Mob.Direction.LEFT);
                     if (mMainConfig.isTouch() && checkSwim()) {
                         mPlayer.swim = true;
                     }
                     break;
                 case Input.Keys.D:
-                    mPlayer.getVelocity().x = GamePhysics.PL_SPEED;
+                    mPlayer.getVelocity().x = mPlayer.getSpeed();
                     mPlayer.setDir(Mob.Direction.RIGHT);
                     if (mMainConfig.isTouch() && checkSwim()) {
                         mPlayer.swim = true;
@@ -110,7 +110,7 @@ public class GameInput {
                     break;
                 case Input.Keys.S:
                 case Input.Keys.CONTROL_LEFT:
-                    mPlayer.getVelocity().y = GamePhysics.PL_SPEED;
+                    mPlayer.getVelocity().y = mPlayer.getSpeed();
                     break;
             }
         } else {
index 56a2b5860eb6df91ef1691207e37e80d631491d7..5ae4f19a92e70b96ede2cfcecf01c40fac27d8e5 100644 (file)
@@ -91,6 +91,10 @@ public class GameItems {
         return getBlockId(items.getKeyAt(id));
     }
 
+    public static int getItemIdByBlockId(int id) {
+        return getItemId(blocks.getKeyAt(id));
+    }
+
     public static int getBlocksSize() {
         return blocks.size;
     }
index cd900d873dc1398e6b41ea492be77a87feaca25a..70afcbfe5d3f1be77cdd772d61584b142022f7bd 100644 (file)
@@ -20,7 +20,6 @@ import java.util.Iterator;
 @GameScope
 public class GamePhysics {
 
-    public static final float PL_SPEED = 69.072f;
     public static final float PL_JUMP_VELOCITY = -133.332f;
     public static final float PL_TERMINAL_VELOCITY = 1254.4f;
 
@@ -256,8 +255,8 @@ public class GamePhysics {
                 }
             } else {
                 player.getVelocity().y += PL_JUMP_VELOCITY * delta;
-                if (player.getVelocity().y < -PL_SPEED) {
-                    player.getVelocity().y = -PL_SPEED;
+                if (player.getVelocity().y < -player.getSpeed()) {
+                    player.getVelocity().y = -player.getSpeed();
                 }
             }
         } else {
@@ -275,7 +274,7 @@ public class GamePhysics {
         mobXColl(player);
 
         if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) {
-            player.getVelocity().y = PL_JUMP_VELOCITY;
+            player.jump();
             player.setCanJump(false);
         }
     }
@@ -288,8 +287,8 @@ public class GamePhysics {
 
             mob.getVelocity().y += PL_JUMP_VELOCITY * delta;
 
-            if (mob.getVelocity().y < -PL_SPEED) {
-                mob.getVelocity().y = -PL_SPEED;
+            if (mob.getVelocity().y < -mob.getSpeed()) {
+                mob.getVelocity().y = -mob.getSpeed();
             }
         } else if (!mob.isFlyMode() && mob.getVelocity().y < PL_TERMINAL_VELOCITY) {
             mob.getVelocity().y += gravity.y * delta;
@@ -306,7 +305,7 @@ public class GamePhysics {
         mobXColl(mob);
 
         if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) {
-            mob.getVelocity().y = PL_JUMP_VELOCITY;
+            mob.jump();
             mob.setCanJump(false);
         }
     }
index 495b628416c0cecdf3fac155862e7485a38200ed..ede6ba2888490622cc11be47239e4d571d379444 100644 (file)
@@ -22,6 +22,16 @@ public class FallingGravel extends Mob {
         mVelocity = new Vector2(0, 1);
     }
 
+    @Override
+    public float getSpeed() {
+        return 0;
+    }
+
+    @Override
+    public void jump() {
+        // no-op
+    }
+
     @Override
     public void ai(GameWorld gameWorld, float delta) {
         if (mVelocity.isZero()) {
index 07cd8cdd84e3ddf8249be1964681b2a422c09b63..c59c3fc94d4de16a1bf94eb2c03d4295ace04bae 100644 (file)
@@ -23,6 +23,16 @@ public class FallingSand extends Mob {
         mVelocity = new Vector2(0, 1);
     }
 
+    @Override
+    public float getSpeed() {
+        return 0;
+    }
+
+    @Override
+    public void jump() {
+        // no-op
+    }
+
     @Override
     public void ai(GameWorld gameWorld, float delta) {
         if (mVelocity.isZero()) {
index b7a975caf3b4217032573afdfc200760515f78f3..f19d247c4d9df9a972eb4d423cedbd3ea8c35ac1 100644 (file)
@@ -22,8 +22,31 @@ public abstract class Mob extends Rectangle implements Serializable {
     }
 
     public enum Direction {
-        LEFT,
-        RIGHT
+
+        LEFT(0, -1),
+        RIGHT(1, 1);
+
+        private final int index;
+        private final int basis;
+
+        /**
+         * Index for this direction (left = 0, right = 1)
+         */
+        public final int getIndex() {
+            return index;
+        }
+
+        /**
+         * Basis for this direction (left = -1, right = 1)
+         */
+        public final int getBasis() {
+            return basis;
+        }
+
+        Direction(int index, int basis) {
+            this.index = index;
+            this.basis = basis;
+        }
     }
 
     protected Vector2 mVelocity;
@@ -138,10 +161,6 @@ public abstract class Mob extends Rectangle implements Serializable {
         mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
     }
 
-    protected final int dirMultiplier() {
-        return looksLeft() ? 0 : 1;
-    }
-
     public final boolean isDead() {
         return mDead;
     }
@@ -166,6 +185,10 @@ public abstract class Mob extends Rectangle implements Serializable {
         return mVelocity;
     }
 
+    protected final void setVelocity(Vector2 velocity) {
+        mVelocity = velocity;
+    }
+
     public final boolean canJump() {
         return mCanJump;
     }
@@ -186,7 +209,7 @@ public abstract class Mob extends Rectangle implements Serializable {
         return mType;
     }
 
-    public void checkWorldBounds(GameWorld gameWorld) {
+    public final void checkWorldBounds(GameWorld gameWorld) {
         if (x + width / 2 < 0) {
             x += gameWorld.getWidthPx();
         }
@@ -200,4 +223,8 @@ public abstract class Mob extends Rectangle implements Serializable {
     public abstract void ai(GameWorld gameWorld, float delta);
 
     public abstract void changeDir();
+
+    public abstract float getSpeed();
+
+    public abstract void jump();
 }
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java
deleted file mode 100644 (file)
index 2290387..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-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, randomDir(), Type.MOB);
-        mVelocity = new Vector2((looksLeft() ? -1 : 1) * GamePhysics.PL_SPEED, 0);
-    }
-
-    @Override
-    public void changeDir() {
-        switchDir();
-        mVelocity.x = (-1 + 2 * dirMultiplier()) * GamePhysics.PL_SPEED;
-    }
-
-    @Override
-    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, 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);
-    }
-}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt
new file mode 100644 (file)
index 0000000..a929546
--- /dev/null
@@ -0,0 +1,61 @@
+package ru.deadsoftware.cavedroid.game.mobs
+
+import com.badlogic.gdx.graphics.g2d.SpriteBatch
+import com.badlogic.gdx.math.MathUtils
+import com.badlogic.gdx.math.Vector2
+import ru.deadsoftware.cavedroid.game.world.GameWorld
+import ru.deadsoftware.cavedroid.misc.utils.drawSprite
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBackgroundLeg
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getBody
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getForegroundLeg
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLeftLegRelativeX
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getLegsRelativeY
+import ru.deadsoftware.cavedroid.misc.utils.mobs.MobSprites.Pig.getRightLegRelativeX
+
+class Pig(x: Float, y: Float) : Mob(x, y, WIDTH, HEIGHT, randomDir(), Type.MOB) {
+
+    override fun getSpeed(): Float {
+        return SPEED
+    }
+    
+    override fun changeDir() {
+        switchDir()
+        velocity = Vector2(direction.basis * speed, 0f)
+    }
+
+    override fun jump() {
+        velocity.y = JUMP_VELOCITY
+    }
+    
+    override fun ai(world: GameWorld, delta: Float) {
+        if (MathUtils.randomBoolean(delta)) {
+            if (velocity.x != 0f) {
+                velocity.x = 0f
+            } else {
+                changeDir()
+            }
+        }
+    }
+
+    override fun draw(spriteBatch: SpriteBatch, x: Float, y: Float, delta: Float) {
+        updateAnimation(delta)
+
+        val leftLegX = x + getLeftLegRelativeX(direction)
+        val rightLegX = x + getRightLegRelativeX(direction)
+        val legY = y + getLegsRelativeY()
+
+        spriteBatch.drawSprite(getBackgroundLeg(), leftLegX, legY, -anim)
+        spriteBatch.drawSprite(getBackgroundLeg(), rightLegX, legY, -anim)
+        spriteBatch.drawSprite(getBody(direction), x, y)
+        spriteBatch.drawSprite(getForegroundLeg(), leftLegX, legY, anim)
+        spriteBatch.drawSprite(getForegroundLeg(), rightLegX, legY, anim)
+    }
+    
+    
+    private companion object {
+        private const val WIDTH = 25f
+        private const val HEIGHT = 18f
+        private const val SPEED =  69.072f
+        private const val JUMP_VELOCITY = -133.332f
+    }
+}
\ No newline at end of file
index 93150b6a5d3f4870187251071d4dfabf2bdeff2c..b45b942697356a3b23450a355e8b5b5178dc9b09 100644 (file)
@@ -16,6 +16,9 @@ import javax.annotation.CheckForNull;
 
 public class Player extends Mob {
 
+    private static final float SPEED = 69.072f;
+    private static final float JUMP_VELOCITY = -133.332f;
+
     public final int[] inventory;
     public int slot;
     public final int gameMode;
@@ -68,6 +71,16 @@ public class Player extends Mob {
         }
     }
 
+    @Override
+    public float getSpeed() {
+        return SPEED;
+    }
+
+    @Override
+    public void jump() {
+        mVelocity.y = JUMP_VELOCITY;
+    }
+
     @Override
     public void ai(GameWorld gameWorld, float delta) {
     }
@@ -95,8 +108,8 @@ public class Player extends Mob {
         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 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 = mAnim + 30;
@@ -112,7 +125,7 @@ public class Player extends Mob {
             SpriteUtilsKt.applyOrigin(sprite, spriteOrigin);
         }
 
-        SpriteUtilsKt.draw(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate);
+        SpriteUtilsKt.drawSprite(spriteBatch, sprite, itemX, itemY, -handMultiplier * rotate);
 
         // dont forget to reset
         sprite.setFlip(false, sprite.isFlipY());
@@ -127,26 +140,26 @@ 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.drawSprite(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);
+        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);
         }
 
-        SpriteUtilsKt.draw(spriteBatch, frontHand, x + 2, y + 8, mAnim);
+        SpriteUtilsKt.drawSprite(spriteBatch, frontHand, x + 2, y + 8, mAnim);
     }
 
 }
index 0b93809af232d0dce08cdf90fad13cd7cf23f090..215ac26c7a766a8ec0b9111c690343c215f5b0c1 100644 (file)
@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Texture
 import com.badlogic.gdx.graphics.g2d.Sprite
 import com.badlogic.gdx.graphics.g2d.SpriteBatch
 import com.badlogic.gdx.math.Rectangle
+import ru.deadsoftware.cavedroid.game.GameItems
 
 private const val ANIMATION_FRAME_DURATION = 100L
 private const val DEPRECATION_MESSAGE =
@@ -124,6 +125,8 @@ data class Block(
 
     fun toJump() = top < 8 && collision
 
+    fun getItem() = GameItems.getItem(GameItems.getBlockKey(id))
+
     @Deprecated(DEPRECATION_MESSAGE)
     fun hasCollision() = collision
 
index 9eea9767e3967ed2e9783e65d20c84207bcecd24..cb0f7b7269c9526ca5649033371bbf6b485914c3 100644 (file)
@@ -33,6 +33,14 @@ data class Item(
         return GameItems.getBlock(GameItems.getBlockIdByItemId(id))
     }
 
+    fun getItemOrBlockSprite(): Sprite {
+        return requireNotNull(sprite ?: toBlock()?.requireSprite()) { "wtf: sprite is null" }
+    }
+
+    fun isNone(): Boolean {
+        return id == 0;
+    }
+
     @Deprecated("Was renamed to Sprite to comply with variable type.", ReplaceWith("requireSprite()"))
     fun getTexture() = sprite
 }
\ No newline at end of file
index 830dc9a9888b99e3f8870c18bad4beefe010bab9..d465ce1ec1841b306f4d12acf41210ab437e8159 100644 (file)
@@ -1,5 +1,7 @@
 package ru.deadsoftware.cavedroid.misc.utils
 
+import com.badlogic.gdx.graphics.g2d.Sprite
+
 /**
  * An origin of a [com.badlogic.gdx.graphics.g2d.Sprite]
  *
@@ -22,4 +24,8 @@ data class SpriteOrigin(
         )
     }
 
+    fun applyToSprite(sprite: Sprite) {
+        sprite.setOrigin(sprite.width * x, sprite.height * y)
+    }
+
 }
index 72165e7cb196e4bf03dcb39fe8c7efb548aef05f..7d7aae110b22aa7a02b2c571fd1481906012cfd3 100644 (file)
@@ -7,12 +7,12 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
  * Draw sprite at given position rotated by [rotation] degrees
  */
 @JvmOverloads
-fun SpriteBatch.draw(sprite: Sprite, x: Float, y: Float, rotation: Float = 0f) {
+fun SpriteBatch.drawSprite(sprite: Sprite, x: Float, y: Float, rotation: Float = 0f) {
     sprite.rotation = rotation
     sprite.setPosition(x, y)
     sprite.draw(this)
 }
 
 fun Sprite.applyOrigin(origin: SpriteOrigin) {
-    setOrigin(width * origin.x, height * origin.y)
+    origin.applyToSprite(this)
 }
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/mobs/MobSprites.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/mobs/MobSprites.kt
new file mode 100644 (file)
index 0000000..3b5eb4c
--- /dev/null
@@ -0,0 +1,47 @@
+package ru.deadsoftware.cavedroid.misc.utils.mobs
+
+import ru.deadsoftware.cavedroid.game.mobs.Mob
+import ru.deadsoftware.cavedroid.game.mobs.Mob.Direction
+import ru.deadsoftware.cavedroid.misc.Assets
+
+object MobSprites {
+
+    object Player {
+
+        fun getBackgroundHand() = Assets.playerSprite[1][2]
+
+        fun getForegroundHand() = Assets.playerSprite[0][2]
+
+        fun getBackgroundLeg() = Assets.playerSprite[1][3]
+
+        fun getForegroundLeg() = Assets.playerSprite[0][3]
+
+        fun getHead(direction: Mob.Direction) = Assets.playerSprite[direction.index][0]
+
+        fun getBody(direction: Direction) = Assets.playerSprite[direction.index][1]
+
+        fun getBodyRelativeX() = 2
+
+        fun getBodyRelativeY() = 8
+
+        fun getLegsRelativeY() = 20
+
+    }
+
+    object Pig {
+
+        fun getForegroundLeg() = Assets.pigSprite[0][1]
+
+        fun getBackgroundLeg() = Assets.pigSprite[1][1]
+
+        fun getBody(direction: Direction) = Assets.pigSprite[direction.index][0]
+
+        fun getLeftLegRelativeX(direction: Direction) = 9 - direction.index * 9
+
+        fun getRightLegRelativeX(direction: Direction) = 21 - (9 * direction.index)
+
+        fun getLegsRelativeY() = 12
+
+    }
+
+}
\ No newline at end of file