-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
108 lines (94 loc) · 3.14 KB
/
Main.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
module Main where
import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game
import Graphics.Gloss.Interface.Environment
import Debug.Trace
import Util
import Camera
import Controls
import Universe
import Grid
import Constants
data State =
State {
stateControls :: !Controls,
stateUniverse :: !Universe,
stateCamera :: !Camera,
stateGrid :: !Grid,
stateControlsUpdater :: !(Updater (Controls, Universe, Camera)),
stateUniverseUpdater :: !(Updater Universe),
stateCameraUpdater :: !(Updater Camera),
stateGridUpdater :: !(Updater (Grid, Universe, Camera))
}
initialState :: (Int, Int) -> State
initialState (wi, hi) =
State {
stateControls = createControls,
stateUniverse = createUniverse,
stateCamera = createCamera dims (0, 0, 0) 0.5 0.5 camSZ,
stateGrid = emptyGrid,
stateControlsUpdater = createControlsUpdater,
stateUniverseUpdater = createUniverseUpdater,
stateCameraUpdater = createCameraUpdater,
stateGridUpdater = createGridUpdater
}
where dims = (fromIntegral wi, fromIntegral hi)
pollEvents :: Event -> State -> State
pollEvents event state =
state {
stateControls = handleControlsEvent controls event
}
where controls = stateControls state
update :: Float -> State -> State
update delta state =
state {
stateControlsUpdater = controlsUpdater',
stateUniverseUpdater = universeUpdater',
stateCameraUpdater = cameraUpdater',
stateGridUpdater = gridUpdater',
stateControls = controls',
stateUniverse = universe'',
stateCamera = camera'',
stateGrid = grid'
}
where (controlsUpdater', (controls', universe', camera'))
= runUpdater delta controlsUpdater (controls, universe, camera)
(universeUpdater', universe'')
= runUpdater delta universeUpdater universe'
(cameraUpdater', camera'') =
runUpdater delta cameraUpdater camera'
(gridUpdater', (grid', _, _)) =
runUpdater delta gridUpdater (grid, universe'', camera'')
State {
stateControls = controls,
stateUniverse = universe,
stateCamera = camera,
stateGrid = grid,
stateControlsUpdater = controlsUpdater,
stateUniverseUpdater = universeUpdater,
stateCameraUpdater = cameraUpdater,
stateGridUpdater = gridUpdater
} = state
draw :: State -> Picture
draw state =
pictures [
drawGrid grid camera,
drawUniverse universe camera,
drawControls controls
]
where camera = stateCamera state
universe = stateUniverse state
grid = stateGrid state
controls = stateControls state
main :: IO ()
main = do
(width, height) <- getScreenSize
let res@(x, y) = (floor demoSW, floor demoSH)
let pos = ((width - x) `div` 2, (height - y) `div` 2)
play (InWindow "Gravity Demo" res pos)
black
demoUR
(initialState res)
draw
pollEvents
update