Skip to content

Commit

Permalink
On hover, move model in campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
mwunsch committed Dec 22, 2016
1 parent 0e4420b commit 1efb4c5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
65 changes: 64 additions & 1 deletion src/Campaign.elm
@@ -1,12 +1,16 @@
module Campaign exposing (..)

import Game exposing (Game)
import Gang exposing (Gang)
import Html exposing (Html)
import Html.Attributes
import Model exposing (Model)
import Mouse
import Player exposing (Player)
import Random
import Svg exposing (..)
import Svg.Attributes exposing (..)
import Tabletop
import Task
import Utilities exposing (textNode)
import Window
Expand Down Expand Up @@ -49,6 +53,7 @@ init =
type Msg
= Begin Game
| Select Model
| Hover Mouse.Position
| Resize Window.Size
| NoOp

Expand All @@ -58,6 +63,9 @@ update msg campaign =
let
noop =
( campaign, Cmd.none )

activePlayer =
Game.activePlayer campaign.game
in
case msg of
Begin newGame ->
Expand All @@ -70,6 +78,25 @@ update msg campaign =
, Cmd.none
)

Hover ({ x, y } as mouse) ->
let
pos : Tabletop.Position
pos =
tabletopPositionFromMousePosition mouse campaign

pivot : Gang
pivot =
Player.updateSelectedGangMember activePlayer
(\model -> { model | bearing = Tabletop.angle model.position pos })
in
( { campaign
| game =
campaign.game
|> Game.mapActivePlayer (\p -> { p | movementIntention = pos, gang = pivot })
}
, Cmd.none
)

Resize ({ width, height } as size) ->
( { campaign | window = size }
, Cmd.none
Expand All @@ -79,13 +106,49 @@ update msg campaign =
noop


tabletopPositionFromMousePosition : Mouse.Position -> Campaign -> Tabletop.Position
tabletopPositionFromMousePosition { x, y } { game, window } =
let
tableHeight =
(toFloat window.height) * 0.8

tableWidth =
tableHeight * (Tabletop.aspectRatio game.tabletop)

xOffset =
((toFloat window.width) - tableWidth) / 2

yOffset =
((toFloat window.height) - tableHeight) / 2

scale =
tableWidth / (toFloat game.tabletop.width)
in
( x, y )
|> Tuple.mapFirst (\x -> ((toFloat x) - xOffset) / scale)
|> Tuple.mapSecond (\y -> ((toFloat y) - yOffset) / scale)
|> Debug.log "x,y"



-- SUBSCRIPTIONS


subscriptions : Campaign -> Sub Msg
subscriptions campaign =
Sub.batch [ Window.resizes Resize ]
let
activePlayer =
Game.activePlayer campaign.game
in
Sub.batch
[ Window.resizes Resize
, case activePlayer.selection of
Just _ ->
Mouse.moves Hover

Nothing ->
Sub.none
]



Expand Down
34 changes: 19 additions & 15 deletions src/Game.elm
Expand Up @@ -48,24 +48,30 @@ activePlayer game =
Tuple.second game.players


mapActivePlayer : (Player -> Player) -> Game -> ( Player, Player )
mapActivePlayer : (Player -> Player) -> Game -> Game
mapActivePlayer transform game =
case Tuple.second game.turn of
PlayerOne ->
Tuple.mapFirst transform game.players
{ game
| players =
case Tuple.second game.turn of
PlayerOne ->
Tuple.mapFirst transform game.players

PlayerTwo ->
Tuple.mapSecond transform game.players
PlayerTwo ->
Tuple.mapSecond transform game.players
}


mapEnemyPlayer : (Player -> Player) -> Game -> ( Player, Player )
mapEnemyPlayer : (Player -> Player) -> Game -> Game
mapEnemyPlayer transform game =
case Tuple.second game.turn of
PlayerOne ->
Tuple.mapSecond transform game.players
{ game
| players =
case Tuple.second game.turn of
PlayerOne ->
Tuple.mapSecond transform game.players

PlayerTwo ->
Tuple.mapFirst transform game.players
PlayerTwo ->
Tuple.mapFirst transform game.players
}


playerOne : Game -> Player
Expand All @@ -92,9 +98,7 @@ generator =

selectFriendlyModel : Game -> Model -> Game
selectFriendlyModel game model =
{ game
| players = mapActivePlayer (flip Player.selectModel <| model.id) game
}
mapActivePlayer (flip Player.selectModel <| model.id) game


view : Game -> (Model -> msg) -> List (Svg msg)
Expand Down

0 comments on commit 1efb4c5

Please sign in to comment.