-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.hs
178 lines (156 loc) · 4.66 KB
/
Model.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{-# LANGUAGE TemplateHaskell #-}
module Model where
import Control.Monad.Trans.State
import qualified Data.HashMap.Strict as HM
import Game.Model hiding (Model)
import qualified Game.Model
import Game.Prelude
import qualified Scenario.Crisis
import qualified Scenario.Tannen
data Model = Model
{ modelGame :: Game.Model.Model
, modelSelection :: Selection
, modelOrders :: Orders
, modelTurnEnded :: Bool
, modelOpponentOrders :: HashMap Player Orders
, modelWhoAmI :: Player
, modelPan :: BoardPoint
, modelZoom :: Zoom
, modelPlaceScroll :: HashMap PlaceId Natural
, modelScreenSize :: Box
, modelCursorDot :: ScreenPoint
, modelDragToPan :: Drag
, modelTick :: Tick
, modelPopup :: [CombatLog]
, modelExit :: Bool
} deriving stock (Generic)
deriving anyclass (ToJSON)
init :: Gen -> Box -> Scenario -> Player -> Model
init gen screenSize scenario currentPlayer =
Model
{ modelGame = game
, modelSelection = startingSelection
, modelOrders = mempty
, modelTurnEnded = False
, modelOpponentOrders = mempty
, modelWhoAmI = currentPlayer
, modelPan = BoardPoint 0 0
, modelZoom = NoZoom
, modelPlaceScroll = mempty
, modelScreenSize = screenSize
, modelCursorDot = ScreenPoint 10000 10000 -- off the screen to start
, modelDragToPan = NotDragging
, modelTick = Tick
, modelPopup = mempty
, modelExit = False
}
where
game :: Game.Model.Model
game =
let fillBoard :: State Game.Model.Model ()
fillBoard = case scenario of
Tannen -> Scenario.Tannen.fillBoard
Crisis -> Scenario.Crisis.fillBoard
in execState fillBoard (Game.Model.init gen)
startingSelection :: Selection
startingSelection =
case HM.keys (HM.filter friendly (modelPlaces game)) of
[] -> SelectionNone
id:_ -> SelectionPlace id
where
friendly :: Place -> Bool
friendly place =
case placeType place of
Ruin ->
False
PBase base ->
baseOwner base == PlayerOwner currentPlayer
data Scenario
= Tannen
| Crisis
deriving stock (Eq, Ord, Show, Enum, Bounded, Generic)
deriving anyclass (ToJSON, FromJSON)
data Selection
= SelectionNone
| SelectionPlace PlaceId
| SelectionShip ShipId
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
data CombatLog
= CombatLog PlaceId (Set ShipId)
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
-- | Since we only have animations that blink on and off
-- (like thursters for moving ships) we only need to track
-- two states of time.
data Tick
= Tick
| Tock
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
data Zoom
= NoZoom
| ZoomOut
| ZoomOut2
| ZoomOut3
| ZoomOut4
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
data Drag
= NotDragging
| PossibleDragStart ScreenPoint
-- ^ ScreenPoint is where the mouse was when the left button
-- was pressed down.
--
-- If the user then moves the mouse past a certan threshold
-- we switch to Dragging.
--
-- If they release the button before that happens
-- they were using left click to clear their current
-- selection, so we do that and switch to NotDragging.
| Dragging ScreenPoint
-- ^ ScreenPoint is the last position of the mouse.
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
-- | An unprocessed gloss point (to become a game point will need to be
-- de-panned and de-zoomed).
data ScreenPoint
= ScreenPoint Float Float
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
data HudPoint
= HudPoint Float Float
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
data BoardPoint
= BoardPoint Float Float
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (ToJSON)
fromScreenPoint :: ScreenPoint -> Point
fromScreenPoint (ScreenPoint x y) =
(x,y)
fromHudPoint :: HudPoint -> Point
fromHudPoint (HudPoint x y) =
(x,y)
fromBoardPoint :: BoardPoint -> Point
fromBoardPoint (BoardPoint x y) =
(x,y)
maxZoom :: Zoom
maxZoom =
NoZoom
zoomIn :: Zoom -> Zoom
zoomIn = \case
NoZoom -> NoZoom
ZoomOut -> NoZoom
ZoomOut2 -> ZoomOut
ZoomOut3 -> ZoomOut2
ZoomOut4 -> ZoomOut3
zoomOut :: Zoom -> Zoom
zoomOut = \case
NoZoom -> ZoomOut
ZoomOut -> ZoomOut2
ZoomOut2 -> ZoomOut3
ZoomOut3 -> ZoomOut4
ZoomOut4 -> ZoomOut4
-- * Lenses
mkLenses ''Model