v0.8.0: added support for basic keyboard movement

This commit is contained in:
Evan Debenham 2019-10-26 19:26:48 -04:00
parent 8117d4ea2f
commit fcec1836b0
6 changed files with 116 additions and 14 deletions

View File

@ -79,22 +79,14 @@ public class InputHandler extends InputAdapter {
@Override
public synchronized boolean keyDown( int keyCode ) {
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
return false;
}
//TODO should check if key is mapped and ignore if it isn't?
KeyEvent.addKeyEvent( new KeyEvent(keyCode, true) );
return true;
}
@Override
public synchronized boolean keyUp( int keyCode ) {
if (keyCode != KeyEvent.BACK && keyCode != KeyEvent.MENU) {
return false;
}
//TODO should check if key is mapped and ignore if it isn't?
KeyEvent.addKeyEvent( new KeyEvent(keyCode, false) );
return true;
}

View File

@ -605,6 +605,7 @@ public class Hero extends Char {
lastAction = curAction;
}
curAction = null;
GameScene.resetKeyHold();
}
public void resume() {
@ -1559,6 +1560,11 @@ public class Hero extends Char {
super.onAttackComplete();
}
@Override
public void onMotionComplete() {
GameScene.checkKeyHold();
}
@Override
public void onOperateComplete() {

View File

@ -21,19 +21,23 @@
package com.shatteredpixel.shatteredpixeldungeon.scenes;
import com.badlogic.gdx.Input;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.SPDSettings;
import com.shatteredpixel.shatteredpixeldungeon.actors.Actor;
import com.shatteredpixel.shatteredpixeldungeon.actors.Char;
import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob;
import com.shatteredpixel.shatteredpixeldungeon.items.Heap;
import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite;
import com.shatteredpixel.shatteredpixeldungeon.tiles.DungeonTilemap;
import com.watabou.input.KeyEvent;
import com.watabou.input.PointerEvent;
import com.watabou.input.ScrollEvent;
import com.watabou.noosa.Camera;
import com.watabou.noosa.ScrollArea;
import com.watabou.utils.GameMath;
import com.watabou.utils.PointF;
import com.watabou.utils.Signal;
public class CellSelector extends ScrollArea {
@ -50,6 +54,7 @@ public class CellSelector extends ScrollArea {
dragThreshold = PixelScene.defaultZoom * DungeonTilemap.SIZE / 2;
mouseZoom = camera.zoom;
KeyEvent.addKeyListener(movementListener);
}
private float mouseZoom;
@ -202,6 +207,84 @@ public class CellSelector extends ScrollArea {
}
private KeyEvent heldKey = null;
private int heldKeyTurns = 0;
private Signal.Listener<KeyEvent> movementListener = new Signal.Listener<KeyEvent>() {
@Override
public boolean onSignal(KeyEvent event) {
if (!event.pressed){
if (heldKey != null && heldKey.code == event.code) {
resetKeyHold();
return true;
}
} else if (moveFromKey(event)) {
heldKey = event;
return true;
}
return false;
}
};
private boolean moveFromKey(KeyEvent event){
boolean moved = true;
int cell = Dungeon.hero.pos;
//TODO implement game actions, instead of using keys directly
switch (event.code){
case Input.Keys.NUMPAD_7:
cell += -1-Dungeon.level.width();
break;
case Input.Keys.UP: case Input.Keys.NUMPAD_8:
cell += -Dungeon.level.width();
break;
case Input.Keys.NUMPAD_9:
cell += +1-Dungeon.level.width();
break;
case Input.Keys.RIGHT: case Input.Keys.NUMPAD_6:
cell += +1;
break;
case Input.Keys.NUMPAD_3:
cell += +1+Dungeon.level.width();
break;
case Input.Keys.DOWN: case Input.Keys.NUMPAD_2:
cell += +Dungeon.level.width();
break;
case Input.Keys.NUMPAD_1:
cell += -1+Dungeon.level.width();
break;
case Input.Keys.LEFT: case Input.Keys.NUMPAD_4:
cell += -1;
break;
default:
moved = false;
}
if (moved){
//each step when keyboard moving takes 0.15s, 0.125s, 0.1s, 0.1s, ...
// this is to make it easier to move 1 or 2 steps without overshooting
CharSprite.setMoveInterval( CharSprite.DEFAULT_MOVE_INTERVAL +
Math.max(0, 0.05f - heldKeyTurns*0.025f));
select(cell);
}
return moved;
}
public void processKeyHold(){
if (heldKey != null){
enabled = true;
heldKeyTurns++;
moveFromKey(heldKey);
}
}
public void resetKeyHold(){
heldKey = null;
heldKeyTurns = 0;
CharSprite.setMoveInterval( CharSprite.DEFAULT_MOVE_INTERVAL );
}
public void cancel() {
if (listener != null) {
@ -227,7 +310,13 @@ public class CellSelector extends ScrollArea {
enabled = value;
}
}
@Override
public void destroy() {
super.destroy();
KeyEvent.removeKeyListener(movementListener);
}
public interface Listener {
void onSelect( Integer cell );
String prompt();

View File

@ -988,6 +988,14 @@ public class GameScene extends PixelScene {
QuickSlotButton.cancel();
if (scene != null && scene.toolbar != null) scene.toolbar.examining = false;
}
public static void checkKeyHold(){
cellSelector.processKeyHold();
}
public static void resetKeyHold(){
cellSelector.resetKeyHold();
}
public static void examineCell( Integer cell ) {
if (cell == null

View File

@ -65,7 +65,8 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
public static final int WARNING = 0xFF8800;
public static final int NEUTRAL = 0xFFFF00;
private static final float MOVE_INTERVAL = 0.1f;
public static final float DEFAULT_MOVE_INTERVAL = 0.1f;
private static float moveInterval = DEFAULT_MOVE_INTERVAL;
private static final float FLASH_INTERVAL = 0.05f;
//the amount the sprite is raised from flat when viewed in a raised perspective
@ -196,7 +197,7 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
play( run );
motion = new PosTweener( this, worldToCamera( to ), MOVE_INTERVAL );
motion = new PosTweener( this, worldToCamera( to ), moveInterval );
motion.listener = this;
parent.add( motion );
@ -208,6 +209,10 @@ public class CharSprite extends MovieClip implements Tweener.Listener, MovieClip
}
public static void setMoveInterval( float interval){
moveInterval = interval;
}
//returns where the center of this sprite will be after it completes any motion in progress
public PointF destinationCenter(){
if (motion != null){

View File

@ -166,7 +166,9 @@ public class Window extends Group implements Signal.Listener<KeyEvent> {
}
}
return false;
//TODO currently always eats the key event as windows always take full focus
// if they are ever made more flexible, might not want to do this in all cases
return true;
}
public void onBackPressed() {