/
southfall.cpp
485 lines (444 loc) · 14.4 KB
/
southfall.cpp
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "Southfall.h"
//=============================================================================
// Constructor
//=============================================================================
Southfall::Southfall() {}
//=============================================================================
// Destructor
//=============================================================================
Southfall::~Southfall()
{ // Call onLostDevice() for every graphics item
releaseAll();
}
void Southfall::loadIntro()
{
ifstream fin(INTRO_TEXT);
char buffer[500];
introText = new vector<string>;
while(!fin.eof())
{
fin.getline(buffer, 500);
introText->push_back(buffer);
}
fin.close();
}
//=============================================================================
// Initializes the game
// Throws GameError on error
//=============================================================================
void Southfall::initialize(HWND hwnd)
{
Game::initialize(hwnd);
currentState = MAIN_MENU;
pause = false;
firstTimeBattle = true;
Entity::killCount = 0;
done = false;
// Graphics
graphics = new Graphics();
graphics->initialize(hwnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN);
//NPC::initGraphics(graphics);
imageLibrary = new ImageLibrary(graphics);
// Menu
mainMenu = new Menu();
mainMenu->initialize(graphics, input);
actionMenu = new GameMenu();
actionMenu->initialize(graphics, input, "Available Actions:");
// Font
gameFont = new TextDX();
gameFont->initialize(graphics, 40, false, false, "Andalus");
gameFont->setFontColor(SETCOLOR_ARGB(255,255,255,255));
//Initialize global TextBox
textbox = new TextBox(gameFont, audio, input, &imageLibrary->TextBoxIM, &imageLibrary->TextBoxArrowIM);
textbox->setActive(false);
// WorldInterface
Interface = new WorldInterface(imageLibrary);
Interface->initialize(graphics, audio, textbox);
// Initialized Player here, have center point at player's position
player = new Hero(ZERO, heroNS::HERO_RADIUS, &imageLibrary->Character1IM, input,
audio, textbox, new Drawable(&imageLibrary->SwingingSwordIM));
//places player right by first goblin
//player->setPosition(VECTOR2(112.5,60.5));
//places player by house in Southfall
//player->setPosition(VECTOR2(102.5,96.5));
//places player on chair in Bar
player->setPosition(VECTOR2(11.5,7));
player->setWorld(Interface->getCurrent());
player->getWorld()->addEntity(player);
player->setSpellType(NULLTYPE);
birm = new Birmingham(audio, &imageLibrary->BirmSpriteIM);
birm->setPosition(VECTOR2(113,101.5));
birm->setController(new StationaryAI(birm));
fontLoc = 0;
fontTimer = 6;
// For testing: set up action Menu:
/*actionMenu->addButton(new Button("Sword", &imageLibrary->SwordIconIM, 0));
actionMenu->addButton(new Button("Impede Spell", &imageLibrary->ImpedeEffectIM, 1));
actionMenu->addButton(new Button("Quick Portal", &imageLibrary->PortalOpenIM, 2));
actionMenu->addButton(new Button("Blink", &imageLibrary->BlinkIconIM, 3));
actionMenu->addButton(new Button("Fireball", &imageLibrary->FireballIconIM, 4));
actionMenu->addButton(new Button("Shadowball", &imageLibrary->ShadowballIconIM, 5));
actionMenu->addButton(new Button("Magic Sight On", &imageLibrary->MagicSightOnIM, 8));
actionMenu->addButton(new Button("Magic Sight Off", &imageLibrary->MagicSightOffIM, 9)); */
}
//=============================================================================
// Update all game items
//=============================================================================
void Southfall::update()
{
switch(currentState)
{
case MAIN_MENU:
if (!mainMenu->update())
{
currentState = INTRO;
loadIntro();
textbox->setText(*introText);
textbox->setActive(true);
}
break;
case INTRO:
if (!textbox->isActive())
{
audio->playCue(SOUTHFALL_THEME);
currentState = GAME;
Entity* temp = player->getWorld()->getNPCFacing(player->getPosition(), entityNS::UP);
if (temp)
{
temp->setDir(entityNS::DOWN);
player->reset();
player->setDir(entityNS::UP);
textbox->setText((NPC*)temp);
textbox->setActive(true);
audio->playCue(ALERT);
delete introText;
}
}
case OPENTEXTBOX: //fallthrough is intentional
textbox->update(frameTime);
if (!textbox->isActive()) currentState = GAME;
if (input->wasKeyPressed('E'))
{
audio->playCue(SELECT);
textbox->next();
}
break;
case GAME:
if (!birm->isActive()){ currentState = GAME_OVER; break; }
if(input->isKeyDown(O_KEY) && input->isKeyDown(P_KEY))
{
birminghamRot = .001;
player->setHP(INT_MAX);
birm->setHP(INT_MAX);
currentState = BIRMINGHAMSTATE;
imageLibrary->BirminghamIM.setScale(10);
audio->playCue(BIRMINGHAM_SOUND);
}
if(player->hasSword && !player->hasAddedSword)
{
float birmX = birm->getPosition().x;
float birmY = birm->getPosition().y;
int xs[] = {76,78, birmX,birmX, 138,136};
int ys[] = {birmY,birmY, 69,71, birmY,birmY};
for(int j = 0; j < 2; ++j)
{
for(int i = 0; i < GOBLIN_WAVE_COUNT/2; ++i)
{
Entity* goblin = new Entity(VECTOR2(xs[i]+j,ys[i]+j-.1), 0.3, GOBLIN_HEALTH, &imageLibrary->Goblin1IM, audio, 1, HUMAN_CRECT);
goblin->setSpeed(3); goblin->setDeathSound(GOBLIN_DEATH);
Interface->getMain()->addEntity(goblin, new WaveAI(goblin));
Interface->getMain()->addEnemy(goblin);
}
}
for (int i=0; i<3; i++)
{
Entity* guard = new Entity(VECTOR2(120-i+.5,120), 0.5, GOBLIN_HEALTH, &imageLibrary->GuardIM, audio, 0, HUMAN_CRECT);
guard->setSpeed(2);
npcAI* ai = new WaveAI(guard);
ai->setSight(12);
Interface->getMain()->addEntity(guard, ai);
}
Entity* guard1 = new Entity(VECTOR2(birmX-1, birmY-1), 0.5, GOBLIN_HEALTH, &imageLibrary->GuardIM, audio, 0, HUMAN_CRECT);
guard1->setSpeed(2);
npcAI* ai1 = new WaveAI(guard1);
ai1->setSight(12);
Interface->getMain()->addEntity(guard1, ai1);
Entity* guard2 = new Entity(VECTOR2(birmX, birmY-1), 0.5, GOBLIN_HEALTH, &imageLibrary->GuardIM, audio, 0, HUMAN_CRECT);
guard2->setSpeed(2);
npcAI* ai2 = new WaveAI(guard2);
ai2->setSight(12);
Interface->getMain()->addEntity(guard2, ai2);
Entity* guard3 = new Entity(VECTOR2(birmX+1, birmY-1), 0.5, GOBLIN_HEALTH, &imageLibrary->GuardIM, audio, 0, HUMAN_CRECT);
guard3->setSpeed(2);
npcAI* ai3 = new WaveAI(guard3);
ai3->setSight(12);
Interface->getMain()->addEntity(guard3, ai3);
Interface->getMain()->addEntity(birm);
actionMenu->addButton(new Button("Sword", &imageLibrary->SwordIconIM, 0));
player->hasAddedSword = true;
}
if(player->hasFireball && !player->hasAddedFireball)
{
actionMenu->addButton(new Button("Fireball", &imageLibrary->FireballIconIM, 4));
player->hasAddedFireball = true;
}
if (Interface->getMain()->winCondition())
{
currentState = GAME_OVER;
audio->stopCue(SOUTHFALL_THEME);
audio->stopCue(MAIN_THEME);
audio->stopCue(BATTLE);
audio->playCue(WIN);
}
if(input->wasKeyPressed(T_KEY))
{// Open action menu
pause = true;
currentState = ACTIONMENU;
}
else
{
playerClickActions();
//if player has died, then...
if(!player->getWorld()->update(Center(), frameTime))
{
player->setWorld(Interface->getStart());
player->reset();
currentState = INTRO;
loadIntro();
textbox->setText(*introText);
textbox->setActive(true);
audio->stopCue(SOUTHFALL_THEME);
audio->stopCue(MAIN_THEME);
audio->stopCue(BATTLE);
fontLoc = 0;
break;
}
}
if (textbox->isActive())
{
pause = true;
currentState = OPENTEXTBOX;
}
handleFX();
break;
case ACTIONMENU:
actionMenu->update(frameTime);
if(input->wasKeyPressed(T_KEY))
currentState = GAME;
if(actionMenu->getSelected() != -1)
{
if(actionMenu->getSelected() == MAGICSIGHTON)
player->setMagicSight(true);
else if(actionMenu->getSelected() == MAGICSIGHTOFF)
player->setMagicSight(false);
else player->setSpellType(SPELLTYPE(actionMenu->getSelected()));
player->resetTarget();
}
break;
}
}
inline void Southfall::handleFX()
{
if(player->getWorld() == Interface->getMain())
{
if (player->getPosition().x < 133 && player->getPosition().x > 86 && player->getPosition().y > 80 && fontLoc != 0)
{
audio->stopCue(BATTLE);
audio->stopCue(MAIN_THEME);
audio->playCue(SOUTHFALL_THEME);
fontTimer = 6; fontLoc = 0;
}
else if (player->getPosition().x > 160 && fontLoc != 1)
{
audio->stopCue(BATTLE);
audio->stopCue(SOUTHFALL_THEME);
audio->playCue(MAIN_THEME);
fontTimer = 6; fontLoc = 1;
}
else if (player->getPosition().y < 65 && fontLoc != 3)
{
fontTimer = 6; fontLoc = 3;
audio->stopCue(SOUTHFALL_THEME);
audio->stopCue(MAIN_THEME);
if (firstTimeBattle)
{
audio->playCue(BATTLE_INTRO);
Sleep(3400);
firstTimeBattle = false;
}
audio->playCue(BATTLE);
}
else if (player->getPosition().x < 50 && fontLoc != 2)
{
audio->stopCue(BATTLE);
audio->stopCue(SOUTHFALL_THEME);
audio->playCue(MAIN_THEME);
fontTimer = 6; fontLoc = 2;
}
}
}
inline void Southfall::playerClickActions()
{
if(player->canAction() && input->getMouseLButton())
{
int X = input->getMouseX();
int Y = input->getMouseY();
VECTOR2 mouse(X,Y);
VECTOR2 target = player->getPosition()+(mouse-HSCREEN)*INVTILE_SIZE;
float sY = static_cast<float>(Y - (int)HSCREEN_HEIGHT);
float sX = static_cast<float>(X - (int)HSCREEN_WIDTH);
float orient = atan2(sY, sX);
Projectile* P;
switch (player->getSpellType())
{
case NOSPELL:
if (!player->isAttacking()) audio->playCue(HOOH);
player->attack(orient);
break;
case IMPEDE:
player->getWorld()->addEffect(new ImpedeEffect(target, 0.2, &imageLibrary->ImpedeEffectIM));
player->resetAction();
break;
case QUICKPORTAL:
if(player->hasTarget())
{
if(!player->getWorld()->collidesWithTile(player, target))
{
player->getWorld()->addEffect(new QuickPortal(player->getTarget(),
target, 0.5, &imageLibrary->PortalOpenIM, &imageLibrary->PortalCloseIM));
player->resetTarget();
}
}
else if(!player->getWorld()->collidesWithTile(player, target)) player->setTarget(target);
player->resetAction();
break;
case BLINK:
if(player->getWorld()->canMoveHere(player, target))
{
player->newPosition(target, player->getWorld());
player->resetAction();
}
break;
case FIREBALL:
P = new Projectile(player->getPosition(), FIREBALLSPEED, FIREBALLRADIUS,
FIREBALLRANGE, orient, &imageLibrary->FireballSheetIM, FIREBALLDAMAGE, PROJ_FIREBALL);
P->setFrames(FIREBALLSTART, FIREBALLEND);
P->setFrameDelay(0.1);
player->getWorld()->addProjectile(P);
player->resetAction();
audio->playCue(FIREBALL_SOUND);
break;
case FREEZE:
break;
case SHADOWBALL:
P = new Projectile(player->getPosition(), FIREBALLSPEED, FIREBALLRADIUS,
FIREBALLRANGE, orient, &imageLibrary->ShadowballSheetIM, 10, PROJ_SHADOWBALL, 0.f, 1.0f);
P->setFrames(FIREBALLSTART, FIREBALLEND);
P->setFrameDelay(0.1);
player->getWorld()->addProjectile(P);
player->resetAction();
break;
case MAGICSIGHTON: // Not a castable spell
case MAGICSIGHTOFF: // Not a castable spell
case NULLTYPE:
default:
break;
}
}
if(input->getMouseRButton()) player->resetTarget();
}
//=============================================================================
// Artificial Intelligence
//=============================================================================
void Southfall::ai()
{
player->getWorld()->act();
}
//=============================================================================
// Handle collisions
//=============================================================================
void Southfall::collisions()
{
player->getWorld()->collisions();
}
//=============================================================================
// Render game items
//=============================================================================
void Southfall::render()
{// sprite begin and end in game now
switch(currentState)
{
case MAIN_MENU:
mainMenu->draw();
break;
case INTRO:
textbox->draw();
break;
case GAME:
player->getWorld()->draw(Center(), player->usingMagicSight());
char out[100];
sprintf(out, "Goblins killed: %i", Entity::killCount);
gameFont->print(out, 0, 0);
if(fontTimer >= 0)
{
if(fontTimer >= 4)
imageLibrary->SouthfallFontIM[fontLoc].draw(SETCOLOR_ARGB(int((6-fontTimer)*80),255,255,255));
else if(fontTimer < 2)
imageLibrary->SouthfallFontIM[fontLoc].draw(SETCOLOR_ARGB(int(fontTimer*80),255,255,255));
else
imageLibrary->SouthfallFontIM[fontLoc].draw(SETCOLOR_ARGB(160,255,255,255));
fontTimer -= frameTime;
}
break;
case BIRMINGHAMSTATE:
renderBirmingham();
break;
case ACTIONMENU:
player->getWorld()->draw(Center(), player->usingMagicSight());
actionMenu->draw();
break;
case OPENTEXTBOX:
player->getWorld()->draw(Center(), player->usingMagicSight());
textbox->draw();
break;
case GAME_OVER:
Image* temp;
if (Interface->getMain()->winCondition()) temp = &imageLibrary->WinIM;
else temp = &imageLibrary->DeadIM;
temp->setX(SCREEN_WIDTH/2 - temp->getWidth()/2);
temp->setY(SCREEN_HEIGHT/2 - temp->getHeight()/2);
temp->draw();
//if (input->anyKeyPressed()) done = true;
break;
}
}
//=============================================================================
// The graphics device was lost.
// Release all reserved video memory so graphics device may be reset.
//=============================================================================
void Southfall::releaseAll()
{
Game::releaseAll();
return;
}
//=============================================================================
// The grahics device has been reset.
// Recreate all surfaces.
//=============================================================================
void Southfall::resetAll()
{
Game::resetAll();
return;
}
void Southfall::renderBirmingham()
{
Image *BirminghamIM = &imageLibrary->BirminghamIM;
if(BirminghamIM->getScale() < .1)
currentState = GAME;
BirminghamIM->setX(SCREEN_WIDTH/2-BirminghamIM->getWidth()*BirminghamIM->getScale()/2);
BirminghamIM->setY(SCREEN_HEIGHT/2-BirminghamIM->getHeight()*BirminghamIM->getScale()/2);
BirminghamIM->draw();
BirminghamIM->setScale(BirminghamIM->getScale()*.996);
BirminghamIM->setRadians(BirminghamIM->getRadians()+birminghamRot);
birminghamRot += .0001;
}