{
- "up": {
- "x": 26,
- "y": -52,
- "w": 26,
- "h": 26,
- "key": "W"
- },
- "down": {
- "x": 26,
- "y": -26,
- "w": 26,
- "h": 26,
- "key": "S"
- },
- "left": {
- "x": 0,
- "y": -26,
- "w": 26,
- "h": 26,
- "key": "A"
- },
- "right": {
- "x": 52,
- "y": -26,
- "w": 26,
- "h": 26,
- "key": "D"
- },
"alt": {
- "x": 78,
- "y": -26,
- "w": 26,
- "h": 26,
+ "x": -32,
+ "y": -32,
+ "w": 32,
+ "h": 32,
"key": "L-Alt"
- },
- "lmb": {
- "x": -52,
- "y": -26,
- "w": 26,
- "h": 26,
- "mouse": true,
- "key": "Left"
- },
- "rmb": {
- "x": -26,
- "y": -26,
- "w": 26,
- "h": 26,
- "mouse": true,
- "key": "Right"
}
}
\ No newline at end of file
package ru.deadsoftware.cavedroid;
import ru.deadsoftware.cavedroid.game.GameUiWindow;
+import ru.deadsoftware.cavedroid.game.input.Joystick;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
@CheckForNull
private MainComponent mMainComponent;
+ @CheckForNull
+ private Joystick mJoystick;
+
private GameUiWindow mGameUiWindow;
private String mGameFolder;
public void setAssetsPackPath(@Nullable String assetsPackPath) {
mAssetsPackPath = assetsPackPath;
}
+
+ @CheckForNull
+ public Joystick getJoystick() {
+ return mJoystick;
+ }
+
+ public void setJoystick(@CheckForNull Joystick joystick) {
+ mJoystick = joystick;
+ }
}
package ru.deadsoftware.cavedroid.game;
import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
-import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.ObjectMap;
import ru.deadsoftware.cavedroid.MainConfig;
import ru.deadsoftware.cavedroid.game.input.IGameInputHandler;
+import ru.deadsoftware.cavedroid.game.input.Joystick;
import ru.deadsoftware.cavedroid.game.input.action.KeyboardInputAction;
import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction;
import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey;
import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager;
import ru.deadsoftware.cavedroid.misc.Assets;
import ru.deadsoftware.cavedroid.misc.Renderer;
+import ru.deadsoftware.cavedroid.misc.utils.RenderingUtilsKt;
+import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
import javax.annotation.CheckForNull;
import javax.inject.Inject;
@GameScope
public class GameRenderer extends Renderer {
+ private static final float DRAG_THRESHOLD = 1f;
private static final TouchButton nullButton = new TouchButton(null, -1, true);
private final MainConfig mMainConfig;
private final GameWindowsManager mGameWindowsManager;
private final TooltipManager mTooltipManager;
+ private final TouchButton mouseLeftTouchButton, mouseRightTouchButton;
+
@Inject
GameRenderer(MainConfig mainConfig,
MobsController mobsController,
mGameWindowsManager = gameWindowsManager;
mTooltipManager = tooltipManager;
+ mouseLeftTouchButton = new TouchButton(new Rectangle(getWidth() / 2, 0f, getWidth() / 2, getHeight() / 2), Input.Buttons.LEFT, true);
+ mouseRightTouchButton = new TouchButton(new Rectangle(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2), Input.Buttons.RIGHT, true);
+
+ mMainConfig.setJoystick(new Joystick(mMobsController.getPlayer().getSpeed()));
+
Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
}
mCursorMouseInputHandler.handle(action);
if (!mTooltipManager.getCurrentMouseTooltip().isEmpty()) {
- final Label.LabelStyle style = new Label.LabelStyle(Assets.minecraftFont, Color.WHITE);
- style.background = new TextureRegionDrawable(Assets.textureRegions.get("background"));
- final Label label = new Label(mTooltipManager.getCurrentMouseTooltip(), style);
- label.setX(screenX);
- label.setY(screenY);
-// label.setHeight(10f);
-// label.setAlignment(Align.left, Align.top);
- label.draw(spriter, 1f);
+ RenderingUtilsKt.drawString(spriter, mTooltipManager.getCurrentMouseTooltip(), screenX + 1, screenY + 1, Color.BLACK);
+ RenderingUtilsKt.drawString(spriter, mTooltipManager.getCurrentMouseTooltip(), screenX, screenY, Color.WHITE);
}
}
return anyProcessed;
}
- private boolean onMouseActionEvent(int mouseX, int mouseY, int button, boolean touchUp) {
+ private boolean onMouseActionEvent(int mouseX, int mouseY, int button, boolean touchUp, int pointer) {
@CheckForNull MouseInputAction action = mMouseInputActionMapper
- .map((float) mouseX, (float) mouseY, getCameraViewport(), button, touchUp);
+ .map((float) mouseX, (float) mouseY, getCameraViewport(), button, touchUp, pointer);
return handleMouseAction(action);
}
if (mMainConfig.isTouch()) {
TouchButton touchedKey = getTouchedKey(touchX, touchY);
if (touchedKey.isMouse()) {
- return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), true);
+ return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), true, pointer);
} else {
return keyUp(touchedKey.getCode());
}
}
- return onMouseActionEvent(screenX, screenY, button, true);
+ return onMouseActionEvent(screenX, screenY, button, true, pointer);
}
private TouchButton getTouchedKey(float touchX, float touchY) {
return button;
}
}
+
+ if (mouseLeftTouchButton.getRect().contains(touchX, touchY)) {
+ return mouseLeftTouchButton;
+ }
+
+ if (mouseRightTouchButton.getRect().contains(touchX, touchY)) {
+ return mouseRightTouchButton;
+ }
+
return nullButton;
}
if (mMainConfig.isTouch()) {
TouchButton touchedKey = getTouchedKey(touchX, touchY);
if (touchedKey.isMouse()) {
- return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), false);
+ return onMouseActionEvent(screenX, screenY, touchedKey.getCode(), false, pointer);
} else {
return keyDown(touchedKey.getCode());
}
}
- return onMouseActionEvent(screenX, screenY, button, false);
+ return onMouseActionEvent(screenX, screenY, button, false, pointer);
}
@Override
float touchX = transformScreenX(screenX);
float touchY = transformScreenY(screenY);
- if (Math.abs(touchX - mTouchDownX) < 16 && Math.abs(touchY - mTouchDownY) < 16) {
+ if (Math.abs(touchX - mTouchDownX) < 16 && Math.abs(touchY - mTouchDownY) < DRAG_THRESHOLD) {
return false;
}
@CheckForNull MouseInputAction action =
- mMouseInputActionMapper.mapDragged(screenX, screenY, getCameraViewport());
+ mMouseInputActionMapper.mapDragged(screenX, screenY, getCameraViewport(), pointer);
return handleMouseAction(action);
}
public void render(float delta) {
updateCameraPosition();
+ if (mMainConfig.getJoystick() != null && mMainConfig.getJoystick().getActive()) {
+ mMainConfig.getJoystick().updateState(
+ transformScreenX(Gdx.input.getX(mMainConfig.getJoystick().getPointer())),
+ transformScreenY(Gdx.input.getY(mMainConfig.getJoystick().getPointer()))
+ );
+ }
+
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriter.begin();
--- /dev/null
+package ru.deadsoftware.cavedroid.game.input
+
+import com.badlogic.gdx.math.Vector2
+import com.badlogic.gdx.utils.TimeUtils
+
+class Joystick(
+ private val value: Float,
+) {
+
+ var active = false
+ private set
+ var centerX = 0f
+ private set
+ var centerY = 0f
+ private set
+
+ var activeX = 0f
+ private set
+ var activeY = 0f
+ private set
+
+ var pointer = 0
+ private set
+
+ private val stickVector = Vector2()
+
+ private var activateTimeMs = 0L
+
+ fun activate(touchX: Float, touchY: Float, pointer: Int) {
+ active = true
+ centerX = touchX
+ centerY = touchY
+ activateTimeMs = TimeUtils.millis()
+ this.pointer = pointer
+ }
+
+ fun deactivate() {
+ active = false
+ }
+
+ fun getVelocityVector(): Vector2 {
+ if (!active) {
+ return Vector2.Zero
+ }
+ println(stickVector)
+ return Vector2(
+ stickVector.x * value,
+ stickVector.y * value
+ )
+ }
+
+ fun updateState(touchX: Float, touchY: Float) {
+ if (!active) {
+ return
+ }
+
+ stickVector.x = touchX - centerX
+ stickVector.y = touchY - centerY
+ stickVector.clamp(0f, RADIUS)
+
+ activeX = centerX + stickVector.x
+ activeY = centerY + stickVector.y
+
+ stickVector.x /= RADIUS
+ stickVector.y /= RADIUS
+ }
+
+ companion object {
+ const val RADIUS = 24f
+ const val SIZE = RADIUS * 2
+ const val STICK_SIZE = 16f
+ }
+
+}
\ No newline at end of file
import dagger.multibindings.IntoSet
import ru.deadsoftware.cavedroid.game.GameScope
import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
+import ru.deadsoftware.cavedroid.game.input.handler.touch.JoystickInputHandler
import ru.deadsoftware.cavedroid.game.input.handler.mouse.*
@Module
fun bindSelectCraftingInventoryItemMouseInputHandler(handler: SelectCraftingInventoryItemMouseInputHandler): IGameInputHandler<MouseInputAction> {
return handler
}
+
+ @Binds
+ @IntoSet
+ @GameScope
+ fun bindJoystickInputHandler(handler: JoystickInputHandler): IGameInputHandler<MouseInputAction> {
+ return handler
+ }
}
\ No newline at end of file
val touchUp: Boolean
+ sealed interface Touch : MouseInputActionKey {
+ val pointer: Int
+ }
+
data object None : MouseInputActionKey {
override val touchUp: Boolean
get() = throw IllegalAccessException("not applicable for mouse move action")
}
- data object Dragged : MouseInputActionKey {
+ data class Dragged(
+ override val pointer: Int
+ ) : Touch {
override val touchUp: Boolean
get() = throw IllegalAccessException("not applicable for mouse dragged action")
}
override val touchUp: Boolean
) : MouseInputActionKey
- data class Touch(
- override val touchUp: Boolean
- ) : MouseInputActionKey
+ data class Screen(
+ override val touchUp: Boolean,
+ override val pointer: Int,
+ ) : Touch
data class Scroll(
val amountX: Float,
package ru.deadsoftware.cavedroid.game.input.handler.keyboard
-import com.badlogic.gdx.math.MathUtils
import ru.deadsoftware.cavedroid.MainConfig
import ru.deadsoftware.cavedroid.game.GameScope
import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
action.actionKey is KeyboardInputActionKey.Down)
}
- private fun checkCursorBounds() {
- val player = mobsController.player
- if (player.gameMode == 0) {
- val minCursorX = player.mapX - SURVIVAL_CURSOR_RANGE
- val maxCursorX = player.mapX + SURVIVAL_CURSOR_RANGE
- val minCursorY = player.middleMapY - SURVIVAL_CURSOR_RANGE
- val maxCursorY = player.middleMapY + SURVIVAL_CURSOR_RANGE
-
- player.cursorX = MathUtils.clamp(player.cursorX, minCursorX, maxCursorX)
- player.cursorY = MathUtils.clamp(player.cursorY, minCursorY, maxCursorY)
- }
-
- player.cursorY = MathUtils.clamp(player.cursorY, 0, gameWorld.height - 1)
- }
-
override fun handle(action: KeyboardInputAction) {
val player = mobsController.player
else -> return
}
- checkCursorBounds()
+ player.checkCursorBounds(gameWorld);
}
companion object {
override fun checkConditions(action: MouseInputAction): Boolean {
return gameWindowsManager.getCurrentWindow() != GameUiWindow.NONE &&
- (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) &&
+ (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) &&
!action.actionKey.touchUp &&
!isInsideWindow(action, getCurrentWindowTexture())
}
}
private fun checkStartDragConditions(action: MouseInputAction): Boolean {
- return (action.actionKey is MouseInputActionKey.Touch) &&
+ return (action.actionKey is MouseInputActionKey.Screen) &&
!action.actionKey.touchUp && !gameWindowsManager.isDragging
}
private fun checkEndDragConditions(action: MouseInputAction): Boolean {
- return action.actionKey is MouseInputActionKey.Touch &&
+ return action.actionKey is MouseInputActionKey.Screen &&
action.actionKey.touchUp && gameWindowsManager.isDragging
}
override fun handle(action: MouseInputAction) {
when (action.actionKey) {
- is MouseInputActionKey.Touch -> handleStartOrEndDrag(action)
+ is MouseInputActionKey.Screen -> handleStartOrEndDrag(action)
is MouseInputActionKey.Dragged -> handleDrag(action)
is MouseInputActionKey.Scroll -> handleScroll(action)
else -> return
private fun GameWorld.isCurrentBlockAutoselectable() =
getForeMap(player.cursorX, player.cursorY).isAutoselectable
- private fun checkCursorBounds() {
- if (player.gameMode == 0) {
- val minCursorX = player.mapX - SURVIVAL_CURSOR_RANGE
- val maxCursorX = player.mapX + SURVIVAL_CURSOR_RANGE
- val minCursorY = player.middleMapY - SURVIVAL_CURSOR_RANGE
- val maxCursorY = player.middleMapY + SURVIVAL_CURSOR_RANGE
-
- player.cursorX = MathUtils.clamp(player.cursorX, minCursorX, maxCursorX)
- player.cursorY = MathUtils.clamp(player.cursorY, minCursorY, maxCursorY)
- }
-
- player.cursorY = MathUtils.clamp(player.cursorY, 0, gameWorld.height - 1)
- }
-
private fun setPlayerDirectionToCursor() {
if (player.controlMode != Player.ControlMode.CURSOR) {
return
!mainConfig.isTouch -> handleMouse(action)
}
- checkCursorBounds()
+ player.checkCursorBounds(gameWorld)
setPlayerDirectionToCursor()
if (player.cursorX != pastCursorX || player.cursorY != pastCursorY) {
override fun checkConditions(action: MouseInputAction): Boolean {
return buttonHoldTask?.isScheduled == true ||
- ((action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch)
+ ((action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen)
&& isInsideHotbar(action)
|| action.actionKey is MouseInputActionKey.Scroll) &&
gameWindowsManager.getCurrentWindow() == GameUiWindow.NONE
cancelHold()
}
- if (action.actionKey !is MouseInputActionKey.Left && action.actionKey !is MouseInputActionKey.Touch ) {
+ if (action.actionKey !is MouseInputActionKey.Left && action.actionKey !is MouseInputActionKey.Screen ) {
if (action.actionKey is MouseInputActionKey.Scroll) {
handleScroll(action)
}
override fun checkConditions(action: MouseInputAction): Boolean {
return gameWindowsManager.getCurrentWindow() == GameUiWindow.CRAFTING_TABLE &&
isInsideWindow(action, survivalWindowTexture) &&
- (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Touch)
+ (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
&& action.actionKey.touchUp
}
itemIndex -= 36
}
- if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
+ if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
} else {
onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize
- if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
+ if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
onLeftCLick(window.craftingItems, window, index)
} else {
onRightClick(window.craftingItems, window, index)
override fun checkConditions(action: MouseInputAction): Boolean {
return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
!gameWindowsManager.isDragging &&
- (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) &&
+ (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) &&
action.actionKey.touchUp && isInsideWindow(action, creativeInventoryTexture)
}
override fun checkConditions(action: MouseInputAction): Boolean {
return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
isInsideWindow(action, survivalWindowTexture) &&
- (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Touch)
+ (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
&& action.actionKey.touchUp
}
itemIndex -= 36
}
- if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
+ if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
} else {
onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
- if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
+ if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
onLeftCLick(window.craftingItems, window, index)
} else {
onRightClick(window.craftingItems, window, index)
--- /dev/null
+package ru.deadsoftware.cavedroid.game.input.handler.touch
+
+import com.badlogic.gdx.math.Vector2
+import com.badlogic.gdx.utils.TimeUtils
+import ru.deadsoftware.cavedroid.MainConfig
+import ru.deadsoftware.cavedroid.game.GameScope
+import ru.deadsoftware.cavedroid.game.GameUiWindow
+import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
+import ru.deadsoftware.cavedroid.game.input.Joystick
+import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
+import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
+import ru.deadsoftware.cavedroid.game.input.isInsideHotbar
+import ru.deadsoftware.cavedroid.game.mobs.Mob
+import ru.deadsoftware.cavedroid.game.mobs.MobsController
+import ru.deadsoftware.cavedroid.game.mobs.player.Player
+import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
+import ru.deadsoftware.cavedroid.game.world.GameWorld
+import javax.inject.Inject
+
+@GameScope
+class JoystickInputHandler @Inject constructor(
+ private val mainConfig: MainConfig,
+ private val mobsController: MobsController,
+ private val gameWindowsManager: GameWindowsManager,
+ private val gameWorld: GameWorld,
+) : IGameInputHandler<MouseInputAction> {
+
+ private var activateTimeMs = 0L
+ private var cursorTimeoutMs = 100L
+
+ private var active = false
+ set(value) {
+ if (!value) {
+ resetVelocity()
+ if (TimeUtils.timeSinceMillis(activateTimeMs) < 100L &&
+ mobsController.player.controlMode != Player.ControlMode.CURSOR) {
+ mobsController.player.jump()
+ }
+ } else {
+ activateTimeMs = TimeUtils.millis()
+ }
+ field = value
+ }
+
+ private fun resetVelocity() {
+ mobsController.player.velocity.x = 0f
+
+ if (mobsController.player.isFlyMode) {
+ mobsController.player.velocity.y = 0f
+ }
+ }
+
+ override fun checkConditions(action: MouseInputAction): Boolean {
+ return gameWindowsManager.getCurrentWindow() == GameUiWindow.NONE &&
+ mainConfig.isTouch &&
+// mobsController.player.controlMode == Player.ControlMode.WALK &&
+ mainConfig.joystick != null &&
+ (action.actionKey is MouseInputActionKey.Touch) &&
+ (action.actionKey.pointer == mainConfig.joystick?.pointer || !active) &&
+ ((action.actionKey is MouseInputActionKey.Dragged) ||
+ (action.screenX < action.cameraViewport.width / 2 && !action.actionKey.touchUp || active)) &&
+ !(action.actionKey is MouseInputActionKey.Screen && isInsideHotbar(action))
+
+ }
+
+ private fun handleTouchDown(action: MouseInputAction) {
+ val key = action.actionKey as MouseInputActionKey.Screen
+ mainConfig.joystick?.activate(action.screenX, action.screenY, key.pointer) ?: return
+ active = true
+ }
+
+ private fun handleTouchUp(action: MouseInputAction) {
+ mainConfig.joystick?.deactivate()
+ active = false
+ }
+
+ private fun handleCursor() {
+ val joystick = mainConfig.joystick ?: return
+
+ if (TimeUtils.timeSinceMillis(cursorTimeoutMs) < 200L) {
+ return
+ }
+
+ if (Math.abs(joystick.activeX - joystick.centerX) >= Joystick.RADIUS / 2) {
+ mobsController.player.cursorX += if (joystick.activeX > joystick.centerX) 1 else -1
+ cursorTimeoutMs = TimeUtils.millis()
+ }
+
+ if (Math.abs(joystick.activeY - joystick.centerY) >= Joystick.RADIUS / 2) {
+ mobsController.player.cursorY += if (joystick.activeY > joystick.centerY) 1 else -1
+ cursorTimeoutMs = TimeUtils.millis()
+ }
+
+ mobsController.player.checkCursorBounds(gameWorld)
+ }
+
+ private fun handleDragged() {
+ if (mobsController.player.controlMode == Player.ControlMode.CURSOR) {
+ handleCursor()
+ return
+ }
+
+ val joystick = mainConfig.joystick ?: return
+ val joyVector = joystick.getVelocityVector()
+
+ mobsController.player.velocity.x = joyVector.x
+
+ mobsController.player.setDir(
+ if (joyVector.x < 0) {
+ Mob.Direction.LEFT
+ } else {
+ Mob.Direction.RIGHT
+ }
+ )
+
+ if (mobsController.player.isFlyMode) {
+ mobsController.player.velocity.y = joyVector.y
+ }
+ }
+
+ override fun handle(action: MouseInputAction) {
+ when (action.actionKey) {
+ is MouseInputActionKey.Dragged -> handleDragged()
+ else -> {
+ if (action.actionKey.touchUp) {
+ handleTouchUp(action)
+ } else {
+ handleTouchDown(action)
+ }
+ }
+ }
+ }
+
+}
\ No newline at end of file
mouseY: Float,
cameraViewport: Rectangle,
button: Int,
- touchUp: Boolean
+ touchUp: Boolean,
+ pointer: Int,
): MouseInputAction? {
- val actionKey = mapActionKey(button, touchUp) ?: return null
+ val actionKey = mapActionKey(button, touchUp, pointer) ?: return null
return MouseInputAction(
screenX = getScreenX(mouseX),
mouseX: Float,
mouseY: Float,
cameraViewport: Rectangle,
+ pointer: Int,
): MouseInputAction {
return MouseInputAction(
screenX = getScreenX(mouseX),
screenY = getScreenY(mouseY),
- actionKey = MouseInputActionKey.Dragged,
+ actionKey = MouseInputActionKey.Dragged(pointer),
cameraViewport = cameraViewport,
)
}
)
}
- private fun mapActionKey(button: Int, touchUp: Boolean): MouseInputActionKey? {
+ private fun mapActionKey(button: Int, touchUp: Boolean, pointer: Int): MouseInputActionKey? {
return when (button) {
Input.Buttons.LEFT -> MouseInputActionKey.Left(touchUp)
Input.Buttons.RIGHT -> MouseInputActionKey.Right(touchUp)
Input.Buttons.MIDDLE -> MouseInputActionKey.Middle(touchUp)
- -1 -> MouseInputActionKey.Touch(touchUp)
+ -1 -> MouseInputActionKey.Screen(touchUp, pointer)
else -> null
}
}
}
protected final void updateAnimation(float delta) {
- if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * delta) {
- mAnim += mAnimDelta * delta;
+ final float velocityMultiplier = (Math.abs(getVelocity().x) / getSpeed());
+ final float animMultiplier = (velocityMultiplier == 0f ? 1f : velocityMultiplier) * delta;
+ final float maxAnim = 60f * (velocityMultiplier == 0f ? 1f : velocityMultiplier);
+
+ if (mVelocity.x != 0f || Math.abs(mAnim) > mAnimDelta * animMultiplier) {
+ mAnim += mAnimDelta * animMultiplier;
} else {
mAnim = 0;
}
- if (mAnim > 60f) {
- mAnim = 60f;
+ if (mAnim > maxAnim) {
+ mAnim = maxAnim;
mAnimDelta = -ANIMATION_SPEED;
- } else if (mAnim < -60f) {
- mAnim = -60f;
+ } else if (mAnim < -maxAnim) {
+ mAnim = -maxAnim;
mAnimDelta = ANIMATION_SPEED;
}
private static final float SPEED = 69.072f;
private static final float JUMP_VELOCITY = -133.332f;
+ private static final int SURVIVAL_CURSOR_RANGE = 4;
public static final int MAX_HEALTH = 20;
public static final int INVENTORY_SIZE = 36;
@Override
public void jump() {
+ if (!canJump()) {
+ if (gameMode == 1) {
+ if (isFlyMode()) {
+ setFlyMode(false);
+ } else {
+ getVelocity().y = 0f;
+ setFlyMode(true);
+ }
+ }
+ return;
+ }
mVelocity.y = JUMP_VELOCITY;
}
super.heal(heal);
}
+ public void checkCursorBounds(GameWorld gameWorld) {
+ if (gameMode == 0) {
+ int minCursorX = getMapX() - SURVIVAL_CURSOR_RANGE;
+ int maxCursorX = getMapX() + SURVIVAL_CURSOR_RANGE;
+ int minCursorY = getMiddleMapY() - SURVIVAL_CURSOR_RANGE;
+ int maxCursorY = getMiddleMapY() + SURVIVAL_CURSOR_RANGE;
+
+ cursorX = MathUtils.clamp(cursorX, minCursorX, maxCursorX);
+ cursorY = MathUtils.clamp(cursorY, minCursorY, maxCursorY);
+ }
+
+ cursorY = MathUtils.clamp(cursorY, 0, gameWorld.getHeight() - 1);
+ }
+
private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) {
final Item item = inventory.getActiveItem().getItem();
import ru.deadsoftware.cavedroid.MainConfig
import ru.deadsoftware.cavedroid.game.GameScope
import ru.deadsoftware.cavedroid.game.GameUiWindow
+import ru.deadsoftware.cavedroid.game.input.Joystick
import ru.deadsoftware.cavedroid.game.mobs.MobsController
import ru.deadsoftware.cavedroid.game.mobs.player.Player.ControlMode
import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
import ru.deadsoftware.cavedroid.misc.Assets
import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component1
import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component2
+import ru.deadsoftware.cavedroid.misc.utils.drawSprite
import javax.inject.Inject
@GameScope
private val shadeTexture get() = Assets.textureRegions[SHADE_KEY]
+ private fun drawJoystick(spriteBatch: SpriteBatch) {
+ val joystick = mainConfig.joystick?.takeIf { it.active } ?: return
+
+ spriteBatch.drawSprite(
+ sprite = Assets.joyBackground,
+ x = joystick.centerX - Joystick.RADIUS,
+ y = joystick.centerY - Joystick.RADIUS,
+ width = Joystick.SIZE,
+ height = Joystick.SIZE
+ )
+
+ spriteBatch.drawSprite(
+ sprite = Assets.joyStick,
+ x = joystick.activeX - Joystick.STICK_SIZE / 2,
+ y = joystick.activeY - Joystick.STICK_SIZE / 2,
+ width = Joystick.STICK_SIZE,
+ height = Joystick.STICK_SIZE
+ )
+ }
+
override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
if (!mainConfig.isTouch || gameWindowsManager.getCurrentWindow() != GameUiWindow.NONE) {
return
val altKeyRect = touchControlsMap.get("alt").rect
spriteBatch.draw(shadeTexture, altKeyRect.x, altKeyRect.y, altKeyRect.width, altKeyRect.height)
}
+
+ drawJoystick(spriteBatch)
}
companion object {
public static Map<String, Texture> blockTextures = new HashMap<>();
public static Map<String, Texture> itemTextures = new HashMap<>();
+ public static Sprite joyBackground;
+ public static Sprite joyStick;
+
public static void dispose() {
minecraftFont.dispose();
loadedTextures.forEach(Texture::dispose);
loadAllPngsFromDirInto(blocksDir, blockTextures);
}
+ private static void loadJoystick(AssetLoader assetLoader) {
+ joyStick = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_stick.png")));
+ joyBackground = new Sprite(loadTexture(assetLoader.getAssetHandle("joy_background.png")));
+ }
+
public static void load(final AssetLoader assetLoader) {
loadMob(assetLoader, playerSprite, "char");
loadMob(assetLoader, pigSprite, "pig");
loadBlocks(assetLoader);
loadItems(assetLoader);
loadTouchButtonsFromJSON(assetLoader);
+ loadJoystick(assetLoader);
setPlayerHeadOrigin();
minecraftFont = new BitmapFont(assetLoader.getAssetHandle("font.fnt"), true);
minecraftFont.getData().setScale(.375f);