Reactor.Reaction
- Package
- purescript-grid-reactors
- Repository
- Eugleo/purescript-grid-reactors
#ReactionF Source
data ReactionF world a
A DSL for constructing reactions. For internal use only.
You should construct a Reaction
by calling the different helper functions
in this module, like modifyW
, instead of building it manually.
Constructors
Lift (Effect a)
Modify (world -> world) (world -> a)
Dimensions (Dimensions -> a)
Widget String Ordering Widget (a)
RemoveWidget String (a)
ExecuteDefaultBehavior (a)
Instances
#ReactionM Source
newtype ReactionM world a
ReactionM world a
describes a general reaction in a reactor with the world world
,
that returns a
. In the handleEvent
function we use reactions which don't return anything
(or more precisely, return Unit
), ReactionM world Unit
. To simplify things, the following
type synonym is set up.
type Reaction world = ReactionM world Unit
Constructors
Instances
#dimensions Source
dimensions :: forall world. ReactionM world Dimensions
Get a record of the following:
height :: Int
, the height of the grid, in tileswidth :: Int
, the width of the grid, in tilestileSize :: Int
the size of one grid tile, in points. The size is set internally, and this is the only way to get its value.
#executeDefaultBehavior Source
executeDefaultBehavior :: forall world. Reaction world
After handling the event, execute the default behavior as well.
You can read more in the documentation of Reactor.Events
.
Usually you'll call this in the handleEvent
function, for events that you don't want to handle,
e.g. when pressing 'J' doesn't do anything in your game.
#modifyW Source
modifyW :: forall world. (world -> world) -> ReactionM world world
Modify the current world by passing in a (world -> world)
updating function.
The function will receive the current value of the world, and should return the new updated value.
Returns the value of the new world.
type World = { player :: { x :: Int, y :: Int } }
handleEvent event = case event of
KeyPress { key : "ArrowUp" } ->
newWorld <- modifyW \oldWorld ->
{ player: { x : oldWorld.player.x, y: oldWorld.player.y + 1 } }
log $ "New world is: " <> show newWorld
#removeWidget' Source
removeWidget' :: forall world. String -> Reaction world
Remove a Widget
with a given identifier from the reactor. If there is no widget with the given identifier, do nothing.
#updateW Source
updateW :: forall changes t c world. Union changes t world => Nub changes c => Record changes -> ReactionM (Record world) (Record world)
Modify certain fields of the world by passing new values for them.
Returns the value of the new world.
type World = { player :: { x :: Int, y :: Int } }
handleEvent event = case event of
KeyPress { key : "ArrowUp" } ->
oldWorld <- getW
newWorld <-
updateW
{ player: { x : oldWorld.player.x, y: oldWorld.player.y + 1 } }
log $ "New world is: " <> show newWorld
#updateW_ Source
updateW_ :: forall changes t c world. Union changes t world => Nub changes c => Record changes -> Reaction (Record world)
Modify certain fields of the world by passing new values for them.
Behaves the same as the updateW
function, but returns unit
instead of the new value of the world.
type World = { player :: { x :: Int, y :: Int } }
handleEvent event = case event of
KeyPress { key : "ArrowUp" } -> do
oldWorld <- getW
updateW_
{ player: { x : oldWorld.player.x, y: oldWorld.player.y + 1 } }
newWorld <- getW
log $ "New world is: " <> show newWorld