1 /********************************************************************
2   Mega Mario SRC
3     created:	2005/09/18
4 	created:	18:9:2005   10:13
5 	author:		Jens Wellmann (c)
6 *********************************************************************/
7 
8 
9 
10 #include "Global.h"
11 
12 //#include <Math.h>
13 #ifndef M_PI
14 #define M_PI 3.141592654
15 #endif
16 
17 
18 #define JUMPS 12
19 #define STEP_DEFAULT 4
20 
21 
22 int counta=0;
cPlayer(int LEFT,int RIGHT,int UP,int DOWN,int B,int A,int _speed_jump_height)23 cPlayer::cPlayer(int LEFT,int RIGHT,int UP,int DOWN,int B,int A,int _speed_jump_height)
24 {
25 
26 	x = 200;
27 	y = 3000;
28 
29 	incincible = 0;
30 	Surface_R[0][0] = LoadIMG(DATADIR "gfx/characters/small/player1r.png");
31 	Surface_R[0][1] = LoadIMG(DATADIR "gfx/characters/small/player2r.png");
32 	Surface_R[0][2] = LoadIMG(DATADIR "gfx/characters/small/player3r.png");
33 	Surface_L[0][0] = LoadIMG(DATADIR "gfx/characters/small/player1l.png");
34 	Surface_L[0][1] = LoadIMG(DATADIR "gfx/characters/small/player2l.png");
35 	Surface_L[0][2] = LoadIMG(DATADIR "gfx/characters/small/player3l.png");
36 	Surface_R[0][3] = LoadIMG(DATADIR "gfx/characters/small/downr.png");
37 	Surface_L[0][3] = LoadIMG(DATADIR "gfx/characters/small/downl.png");
38 
39 	Surface_R[1][0] = LoadIMG(DATADIR "gfx/characters/big/player1r.png");
40 	Surface_R[1][1] = LoadIMG(DATADIR "gfx/characters/big/player2r.png");
41 	Surface_R[1][2] = LoadIMG(DATADIR "gfx/characters/big/player3r.png");
42 	Surface_L[1][0] = LoadIMG(DATADIR "gfx/characters/big/player1l.png");
43 	Surface_L[1][1] = LoadIMG(DATADIR "gfx/characters/big/player2l.png");
44 	Surface_L[1][2] = LoadIMG(DATADIR "gfx/characters/big/player3l.png");
45 	Surface_R[1][3] = LoadIMG(DATADIR "gfx/characters/big/downr.png");
46 	Surface_L[1][3] = LoadIMG(DATADIR "gfx/characters/big/downl.png");
47 
48 	Surface_R[2][0] = LoadIMG(DATADIR "gfx/characters/fire/player1r.png");
49 	Surface_R[2][1] = LoadIMG(DATADIR "gfx/characters/fire/player2r.png");
50 	Surface_R[2][2] = LoadIMG(DATADIR "gfx/characters/fire/player3r.png");
51 	Surface_L[2][0] = LoadIMG(DATADIR "gfx/characters/fire/player1l.png");
52 	Surface_L[2][1] = LoadIMG(DATADIR "gfx/characters/fire/player2l.png");
53 	Surface_L[2][2] = LoadIMG(DATADIR "gfx/characters/fire/player3l.png");
54 	Surface_R[2][3] = LoadIMG(DATADIR "gfx/characters/fire/downr.png");
55 	Surface_L[2][3] = LoadIMG(DATADIR "gfx/characters/fire/downl.png");
56 
57 	K_LEFT = LEFT;
58 	K_RIGHT = RIGHT;
59 	K_UP = UP;
60 	K_DOWN = DOWN;
61 
62 	speed_jump_height = _speed_jump_height;
63 
64 	K_B = B;
65 	K_A = A;
66 
67 	STARPOWER_OVER = 1;
68 
69 	walkcount = 0;
70 	walkcount2 = 0;
71 	walkadd = 1;
72 
73 	y_speed = 0;
74 	x_speed = 0;
75 	JUMPcount = 400;
76 
77 	dead = 0;
78 
79 	STARPOWER = 0;
80 	stat = 0;
81 	W = 38;
82 	H = 42;
83 	Aed=0;
84 	Bed=0;
85 	invincible=200;
86 }
87 #define CAMRANGE 300
88 #define CAMRANGE_H 200
movecam()89 void cPlayer::movecam()
90 {
91 	if(cam_x>0&&x<cam_x+CAMRANGE)
92 		cam_x = x-CAMRANGE;
93 	else if(x>cam_x+WIDTH-CAMRANGE)
94 		cam_x = x-WIDTH+CAMRANGE;
95 
96 	if(y<cam_y+CAMRANGE_H)
97 		cam_y = y-CAMRANGE_H;
98 	else if(y>cam_y+HEIGHT-CAMRANGE_H)
99 		cam_y = y-HEIGHT+CAMRANGE_H;
100 
101 
102 	if(cam_x<40)
103 		cam_x = 40;
104 	else
105 		if(cam_x>16000 - WIDTH)
106 			cam_x = 16000 - WIDTH;
107 	if(cam_y<40)
108 		cam_y = 40;
109 	else
110 		if(cam_y > 4000 - HEIGHT)
111 			cam_y = 4000 - HEIGHT;
112 
113 }
114 
update()115 void cPlayer::update()
116 {
117 	if(invincible)
118 		invincible--;
119 
120 	if(x<100)
121 		x = 100;
122 
123 	if(x>15500)
124 		x = 15500;
125 	if(y > 4000)
126 		this->die();
127 
128 	movecam();
129 
130 	if(!touch_now)
131 		down_touch  = LEVEL->posValid(x+3,y+3+H) + LEVEL->posValid(x+W-3,y+3+H);
132 	else
133 		down_touch = 1;
134 	top_touch   = LEVEL->posValid(x+3,y) + LEVEL->posValid(x+W-3,y);
135 	left_touch  = LEVEL->posValid(x+x_speed-1,y+H/1.25) + LEVEL->posValid(x+x_speed-1,y+H/4);
136 	right_touch = LEVEL->posValid(x+W+x_speed+1,y+H/1.25) + LEVEL->posValid(x+W+x_speed+1,y+H/4);
137 	touch_now = 0;
138 
139 	if(LEVEL->posValid(x,y+3+H)==33||(LEVEL->posValid(x+W,y+3+H)==33))
140 		if(!right_touch)x+=3;
141 	if(LEVEL->posValid(x+20,y+3+H)==32||(LEVEL->posValid(x+W,y+3+H)==32))
142 		if(!left_touch)x-=3;
143 
144 
145 	if(bonus_activ<0)
146 		LEVEL->BonusCheck(x+5,y,x+W-5,y);
147 	else bonus_activ--;
148 
149 	if(y_speed>=0)
150 	{
151 		if(!down_touch)
152 		{
153 			y += y_speed;
154 			if(y_speed<speed_jump_height)
155 			y_speed+=0.5;
156 
157 		}
158 		else
159 		{
160 			y_speed = 0;
161 			JUMPcount = 400;
162 			if(stat==0)
163 				y = (((int)((y-20)/40))*40)+80-H;
164 
165 		}
166 	}
167 	else
168 	{
169 		if(!top_touch)
170 		{
171 			y += y_speed;
172 			y_speed+=0.5;
173 		}
174 		else y_speed = 0;
175 	}
176 
177 	///SHOOOOOOOOOT N RUUUUUUUUUUUUUUUUN
178 	if(keys[K_B]||Gamepad->Button_B)
179 	{
180 		max_speed = 8;
181 		if(!Bed&&stat==2)///////NEW FIREBALL!!!!!!!!!!!!!
182 		{
183 			#define FIRESPEED 3
184 			if(dir)
185 			{
186 				if(!FIREBALL[0]->init(x,y+35,FIRESPEED))
187 					FIREBALL[1]->init(x,y+35,FIRESPEED);
188 			}
189 				else
190 					if(!FIREBALL[0]->init(x,y+35,-FIRESPEED))
191 						FIREBALL[1]->init(x,y+35,-FIRESPEED);
192 		}
193 		Bed=1;
194 	}
195 	else
196 	{max_speed = 4;Bed=0;}
197 
198 	///JUUUUUUUUMP
199 	if(keys[K_A]||Gamepad->Button_A)
200 	{
201 		if(Aed==0&&JUMPcount==400&&down_touch)
202 		{
203 			jumpheight = JUMPS+abs(x_speed*1.4);
204 			PLAYSOUND0(S_JUMP);
205 			JUMPcount = 0;
206 		}
207 
208 		if(JUMPcount<jumpheight)
209 		{
210 			if(top_touch)
211 			{
212 				JUMPcount = 410;
213 				Aed=0;
214 			}
215 			y_speed=-8;
216 			JUMPcount++;
217 		}
218 		Aed=1;
219 	}else
220 	{
221 		if(Aed)
222 		{
223 			JUMPcount = 400;
224 			Aed=0;
225 		}
226 	}
227 
228 	if(keys[K_DOWN]||Gamepad->down)
229 	{
230 		if(!KeyDown_down)
231 		{
232 			y+=H-34;
233 			H = 34;
234 			walkcount = 3;
235 		}
236 
237 		KeyDown_down = 1;
238 	}
239 	else
240 	{
241 		if(KeyDown_down)
242 		{
243 			walkcount = 0;
244 			H = (stat==0) ? 42 : 75;
245 			y-=H-34;
246 			KeyDown_down = 0;
247 		}
248 	}
249 	if((keys[K_LEFT]||Gamepad->left)&&!KeyDown_down)
250 	{
251 		x_speed -= 0.2;
252 		dir = 0;
253 		MakeMyWalk();
254 	}
255 	else if((keys[K_RIGHT]||Gamepad->right)&&!KeyDown_down)
256 	{
257 		x_speed += 0.2;
258 		dir = 1;
259 		MakeMyWalk();
260 	}
261 	else
262 	{
263 		max_speed = 4;
264 		if(x_speed<-0.04)
265 		{
266 			x_speed+=0.05;
267 			if(!keys[K_DOWN]&&!Gamepad->down)MakeMyWalk();
268 		}
269 		else
270 			if(x_speed>0.04)
271 			{
272 				x_speed-=0.05;
273 				if(!keys[K_DOWN]&&!Gamepad->down)MakeMyWalk();
274 			}
275 			else if(walkcount!=3)walkcount=0;
276 	}
277 
278 
279 	if(x_speed>=max_speed)
280 		x_speed = max_speed;
281 	else if(x_speed<=-max_speed)
282 		x_speed = -max_speed;
283 
284 
285 	if(x_speed<0)
286 			if(!left_touch)
287 				x += x_speed;
288 			else x_speed = 0;
289 		else
290 			if(!right_touch)
291 				x += x_speed;
292 			else x_speed = 0;
293 
294 
295 	rect.x = (int)x - cam_x;
296 	rect.y = (int)y - cam_y;
297 
298 	if(STARPOWER)
299 	{
300 		if(STARPOWER%6 == 0)
301 			GLIDDER->init(x/40,y/40 +1,0,-10);
302 
303 		STARPOWER--;
304 		invincible = STARPOWER;
305 
306 
307 	for(int i=0;i<GOOMBAcount;i++)
308 		if(!GOOMBA[i]->DEATHcount&&!GOOMBA[i]->dying && collision(&rect,&GOOMBA[i]->rect))
309 		{
310 			GOOMBA[i]->die();
311 		}
312 
313 	for(int i=0;i<TURTLEFLYcount;i++)
314 		if(!TURTLEFLY[i]->dead&&!TURTLEFLY[i]->dying && collision(&rect,&TURTLEFLY[i]->rect))
315 		{
316 			TURTLEFLY[i]->die();
317 		}
318 	for(int i=0;i<TURTLEcount;i++)
319 		if(!TURTLE[i]->dead&&!TURTLE[i]->dying && collision(&rect,&TURTLE[i]->rect))
320 		{
321 			TURTLE[i]->die();
322 		}
323 	for(int i=0;i<SPINYcount;i++)
324 		if(!SPINY[i]->dead&&!SPINY[i]->dying && collision(&rect,&SPINY[i]->rect))
325 		{
326 			SPINY[i]->die();
327 		}
328 	for(int i=0;i<KILLERBLUMEcount;i++)
329 		if(!KILLERBLUME[i]->dead&& collision(&rect,&KILLERBLUME[i]->rect))
330 		{
331 			KILLERBLUME[i]->dead=1;
332 			SCORE->init(KILLERBLUME[i]->x,KILLERBLUME[i]->y,250);
333 			BLOOD_GENERATOR->newBlood(KILLERBLUME[i]->x,KILLERBLUME[i]->y);
334 			PLAYSOUND2(S_KICK);
335 		}
336 
337 		STARPOWER_OVER = 0;
338 	}else{
339 		if(!STARPOWER_OVER)
340 		{
341 			if(nosound==2)
342 				playMusic("megamusic.mid");
343 			STARPOWER_OVER=1;
344 		}
345 	}
346 
347 	for(int a=0;a<5;a++)
348 	{
349 			tmp_rect.x=LEVEL->EXITPOINTS[a].x - cam_x + 5;
350 			tmp_rect.y=LEVEL->EXITPOINTS[a].y - cam_y;
351 			tmp_rect.w = 10;
352 			tmp_rect.h = 20;
353 			if(LEVEL->EXITPOINTS[a].type == 1 )
354 				if(!keys[K_DOWN]&&!Gamepad->down)
355 					continue;
356 
357 			if(collision(&rect,&tmp_rect))
358 			{
359 				Mix_FadeOutMusic(1000);
360 				PLAYSOUND2(S_PIPE);
361 				//rect.x = H =tmp_rect.x-10;
362 				for(int n=0;n<30;n++)
363 				{
364 					SDL_FillRect(screen,0,LEVEL->BGCOLOR);
365 					LEVEL->drawBackground();
366 					if(LEVEL->EXITPOINTS[a].type == 1)//DOWN
367 						PLAYER->y+=2;
368 					else if(LEVEL->EXITPOINTS[a].type==2)//RIGHT
369 						PLAYER->x+=2;
370 					else if(LEVEL->EXITPOINTS[a].type==3)//LEFT
371 						PLAYER->x-=2;
372 					else if(LEVEL->EXITPOINTS[a].type==4)//UP
373 						PLAYER->y-=2;
374 
375 						tmp_rect.x = PLAYER->x-cam_x;
376 						tmp_rect.y = PLAYER->y-cam_y;
377 						SDL_UpperBlit(PLAYER->Surface_R[PLAYER->stat][0],0,screen,&tmp_rect);
378 						LEVEL->drawLevel();
379 						drawGLscreen();
380 						SDL_Delay(10);
381 				}
382 				LEVEL->loadLevel(LEVEL->EXITPOINTS[a].level,LEVEL->EXITPOINTS[a].startpoint);
383 			}
384 
385 	}
386 }
387 
MakeMyWalk()388 void cPlayer::MakeMyWalk()
389 {
390 		walkcount2++;
391 		if(walkcount2 >= 10 - (int)(abs((int)x_speed)))
392 		{
393 			if(walkcount==2)
394 				walkadd = -1;
395 			else
396 				if(walkcount ==  0)
397 					walkadd = 1;
398 
399 			walkcount+=walkadd;
400 			walkcount2=0;
401 		}
402 }
403 #define DELAYTIME 100
changeStat(int staty)404 void cPlayer::changeStat(int staty)
405 {
406 	if(STARPOWER)
407 		return;
408 
409 	y-=40;
410 	if(stat<=staty)
411 	{
412 		PLAYSOUND1(S_GROW);
413 		invincible = 0;
414 	}
415 	else
416 	{
417 		invincible = 400;
418 		PLAYSOUND1(S_SHRINK);
419 	}
420 
421 	stat=staty;
422 	if(stat==0)
423 	{
424 		W = 38;
425 		H = 42;
426 	}
427 	else
428 	{
429 		W = 38;
430 		H = 75;
431 		GLIDDER->init(this->x/40,this->y/40,5,3);
432 		GLIDDER->init(this->x/40,this->y/40,0,0);
433 		GLIDDER->init(this->x/40,this->y/40+1,0,0);
434 		GLIDDER->init(this->x/40,this->y/40+1,0,0);
435 		GLIDDER->init(this->x/40,this->y/40+1,0,0);
436 	}
437 
438 }
439 
440 
draw()441 void cPlayer::draw()
442 {
443 
444 	if(STARPOWER)
445 	{
446 		SDL_LockSurface(screen);
447 		for(int i=100;i<100+STARPOWER/2;i++)
448 		{
449 			for(int x=100;x<110;x++)
450 				FastPixelColorNolock(screen,i,x,8394261);
451 		}
452 		SDL_UnlockSurface(screen);
453 	}
454 
455 	if(invincible%3)
456 	{
457 //		if(!(invincible%5))
458 //			BLOOD_GENERATOR->newBlood(x,y);
459 
460 		return;
461 	}
462 
463 	if(down_touch)
464 	{
465 		if(dir)
466 		{
467 			SDL_UpperBlit(Surface_R[stat][walkcount],0,screen,&rect);
468 		}
469 		else
470 		{
471 			SDL_UpperBlit(Surface_L[stat][walkcount],0,screen,&rect);
472 		}
473 	}
474 	else
475 		if(dir)
476 		{
477 			SDL_UpperBlit(Surface_R[stat][2],0,screen,&rect);
478 		}
479 		else
480 		{
481 			SDL_UpperBlit(Surface_L[stat][2],0,screen,&rect);
482 		}
483 
484 
485 }
486 
die()487 void cPlayer::die()
488 {
489 	if(y < 4000)
490 	{
491 	if(invincible)
492 		return;
493 
494 	invincible = 30;
495 
496 	if(incincible)
497 		return;
498 	}
499 	BLOOD_GENERATOR->reset();
500 
501 	PLAYSOUND1(S_DIE);
502 
503 	LEVEL->drawBackground();
504 	LEVEL->drawLevel();
505 
506 	int speed = 8;
507 	if(show_blood)
508 		for(int i=0;i<200;i++)
509 		{
510 			BLOOD_GENERATOR->oneNewBlood(x,y);
511 			BLOOD_GENERATOR->oneNewBlood(x,y);
512 			BLOOD_GENERATOR->oneNewBlood(x,y);
513 			BLOOD_GENERATOR->oneNewBlood(x,y);
514 			BLOOD_GENERATOR->update();
515 
516 			drawGLscreen();
517 			framerate();
518 		}
519 	else speed = 4;
520 
521 	Mix_FadeOutMusic(600);
522 	for(int  i=0;i<HEIGHT/2;i+=speed)
523 	{
524 		SDL_LockSurface(screen);
525 		for(int n=0;n<WIDTH;n++)
526 		{
527 			for(int x=i;x<i+speed;x+=rand()%6)
528 				FastPixelColorNolock(screen,n,x,8394261);
529 		}
530 		for( int n=0;n<WIDTH;n++)
531 		{
532 			for(int x=HEIGHT-i;x<HEIGHT-i+speed;x+=rand()%6)
533 				FastPixelColorNolock(screen,n,x,8394261);
534 		}
535 		SDL_UnlockSurface(screen);
536 
537 		drawGLscreen();
538 		SDL_LockSurface(screen);
539 		for(int  n=0;n<WIDTH;n++)
540 		{
541 			for(int x=i;x<i+speed;x+=rand()%6)
542 				FastPixelColorNolock(screen,n,x,8394261);
543 		}
544 		for( int n=0;n<WIDTH;n++)
545 		{
546 			for(int x=HEIGHT-i;x<HEIGHT-i+speed;x+=rand()%6)
547 				FastPixelColorNolock(screen,n,x,8394261);
548 		}
549 		SDL_UnlockSurface(screen);
550 		drawGLscreen();
551 
552 		framerate();
553 	}
554 
555 	dead = 1;
556 
557 }
558 
cFireball()559 cFireball::cFireball()
560 {
561 	FIREBALLS[0] = LoadIMG(DATADIR "gfx/fire1.png");
562 	FIREBALLS[1] = LoadIMG(DATADIR "gfx/fire2.png");
563 	FIREBALLS[2] = LoadIMG(DATADIR "gfx/fire3.png");
564 	FIREBALLS[3] = LoadIMG(DATADIR "gfx/fire4.png");
565 	online = 0;
566 }
567 
init(int _x,int _y,int dir)568 bool cFireball::init(int _x, int _y,int dir)
569 {
570 	if(online)
571 		return 0;
572 
573 	PLAYSOUND1(S_FIREBALL);
574 	x = _x;
575 	y = _y;
576 	online = 1;
577 	x_speed = dir*3;
578 	y_speed = 3;
579 	ani = 0;
580 	return 1;
581 }
update()582 void cFireball::update()
583 {
584 	if(!online)
585 		return;
586 
587 	ani+=0.2;
588 	if(ani>=4)
589 		ani = 0;
590 
591 	center_touch   = LEVEL->posValid(x,y+2) + LEVEL->posValid(x+20,y+2);
592 	down_touch  = LEVEL->posValid(x+10,y+20);
593 
594 	x += x_speed;
595 	y += y_speed;
596 
597 	if(down_touch)
598 		y_speed=-5;
599 
600 	y_speed+=0.5;
601 
602 	rect.x = x - cam_x;
603 	rect.y = y - cam_y;
604 
605 	if(center_touch||rect.x<-30||rect.x>WIDTH+50)
606 		online = 0;
607 
608 	SDL_UpperBlit(FIREBALLS[(int)ani],0,screen,&rect);
609 	if(BOWSER!=0)
610 		if(collision(&rect,&BOWSER->rect))
611 		{
612 			BOWSER->hit();
613 			online=0;
614 		}
615 
616 	for(int i=0;i<GOOMBAcount;i++)
617 		if(!GOOMBA[i]->DEATHcount&&!GOOMBA[i]->dying && collision(&rect,&GOOMBA[i]->rect))
618 		{
619 			GOOMBA[i]->die();
620 			online=0;
621 		}
622 
623 	for(int i=0;i<TURTLEFLYcount;i++)
624 		if(!TURTLEFLY[i]->dead&&!TURTLEFLY[i]->dying && collision(&rect,&TURTLEFLY[i]->rect))
625 		{
626 			TURTLEFLY[i]->die();
627 			online=0;
628 		}
629 	for(int i=0;i<TURTLEcount;i++)
630 		if(!TURTLE[i]->dead&&!TURTLE[i]->dying && collision(&rect,&TURTLE[i]->rect))
631 		{
632 			TURTLE[i]->die();
633 			online=0;
634 		}
635 	for(int i=0;i<SPINYcount;i++)
636 		if(!SPINY[i]->dead&&!SPINY[i]->dying && collision(&rect,&SPINY[i]->rect))
637 		{
638 			SPINY[i]->die();
639 			online=0;
640 		}
641 	for(int i=0;i<KILLERBLUMEcount;i++)
642 		if(!KILLERBLUME[i]->dead&& collision(&rect,&KILLERBLUME[i]->rect))
643 		{
644 			KILLERBLUME[i]->dead=1;
645 			SCORE->init(KILLERBLUME[i]->x,KILLERBLUME[i]->y,250);
646 			BLOOD_GENERATOR->newBlood(KILLERBLUME[i]->x,KILLERBLUME[i]->y);
647 			online=0;
648 			PLAYSOUND2(S_KICK);
649 		}
650 }
651