Skip to content

Instantly share code, notes, and snippets.

@gcentauri
Last active June 10, 2018 19:41
Show Gist options
  • Save gcentauri/6101906ab3d56738ee83f27d76819d2a to your computer and use it in GitHub Desktop.
Save gcentauri/6101906ab3d56738ee83f27d76819d2a to your computer and use it in GitHub Desktop.
import Html exposing (Html, button, div, text)
import Html.Events exposing (onClick)
import Game exposing (..)
import Game.Lenses exposing(..)
main =
Html.beginnerProgram { model = game, view = view, update = update }
type Msg = UpdateGame
update msg model =
case msg of
UpdateGame ->
updateGame model
view model =
div []
[ div [] [ text (toString (model |> inventory_.get)) ]
]
player = Player 10 [Item "Sword" "it cuts"]
slime = Monster "Slime" 1 (Item "goo" "it goos")
game = Game player [slime]
module Game exposing (..)
type alias Game =
{ player: Player
, monsters: List Monster
}
type alias Player =
{ health: Int -- [lens : playerHealth]
, inventory: List Item
}
type alias Item =
{ name: String -- [lens : itemName]
, description: String
}
type alias Monster =
{ name: String -- [lens :monsterName]
, health: Int -- [lens :monsterHealth]
, drops: Item
}
updateGame : Game -> Game
updateGame game = game
Copy link

ghost commented Jun 10, 2018

model |> (player_ >. inventory_).get

Copy link

ghost commented Jun 10, 2018

  inv_ = player_ >. inventory_ 

Then you can just do model |> inv_.set [...] or whatever

Copy link

ghost commented Jun 10, 2018

To append to the inventory:

 model |> (player_ >. inventory_ >. (nth_ hugenumber defaultItem)).set myitem

where hugenumber is either a fixed huge number or is the length of the current inventory as determined by an erlier step, say stored in a let expression, and defaultItem is some fixed default item that is needed for formal purposes in this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment