Module

Reactor.Reaction

Package
purescript-grid-reactors
Repository
Eugleo/purescript-grid-reactors

#Reaction Source

type Reaction world = ReactionM world Unit

#dimensions Source

dimensions :: forall world. ReactionM world Dimensions

Get a record of the following:

  • height :: Int, the height of the grid, in tiles
  • width :: Int, the width of the grid, in tiles
  • tileSize :: Int the size of one grid tile, in points. The size is set internally, and this is the only way to get its value.

#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

#modifyW_ Source

modifyW_ :: forall world. (world -> world) -> Reaction world

Similar to modifyW, but doesn't return the value of the new world.

#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

#getW Source

getW :: forall world. ReactionM world world

Obtain the current value of the world.

type World = { player :: { x :: Int, y :: Int } }

handleEvent event = case event of
  KeyPress { key : "Space" } -> do
    w <- getW
    log $ "The player is at position: "
      <> show w.player.x
      <> ", "
      <> show w.player.y

#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.

#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

#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

Instances

#Dimensions Source

type Dimensions = { height :: Int, tileSize :: Int, width :: Int }