-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data.hs
456 lines (371 loc) · 12.2 KB
/
Data.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
{-# LANGUAGE OverloadedStrings #-}
module Data where
import HGamer3D
import qualified Data.Text as T
import Data.Tree
import Data.Unique
import qualified Data.Map as M
import qualified Data.HMap as HM
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import System.Random
import Debug.Trace
-- state game is in (which screen, mode, ...)
data GameState = ProgramInitializing | InitScreen | PlayGame | Flying | FinalScore deriving (Eq, Ord, Show)
-- main data structure for game content - the game data tree
-- ---------------------------------------------------------
-- HGamer3D website, space invaders, data tree
data NodeType = Canon | ReserveCanon | Boulder | Invader Int | Ship | Shot
| InvaderRow | CanonRow | BoulderRow | ShotRow
| Pixel | PixelA | PixelB -- the single pixel cubes, different variants for animation
| Empty
deriving (Eq, Show, Ord)
type NodeData = HM.HMap
type GameData = Tree (NodeType, NodeData)
type KPos = HM.HKey HM.T PixelPos
type KHits = HM.HKey HM.T HitInfo
type KDim = HM.HKey HM.T DimInfo
type KEnt = HM.HKey HM.T Entity
type KAnim = HM.HKey HM.T AnimInfo
type KUni = HM.HKey HM.T Unique
-- end of website text
type Keys = (KEnt, KDim, KPos, KHits, KAnim, KUni)
genKeys :: IO Keys
genKeys = do
kent <- HM.createKey
kdim <- HM.createKey
kpos <- HM.createKey
khits <- HM.createKey
kanim <- HM.createKey
kuni <- HM.createKey
return (kent, kdim, kpos, khits, kanim, kuni)
-- data for all moving parts are kept in a flexible tree data structure
-- each node of this tree has a node type and node data
emptyData :: NodeData
emptyData = HM.empty
initData :: HM.HKey t a -> a -> NodeData
initData k v = HM.singleton k v
setData :: HM.HKey t a -> a -> NodeData -> NodeData
setData k v nd = HM.insert k v nd
(!) :: NodeData -> HM.HKey t a -> a
(!) nd k = case HM.lookup k nd of
Just v -> v
Nothing -> trace ("NodeData ! not there") undefined
isNodeType :: GameData -> NodeType -> Bool
isNodeType (Node (nt, _) _) nt' = nt == nt'
getChildren :: GameData -> [GameData]
getChildren (Node _ c) = c
addChildren :: GameData -> [GameData] -> GameData
addChildren (Node v c) c' = Node v (c ++ c')
removeChildren :: GameData -> GameData
removeChildren (Node v c) = Node v []
-- data concerning positions, damage and animations
-- ------------------------------------------------
type PixelPos = (Int, Int)
pixelWidth :: Float
pixelWidth = 0.1
lineWidth :: Float
lineWidth = 0.02
data DimInfo = DimInfo {
diWidth :: Int,
diHeight :: Int,
diCenterX :: Float,
diCenterY :: Float,
diOffX :: Float,
diOffY :: Float }
deriving (Eq, Show, Ord)
data HitInfo = HitInfo {
hiHits :: Int,
hiMaxHits :: Int
}
deriving (Eq, Show, Ord)
data AnimInfo = AnimInfo {
aiCycles :: Int,
aiStartCycle :: Int,
aiSwapNow :: Bool,
aiType :: NodeType -- PixelA or PixelB, current status
}
-- position, size data, all positions are given in pixel pos and converted to Vec3
diFromSize :: Int -> Int -> DimInfo
diFromSize width height = let
cx = ((fromIntegral width) * pixelWidth + (fromIntegral (width - 1)) * lineWidth) / 2.0
cy = ((fromIntegral height) * pixelWidth + (fromIntegral (height - 1)) * lineWidth) / 2.0
ox = if (width `mod` 2) > 0 then (pixelWidth + lineWidth) * (-0.5) else 0.0
oy = if (height `mod` 2) > 0 then (pixelWidth + lineWidth) * (-0.5) else 0.0
in (DimInfo width height cx cy ox oy)
posFromPixelPos :: DimInfo -> PixelPos -> Vec3
posFromPixelPos di (x, y) = let
delta = pixelWidth + lineWidth
in Vec3 ((fromIntegral x) * delta + (diOffX di)) ((fromIntegral y) * delta + (diOffY di)) 0.0
relativePosFromPixelPos :: DimInfo -> PixelPos -> Vec3
relativePosFromPixelPos dim (x, y) = let
delta = pixelWidth + lineWidth
in ((Vec3 ((fromIntegral x) * delta) ((fromIntegral y) * delta) 0.0) &- (Vec3 (diCenterX dim) (diCenterY dim) 0.0) )
-- damage, hits
hiNew :: Int -> HitInfo
hiNew maxHits = HitInfo 0 maxHits
takeHits :: HitInfo -> Int -> HitInfo
takeHits (HitInfo h mh) hits = (HitInfo (h + hits) mh)
damage :: HitInfo -> Float
damage (HitInfo h m) = if h >= m then 1.0 else (fromIntegral h) / (fromIntegral m)
-- animation info
aiNew :: IO AnimInfo -- io for random number generator
aiNew = do
cycles <- randomRIO (1, 5) :: IO Int
startCycle <- randomRIO (1, cycles) :: IO Int
b <- randomRIO (0, 1) :: IO Int
let aiType = case b of
0 -> PixelA
1 -> PixelB
return (AnimInfo cycles startCycle False aiType)
getCurrentAnimation :: AnimInfo -> Int -> AnimInfo -- active, non-active node type
getCurrentAnimation anim cycle = if (cycle + aiStartCycle anim) `mod` (aiCycles anim) /= 0
then anim {aiSwapNow = False}
else anim {aiType = case (aiType anim) of
PixelA -> PixelB
PixelB -> PixelA,
aiSwapNow = True
}
-- create moving entities
-- ----------------------
createMoveNode :: HG3D -> Keys -> NodeType -> PixelPos -> IO GameData
createMoveNode hg3d keys nodeType pos = do
let (kent, kdim, kpos, khits, kanim, kuni) = keys
let arts = artwork M.! nodeType
let (ppA, ppB) = pixelPairs arts
let (w, h) = dimArt arts
let dim = diFromSize w h
let hits = hiNew (hitData M.! nodeType)
anim <- aiNew
uni <- newUnique
let delta = pixelWidth + lineWidth
let mat = matArt arts
let createPars = [
ctGraphicsElement #: (),
ctScale #: Vec3 1.0 1.0 1.0,
ctPosition #: posFromPixelPos dim pos,
ctOrientation #: unitU
]
let pars = if nodeType == Shot
then (ctLight #: Light PointLight 1.0 5.0 1.0) : createPars
else createPars
eGeo <- newE hg3d pars
let nd = setData kuni uni $ setData kanim anim $ setData kdim dim $ setData kent eGeo $ setData kpos pos $ initData khits hits
eGeoId <- idE eGeo
let createPE t (x, y) = do
e <- newE hg3d [
ctGeometry #: ShapeGeometry Cube,
ctMaterial #: mat,
ctParent #: eGeoId,
ctScale #: Vec3 pixelWidth pixelWidth pixelWidth,
ctPosition #: (if t == Pixel || t == PixelA then relativePosFromPixelPos dim (x, y) else (Vec3 (-1000) 0 0)),
ctOrientation #: unitU
]
uni' <- newUnique
return (e, (x, y), t, uni')
-- return (e, (x - ((diWidth dim) `div` 2), y - ((diHeight dim) `div` 2)), t)
case ppB of
Nothing -> do
pes <- mapM (createPE Pixel) ppA
return $ Node (nodeType, nd) (map (\(e, p, t, u) -> Node (t, (setData kuni u (setData kent e (initData kpos p)))) []) pes)
Just ppB' -> do
let ppBoth = filter (\p -> p `elem` ppB') ppA
pesBoth <- mapM (createPE Pixel) ppBoth
let ppAOnly = filter (\p -> not (p `elem` ppB')) ppA
pesA <- mapM (createPE PixelA) ppAOnly
let ppBOnly = filter (\p -> not (p `elem` ppA)) ppB'
pesB <- mapM (createPE PixelB) ppBOnly
return $ Node (nodeType, nd) (map (\(e, p, t, u) -> Node (t, (setData kuni u (setData kent e (initData kpos p)))) []) (pesBoth ++ pesA ++ pesB))
gameDataFromBuildData :: HG3D -> Keys -> [BuildElement] -> IO GameData
gameDataFromBuildData hg3d keys bd = do
let (kent, kdim, kpos, khits, kanim, kuni) = keys
nodes <- mapM (\be -> case be of
BEOne nt pix -> createMoveNode hg3d keys nt pix
BERow nt pix@(x, y) space count -> do
let (nt', nt'') = case nt of
(Invader n) -> (InvaderRow, Invader n)
Boulder -> (BoulderRow, Boulder)
Canon -> (CanonRow, ReserveCanon)
Shot -> (ShotRow, Shot)
subnodes <- mapM (\pix' -> createMoveNode hg3d keys nt'' pix') [(x' + x, y) | x' <- [0, space .. ((count-1)*space)]]
return (Node (nt', (HM.singleton kpos pix)) subnodes)
) bd
return $ Node (Empty, HM.empty) nodes
moveNode :: Keys -> NodeData -> PixelPos -> IO NodeData
moveNode keys nodeData (x', y') = do
let (kent, kdim, kpos, khits, kanim, kuni) = keys
let (x, y) = nodeData ! kpos
let nodeData' = setData kpos (x + x', y + y') nodeData
let e = nodeData ! kent
let di = nodeData ! kdim
liftIO $ setC e ctPosition $ posFromPixelPos di (x + x', y + y')
return nodeData'
-- data for flying
-- ---------------
data Speed = Speed Int
-- game level data
-- ---------------
data BuildElement = BEOne NodeType PixelPos
| BERow NodeType PixelPos Int Int -- space, count
deriving (Eq, Ord, Show)
type Artwork = ([T.Text], Maybe [T.Text], Material)
hitData :: M.Map NodeType Int
hitData = M.fromList [ (Shot, 1), (Canon, 0), (ReserveCanon, 0), (Boulder, 0), (Ship, 3), (Invader 1, 1), (Invader 2, 1), (Invader 3, 2) ]
-- get a list of (x, y) from Artwork
pixelPairs :: Artwork -> ([(Int, Int)], Maybe [(Int, Int)])
pixelPairs (a, b, _) = let
ppFromLineArray lineArray = let
lineIdx line = [ i | (i, c) <- zip [0..] (T.unpack line), c == 'x']
in [ (i, j) | (j, l) <- zip [0..] (reverse lineArray), i <- lineIdx l]
a' = ppFromLineArray a
b' = fmap ppFromLineArray b
in (a', b')
-- get width, height from Artwork
dimArt :: Artwork -> (Int, Int)
dimArt (lineArray, _, _) = let
width = maximum (map (length . T.unpack) lineArray)
height = length lineArray
in (width, height)
matArt :: Artwork -> Material
matArt (_, _, mat) = mat
artwork :: M.Map NodeType Artwork
artwork = M.fromList [
(Shot,
(
[
"x",
"x"
],
Nothing,
matYellow
)),
(Canon,
(
[
" x ",
" xxx ",
" xxx ",
" xxxxxxxxx ",
"xxxxxxxxxxx",
"xxxxxxxxxxx"
],
Nothing,
matLime
)),
(ReserveCanon,
(
[
" x ",
" xxx ",
" xxx ",
" xxxxxxxxx ",
"xxxxxxxxxxx",
"xxxxxxxxxxx"
],
Nothing,
matLime
)),
(Boulder,
(
[
" xxxxxxxxxxxx ",
" xxxxxxxxxxxxxx ",
" xxxxxxxxxxxxxxxx ",
" xxxxxxxxxxxxxxxxxx ",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxx",
"xxxxxx xxxxxx",
"xxxxx xxxxx",
"xxxx xxxx",
"xxxx xxxx"
],
Nothing,
matLime
)),
(Invader 1,
(
[" xxxx ",
" xxxxxxxxxx ",
"xxxxxxxxxxxx",
"xxx xx xxx",
"xxxxxxxxxxxx",
" xxx xxx ",
" xx xx xx ",
" xx xx "],
Just [" xxxx ",
" xxxxxxxxxx ",
"xxxxxxxxxxxx",
"xxx xx xxx",
"xxxxxxxxxxxx",
" xx xx ",
" xx xx xx ",
"xx xx"],
matGreen
)),
(Invader 2,
(
[" x x ",
" x x ",
" xxxxxxx ",
" xx xxx xx ",
"xxxxxxxxxxx",
"x xxxxxxx x",
"x x x x",
" xx xx "],
Just [" x x ",
"x x x x",
"x xxxxxxx x",
"xxx xxx xxx",
"xxxxxxxxxxx",
" xxxxxxxxx ",
" x x ",
" x x "],
matRed
)),
(Invader 3,
(
[" xx ",
" xxxx ",
" xxxxxx ",
"xx xx xx",
"xxxxxxxx",
" x xx x ",
"x x",
" x x "],
Just [" xx ",
" xxxx ",
" xxxxxx ",
"xx xx xx",
"xxxxxxxx",
" x x ",
" x xx x ",
"x x x x"],
matMaroon
)),
(Ship,
(
[" xxxxxx ",
" xxxxxxxxxx ",
" xxxxxxxxxxxx ",
" xx xx xx xx xx ",
"xxxxxxxxxxxxxxxx",
" xxx xx xxx ",
" x x "],
Just [" xxxxxx ",
" xxxxxxxxxx ",
" xxxxxxxxxxxx ",
" xx xx xx xx xx ",
"xxxxxxxxxxxxxxxx",
" xxx xx xxx ",
" x x "],
matBlue
))
]
-- interesting cam positions
cam_pos_1 = (Vec3 1.0 (-22.863186) (-10.812058), U (Vec4 0.8459173 (-0.533298) 0.0 0.0))