summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: aa867d1)
raw | patch | inline | side by side (parent: aa867d1)
author | fred-boy <fredboy@protonmail.com> | |
Tue, 5 May 2020 18:57:21 +0000 (01:57 +0700) | ||
committer | fred-boy <fredboy@protonmail.com> | |
Wed, 6 May 2020 09:17:20 +0000 (16:17 +0700) |
35 files changed:
diff --git a/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java b/android/src/ru/deadsoftware/cavedroid/AndroidLauncher.java
index 51961b4c3854840a88090256e2415e232698744c..fcf083199e0e667bb839c4e45a2c7fdfc22330a0 100644 (file)
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.hideStatusBar = true;
config.useImmersiveMode = true;
- String gameFolder = null;
+ String gameFolder = "";
try {
gameFolder = getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
exit();
}
- initialize(new CaveGame(gameFolder, true), config);
-
- if (BuildConfig.DEBUG) {
- GameScreen.SHOW_DEBUG = true;
- }
+ CaveGame caveGame = new CaveGame(gameFolder, true);
+ caveGame.setDebug(BuildConfig.DEBUG);
+ initialize(caveGame, config);
}
@Override
diff --git a/build.gradle b/build.gradle
index 7155a60b2d0d313cb626b3319fc8b5916786cc96..354fa1e04d05223b3909ec953fb7b86eac26c5d2 100644 (file)
--- a/build.gradle
+++ b/build.gradle
buildscript {
-
repositories {
mavenLocal()
mavenCentral()
jcenter()
google()
}
+
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
-
-
}
}
project(":desktop") {
apply plugin: "java-library"
-
dependencies {
implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
project(":core") {
apply plugin: "java-library"
-
dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.google.guava:guava:$guavaVersion-android"
+ api 'com.google.dagger:dagger:2.27'
+ implementation 'org.jetbrains:annotations:15.0'
+ annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
}
}
\ No newline at end of file
diff --git a/core/build.gradle b/core/build.gradle
index 1f36e4cc46c622146451680fea6f6b625ac67d0f..30b4faf6d4f239c7dd76c89e8d9d16dcf5bdae21 100644 (file)
--- a/core/build.gradle
+++ b/core/build.gradle
-apply plugin: "java"
+plugins {
+ id "java"
+ id "idea"
+ id "net.ltgt.apt" version "0.21"
+}
sourceCompatibility = 1.8
-dependencies {
- implementation 'org.jetbrains:annotations:15.0'
-}
-
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/CaveGame.java b/core/src/ru/deadsoftware/cavedroid/CaveGame.java
index 57704d98f94f56d186c6c2c845f0dfd2f699d8d6..f1fb3eed43b8be66c7f358e028561085358bdcdb 100644 (file)
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
-import ru.deadsoftware.cavedroid.misc.states.AppState;
-import ru.deadsoftware.cavedroid.misc.states.GameState;
-import ru.deadsoftware.cavedroid.misc.states.MenuState;
+import ru.deadsoftware.cavedroid.game.GameItems;
+import ru.deadsoftware.cavedroid.misc.Assets;
public class CaveGame extends Game {
+ private static final String TAG = "CaveGame";
+
public static final String VERSION = "alpha 0.4";
- public static AppState APP_STATE;
- public static GameState GAME_STATE;
- public static MenuState MENU_STATE;
+ private final String mGameFolder;
+ private final boolean mTouch;
+ private boolean mDebug;
- public static String GAME_FOLDER;
- public static boolean TOUCH;
+ public CaveGame(String gameFolder, boolean touch) {
+ mGameFolder = gameFolder;
+ mTouch = touch;
+ }
- public CaveGame(String gameFolder) {
- this(gameFolder, false);
+ public void setDebug(boolean debug) {
+ mDebug = debug;
}
- public CaveGame(String gameFolder, boolean touch) {
- GAME_FOLDER = gameFolder;
- TOUCH = touch;
- APP_STATE = AppState.MENU;
- MENU_STATE = MenuState.MAIN;
- GAME_STATE = GameState.PLAY;
+ private void initConfig(MainConfig mainConfig, MainComponent mainComponent) {
+ int width = mTouch ? 320 : 480;
+ int height = (int) (width * ((float) Gdx.graphics.getHeight() / Gdx.graphics.getWidth()));
+
+ mainConfig.setMainComponent(mainComponent);
+ mainConfig.setGameFolder(mGameFolder);
+ mainConfig.setTouch(mTouch);
+ mainConfig.setWidth(width);
+ mainConfig.setHeight(height);
+ mainConfig.setShowInfo(true);
}
@Override
public void create() {
- Gdx.app.log("CaveGame", GAME_FOLDER);
- Gdx.files.absolute(GAME_FOLDER).mkdirs();
- setScreen(new GameScreen());
+ Gdx.app.log(TAG, mGameFolder);
+ Gdx.files.absolute(mGameFolder).mkdirs();
+
+ Assets.load();
+ GameItems.load();
+
+ MainComponent mainComponent = DaggerMainComponent.create();
+ initConfig(mainComponent.getGameConfig(), mainComponent);
+ setScreen(mainComponent.getGameScreen());
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/GameScreen.java
+++ /dev/null
@@ -1,126 +0,0 @@
-package ru.deadsoftware.cavedroid;
-
-import com.badlogic.gdx.Gdx;
-import com.badlogic.gdx.Screen;
-import ru.deadsoftware.cavedroid.game.GameItems;
-import ru.deadsoftware.cavedroid.game.GameProc;
-import ru.deadsoftware.cavedroid.game.GameSaver;
-import ru.deadsoftware.cavedroid.menu.MenuProc;
-import ru.deadsoftware.cavedroid.misc.Assets;
-import ru.deadsoftware.cavedroid.misc.InputHandlerGame;
-import ru.deadsoftware.cavedroid.misc.Renderer;
-import ru.deadsoftware.cavedroid.misc.states.AppState;
-import ru.deadsoftware.cavedroid.misc.states.GameState;
-import ru.deadsoftware.cavedroid.misc.states.MenuState;
-
-import javax.annotation.Nullable;
-
-public class GameScreen implements Screen {
-
- public static GameProc GP;
-
- public static int FPS;
- public static boolean SHOW_DEBUG = false;
- public static boolean SHOW_MAP = false;
-
- private Renderer renderer;
- private MenuProc menuProc;
-
- @Nullable private InputHandlerGame inputHandlerGame;
-
- public GameScreen() {
- Assets.load();
- GameItems.load();
- menuProc = new MenuProc(CaveGame.TOUCH ? 320 : 480);
- renderer = menuProc;
- Gdx.input.setInputProcessor(menuProc);
- }
-
- public static float getWidth() {
- return Gdx.graphics.getWidth();
- }
-
- public static float getHeight() {
- return Gdx.graphics.getHeight();
- }
-
- private void game() {
- GP.update();
- }
-
- private void menu() {
- }
-
- @Override
- public void show() {
- }
-
- @Override
- public void render(float delta) {
- FPS = (int) (1 / delta);
- switch (CaveGame.APP_STATE) {
- case GAME:
- game();
- break;
-
- case MENU:
- menu();
- break;
-
- case LOAD:
- renderer = GP.resetRenderer();
- if (inputHandlerGame == null) {
- inputHandlerGame = new InputHandlerGame();
- }
- Gdx.input.setInputProcessor(inputHandlerGame);
- CaveGame.APP_STATE = AppState.GAME;
- CaveGame.GAME_STATE = GameState.PLAY;
- break;
-
- case SAVE:
- GameSaver.save(GP);
- CaveGame.APP_STATE = AppState.MENU;
- CaveGame.MENU_STATE = MenuState.MAIN;
- GP.dispose();
- renderer = menuProc;
- Gdx.input.setInputProcessor(menuProc);
- break;
- }
- renderer.render();
- }
-
- @Override
- public void resize(int width, int height) {
- switch (CaveGame.APP_STATE) {
- case MENU:
- menuProc = new MenuProc(CaveGame.TOUCH ? 320 : 480);
- Gdx.input.setInputProcessor(menuProc);
- renderer = menuProc;
- break;
- case GAME:
- renderer = GP.resetRenderer();
- break;
- }
- }
-
- @Override
- public void pause() {
-
- }
-
- @Override
- public void resume() {
-
- }
-
- @Override
- public void hide() {
-
- }
-
- @Override
- public void dispose() {
-
- }
-
-}
diff --git a/core/src/ru/deadsoftware/cavedroid/MainComponent.java b/core/src/ru/deadsoftware/cavedroid/MainComponent.java
--- /dev/null
@@ -0,0 +1,13 @@
+package ru.deadsoftware.cavedroid;
+
+import dagger.Component;
+import ru.deadsoftware.cavedroid.game.GameScreen;
+
+import javax.inject.Singleton;
+
+@Singleton
+@Component
+public interface MainComponent {
+ GameScreen getGameScreen();
+ MainConfig getGameConfig();
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/MainConfig.java b/core/src/ru/deadsoftware/cavedroid/MainConfig.java
--- /dev/null
@@ -0,0 +1,96 @@
+package ru.deadsoftware.cavedroid;
+
+import ru.deadsoftware.cavedroid.game.GameUiWindow;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+@Singleton
+public class MainConfig {
+
+ private MainComponent mMainComponent;
+
+ private GameUiWindow mGameUiWindow;
+ private String mGameFolder;
+
+ private boolean mTouch;
+ private boolean mShowInfo;
+ private boolean mShowMap;
+
+ private float mWidth;
+ private float mHeight;
+
+ @Inject
+ public MainConfig() {
+ mGameUiWindow = GameUiWindow.NONE;
+ mGameFolder = "";
+ }
+
+ public MainComponent getMainComponent() {
+ return mMainComponent;
+ }
+
+ public void setMainComponent(MainComponent mainComponent) {
+ mMainComponent = mainComponent;
+ }
+
+ public boolean checkGameUiWindow(GameUiWindow gameUiWindow) {
+ return mGameUiWindow == gameUiWindow;
+ }
+
+ public GameUiWindow getGameUiWindow() {
+ return mGameUiWindow;
+ }
+
+ public void setGameUiWindow(GameUiWindow gameUiWindow) {
+ mGameUiWindow = gameUiWindow;
+ }
+
+ public String getGameFolder() {
+ return mGameFolder;
+ }
+
+ public void setGameFolder(String gameFolder) {
+ mGameFolder = gameFolder;
+ }
+
+ public boolean isTouch() {
+ return mTouch;
+ }
+
+ public void setTouch(boolean touch) {
+ mTouch = touch;
+ }
+
+ public float getWidth() {
+ return mWidth;
+ }
+
+ public void setWidth(float width) {
+ mWidth = width;
+ }
+
+ public float getHeight() {
+ return mHeight;
+ }
+
+ public void setHeight(float height) {
+ mHeight = height;
+ }
+
+ public boolean isShowInfo() {
+ return mShowInfo;
+ }
+
+ public void setShowInfo(boolean showInfo) {
+ mShowInfo = showInfo;
+ }
+
+ public boolean isShowMap() {
+ return mShowMap;
+ }
+
+ public void setShowMap(boolean showMap) {
+ mShowMap = showMap;
+ }
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java
--- /dev/null
@@ -0,0 +1,11 @@
+package ru.deadsoftware.cavedroid.game;
+
+import dagger.Component;
+import ru.deadsoftware.cavedroid.MainComponent;
+
+@GameScope
+@Component(dependencies = MainComponent.class)
+public interface GameComponent {
+ GameProc getGameProc();
+ GameInputProcessor getGameInputProcessor();
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java b/core/src/ru/deadsoftware/cavedroid/game/GameFluidsThread.java
index d069a4058b171a259f57ab6e8a0de86266424c5b..20e95b8799bda3cdcfbcb3513db1d0a6e4921506 100644 (file)
package ru.deadsoftware.cavedroid.game;
import com.badlogic.gdx.utils.TimeUtils;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
import java.util.Arrays;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
import static ru.deadsoftware.cavedroid.game.GameItems.*;
class GameFluidsThread extends Thread {
private static final int[] WATER_IDS = {8, 60, 61, 62, 63};
private static final int[] LAVA_IDS = {9, 64, 65, 66, 67};
- private long fluidLastUpdateTimestamp = 0;
+ private long mFluidLastUpdateTimestamp = 0;
+
+ private final GameWorld mGameWorld;
+ private final MobsController mMobsController;
+
+ private final Thread mMainThread;
+
+ GameFluidsThread(GameWorld gameWorld,
+ MobsController mobsController,
+ Thread mainThread) {
+ mGameWorld = gameWorld;
+ mMobsController = mobsController;
+ mMainThread = mainThread;
+ }
private int getBlockState(int id) {
return isWater(id) ? Arrays.binarySearch(WATER_IDS, id) : Arrays.binarySearch(LAVA_IDS, id);
}
private int id(int x, int y) {
- return GP.world.getForeMap(x, y);
+ return mGameWorld.getForeMap(x, y);
}
private boolean sameFluid(int thisId, int thatId) {
private boolean drainFluid(int x, int y) {
if (getBlockState(id(x, y)) > 0) {
if (noFluidNearby(x, y)) {
- GP.world.setForeMap(x, y, getNextBlockStateId(id(x, y)));
+ mGameWorld.setForeMap(x, y, getNextBlockStateId(id(x, y)));
}
if (!isFluid(id(x, y))) {
- GP.world.setForeMap(x, y, 0);
+ mGameWorld.setForeMap(x, y, 0);
return true;
}
}
private void flowFluidTo(int thisId, int x, int y, int nextStateId) {
int thatId = id(x, y);
if (fluidCanFlowThere(thisId, thatId)) {
- GP.world.setForeMap(x, y, nextStateId);
+ mGameWorld.setForeMap(x, y, nextStateId);
} else if (isWater(thisId) && isLava(thatId)) {
if (getBlockState(thatId) > 0) {
- GP.world.setForeMap(x, y, 4); //cobblestone
+ mGameWorld.setForeMap(x, y, 4); //cobblestone
} else {
- GP.world.setForeMap(x, y, 68); //obsidian
+ mGameWorld.setForeMap(x, y, 68); //obsidian
}
} else if (isLava(thisId) && isWater(thatId)) {
- GP.world.setForeMap(x, y, 1); //stone
+ mGameWorld.setForeMap(x, y, 1); //stone
}
}
}
private void fluidUpdater() {
- int midScreen = (int) (GP.renderer.getCamX() + GP.renderer.getWidth() / 2) / 16;
- for (int y = GP.world.getHeight() - 1; y >= 0; y--) {
- for (int x = 0; x <= GP.world.getWidth() / 2; x++) {
+ int midScreen = (int) mMobsController.getPlayer().x / 16;
+ for (int y = mGameWorld.getHeight() - 1; y >= 0; y--) {
+ for (int x = 0; x <= mGameWorld.getWidth() / 2; x++) {
updateFluids(midScreen + x, y);
updateFluids(midScreen - x, y);
}
}
private boolean timeToUpdate() {
- if (TimeUtils.timeSinceMillis(fluidLastUpdateTimestamp) >= FLUID_UPDATE_INTERVAL_MS) {
- fluidLastUpdateTimestamp = TimeUtils.millis();
+ if (TimeUtils.timeSinceMillis(mFluidLastUpdateTimestamp) >= FLUID_UPDATE_INTERVAL_MS) {
+ mFluidLastUpdateTimestamp = TimeUtils.millis();
return true;
}
return false;
@Override
public void run() {
- while (!this.isInterrupted()) {
+ while (!this.isInterrupted() && mMainThread.isAlive()) {
if (timeToUpdate()) {
fluidUpdater();
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java
index dcc1615667226d96b352a3597160f1fdf4cc6def..3a5e3c4ff47044665b0d69cac4384f0c7bd7072c 100644 (file)
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.TimeUtils;
import com.google.common.collect.Range;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.MainConfig;
import ru.deadsoftware.cavedroid.game.mobs.Mob;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
import ru.deadsoftware.cavedroid.game.mobs.Pig;
+import ru.deadsoftware.cavedroid.game.mobs.Player;
import ru.deadsoftware.cavedroid.misc.Assets;
import ru.deadsoftware.cavedroid.misc.ControlMode;
-import ru.deadsoftware.cavedroid.misc.states.AppState;
-import ru.deadsoftware.cavedroid.misc.states.GameState;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+import javax.inject.Inject;
+
import static ru.deadsoftware.cavedroid.game.GameItems.*;
+@GameScope
public class GameInput {
- private boolean keyDown, touchedDown, dragging;
+ private final MainConfig mMainConfig;
+ private final GameWorld mGameWorld;
+ private final MobsController mMobsController;
+ private final Player mPlayer;
+
+ private ControlMode mControlMode;
+
+ private boolean mKeyDown;
+ private boolean mTouchedDown;
+ private boolean mDragging;
+
+ private int mKeyDownCode;
+ private int mTouchDownBtn;
+ 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,
+ GameWorld gameWorld,
+ MobsController mobsController) {
+ mMainConfig = mainConfig;
+ mGameWorld = gameWorld;
+ mMobsController = mobsController;
- private int keyDownCode, touchDownBtn;
- private float touchDownX, touchDownY;
- private long touchDownTime;
+ mPlayer = mMobsController.getPlayer();
- private int curX, curY;
- private int creativeScroll;
- private int blockDamage;
+ mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR;
+ }
private boolean checkSwim() {
- return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY()));
+ return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()));
}
private void goUpwards() {
if (checkSwim()) {
- GP.player.swim = true;
- } else if (GP.player.canJump()) {
- GP.player.getMove().add(0, -7);
- } else if (!GP.player.isFlyMode() && GP.player.gameMode == 1) {
- GP.player.setFlyMode(true);
- GP.player.getMove().y = 0;
- } else if (GP.player.isFlyMode()) {
- GP.player.getMove().y = -GamePhysics.PL_SPEED;
+ mPlayer.swim = true;
+ } else if (mPlayer.canJump()) {
+ mPlayer.getMove().add(0, -7);
+ } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
+ mPlayer.setFlyMode(true);
+ mPlayer.getMove().y = 0;
+ } else if (mPlayer.isFlyMode()) {
+ mPlayer.getMove().y = -GamePhysics.PL_SPEED;
}
}
@SuppressWarnings("IntegerDivisionInFloatingPointContext")
private boolean insideCreativeInv(float screenX, float screenY) {
TextureRegion creative = Assets.textureRegions.get("creative");
- return (screenX > GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 &&
- screenX < GP.renderer.getWidth() / 2 + creative.getRegionWidth() / 2 &&
- screenY > GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 &&
- screenY < GP.renderer.getHeight() / 2 + creative.getRegionHeight() / 2);
+ return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 &&
+ screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 &&
+ screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 &&
+ screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2);
}
private void wasdPressed(int keycode) {
- if (GP.controlMode == ControlMode.WALK || !CaveGame.TOUCH) {
+ if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
switch (keycode) {
case Input.Keys.A:
- GP.player.getMove().x = -GamePhysics.PL_SPEED;
- GP.player.setDir(Mob.Direction.LEFT);
- if (CaveGame.TOUCH && checkSwim()) {
- GP.player.swim = true;
+ mPlayer.getMove().x = -GamePhysics.PL_SPEED;
+ mPlayer.setDir(Mob.Direction.LEFT);
+ if (mMainConfig.isTouch() && checkSwim()) {
+ mPlayer.swim = true;
}
break;
case Input.Keys.D:
- GP.player.getMove().x = GamePhysics.PL_SPEED;
- GP.player.setDir(Mob.Direction.RIGHT);
- if (CaveGame.TOUCH && checkSwim()) {
- GP.player.swim = true;
+ mPlayer.getMove().x = GamePhysics.PL_SPEED;
+ mPlayer.setDir(Mob.Direction.RIGHT);
+ if (mMainConfig.isTouch() && checkSwim()) {
+ mPlayer.swim = true;
}
break;
case Input.Keys.W:
break;
case Input.Keys.S:
case Input.Keys.CONTROL_LEFT:
- GP.player.getMove().y = GamePhysics.PL_SPEED;
+ mPlayer.getMove().y = GamePhysics.PL_SPEED;
break;
}
} else {
switch (keycode) {
case Input.Keys.A:
- curX--;
+ mCurX--;
break;
case Input.Keys.D:
- curX++;
+ mCurX++;
break;
case Input.Keys.W:
- curY--;
+ mCurY--;
break;
case Input.Keys.S:
- curY++;
+ mCurY++;
break;
}
- blockDamage = 0;
+ mBlockDamage = 0;
}
}
private boolean isNotAutoselectable(int x, int y) {
- return (!GP.world.hasForeAt(x, y) || !GP.world.getForeMapBlock(x, y).hasCollision());
+ return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision());
}
private void checkCursorBounds() {
- if (curY < 0) {
- curY = 0;
- } else if (curY >= GP.world.getHeight()) {
- curY = GP.world.getHeight() - 1;
+ if (mCurY < 0) {
+ mCurY = 0;
+ } else if (mCurY >= mGameWorld.getHeight()) {
+ mCurY = mGameWorld.getHeight() - 1;
}
- if (GP.controlMode == ControlMode.CURSOR) {
- if (curX * 16 + 8 < GP.player.getX() + GP.player.getWidth() / 2) {
- GP.player.setDir(Mob.Direction.LEFT);
+ if (mControlMode == ControlMode.CURSOR) {
+ if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) {
+ mPlayer.setDir(Mob.Direction.LEFT);
} else {
- GP.player.setDir(Mob.Direction.RIGHT);
+ mPlayer.setDir(Mob.Direction.RIGHT);
}
}
}
- private void moveCursor() {
- int pastX = curX;
- int pastY = curY;
+ public void moveCursor(GameRenderer gameRenderer) {
+ int pastX = mCurX;
+ int pastY = mCurY;
- if (GP.controlMode == ControlMode.WALK && CaveGame.TOUCH) {
- curX = GP.player.getMapX() + (GP.player.looksLeft() ? -1 : 1);
- curY = GP.player.getUpperMapY();
- for (int i = 0; i < 2 && isNotAutoselectable(curX, curY); i++) {
- curY++;
+ 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++;
}
- if (isNotAutoselectable(curX, curY)) {
- curX += GP.player.looksLeft() ? 1 : -1;
+ if (isNotAutoselectable(mCurX, mCurY)) {
+ mCurX += mPlayer.looksLeft() ? 1 : -1;
}
- } else if (!CaveGame.TOUCH) {
- curX = (int) (Gdx.input.getX() * (GP.renderer.getWidth() /
- GameScreen.getWidth()) + GP.renderer.getCamX()) / 16;
-
- curY = (int) (Gdx.input.getY() * (GP.renderer.getHeight() /
- GameScreen.getHeight()) + GP.renderer.getCamY()) / 16;
- if (curX < 0) {
- curX--;
+ } else if (!mMainConfig.isTouch()) {
+ mCurX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
+ Gdx.graphics.getWidth()) + gameRenderer.getCamX()) / 16;
+
+ mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
+ Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16;
+ if (mCurX < 0) {
+ mCurX--;
}
}
- if (pastX != curX || pastY != curY) {
- blockDamage = 0;
+ if (pastX != mCurX || pastY != mCurY) {
+ mBlockDamage = 0;
}
checkCursorBounds();
if (id > 0) {
if (getItem(id).isBlock()) {
if (!bg) {
- GP.world.placeToForeground(x, y, getBlockIdByItemId(id));
+ mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id));
} else {
- GP.world.placeToBackground(x, y, getBlockIdByItemId(id));
+ mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id));
}
} else {
switch (key) {
case "bucket_water":
- GP.world.placeToForeground(x, y, getBlockId("water"));
- GP.player.inventory[GP.player.slot] = getItemId("bucket_empty");
+ mGameWorld.placeToForeground(x, y, getBlockId("water"));
+ mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
break;
case "bucket_lava":
- GP.world.placeToForeground(x, y, getBlockId("lava"));
- GP.player.inventory[GP.player.slot] = getItemId("bucket_empty");
+ mGameWorld.placeToForeground(x, y, getBlockId("lava"));
+ mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
break;
}
}
}
private void pressLMB() {
- if (CaveGame.GAME_STATE == GameState.PLAY &&
- ((GP.world.hasForeAt(curX, curY) && GP.world.getForeMapBlock(curX, curY).getHp() >= 0) ||
- (!GP.world.hasForeAt(curX, curY) && GP.world.hasBackAt(curX, curY) &&
- GP.world.getBackMapBlock(curX, curY).getHp() >= 0))) {
- if (GP.player.gameMode == 0) {
- blockDamage++;
- if (GP.world.hasForeAt(curX, curY)) {
- if (blockDamage >= GP.world.getForeMapBlock(curX, curY).getHp()) {
- GP.world.destroyForeMap(curX, curY);
- blockDamage = 0;
+ if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE) &&
+ ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) ||
+ (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
+ mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0))) {
+ if (mPlayer.gameMode == 0) {
+ mBlockDamage++;
+ if (mGameWorld.hasForeAt(mCurX, mCurY)) {
+ if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) {
+ mGameWorld.destroyForeMap(mCurX, mCurY);
+ mBlockDamage = 0;
}
- } else if (GP.world.hasBackAt(curX, curY)) {
- if (blockDamage >= GP.world.getBackMapBlock(curX, curY).getHp()) {
- GP.world.destroyBackMap(curX, curY);
- blockDamage = 0;
+ } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
+ if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) {
+ mGameWorld.destroyBackMap(mCurX, mCurY);
+ mBlockDamage = 0;
}
}
} else {
- if (GP.world.hasForeAt(curX, curY)) {
- GP.world.placeToForeground(curX, curY, 0);
- } else if (GP.world.hasBackAt(curX, curY)) {
- GP.world.placeToBackground(curX, curY, 0);
+ if (mGameWorld.hasForeAt(mCurX, mCurY)) {
+ mGameWorld.placeToForeground(mCurX, mCurY, 0);
+ } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
+ mGameWorld.placeToBackground(mCurX, mCurY, 0);
}
- touchedDown = false;
+ mTouchedDown = false;
}
}
}
private boolean insideHotbar(float x, float y) {
TextureRegion hotbar = Assets.textureRegions.get("hotbar");
return y < hotbar.getRegionHeight() &&
- Range.open(GP.renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
- GP.renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
+ Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
+ mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
}
private void holdMB() {
- if (touchDownBtn == Input.Buttons.RIGHT) {
- useItem(curX, curY, GP.player.inventory[GP.player.slot], true);
- touchedDown = false;
+ if (mTouchDownBtn == Input.Buttons.RIGHT) {
+ useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
+ mTouchedDown = false;
} else {
- if (insideHotbar(touchDownX, touchDownY)) {
- CaveGame.GAME_STATE = GameState.CREATIVE_INV;
- touchedDown = false;
+ if (insideHotbar(mTouchDownX, mTouchDownY)) {
+// CaveGame.GAME_STATE = GameState.CREATIVE_INV;
+ mTouchedDown = false;
}
}
}
public void keyDown(int keycode) {
- keyDown = true;
- keyDownCode = keycode;
+ mKeyDown = true;
+ mKeyDownCode = keycode;
switch (keycode) {
case Input.Keys.A:
case Input.Keys.D:
break;
case Input.Keys.ALT_LEFT:
- if (CaveGame.TOUCH) {
- GP.controlMode = GP.controlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
+ if (mMainConfig.isTouch()) {
+ mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
}
break;
case Input.Keys.E:
- if (CaveGame.GAME_STATE == GameState.PLAY) {
- switch (GP.player.gameMode) {
+ if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
+ switch (mPlayer.gameMode) {
case 0:
//TODO survival inv
break;
case 1:
- CaveGame.GAME_STATE = GameState.CREATIVE_INV;
+ mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
break;
}
} else {
- CaveGame.GAME_STATE = GameState.PLAY;
+ mMainConfig.setGameUiWindow(GameUiWindow.NONE);
}
break;
case Input.Keys.G:
- GP.mobs.add(new Pig(curX * 16, curY * 16));
+ mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16);
break;
case Input.Keys.Q:
- GP.world.placeToForeground(curX, curY, 8);
+ mGameWorld.placeToForeground(mCurX, mCurY, 8);
break;
- case Input.Keys.ESCAPE:
- case Input.Keys.BACK:
- CaveGame.APP_STATE = AppState.SAVE;
- CaveGame.GAME_STATE = GameState.PAUSE;
- break;
+// case Input.Keys.ESCAPE:
+// case Input.Keys.BACK:
+// CaveGame.APP_STATE = AppState.SAVE;
+// CaveGame.GAME_STATE = GameState.PAUSE;
+// break;
case Input.Keys.F1:
- GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG;
+ mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
break;
case Input.Keys.M:
- GameScreen.SHOW_MAP = !GameScreen.SHOW_MAP;
+ mMainConfig.setShowMap(!mMainConfig.isShowMap());
break;
}
}
switch (keycode) {
case Input.Keys.A:
case Input.Keys.D:
- GP.player.getMove().x = 0;
- if (CaveGame.TOUCH && GP.player.swim) {
- GP.player.swim = false;
+ mPlayer.getMove().x = 0;
+ if (mMainConfig.isTouch() && mPlayer.swim) {
+ mPlayer.swim = false;
}
break;
case Input.Keys.S:
case Input.Keys.SPACE:
case Input.Keys.CONTROL_LEFT:
- if (GP.player.isFlyMode()) {
- GP.player.getMove().y = 0;
+ if (mPlayer.isFlyMode()) {
+ mPlayer.getMove().y = 0;
}
- if (GP.player.swim) {
- GP.player.swim = false;
+ if (mPlayer.swim) {
+ mPlayer.swim = false;
}
break;
}
}
public void touchDown(float touchX, float touchY, int button) {
- touchDownTime = TimeUtils.millis();
- touchedDown = true;
- touchDownBtn = button;
- touchDownX = touchX;
- touchDownY = touchY;
+ mTouchDownTime = TimeUtils.millis();
+ mTouchedDown = true;
+ mTouchDownBtn = button;
+ mTouchDownX = touchX;
+ mTouchDownY = touchY;
}
public void touchUp(float screenX, float screenY, int button) {
- if (dragging) {
- dragging = false;
+ if (mDragging) {
+ mDragging = false;
return;
}
- if (CaveGame.TOUCH && keyDown) {
- keyUp(keyDownCode);
- keyDown = false;
+ if (mMainConfig.isTouch() && mKeyDown) {
+ keyUp(mKeyDownCode);
+ mKeyDown = false;
}
TextureRegion hotbar = Assets.textureRegions.get("hotbar");
TextureRegion creative = Assets.textureRegions.get("creative");
- if (touchedDown) {
- if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && insideCreativeInv(screenX, screenY)) {
- int ix = (int) (screenX - (GP.renderer.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
- int iy = (int) (screenY - (GP.renderer.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
- int item = creativeScroll * 8 + (ix + iy * 8);
+ if (mTouchedDown) {
+ if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
+ int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
+ int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
+ int item = mCreativeScroll * 8 + (ix + iy * 8);
if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
item = -1;
}
if (item >= 0 && item < GameItems.getItemsSize()) {
- System.arraycopy(GP.player.inventory, 0, GP.player.inventory, 1, 8);
- GP.player.inventory[0] = item;
+ System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
+ mPlayer.inventory[0] = item;
}
- } else if (CaveGame.GAME_STATE == GameState.CREATIVE_INV) {
- CaveGame.GAME_STATE = GameState.PLAY;
+ } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
+ mMainConfig.setGameUiWindow(GameUiWindow.NONE);
} else if (screenY < hotbar.getRegionHeight() &&
- screenX > GP.renderer.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
- screenX < GP.renderer.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
- GP.player.slot = (int) ((screenX - (GP.renderer.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
+ screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
+ 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(curX, curY,
- GP.player.inventory[GP.player.slot], false);
+ useItem(mCurX, mCurY,
+ mPlayer.inventory[mPlayer.slot], false);
} else if (button == Input.Buttons.LEFT) {
- blockDamage = 0;
+ mBlockDamage = 0;
}
}
- touchedDown = false;
+ mTouchedDown = false;
}
public void touchDragged(float screenX, float screenY) {
- dragging = true;
- if (CaveGame.GAME_STATE == GameState.CREATIVE_INV && Math.abs(screenY - touchDownY) > 16) {
+ mDragging = true;
+ if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
if (insideCreativeInv(screenX, screenY)) {
- creativeScroll -= (screenY - touchDownY) / 16;
- touchDownX = screenX;
- touchDownY = screenY;
- if (creativeScroll < 0) {
- creativeScroll = 0;
+ mCreativeScroll -= (screenY - mTouchDownY) / 16;
+ mTouchDownX = screenX;
+ mTouchDownY = screenY;
+ if (mCreativeScroll < 0) {
+ mCreativeScroll = 0;
}
- if (creativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
- creativeScroll = GameProc.MAX_CREATIVE_SCROLL;
+ if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
+ mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
}
}
}
}
public void scrolled(int amount) {
- switch (CaveGame.GAME_STATE) {
- case PLAY:
- GP.player.slot += amount;
- if (GP.player.slot < 0) {
- GP.player.slot = 8;
+ switch (mMainConfig.getGameUiWindow()) {
+ case NONE:
+ mPlayer.slot += amount;
+ if (mPlayer.slot < 0) {
+ mPlayer.slot = 8;
}
- if (GP.player.slot > 8) {
- GP.player.slot = 0;
+ if (mPlayer.slot > 8) {
+ mPlayer.slot = 0;
}
break;
- case CREATIVE_INV:
- creativeScroll += amount;
- if (creativeScroll < 0) {
- creativeScroll = 0;
+ case CREATIVE_INVENTORY:
+ mCreativeScroll += amount;
+ if (mCreativeScroll < 0) {
+ mCreativeScroll = 0;
}
- if (creativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
- creativeScroll = GameProc.MAX_CREATIVE_SCROLL;
+ if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
+ mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
}
break;
}
}
public int getKeyDownCode() {
- return keyDownCode;
+ return mKeyDownCode;
}
public boolean isKeyDown() {
- return keyDown;
+ return mKeyDown;
}
int getBlockDamage() {
- return blockDamage;
+ return mBlockDamage;
}
int getCurX() {
- return curX;
+ return mCurX;
}
int getCurY() {
- return curY;
+ return mCurY;
}
int getCreativeScroll() {
- return creativeScroll;
+ return mCreativeScroll;
+ }
+
+ public ControlMode getControlMode() {
+ return mControlMode;
+ }
+
+ public void setControlMode(ControlMode controlMode) {
+ mControlMode = controlMode;
}
void update() {
- moveCursor();
- if (touchedDown && touchDownBtn == Input.Buttons.LEFT) {
+ if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) {
pressLMB();
}
- if (touchedDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
+ if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
holdMB();
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java b/core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java
similarity index 64%
rename from core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java
rename to core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java
index 6a7e4d388f9dce5c22a137331361039887d7422f..faafd63959c4ede000938d27d503ce13ac0c6f92 100644 (file)
rename from core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java
rename to core/src/ru/deadsoftware/cavedroid/game/GameInputProcessor.java
index 6a7e4d388f9dce5c22a137331361039887d7422f..faafd63959c4ede000938d27d503ce13ac0c6f92 100644 (file)
-package ru.deadsoftware.cavedroid.misc;
+package ru.deadsoftware.cavedroid.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.JsonValue;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.MainConfig;
import ru.deadsoftware.cavedroid.game.objects.TouchButton;
+import ru.deadsoftware.cavedroid.misc.Assets;
-import static com.badlogic.gdx.utils.ObjectMap.Entry;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
-public class InputHandlerGame extends InputAdapter {
+import javax.inject.Inject;
- private static TouchButton nullButton = new TouchButton(null, -1, true);
+import static com.badlogic.gdx.utils.ObjectMap.Entry;
- public InputHandlerGame() {
+@GameScope
+public class GameInputProcessor extends InputAdapter {
+
+ private static final TouchButton nullButton = new TouchButton(null, -1, true);
+
+ private final GameInput mGameInput;
+ private final GameRenderer mGameRenderer;
+ private final MainConfig mMainConfig;
+
+ @Inject
+ public GameInputProcessor(GameInput gameInput,
+ GameRenderer gameRenderer,
+ MainConfig mainConfig) {
+ mGameInput = gameInput;
+ mGameRenderer = gameRenderer;
+ mMainConfig = mainConfig;
+
loadTouchButtonsFromJSON();
}
String name = key.getString("key");
int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
if (x < 0) {
- x = GP.renderer.getWidth() + x;
+ x = mGameRenderer.getWidth() + x;
}
if (y < 0) {
- y = GP.renderer.getHeight() + y;
+ y = mGameRenderer.getHeight() + y;
}
Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
}
}
private float transformScreenX(int screenX) {
- return GP.renderer.getWidth() / GameScreen.getWidth() * screenX;
+ return mGameRenderer.getWidth() / Gdx.graphics.getWidth() * screenX;
}
private float transformScreenY(int screenY) {
- return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
+ return mGameRenderer.getHeight() / Gdx.graphics.getHeight() * screenY;
}
private TouchButton getTouchedKey(float touchX, float touchY) {
- for (Entry entry : Assets.guiMap) {
- TouchButton button = (TouchButton) entry.value;
+ for (Entry<String, TouchButton> entry : Assets.guiMap) {
+ TouchButton button = entry.value;
if (button.getRect().contains(touchX, touchY)) {
return button;
}
@Override
public boolean keyDown(int keycode) {
- GP.input.keyDown(keycode);
+ mGameInput.keyDown(keycode);
return false;
}
@Override
public boolean keyUp(int keycode) {
- GP.input.keyUp(keycode);
+ mGameInput.keyUp(keycode);
return false;
}
float touchX = transformScreenX(screenX);
float touchY = transformScreenY(screenY);
- if (CaveGame.TOUCH) {
+ if (mMainConfig.isTouch()) {
TouchButton touchedKey = getTouchedKey(touchX, touchY);
if (touchedKey.isMouse()) {
- GP.input.touchDown(touchX, touchY, touchedKey.getCode());
+ mGameInput.touchDown(touchX, touchY, touchedKey.getCode());
} else {
- GP.input.keyDown(touchedKey.getCode());
+ mGameInput.keyDown(touchedKey.getCode());
}
} else {
- GP.input.touchDown(touchX, touchY, button);
+ mGameInput.touchDown(touchX, touchY, button);
}
return false;
}
float touchX = transformScreenX(screenX);
float touchY = transformScreenY(screenY);
- if (CaveGame.TOUCH) {
+ if (mMainConfig.isTouch()) {
TouchButton touchedKey = getTouchedKey(touchX, touchY);
if (touchedKey.isMouse()) {
- GP.input.touchUp(touchX, touchY, touchedKey.getCode());
+ mGameInput.touchUp(touchX, touchY, touchedKey.getCode());
} else {
- GP.input.keyUp(GP.input.getKeyDownCode());
+ mGameInput.keyUp(mGameInput.getKeyDownCode());
}
} else {
- GP.input.touchUp(touchX, touchY, button);
+ mGameInput.touchUp(touchX, touchY, button);
}
return false;
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
float touchX = transformScreenX(screenX);
float touchY = transformScreenY(screenY);
- if (CaveGame.TOUCH && GP.input.isKeyDown()) {
+ if (mMainConfig.isTouch() && mGameInput.isKeyDown()) {
if (getTouchedKey(touchX, touchY).getCode() == -1) {
- GP.input.keyUp(GP.input.getKeyDownCode());
+ mGameInput.keyUp(mGameInput.getKeyDownCode());
}
} else {
- GP.input.touchDragged(touchX, touchY);
+ mGameInput.touchDragged(touchX, touchY);
}
return false;
}
@Override
public boolean scrolled(int amount) {
- GP.input.scrolled(amount);
+ mGameInput.scrolled(amount);
return false;
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java
index 8c3720d42927dffb5da03e7c95c677447f031f61..f210baabf9abcb5658882331a00ea8842e6727bf 100644 (file)
}
public static Sprite getBlockTex(int id) {
- return getBlock(id).getTex();
+ return getBlock(id).getTexture();
}
public static Sprite getItemTex(int id) {
- if (items.getValueAt(id).getType().equals("block")) {
- return getBlockTex(id);
- } else {
- return getItem(id).getTex();
- }
+ return items.getValueAt(id).getType().equals("block") ? getBlockTex(id) : getItem(id).getTexture();
}
public static void load() {
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
index d753b668bc969e6410ba801fe173a594e0d510a7..6695d6e76864606e4fe06a6aaaa80e8dcf3e97b1 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
-import ru.deadsoftware.cavedroid.CaveGame;
+import ru.deadsoftware.cavedroid.MainConfig;
import ru.deadsoftware.cavedroid.game.mobs.Mob;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
import ru.deadsoftware.cavedroid.game.mobs.Player;
import ru.deadsoftware.cavedroid.game.objects.Drop;
+import javax.inject.Inject;
import java.util.Iterator;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+@GameScope
class GamePhysics {
static final int PL_SPEED = 2;
private final Vector2 gravity = new Vector2(0, .9f);
+ private final GameWorld mGameWorld;
+ private final MainConfig mMainConfig;
+ private final MobsController mMobsController;
+
+ @Inject
+ public GamePhysics(GameWorld gameWorld,
+ MainConfig mainConfig,
+ MobsController mobsController) {
+ mGameWorld = gameWorld;
+ mMainConfig = mainConfig;
+ mMobsController = mobsController;
+ }
+
/**
* Checks if mob should jump
*
int dir = mob.looksLeft() ? 0 : 1;
int blX = (int) (mob.getX() + mob.getWidth() * dir - 8 + 16 * dir);
int blY = (int) (mob.getY() + mob.getHeight() - 8);
- int block = GP.world.getForeMap(blX / 16, blY / 16);
+ int block = mGameWorld.getForeMap(blX / 16, blY / 16);
if (checkColl(new Rectangle(blX, mob.getY() - 18, mob.getWidth(), mob.getHeight()))) {
block = 0;
}
return (block > 0 && GameItems.getBlock(block).toJump() &&
- (mob.getY() + mob.getHeight()) - GameItems.getBlock(block).getRect(blX / 16, blY / 16).y > 8);
+ (mob.getY() + mob.getHeight()) - GameItems.getBlock(block).getRectangle(blX / 16, blY / 16).y > 8);
}
private boolean checkColl(Rectangle rect) {
minY = 0;
}
- if (maxY > GP.world.getHeight()) {
- maxY = GP.world.getHeight();
+ if (maxY > mGameWorld.getHeight()) {
+ maxY = mGameWorld.getHeight();
}
int block;
for (int y = minY; y < maxY; y++) {
for (int x = minX; x < maxX; x++) {
- block = GP.world.getForeMap(x, y);
+ block = mGameWorld.getForeMap(x, y);
if (block > 0 && GameItems.getBlock(block).hasCollision()) {
- if (Intersector.overlaps(rect, GameItems.getBlock(block).getRect(x, y))) {
+ if (Intersector.overlaps(rect, GameItems.getBlock(block).getRectangle(x, y))) {
return true;
}
}
}
private int getBlock(Rectangle rect) {
- return GP.world.getForeMap((int) (rect.x + rect.width / 2) / 16,
+ return mGameWorld.getForeMap((int) (rect.x + rect.width / 2) / 16,
(int) (rect.y + rect.height / 8 * 7) / 16);
}
private void dropPhy(Drop drop) {
- if (drop.closeToPlayer() > 0) {
- drop.moveToPlayer();
+ int dropToPlayer = drop.closeToPlayer(mGameWorld, mMobsController.getPlayer());
+ if (dropToPlayer > 0) {
+ drop.moveToPlayer(mGameWorld, mMobsController.getPlayer(), dropToPlayer);
} else {
if (drop.getMove().x >= .5f) {
drop.getMove().x -= .5f;
}
}
- mob.checkWorldBounds();
+ mob.checkWorldBounds(mGameWorld);
}
private void mobYColl(Mob mob) {
mob.setCanJump(false);
}
- if (mob.getY() > GP.world.getHeightPx()) {
+ if (mob.getY() > mGameWorld.getHeightPx()) {
mob.kill();
}
}
}
if (GameItems.isFluid(getBlock(player))) {
- if (CaveGame.TOUCH && player.getMove().x != 0 && !player.swim && !player.isFlyMode()) {
+ if (mMainConfig.isTouch() && player.getMove().x != 0 && !player.swim && !player.isFlyMode()) {
player.swim = true;
}
if (!player.swim) {
mobXColl(player);
- if (CaveGame.TOUCH && !player.isFlyMode() && player.canJump() && player.getMove().x != 0 && checkJump(player)) {
+ if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getMove().x != 0 && checkJump(player)) {
player.getMove().add(0, -8);
player.setCanJump(false);
}
}
void update() {
- for (Iterator<Drop> it = GP.drops.iterator(); it.hasNext(); ) {
- Drop drop = it.next();
- dropPhy(drop);
- if (Intersector.overlaps(drop, GP.player)) {
- drop.pickUpDrop(GP.player);
- }
- if (drop.isPickedUp()) {
- it.remove();
- }
- }
-
- for (Iterator<Mob> it = GP.mobs.iterator(); it.hasNext(); ) {
+// for (Iterator<Drop> it = GP.drops.iterator(); it.hasNext(); ) {
+// Drop drop = it.next();
+// dropPhy(drop);
+// if (Intersector.overlaps(drop, GP.player)) {
+// drop.pickUpDrop(GP.player);
+// }
+// if (drop.isPickedUp()) {
+// it.remove();
+// }
+// }
+
+ for (Iterator<Mob> it = mMobsController.getIterator(); it.hasNext(); ) {
Mob mob = it.next();
- mob.ai();
+ mob.ai(mGameWorld);
mobPhy(mob);
if (mob.isDead()) {
it.remove();
}
}
- playerPhy(GP.player);
- if (GP.player.isDead()) {
- GP.player.respawn();
+ Player player = mMobsController.getPlayer();
+ playerPhy(player);
+ if (player.isDead()) {
+ player.respawn(mGameWorld);
}
-
- GP.renderer.setCamPos(GP.player.getX() + GP.player.getWidth() / 2 - GP.renderer.getWidth() / 2,
- GP.player.getY() + GP.player.getHeight() / 2 - GP.renderer.getHeight() / 2);
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameProc.java b/core/src/ru/deadsoftware/cavedroid/game/GameProc.java
index ea5296a4f226aa93c4d3bb594d0071de41c8ec91..ef0821107a4e520b0d56e4c21d145e9203549992 100644 (file)
package ru.deadsoftware.cavedroid.game;
-import com.badlogic.gdx.utils.Disposable;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
-import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
-import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
-import ru.deadsoftware.cavedroid.game.mobs.Mob;
-import ru.deadsoftware.cavedroid.game.mobs.Player;
-import ru.deadsoftware.cavedroid.game.objects.Drop;
-import ru.deadsoftware.cavedroid.misc.ControlMode;
+import javax.inject.Inject;
-import javax.annotation.Nullable;
-import java.io.Serializable;
-import java.util.LinkedList;
+@GameScope
+public class GameProc {
-public class GameProc implements Serializable, Disposable {
+ public static final int MAX_CREATIVE_SCROLL = GameItems.getItemsSize() / 8;
- static final int MAX_CREATIVE_SCROLL = GameItems.getItemsSize() / 8;
+ private final GameWorld mGameWorld;
+ private final GamePhysics mGamePhysics;
+ private final GameInput mGameInput;
+ private final GameRenderer mGameRenderer;
- private static final int WORLD_WIDTH = 1024;
- private static final int WORLD_HEIGHT = 256;
- private static final int UPD_RANGE = 16;
+ @Inject
+ public GameProc(GameWorld gameWorld,
+ GamePhysics gamePhysics,
+ GameInput gameInput,
+ GameRenderer gameRenderer) {
+ mGameWorld = gameWorld;
+ mGamePhysics = gamePhysics;
+ mGameInput = gameInput;
+ mGameRenderer = gameRenderer;
- static boolean DO_UPD = false;
- static int UPD_X = -1, UPD_Y = -1;
- public final Player player;
- public final LinkedList<Mob> mobs;
- final LinkedList<Drop> drops;
- public transient GameWorld world;
- public transient GameRenderer renderer;
- public transient GameInput input;
- public ControlMode controlMode;
- transient GamePhysics physics;
- @Nullable private transient GameFluidsThread fluidThread;
-
- public GameProc(int gameMode) {
- world = new GameWorld(WORLD_WIDTH, WORLD_HEIGHT);
- player = new Player(gameMode);
- drops = new LinkedList<>();
- mobs = new LinkedList<>();
- physics = new GamePhysics();
- input = new GameInput();
- controlMode = CaveGame.TOUCH ? ControlMode.WALK : ControlMode.CURSOR;
- renderer = resetRenderer();
- startFluidThread();
- }
-
- public GameRenderer resetRenderer() {
- int scale = CaveGame.TOUCH ? 320 : 480;
- renderer = new GameRenderer(scale, scale * GameScreen.getHeight() / GameScreen.getWidth());
- return renderer;
+ mGameWorld.startFluidsThread();
}
- private void startFluidThread() {
- fluidThread = new GameFluidsThread();
- fluidThread.start();
+ public void update(float delta) {
+ mGamePhysics.update();
+ mGameInput.update();
+ mGameWorld.update();
+ mGameRenderer.render(delta);
}
- private void updateBlock(int x, int y) {
- if (world.getForeMap(x, y) == 10) {
- if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
- world.setForeMap(x, y, 0);
- mobs.add(new FallingSand(x * 16, y * 16));
- updateBlock(x, y - 1);
- }
- }
-
- if (world.getForeMap(x, y) == 11) {
- if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
- world.setForeMap(x, y, 0);
- mobs.add(new FallingGravel(x * 16, y * 16));
- updateBlock(x, y - 1);
- }
- }
-
- if (world.hasForeAt(x, y) && world.getForeMapBlock(x, y).requiresBlock()) {
- if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
- world.destroyForeMap(x, y);
- updateBlock(x, y - 1);
- }
- }
-
- if (world.getForeMap(x, y) == 2) {
- if (world.hasForeAt(x, y - 1) && (world.getForeMapBlock(x, y - 1).hasCollision() ||
- GameItems.isFluid(world.getForeMap(x, y - 1)))) {
- world.setForeMap(x, y, 3);
- }
- }
- }
-
- private void blockUpdater() {
- if (DO_UPD) {
- for (int y = UPD_Y; y < UPD_Y + UPD_RANGE; y++) {
- for (int x = UPD_X; x < UPD_X + UPD_RANGE; x++) {
- updateBlock(x, y);
- }
- }
- DO_UPD = false;
- }
- }
-
- public void update() {
- physics.update();
- input.update();
- blockUpdater();
- if (fluidThread == null || !fluidThread.isAlive()) {
- startFluidThread();
- }
- }
-
- @Override
- public void dispose() {
- if (fluidThread != null) {
- fluidThread.interrupt();
- }
- }
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
index 76d806f40f4c8ef977b8cac409819b22f9cde1ad..018fe1bf750e5b3fba65d98bb1acddcead8af876 100644 (file)
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
-import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.MainConfig;
import ru.deadsoftware.cavedroid.game.mobs.Mob;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.mobs.Player;
import ru.deadsoftware.cavedroid.game.objects.Drop;
+import ru.deadsoftware.cavedroid.game.objects.DropController;
import ru.deadsoftware.cavedroid.misc.ControlMode;
import ru.deadsoftware.cavedroid.misc.Renderer;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+import javax.inject.Inject;
+
import static ru.deadsoftware.cavedroid.misc.Assets.guiMap;
import static ru.deadsoftware.cavedroid.misc.Assets.textureRegions;
+@GameScope
public class GameRenderer extends Renderer {
- GameRenderer(float width, float height) {
- super(width, height);
+ private static final String TAG = "GameRenderer";
+
+ private final MainConfig mMainConfig;
+ private final GameInput mGameInput;
+ private final GameWorld mGameWorld;
+ private final MobsController mMobsController;
+ private final DropController mDropController;
+
+ @Inject
+ GameRenderer(MainConfig mainConfig,
+ GameInput gameInput,
+ GameWorld gameWorld,
+ MobsController mobsController,
+ DropController dropController) {
+ super(mainConfig.getWidth(), mainConfig.getHeight());
+
+ mMainConfig = mainConfig;
+ mGameInput = gameInput;
+ mGameWorld = gameWorld;
+ mMobsController = mobsController;
+ mDropController = dropController;
+
Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
}
}
private void drawWreck(int bl) {
- if (GP.input.getBlockDamage() > 0) {
- int index = 10 * GP.input.getBlockDamage() / GameItems.getBlock(bl).getHp();
+ if (mGameInput.getBlockDamage() > 0) {
+ int index = 10 * mGameInput.getBlockDamage() / GameItems.getBlock(bl).getHp();
String key = "break_" + index;
- spriter.draw(textureRegions.get(key), GP.input.getCurX() * 16 - getCamX(),
- GP.input.getCurY() * 16 - getCamY());
+ spriter.draw(textureRegions.get(key), mGameInput.getCurX() * 16 - getCamX(),
+ mGameInput.getCurY() * 16 - getCamY());
}
}
private void drawBlock(int x, int y, boolean drawBG) {
if (drawBG) {
- if ((!GP.world.hasForeAt(x, y) || GP.world.getForeMapBlock(x, y).isTransparent())
- && GP.world.hasBackAt(x, y)) {
- spriter.draw(GP.world.getBackMapBlock(x, y).getTex(), drawX(x), drawY(y));
- if (!GP.world.hasForeAt(x, y) && x == GP.input.getCurX() && y == GP.input.getCurY()) {
- drawWreck(GP.world.getBackMap(GP.input.getCurX(), GP.input.getCurY()));
+ if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
+ && mGameWorld.hasBackAt(x, y)) {
+ spriter.draw(mGameWorld.getBackMapBlock(x, y).getTexture(), drawX(x), drawY(y));
+ if (!mGameWorld.hasForeAt(x, y) && x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
+ drawWreck(mGameWorld.getBackMap(mGameInput.getCurX(), mGameInput.getCurY()));
}
}
}
- if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).isBackground() == drawBG) {
- spriter.draw(GP.world.getForeMapBlock(x, y).getTex(), drawX(x), drawY(y));
- if (x == GP.input.getCurX() && y == GP.input.getCurY()) {
- drawWreck(GP.world.getForeMap(GP.input.getCurX(), GP.input.getCurY()));
+ if (mGameWorld.hasForeAt(x, y) && mGameWorld.getForeMapBlock(x, y).isBackground() == drawBG) {
+ spriter.draw(mGameWorld.getForeMapBlock(x, y).getTexture(), drawX(x), drawY(y));
+ if (x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
+ drawWreck(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY()));
}
}
}
if (minY < 0) {
minY = 0;
}
- if (maxY > GP.world.getHeight()) {
- maxY = GP.world.getHeight();
+ if (maxY > mGameWorld.getHeight()) {
+ maxY = mGameWorld.getHeight();
}
for (int y = minY; y < maxY; y++) {
for (int x = minX; x < maxX; x++) {
shaper.setColor(0f, 0f, 0f, .5f);
for (int y = minY; y < maxY; y++) {
for (int x = minX; x < maxX; x++) {
- if ((!GP.world.hasForeAt(x, y) || GP.world.getForeMapBlock(x, y).isTransparent())
- && GP.world.hasBackAt(x, y)) {
+ if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
+ && mGameWorld.hasBackAt(x, y)) {
shaper.rect(drawX(x), drawY(y), 16, 16);
}
}
float mobDrawX = mob.getX() - getCamX();
float mobDrawY = mob.getY() - getCamY();
- if (mobDrawX + mob.getWidth() < 0 && mobDrawX + GP.world.getWidthPx() > 0) {
- mobDrawX += GP.world.getWidthPx();
- } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - GP.world.getWidthPx() > 0) {
- mobDrawX -= GP.world.getWidthPx();
+ if (mobDrawX + mob.getWidth() < 0 && mobDrawX + mGameWorld.getWidthPx() > 0) {
+ mobDrawX += mGameWorld.getWidthPx();
+ } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - mGameWorld.getWidthPx() > 0) {
+ mobDrawX -= mGameWorld.getWidthPx();
} else if (mobDrawX + mob.getWidth() < 0 && mobDrawX > getWidth()) {
return;
}
float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2;
spriter.draw(creative, x, y);
spriter.draw(textureRegions.get("handle"), x + 156,
- y + 18 + (GP.input.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
- for (int i = GP.input.getCreativeScroll() * 8; i < GP.input.getCreativeScroll() * 8 + 40; i++) {
+ y + 18 + (mGameInput.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
+ for (int i = mGameInput.getCreativeScroll() * 8; i < mGameInput.getCreativeScroll() * 8 + 40; i++) {
if (i > 0 && i < GameItems.getItemsSize()) {
if (GameItems.getItem(i).isBlock()) {
- spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTex(),
- x + 8 + ((i - GP.input.getCreativeScroll() * 8) % 8) * 18,
- y + 18 + ((i - GP.input.getCreativeScroll() * 8) / 8) * 18);
+ spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTexture(),
+ x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
+ y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
} else {
- spriter.draw(GameItems.getItem(i).getTex(),
- x + 8 + ((i - GP.input.getCreativeScroll() * 8) % 8) * 18,
- y + 18 + ((i - GP.input.getCreativeScroll() * 8) / 8) * 18);
+ spriter.draw(GameItems.getItem(i).getTexture(),
+ x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
+ y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
}
}
}
for (int i = 0; i < 9; i++) {
- if (GP.player.inventory[i] > 0) {
- if (GameItems.getItem(GP.player.inventory[i]).isBlock()) {
- spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(),
+ if (mMobsController.getPlayer().inventory[i] > 0) {
+ if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
+ spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
x + 8 + i * 18, y + creative.getRegionHeight() - 24);
} else {
- spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(),
+ spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
x + 8 + i * 18, y + creative.getRegionHeight() - 24);
}
}
TextureRegion hotbar = textureRegions.get("hotbar");
TextureRegion hotbarSelector = textureRegions.get("hotbar_selector");
- if (GP.world.hasForeAt(GP.input.getCurX(), GP.input.getCurY()) ||
- GP.world.hasBackAt(GP.input.getCurX(), GP.input.getCurY()) ||
- GP.controlMode == ControlMode.CURSOR ||
- !CaveGame.TOUCH) {
- spriter.draw(cursor,
- GP.input.getCurX() * 16 - getCamX(),
- GP.input.getCurY() * 16 - getCamY());
+ if (mGameWorld.hasForeAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
+ mGameWorld.hasBackAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
+ mGameInput.getControlMode() == ControlMode.CURSOR || mMainConfig.isTouch()) {
+ spriter.draw(cursor, mGameInput.getCurX() * 16 - getCamX(), mGameInput.getCurY() * 16 - getCamY());
}
spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0);
for (int i = 0; i < 9; i++) {
- if (GP.player.inventory[i] > 0) {
- if (GameItems.getItem(GP.player.inventory[i]).isBlock()) {
- spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(),
+ if (mMobsController.getPlayer().inventory[i] > 0) {
+ if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
+ spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
3);
} else {
- spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(),
+ spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
3);
}
}
}
spriter.draw(hotbarSelector,
- getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot,
+ getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * mMobsController.getPlayer().slot,
-1);
}
spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
touchKey.x, touchKey.y, touchKey.width, touchKey.height);
}
- if (GP.controlMode == ControlMode.CURSOR) {
+ if (mGameInput.getControlMode() == ControlMode.CURSOR) {
spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21);
}
}
private void drawGamePlay() {
+ Player player = mMobsController.getPlayer();
+
drawWorld(true);
- GP.player.draw(spriter, GP.player.getX() - getCamX() - 2, GP.player.getY() - getCamY());
- GP.mobs.forEach(this::drawMob);
- GP.drops.forEach(this::drawDrop);
+ player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY());
+ mMobsController.forEach(this::drawMob);
+ mDropController.forEach(this::drawDrop);
drawWorld(false);
drawGUI();
}
+ private void updateCameraPosition() {
+ Player player = mMobsController.getPlayer();
+ setCamPos(player.getX() + player.getWidth() / 2 - getWidth() / 2,
+ player.getY() + player.getHeight() / 2 - getHeight() / 2);
+ }
+
@Override
- public void render() {
+ public void render(float delta) {
+ int fps = (int) (1 / delta);
+ updateCameraPosition();
+ mGameInput.moveCursor(this);
+
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriter.begin();
- switch (CaveGame.GAME_STATE) {
- case PLAY:
- drawGamePlay();
- break;
- case CREATIVE_INV:
- drawGamePlay();
+
+ drawGamePlay();
+
+ switch (mMainConfig.getGameUiWindow()) {
+ case CREATIVE_INVENTORY:
drawCreative();
break;
+ //TODO draw other ui windows
}
- if (CaveGame.TOUCH) {
+
+ if (mMainConfig.isTouch()) {
drawTouchGui();
}
spriter.end();
- if (GameScreen.SHOW_MAP) {
+ if (mMainConfig.isShowMap()) {
//DRAW MAP
shaper.begin(ShapeRenderer.ShapeType.Filled);
shaper.setColor(Color.LIGHT_GRAY);
- shaper.rect(0, 0, GP.world.getWidth(), 128);
+ shaper.rect(0, 0, mGameWorld.getWidth(), 128);
for (int y = 128; y < 256; y++) {
for (int x = 0; x < getWidth(); x++) {
- if (GP.world.hasForeAt(x, y) || GP.world.hasBackAt(x, y)) {
- if (GameItems.isWater(GP.world.getForeMap(x, y))) {
+ if (mGameWorld.hasForeAt(x, y) || mGameWorld.hasBackAt(x, y)) {
+ if (GameItems.isWater(mGameWorld.getForeMap(x, y))) {
shaper.setColor(Color.BLUE);
- } else if (GameItems.isLava(GP.world.getForeMap(x, y))) {
+ } else if (GameItems.isLava(mGameWorld.getForeMap(x, y))) {
shaper.setColor(Color.RED);
} else {
- if (GP.world.hasForeAt(x, y)) {
+ if (mGameWorld.hasForeAt(x, y)) {
shaper.setColor(Color.BLACK);
} else {
shaper.setColor(Color.DARK_GRAY);
}
}
shaper.setColor(Color.OLIVE);
- shaper.rect(GP.player.getMapX(), GP.player.getUpperMapY() - 128, 1, 2);
+ shaper.rect(mMobsController.getPlayer().getMapX(), mMobsController.getPlayer().getUpperMapY() - 128, 1, 2);
shaper.end();
//=================
}
- if (GameScreen.SHOW_DEBUG) {
+ if (mMainConfig.isShowInfo()) {
spriter.begin();
- drawString("FPS: " + GameScreen.FPS, 0, 0);
- drawString("X: " + GP.player.getMapX(), 0, 10);
- drawString("Y: " + GP.player.getUpperMapY() / 16, 0, 20);
- drawString("CurX: " + GP.input.getCurX(), 0, 30);
- drawString("CurY: " + GP.input.getCurY(), 0, 40);
- drawString("Mobs: " + GP.mobs.size(), 0, 50);
- drawString("Drops: " + GP.drops.size(), 0, 60);
- drawString("Block: " + GameItems.getBlockKey(GP.world.getForeMap(GP.input.getCurX(), GP.input.getCurY())), 0, 70);
- drawString("Hand: " + GameItems.getItemKey(GP.player.inventory[GP.player.slot]), 0, 80);
- drawString("Game mode: " + GP.player.gameMode, 0, 90);
+ drawString("FPS: " + fps, 0, 0);
+ drawString("X: " + mMobsController.getPlayer().getMapX(), 0, 10);
+ drawString("Y: " + mMobsController.getPlayer().getUpperMapY(), 0, 20);
+ drawString("CurX: " + mGameInput.getCurX(), 0, 30);
+ drawString("CurY: " + mGameInput.getCurY(), 0, 40);
+ drawString("Mobs: " + mMobsController.getSize(), 0, 50);
+ drawString("Drops: " + mDropController.getSize(), 0, 60);
+ drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 70);
+ drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 80);
+ drawString("Game mode: " + mMobsController.getPlayer().gameMode, 0, 90);
spriter.end();
}
-
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java b/core/src/ru/deadsoftware/cavedroid/game/GameSaver.java
index 0788561d8edf39e8a4151278b41abd7a5398c8e3..26281f6c81dfa5c16207c91ce810a6e5f3016192 100644 (file)
package ru.deadsoftware.cavedroid.game;
-import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
-import ru.deadsoftware.cavedroid.CaveGame;
-import java.io.*;
+import java.io.BufferedOutputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
import java.nio.ByteBuffer;
+//TODO rewrite saver
public class GameSaver {
private static final int VERSION = 0;
}
public static GameProc load() {
- FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
- GameProc gameProc = null;
- try {
- ObjectInputStream in = new ObjectInputStream(file.read());
- int ver = in.readInt();
- if (VERSION == ver) {
- gameProc = (GameProc) in.readObject();
- } else {
- throw new Exception("version mismatch");
- }
- in.close();
- gameProc.world = new GameWorld(
- loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
- loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
- );
- gameProc.physics = new GamePhysics();
- gameProc.input = new GameInput();
- } catch (Exception e) {
- Gdx.app.error("GameSaver", e.getMessage(), e);
- Gdx.app.exit();
- }
- return gameProc;
+// FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
+// GameProc gameProc = null;
+// try {
+// ObjectInputStream in = new ObjectInputStream(file.read());
+// int ver = in.readInt();
+// if (VERSION == ver) {
+// gameProc = (GameProc) in.readObject();
+// } else {
+// throw new Exception("version mismatch");
+// }
+// in.close();
+// gameProc.world = new GameWorld(
+// loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav")),
+// loadMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"))
+// );
+// gameProc.physics = new GamePhysics();
+// gameProc.input = new GameInput();
+// } catch (Exception e) {
+// Gdx.app.error("GameSaver", e.getMessage(), e);
+// Gdx.app.exit();
+// }
+// return gameProc;
+ return null;
}
public static void save(GameProc gp) {
- FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
- file.mkdirs();
- file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
- try {
- ObjectOutputStream out = new ObjectOutputStream(file.write(false));
- out.writeInt(VERSION);
- out.writeObject(gp);
- out.close();
- saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
- saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
- } catch (Exception e) {
- e.printStackTrace();
- }
+// FileHandle file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/");
+// file.mkdirs();
+// file = Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav");
+// try {
+// ObjectOutputStream out = new ObjectOutputStream(file.write(false));
+// out.writeInt(VERSION);
+// out.writeObject(gp);
+// out.close();
+// saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav"), gp.world.getFullForeMap());
+// saveMap(Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav"), gp.world.getFullBackMap());
+// } catch (Exception e) {
+// e.printStackTrace();
+// }
}
public static boolean exists() {
- return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
- Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
- Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());
+// return (Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/game.sav").exists() &&
+// Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/foremap.sav").exists() &&
+// Gdx.files.absolute(CaveGame.GAME_FOLDER + "/saves/backmap.sav").exists());
+ return false;
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameScope.java b/core/src/ru/deadsoftware/cavedroid/game/GameScope.java
--- /dev/null
@@ -0,0 +1,10 @@
+package ru.deadsoftware.cavedroid.game;
+
+import javax.inject.Scope;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Scope
+@Retention(RetentionPolicy.RUNTIME)
+public @interface GameScope {
+}
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java b/core/src/ru/deadsoftware/cavedroid/game/GameScreen.java
--- /dev/null
@@ -0,0 +1,66 @@
+package ru.deadsoftware.cavedroid.game;
+
+import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Screen;
+import ru.deadsoftware.cavedroid.MainConfig;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+@Singleton
+public class GameScreen implements Screen {
+
+ private static final String TAG = "GameScreen";
+
+ private final MainConfig mMainConfig;
+
+ private GameProc mGameProc;
+
+ @Inject
+ public GameScreen(MainConfig mainConfig) {
+ mMainConfig = mainConfig;
+ newGame();
+ }
+
+ public void newGame() {
+ GameComponent gameComponent = DaggerGameComponent.builder()
+ .mainComponent(mMainConfig.getMainComponent()).build();
+ mGameProc = gameComponent.getGameProc();
+ Gdx.input.setInputProcessor(gameComponent.getGameInputProcessor());
+ }
+
+ @Override
+ public void render(float delta) {
+ mGameProc.update(delta);
+ }
+
+ @Override
+ public void show() {
+
+ }
+
+ @Override
+ public void resize(int width, int height) {
+
+ }
+
+ @Override
+ public void pause() {
+
+ }
+
+ @Override
+ public void resume() {
+
+ }
+
+ @Override
+ public void hide() {
+
+ }
+
+ @Override
+ public void dispose() {
+ }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameUiWindow.java b/core/src/ru/deadsoftware/cavedroid/game/GameUiWindow.java
--- /dev/null
@@ -0,0 +1,10 @@
+package ru.deadsoftware.cavedroid.game;
+
+public enum GameUiWindow {
+ NONE,
+ CREATIVE_INVENTORY,
+ SURVIVAL_INVENTORY,
+ CHEST,
+ CRAFTING_TABLE,
+ FURNACE
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/GameWorld.java
index c5e7d46ecd99eb7b33f9b07eb0c762d6fdda914b..5aecce295466dda979db1ed307f27f0cbead3706 100644 (file)
package ru.deadsoftware.cavedroid.game;
+import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
+import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
+import ru.deadsoftware.cavedroid.game.mobs.MobsController;
import ru.deadsoftware.cavedroid.game.objects.Block;
-import ru.deadsoftware.cavedroid.game.objects.Drop;
+import ru.deadsoftware.cavedroid.game.objects.DropController;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+import javax.inject.Inject;
-@SuppressWarnings("WeakerAccess")
+@GameScope
public class GameWorld {
- private final int WIDTH;
- private final int HEIGHT;
- private final int[][] foreMap;
- private final int[][] backMap;
-
- GameWorld(int width, int height) {
- WIDTH = width;
- HEIGHT = height;
- WorldGen.genWorld(WIDTH, HEIGHT);
- foreMap = WorldGen.getForeMap();
- backMap = WorldGen.getBackMap();
+ private static final int DEFAULT_WIDTH = 1024;
+ private static final int DEFAULT_HEIGHT = 256;
+ private static final int UPDATE_RANGE = 16;
+
+ private final DropController mDropController;
+ private final MobsController mMobsController;
+ private final GameFluidsThread mGameFluidsThread;
+
+ private final int mWidth;
+ private final int mHeight;
+ private final int[][] mForeMap;
+ private final int[][] mBackMap;
+
+ private boolean mShouldUpdate;
+ private int mUpdateX;
+ private int mUpdateY;
+
+ @Inject
+ GameWorld(DropController dropController,
+ MobsController mobsController) {
+ mDropController = dropController;
+ mMobsController = mobsController;
+
+ mWidth = DEFAULT_WIDTH;
+ mHeight = DEFAULT_HEIGHT;
+ WorldGen.genWorld(mWidth, mHeight);
+ mForeMap = WorldGen.getForeMap();
+ mBackMap = WorldGen.getBackMap();
WorldGen.clear();
- }
- GameWorld(int[][] foreMap, int[][] backMap) {
- this.foreMap = foreMap.clone();
- this.backMap = backMap.clone();
- WIDTH = foreMap.length;
- HEIGHT = foreMap[0].length;
+ mGameFluidsThread = new GameFluidsThread(this, mMobsController, Thread.currentThread());
}
+// GameWorld(int[][] foreMap, int[][] backMap) {
+// this.foreMap = foreMap.clone();
+// this.backMap = backMap.clone();
+// WIDTH = foreMap.length;
+// HEIGHT = foreMap[0].length;
+// }
+
public int getWidth() {
- return WIDTH;
+ return mWidth;
}
public int getHeight() {
- return HEIGHT;
+ return mHeight;
}
public float getWidthPx() {
- return WIDTH * 16f;
+ return mWidth * 16f;
}
public float getHeightPx() {
- return HEIGHT * 16f;
+ return mHeight * 16f;
}
int[][] getFullForeMap() {
- return foreMap;
+ return mForeMap;
}
int[][] getFullBackMap() {
- return backMap;
+ return mBackMap;
}
private int transformX(int x) {
int map = 0;
try {
x = transformX(x);
- map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
+ map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
} catch (ArrayIndexOutOfBoundsException ignored) {
}
return map;
try {
x = transformX(x);
if (layer == 0) {
- foreMap[x][y] = value;
+ mForeMap[x][y] = value;
} else {
- backMap[x][y] = value;
+ mBackMap[x][y] = value;
}
} catch (ArrayIndexOutOfBoundsException ignored) {
}
} else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
placeSlab(x, y, value);
}
- GameProc.UPD_X = x - 8;
- GameProc.UPD_Y = y - 8;
- GameProc.DO_UPD = true;
+ mUpdateX = x - 8;
+ mUpdateY = y - 8;
+ mShouldUpdate = true;
}
public void placeToBackground(int x, int y, int value) {
}
public void destroyForeMap(int x, int y) {
- if (GameItems.getBlock(getForeMap(x, y)).hasDrop()) {
- GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
- GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
+ Block block = GameItems.getBlock(getForeMap(x, y));
+ if (block.hasDrop()) {
+ mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
}
placeToForeground(x, y, 0);
}
public void destroyBackMap(int x, int y) {
- if (GameItems.getBlock(getBackMap(x, y)).hasDrop()) {
- GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
- GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
+ Block block = GameItems.getBlock(getBackMap(x, y));
+ if (block.hasDrop()) {
+ mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
}
placeToBackground(x, y, 0);
}
+ private void updateBlock(int x, int y) {
+ 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);
+ updateBlock(x, y - 1);
+ }
+ }
+
+ 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);
+ updateBlock(x, y - 1);
+ }
+ }
+
+ if (hasForeAt(x, y) && getForeMapBlock(x, y).requiresBlock()) {
+ if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
+ destroyForeMap(x, y);
+ updateBlock(x, y - 1);
+ }
+ }
+
+ if (getForeMap(x, y) == 2) {
+ if (hasForeAt(x, y - 1) && (getForeMapBlock(x, y - 1).hasCollision() ||
+ GameItems.isFluid(getForeMap(x, y - 1)))) {
+ setForeMap(x, y, 3);
+ }
+ }
+ }
+
+ public void update() {
+ if (mShouldUpdate) {
+ for (int y = mUpdateY; y < mUpdateY + UPDATE_RANGE; y++) {
+ for (int x = mUpdateX; x < mUpdateX + UPDATE_RANGE; x++) {
+ updateBlock(x, y);
+ }
+ }
+ mShouldUpdate = false;
+ }
+
+ if (!mGameFluidsThread.isAlive()) {
+ mGameFluidsThread.start();
+ }
+ }
+
+ public void startFluidsThread() {
+ mGameFluidsThread.start();
+ }
+
}
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java
index 70879e4f1499d2db2d91d19107a9852d45c31ede..23cf1dcc836de3b434aa7e9deea52ca53b691568 100644 (file)
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import ru.deadsoftware.cavedroid.game.GameItems;
-
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
+import ru.deadsoftware.cavedroid.game.GameWorld;
/**
* Falling gravel is actually a mob, that spawns in place of gravel when there is no block under it,
*/
public FallingGravel(float x, float y) {
super(x, y, 16, 16, Direction.LEFT, Type.GRAVEL);
- move = new Vector2(0, 1);
+ mMove = new Vector2(0, 1);
}
@Override
- public void ai() {
- if (move.isZero()) {
- GP.world.setForeMap(getMapX(), getMiddleMapY(), 11);
+ public void ai(GameWorld gameWorld) {
+ if (mMove.isZero()) {
+ gameWorld.setForeMap(getMapX(), getMiddleMapY(), 11);
kill();
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java
index a52d92008a24370a3d411b07538fd6c529acc8e6..9a383a21315cce839145870231c0e6f9bbb8cb35 100644 (file)
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import ru.deadsoftware.cavedroid.game.GameItems;
+import ru.deadsoftware.cavedroid.game.GameWorld;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
/**
* Falling sand is actually a mob, that spawns in place of gravel when there is no block under it,
*/
public FallingSand(float x, float y) {
super(x, y, 16, 16, Direction.LEFT, Type.SAND);
- move = new Vector2(0, 1);
+ mMove = new Vector2(0, 1);
}
@Override
- public void ai() {
- if (move.isZero()) {
- GP.world.setForeMap(getMapX(), getMiddleMapY(), 10);
+ public void ai(GameWorld gameWorld) {
+ if (mMove.isZero()) {
+ gameWorld.setForeMap(getMapX(), getMiddleMapY(), 10);
kill();
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java
index 698ebef8c847ba3f5c2a802d0ba1c045cfacd6a4..91dfae55e90823ce7fdc91aa6f919835edc6e58c 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.GameWorld;
import java.io.Serializable;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
/**
* Mob class.
*/
public abstract class Mob extends Rectangle implements Serializable {
- protected Vector2 move;
- protected Type type;
- protected int animDelta = 6;
- protected int anim;
- private Direction dir;
- private boolean dead;
- private boolean canJump, flyMode;
+ public enum Type {
+ MOB,
+ SAND,
+ GRAVEL
+ }
+
+ public enum Direction {
+ LEFT,
+ RIGHT
+ }
+
+ protected Vector2 mMove;
+ protected Type mType;
+ protected int mAnimDelta = 6;
+ protected int mAnim;
+
+ private Direction mDirection;
+ private boolean mDead;
+ private boolean mCanJump;
+ private boolean mFlyMode;
/**
* @param x in pixels
* @param y in pixels
* @param width in pixels
* @param height in pixels
- * @param dir Direction in which mob is looking
+ * @param mDirection Direction in which mob is looking
*/
- protected Mob(float x, float y, float width, float height, Direction dir, Type type) {
+ protected Mob(float x, float y, float width, float height, Direction mDirection, Type type) {
super(x, y, width, height);
- move = new Vector2(0, 0);
- canJump = false;
- dead = false;
- this.dir = dir;
- this.type = type;
+ mMove = new Vector2(0, 0);
+ mCanJump = false;
+ mDead = false;
+ this.mDirection = mDirection;
+ this.mType = type;
}
protected static Direction randomDir() {
* @return Integer representing a direction in which mob is looking, where 0 is left and 1 is right
*/
public final Direction getDirection() {
- return dir;
+ return mDirection;
}
public final boolean looksLeft() {
- return dir == Direction.LEFT;
+ return mDirection == Direction.LEFT;
}
public final boolean looksRight() {
- return dir == Direction.RIGHT;
+ return mDirection == Direction.RIGHT;
}
/**
* Switches direction in which mob is looking
*/
protected final void switchDir() {
- dir = looksLeft() ? Direction.RIGHT : Direction.LEFT;
+ mDirection = looksLeft() ? Direction.RIGHT : Direction.LEFT;
}
protected final int dirMultiplier() {
}
public final boolean isDead() {
- return dead;
+ return mDead;
}
public final int getAnim() {
- return anim;
+ return mAnim;
}
/**
* Set's mob's dead variable to true and nothing else. It doesn't delete the
*/
public final void kill() {
- dead = true;
+ mDead = true;
}
public final void move() {
- x += move.x;
- y += move.y;
+ x += mMove.x;
+ y += mMove.y;
}
public final Vector2 getMove() {
- return move;
+ return mMove;
}
public final boolean canJump() {
- return canJump;
+ return mCanJump;
}
public final void setCanJump(boolean canJump) {
- this.canJump = canJump;
+ this.mCanJump = canJump;
}
public final boolean isFlyMode() {
- return flyMode;
+ return mFlyMode;
}
public final void setFlyMode(boolean flyMode) {
- this.flyMode = flyMode;
+ this.mFlyMode = flyMode;
}
public final Type getType() {
- return type;
+ return mType;
}
- public void checkWorldBounds() {
+ public void checkWorldBounds(GameWorld gameWorld) {
if (x + width / 2 < 0) {
- x += GP.world.getWidthPx();
+ x += gameWorld.getWidthPx();
}
- if (x + width / 2 > GP.world.getWidthPx()) {
- x -= GP.world.getWidthPx();
+ if (x + width / 2 > gameWorld.getWidthPx()) {
+ x -= gameWorld.getWidthPx();
}
}
public abstract void draw(SpriteBatch spriteBatch, float x, float y);
- public abstract void ai();
+ public abstract void ai(GameWorld gameWorld);
public abstract void changeDir();
-
- public enum Type {
- MOB,
- SAND,
- GRAVEL
- }
-
- public enum Direction {
- LEFT,
- RIGHT
- }
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/MobsController.java
--- /dev/null
@@ -0,0 +1,51 @@
+package ru.deadsoftware.cavedroid.game.mobs;
+
+import com.badlogic.gdx.Gdx;
+import ru.deadsoftware.cavedroid.game.GameScope;
+
+import javax.inject.Inject;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+@GameScope
+public class MobsController {
+
+ public interface Callback {
+ void run(Mob mob);
+ }
+
+ private static final String TAG = "MobsController";
+
+ private final Player mPlayer;
+ private final LinkedList<Mob> mMobs = new LinkedList<>();
+
+ @Inject
+ public MobsController() {
+ mPlayer = new Player();
+ }
+
+ public Player getPlayer() {
+ return mPlayer;
+ }
+
+ public void addMob(Class<? extends Mob> 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<Mob> getIterator() {
+ return mMobs.iterator();
+ }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Pig.java
index 394d9be23533200e00478f3af5ede019a2c37d51..b362da73a38f6d831aa1fa54b9f2a2e6a9679a58 100644 (file)
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.GameWorld;
import ru.deadsoftware.cavedroid.misc.Assets;
import static ru.deadsoftware.cavedroid.misc.Assets.pigSprite;
public Pig(float x, float y) {
super(x, y, 25, 18, randomDir(), Type.MOB);
- move = new Vector2(looksLeft() ? -1 : 1, 0);
+ mMove = new Vector2(looksLeft() ? -1 : 1, 0);
}
@Override
public void changeDir() {
switchDir();
- move.x = -1 + 2 * dirMultiplier();
+ mMove.x = -1 + 2 * dirMultiplier();
}
@Override
- public void ai() {
+ public void ai(GameWorld gameWorld) {
if (MathUtils.randomBoolean(.0025f)) {
- if (move.x != 0f) {
- move.x = 0;
+ if (mMove.x != 0f) {
+ mMove.x = 0;
} else {
changeDir();
}
}
- if (move.x != 0f) {
- anim += animDelta;
+ if (mMove.x != 0f) {
+ mAnim += mAnimDelta;
} else {
- anim = 0;
+ mAnim = 0;
}
- if (anim >= 60 || anim <= -60) {
- animDelta = -animDelta;
+ if (mAnim >= 60 || mAnim <= -60) {
+ mAnimDelta = -mAnimDelta;
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
index fd7101c7dbd0dafbefab5675098e47cb014723f7..0b7ab8bc8148c48a24b02a3d7d266ec43e7cd3de 100644 (file)
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.GameWorld;
import ru.deadsoftware.cavedroid.misc.Assets;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
public class Player extends Mob {
public final int[] inventory;
public final int gameMode;
public boolean swim;
- public Player(int gameMode) {
+ public Player() {
super(0, 0, 4, 30, randomDir(), Type.MOB);
- this.gameMode = gameMode;
+ this.gameMode = 1;
inventory = new int[9];
swim = false;
}
- public void respawn() {
- Vector2 pos = getSpawnPoint();
+ public void respawn(GameWorld gameWorld) {
+ Vector2 pos = getSpawnPoint(gameWorld);
this.x = pos.x;
this.y = pos.y;
- move.setZero();
+ mMove.setZero();
}
- private Vector2 getSpawnPoint() {
- int x = 0, y;
- for (y = 0; y < GP.world.getHeight(); y++) {
- if (y == GP.world.getHeight() - 1) {
+ private Vector2 getSpawnPoint(GameWorld gameWorld) {
+ int y;
+ for (y = 0; y < gameWorld.getHeight(); y++) {
+ if (y == gameWorld.getHeight() - 1) {
y = 60;
- GP.world.setForeMap(x, y, 1);
+ gameWorld.setForeMap(0, y, 1);
break;
}
- if (GP.world.hasForeAt(x, y) && GP.world.getForeMapBlock(x, y).hasCollision()) {
+ if (gameWorld.hasForeAt(0, y) && gameWorld.getForeMapBlock(0, y).hasCollision()) {
break;
}
}
- return new Vector2(x * 16 + 8 - getWidth() / 2, (float) y * 16 - getHeight());
+ return new Vector2(8 - getWidth() / 2, (float) y * 16 - getHeight());
}
public void setDir(Direction dir) {
}
@Override
- public void ai() {
+ public void ai(GameWorld gameWorld) {
}
@Override
@Override
public void draw(SpriteBatch spriteBatch, float x, float y) {
- if (move.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
- Assets.playerSprite[0][2].rotate(animDelta);
- Assets.playerSprite[1][2].rotate(-animDelta);
- Assets.playerSprite[0][3].rotate(-animDelta);
- Assets.playerSprite[1][3].rotate(animDelta);
+ if (mMove.x != 0 || Assets.playerSprite[0][2].getRotation() != 0) {
+ Assets.playerSprite[0][2].rotate(mAnimDelta);
+ Assets.playerSprite[1][2].rotate(-mAnimDelta);
+ Assets.playerSprite[0][3].rotate(-mAnimDelta);
+ Assets.playerSprite[1][3].rotate(mAnimDelta);
} else {
Assets.playerSprite[0][2].setRotation(0);
Assets.playerSprite[1][2].setRotation(0);
Assets.playerSprite[1][3].setRotation(0);
}
if (Assets.playerSprite[0][2].getRotation() >= 60 || Assets.playerSprite[0][2].getRotation() <= -60) {
- animDelta = -animDelta;
+ mAnimDelta = -mAnimDelta;
}
//back hand
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Block.java
index b6628cb20a1f283ab17f63a61e833a79461e9032..0800b5b5cb8d8f713aef6e017a4c7af355675c79 100644 (file)
private final int hp;
private final String drop;
private final String meta;
+ @CheckForNull
private final Sprite tex;
private final boolean coll;
return !drop.equals("none");
}
- public Sprite getTex() {
+ public Sprite getTexture() {
+ assert tex != null;
return tex;
}
- public Rectangle getRect(int x, int y) {
+ public Rectangle getRectangle(int x, int y) {
x *= 16;
y *= 16;
return new Rectangle(x + this.x, y + this.y, w, h);
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.java
index e523c4e9d6b79d245b20f12896373086c85d2459..3b5c76031d5a41b8a78861f48b2648441431cbcb 100644 (file)
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavedroid.game.GameWorld;
import ru.deadsoftware.cavedroid.game.mobs.Player;
import java.io.Serializable;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
public class Drop extends Rectangle implements Serializable {
private final int id;
private final Vector2 move;
private boolean pickedUp = false;
- public Drop(float x, float y, int id) {
+ Drop(float x, float y, int id) {
super(x, y, 8, 8);
this.id = id;
this.move = new Vector2(0, -1);
return move;
}
- public int closeToPlayer() {
+ public int closeToPlayer(GameWorld gameWorld, Player player) {
boolean[] c = new boolean[3];
- c[0] = Intersector.overlaps(new Rectangle(GP.player.getX() - 16,
- GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), this);
- c[1] = Intersector.overlaps(new Rectangle((GP.player.getX() + GP.world.getWidthPx()) - 16,
- GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), this);
- c[2] = Intersector.overlaps(new Rectangle((GP.player.getX() - GP.world.getWidthPx()) - 16,
- GP.player.getY() - 16, GP.player.getWidth() + 32, GP.player.getHeight() + 32), this);
+ c[0] = Intersector.overlaps(new Rectangle(player.getX() - 16,
+ player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
+ c[1] = Intersector.overlaps(new Rectangle((player.getX() + gameWorld.getWidthPx()) - 16,
+ player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
+ c[2] = Intersector.overlaps(new Rectangle((player.getX() - gameWorld.getWidthPx()) - 16,
+ player.getY() - 16, player.getWidth() + 32, player.getHeight() + 32), this);
for (int i = 0; i < 3; i++) {
if (c[i]) {
return 0;
}
- public void moveToPlayer() {
- int ctp = closeToPlayer();
+ public void moveToPlayer(GameWorld gameWorld, Player player, int ctp) {
if (ctp > 0) {
- float px = GP.player.getX();
- float py = GP.player.getY();
+ float px = player.getX();
+ float py = player.getY();
switch (ctp) {
case 2:
- px += GP.world.getWidthPx();
+ px += gameWorld.getWidthPx();
break;
case 3:
- px -= GP.world.getWidthPx();
+ px -= gameWorld.getWidthPx();
break;
}
float dx = 0, dy = 0;
- if (px + GP.player.getWidth() < x + 4) {
+ if (px + player.getWidth() < x + 4) {
dx = -.5f;
} else if (px > x + 4) {
dx = .5f;
}
- if (py + GP.player.getHeight() < y + 4) {
+ if (py + player.getHeight() < y + 4) {
dy = -.5f;
} else if (py > y + 4) {
dy = .5f;
}
private void checkWorldBounds() {
- if (x + 8 > GP.world.getWidthPx()) {
- x -= GP.world.getWidthPx();
- } else if (x < 0) {
- x += GP.world.getWidthPx();
- }
+// if (x + 8 > world.getWidthPx()) {
+// x -= world.getWidthPx();
+// } else if (x < 0) {
+// x += world.getWidthPx();
+// }
}
public void move() {
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java b/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java
--- /dev/null
@@ -0,0 +1,33 @@
+package ru.deadsoftware.cavedroid.game.objects;
+
+import ru.deadsoftware.cavedroid.game.GameScope;
+
+import javax.inject.Inject;
+import java.util.LinkedList;
+
+@GameScope
+public class DropController {
+
+ public interface Callback {
+ void run(Drop drop);
+ }
+
+ private final LinkedList<Drop> mDrops = new LinkedList<>();
+
+ @Inject
+ public DropController() {
+ }
+
+ public void addDrop(float x, float y, int id) {
+ mDrops.add(new Drop(x, y, id));
+ }
+
+ public int getSize() {
+ return mDrops.size();
+ }
+
+ public void forEach(Callback callback) {
+ mDrops.forEach(callback::run);
+ }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.java b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.java
index 9868a96b18d0d0dfe4a2c502cf29c50f7193f96a..8e664ace9bf0a134783d92e7bca5781eb91ba41d 100644 (file)
private final String name;
private final String type;
+ @CheckForNull
private final Sprite tex;
public Item(String name, String type, @CheckForNull Sprite tex) {
}
}
- public Sprite getTex() {
+ public Sprite getTexture() {
+ assert tex != null;
return tex;
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java b/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java
index 3cf44832f4580b28108050ff4ff5583808ec58e5..c33fcb5b2521d8d2f52a809df6dc12e87e10aa12 100644 (file)
public class TouchButton {
- private Rectangle rect;
- private int code;
- private boolean mouse;
+ private final Rectangle rect;
+ private final int code;
+ private final boolean mouse;
public TouchButton(Rectangle rect, int code, boolean mouse) {
this.rect = rect;
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java b/core/src/ru/deadsoftware/cavedroid/menu/MenuProc.java
index 989a5d1d35bdf431aa0b7354d21711a91934da4a..8bc9dd6615f37fec87e79aa0acda38bdd4bb1ac4 100644 (file)
package ru.deadsoftware.cavedroid.menu;
+import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.ObjectMap;
import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.game.GameScreen;
import ru.deadsoftware.cavedroid.menu.objects.Button;
import ru.deadsoftware.cavedroid.menu.submenus.Menu;
import ru.deadsoftware.cavedroid.menu.submenus.MenuMain;
private Menu currentMenu;
public MenuProc(int width) {
- super(width, width * GameScreen.getHeight() / GameScreen.getWidth());
+ super(width, width * Gdx.graphics.getHeight() / Gdx.graphics.getWidth());
mainMenu = new MenuMain(getWidth(), getHeight(), this::drawButton);
newGameMenu = new MenuNewGame(getWidth(), getHeight(), this::drawButton);
currentMenu = mainMenu;
}
private void update() {
- switch (CaveGame.MENU_STATE) {
- case MAIN:
- currentMenu = mainMenu;
- break;
- case NEW_GAME:
- currentMenu = newGameMenu;
- break;
- case LOADING:
- drawString("Generating World...");
- CaveGame.APP_STATE = AppState.GAME;
- CaveGame.GAME_STATE = GameState.PLAY;
- break;
- case SAVING:
- drawString("Saving Game...");
- CaveGame.APP_STATE = AppState.MENU;
- CaveGame.MENU_STATE = MenuState.MAIN;
- break;
- }
+// switch (CaveGame.MENU_STATE) {
+// case MAIN:
+// currentMenu = mainMenu;
+// break;
+// case NEW_GAME:
+// currentMenu = newGameMenu;
+// break;
+// case LOADING:
+// drawString("Generating World...");
+// CaveGame.APP_STATE = AppState.GAME;
+// CaveGame.GAME_STATE = GameState.PLAY;
+// break;
+// case SAVING:
+// drawString("Saving Game...");
+// CaveGame.APP_STATE = AppState.MENU;
+// CaveGame.MENU_STATE = MenuState.MAIN;
+// break;
+// }
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int mb) {
- screenX *= getWidth() / GameScreen.getWidth();
- screenY *= getHeight() / GameScreen.getHeight();
+ screenX *= getWidth() / Gdx.graphics.getWidth();
+ screenY *= getHeight() / Gdx.graphics.getHeight();
for (ObjectMap.Entry<String, Button> entry : currentMenu.getButtons()) {
Button button = entry.value;
if (button.getRect().contains(screenX, screenY)) {
}
@Override
- public void render() {
+ public void render(float delta) {
update();
spriter.begin();
currentMenu.draw(spriter);
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/Menu.java
index dcb06d3d937877a2cf3aa8ea4dd8daf05ad119f9..2a89e04608f3064a170bdb69c363a99deb84d23d 100644 (file)
import com.badlogic.gdx.utils.ArrayMap;
import com.badlogic.gdx.utils.JsonValue;
import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.game.GameScreen;
import ru.deadsoftware.cavedroid.menu.objects.Button;
import ru.deadsoftware.cavedroid.menu.objects.ButtonEventListener;
import ru.deadsoftware.cavedroid.menu.objects.ButtonRenderer;
}
spriter.draw(gamelogo, width / 2 - (float) gamelogo.getRegionWidth() / 2, 8);
- float inputX = Gdx.input.getX() * width / GameScreen.getWidth();
- float inputY = Gdx.input.getY() * height / GameScreen.getHeight();
+ float inputX = Gdx.input.getX() * width / Gdx.graphics.getWidth();
+ float inputY = Gdx.input.getY() * height / Gdx.graphics.getHeight();
for (Button button : buttons.values()) {
if (button.getType() > 0) {
- if (button.getRect().contains(inputX, inputY) && (!CaveGame.TOUCH || Gdx.input.isTouched())) {
+ if (button.getRect().contains(inputX, inputY) && (/*!CaveGame.TOUCH || */Gdx.input.isTouched())) {
button.setType(2);
} else {
button.setType(1);
diff --git a/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java b/core/src/ru/deadsoftware/cavedroid/menu/submenus/MenuInput.java
index 2ebe4271428c060b173552777d4366fe384c66b9..eccf9af91acfc7129b018245099222ec70852405 100644 (file)
import com.badlogic.gdx.Gdx;
import ru.deadsoftware.cavedroid.CaveGame;
-import ru.deadsoftware.cavedroid.game.GameProc;
import ru.deadsoftware.cavedroid.game.GameSaver;
import ru.deadsoftware.cavedroid.misc.states.AppState;
import ru.deadsoftware.cavedroid.misc.states.MenuState;
-import static ru.deadsoftware.cavedroid.GameScreen.GP;
-
class MenuInput {
private static void startNewGame(int gameMode) {
- GP = new GameProc(gameMode);
- GP.player.respawn();
- GameSaver.save(GP);
- CaveGame.APP_STATE = AppState.LOAD;
+// GP = new GameProc();
+// GP.player.respawn();
+// GameSaver.save(GP);
+// CaveGame.APP_STATE = AppState.LOAD;
}
static void newGameClicked() {
- CaveGame.MENU_STATE = MenuState.NEW_GAME;
+// CaveGame.MENU_STATE = MenuState.NEW_GAME;
}
static void loadGameClicked() {
- CaveGame.APP_STATE = AppState.LOAD;
- GP = GameSaver.load();
+// CaveGame.APP_STATE = AppState.LOAD;
+// GP = GameSaver.load();
}
static void quitClicked() {
}
static void backClicked() {
- CaveGame.MENU_STATE = MenuState.MAIN;
+// CaveGame.MENU_STATE = MenuState.MAIN;
}
}
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Renderer.java b/core/src/ru/deadsoftware/cavedroid/misc/Renderer.java
index a6154eed85952aa8889028b8b942e96987d403e6..b59e977c5045c05195f72e870c2c3c0b82d69335 100644 (file)
package ru.deadsoftware.cavedroid.misc;
+import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
-import ru.deadsoftware.cavedroid.GameScreen;
public abstract class Renderer implements InputProcessor {
private final OrthographicCamera camera;
protected Renderer() {
- this(GameScreen.getWidth(), GameScreen.getHeight());
+ this(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
protected Renderer(float width, float height) {
getHeight() / 2 - (float) Assets.getStringHeight(str) / 2);
}
- public abstract void render();
+ public abstract void render(float delta);
@Override
public boolean keyDown(int keycode) {