Rohan Sircar
3 years ago
6 changed files with 249 additions and 33 deletions
-
39src/main/scala/wow/doge/mygame/MainApp.scala
-
16src/main/scala/wow/doge/mygame/game/subsystems/input/GameInputHandler.scala
-
87src/main/scala/wow/doge/mygame/game/subsystems/input/InputMappings.scala
-
9src/main/scala/wow/doge/mygame/utils/MonixDirectoryWatcher.scala
-
5src/main/scala/wow/doge/mygame/utils/package.scala
-
126src/main/scala/wow/doge/mygame/utils/wrappers/jme/InputManager.scala
@ -0,0 +1,87 @@ |
|||
package wow.doge.mygame.game.subsystems.input |
|||
|
|||
import com.jme3.input.KeyInput |
|||
import com.jme3.input.MouseInput |
|||
import com.jme3.input.controls.KeyTrigger |
|||
import com.jme3.input.controls.MouseAxisTrigger |
|||
import monix.{eval => me} |
|||
import wow.doge.mygame.implicits._ |
|||
import wow.doge.mygame.utils.wrappers.jme.InputManager |
|||
|
|||
class InputMappings(inputManager: InputManager) { |
|||
|
|||
def setup = |
|||
for { |
|||
_ <- setupMovementKeys |
|||
_ <- setupAnalogMovementKeys |
|||
_ <- setupCameraKeys |
|||
_ <- cursorToggle |
|||
} yield () |
|||
|
|||
def setupMovementKeys = |
|||
inputManager.withEnumMappings(PlayerMovementInput) { |
|||
case PlayerMovementInput.WalkRight => |
|||
new KeyTrigger(KeyInput.KEY_D) :: Nil |
|||
case PlayerMovementInput.WalkLeft => |
|||
new KeyTrigger(KeyInput.KEY_A) :: Nil |
|||
case PlayerMovementInput.WalkForward => |
|||
new KeyTrigger(KeyInput.KEY_W) :: Nil |
|||
case PlayerMovementInput.WalkBackward => |
|||
new KeyTrigger(KeyInput.KEY_S) :: Nil |
|||
case PlayerMovementInput.Jump => |
|||
new KeyTrigger(KeyInput.KEY_SPACE) :: Nil |
|||
} |
|||
|
|||
def setupAnalogMovementKeys = |
|||
inputManager.withEnumMappings(PlayerAnalogMovementInput) { |
|||
case PlayerAnalogMovementInput.TurnRight => |
|||
Seq(new KeyTrigger(KeyInput.KEY_D)) |
|||
case PlayerAnalogMovementInput.TurnLeft => |
|||
Seq(new KeyTrigger(KeyInput.KEY_A)) |
|||
} |
|||
|
|||
def setupCameraKeys = |
|||
inputManager.withEnumMappings(PlayerCameraInput) { |
|||
case PlayerCameraInput.CameraRotateLeft => |
|||
Seq( |
|||
new KeyTrigger(KeyInput.KEY_LEFT), |
|||
new MouseAxisTrigger(MouseInput.AXIS_X, false) |
|||
) |
|||
case PlayerCameraInput.CameraRotateRight => |
|||
Seq( |
|||
new KeyTrigger(KeyInput.KEY_RIGHT), |
|||
new MouseAxisTrigger(MouseInput.AXIS_X, true) |
|||
) |
|||
case PlayerCameraInput.CameraRotateUp => |
|||
Seq( |
|||
new KeyTrigger(KeyInput.KEY_UP), |
|||
new MouseAxisTrigger(MouseInput.AXIS_Y, false) |
|||
) |
|||
case PlayerCameraInput.CameraRotateDown => |
|||
Seq( |
|||
new KeyTrigger(KeyInput.KEY_DOWN), |
|||
new MouseAxisTrigger(MouseInput.AXIS_Y, true) |
|||
) |
|||
} |
|||
|
|||
def cursorToggle = |
|||
inputManager.withMapping( |
|||
MiscInput.ToggleCursor, |
|||
new KeyTrigger(KeyInput.KEY_Z) |
|||
) >> |
|||
inputManager |
|||
.enumEntryObservableAction(MiscInput.ToggleCursor) |
|||
.doOnNext(action => |
|||
action.binding match { |
|||
case MiscInput.ToggleCursor => |
|||
if (action.value)(inputManager.cursorVisible = |
|||
!inputManager.cursorVisible).toTask |
|||
else me.Task.unit |
|||
} |
|||
) |
|||
.completedL |
|||
.toIO |
|||
.hideErrors |
|||
.startAndForget |
|||
|
|||
} |
@ -1,7 +1,6 @@ |
|||
package wow.doge.mygame |
|||
|
|||
// import wow.doge.mygame.utils.wrappers.Node |
|||
|
|||
package object utils { |
|||
// type AppNode = Node |
|||
def methodName(implicit enclosing: sourcecode.Enclosing) = |
|||
enclosing.value.split(" ")(0).split("""\.""").last |
|||
} |
@ -0,0 +1,126 @@ |
|||
package wow.doge.mygame.utils.wrappers.jme |
|||
import com.jme3.input.controls.InputListener |
|||
import com.jme3.input.controls.Trigger |
|||
import com.jme3.{input => jmei} |
|||
import enumeratum._ |
|||
import monix.bio.UIO |
|||
import monix.reactive.Observable |
|||
import wow.doge.mygame.ActionEvent |
|||
import wow.doge.mygame.AnalogEvent |
|||
import wow.doge.mygame.EnumActionEvent |
|||
import wow.doge.mygame.EnumAnalogEvent |
|||
import wow.doge.mygame.implicits._ |
|||
|
|||
final class InputManager(val delegate: jmei.InputManager) { |
|||
|
|||
/** |
|||
* Create a new mapping to the given triggers. |
|||
* |
|||
* <p> |
|||
* The given mapping will be assigned to the given triggers, when |
|||
* any of the triggers given raise an event, the listeners |
|||
* registered to the mappings will receive appropriate events. |
|||
* |
|||
* @param mappingName The mapping name to assign. |
|||
* @param triggers The triggers to which the mapping is to be registered. |
|||
*/ |
|||
def withMapping(mapping: String, triggers: Trigger*): UIO[Unit] = |
|||
UIO(delegate.withMapping(mapping, triggers: _*)).void |
|||
|
|||
def withMapping[T <: EnumEntry]( |
|||
mapping: T, |
|||
triggers: Trigger* |
|||
): UIO[Unit] = UIO(delegate.withMapping(mapping, triggers: _*)).void |
|||
|
|||
def withListener(listener: InputListener, mappings: String*) = |
|||
UIO(delegate.withListener(listener, mappings: _*)) |
|||
|
|||
/** |
|||
* Creates new mappings from the values of the given Enum |
|||
* |
|||
* <p> |
|||
* The given mapping will be assigned to the given triggers, when |
|||
* any of the triggers given raise an event, the listeners |
|||
* registered to the mappings will receive appropriate events. |
|||
* |
|||
* @param mappingName The mapping name to assign. |
|||
* @param mappingFn Function from enum values to the sequence of trigers. |
|||
* |
|||
* @example |
|||
* |
|||
* {{{ |
|||
* |
|||
* sealed trait PlayerAnalogMovementInput extends EnumEntry with UpperSnakecase |
|||
* object PlayerAnalogMovementInput extends Enum[PlayerAnalogMovementInput] { |
|||
* val values = findValues |
|||
* case object TurnRight extends PlayerAnalogMovementInput |
|||
* case object TurnLeft extends PlayerAnalogMovementInput |
|||
* } |
|||
* |
|||
* { |
|||
* inputManager.withEnumMappings(PlayerAnalogMovementInput) { |
|||
* case PlayerAnalogMovementInput.TurnRight => |
|||
* Seq(new KeyTrigger(KeyInput.KEY_RIGHT)) |
|||
* case PlayerAnalogMovementInput.TurnLeft => |
|||
* Seq(new KeyTrigger(KeyInput.KEY_LEFT)) |
|||
* } |
|||
* } |
|||
* }}} |
|||
*/ |
|||
def withEnumMappings[T <: EnumEntry]( |
|||
mappingEnum: Enum[T] |
|||
)(mappingFn: T => Seq[Trigger]) = |
|||
UIO(delegate.withEnumMappings(mappingEnum)(mappingFn)) |
|||
|
|||
/** |
|||
* Create an observable which emits the given mappings as elements of an observable |
|||
* |
|||
* @param mappingNames |
|||
* @return Observable of action events |
|||
* |
|||
* @see [[ActionEvent]] |
|||
* @see [[enumObservableAction]] |
|||
*/ |
|||
def observableAction(mappingNames: String*): Observable[ActionEvent] = |
|||
delegate.observableAction(mappingNames: _*) |
|||
|
|||
/** |
|||
* <p> |
|||
* Create an observable which emits the values of the given |
|||
* enum as elements of an observable |
|||
* |
|||
* @param mappingNames |
|||
* @return Observable of enum values |
|||
* |
|||
* @example {{{ |
|||
* inputManager |
|||
* .enumObservableAction(PlayerMovementInput) |
|||
* .doOnNext { action => |
|||
* action.binding match { |
|||
* case PlayerMovementInput.WalkLeft => Task {/* your actions */} |
|||
* } |
|||
* } |
|||
* }}} |
|||
* |
|||
* @see [[EnumActionEvent]] |
|||
* @see [[enumAnalogObservable]] |
|||
*/ |
|||
def enumObservableAction[T <: EnumEntry]( |
|||
mappingEnum: Enum[T] |
|||
): Observable[EnumActionEvent[T]] = |
|||
delegate.enumObservableAction(mappingEnum) |
|||
|
|||
def enumEntryObservableAction[T <: EnumEntry]( |
|||
mappingEnumEntry: T |
|||
) = delegate.enumEntryObservableAction(mappingEnumEntry) |
|||
|
|||
def analogObservable(mappingNames: String*): Observable[AnalogEvent] = |
|||
delegate.analogObservable(mappingNames: _*) |
|||
|
|||
def enumAnalogObservable[T <: EnumEntry]( |
|||
mappingEnum: Enum[T] |
|||
): Observable[EnumAnalogEvent[T]] = delegate.enumAnalogObservable(mappingEnum) |
|||
|
|||
def cursorVisible_=(value: Boolean) = UIO(delegate.setCursorVisible(value)) |
|||
def cursorVisible = delegate.isCursorVisible |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue