From: fredboy Date: Fri, 19 Apr 2024 17:43:29 +0000 (+0700) Subject: Mobs controller to kotlin X-Git-Tag: alpha0.5.2~6 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=5646018e364d9a708e985f04ab510bf88692082b;p=cavedroid.git Mobs controller to kotlin --- diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index a0c5f66..015d81a 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -218,7 +218,7 @@ public class GameInput { private void hitMobs() { final Player player = mMobsController.getPlayer(); - mMobsController.forEach((mob) -> { + mMobsController.getMobs().forEach((mob) -> { if (Intersector.overlaps(mob, player)) { mob.damage(5); mob.jump(); @@ -315,7 +315,7 @@ public class GameInput { break; case Input.Keys.G: - mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16); + mMobsController.addMob(new Pig(mCurX * 16, mCurY * 16)); break; case Input.Keys.Q: diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index 4127b6d..1ff603c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -318,7 +318,7 @@ public class GamePhysics { } } - for (Iterator it = mMobsController.getIterator(); it.hasNext(); ) { + for (Iterator it = mMobsController.getMobs().iterator(); it.hasNext(); ) { Mob mob = it.next(); mob.ai(mGameWorld, delta); mobPhy(mob, delta); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java index 09878f5..20401ac 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java @@ -305,7 +305,7 @@ public class GameRenderer extends Renderer { drawWorld(true); player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY(), delta); - mMobsController.forEach( (mob) -> { drawMob(mob, delta); }); + mMobsController.getMobs().forEach( (mob) -> { drawMob(mob, delta); }); mDropController.forEach(this::drawDrop); drawWorld(false); drawGUI(); @@ -408,7 +408,7 @@ public class GameRenderer extends Renderer { drawString("CurY: " + mGameInput.getCurY(), 0, 40); drawString("Velocity: " + player.getVelocity(), 0, 50); drawString("Swim: " + player.swim, 0, 60); - drawString("Mobs: " + mMobsController.getSize(), 0, 70); + drawString("Mobs: " + mMobsController.getMobs().size(), 0, 70); drawString("Drops: " + mDropController.getSize(), 0, 80); drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 90); drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 100); diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java deleted file mode 100644 index cda54ee..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.deadsoftware.cavedroid.game.mobs; - -import com.badlogic.gdx.Gdx; -import ru.deadsoftware.cavedroid.game.GameScope; - -import javax.inject.Inject; -import java.io.Serializable; -import java.util.Iterator; -import java.util.LinkedList; - -@GameScope -public class MobsController implements Serializable { - - public interface Callback { - void run(Mob mob); - } - - private static final String TAG = "MobsController"; - - private final Player mPlayer; - private final LinkedList mMobs = new LinkedList<>(); - - @Inject - public MobsController() { - mPlayer = new Player(); - } - - public Player getPlayer() { - return mPlayer; - } - - public void addMob(Class mobClass, float x, float y) { - try { - mMobs.add(mobClass.getConstructor(float.class, float.class).newInstance(x, y)); - } catch (Exception e) { - Gdx.app.error(TAG, e.getMessage()); - } - } - - public int getSize() { - return mMobs.size(); - } - - public void forEach(Callback callback) { - mMobs.forEach(callback::run); - } - - public Iterator getIterator() { - return mMobs.iterator(); - } - -} diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt new file mode 100644 index 0000000..e839088 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.kt @@ -0,0 +1,25 @@ +package ru.deadsoftware.cavedroid.game.mobs + +import ru.deadsoftware.cavedroid.game.GameScope +import java.io.Serializable +import java.util.* +import javax.inject.Inject + +@GameScope +class MobsController @Inject constructor() : Serializable { + + private val _mobs = LinkedList() + + val player: Player = Player() + + val mobs: List + get() = _mobs + + fun addMob(mob: Mob) { + _mobs.add(mob) + } + + companion object { + private const val TAG = "MobsController" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index 31d842b..692eabc 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -213,7 +213,7 @@ public class GameWorld implements Disposable { if (getForeMap(x, y) == 10) { if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { setForeMap(x, y, 0); - mMobsController.addMob(FallingSand.class, x * 16, y * 16); + mMobsController.addMob(new FallingSand(x * 16, y * 16)); updateBlock(x, y - 1); } } @@ -221,7 +221,7 @@ public class GameWorld implements Disposable { if (getForeMap(x, y) == 11) { if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { setForeMap(x, y, 0); - mMobsController.addMob(FallingGravel.class, x * 16, y * 16); + mMobsController.addMob(new FallingGravel(x * 16, y * 16)); updateBlock(x, y - 1); } }