summaryrefslogtreecommitdiff
path: root/osframework/source/demos/Hun-garr/LevelupEffect.cpp
blob: 7c728a0a006190a5d5a120fcbb707d6ec8557e2a (plain)
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
#pragma warning(disable:4244) 
#pragma warning(disable:4018)


#include "LevelupEffect.h"
#include "Board.h"
#include "Res.h"
#include "SexyAppFramework/SexyAppBase.h"
#include "SexyAppFramework/Graphics.h"
#include "SexyAppFramework/Font.h"

using namespace Sexy;

const int STRIP_WIDTH	= 20;

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
LevelupEffect::LevelupEffect()
{
	Init();
	mActive = false;
	mCoverWidth = 0;
	mStripSizeChange = 60;
	mStripHeight = 0;
	mFadeOutAlpha = 255;
	mStartNextLevel = false;
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void LevelupEffect::Init()
{
	mText.clear();

	//////////////////////////////////////////////////////////////////////////
	//	Each letter, starting with the 'L' in "LEVEL UP!", will drop, delayed
	//	by a height of FONT_HUNGARR->GetHeight() pixels. Each letter will initially
	//	drop at a rate of 5 pixels per update (500 pixels per second). Once they
	//	reach their target Y coordinate (300), the speed will decrease and reverse
	//	to make the letters bounce upward. Gravity is applied each frame, so eventually
	//	the letters return back to Y of 300, where the speed is again decreased and
	//	reversed until it drops below the threshhold speed of 0.1 pixels per update.
	//////////////////////////////////////////////////////////////////////////	

	float x = (GRID_END_X - GRID_START_X) / 2 - FONT_HUNGARR->StringWidth(_S("LEVEL UP!")) / 2;
	float y = FONT_HUNGARR->GetHeight();
	float startingY = y;
	float ydec = y;
	float speed = 5.0f;

	mText.push_back(BouncyChar(_S("L"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("L"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("E"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("E"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("V"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("V"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("E"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("E"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("L"), x, y, speed));



	x += FONT_HUNGARR->StringWidth(_S("L "));
	y -= ydec;
	mText.push_back(BouncyChar(_S("U"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("U"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("P"), x, y, speed));

	x += FONT_HUNGARR->StringWidth(_S("P"));
	y -= ydec;
	mText.push_back(BouncyChar(_S("!"), x, y, speed));
	
	mDone = false;
	mHue = 0;
	mCurtainX = 0;
	mState = LevelupEffect::LEVELUP_TEXT;	
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void LevelupEffect::Activate(LevelupStats ls)
{
	Init();
	mActive = true;
	mStats = ls;

	gSexyAppBase->PlaySample(SOUND_LEVEL_UP1);
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void LevelupEffect::Update(float theFrac)
{

	// HSL is an alternative to specifying an RGB color format.
	// Using HSL lets us easily do the hyper blinking crazy weird
	// flashing effect commonly found in old games, such as Robotron.
	// Below, we increment the value by 7 per update. The &0xFF is an 
	// easy way to clamp the value between 0 and 255 instead of having to
	// do a separate if (mHue > 255) mHue -= 255. This lets the value
	// rollover and keep cycling.
	mHue = (mHue + 7) & 0xFF;

	if (mState == LevelupEffect::LEVELUP_TEXT)
	{
		// Move all the letters, applying 0.15 pixels per update of
		// "gravity" to pull the letters down. Once reaching Y of 300,
		// the speed of the letters decreases by 0.75 and is reversed, making
		// them "bounce" in the up direction. Once this bounce speed drops below 0.1
		// pixels per update, the letter is done bouncing. When all letters are done bouncing,
		// switch to the next state.
		bool allDone = true;
		for (int i = 0; i < mText.size(); i++)
		{
			BouncyChar* c = &mText[i];

			if (c->mDone)
				continue;

			c->mY += c->mBounceSpeed;
			
			c->mBounceSpeed += 0.15f;

			if (c->mY >= 300)
			{
				c->mY = 300;

				c->mOldBounceSpeed -= 0.750f;
				if (c->mOldBounceSpeed <= 0.1f)
				{
					c->mDone = true;
					c->mOldBounceSpeed = 0;
				}
				else
				{
					allDone = false;
					c->mBounceSpeed = -c->mOldBounceSpeed;
				}
			}
			else
				allDone = false;
		}

		if (allDone)
			mState = LevelupEffect::CURTAIN_IN;
	}
	else if (mState == LevelupEffect::CURTAIN_IN)
	{
		// Moves the black rectangles ("curtains") inward to cover up the
		// previous level. We only move the left one. The right one is logically
		// just the same width but starting from the right of the screen instead
		// of the left. The curtain moves 25 pixels per update. Once it reaches
		// the center (and thus fully covers the screen), we switch to the next state.
		if ((mCurtainX += 25) >= gSexyAppBase->mWidth / 2)
		{
			mCurtainX = gSexyAppBase->mWidth / 2;
			mState = LevelupEffect::CURTAIN_OUT;
			gSexyAppBase->PlaySample(SOUND_LEVEL_UP2);
		}
	}
	else if (mState == LevelupEffect::CURTAIN_OUT)
	{
		// Now the curtain is red and slowly moves back out to reveal the
		// stats below it. This works just like above.
		if ((mCurtainX -= 2) <= 0)
			mState = LevelupEffect::SHOWING_STATS;
	}
	else if (mState == LevelupEffect::COVERING_STATS)
	{
		// This state begins when the user clicks the mouse to dismiss the
		// stats screen. We then make these strips quickly appear. Imagine
		// rectangles of width 20 coming in from the left and right of the screen.
		// The left one starts comign from the top left of the screen, the right
		// from the bottom right. These strips quickly expand to be screen height in size
		// and then move closer inward (by their width of 20) and reverse the direction
		// that they come in at. Once the strips have traveled to the center of the screen,
		// it's fully covered and we then move to the next state. After a strip becomes
		// the height of the screen, we update mCoverWidth, which is just simply the width
		// of the region fully filled in. Again, like with the curtain effects, we only
		// keep track of the left side, since the right moves the same amount and it's
		// easy to compute the right's offsets. 
		if (mStripSizeChange > 0)
		{
			// Left strip is moving downward from Y of 0, right is moving up
			// from Y of app height
			if ((mStripHeight += mStripSizeChange) >= gSexyAppBase->mHeight)
			{
				// Left strip and right strip are now the height of the screen.
				// Reverse the direction they come in at and reset their heights.
				// Then, increment the width of the fully covered region by the
				// size of one of the strip's width.
				mStripSizeChange = -mStripSizeChange;
				mStripHeight = 0;
				if ((mCoverWidth += STRIP_WIDTH) >= gSexyAppBase->mWidth / 2)
				{
					// The screen is fully covered. Fade out.
					mStartNextLevel = true;
					mState = LevelupEffect::FADE_OUT_STATS;
				}
			}
		}
		else
		{
			if ((mStripHeight -= mStripSizeChange) >= gSexyAppBase->mHeight)
			{
				mStripSizeChange = -mStripSizeChange;
				mStripHeight = 0;
				if ((mCoverWidth += STRIP_WIDTH) >= gSexyAppBase->mWidth / 2)
				{
					mStartNextLevel = true;
					mState = LevelupEffect::FADE_OUT_STATS;
				}
			}
		}				
	}	
	else if (mState == LevelupEffect::FADE_OUT_STATS)
	{
		//Fade the screen out, with the next level appearing below it.
		if ((mFadeOutAlpha -= 2) <= 0)
		{
			mDone = true;
			mActive = false;
		}
	}
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void LevelupEffect::Draw(Graphics* g)
{
	g->SetFont(FONT_HUNGARR);
	if (mState == LevelupEffect::LEVELUP_TEXT)
	{		
		// This is how we convert an HSL value to an RGB value, which we have to
		// do to specify the color for the graphics object. The function HSLToRGB
		// takes as parameters: hue, saturation, luminance. We want to leave the
		// saturation at max and luminance at half for our particular example.
		// The returned value is ANDed with 0xFFFFFFFF to clamp the values for
		// the alpha, red, green, and blue to the valid region of 0 to 255. 
		g->SetColor( (gSexyAppBase->HSLToRGB(mHue, 255, 128) & 0xFFFFFFFF) );
		for (int i = 0; i < mText.size(); i++)
		{
			BouncyChar* c = &mText[i];
			g->DrawString(c->mChar, c->mX, c->mY);
		}
	}
	else if (mState == LevelupEffect::CURTAIN_IN)
	{
		// The righ tcurtain is just the same width as the left, but starts from the right
		// side instead of X of 0.
		g->SetColor(Color(0, 0, 0));
		g->FillRect(0, 0, mCurtainX, gSexyAppBase->mHeight);
		g->FillRect(gSexyAppBase->mWidth - mCurtainX, 0, mCurtainX, gSexyAppBase->mHeight);
	}
	else if ((mState == LevelupEffect::SHOWING_STATS) || (mState == LevelupEffect::CURTAIN_OUT) || 
				(mState == LevelupEffect::COVERING_STATS))
	{
		// When just showing the stats normally, fading out the red curtain (to reveal the stats
		// beneath it), or covering up the stats with the effect triggered when the user clicks
		// to begin the next level, we display info on the user's performance from the last level.
		int y = 50;
		g->SetColor(gSexyAppBase->HSLToRGB(mHue, 255, 128) & 0xFFFFFFFF);
		SexyString s = StrFormat(_S("LEVEL %d COMPLETE!"), mStats.mLevelCompleted);
		g->DrawString(s, gSexyAppBase->mWidth / 2 - FONT_HUNGARR->StringWidth(s) / 2, y);

		g->SetColor(Color::White);
		s = _S("POPULATION CONSUMED:");
		int strWidth = FONT_HUNGARR->StringWidth(s);

		int rightX = strWidth + 100;
		y += 50;
		g->DrawString(s, 100, y);
		g->SetColor(Color(255, 0, 0));
		g->DrawString(CommaSeperate(mStats.mPopulationEaten), rightX + 5, y);

		y += FONT_HUNGARR->GetHeight();
		g->SetColor(Color::White);		
		s = _S("SYSTEMS SUBJUGATED:");
		strWidth = FONT_HUNGARR->StringWidth(s);
		g->DrawString(s, rightX - strWidth, y);
		g->SetColor(Color(255, 0, 0));
		g->DrawString(StrFormat(_S("%d%%"), mStats.mPercentComplete), rightX + 5, y);
		
		y += FONT_HUNGARR->GetHeight();
		if (mStats.mPercentComplete >= COMPLETION_BONUS_PCT)
		{
			s = StrFormat(_S("%d%%+ BONUS:"), COMPLETION_BONUS_PCT);
			g->SetColor(Color::White);
			strWidth = FONT_HUNGARR->StringWidth(s);
			g->DrawString(s, rightX - strWidth, y);
			g->SetColor(Color(255, 0, 0));
			g->DrawString(StrFormat(_S("%d"), COMPLETION_BONUS * mStats.mLevelCompleted), 
							rightX + 5, y);
		}

		if (mStats.mPlanetsEaten.size() > 0)
		{
			y += 50;
			int third =  gSexyAppBase->mWidth / 3;
			g->SetColor(Color(255, 255, 0));

			s = _S("PLANET EATEN:");
			g->DrawString(s, third / 2 - FONT_HUNGARR->StringWidth(s) / 2, y);
			s = _S("EXPORTS:");
			g->DrawString(s, third + (third / 2 - FONT_HUNGARR->StringWidth(s) / 2), y);
			s = _S("POPULATION:");
			g->DrawString(s, third*2 + (third / 2 - FONT_HUNGARR->StringWidth(s) / 2), y);
			
			y += FONT_HUNGARR->GetHeight();

			// If the user ate too many planets to fit on screen, we'll just display "..."
			// to indicate that they ate a bunch but we just can't fit it all on screen. 
			// In reality, it'd be best to either ensure that all the planets fit on screen,
			// or that there's some sort of scrolling mechanism to allow the user to view all their
			// stats.
			bool drawDotDotDot = false;
			for (int i = 0; i < mStats.mPlanetsEaten.size(); i += 3)
			{
				if (y >= gSexyAppBase->mHeight - FONT_HUNGARR->GetHeight() * 2)
				{
					drawDotDotDot = true;
					break;
				}
				
				g->SetColor(Color(255, 255, 255));
				s = mStats.mPlanetsEaten[i];
				g->DrawString(s, third / 2 - FONT_HUNGARR->StringWidth(s) / 2, y);

				g->SetColor(Color(128, 255, 0));
				s = mStats.mPlanetsEaten[i+1];
				g->DrawString(s, third + (third / 2 - FONT_HUNGARR->StringWidth(s) / 2), y);

				g->SetColor(Color(255, 128, 0));
				s = mStats.mPlanetsEaten[i+2];
				g->DrawString(s, third*2 + (third / 2 - FONT_HUNGARR->StringWidth(s) / 2), y);
				
				y += FONT_HUNGARR->GetHeight();

			}

			if (drawDotDotDot)
			{
				g->SetColor(Color::White);
				g->DrawString(_S("..."), 5, y);
			}
		}

		g->SetColor(Color::White);
		s = _S("CLICK TO CONTINUE");
		g->DrawString(s, gSexyAppBase->mWidth / 2 - FONT_HUNGARR->StringWidth(s) / 2, gSexyAppBase->mHeight - 20);		
	}

	if (mState == LevelupEffect::CURTAIN_OUT)
	{
		// Draw a red curtain, whose alpha decreases as it withdrawls more.
		// This lets the stats display underneath the curtain while it moves.
		int alpha = mCurtainX > 255 ? 255 : mCurtainX;
		g->SetColor(Color(255, 0, 0, alpha));
		g->FillRect(0, 0, mCurtainX, gSexyAppBase->mHeight);
		g->FillRect(gSexyAppBase->mWidth - mCurtainX, 0, mCurtainX, gSexyAppBase->mHeight);
	}

	if ((mState == LevelupEffect::COVERING_STATS) || (mState == LevelupEffect::FADE_OUT_STATS))
	{
		// Both the states where we cover up the stats and fade them out are controlled here,
		// since they are functionally the same. Again, with the HSL we do the usual HSLToRGB
		// stuff. However, since the FADE_OUT_STATS state actually fades out the screen and
		// has an alpha value that decreases over time (mFadeOutAlpha), instead of always
		// setting the alpha to 255, we need to set the alpha to mFadeOutAlpha. To do this,
		// we AND with 0xFFFFFF instead of 0xFFFFFFFF. Then, we OR it with the alpha value
		// shifted left 24 bits. This then sticks the alpha value, which changes over time,
		// into our HSL color. As you may recall from previous demos, the actual color
		// structure is 32 bit, and looks like this in binary form:
		//	AAAA RRRR GGGG BBBB  Where A,R,G,B are alpha, red, green, blue. 
		//
		// We draw the totally filled in regions separately, since they're easy.
		// The strips then are drawn differently depending on if they are moving up or down.
		// The left and right ones move oppositely.
		g->SetColor( (gSexyAppBase->HSLToRGB(mHue, 255, 128) & 0xFFFFFF) | (mFadeOutAlpha << 24) );
		g->FillRect(0, 0, mCoverWidth, gSexyAppBase->mHeight);
		g->FillRect(gSexyAppBase->mWidth - mCoverWidth, 0, mCoverWidth, gSexyAppBase->mHeight);

		if (mStripSizeChange > 0)
		{
			g->FillRect(mCoverWidth, 0, STRIP_WIDTH, mStripHeight);
			g->FillRect(gSexyAppBase->mWidth - mCoverWidth - STRIP_WIDTH,
						gSexyAppBase->mHeight - mStripHeight,
						STRIP_WIDTH,
						mStripHeight);
		}
		else
		{			
			g->FillRect(mCoverWidth, gSexyAppBase->mHeight - mStripHeight, STRIP_WIDTH, mStripHeight);
			g->FillRect(gSexyAppBase->mWidth - mCoverWidth - STRIP_WIDTH, 0, STRIP_WIDTH, mStripHeight);
		}

	}
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void LevelupEffect::DoneViewingStats()
{
	// The user clicked to dismiss the stats screen. Cover it up
	// and begin the transition to the next level.
	mState = LevelupEffect::COVERING_STATS;
	mCoverWidth = 0;
	mStripSizeChange = 90;
	mStripHeight = 0;
	mFadeOutAlpha = 255;
	mStartNextLevel = false;
	gSexyAppBase->PlaySample(SOUND_LEVEL_UP3);
}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
bool LevelupEffect::StartNextLevel()
{
	// See function header for a complete description
	if (mStartNextLevel)
	{
		mStartNextLevel = false;
		gSexyAppBase->PlaySample(SOUND_LEVEL_UP4);
		return true;
	}

	return false;
}