Subversion Repositories Kolibri OS

Rev

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