summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7c52562)
raw | patch | inline | side by side (parent: 7c52562)
author | fred-boy <fredboy@protonmail.com> | |
Sun, 29 Sep 2019 06:01:07 +0000 (13:01 +0700) | ||
committer | fred-boy <fredboy@protonmail.com> | |
Sun, 29 Sep 2019 06:01:07 +0000 (13:01 +0700) |
index ef5c42e7ee7fde8d197a8c85d27b28d76c094432..cddaf534397ea531931a299d935742d52e4b3b82 100644 (file)
"x": 26,
"y": -52,
"w": 26,
- "h": 26
+ "h": 26,
+ "key": "W"
},
"down": {
"x": 26,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "key": "S"
},
"left": {
"x": 0,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "key": "A"
},
"right": {
"x": 52,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "key": "D"
},
"alt": {
"x": 78,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "key": "L-Alt"
},
"lmb": {
"x": -52,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "mouse": true,
+ "key": "Left"
},
"rmb": {
"x": -26,
"y": -26,
"w": 26,
- "h": 26
+ "h": 26,
+ "mouse": true,
+ "key": "Right"
}
}
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java
index df002fa8df09fd5d6e8836e5fb50e549f28c81ca..58d377885204430900497a5a5a5daf845e64b186 100644 (file)
return GameItems.isFluid(GP.world.getForeMap(GP.player.getMapX(), GP.player.getLowerMapY()));
}
+ private void goUpwards() {
+ if (checkSwim()) {
+ GP.player.swim = true;
+ } else if (GP.player.canJump) {
+ GP.player.mov.add(0, -7);
+ } else if (!GP.player.flyMode && GP.player.gameMode == 1) {
+ GP.player.flyMode = true;
+ GP.player.mov.y = 0;
+ } else if (GP.player.flyMode) {
+ GP.player.mov.y = -GamePhysics.PL_SPEED;
+ }
+ }
+
@SuppressWarnings("IntegerDivisionInFloatingPointContext")
private boolean insideCreativeInv(float screenX, float screenY) {
TextureRegion creative = Assets.textureRegions.get("creative");
switch (keycode) {
case Input.Keys.A:
GP.player.mov.x = -GamePhysics.PL_SPEED;
- GP.player.setDir(0);
+ GP.player.setDir(Mob.LEFT);
if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
break;
case Input.Keys.D:
GP.player.mov.x = GamePhysics.PL_SPEED;
- GP.player.setDir(1);
+ GP.player.setDir(Mob.RIGHT);
if (CaveGame.TOUCH && checkSwim()) GP.player.swim = true;
break;
+ case Input.Keys.W:
+ case Input.Keys.SPACE:
+ goUpwards();
+ break;
+ case Input.Keys.S:
+ case Input.Keys.CONTROL_LEFT:
+ GP.player.mov.y = GamePhysics.PL_SPEED;
+ break;
}
} else {
switch (keycode) {
public void keyDown(int keycode) {
keyDown = true;
keyDownCode = keycode;
- if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
- keycode == Input.Keys.S || keycode == Input.Keys.D) {
- wasdPressed(keycode);
- } else switch (keycode) {
+ switch (keycode) {
+ case Input.Keys.A:
+ case Input.Keys.D:
+ case Input.Keys.W:
+ case Input.Keys.S:
+ case Input.Keys.SPACE:
+ case Input.Keys.CONTROL_LEFT:
+ wasdPressed(keycode);
+ break;
+
case Input.Keys.ALT_LEFT:
if (CaveGame.TOUCH) {
GP.controlMode = GP.controlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
}
break;
- case Input.Keys.SPACE:
- if (checkSwim()) {
- GP.player.swim = true;
- } else if (GP.player.canJump) {
- GP.player.mov.add(0, -7);
- } else if (!GP.player.flyMode && GP.player.gameMode == 1) {
- GP.player.flyMode = true;
- GP.player.mov.y = 0;
- } else if (GP.player.flyMode) {
- GP.player.mov.y = -GamePhysics.PL_SPEED;
- }
- break;
-
- case Input.Keys.CONTROL_LEFT:
- GP.player.mov.y = GamePhysics.PL_SPEED;
- break;
-
case Input.Keys.E:
if (CaveGame.GAME_STATE == GameState.PLAY) {
switch (GP.player.gameMode) {
if (CaveGame.TOUCH && GP.player.swim) GP.player.swim = false;
break;
+ case Input.Keys.W:
+ case Input.Keys.S:
case Input.Keys.SPACE:
case Input.Keys.CONTROL_LEFT:
if (GP.player.flyMode) GP.player.mov.y = 0;
diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java b/core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
index b45479914086c5f8474023502866d4f295b3ddda..1f1858989ac0284644b0e725d2d3f433c4bf36fd 100644 (file)
private void drawTouchGui() {
for (int i = 0; i < guiMap.size; i++) {
- Rectangle touchKey = guiMap.getValueAt(i);
+ Rectangle touchKey = guiMap.getValueAt(i).getRect();
spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
touchKey.x, touchKey.y, touchKey.width, touchKey.height);
}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java b/core/src/ru/deadsoftware/cavedroid/game/objects/TouchButton.java
--- /dev/null
@@ -0,0 +1,29 @@
+package ru.deadsoftware.cavedroid.game.objects;
+
+import com.badlogic.gdx.math.Rectangle;
+
+public class TouchButton {
+
+ private Rectangle rect;
+ private int code;
+ private boolean mouse;
+
+ public TouchButton(Rectangle rect, int code, boolean mouse) {
+ this.rect = rect;
+ this.code = code;
+ this.mouse = mouse;
+ }
+
+ public Rectangle getRect() {
+ return rect;
+ }
+
+ public int getCode() {
+ return code;
+ }
+
+ public boolean isMouse() {
+ return mouse;
+ }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/Assets.java b/core/src/ru/deadsoftware/cavedroid/misc/Assets.java
index 93a5f86ea4a9509bfcd40f09f240db06a05ccb64..b01153a4c6f697c9d3251b921ea494977e0ef3f2 100644 (file)
import com.badlogic.gdx.utils.ArrayMap;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
import java.util.HashMap;
public static final Sprite[][] playerSprite = new Sprite[2][4];
public static final Sprite[][] pigSprite = new Sprite[2][2];
public static final HashMap<String, TextureRegion> textureRegions = new HashMap<>();
- public static final ArrayMap<String, Rectangle> guiMap = new ArrayMap<>();
+ public static final ArrayMap<String, TouchButton> guiMap = new ArrayMap<>();
public static final Sprite sandSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/sand.png"))));
public static final Sprite gravelSprite = flippedSprite(new Texture((Gdx.files.internal("textures/blocks/gravel.png"))));
diff --git a/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java b/core/src/ru/deadsoftware/cavedroid/misc/InputHandlerGame.java
index 31e6dd06919aa20cf1ca70e5ba2cc89d93c0e2ec..359ac98f311aa00a36297cecf1803f58e875a9e6 100644 (file)
import com.badlogic.gdx.utils.JsonValue;
import ru.deadsoftware.cavedroid.CaveGame;
import ru.deadsoftware.cavedroid.GameScreen;
+import ru.deadsoftware.cavedroid.game.objects.TouchButton;
+import static com.badlogic.gdx.utils.ObjectMap.Entry;
import static ru.deadsoftware.cavedroid.GameScreen.GP;
public class InputHandlerGame extends InputAdapter {
- private static final int
- UP = 0,
- DOWN = 1,
- LEFT = 2,
- RIGHT = 3,
- ALT = 4,
- LMB = 5,
- RMB = 6;
+ private static TouchButton nullButton = new TouchButton(null, -1, true);
public InputHandlerGame() {
loadTouchButtonsFromJSON();
}
+ private int getMouseKey(String name) {
+ switch (name) {
+ case "Left":
+ return Input.Buttons.LEFT;
+ case "Right":
+ return Input.Buttons.RIGHT;
+ case "Middle":
+ return Input.Buttons.MIDDLE;
+ case "Back":
+ return Input.Buttons.BACK;
+ case "Forward":
+ return Input.Buttons.FORWARD;
+ default:
+ return -1;
+ }
+ }
+
private void loadTouchButtonsFromJSON() {
JsonValue json = Assets.jsonReader.parse(Gdx.files.internal("json/touch_buttons.json"));
for (JsonValue key = json.child(); key != null; key = key.next()) {
float y = key.getFloat("y");
float w = key.getFloat("w");
float h = key.getFloat("h");
+ boolean mouse = Assets.getBooleanFromJson(key, "mouse", false);
+ String name = key.getString("key");
+ int code = mouse ? getMouseKey(name) : Input.Keys.valueOf(name);
if (x < 0) x = GP.renderer.getWidth() + x;
if (y < 0) y = GP.renderer.getHeight() + y;
- Assets.guiMap.put(key.name(), new Rectangle(x, y, w, h));
+ Assets.guiMap.put(key.name(), new TouchButton(new Rectangle(x, y, w, h), code, mouse));
}
}
return GP.renderer.getHeight() / GameScreen.getHeight() * screenY;
}
- private int getTouchedKey(float touchX, float touchY) {
- for (int i = 0; i < Assets.guiMap.size; i++) {
- if (Assets.guiMap.getValueAt(i).contains(touchX, touchY)) {
- return i;
+ private TouchButton getTouchedKey(float touchX, float touchY) {
+ for (Entry entry : Assets.guiMap) {
+ TouchButton button = (TouchButton) entry.value;
+ if (button.getRect().contains(touchX, touchY)) {
+ return button;
}
}
- return -1;
+ return nullButton;
}
@Override
float touchY = transformScreenY(screenY);
if (CaveGame.TOUCH) {
- int touchedKey = getTouchedKey(touchX, touchY);
- switch (touchedKey) {
- case UP:
- GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.W : Input.Keys.SPACE);
- break;
- case DOWN:
- GP.input.keyDown(GP.controlMode == ControlMode.CURSOR ? Input.Keys.S : Input.Keys.CONTROL_LEFT);
- break;
- case LEFT:
- GP.input.keyDown(Input.Keys.A);
- break;
- case RIGHT:
- GP.input.keyDown(Input.Keys.D);
- break;
- case ALT:
- GP.input.keyDown(Input.Keys.ALT_LEFT);
- break;
- case LMB:
- GP.input.touchDown(touchX, touchY, Input.Buttons.LEFT);
- break;
- case RMB:
- GP.input.touchDown(touchX, touchY, Input.Buttons.RIGHT);
- break;
- default:
- GP.input.touchDown(touchX, touchY, touchedKey);
+ TouchButton touchedKey = getTouchedKey(touchX, touchY);
+ if (touchedKey.isMouse()) {
+ GP.input.touchDown(touchX, touchY, touchedKey.getCode());
+ } else {
+ GP.input.keyDown(touchedKey.getCode());
}
} else {
GP.input.touchDown(touchX, touchY, button);
float touchY = transformScreenY(screenY);
if (CaveGame.TOUCH) {
- int touchedKey = getTouchedKey(touchX, touchY);
- switch (touchedKey) {
- case UP:
- case DOWN:
- case LEFT:
- case RIGHT:
- case ALT:
- GP.input.keyUp(GP.input.getKeyDownCode());
- break;
- case LMB:
- GP.input.touchUp(touchX, touchY, Input.Buttons.LEFT);
- break;
- case RMB:
- GP.input.touchUp(touchX, touchY, Input.Buttons.RIGHT);
- break;
- default:
- GP.input.touchUp(touchX, touchY, touchedKey);
+ TouchButton touchedKey = getTouchedKey(touchX, touchY);
+ if (touchedKey.isMouse()) {
+ GP.input.touchUp(touchX, touchY, touchedKey.getCode());
+ } else {
+ GP.input.keyUp(GP.input.getKeyDownCode());
}
} else {
GP.input.touchUp(touchX, touchY, button);
float touchX = transformScreenX(screenX);
float touchY = transformScreenY(screenY);
if (CaveGame.TOUCH && GP.input.isKeyDown()) {
- if (getTouchedKey(touchX, touchY) == -1) {
+ if (getTouchedKey(touchX, touchY).getCode() == -1) {
GP.input.keyUp(GP.input.getKeyDownCode());
}
} else {