Subversion Repositories Kolibri OS

Rev

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