From: fredboy Date: Sun, 21 Apr 2024 14:03:09 +0000 (+0700) Subject: Move block damage and cursor to player class X-Git-Tag: alpha0.6.0~5 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=3512986410bc04017c9c15d3bebac5fe5cdd0e84;p=cavedroid.git Move block damage and cursor to player class --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index e5b0838..e1257d5 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -52,11 +52,8 @@ public class GameInput { private float mTouchDownX; private float mTouchDownY; private long mTouchDownTime; - - private int mCurX; - private int mCurY; + private int mCreativeScroll; - private int mBlockDamage; @Inject public GameInput(MainConfig mainConfig, @@ -134,19 +131,19 @@ public class GameInput { } else { switch (keycode) { case Input.Keys.A: - mCurX--; + mPlayer.cursorX--; break; case Input.Keys.D: - mCurX++; + mPlayer.cursorX++; break; case Input.Keys.W: - mCurY--; + mPlayer.cursorY--; break; case Input.Keys.S: - mCurY++; + mPlayer.cursorY++; break; } - mBlockDamage = 0; + mPlayer.blockDamage = 0; } } @@ -155,14 +152,14 @@ public class GameInput { } private void checkCursorBounds() { - if (mCurY < 0) { - mCurY = 0; - } else if (mCurY >= mGameWorld.getHeight()) { - mCurY = mGameWorld.getHeight() - 1; + if (mPlayer.cursorY < 0) { + mPlayer.cursorY = 0; + } else if (mPlayer.cursorY >= mGameWorld.getHeight()) { + mPlayer.cursorY = mGameWorld.getHeight() - 1; } if (mControlMode == ControlMode.CURSOR) { - if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) { + if (mPlayer.cursorX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) { mPlayer.setDir(Mob.Direction.LEFT); } else { mPlayer.setDir(Mob.Direction.RIGHT); @@ -171,29 +168,29 @@ public class GameInput { } public void moveCursor(GameRenderer gameRenderer) { - int pastX = mCurX; - int pastY = mCurY; + int pastX = mPlayer.cursorX; + int pastY = mPlayer.cursorY; if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) { - mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1); - mCurY = mPlayer.getUpperMapY(); - for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) { - mCurY++; + mPlayer.cursorX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1); + mPlayer.cursorY = mPlayer.getUpperMapY(); + for (int i = 0; i < 2 && isNotAutoselectable(mPlayer.cursorX, mPlayer.cursorY); i++) { + mPlayer.cursorY++; } - if (isNotAutoselectable(mCurX, mCurY)) { - mCurX += mPlayer.looksLeft() ? 1 : -1; + if (isNotAutoselectable(mPlayer.cursorX, mPlayer.cursorY)) { + mPlayer.cursorX += mPlayer.looksLeft() ? 1 : -1; } } else if (!mMainConfig.isTouch()) { final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() / Gdx.graphics.getWidth()) + gameRenderer.getCamX()); - mCurX = tmpX / 16; + mPlayer.cursorX = tmpX / 16; final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() / Gdx.graphics.getHeight()) + gameRenderer.getCamY()); - mCurY = tmpY / 16; + mPlayer.cursorY = tmpY / 16; if (tmpX < 0) { - mCurX--; + mPlayer.cursorX--; } final double a = tmpX - mPlayer.x; @@ -202,8 +199,8 @@ public class GameInput { mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg; } - if (pastX != mCurX || pastY != mCurY) { - mBlockDamage = 0; + if (pastX != mPlayer.cursorX || pastY != mPlayer.cursorY) { + mPlayer.blockDamage = 0; } checkCursorBounds(); @@ -248,27 +245,26 @@ public class GameInput { if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) { mPlayer.startHitting(); - if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMap(mCurX, mCurY).getHp() >= 0) || - (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) && - mGameWorld.getBackMap(mCurX, mCurY).getHp() >= 0)) { + if ((mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY) && mGameWorld.getForeMap(mPlayer.cursorX, mPlayer.cursorY).getHp() >= 0) || + (!mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY) && mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY) && + mGameWorld.getBackMap(mPlayer.cursorX, mPlayer.cursorY).getHp() >= 0)) { if (mPlayer.gameMode == 0) { - mBlockDamage++; - if (mGameWorld.hasForeAt(mCurX, mCurY)) { - if (mBlockDamage >= mGameWorld.getForeMap(mCurX, mCurY).getHp()) { - mGameWorld.destroyForeMap(mCurX, mCurY); - mBlockDamage = 0; + if (mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY)) { + if (mPlayer.blockDamage >= mGameWorld.getForeMap(mPlayer.cursorX, mPlayer.cursorY).getHp()) { + mGameWorld.destroyForeMap(mPlayer.cursorX, mPlayer.cursorY); + mPlayer.blockDamage = 0; } - } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { - if (mBlockDamage >= mGameWorld.getBackMap(mCurX, mCurY).getHp()) { - mGameWorld.destroyBackMap(mCurX, mCurY); - mBlockDamage = 0; + } else if (mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY)) { + if (mPlayer.blockDamage >= mGameWorld.getBackMap(mPlayer.cursorX, mPlayer.cursorY).getHp()) { + mGameWorld.destroyBackMap(mPlayer.cursorX, mPlayer.cursorY); + mPlayer.blockDamage = 0; } } } else { - if (mGameWorld.hasForeAt(mCurX, mCurY)) { - mGameWorld.placeToForeground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock()); - } else if (mGameWorld.hasBackAt(mCurX, mCurY)) { - mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock()); + if (mGameWorld.hasForeAt(mPlayer.cursorX, mPlayer.cursorY)) { + mGameWorld.placeToForeground(mPlayer.cursorX, mPlayer.cursorY, mGameItemsHolder.getFallbackBlock()); + } else if (mGameWorld.hasBackAt(mPlayer.cursorX, mPlayer.cursorY)) { + mGameWorld.placeToBackground(mPlayer.cursorX, mPlayer.cursorY, mGameItemsHolder.getFallbackBlock()); } mTouchedDown = false; } @@ -288,7 +284,7 @@ public class GameInput { private void holdMB() { if (mTouchDownBtn == Input.Buttons.RIGHT) { - useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot].getItem(), true); + useItem(mPlayer.cursorX, mPlayer.cursorY, mPlayer.inventory[mPlayer.slot].getItem(), true); mTouchedDown = false; } else { if (insideHotbar(mTouchDownX, mTouchDownY)) { @@ -333,7 +329,7 @@ public class GameInput { break; case Input.Keys.G: - final Mob pig = new Pig(mCurX * 16, mCurY * 16); + final Mob pig = new Pig(mPlayer.cursorX * 16, mPlayer.cursorY * 16); pig.attachToController(mMobsController); break; @@ -419,10 +415,11 @@ public class GameInput { screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) { mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20); } else if (button == Input.Buttons.RIGHT) { - useItem(mCurX, mCurY, + useItem(mPlayer.cursorX, mPlayer.cursorY, mPlayer.inventory[mPlayer.slot].getItem(), false); } else if (button == Input.Buttons.LEFT) { - mBlockDamage = 0; + mPlayer.stopHitting(); + mPlayer.blockDamage = 0; } } mTouchedDown = false; @@ -484,18 +481,6 @@ public class GameInput { return mKeyDown; } - public int getBlockDamage() { - return mBlockDamage; - } - - public int getCurX() { - return mCurX; - } - - public int getCurY() { - return mCurY; - } - public int getCreativeScroll() { return mCreativeScroll; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 9edffe4..a0c8c37 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -335,6 +335,7 @@ public class GamePhysics { } playerPhy(player, delta); + player.ai(mGameWorld, mGameItemsHolder, delta); if (player.isDead()) { player.respawn(mGameWorld, mGameItemsHolder); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt b/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt index 678a91f..3a0ef06 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/debug/DebugInfoStringsProvider.kt @@ -1,7 +1,6 @@ package ru.deadsoftware.cavedroid.game.debug import com.badlogic.gdx.Gdx -import ru.deadsoftware.cavedroid.game.GameInput import ru.deadsoftware.cavedroid.game.GameScope import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.objects.DropController @@ -11,7 +10,6 @@ import javax.inject.Inject @GameScope class DebugInfoStringsProvider @Inject constructor( private val mobsController: MobsController, - private val gameInput: GameInput, private val dropController: DropController, private val gameWorld: GameWorld ) { @@ -23,15 +21,16 @@ class DebugInfoStringsProvider @Inject constructor( "FPS: ${Gdx.graphics.framesPerSecond}", "X: ${player.mapX}", "Y: ${player.upperMapY}", - "CurX: ${gameInput.curX}", - "CurY: ${gameInput.curY}", + "CurX: ${player.cursorX}", + "CurY: ${player.cursorY}", "Velocity: ${player.velocity}", "Swim: ${player.swim}", "Mobs: ${mobsController.mobs.size}", "Drops: ${dropController.size}", - "Block: ${gameWorld.getForeMap(gameInput.curX, gameInput.curY).params.key}", + "Block: ${gameWorld.getForeMap(player.cursorX, player.cursorY).params.key}", "Hand: ${player.inventory[player.slot].item.params.key}", - "Game mode: ${player.gameMode}" + "Game mode: ${player.gameMode}", + "Block damage: ${player.blockDamage}" ) } } \ 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 9972617..216db6a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -5,6 +5,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import ru.deadsoftware.cavedroid.game.GameItemsHolder; +import ru.deadsoftware.cavedroid.game.model.block.Block; import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; import ru.deadsoftware.cavedroid.game.model.item.Item; import ru.deadsoftware.cavedroid.game.objects.Drop; @@ -31,6 +32,10 @@ public class Player extends Mob { public boolean swim; public float headRotation = 0f; + public float blockDamage = 0f; + public int cursorX = 0; + public int cursorY = 0; + public Player(GameItemsHolder gameItemsHolder) { super(0, 0, 4, 30, randomDir(), Type.MOB, MAX_HEALTH); inventory = new InventoryItem[9]; @@ -107,6 +112,21 @@ public class Player extends Mob { @Override public void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, float delta) { + if (gameMode == 1) { + return; + } + + final Block foregroundBlock = gameWorld.getForeMap(cursorX, cursorY); + final Block backgroundBlock = gameWorld.getBackMap(cursorX, cursorY); + + final boolean canHitBlock = !foregroundBlock.isNone() && foregroundBlock.getParams().getHitPoints() >= 0 + || !backgroundBlock.isNone() && backgroundBlock.getParams().getHitPoints() >= 0; + + if (hitting && canHitBlock) { + blockDamage += 60f * delta; + } else { + blockDamage = 0f; + } } @Override @@ -181,6 +201,7 @@ public class Player extends Mob { } public void stopHitting() { + blockDamage = 0f; hitting = false; } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt index 515ad79..aae4f98 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/BackgroundBlocksRenderer.kt @@ -5,8 +5,8 @@ import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.Rectangle -import ru.deadsoftware.cavedroid.game.GameInput import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import javax.inject.Inject @@ -14,8 +14,8 @@ import javax.inject.Inject @GameScope class BackgroundBlocksRenderer @Inject constructor( gameWorld: GameWorld, - gameInput: GameInput -) : BlocksRenderer(gameWorld, gameInput) { + mobsController: MobsController +) : BlocksRenderer(gameWorld, mobsController) { override val renderLayer get() = RENDER_LAYER diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt index 65d9e0a..ae243a3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/BlocksRenderer.kt @@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.MathUtils import com.badlogic.gdx.math.Rectangle import ru.deadsoftware.cavedroid.game.GameInput +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.model.block.Block import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.Assets @@ -13,7 +14,7 @@ import ru.deadsoftware.cavedroid.misc.utils.px abstract class BlocksRenderer( protected val gameWorld: GameWorld, - protected val gameInput: GameInput, + protected val mobsController: MobsController, ) : IGameRenderer { protected abstract val background: Boolean @@ -30,23 +31,23 @@ abstract class BlocksRenderer( } protected fun drawBlockDamage(spriteBatch: SpriteBatch, viewport: Rectangle) { - val blockDamage = gameInput.blockDamage - if (blockDamage <= 0) { - return - } + val player = mobsController.player + val blockDamage = player.blockDamage.takeIf { it > 0f } ?: return + val cursorX = player.cursorX + val cursorY = player.cursorY val block = if (background) { - gameWorld.getBackMap(gameInput.curX, gameInput.curY) + gameWorld.getBackMap(cursorX, cursorY) } else { - gameWorld.getForeMap(gameInput.curX, gameInput.curY) + gameWorld.getForeMap(cursorX, cursorY) } val index = (MAX_BLOCK_DAMAGE_INDEX.toFloat() * (blockDamage.toFloat() / block.params.hitPoints.toFloat())) .let(MathUtils::floor) val texture = blockDamageTexture(index) ?: return - if (gameWorld.hasForeAt(gameInput.curX, gameInput.curY) != background) { - spriteBatch.draw(texture, gameInput.curX.px - viewport.x, gameInput.curY.px - viewport.y) + if (gameWorld.hasForeAt(cursorX, cursorY) != background) { + spriteBatch.draw(texture, cursorX.px - viewport.x, cursorY.px - viewport.y) } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt index 3073695..ef8be71 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/ForegroundBlocksRenderer.kt @@ -3,8 +3,8 @@ package ru.deadsoftware.cavedroid.game.render import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.math.Rectangle -import ru.deadsoftware.cavedroid.game.GameInput import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.utils.forEachBlockInArea import javax.inject.Inject @@ -12,8 +12,8 @@ import javax.inject.Inject @GameScope class ForegroundBlocksRenderer @Inject constructor( gameWorld: GameWorld, - gameInput: GameInput -) : BlocksRenderer(gameWorld, gameInput) { + mobsController: MobsController +) : BlocksRenderer(gameWorld, mobsController) { override val renderLayer get() = RENDER_LAYER diff --git a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt index 911df5a..cea623a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/render/HudRenderer.kt @@ -29,11 +29,14 @@ class HudRenderer @Inject constructor( private val halfHeartTexture get() = requireNotNull(Assets.textureRegions[HALF_HEART_KEY]) private fun drawCursor(spriteBatch: SpriteBatch, viewport: Rectangle) { - if (gameWorld.hasForeAt(gameInput.curX, gameInput.curY) || - gameWorld.hasBackAt(gameInput.curX, gameInput.curY) || + val cursorX = mobsController.player.cursorX + val cursorY = mobsController.player.cursorY + + if (gameWorld.hasForeAt(cursorX, cursorY) || + gameWorld.hasBackAt(cursorX, cursorY) || gameInput.controlMode == ControlMode.CURSOR ) { - spriteBatch.draw(cursorTexture, gameInput.curX.px - viewport.x, gameInput.curY.px - viewport.y) + spriteBatch.draw(cursorTexture, cursorX.px - viewport.x, cursorY.px - viewport.y) } }