Skip to content

Commit

Permalink
Randomly generate 5 gang members and position them
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Oct 13, 2016
1 parent 3d597be commit 649ba22
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/Gang.elm
@@ -1,10 +1,11 @@
module Gang exposing (..)

import Dict exposing (Dict)
import Dict exposing (Dict, toList)
import List
import Model exposing (Model, Id)
import Random exposing (Generator)
import Random exposing (Generator, andThen)
import Svg exposing (Svg)
import Tabletop exposing (Tabletop)


type alias Gang =
Expand All @@ -27,6 +28,17 @@ generator =
|> Random.map fromList


positionedGenerator : Tabletop -> Generator Gang
positionedGenerator table =
Random.list 5 Model.generator
`andThen`
\models ->
Random.list (List.length models) (Tabletop.positionGenerator table)
|> Random.map (List.map2 (,) models)
|> Random.map (List.map (\( model, pos ) -> { model | position = pos }))
|> Random.map fromList


view : Gang -> (Model -> msg) -> List (Svg msg)
view gang msg =
Dict.values gang
Expand Down
16 changes: 14 additions & 2 deletions src/Main.elm
@@ -1,6 +1,6 @@
module Main exposing (..)

import Gang
import Gang exposing (Gang)
import Html exposing (Html)
import Html.App as App
import Html.Events exposing (on, onClick)
Expand All @@ -11,6 +11,7 @@ import Maybe exposing (andThen)
import Model exposing (Model)
import Mouse
import Player exposing (Player)
import Random
import String exposing (join)
import Svg exposing (..)
import Svg.Attributes exposing (..)
Expand Down Expand Up @@ -52,7 +53,10 @@ init =
, windowWidth = 1000
, windowScale = 10
}
, Task.perform (always NoOp) Resize Window.width
, Cmd.batch
[ Task.perform (always NoOp) Resize Window.width
, Random.generate Generate (Gang.positionedGenerator table)
]
)


Expand All @@ -66,6 +70,7 @@ type Msg
| Hover Mouse.Position
| KeyPress Keyboard.KeyCode
| Resize Int
| Generate Gang
| NoOp


Expand Down Expand Up @@ -125,6 +130,13 @@ update msg game =
, Cmd.none
)

Generate gang ->
let
updatePlayer player =
{ player | gang = gang }
in
( { game | player = updatePlayer game.player }, Cmd.none )

NoOp ->
( game, Cmd.none )

Expand Down
2 changes: 1 addition & 1 deletion src/Player.elm
Expand Up @@ -16,7 +16,7 @@ type alias Player =

init : Tabletop -> Player
init table =
{ gang = Dict.singleton 1 <| Model.averageFighter (Tabletop.center table)
{ gang = Gang.empty
, selection = Nothing
, movementIntention = Tabletop.center table
}
Expand Down

0 comments on commit 649ba22

Please sign in to comment.