Skip to content

Commit

Permalink
Move "measuring tape" concern into Tabletop
Browse files Browse the repository at this point in the history
Formalize Distance as a Float
  • Loading branch information
mwunsch committed Oct 12, 2016
1 parent 70b178f commit b945c18
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
11 changes: 5 additions & 6 deletions src/Main.elm
@@ -1,6 +1,5 @@
module Main exposing (..)

import Dict
import Gang
import Html exposing (Html)
import Html.App as App
Expand All @@ -15,7 +14,7 @@ import Player exposing (Player)
import String exposing (join)
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Tabletop exposing (posX, posY, Tabletop, positionFromMouseCoords)
import Tabletop exposing (Tabletop, positionFromMouseCoords)
import Task
import Window

Expand Down Expand Up @@ -169,9 +168,9 @@ onClickWithCoords message =
view : GameState -> Html Msg
view game =
let
movementArea =
measuringTape =
Player.getSelectedGangMember game.player
|> Maybe.map (\fighter -> Model.movementView fighter game.player.movementIntention :: [])
|> Maybe.map (\fighter -> Tabletop.viewMeasuringTape fighter.position game.player.movementIntention fighter.remainingMove :: [])
|> Maybe.withDefault []
in
svg
Expand All @@ -181,7 +180,7 @@ view game =
(game.windowWidth |> toString)
, onClickWithCoords Click
]
(Tabletop.view game.tabletop []
:: movementArea
(Tabletop.view game.tabletop
:: measuringTape
++ Gang.view game.player.gang Select
)
17 changes: 5 additions & 12 deletions src/Model.elm
Expand Up @@ -141,8 +141,11 @@ attemptMove model pos =

allowableDistance =
model.remainingMove - distance

maxPosition =
Tabletop.positionFromDirection model.position pos model.remainingMove
in
if allowableDistance >= 0 then
if allowableDistance > 0 then
Ok
{ model
| position = pos
Expand All @@ -152,22 +155,12 @@ attemptMove model pos =
Err
( "Can't move there"
, { model
| position = maxAllowedMovement model pos
| position = maxPosition
, remainingMove = 0
}
)


maxAllowedMovement : Model -> Position -> Position
maxAllowedMovement model intent =
Tabletop.positionFromDirection model.position intent model.remainingMove


type Injury
= FleshWound
| Down
| OutOfAction


view : Model -> msg -> Svg msg
view model msg =
Expand Down
59 changes: 50 additions & 9 deletions src/Tabletop.elm
@@ -1,8 +1,9 @@
module Tabletop exposing (..)

import Svg exposing (rect, Svg)
import Svg exposing (Svg, rect, g, line)
import Svg.Attributes exposing (..)


{-| The Tabletop module exposes types and functions related to the
structure of the game board as well as positioning and movement on the
board.
Expand All @@ -13,13 +14,19 @@ type alias Tabletop =
, height : Inch
}


type alias Inch =
Int


type alias Position =
( Float, Float )


type alias Distance =
Float


posX : Position -> Float
posX ( x', _ ) =
x'
Expand Down Expand Up @@ -48,10 +55,10 @@ positionFromMouseCoords ( x, y ) scale =

center : Tabletop -> Position
center table =
( toFloat table.width / 2 , toFloat table.height / 2 )
( toFloat table.width / 2, toFloat table.height / 2 )


distance : Position -> Position -> Float
distance : Position -> Position -> Distance
distance ( x1, y1 ) ( x2, y2 ) =
let
y' =
Expand All @@ -60,10 +67,10 @@ distance ( x1, y1 ) ( x2, y2 ) =
x' =
(x1 - x2) ^ 2
in
(x' + y') |> sqrt
sqrt (x' + y')


positionFromDirection : Position -> Position -> Float -> Position
positionFromDirection : Position -> Position -> Distance -> Position
positionFromDirection start end len =
let
h =
Expand All @@ -76,7 +83,7 @@ positionFromDirection start end len =
(posY end) - (posY start)

angle =
acos <| y' / h
acos (y' / h)

co =
if x' > 0 then
Expand All @@ -89,11 +96,45 @@ positionFromDirection start end len =
)


view : Tabletop -> List (Svg msg) -> Svg msg
view tabletop children =
view : Tabletop -> Svg msg
view tabletop =
rect
[ width (tabletop.width |> toString)
, height (tabletop.height |> toString)
, fill "silver"
]
children
[]


viewMeasuringTape : Position -> Position -> Distance -> Svg msg
viewMeasuringTape start end range =
let
length =
distance start end

( rangeX, rangeY ) =
if length > range then
positionFromDirection start end range
else
end
in
g []
[ line
[ x1 (start |> posX |> toString)
, y1 (start |> posY |> toString)
, x2 (rangeX |> toString)
, y2 (rangeY |> toString)
, stroke "yellow"
, strokeWidth "0.63"
]
[]
, line
[ x1 (rangeX |> toString)
, y1 (rangeY |> toString)
, x2 (end |> posX |> toString)
, y2 (end |> posY |> toString)
, stroke "grey"
, strokeWidth "0.63"
]
[]
]

0 comments on commit b945c18

Please sign in to comment.