From: fredboy Date: Thu, 18 Apr 2024 05:15:14 +0000 (+0700) Subject: Some mobs refactor X-Git-Tag: alpha0.5.2~14 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=462f97f8da742fe35f516fec00ca9a581d688e7a;p=cavedroid.git Some mobs refactor --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 29c8188..2bf6e7d 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -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 { diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index 56a2b58..5ae4f19 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -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; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index cd900d8..70afcbf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -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); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java index 495b628..ede6ba2 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java @@ -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()) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java index 07cd8cd..c59c3fc 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java @@ -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()) { diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index b7a975c..f19d247 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -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 index 2290387..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java +++ /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 index 0000000..a929546 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 93150b6..b45b942 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -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); } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt index 0b93809..215ac26 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt index 9eea976..cb0f7b7 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt @@ -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 diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt index 830dc9a..d465ce1 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteOrigin.kt @@ -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) + } + } diff --git a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt index 72165e7..7d7aae1 100644 --- a/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/SpriteUtils.kt @@ -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 index 0000000..3b5eb4c --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/misc/utils/mobs/MobSprites.kt @@ -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