standardize keyboard-based FlxCamera movement

This commit is contained in:
2022-06-21 20:14:48 +00:00
parent ca6a03cbeb
commit a6c8666160
7 changed files with 54 additions and 40 deletions

View File

@@ -16,5 +16,7 @@ import data.blades.ScenData;
import data.blades.Scenario;
import data.blades.TileMap;
using kiss_flixel.CameraTools;
@:build(kiss.Kiss.build())
class BladesMapState extends FlxState {}

View File

@@ -78,16 +78,10 @@
// F1 to toggle fullscreen:
(when FlxG.keys.justPressed.F1
(set FlxG.fullscreen !FlxG.fullscreen))
(let [&mut cameraMovement (new FlxVector 0 0)]
(when (or (= 0 FlxG.mouse.screenX) FlxG.keys.pressed.LEFT)
(set cameraMovement.x -1))
(when (or (= 0 FlxG.mouse.screenY) FlxG.keys.pressed.UP)
(set cameraMovement.y -1))
(when (or (= (- FlxG.width 1) FlxG.mouse.screenX) FlxG.keys.pressed.RIGHT)
(set cameraMovement.x 1))
(when (or (= (- FlxG.height 1) FlxG.mouse.screenY) FlxG.keys.pressed.DOWN)
(set cameraMovement.y 1))
(set cameraMovement (cameraMovement.truncate 1))
(set cameraMovement (cameraMovement.scale (* elapsed CAMERA_SPEED)))
(set FlxG.camera.scroll (FlxG.camera.scroll.add cameraMovement.x cameraMovement.y))))
(FlxG.camera.updateKeyControl
elapsed
CAMERA_SPEED
->{FlxG.keys.pressed.LEFT}
->{FlxG.keys.pressed.RIGHT}
->{FlxG.keys.pressed.UP}
->{FlxG.keys.pressed.DOWN}))

View File

@@ -0,0 +1,10 @@
package kiss_flixel;
import kiss.Prelude;
import kiss.List;
import flixel.FlxCamera;
import flixel.math.FlxVector;
import flixel.math.FlxPoint;
@:build(kiss.Kiss.build())
class CameraTools {}

View File

@@ -0,0 +1,14 @@
(function updateKeyControl [:FlxCamera camera :Float elapsed :Float speed :Void->Bool leftKey :Void->Bool rightKey :Void->Bool upKey :Void->Bool downKey]
(let [scrollAmount (* speed elapsed)
&mut :FlxVector movement (new FlxPoint)]
(when (leftKey) (-= movement.x 1))
(when (rightKey) (+= movement.x 1))
(when (upKey) (-= movement.y 1))
(when (downKey) (+= movement.y 1))
(when (< 0 movement.length)
(set movement (movement.normalize)))
(movement.scale scrollAmount)
(+= camera.scroll.x movement.x)
(+= camera.scroll.y movement.y)))
(function updateKeyControl [:FlxCamera camera :Float elapsed :Float speed :Void->Bool leftKey :Void->Bool rightKey :Void->Bool upKey :Void->Bool downKey]

View File

@@ -40,6 +40,8 @@
<haxelib name="kiss" />
<haxeflag name="--macro" value="kiss.Kiss.setup()" />
<haxelib name="kiss-flixel" />
<haxelib name="nat-archive-tool" />
<!--In case you want to use the addons package-->

View File

@@ -14,11 +14,11 @@ import flixel.text.FlxText;
import flixel.util.FlxColor;
using flixel.util.FlxSpriteUtil;
import flixel.FlxCamera;
import flixel.math.FlxVector;
import flixel.math.FlxRect;
import flixel.addons.plugin.FlxMouseControl;
import flixel.input.mouse.FlxMouseEventManager;
using StringTools;
using kiss_flixel.CameraTools;
@:build(kiss.Kiss.build())
class PlayState extends FlxState implements ArchiveUI {}

View File

@@ -117,36 +117,28 @@
(+= uiCamera.y (* FlxG.mouse.wheel UI_SCROLL_FACTOR))
// TODO allow changing the a scroll factor
// Control the UI camera with the arrow keys, and the playground camera with wasd:
// Control the UI camera with WASD, and the playground camera with arrow keys:
(var KEYBOARD_SCROLL_SPEED 200)
(method cameraKeyControl [:FlxCamera camera :Void->Bool leftKey :Void->Bool rightKey :Void->Bool upKey :Void->Bool downKey]
// but not when textInput is focused
(unless (and textInput textInput.hasFocus)
(let [scrollPerSec (/ KEYBOARD_SCROLL_SPEED 60)
&mut :FlxVector movement (new FlxPoint)]
(when (leftKey) (-= movement.x 1))
(when (rightKey) (+= movement.x 1))
(when (upKey) (-= movement.y 1))
(when (downKey) (+= movement.y 1))
(when (< 0 movement.length)
(set movement (movement.normalize)))
(movement.scale scrollPerSec)
(+= camera.scroll.x movement.x)
(+= camera.scroll.y movement.y))))
// don't move the ui camera before ui has been placed -- new UI elements could appear offscreen
(when (> uiGroup.length 0)
(cameraKeyControl uiCamera
(FlxG.camera.updateKeyControl
elapsed
KEYBOARD_SCROLL_SPEED
->{FlxG.keys.pressed.LEFT}
->{FlxG.keys.pressed.RIGHT}
->{FlxG.keys.pressed.UP}
->{FlxG.keys.pressed.DOWN}))
->{FlxG.keys.pressed.DOWN})
(cameraKeyControl FlxG.camera
// don't move the ui camera before ui has been placed -- new UI elements could appear offscreen
(when (> uiGroup.length 0)
(unless (and textInput textInput.hasFocus)
(uiCamera.updateKeyControl
elapsed
KEYBOARD_SCROLL_SPEED
// TODO support dvorak
->{FlxG.keys.pressed.A}
->{FlxG.keys.pressed.D}
->{FlxG.keys.pressed.W}
->{FlxG.keys.pressed.S}))
->{FlxG.keys.pressed.S}))))
(method :Void typeCommand []
(enterText