Skip to content

Commit

Permalink
Draw a line for movement intent from selection
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Oct 5, 2016
1 parent ecfdf43 commit 808052c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Main.elm
Expand Up @@ -11,7 +11,7 @@ import Mouse
import String exposing (join)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Tabletop exposing (posX, posY, Tabletop)
import Tabletop exposing (posX, posY, Tabletop, positionFromMouseCoords)
import Task
import Window

Expand All @@ -36,6 +36,7 @@ type alias GameState =
, tabletop : Tabletop
, windowWidth : Int
, windowScale : Float
, movementIntention : Tabletop.Position
}


Expand All @@ -47,6 +48,7 @@ init =
, tabletop = Tabletop 100 50
, windowWidth = 1000
, windowScale = 10
, movementIntention = ( 50, 25 )
}
, Task.perform (\_ -> NoOp) Resize Window.width
)
Expand All @@ -59,6 +61,7 @@ init =
type Msg
= Select Model
| Click Mouse.Position
| Hover Mouse.Position
| Resize Int
| NoOp

Expand All @@ -74,10 +77,7 @@ update msg game =
moveFighter =
case game.playerSelection of
Just fighter ->
Model.move fighter
( round <| (toFloat x) / game.windowScale
, round <| (toFloat y) / game.windowScale
)
Model.move fighter <| positionFromMouseCoords ( x, y ) game.windowScale

Nothing ->
game.fighter
Expand All @@ -89,6 +89,18 @@ update msg game =
, Cmd.none
)

Hover { x, y } ->
case game.playerSelection of
Just fighter ->
( { game
| movementIntention = positionFromMouseCoords ( x, y ) game.windowScale
}
, Cmd.none
)

Nothing ->
( game, Cmd.none )

Resize w ->
( { game
| windowWidth = w
Expand All @@ -107,7 +119,10 @@ update msg game =

subscriptions : GameState -> Sub Msg
subscriptions game =
Window.resizes (\size -> Resize size.width)
Sub.batch
[ Mouse.moves Hover
, Window.resizes (\size -> Resize size.width)
]



Expand Down Expand Up @@ -144,14 +159,7 @@ view game =

Just x ->
[ fighter
, circle
[ cx (x.position |> posX |> toString)
, cy (x.position |> posY |> toString)
, r (x.profile.move |> toString)
, fill "white"
, fillOpacity "0.25"
]
[]
, Model.movementView x game.movementIntention
]
in
svg
Expand Down
20 changes: 20 additions & 0 deletions src/Model.elm
Expand Up @@ -152,3 +152,23 @@ view model msg =
, Svg.Attributes.cursor "pointer"
]
[ text "@" ]


movementView : Model -> Position -> Svg msg
movementView model pos =
let
( modelX, modelY ) =
model.position

( newX, newY ) =
pos
in
line
[ x1 (modelX |> toString)
, y1 (modelY |> toString)
, x2 (newX |> toString)
, y2 (newY |> toString)
, stroke "white"
, strokeWidth "0.25"
]
[]
14 changes: 14 additions & 0 deletions src/Tabletop.elm
Expand Up @@ -29,6 +29,20 @@ posY ( _, y' ) =
y'


positionFromMouseCoords : ( Int, Int ) -> Float -> Position
positionFromMouseCoords ( x, y ) scale =
let
transform : Int -> Int
transform a =
round <| (toFloat a) / scale

x' = transform x

y' = transform y
in
( x', y' )


type alias Inch =
Int

Expand Down

0 comments on commit 808052c

Please sign in to comment.