Subversion Repositories Kolibri OS

Rev

Rev 7479 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1805 yogev_ezra 1
/* Rocket Forces
2
 * Filename: rforces.cpp
3
 * Version 0.1.1
4
 * Copyright (c) Serial 2007
5
 */
6
 
7
 
8
#include 
9
 
10
#include "kosSyst.h"
11
#include "kosFile.h"
12
#include "mymath.h"
13
#include "properties.h"
14
#include "objects.h"
15
#include "rforces.h"
16
 
17
 
18
const char header[] = GAME_NAME;
19
cCursor *cursor;
20
cGun *gun = new cGun;
21
cCross **crosses = new cCross*[R_COUNT];
22
cRocket **rockets = new cRocket*[R_COUNT];
23
cBomb **bombs = new cBomb*[B_COUNT];
24
cExplode **explodes = new cExplode*[R_COUNT + B_COUNT];
25
cBuilding *house = new cBuilding();
26
Dword *cur_handle;
27
int score, health;
28
 
29
struct MouseState
30
{
31
	int x, y, lbclick;
32
	Dword buttons;
33
} ms;
34
 
35
 
36
void kos_Main()
37
{
38
	Dword frame_start, frame_end;
39
	OnStart();
40
	Menu();
41
	for (;;)
42
	{
43
		frame_start = kos_GetTime();
44
		switch (kos_CheckForEvent())
45
		{
46
		case 1:
47
			DrawWindow();
48
			break;
49
		case 2:	// key pressed, read it and ignore
50
			Byte keyCode;
51
			kos_GetKey(keyCode);
52
			if (keyCode == 27)
53
			{
54
				OnExit();
55
			}
56
			if (keyCode == 51)
57
			{
58
				OnStart();
59
			}
60
			break;
61
		case 3: // button pressed; we have only one button, close
62
			OnExit();
63
			break;
64
		case 6: // событие от мыши (нажатие на кнопку мыши или перемещение; сбрасывается при прочтении)
65
			OnMouseMove();
66
			if (ms.lbclick == 1)
67
			{
68
				OnLMBClick();
69
			}
70
			break;
71
		default:
72
			OnMouseMove();
73
			break;
74
		}
75
		DrawBombs();
76
		DrawRocketsAndCrosses();
77
		DrawExplodes();
78
		frame_end = kos_GetTime();
79
		if (frame_end - frame_start < FRAME_TIME)
80
		{
81
			kos_Pause(FRAME_TIME - (frame_end - frame_start));
82
		}
83
		if (health <= 0)
84
		{
85
			OnExit();
86
		}
87
	}
88
}
89
 
90
void DrawWindow()
91
{
92
	kos_WindowRedrawStatus(1);
93
	kos_DefineAndDrawWindow(10, 40, WINDOW_WIDTH + 8, WINDOW_HEIGHT + 25, 0x33, BG_COLOR, 0, 0, (Dword)header);
94
	kos_WindowRedrawStatus(2);
95
 
96
	OnMouseMove();
97
 
98
	// Draw buildings
99
	for (int i = 20; i < 5 * 50; i += 50)
100
	{
101
		house->Draw(i, 467, H_COLOR);
102
	}
103
	for (int i = 8 * 50; i < 13 * 50; i += 50)
104
	{
105
		house->Draw(i, 467, H_COLOR);
106
	}
107
 
108
}
109
 
110
void DrawBombs()
111
{
112
	for (int i = 0; i < B_COUNT; i++)
113
	{
114
		if (bombs[i]->IsEnabled() == 0)
115
		{
116
			int rnd;
117
			rnd = rtlRand() % B_POSSIBILITY;
118
			if (rnd == 1)
119
			{
120
				rnd = 10 + rtlRand() % 620;
121
				bombs[i]->Enable(rnd, 0, 4, 9, rnd + 2, 0);
122
			}
123
		}
124
		else
125
		{
126
			if (bombs[i]->cy > gun->cy + 5)
127
			{
128
				health -= 5;
129
				if (explodes[R_COUNT + i]->IsEnabled() == 1)
130
				{
131
					explodes[R_COUNT + i]->Disable(BG_COLOR);
132
				}
133
				explodes[R_COUNT + i]->Enable(bombs[i]->cx, bombs[i]->cy);
134
				bombs[i]->Disable(BG_COLOR);
135
			}
136
			else
137
			{
138
				bombs[i]->cy += B_SPEED;
139
				bombs[i]->DrawAngle(bombs[i]->cx, 639, B_COLOR);
140
			}
141
		}
142
	}
143
}
144
 
145
void DrawRocketsAndCrosses()
146
{
147
	double a;
148
	for (int i = 0; i < R_COUNT; i++)
149
	{
150
		if (crosses[i]->IsEnabled() == 1)
151
		{
152
			if (sqrt(((long int) (crosses[i]->x - rockets[i]->cx) * (crosses[i]->x - rockets[i]->cx)) + ((long int) (crosses[i]->y - rockets[i]->cy) * (crosses[i]->y - rockets[i]->cy))) < 5)
153
			{
154
				if (explodes[i]->IsEnabled() == 1)
155
				{
156
					explodes[i]->Disable(BG_COLOR);
157
				}
158
				explodes[i]->Enable(crosses[i]->x, crosses[i]->y);
159
				crosses[i]->Disable(BG_COLOR);
160
				rockets[i]->Disable(BG_COLOR);
161
			}
162
			else
163
			{
164
				crosses[i]->Draw(CROSS_COLOR);
165
				if (rockets[i]->cx - crosses[i]->x == 0)
166
				{
167
					a = M_PI / 2;
168
				}
169
				else
170
				{
171
					a = atan((double)(rockets[i]->cy - crosses[i]->y) / (double)(rockets[i]->cx - crosses[i]->x));
172
					if (rockets[i]->cx - crosses[i]->x < 0) a += M_PI;
173
				}
174
				rockets[i]->cx = round_int(rockets[i]->cx - R_SPEED * cos(a));
175
				rockets[i]->cy = round_int(rockets[i]->cy - R_SPEED * sin(a));
176
				rockets[i]->DrawAngle(crosses[i]->x, crosses[i]->y, R_COLOR);
177
			}
178
		}
179
	}
180
}
181
 
182
void DrawExplodes()
183
{
184
	for (int i = 0; i < R_COUNT + B_COUNT; i++)
185
	{
186
		if (explodes[i]->IsEnabled() == 1)
187
		{
188
			explodes[i]->DrawNext(EXP_COLOR);
189
			for (int j = 0; j < B_COUNT; j++)
190
			{
191
				if ( bombs[j]->IsEnabled() == 1 &&
192
					 bombs[j]->cx > explodes[i]->cx - explodes[i]->step - 1 && bombs[j]->cx < explodes[i]->cx + explodes[i]->step + 1 &&
193
					 bombs[j]->cy + 5 > explodes[i]->cy - explodes[i]->step - 1 && bombs[j]->cy + 5 < explodes[i]->cy + explodes[i]->step + 1
194
					)
195
				{
196
					score += B_COUNT + 2;
197
					if (explodes[R_COUNT + j]->IsEnabled() == 1)
198
					{
199
						explodes[R_COUNT + j]->Disable(BG_COLOR);
200
					}
201
					explodes[R_COUNT + j]->Enable(bombs[j]->cx, bombs[j]->cy);
202
					bombs[j]->Disable(BG_COLOR);
203
				}
204
			}
205
		}
206
	}
207
}
208
 
209
void OnMouseMove()
210
{
211
	Dword old_buttons = ms.buttons;
212
	kos_GetMouseWindowXY(ms.x, ms.y);
213
	kos_GetMouseButtonsState(ms.buttons);
214
	if ((old_buttons & 0x00000001) == 0 && (ms.buttons & 0x00000001) == 1)
215
	{
216
		ms.lbclick = 1;
217
	}
218
	else
219
	{
220
		ms.lbclick = 0;
221
	}
222
 
223
 
224
	kos_WriteTextToWindow(8, 10, 0, TEXT_COLOR, "Population:    %", 16);
225
	kos_DisplayNumberToWindowBg(health, 3, 79, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
226
	kos_WriteTextToWindow(8, 22, 0, TEXT_COLOR, "Score:", 6);
227
	kos_DisplayNumberToWindowBg(score, 4, 49, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
228
 
229
	if (ms.x >= 0 && ms.x < WINDOW_WIDTH &&  ms.y >= 0 && ms.y < WINDOW_HEIGHT)
230
	{
231
		gun->DrawAngle(ms.x, ms.y, G_COLOR);
232
	}
233
 
234
	if (HARDWARE_CURSOR == 0)
235
	{
236
		cursor->Draw(ms.x, ms.y, CUR_COLOR);
237
	}
238
 
239
	/*if (DEBUG == 1)
240
	{
241
		kos_DisplayNumberToWindowBg(ms.x, 3, WINDOW_WIDTH - 30, 10, TEXT_COLOR, BG_COLOR, nbDecimal, false);
242
		kos_DisplayNumberToWindowBg(ms.y, 3, WINDOW_WIDTH - 30, 22, TEXT_COLOR, BG_COLOR, nbDecimal, false);
243
		kos_DisplayNumberToWindowBg(ms.buttons, 1, WINDOW_WIDTH - 30, 34, TEXT_COLOR, BG_COLOR, nbDecimal, false);
244
	}*/
245
 
246
}
247
 
248
void OnLMBClick()
249
{
250
	if (ms.y < gun->cy - 10)
251
	{
252
		double a;
253
		int j = -1;
254
		for (int i = 0; i < R_COUNT; i++)
255
		{
256
			if (crosses[i]->IsEnabled() == 0)
257
			{
258
				if (j >= -1) j = i;
259
			}
260
			else if (ms.x > crosses[i]->x - 10 && ms.x < crosses[i]->x + 10 && ms.y > crosses[i]->y - 10 && ms.y < crosses[i]->y + 10)
261
			{
262
				j = -2;
263
				break;
264
			}
265
		}
266
		if (j >= 0)
267
		{
268
			if (score > 0) score -= 1;
269
			crosses[j]->Enable(ms.x, ms.y);
270
			if (gun->cx - ms.x == 0)
271
			{
272
				a = M_PI/2;
273
			}
274
			else
275
			{
276
				a = atan((double)gun->cy - ms.y / (double) gun->cx - ms.x);
277
				if (gun->cx - ms.x < 0) a += M_PI;
278
			}
279
			rockets[j]->Enable(round_int(gun->cx - 15 * cos(a)) - 2, round_int(gun->cy - 15 * sin(a)) - 5, 3, 6, round_int(gun->cx - 15 * cos(a)), round_int(gun->cy - 15 * sin(a)));
280
		}
281
	}
282
}
283
 
284
void OnRMBClick()
285
{
286
}
287
 
288
void ChangeCursor()
289
{
290
	Dword *cur = new Dword[1024];
291
	for (int i = 0; i < 1024; i++)
292
	{
293
		cur[i] = 0x00000000;
294
	}
295
	if (HARDWARE_CURSOR == 1)
296
	{
297
		Dword cur_color = 0xFF000000 | CUR_COLOR;
298
		cur[0 * 32 + 5] = cur_color;
299
		cur[1 * 32 + 5] = cur_color;
300
		cur[2 * 32 + 5] = cur_color;
301
		cur[2 * 32 + 3] = cur_color;
302
		cur[2 * 32 + 4] = cur_color;
303
		cur[2 * 32 + 6] = cur_color;
304
		cur[3 * 32 + 2] = cur_color;
305
		cur[4 * 32 + 2] = cur_color;
306
		cur[5 * 32 + 2] = cur_color;
307
		cur[5 * 32 + 1] = cur_color;
308
		cur[5 * 32 + 0] = cur_color;
309
 
310
		cur[5 * 32 + 5] = cur_color;
311
 
312
		cur[8 * 32 + 4] = cur_color;
313
		cur[8 * 32 + 5] = cur_color;
314
		cur[8 * 32 + 6] = cur_color;
315
		cur[8 * 32 + 7] = cur_color;
316
		cur[9 * 32 + 5] = cur_color;
317
		cur[10 * 32 + 5] = cur_color;
318
		cur[7 * 32 + 8] = cur_color;
319
		cur[6 * 32 + 8] = cur_color;
320
		cur[5 * 32 + 8] = cur_color;
321
		cur[5 * 32 + 9] = cur_color;
322
		cur[5 * 32 + 10] = cur_color;
323
	}
324
	cur_handle = kos_LoadMouseCursor(cur, 0x05050002);
325
	delete[] cur;
326
	kos_SetMouseCursor(cur_handle);
327
}
328
 
329
void Menu()
330
{
331
	NewGame();
332
}
333
 
334
void NewGame()
335
{
336
	gun->DrawAngle((WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20, G_COLOR);
337
}
338
 
339
void OnStart()
340
{
341
	if (HARDWARE_CURSOR == 0)
342
	{
343
		cursor = new cCursor();
344
	}
345
	ChangeCursor();
346
 
347
	gun->Enable((WINDOW_WIDTH / 2) - 10, WINDOW_HEIGHT - 30, 10, 20, (WINDOW_WIDTH / 2) - 5, WINDOW_HEIGHT - 20);
348
 
349
	for (int i = 0; i < R_COUNT; i++)
350
	{
351
		crosses[i] = new cCross();
352
		rockets[i] = new cRocket();
353
	}
354
	for (int i = 0; i < B_COUNT; i++)
355
	{
356
		bombs[i] = new cBomb();
357
	}
358
	for (int i = 0; i < R_COUNT + B_COUNT; i++)
359
	{
360
		explodes[i] = new cExplode();
361
	}
362
 
363
	health = 100;
364
	score = 0;
365
 
366
	rtlSrand(kos_GetTime());
367
 
368
	DrawWindow();
369
	kos_SetMaskForEvents(39);
370
}
371
 
372
void OnExit()
373
{
374
	kos_WriteTextToWindow(WINDOW_WIDTH / 2 - 35, WINDOW_HEIGHT / 2 - 10, 0, TEXT_COLOR, "Game Over", 9);
375
 
376
	kos_Pause(200);
377
 
378
	kos_ExitApp();
379
}