Skip to content

Commit

Permalink
Only allow movement up to max
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Oct 7, 2016
1 parent 552e819 commit 4904dd3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 17 deletions.
57 changes: 48 additions & 9 deletions src/Model.elm
Expand Up @@ -121,16 +121,35 @@ averageFighter pos =
}



-- TODO: move movement logic into attemptMove


move : Model -> Position -> Model
move model pos =
{ model | position = pos }
let
distance =
Tabletop.distance model.position pos

newPos =
if distance > (toFloat model.remainingMove) then
maxAllowedMovement model pos
else
pos
in
{ model | position = newPos }


attemptMove : Model -> Position -> Result ( String, Model ) Model
attemptMove model pos =
Ok { model | position = pos }


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


type Injury
= FleshWound
| Down
Expand Down Expand Up @@ -162,13 +181,33 @@ movementView model pos =

( newX, newY ) =
pos

distance =
Tabletop.distance model.position pos

( maxX, maxY ) =
if distance > (toFloat model.remainingMove) then
maxAllowedMovement model pos
else
pos
in
line
[ x1 (modelX |> toString)
, y1 (modelY |> toString)
, x2 (newX |> toString)
, y2 (newY |> toString)
, stroke "white"
, strokeWidth "0.25"
g []
[ line
[ x1 (modelX |> toString)
, y1 (modelY |> toString)
, x2 (maxX |> toString)
, y2 (maxY |> toString)
, stroke "white"
, strokeWidth "0.25"
]
[]
, line
[ x1 (maxX |> toString)
, y1 (maxY |> toString)
, x2 (newX |> toString)
, y2 (newY |> toString)
, stroke "grey"
, strokeWidth "0.25"
]
[]
]
[]
40 changes: 32 additions & 8 deletions src/Tabletop.elm
Expand Up @@ -16,25 +16,25 @@ type alias Tabletop =


type alias Position =
( Int, Int )
( Float, Float )


posX : Position -> Int
posX : Position -> Float
posX ( x', _ ) =
x'


posY : Position -> Int
posY : Position -> Float
posY ( _, y' ) =
y'


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

x' =
transform x
Expand All @@ -58,9 +58,33 @@ distance ( x1, y1 ) ( x2, y2 ) =
x' =
(x1 - x2) ^ 2
in
(x' + y')
|> toFloat
|> sqrt
(x' + y') |> sqrt


positionFromDirection : Position -> Position -> Float -> Position
positionFromDirection start end len =
let
h =
distance start end

x' =
(posX end) - (posX start)

y' =
(posY end) - (posY start)

angle =
acos <| y' / h

co =
if x' > 0 then
1
else
-1
in
( (posX start) + (sin angle * len) * co
, (posY start) + (cos angle * len)
)


view : Tabletop -> List (Svg msg) -> Svg msg
Expand Down

0 comments on commit 4904dd3

Please sign in to comment.