-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
212 lines (174 loc) · 6.25 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
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
module Main where
import Prelude hiding (Either(..))
import System.IO
import qualified Data.Map as M
import Data.Maybe
import Data.List
import Render.SDL.GUI as GUI
import Render.SDL.Render
import Combat
import Helpers
import Logic
import WorldBuilder
import Player
import Types.World
import Types.Common
import Content.Classes
import Content.Races
import Content.Skills
main :: IO ()
main = do
assets <- loadAssets
GUI.setup
world <- returnWorld
(name, race, klass) <- GUI.chooseProtagonist world assets
let world' = createHero world name klass race
gameLoop world' assets
-- main game loop!
gameLoop :: World -> Assets -> IO ()
gameLoop world assets
| null (wLevels world) ||
(eCurrHP (wHero world) < 0) ||
fst (eCurrPos (wBoss world)) > fst (eCurrPos (wHero world)) = -- game end
GUI.delayedShutdown world assets
| eNextMove (wHero world) == 0 = do -- player's turn
let world' = checkVision world
GUI.update_ world' assets
input <- GUI.getInput
case input of
Show screen -> gameLoop (handleShow screen world') assets
Exit -> handleExit (world' {wScreenShown = Console}) assets
Wait -> gameLoop (handleWait (world' {wScreenShown = Console})) assets
Rest -> gameLoop (handleRest (world' {wScreenShown = Console})) assets
Dir dir -> gameLoop (handleDir (world' {wScreenShown = Console}) dir) assets
Queue i -> queueSkill i (world' {wScreenShown = Skills}) assets
ExecuteSkills -> executeSkills (world' {wScreenShown = Console}) assets
| otherwise = do -- ai's turn
world' <- think world
gameLoop world' assets
-- tries to move the player.
-- if a door is encountered, open it.
-- if an enemy is encountered: attack it,
handleDir :: World -> Direction -> World
handleDir w dir
| dir == Left && fst (eCurrPos h) == firstInFrame = -- left corner, does not use a turn.
w {
wMessageBuffer = "You can't go there! No turn used.":mBuffer
}
| dir == Right && fst coord > lSize lvl - 1 = -- Right edge of map, does not use a turn, possible issue. Perhaps load next level here.
nextLevel $ w { wTimeElapsed = newTime }
| isDoor coord lvl = -- Destroys the door, uses a turn.
w {
wLevel = lvl {
lWallTiles = M.delete coord $ lWallTiles lvl
},
wHero = h {
eOldPos = eCurrPos h,
eNextMove = eSpeed h,
hCurrEnergy = newEnergy
},
wTimeElapsed = newTime,
wMessageBuffer = "Opened door!":mBuffer
}
| isMonster coord lvl = -- Simple combat (using movement keys)
(simpleCombat h (fromJust $ M.lookup coord (lEntities lvl)) w)
{
wHero = h {eOldPos = eCurrPos h,
eNextMove = eSpeed h,
hCurrEnergy = newEnergy
},
wTimeElapsed = newTime
}
| dir == Right && fst (eCurrPos h) == lastInFrame && lastInFrame < lSize lvl = -- If at right side of frame
w {
wHero = h {
eOldPos = eCurrPos h,
eCurrPos = coord,
eNextMove = eSpeed h,
hCurrEnergy = newEnergy,
hMovementSlack = (firstInFrame+1, lastInFrame+1)
},
wTimeElapsed = newTime
}
| otherwise = -- simple movement, no wrapping.
w { wHero = h { eOldPos = eCurrPos h, eCurrPos = coord, eNextMove = eSpeed h, hCurrEnergy = newEnergy},
wTimeElapsed = newTime
}
where
h = wHero w
lvl = wLevel w
coord = (newX, newY)
newX = heroX
newY = 0
(heroX, heroY) = eCurrPos h |+| dirToCoord dir
hConst i = max 0 (min i 20)
(firstInFrame, lastInFrame) = hMovementSlack h
mBuffer = wMessageBuffer w
newEnergy = max 0 $ hCurrEnergy h - 1
newTime = wTimeElapsed w + fromIntegral (eSpeed h)
-- simply passes the time.
handleWait :: World -> World
handleWait w =
w {
wHero = h {
eOldPos = eCurrPos h,
eNextMove = eSpeed h
},
wTimeElapsed = fromIntegral (eSpeed h) + wTimeElapsed w,
wMessageBuffer = "Waiting!":wMessageBuffer w
}
where
h = wHero w
--Exits!
handleExit :: World -> Assets -> IO ()
handleExit world assets = do
GUI.shutdown world assets
--nextLevel :: World -> IO
nextLevel world
| null $ tail $ wLevels world =
world { wLevels = [] } -- just set wLevels to [] and check for that in the main Loop... (ugly.)
| otherwise =
world { wLevel = wLevels world !! 1,
wLevels = tail $ wLevels world,
wHero = hero { eCurrPos = (0,0),
eOldPos = (0,0),
hMovementSlack = (0, 9)}
}
where
hero = wHero world
-- Show different content in the context window.
handleShow :: Screen -> World -> World
handleShow screen w = w { wScreenShown = screen }
-- Rests the player, fails if enemies are nearby.
handleRest :: World -> World
handleRest w =
if null enemiesInView -- Safe to rest?
then
if hMaxEnergy h == hCurrEnergy h
then
w { wMessageBuffer = "You're already at maximum energy!":wMessageBuffer w }
else
w { wHero = h { hCurrEnergy = hMaxEnergy h, eNextMove = fromIntegral newTime}, wMessageBuffer = "You wake up from your rest brimming with energy.":wMessageBuffer w, wTimeElapsed = newTime}
else
w { wMessageBuffer = "You can't rest while enemies are nearby!":wMessageBuffer w}
where
h = wHero w
enemiesInView = getEntitiesFromViewFrame w $ getViewFrame w
newTime = 500 + wTimeElapsed w + fromIntegral((hMaxEnergy h - hCurrEnergy h) * 2)
-- handles skill queue (duh), asks for input.
queueSkill :: Int -> World -> Assets -> IO ()
queueSkill n w a = do
skill <- chooseSkill w a
let w' = case skill of
NoSkill -> w { wHero = h { hSkillQueue = removeFromQueue n $ hSkillQueue h},
wScreenShown = Console
}
_ -> w { wHero = h { hSkillQueue = addToQueue skill n $ hSkillQueue h},
wScreenShown = Console
}
gameLoop w' a
where
h = wHero w
-- executes queued skills. (need to check for energy etc)
executeSkills :: World -> Assets -> IO ()
executeSkills w a = gameLoop (performSkills w) a