This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrat.lhs
135 lines (121 loc) · 4.69 KB
/
Strat.lhs
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
> import qualified Data.Maybe as DM
> import qualified Data.Map as DMap
> import qualified System.Random as R
> import qualified Control.Monad as CM
> import qualified Graphics.UI.SDL as SDL
> import qualified Graphics.UI.SDL.Image as SDLi
> import UIState
> artFilePaths = [ "art/64x74_blue.png",
> "art/64x74_green.png",
> "art/64x74_white.png",
> "art/64x74_brown.png" ]
> tileWidth = 64
> tileHeight = 74
> windowWidth = 640
> windowHeight = 480
> mapRows = 100
> mapColumns = 100
> getRandomTerrain :: Int -> IO [TerrainType]
> getRandomTerrain l = do
> randomNumbers <- CM.replicateM l $ R.randomRIO (0,terrainMaxBound)
> return $ map toEnum randomNumbers
> makeRandomMap :: Int -> Int -> IO TerrainMap
> makeRandomMap w h = do
> CM.foldM (\m y -> makeRow w y m) DMap.empty [1..h]
> where
> makeRow :: Int -> Int -> TerrainMap -> IO (TerrainMap)
> makeRow w y tileMap = do
> rt <- getRandomTerrain w
> let tp = zip [1..w] rt
> return $ foldr (\(x,t) m -> DMap.insert (x,y) t m) tileMap tp
> loadArt :: [String] -> IO TerrainSurfaces
> loadArt paths = do
> tileSurfs <- mapM SDLi.load paths
> return $ zip terrainTypes tileSurfs
> mapCoordinates :: [Point]
> mapCoordinates = [(x,y) | x <- [1..mapColumns], y <- [1..mapRows]]
> redrawScreen :: UIState -> IO ()
> redrawScreen ui@(UIState vp _ mainSurf terrainSurfs terrainMap) = do
> SDL.fillRect mainSurf Nothing (SDL.Pixel 0)
> mapM_ (drawTile ui) mapCoordinates
> SDL.flip mainSurf
> return ()
> drawTile :: UIState -> Point -> IO ()
> drawTile (UIState vp _ mainSurf terrainSurfs tm) (x,y) = do
> let sr = {-# SCC "dt-sr" #-} Just (SDL.Rect 0 0 tileWidth tileHeight)
> (tX, tY) = {-# SCC "dT-gP2V" #-} gamePoint2View vp $ {-# SCC "dT-gHO" #-} getHexmapOffset tileWidth tileHeight x y
> dr = {-# SCC "dT-dr" #-} Just $ SDL.Rect tX tY 0 0
> tt = DM.fromJust $ {-# SCC "dT-tmLookup" #-} DMap.lookup (x,y) tm
> terrainSurf = DM.fromJust $ {-# SCC "dT-tsLookup" #-} lookup tt terrainSurfs
> if {-# SCC "dT-tIVP" #-} tileInViewPort vp tileWidth tileHeight (tX,tY)
> then do
> {-# SCC "dT-blit" #-} SDL.blitSurface terrainSurf sr mainSurf dr
> return ()
> else
> return ()
> getHexmapOffset :: Int -> Int -> Int -> Int -> Point
> getHexmapOffset tileW tileH x y =
> (adjX , adjY)
> where
> baseAdjX = (tileW * (x-1))
> baseAdjY = (tileH * (y-1))
> quarterH = tileH `div` 4
> halfW = tileW `div` 2
> adjX = if odd y
> then baseAdjX + halfW
> else baseAdjX
> adjY = baseAdjY - ((y-1) * quarterH)
> tileInViewPort :: ViewPort -> Int -> Int -> Point -> Bool
> tileInViewPort (ViewPort _ _ vpW vpH) tileW tileH (pX , pY) =
> let pX' = pX + tileW
> pY' = pY + tileH
> in
> if ((pX' < 0) || (pX > vpW) || (pY' < 0) || pY > vpH)
> then False else True
> gamePoint2View :: ViewPort -> Point -> Point
> gamePoint2View (ViewPort vpx vpy _ _) (gx , gy) =
> ((gx + vpx) , (gy + vpy))
> main :: IO ()
> main = do
> SDL.init [SDL.InitEverything]
> SDL.setVideoMode windowWidth windowHeight 32 []
> SDL.setCaption "Video Test!" "video test"
>
> mainSurf <- SDL.getVideoSurface
> tileSurfs <- loadArt artFilePaths
> randomMap <- makeRandomMap mapColumns mapRows
>
> let initialUI = UIState (ViewPort 0 0 windowWidth windowHeight) [] mainSurf tileSurfs randomMap
> eventLoop initialUI
>
> mapM_ freeSurf tileSurfs
> SDL.quit
> putStrLn "done"
> where
> freeSurf (_ , s) = SDL.freeSurface s
> eventLoop ui = do
> e <- SDL.pollEvent
> checkEvent ui e
> checkEvent ui (SDL.NoEvent) = do
> redrawScreen ui
> e <- SDL.waitEvent
> checkEvent ui e
> checkEvent ui (SDL.KeyUp _) = return ()
> checkEvent ui (SDL.MouseMotion _ _ xr yr ) = do
> if elem SDL.ButtonRight $ uiMouseButtonsDown ui
> then eventLoop ui'
> else eventLoop ui
> where
> ui' = ui { uiViewPort = updatedVP }
> updatedVP = vp { vpX = x', vpY = y' }
> vp = uiViewPort ui
> x' = (vpX vp) + fromIntegral xr
> y' = (vpY vp) + fromIntegral yr
> checkEvent ui (SDL.MouseButtonDown _ _ b) = do
> let mbs = uiMouseButtonsDown ui
> eventLoop $ ui { uiMouseButtonsDown = mbs ++ [b] }
> checkEvent ui (SDL.MouseButtonUp _ _ b) = do
> let mbs = uiMouseButtonsDown ui
> let mbs' = filter (/= b) mbs
> eventLoop $ ui { uiMouseButtonsDown = mbs' }
> checkEvent ui _ = eventLoop ui