1 /*
2  * DESCRIPCION DEL JUEGO
3  * Copyright (C) 2007  Javier P�rez Pacheco
4  *
5  * Este juego tienen licencia Creative Commons y se permite
6  * su modificacion y utilizacion libremente siempre y cuando se
7  * cite al autor original y se comparta con la misma licencia.
8  * No se permite su uso comercial.
9  *
10  * Para mas informacion visite la web:
11  * http://creativecommons.org/licenses/by-nc-sa/2.0/es/
12  *
13  * PROGRAMADOR
14  * Javier P�rez Pacheco
15  * Cadiz (Spain)
16  * javielinux@gmail.com
17  *
18  * GRAFISMO Y 3D
19  * Jesus Carrasco
20  * Cadiz (Spain)
21  * carrasco.carrasco@gmail.com
22  *
23  * MUSICA Y GRAFISMO
24  * Shano Lores
25  * Cadiz (Spain)
26  * shanakla@gmail.com
27  *
28  */
29 
30 #include "class_game.h"
31 
32 /*****************************
33 **
34 ** CLASE BoardInfo
35 **
36 ******************************/
37 
38 BoardInfo* BoardInfo::instance = NULL;
39 
BoardInfo(string file)40 BoardInfo::BoardInfo(string file) : Hash (file, false) {
41 }
42 
GetInstance()43 BoardInfo* BoardInfo::GetInstance () {
44 	if ( instance == NULL ) {
45 		instance = new BoardInfo("/board/boardinfo.xml");
46 	}
47 	return instance;
48 }
49 
50 /*****************************
51 **
52 ** CLASE TypeObjectScenary
53 **
54 ******************************/
55 
56 void TypeObjectsScenary_parseXML(char fileXML[128]);
57 
58 TypeObjectsScenary* TypeObjectsScenary::instance = NULL;
59 
TypeObjectsScenary()60 TypeObjectsScenary::TypeObjectsScenary() {
61 	cursor = 0;
62 }
63 
GetInstance()64 TypeObjectsScenary* TypeObjectsScenary::GetInstance () {
65 	if ( instance == NULL ) {
66 		instance = new TypeObjectsScenary();
67 	}
68 	return instance;
69 }
70 
getObject(string name)71 TypeObjectScenary* TypeObjectsScenary::getObject(string name) {
72 	for (int i=0; i<(int)objects.size(); i++) {
73 		if (objects[i].name == name) return &objects[i];
74 	}
75 	return NULL;
76 }
77 
load()78 void TypeObjectsScenary::load() {
79 	clear();
80 	char tmp[128];
81 	sprintf(tmp, "%s/scenary/objects.xml", DATA_DIR);
82 	TypeObjectsScenary_parseXML(tmp);
83 }
84 
TypeObjectsScenary_start(void * userData,const char * el,const char ** attr)85 static void TypeObjectsScenary_start(void *userData, const char *el, const char **attr) {
86 	int i;
87 	if (strcmp(el, "object") == 0) {
88 		TypeObjectScenary obj;
89 		obj.minScale = 0.3;
90 		obj.maxScale = 1.2;
91 		for (i = 0; attr[i]; i += 2) {
92 			if (strcmp(attr[i], "name") == 0) {
93 				obj.name = attr[i+1];
94 			}
95 			if (strcmp(attr[i], "file") == 0) {
96 				obj.file = attr[i+1];
97 			}
98 			if (strcmp(attr[i], "min-scale") == 0) {
99 				obj.minScale = atof(attr[i+1]);
100 			}
101 			if (strcmp(attr[i], "max-scale") == 0) {
102 				obj.maxScale = atof(attr[i+1]);
103 			}
104 			if (strcmp(attr[i], "type") == 0) {
105 				if (strcmp(attr[i+1], "opened") == 0) {
106 					obj.type = SCENARY_OPENED;
107 				} else {
108 					obj.type = SCENARY_CLOSED;
109 				}
110 			}
111 			if (strcmp(attr[i], "category") == 0) {
112 				obj.category = attr[i+1];
113 			}
114 		}
115 		TypeObjectsScenary::GetInstance()->addObject(obj);
116 	}
117 }
118 
TypeObjectsScenary_end(void * userData,const char * el)119 static void TypeObjectsScenary_end(void *userData, const char *el)
120 {
121 
122 }
123 
TypeObjectsScenary_parseXML(char fileXML[128])124 void TypeObjectsScenary_parseXML(char fileXML[128]) {
125 	char buffer[8192];
126 	int done;
127 
128 	XML_Parser p = XML_ParserCreate("ISO-8859-1");
129 	if (! p) {
130 		printf("It could not have sufficient memory parser\n");
131 	}
132 	string data = "";
133 	XML_SetUserData(p, &data);
134 	XML_SetElementHandler(p, TypeObjectsScenary_start, TypeObjectsScenary_end);
135 
136 	FILE *file = fopen(fileXML, "r");
137 	if(!file)
138 		printf("Error opening file XML\n");
139 
140 	do
141 	{
142 		size_t len = fread(buffer, 1, sizeof(buffer), file);
143 		done = len < sizeof(buffer);
144 		if(!XML_Parse(p, buffer, len, done)){
145 			printf("Error making the parse\n");
146 		}
147 			//parse_error(&data, XML_ErrorString(XML_GetErrorCode(data.parser)));
148 	}
149 	while(!done);
150 	fclose(file);
151 }
152 
153 /*****************************
154 **
155 ** CLASE BoardScale
156 **
157 ******************************/
158 
159 BoardScale* BoardScale::instance = NULL;
160 
BoardScale()161 BoardScale::BoardScale() {
162     w_tile = W_TILE;
163     h_tile = H_TILE;
164     scale = 1.0f;
165 }
166 
GetInstance()167 BoardScale* BoardScale::GetInstance () {
168 	if ( instance == NULL ) {
169 		instance = new BoardScale();
170 	}
171 	return instance;
172 }
173 
setScale(float s)174 void BoardScale::setScale (float s) {
175     w_tile = (int)W_TILE*s;
176     h_tile = (int)H_TILE*s;
177     scale = s;
178 }
179 
180 /*****************************
181 **
182 ** CLASE MouseMap
183 **
184 ******************************/
185 
186 MouseMapTile* MouseMapTile::instance = NULL;
187 
MouseMapTile()188 MouseMapTile::MouseMapTile() {
189 	//createMap ();
190 }
191 
GetInstance()192 MouseMapTile* MouseMapTile::GetInstance () {
193 	if ( instance == NULL ) {
194 		instance = new MouseMapTile();
195 	}
196 	return instance;
197 }
198 
createMap()199 void MouseMapTile::createMap () {
200 	int x, y;
201 	int rest=0;
202 	map.clear();
203 
204 	int w = BoardScale::GetInstance()->getWTile();
205 	int h = BoardScale::GetInstance()->getHTile();
206 
207 	int chain = (w/2)/(h/2);
208 	int aux=0;
209 
210 	for (y=0; y<h; y++) {
211 		// no se pq pero tengo que multiplicar cuando es 6x6
212 		if (y<(h/2)) {
213 			if (w==W_TILE) rest += chain*2;
214 			else rest += chain;
215 		} else {
216 			if (w==W_TILE) rest -= chain*2;
217 			else rest -= chain;
218 		}
219 
220 		for (x=0; x<w; x++) {
221 			if ( (x<(w/2)) && (y<(h/2)) ) { // 1
222 				if (x < (w/2)-rest) {
223 					aux = 1;
224 				} else {
225 					aux = 0;
226 				}
227 			}
228 			if ( (x>=(w/2)) && (y<(h/2)) ) { // 2
229 				if (x > (w/2)+rest) {
230 					aux = 2;
231 				} else {
232 					aux = 0;
233 				}
234 			}
235 			if ( (x<(w/2)) && (y>=(h/2)) ) { // 3
236 				if (x < (w/2)-rest) {
237 					aux = 3;
238 				} else {
239 					aux = 0;
240 				}
241 			}
242 			if ( (x>=(w/2)) && (y>=(h/2)) ) { // 4
243 				if (x > (w/2)+rest) {
244 					aux = 4;
245 				} else {
246 					aux = 0;
247 				}
248 			}
249 			map.push_back(aux);
250 		}
251 	}
252 
253 }
254 
getPosMouse(int x,int y)255 int MouseMapTile::getPosMouse (int x, int y) {
256 	int w = BoardScale::GetInstance()->getWTile();
257 	int h = BoardScale::GetInstance()->getHTile();
258     if ( ( (y*w)+x>=0 ) && ( (y*h)+x<map.size() ) ) {
259          return map[(y*w)+x];
260     } else {
261            return 0;
262     }
263 }
264 
265 /*****************************
266 **
267 ** CLASE Plot
268 **
269 ******************************/
270 
271 Plot* Plot::instance = NULL;
272 
Plot()273 Plot::Plot() {
274 }
275 
GetInstance()276 Plot* Plot::GetInstance () {
277 	if ( instance == NULL ) {
278 		instance = new Plot();
279 	}
280 	return instance;
281 }
282 
283 /*****************************
284 **
285 ** CLASE ObjectScenary
286 **
287 ******************************/
288 
289 
ObjectScenary()290 ObjectScenary::ObjectScenary() : ElementDnD () {
291 	showIcons = false;
292 	lock = false;
293 	flipObject = false;
294 	typeScenary = SCENARY_CLOSED;
295 	origen = ORIGENOBJECT_GAME;
296 	typeObject = TYPEOBJECT_3D;
297 
298 	fontText = "";
299 	text = "-";
300 	alignText = ALIGN_LEFT;
301 	colorText.r = 255;
302 	colorText.g = 255;
303 	colorText.b = 255;
304 
305 	minScale = 0.3;
306     maxScale = 1.2;
307 
308 	iconZoomin = new Button();
309 	iconZoomin->setScales(1.0, 1.1);
310 	iconZoomin->setVAlign(VALIGN_BOTTOM);
311 	iconZoomin->setX(0);
312 	iconZoomin->setY(0);
313 
314 	iconZoomin->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_zoomin.png"));
315 	iconZoomin->setAnimation("out");
316 
317 	iconZoomout = new Button();
318 	iconZoomout->setScales(1.0, 1.1);
319 	iconZoomout->setVAlign(VALIGN_BOTTOM);
320 	iconZoomout->setX(0);
321 	iconZoomout->setY(0);
322 
323 	iconZoomout->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_zoomout.png"));
324 	iconZoomout->setAnimation("out");
325 
326 	iconDuplicate = new Button();
327 	iconDuplicate->setScales(1.0, 1.1);
328 	iconDuplicate->setVAlign(VALIGN_BOTTOM);
329 	iconDuplicate->setX(0);
330 	iconDuplicate->setY(0);
331 
332 	iconDuplicate->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_duplicate.png"));
333 	iconDuplicate->setAnimation("out");
334 
335 	iconDelete = new Button();
336 	iconDelete->setScales(1.0, 1.1);
337 	iconDelete->setVAlign(VALIGN_BOTTOM);
338 	iconDelete->setX(0);
339 	iconDelete->setY(0);
340 
341 	iconDelete->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_delete.png"));
342 	iconDelete->setAnimation("out");
343 
344 	iconLock = new Button();
345 	iconLock->setScales(1.0, 1.1);
346 	iconLock->setVAlign(VALIGN_BOTTOM);
347 	iconLock->setX(0);
348 	iconLock->setY(0);
349 
350 	iconLock->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_unlock.png"));
351 	iconLock->setAnimation("out");
352 
353 	iconFlip = new Button();
354 	iconFlip->setScales(1.0, 1.1);
355 	iconFlip->setVAlign(VALIGN_BOTTOM);
356 	iconFlip->setX(0);
357 	iconFlip->setY(0);
358 
359 	iconFlip->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_flip.png"));
360 	iconFlip->setAnimation("out");
361 
362 	iconOrderBottom = new Button();
363 	iconOrderBottom->setScales(1.0, 1.1);
364 	iconOrderBottom->setVAlign(VALIGN_BOTTOM);
365 	iconOrderBottom->setX(0);
366 	iconOrderBottom->setY(0);
367 
368 	iconOrderBottom->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_orderbottom.png"));
369 	iconOrderBottom->setAnimation("out");
370 
371 	iconOrderTop = new Button();
372 	iconOrderTop->setScales(1.0, 1.1);
373 	iconOrderTop->setVAlign(VALIGN_BOTTOM);
374 	iconOrderTop->setX(0);
375 	iconOrderTop->setY(0);
376 
377 	iconOrderTop->imageOut(WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_ordertop.png"));
378 	iconOrderTop->setAnimation("out");
379 
380 
381 }
382 
~ObjectScenary()383 ObjectScenary::~ObjectScenary () {
384 	parameters.clear();
385 
386 	delete iconZoomin;
387 	delete iconZoomout;
388 	delete iconDuplicate;
389 	delete iconDelete;
390 	delete iconOrderTop;
391 	delete iconOrderBottom;
392 	delete iconFlip;
393 	delete iconLock;
394 }
395 
setFlip(bool f)396 void ObjectScenary::setFlip (bool f) {
397 	flipObject = f;
398 	if (typeObject == TYPEOBJECT_3D) {
399 		flip = f;
400 	} else {
401 		float d = (tan(25.5*3.14159265/180)*getWidthOriginal())/2;
402 		if (f) {
403 			setPerspective(0, d, 0, -d, 0, d, 0, -d);
404 		} else {
405 			setPerspective(0, -d, 0, d, 0, -d, 0, d);
406 		}
407 	}
408 }
409 
swapLock()410 void ObjectScenary::swapLock () {
411      if (!lock) {
412          lock = true;
413          setToDoDnd (false);
414          iconZoomin->setSensitive(false);
415          iconZoomout->setSensitive(false);
416          iconDuplicate->setSensitive(false);
417          iconDelete->setSensitive(false);
418          iconOrderTop->setSensitive(false);
419          iconOrderBottom->setSensitive(false);
420          iconFlip->setSensitive(false);
421 
422          iconZoomin->setRGB(100, 100, 100);
423          iconZoomout->setRGB(100, 100, 100);
424          iconDuplicate->setRGB(100, 100, 100);
425          iconDelete->setRGB(100, 100, 100);
426          iconOrderTop->setRGB(100, 100, 100);
427          iconOrderBottom->setRGB(100, 100, 100);
428          iconFlip->setRGB(100, 100, 100);
429 
430 	     iconLock->changeFrameSurface(0, WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_lock.png"));
431      } else {
432          lock = false;
433          setToDoDnd (true);
434          iconZoomin->setSensitive(true);
435          iconZoomout->setSensitive(true);
436          iconDuplicate->setSensitive(true);
437          iconDelete->setSensitive(true);
438          iconOrderTop->setSensitive(true);
439          iconOrderBottom->setSensitive(true);
440          iconFlip->setSensitive(true);
441 
442          iconZoomin->setRGB(255, 255, 255);
443          iconZoomout->setRGB(255, 255, 255);
444          iconDuplicate->setRGB(255, 255, 255);
445          iconDelete->setRGB(255, 255, 255);
446          iconOrderTop->setRGB(255, 255, 255);
447          iconOrderBottom->setRGB(255, 255, 255);
448          iconFlip->setRGB(255, 255, 255);
449 
450          iconLock->changeFrameSurface(0, WorkingSurfaces::GetInstance()->surfaceFromImage((string)DATA_DIR + "/editgame/ico_unlock.png"));
451      }
452 }
453 
setX(float x)454 void ObjectScenary::setX (float x) {
455 	oldX = posX;
456 	posX = x;
457     if (getBounds().x1>0) {
458     	iconZoomin->setX(getBounds().x1);
459     	iconZoomout->setX(getBounds().x1+20);
460     	iconLock->setX(getBounds().x1+40);
461     	iconDelete->setX(getBounds().x1+60);
462 
463     	iconOrderTop->setX(getBounds().x1);
464     	iconOrderBottom->setX(getBounds().x1+20);
465     	iconDuplicate->setX(getBounds().x1+40);
466     	iconFlip->setX(getBounds().x1+60);
467     } else {
468     	iconZoomin->setX(0);
469     	iconZoomout->setX(20);
470     	iconLock->setX(40);
471     	iconDelete->setX(60);
472 
473     	iconOrderTop->setX(0);
474     	iconOrderBottom->setX(20);
475     	iconDuplicate->setX(40);
476     	iconFlip->setX(60);
477     }
478 }
479 
setY(float y)480 void ObjectScenary::setY (float y) {
481 	oldY = posY;
482 	posY = y;
483     if (getBounds().y1>36) {
484     	iconZoomin->setY(getBounds().y1-20);
485     	iconZoomout->setY(getBounds().y1-20);
486     	iconLock->setY(getBounds().y1-20);
487     	iconDelete->setY(getBounds().y1-20);
488 
489     	iconOrderTop->setY(getBounds().y1);
490     	iconOrderBottom->setY(getBounds().y1);
491     	iconDuplicate->setY(getBounds().y1);
492     	iconFlip->setY(getBounds().y1);
493     } else {
494     	iconZoomin->setY(16);
495     	iconZoomout->setY(16);
496     	iconLock->setY(16);
497     	iconDelete->setY(16);
498 
499     	iconOrderTop->setY(36);
500     	iconOrderBottom->setY(36);
501     	iconDuplicate->setY(36);
502     	iconFlip->setY(36);
503     }
504 }
505 
addX(float x)506 void ObjectScenary::addX (float x) {
507      setX(getXWithoutDisplacementByGroups()+x);
508 }
509 
addY(float y)510 void ObjectScenary::addY (float y) {
511     setY(getY()+y);
512 }
513 
setGroup(Group * g)514 void ObjectScenary::setGroup(Group *g) {
515 	group = g;
516 }
517 
onOver()518 void ObjectScenary::onOver()
519 {
520 	if (showIcons) {
521 		iconZoomin->onOver();
522 		iconZoomout->onOver();
523 		iconDuplicate->onOver();
524 		iconDelete->onOver();
525     	iconOrderTop->onOver();
526     	iconOrderBottom->onOver();
527     	if (typeScenary==SCENARY_OPENED) iconFlip->onOver();
528     	iconLock->onOver();
529 	}
530 	if (isOnMouseOver() && !isRollOver) {
531 		isRollOver = true;
532 	}
533 	if (!isOnMouseOver() && isRollOver) {
534 		isRollOver = false;
535 		if (getTypeObject()==TYPEOBJECT_TEXT) {
536 			setRGB(getColorText().r, getColorText().g, getColorText().b);
537 		} else {
538 			setRGB(255, 255, 255);
539 		}
540 	}
541 	if (toDoDnd && isDnd) {
542 		int cMouseX, cMouseY;
543 		SDL_GetMouseState(&cMouseX, &cMouseY);
544 		cMouseX -= (int)getDisplacementInXByGroups();
545 		cMouseY -= (int)getDisplacementInYByGroups();
546 		addX(cMouseX - mouseX);
547 		addY(cMouseY - mouseY);
548 		mouseX = cMouseX;
549 		mouseY = cMouseY;
550 		if (typeScenary==SCENARY_CLOSED) {
551     		if ( (getX()<getDisplacementInXByGroups()) && (!getFlip()) ) {
552     			setFlip(true);
553     			setAlign(ALIGN_RIGHT);
554     			iconZoomin->addX(-getWidth());
555     			iconZoomout->addX(-getWidth());
556     			iconDuplicate->addX(-getWidth());
557     			iconDelete->addX(-getWidth());
558     			iconOrderTop->addX(-getWidth());
559     			iconOrderBottom->addX(-getWidth());
560     			iconFlip->addX(-getWidth());
561     			iconLock->addX(-getWidth());
562     		}
563     		if ( (getX()>=getDisplacementInXByGroups()) && (getFlip()) ) {
564     			setFlip(false);
565     			setAlign(ALIGN_LEFT);
566     			iconZoomin->addX(getWidth());
567     			iconZoomout->addX(getWidth());
568     			iconDuplicate->addX(getWidth());
569     			iconDelete->addX(getWidth());
570     			iconOrderTop->addX(getWidth());
571     			iconOrderBottom->addX(getWidth());
572     			iconFlip->addX(getWidth());
573     			iconLock->addX(getWidth());
574     		}
575         }
576 	}
577 
578 }
579 
draw()580 void ObjectScenary::draw () {
581 	move();
582 	transforms();
583 	paintSprite();
584 }
585 
drawIcons()586 void ObjectScenary::drawIcons () {
587 	if (showIcons) {
588 		iconZoomin->draw();
589 		iconZoomout->draw();
590 		iconDuplicate->draw();
591 		iconDelete->draw();
592 		iconOrderTop->draw();
593 		iconOrderBottom->draw();
594 		if (typeScenary==SCENARY_OPENED) iconFlip->draw();
595 		iconLock->draw();
596 	}
597 }
598 
unLoad()599 void ObjectScenary::unLoad() {
600 	int i;
601 	for (i=0; i<(int)sprite.size(); i++)
602 		delete sprite[i];
603 	sprite.clear();
604 	parameters.clear();
605 
606 	delete iconZoomin;
607 	delete iconZoomout;
608 	delete iconDuplicate;
609 	delete iconDelete;
610 	delete iconOrderTop;
611 	delete iconOrderBottom;
612 	delete iconFlip;
613 	delete iconLock;
614 }
615 
616 /*****************************
617 **
618 ** CLASE Scenary
619 **
620 ******************************/
621 
622 
Scenary()623 Scenary::Scenary() : Group () {
624 	posX = World::width/2;
625 	posY = 0;
626 
627 	separatedTilesClosed = 25.0;
628 
629     colorRWallClosed = 255;
630     colorGWallClosed = 255;
631     colorBWallClosed = 255;
632 
633 	editable = false;
634 	showGuides = false;
635 	type = SCENARY_CLOSED;
636 	c_typeTiledBottom = 0;
637 	nVerticalTiles = 4;
638 
639 	shadowWall = new Element();
640 	shadowWall->addFrameFileFromData("/scenary/wall_shadow.png");
641 	shadowWall->setAlign(ALIGN_CENTER);
642 	addElement(shadowWall);
643 
644 	wallRight = new Element();
645 	wallRight->addFrameFileFromData("/scenary/wallscenary0.png");
646 	wallRight->setAlign(ALIGN_LEFT);
647 	addElement(wallRight);
648 
649 	wallLeft = new Element();
650 	wallLeft->addFrameFileFromData("/scenary/wallscenary0.png");
651 	wallLeft->setFlip(true);
652 	wallLeft->setAlign(ALIGN_RIGHT);
653 	addElement(wallLeft);
654 
655 	guideRight = new Element();
656 	guideRight->addFrameFileFromData("/scenary/guide_closed.png");
657 	guideRight->addFrameFileFromData("/scenary/guide_opened.png");
658 	guideRight->setAlign(ALIGN_LEFT);
659 	addElement(guideRight);
660 
661 	guideLeft = new Element();
662 	guideLeft->addFrameFileFromData("/scenary/guide_closed.png");
663 	guideLeft->addFrameFileFromData("/scenary/guide_opened.png");
664 	guideLeft->setFlip(true);
665 	guideLeft->setAlign(ALIGN_RIGHT);
666 	addElement(guideLeft);
667 
668 	tileRight = new Element();
669 	tileRight->addFrameFileFromData("/scenary/tile_0_down.png");
670 	tileRight->setVAlign(VALIGN_BOTTOM);
671 	tileRight->setGroup(Board::GetInstance());
672 
673 	tileRightUp = new Element();
674 	tileRightUp->addFrameFileFromData("/scenary/tile_0_up.png");
675 	tileRightUp->setVAlign(VALIGN_BOTTOM);
676 	tileRightUp->setGroup(Board::GetInstance());
677 
678 	tileLeft = new Element();
679 	tileLeft->addFrameFileFromData("/scenary/tile_0_down.png");
680 	tileLeft->setFlip(true);
681 	tileLeft->setAlign(ALIGN_RIGHT);
682 	tileLeft->setVAlign(VALIGN_BOTTOM);
683 	tileLeft->setGroup(Board::GetInstance());
684 
685 	tileLeftUp = new Element();
686 	tileLeftUp->addFrameFileFromData("/scenary/tile_0_up.png");
687 	tileLeftUp->setFlip(true);
688 	tileLeftUp->setAlign(ALIGN_RIGHT);
689 	tileLeftUp->setVAlign(VALIGN_BOTTOM);
690 	tileLeftUp->setGroup(Board::GetInstance());
691 
692 	terrainOpened = new Element();
693 	terrainOpened->setAlign(ALIGN_CENTER);
694 	terrainOpened->addFrameFileFromData("/scenary/terrain0.jpg");
695 	addElement(terrainOpened);
696 
697 	typeTiled = 0;
698 	typeTerrain = 0;
699 	typeWall = 0;
700 
701 }
702 
~Scenary()703 Scenary::~Scenary () {
704 	unLoad();
705 }
706 
setType(int t)707 void Scenary::setType (int t) {
708      type = t;
709      if (type == SCENARY_CLOSED) {
710         guideRight->setCurrentFrame(0);
711         guideLeft->setCurrentFrame(0);
712      } else {
713         guideRight->setCurrentFrame(1);
714         guideLeft->setCurrentFrame(1);
715      }
716 }
717 
clear()718 void Scenary::clear() {
719 	for (int i=0; i<(int)objects.size(); i++) {
720 		//delete objects[i];
721 		objects[i]->unLoad();
722 	}
723 	objects.clear();
724 }
725 
hide()726 void Scenary::hide() {
727     setAlpha(0);
728     tileRight->setAlpha(0);
729     tileRightUp->setAlpha(0);
730     tileLeft->setAlpha(0);
731     tileLeftUp->setAlpha(0);
732 }
733 
show()734 void Scenary::show() {
735     setFadeIn(1000);
736     tileRight->setFadeIn(4000);
737     tileRightUp->setFadeIn(4000);
738     tileLeft->setFadeIn(4000);
739     tileLeftUp->setFadeIn(4000);
740 }
741 
setTypeTiled(int t)742 void Scenary::setTypeTiled(int t) {
743     if ( Missions::GetInstance()->currentLevel->existFile("tile_down.png")
744         && Missions::GetInstance()->currentLevel->existFile("tile_up.png") ) {
745 
746     	tileRight->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/tile_down.png");
747     	tileRightUp->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/tile_up.png");
748     	tileLeft->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/tile_down.png");
749     	tileLeftUp->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/tile_up.png");
750 
751     	Board::GetInstance()->tiledOfLevel = true;
752 
753     } else {
754     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_TILED")) {
755     		typeTiled = t;
756     	} else {
757     		typeTiled = 0;
758     	}
759 
760     	char tmp[4];
761     	sprintf(tmp, "%d", typeTiled);
762 
763     	tileRight->changeFrameFileFromData(0, "/scenary/tile_"+string(tmp)+"_down.png");
764     	tileRightUp->changeFrameFileFromData(0, "/scenary/tile_"+string(tmp)+"_up.png");
765     	tileLeft->changeFrameFileFromData(0, "/scenary/tile_"+string(tmp)+"_down.png");
766     	tileLeftUp->changeFrameFileFromData(0, "/scenary/tile_"+string(tmp)+"_up.png");
767 
768     	Board::GetInstance()->tiledOfLevel = false;
769 
770    }
771 }
772 
setTypeTerrain(int t)773 void Scenary::setTypeTerrain(int t) {
774 
775     if ( Missions::GetInstance()->currentLevel->existFile("terrain.jpg") ) {
776 
777         terrainOpened->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/terrain.jpg");
778         Board::GetInstance()->terrainOfLevel = true;
779 
780     } else {
781     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_TERRAIN")) {
782     		typeTerrain = t;
783     	} else {
784     		typeTerrain = 0;
785     	}
786 
787     	char tmp[4];
788     	sprintf(tmp, "%d", typeTerrain);
789 
790     	terrainOpened->changeFrameFileFromData(0, "/scenary/terrain"+string(tmp)+".jpg");
791 
792     	Board::GetInstance()->terrainOfLevel = false;
793 
794     }
795 }
796 
setTypeWall(int t)797 void Scenary::setTypeWall(int t) {
798 
799     if ( Missions::GetInstance()->currentLevel->existFile("wallscenary.png") ) {
800 
801         wallLeft->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/wallscenary.png");
802 		wallRight->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath() + "/wallscenary.png");
803         Board::GetInstance()->wallScennaryOfLevel = true;
804 
805     } else {
806     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_WALLSCENARY")) {
807     		typeWall = t;
808     	} else {
809     		typeWall = 0;
810     	}
811 
812     	char tmp[4];
813     	sprintf(tmp, "%d", typeWall);
814 
815     	wallLeft->changeFrameFileFromData(0, "/scenary/wallscenary"+string(tmp)+".png");
816 		wallRight->changeFrameFileFromData(0, "/scenary/wallscenary"+string(tmp)+".png");
817 
818     	Board::GetInstance()->wallScennaryOfLevel = false;
819 
820     }
821 }
822 
scaleTiles(float scale)823 void Scenary::scaleTiles (float scale) {
824 	tileRight->setCurrentScale(scale);
825 	tileRightUp->setCurrentScale(scale);
826 	tileLeft->setCurrentScale(scale);
827 	tileLeftUp->setCurrentScale(scale);
828 	separatedTilesClosed = 25.0 * scale;
829 }
830 
setColorInWalls(int r,int g,int b)831 void Scenary::setColorInWalls(int r, int g, int b) {
832      colorRWallClosed = r;
833      colorGWallClosed = g;
834      colorBWallClosed = b;
835 
836      wallLeft->setRGB(r, g, b);
837      wallRight->setRGB(r, g, b);
838 }
839 
addTextObject(string t,string f,int a,Color c,int x,int y,float scale,bool flip,bool lock)840 void Scenary::addTextObject(string t, string f, int a, Color c, int x, int y, float scale, bool flip, bool lock) {
841 
842 	ObjectScenary *obj = new ObjectScenary();
843 	obj->setType("text");
844 
845 	obj->setText(t, f, a, c);
846 
847 	obj->addFrameText(f, t, a);
848 
849 	obj->setRGB(c.r, c.g, c.b);
850 
851 	obj->createBorder(220, 20, 20, 2, 5);
852 	obj->hideBorder();
853 	obj->setX(x);
854 	obj->setY(y);
855 	obj->setCurrentScale(scale);
856 	obj->setTypeScenary(type);
857 	obj->setTypeObject(TYPEOBJECT_TEXT);
858 	obj->setFlip(flip);
859 	addObject(TYPEOBJECT_TEXT, obj, lock);
860 }
861 
addObject(int typeObj,int origenObj,string object)862 void Scenary::addObject(int typeObj, int origenObj, string object) {
863 	addObject(typeObj, origenObj, object, 30, 0);
864 }
865 
addObject(int typeObj,int origenObj,string object,int x,int y)866 void Scenary::addObject(int typeObj, int origenObj, string object, int x, int y) {
867 	addObject(typeObj, origenObj, object, x, y, 1.0);
868 }
869 
addObject(int typeObj,int origenObj,string object,int x,int y,float scale)870 void Scenary::addObject(int typeObj, int origenObj, string object, int x, int y, float scale) {
871 	addObject(typeObj, origenObj, object, x, y, scale, false);
872 }
873 
addObject(int typeObj,int origenObj,string object,int x,int y,float scale,bool flip)874 void Scenary::addObject(int typeObj, int origenObj, string object, int x, int y, float scale, bool flip) {
875 	addObject(typeObj, origenObj, object, x, y, scale, flip, false);
876 }
877 
addObject(int typeObj,int origenObj,string object,int x,int y,float scale,bool flip,bool lock)878 void Scenary::addObject(int typeObj, int origenObj, string object, int x, int y, float scale, bool flip, bool lock) {
879 	char tmp[128];
880 	bool addobj = true;
881 
882 	TypeObjectScenary *os = TypeObjectsScenary::GetInstance()->getObject(object);
883 
884 	if ( (os==NULL) && (origenObj==ORIGENOBJECT_GAME) ) {
885 		addobj = false;
886 	}
887 
888 	if (addobj) {
889 		ObjectScenary *obj = new ObjectScenary();
890 		if (origenObj==ORIGENOBJECT_GAME) {
891 			sprintf(tmp, "%s/scenary/%s", DATA_DIR, os->file.c_str());
892 			obj->setMinScale(os->minScale);
893 			obj->setMaxScale(os->maxScale);
894 		} else {
895 			sprintf(tmp, "%s/objects/%s", Missions::GetInstance()->currentLevel->getPath().c_str(), object.c_str());
896 		}
897 		obj->setType(object);
898 		obj->addFrameFile(tmp);
899 		obj->createBorder(220, 20, 20, 2, 5);
900 		obj->hideBorder();
901 		obj->setX(x);
902 		obj->setY(y);
903 		obj->setCurrentScale(scale);
904 		obj->setTypeScenary(type);
905 		obj->setTypeObject(typeObj);
906 		obj->setFlip(flip);
907 		addObject(origenObj, obj, lock);
908 	} else {
909 		printf("Object '%s' don't exist\n", object.c_str());
910 	}
911 }
912 
addObject(int origenObj,ObjectScenary * el)913 void Scenary::addObject(int origenObj, ObjectScenary* el) {
914 	addObject(origenObj, el, false);
915 }
916 
addObject(int origenObj,ObjectScenary * el,bool lock)917 void Scenary::addObject(int origenObj, ObjectScenary* el, bool lock) {
918 	el->setOrigen(origenObj);
919     addElement(el);
920 	objects.push_back(el);
921 	el->setGroup(this);
922 	if (type==SCENARY_CLOSED) {
923     	if (el->getX()<el->getDisplacementInXByGroups()) {
924     		el->setFlip(true);
925     		el->setAlign(ALIGN_RIGHT);
926     		el->iconZoomin->addX(-el->getWidth());
927     		el->iconZoomout->addX(-el->getWidth());
928     		el->iconDuplicate->addX(-el->getWidth());
929     		el->iconDelete->addX(-el->getWidth());
930     		el->iconLock->addX(-el->getWidth());
931     		el->iconFlip->addX(-el->getWidth());
932     		el->iconOrderBottom->addX(-el->getWidth());
933     		el->iconOrderTop->addX(-el->getWidth());
934     	}
935     }
936     if (lock) el->swapLock();
937 }
938 
onOver()939 void Scenary::onOver() {
940 	if (editable) {
941 		int i;
942 		for (i=0; i<(int)objects.size(); i++) {
943 			objects[i]->onOver();
944 		}
945 
946 		int objColor = -1;
947 		for (i=0; i<(int)objects.size(); i++) {
948 			if ( (objects[i]->isOnMouseOver()) && (!objects[i]->getLock()) ) {
949                 if (objColor < 0) {
950                     objColor = i;
951                 } else {
952                     if (objects[i]->getIndexZ() >= objects[objColor]->getIndexZ()) {
953                         objColor = i;
954                     }
955                 }
956 			}
957         }
958         if (objColor >= 0) {
959             for (i=0; i<(int)objects.size(); i++) {
960                 if (i==objColor) {
961 					objects[i]->setRGB(160, 160, 160);
962                 } else {
963 					if (objects[i]->getTypeObject()==TYPEOBJECT_TEXT) {
964 						objects[i]->setRGB(objects[i]->getColorText().r, objects[i]->getColorText().g, objects[i]->getColorText().b);
965 					} else {
966 						objects[i]->setRGB(255, 255, 255);
967 					}
968                 }
969             }
970         }
971 	}
972 }
973 
verifyClick()974 void Scenary::verifyClick () {
975 	if (editable) {
976 		int i;
977 		int objDrag = -1;
978 		for (i=0; i<(int)objects.size(); i++) {
979 			if ( (objects[i]->isOnMouseOver()) && (!objects[i]->getLock()) ) {
980                 if (objDrag < 0) {
981                     objDrag = i;
982                 } else {
983                     if (objects[i]->getIndexZ() >= objects[objDrag]->getIndexZ()) {
984                         objDrag = i;
985                     }
986                 }
987 			}
988         }
989         if (objDrag >= 0) objects[objDrag]->startDrag();
990 		for (i=0; i<(int)objects.size(); i++) {
991 			/*if (objects[i]->isOnMouseOver()) {
992 				objects[i]->startDrag();
993 			}*/
994 			if (objects[i]->getShowIcons()) {
995 				if (objects[i]->iconZoomin->isOnMouseOver()) {
996 					if (objects[i]->getCurrentScaleX()>objects[i]->getMinScale()) objects[i]->addCurrentScale(-0.1);
997 				}
998 				if (objects[i]->iconZoomout->isOnMouseOver()) {
999 					if (objects[i]->getCurrentScaleX()<objects[i]->getMaxScale()) objects[i]->addCurrentScale(0.1);
1000 				}
1001 				if (objects[i]->iconDuplicate->isOnMouseOver()) {
1002 					char tmp[128];
1003 					ObjectScenary *obj = new ObjectScenary();
1004 					obj->setType(objects[i]->getType());
1005 					if (objects[i]->getOrigen()==ORIGENOBJECT_GAME) {
1006 						sprintf(tmp, "%s/scenary/%s",  DATA_DIR, TypeObjectsScenary::GetInstance()->getObject(objects[i]->getType())->file.c_str());
1007 					} else {
1008 						sprintf(tmp, "%s/objects/%s", Missions::GetInstance()->currentLevel->getPath().c_str(), objects[i]->getType().c_str());
1009 					}
1010 					obj->addFrameFile(tmp);
1011 					float dx = objects[i]->getXWithoutDisplacementByGroups()+objects[i]->getWidth();
1012 					if (objects[i]->getX()+objects[i]->getWidth()>740) {
1013                           dx = objects[i]->getXWithoutDisplacementByGroups()-objects[i]->getWidth();
1014                     }
1015 					obj->createBorder(220, 20, 20, 2, 5);
1016 					obj->hideBorder();
1017 					obj->setX(dx);
1018 					obj->setY(objects[i]->getYWithoutDisplacementByGroups());
1019 					obj->setCurrentScale(objects[i]->getCurrentScaleX());
1020 					obj->setTypeScenary(objects[i]->getTypeScenary());
1021 					obj->setTypeObject(objects[i]->getTypeObject());
1022 					obj->setFlip(objects[i]->getFlip());
1023 					addObject(objects[i]->getOrigen(), obj);
1024 				}
1025 				if (objects[i]->iconDelete->isOnMouseOver()) {
1026 					vector<ObjectScenary*>::iterator e = objects.begin();
1027 					objects.erase(e+i);
1028 				}
1029 				if (objects[i]->iconLock->isOnMouseOver()) {
1030 					objects[i]->swapLock();
1031 				}
1032 				if (objects[i]->iconOrderTop->isOnMouseOver()) {
1033 					if (i<(int)objects.size()-1) {
1034                          ObjectScenary* aux = objects[i];
1035                          objects[i] = objects[i+1];
1036                          objects[i+1] = aux;
1037                     }
1038 				}
1039 				if (objects[i]->iconOrderBottom->isOnMouseOver()) {
1040 					if (i>0) {
1041                          ObjectScenary* aux = objects[i];
1042                          objects[i] = objects[i-1];
1043                          objects[i-1] = aux;
1044                     }
1045 				}
1046 				if (objects[i]->getTypeScenary()==SCENARY_OPENED) {
1047                     if (objects[i]->iconFlip->isOnMouseOver()) {
1048                         objects[i]->swapFlip();
1049                     }
1050                 }
1051 			}
1052 		}
1053 	}
1054 }
1055 
verifyShowIcons()1056 void Scenary::verifyShowIcons () {
1057 	if (editable) {
1058 		int objSelect = -1;
1059 		Uint8 *keystate = SDL_GetKeyState(NULL);
1060 		if ( keystate[SDLK_RCTRL] || keystate[SDLK_LCTRL]) {
1061 			for (int i=0; i<(int)objects.size(); i++) {
1062 				if ( (objects[i]->isOnMouseOver()) && (objects[i]->getLock()) ) {
1063 					if (objSelect < 0) {
1064 						objSelect = i;
1065 					} else {
1066 						if (objects[i]->getIndexZ() >= objects[objSelect]->getIndexZ()) {
1067 							objSelect = i;
1068 						}
1069 					}
1070 				}
1071 			}
1072 		} else {
1073 			for (int i=0; i<(int)objects.size(); i++) {
1074 				if ( (objects[i]->isOnMouseOver()) && (!objects[i]->getLock()) ) {
1075 					if (objSelect < 0) {
1076 						objSelect = i;
1077 					} else {
1078 						if (objects[i]->getIndexZ() >= objects[objSelect]->getIndexZ()) {
1079 							objSelect = i;
1080 						}
1081 					}
1082 				}
1083 			}
1084 		}
1085 
1086 		int i;
1087 		for (i=0; i<(int)objects.size(); i++) {
1088 			if ( (objects[i]->isOnMouseOver()) && (i==objSelect) ) {
1089 				objects[i]->swapShowIcons();
1090 				if (objects[i]->getShowIcons()) {
1091 					objects[i]->showBorder();
1092 				} else {
1093 					objects[i]->hideBorder();
1094 				}
1095 			} else {
1096 				objects[i]->setShowIcons(false);
1097 				objects[i]->hideBorder();
1098 			}
1099 		}
1100 		if (objSelect>=0) {
1101     		objects[objSelect]->setX(objects[objSelect]->getXWithoutDisplacementByGroups());
1102     		objects[objSelect]->setY(objects[objSelect]->getYWithoutDisplacementByGroups());
1103         }
1104 	}
1105 }
1106 
toXML()1107 string Scenary::toXML () {
1108     string xml="";
1109     string tscenary = "closed";
1110 	switch(type) {
1111 		case SCENARY_CLOSED:
1112 			tscenary = "closed";
1113 		break;
1114 		case SCENARY_OPENED:
1115 			tscenary = "opened";
1116 		break;
1117     }
1118 
1119     char tmp_val[128];
1120     sprintf (tmp_val, "\t<scenary type=\"%s\" red=\"%d\" green=\"%d\" blue=\"%d\" type-tiled=\"%d\" ntiles=\"%d\" type-terrain=\"%d\" type-wall=\"%d\">\n", tscenary.c_str(), getR_ColorInWalls(), getG_ColorInWalls(), getB_ColorInWalls(), typeTiled, nVerticalTiles, typeTerrain, typeWall);
1121     xml = xml + tmp_val;
1122 
1123 	int i;
1124 	for (i=0; i<(int)objects.size(); i++) {
1125 		char tmp_val2[512];
1126         char tmp_f[8];
1127         if (objects[i]->getFlip()) {
1128            sprintf(tmp_f, "true");
1129         } else {
1130            sprintf(tmp_f, "false");
1131         }
1132         char tmp_l[8];
1133         if (objects[i]->getLock()) {
1134            sprintf(tmp_l, "true");
1135         } else {
1136            sprintf(tmp_l, "false");
1137         }
1138 		char tmp_o[8];
1139         if (objects[i]->getOrigen() == ORIGENOBJECT_GAME) {
1140            sprintf(tmp_o, "game");
1141         } else {
1142            sprintf(tmp_o, "personal");
1143         }
1144 
1145         if (objects[i]->getTypeObject() == TYPEOBJECT_2D) {
1146         	sprintf (tmp_val2, "\t\t<object name=\"%s\" x=\"%d\" y=\"%d\" scale=\"%f\" flip=\"%s\" lock=\"%s\" origen=\"%s\" type=\"2d\" />\n", objects[i]->getType().c_str(), (int)objects[i]->getXWithoutDisplacementByGroups(), (int)objects[i]->getYWithoutDisplacementByGroups(), objects[i]->getCurrentScaleX(), tmp_f, tmp_l, tmp_o);
1147         } else if (objects[i]->getTypeObject() == TYPEOBJECT_TEXT) {
1148         	sprintf (tmp_val2, "\t\t<object name=\"%s\" x=\"%d\" y=\"%d\" scale=\"%f\" flip=\"%s\" lock=\"%s\" origen=\"%s\" type=\"text\" text=\"%s\" font=\"%s\" align=\"%d\" r=\"%d\" g=\"%d\" b=\"%d\" />\n", objects[i]->getType().c_str(), (int)objects[i]->getXWithoutDisplacementByGroups(), (int)objects[i]->getYWithoutDisplacementByGroups(), objects[i]->getCurrentScaleX(), tmp_f, tmp_l, tmp_o, objects[i]->getText().c_str(), objects[i]->getFontText().c_str(), objects[i]->getAlignText(), objects[i]->getColorText().r, objects[i]->getColorText().g, objects[i]->getColorText().b);
1149         } else {
1150 			sprintf (tmp_val2, "\t\t<object name=\"%s\" x=\"%d\" y=\"%d\" scale=\"%f\" flip=\"%s\" lock=\"%s\" origen=\"%s\" type=\"3d\" />\n", objects[i]->getType().c_str(), (int)objects[i]->getXWithoutDisplacementByGroups(), (int)objects[i]->getYWithoutDisplacementByGroups(), objects[i]->getCurrentScaleX(), tmp_f, tmp_l, tmp_o);
1151 		}
1152 
1153         xml = xml + tmp_val2;
1154 	}
1155 
1156     xml = xml + "\t</scenary>\n";
1157 
1158     return xml;
1159 }
1160 
removeDnd()1161 void Scenary::removeDnd () {
1162 	if (editable) {
1163 		int i;
1164 		for (i=0; i<(int)objects.size(); i++) {
1165 			objects[i]->drop();
1166 		}
1167 	}
1168 }
1169 
draw()1170 void Scenary::draw() {
1171 	if (type==SCENARY_CLOSED) {
1172 		drawScenaryClosed();
1173 	} else {
1174 		drawScenaryOpened();
1175 	}
1176 	if (editable && showGuides) {
1177 		guideLeft->draw();
1178 		guideRight->draw();
1179 	}
1180 }
1181 
drawScenaryClosed()1182 void Scenary::drawScenaryClosed() {
1183 	wallLeft->draw();
1184 	wallRight->draw();
1185 
1186 	paintTiled();
1187 
1188 	shadowWall->draw();
1189 	for (int i=0; i<(int)objects.size(); i++) {
1190 		objects[i]->draw();
1191 	}
1192 
1193 }
1194 
drawScenaryOpened()1195 void Scenary::drawScenaryOpened() {
1196 
1197 	terrainOpened->draw();
1198 	for (int i=0; i<(int)objects.size(); i++) {
1199 		objects[i]->draw();
1200 	}
1201 
1202 	paintTiled();
1203 
1204 }
1205 
paintTiled()1206 void Scenary::paintTiled() {
1207 	int x, y;
1208 
1209 	for (y=0; y<nVerticalTiles; y++) {
1210 		for (x=0; x<Board::GetInstance()->getCols()+1; x++) {
1211 			if (y==nVerticalTiles-1) {
1212 				tileRightUp->setX(Plot::GetInstance()->getX(x,0)-1);
1213 				tileRightUp->setY(Plot::GetInstance()->getY(x,0)+38-(y*separatedTilesClosed));
1214 				tileRightUp->draw();
1215 				tileLeftUp->setX(-Plot::GetInstance()->getX(x,0)+1);
1216 				tileLeftUp->setY(Plot::GetInstance()->getY(x,0)+38-(y*separatedTilesClosed));
1217 				tileLeftUp->draw();
1218 			} else {
1219 				tileRight->setX(Plot::GetInstance()->getX(x,0)-1);
1220 				tileRight->setY(Plot::GetInstance()->getY(x,0)+38-(y*separatedTilesClosed));
1221 				tileRight->draw();
1222 				tileLeft->setX(-Plot::GetInstance()->getX(x,0)+1);
1223 				tileLeft->setY(Plot::GetInstance()->getY(x,0)+38-(y*separatedTilesClosed));
1224 				tileLeft->draw();
1225 			}
1226 		}
1227 	}
1228 }
1229 
unLoad()1230 void Scenary::unLoad() {
1231 
1232 	delete tileLeft;
1233 	delete tileLeftUp;
1234 	delete tileRight;
1235 	delete tileRightUp;
1236 	delete wallLeft;
1237 	delete wallRight;
1238 	delete guideLeft;
1239 	delete guideRight;
1240 	delete shadowWall;
1241 	delete terrainOpened;
1242 
1243 	for (int i=0; i<(int)objects.size(); i++) {
1244 		delete objects[i];
1245 	}
1246 
1247 	objects.clear();
1248 
1249 }
1250 
1251 /*****************************
1252 **
1253 ** CLASE ElementIsometric
1254 **
1255 ******************************/
1256 
1257 
ElementIsometric()1258 ElementIsometric::ElementIsometric() : AnimatedElement () {
1259 	heightElement = 0;
1260 	originalHeightElement = 0;
1261 	xBoard = 0;
1262 	yBoard = 0;
1263 	displacementX = 0;
1264 	originalDisplacementX = 0;
1265 }
1266 
~ElementIsometric()1267 ElementIsometric::~ElementIsometric () {
1268 	unLoad();
1269 }
1270 
1271 /*****************************
1272 **
1273 ** CLASE MovableElementIsometric
1274 **
1275 ******************************/
1276 
1277 
MovableElementIsometric()1278 MovableElementIsometric::MovableElementIsometric() : ElementIsometric () {
1279 	direction = TILE_RIGHT;
1280 	isMoving = false;
1281 	nMovement = 0;
1282 }
1283 
~MovableElementIsometric()1284 MovableElementIsometric::~MovableElementIsometric () {
1285 	unLoad();
1286 }
1287 
setPosition(BoardPosition p)1288 bool MovableElementIsometric::setPosition(BoardPosition p) {
1289     if (!p.out) {
1290 		int rel = Board::GetInstance()->referencePositions(getPosition(), p);
1291 		if (rel== TILE_CENTER) {
1292 			//Board::GetInstance()->nextTurn();
1293 			return true;
1294 		}
1295         if (!Board::GetInstance()->hasWallBetweenCells(getPosition(), p)) {
1296             if ( rel != TILE_OTHER ) {
1297                     if ( (rel== TILE_LEFT) && (getAnimation()->getName() != "walk_left") ) {
1298                          setForceAnimation("walk_left");
1299                          setDirection(TILE_LEFT);
1300                     } else if ( (rel== TILE_RIGHT) && (getAnimation()->getName() != "walk_right") ) {
1301                          setForceAnimation("walk_right");
1302                          setDirection(TILE_RIGHT);
1303                     } else if ( (rel== TILE_UP) && (getAnimation()->getName() != "walk_up") ) {
1304                          setForceAnimation("walk_up");
1305                          setDirection(TILE_UP);
1306                     } else if ( (rel== TILE_DOWN) && (getAnimation()->getName() != "walk_down") ) {
1307                          setForceAnimation("walk_down");
1308                          setDirection(TILE_DOWN);
1309                     }
1310                     setMovementNormal(Plot::GetInstance()->getX(p.x,p.y), Plot::GetInstance()->getY(p.x,p.y)-heightElement, 650);
1311                     /*if ( (rel==TILE_UP) || (rel==TILE_LEFT) ) {
1312                        xBoard = p.x;
1313                        yBoard = p.y;
1314                     }*/
1315 					setNextXBoard(p.x);
1316 					setNextYBoard(p.y);
1317                     isMoving = true;
1318                     addHistoryPositions(p);
1319             }
1320 			return true;
1321         }
1322     }
1323 	return false;
1324 }
1325 
setInitialPosition(BoardPosition p)1326 bool MovableElementIsometric::setInitialPosition(BoardPosition p) {
1327      if (!p.out) {
1328           BoardPosition old = getPosition();
1329           xBoard = p.x;
1330           yBoard = p.y;
1331           setX(Plot::GetInstance()->getX(p.x,p.y));
1332           setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1333           addHistoryPositions(p);
1334           return true;
1335      }
1336      return false;
1337 }
1338 
setForcePosition(BoardPosition p)1339 bool MovableElementIsometric::setForcePosition(BoardPosition p) {
1340      if (!p.out) {
1341           BoardPosition old = getPosition();
1342           xBoard = p.x;
1343           yBoard = p.y;
1344           if ( !Board::GetInstance()->verifyPositions(getPosition(), old) ) {
1345                setX(Plot::GetInstance()->getX(p.x,p.y));
1346                setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1347           }
1348           addHistoryPositions(p);
1349           return true;
1350      }
1351      return false;
1352 }
1353 
stop()1354 void MovableElementIsometric::stop() {
1355     if (getDirection()==TILE_RIGHT) {
1356        setForceAnimation("stop_right");
1357     } else if (getDirection()==TILE_LEFT) {
1358        setForceAnimation("stop_left");
1359     } else if (getDirection()==TILE_UP) {
1360        setForceAnimation("stop_up");
1361     } else if (getDirection()==TILE_DOWN) {
1362        setForceAnimation("stop_down");
1363     }
1364 	isMoving = false;
1365 	removeTypeMovement();
1366 	/*if ((int)historyPositions.size()>0) {
1367        xBoard = historyPositions[historyPositions.size()-1].x;
1368        yBoard = historyPositions[historyPositions.size()-1].y;
1369     }*/
1370 }
1371 
addHistoryPositions(BoardPosition p)1372 void MovableElementIsometric::addHistoryPositions(BoardPosition p) {
1373 	if (nMovement < (int)historyPositions.size()) {
1374 		int i;
1375 		for (i=(int)historyPositions.size()-1; i>nMovement-1; i--) {
1376 			historyPositions.erase(historyPositions.begin()+i);
1377 		}
1378 	}
1379 	historyPositions.push_back(p);
1380 	nMovement++;
1381 }
1382 
previousHistoryPositions()1383 void MovableElementIsometric::previousHistoryPositions() {
1384 	if (nMovement>1) {
1385 		nMovement--;
1386 		BoardPosition p = historyPositions[nMovement-1];
1387 		xBoard = p.x;
1388 		yBoard = p.y;
1389 		setX(Plot::GetInstance()->getX(p.x,p.y));
1390 		setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1391 		if (nMovement-2 > 0) {
1392 			int i = Board::GetInstance()->referencePositions(historyPositions[nMovement-2], p);
1393 			if (i == TILE_LEFT) {
1394 				setForceAnimation("stop_left");
1395 			} else if (i == TILE_RIGHT) {
1396 				setForceAnimation("stop_right");
1397 			} else if (i == TILE_UP) {
1398 				setForceAnimation("stop_up");
1399 			} else if (i == TILE_DOWN) {
1400 				setForceAnimation("stop_down");
1401 			}
1402 		} else {
1403 			setForceAnimation("stop_right");
1404 		}
1405 	}
1406 }
1407 
nextHistoryPositions()1408 void MovableElementIsometric::nextHistoryPositions() {
1409 	if (nMovement<(int)historyPositions.size()) {
1410 		nMovement++;
1411 		BoardPosition p = historyPositions[nMovement-1];
1412 		xBoard = p.x;
1413 		yBoard = p.y;
1414 		setX(Plot::GetInstance()->getX(p.x,p.y));
1415 		setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1416 		if (nMovement-2 > 0) {
1417 			int i = Board::GetInstance()->referencePositions(historyPositions[nMovement-2], p);
1418 			if (i == TILE_LEFT) {
1419 				setForceAnimation("stop_left");
1420 			} else if (i == TILE_RIGHT) {
1421 				setForceAnimation("stop_right");
1422 			} else if (i == TILE_UP) {
1423 				setForceAnimation("stop_up");
1424 			} else if (i == TILE_DOWN) {
1425 				setForceAnimation("stop_down");
1426 			}
1427 		} else {
1428 			setForceAnimation("stop_right");
1429 		}
1430 	}
1431 }
1432 
1433 /*****************************
1434 **
1435 ** CLASE Player
1436 **
1437 ******************************/
1438 
1439 
Player()1440 Player::Player() : MovableElementIsometric () {
1441 }
1442 
~Player()1443 Player::~Player () {
1444 	unLoad();
1445 }
1446 
setPosition(BoardPosition p)1447 bool Player::setPosition(BoardPosition p) {
1448     if (!p.out) {
1449 		// verifing is nimuh walk to enemy
1450 		if (Board::GetInstance()->hasEnemy1) {
1451 			if ( Board::GetInstance()->verifyPositions(Board::GetInstance()->getPositionEnemy1(), p) ) {
1452 				Board::GetInstance()->nextTurn();
1453 				return false;
1454 			}
1455 		}
1456 		if (Board::GetInstance()->hasEnemy2) {
1457     		if ( Board::GetInstance()->verifyPositions(Board::GetInstance()->getPositionEnemy2(), p) ) {
1458     			Board::GetInstance()->nextTurn();
1459     			return false;
1460     		}
1461         }
1462 		if (Board::GetInstance()->hasEnemy3) {
1463     		if ( Board::GetInstance()->verifyPositions(Board::GetInstance()->getPositionEnemy3(), p) ) {
1464     			Board::GetInstance()->nextTurn();
1465     			return false;
1466     		}
1467         }
1468 
1469 		int rel = Board::GetInstance()->referencePositions(getPosition(), p);
1470 		if (rel== TILE_CENTER) {
1471             addHistoryPositions(p);
1472 			Board::GetInstance()->nextTurn();
1473 			return true;
1474 		}
1475         if (!Board::GetInstance()->hasWallBetweenCells(getPosition(), p)) {
1476             if ( rel != TILE_OTHER ) {
1477                     if (rel== TILE_LEFT) {
1478                          setForceAnimation("walk_left");
1479                          setDirection(TILE_LEFT);
1480                     } else if (rel== TILE_RIGHT) {
1481                          setForceAnimation("walk_right");
1482                          setDirection(TILE_RIGHT);
1483                     } else if (rel== TILE_UP) {
1484                          setForceAnimation("walk_up");
1485                          setDirection(TILE_UP);
1486                     } else if (rel== TILE_DOWN) {
1487                          setForceAnimation("walk_down");
1488                          setDirection(TILE_DOWN);
1489                     }
1490                     setMovementNormal(Plot::GetInstance()->getX(p.x,p.y), Plot::GetInstance()->getY(p.x,p.y)-heightElement, 650);
1491                     /*if ( (rel==TILE_UP) || (rel==TILE_LEFT) ) {
1492                        xBoard = p.x;
1493                        yBoard = p.y;
1494                     }*/
1495 					setNextXBoard(p.x);
1496 					setNextYBoard(p.y);
1497                     isMoving = true;
1498                     addHistoryPositions(p);
1499 					return true;
1500             }
1501 			return false;
1502         }
1503     }
1504 	return false;
1505 }
1506 
1507 
1508 /*****************************
1509 **
1510 ** CLASE Enemy
1511 **
1512 ******************************/
1513 
1514 
Enemy()1515 Enemy::Enemy() : MovableElementIsometric () {
1516     firstMovementHorizontal = true;
1517 	stopped = false;
1518 }
1519 
~Enemy()1520 Enemy::~Enemy () {
1521 	unLoad();
1522 }
1523 
move()1524 bool Enemy::move() {
1525 	if (!stopped) {
1526 		if (firstMovementHorizontal) {
1527 			if (getXBoard() == Board::GetInstance()->getPositionPlayer().x) {
1528 				return move_Vertical(false);
1529 			} else {
1530 				return move_Horizontal(false);
1531 			}
1532 		} else {
1533 			if (getYBoard() == Board::GetInstance()->getPositionPlayer().y) {
1534 				return move_Horizontal(false);
1535 			} else {
1536 				return move_Vertical(false);
1537 			}
1538 		}
1539 	}
1540 	return false;
1541 }
1542 
move_Vertical(bool forceNextTurnWithWall)1543 bool Enemy::move_Vertical(bool forceNextTurnWithWall) {
1544 	if (getYBoard() < Board::GetInstance()->getPositionPlayer().y) {
1545 		BoardPosition p;
1546 		p.x = getXBoard();
1547 		p.y = getYBoard()+1;
1548 		p.out = !Board::GetInstance()->positionIsOnBoard(p);
1549 		if ( !Board::GetInstance()->hasWallBetweenCells(getPosition(), p) ) {
1550 			setPosition(p);
1551 			return true;
1552 		} else {
1553 			if (!forceNextTurnWithWall) {
1554 				return move_Horizontal(true);
1555 			} else {
1556 				setForceAnimation("stop_down"); // CUIDADO
1557 				setForcePosition(getPosition());
1558 				//Board::GetInstance()->nextTurn();
1559 				return false;
1560 			}
1561 		}
1562 	} else if (getYBoard() > Board::GetInstance()->getPositionPlayer().y) {
1563 		BoardPosition p;
1564 		p.x = getXBoard();
1565 		p.y = getYBoard()-1;
1566 		p.out = !Board::GetInstance()->positionIsOnBoard(p);
1567 		if ( !Board::GetInstance()->hasWallBetweenCells(getPosition(), p) ) {
1568 			setPosition(p);
1569 			return true;
1570 		} else {
1571 			if (!forceNextTurnWithWall) {
1572 				return move_Horizontal(true);
1573 			} else {
1574 				setForceAnimation("stop_up"); // CUIDADO
1575 				setForcePosition(getPosition());
1576 				//Board::GetInstance()->nextTurn();
1577 				return false;
1578 			}
1579 		}
1580 	} else {
1581 		if (getXBoard() < Board::GetInstance()->getPositionPlayer().x) {
1582 			setForceAnimation("stop_right");
1583 		} else if (getXBoard() > Board::GetInstance()->getPositionPlayer().x) {
1584 			setForceAnimation("stop_left");
1585 		}
1586 		setForcePosition(getPosition());
1587 		//Board::GetInstance()->nextTurn();
1588 		return false;
1589 	}
1590 
1591 	return false;
1592 }
1593 
move_Horizontal(bool forceNextTurnWithWall)1594 bool Enemy::move_Horizontal(bool forceNextTurnWithWall) {
1595 	if (getXBoard() < Board::GetInstance()->getPositionPlayer().x) {
1596 		BoardPosition p;
1597 		p.x = getXBoard()+1;
1598 		p.y = getYBoard();
1599 		p.out = !Board::GetInstance()->positionIsOnBoard(p);
1600 		if ( !Board::GetInstance()->hasWallBetweenCells(getPosition(), p) ) {
1601 			setPosition(p);
1602 			return true;
1603 		} else {
1604 			if (!forceNextTurnWithWall) {
1605 				return move_Vertical(true);
1606 			} else {
1607 				setForceAnimation("stop_right"); // CUIDADO
1608 				setForcePosition(getPosition());
1609 				//Board::GetInstance()->nextTurn();
1610 				return false;
1611 			}
1612 		}
1613 	} else if (getXBoard() > Board::GetInstance()->getPositionPlayer().x) {
1614 		BoardPosition p;
1615 		p.x = getXBoard()-1;
1616 		p.y = getYBoard();
1617 		p.out = !Board::GetInstance()->positionIsOnBoard(p);
1618 		if ( !Board::GetInstance()->hasWallBetweenCells(getPosition(), p) ) {
1619 			setPosition(p);
1620 			return true;
1621 		} else {
1622 			if (!forceNextTurnWithWall) {
1623 				return move_Vertical(true);
1624 			} else {
1625 				setForceAnimation("stop_left"); // CUIDADO
1626 				setForcePosition(getPosition());
1627 				//Board::GetInstance()->nextTurn();
1628 				return false;
1629 			}
1630 		}
1631 	} else {
1632 		if (getYBoard() < Board::GetInstance()->getPositionPlayer().y) {
1633 			setForceAnimation("stop_down");
1634 		} else if (getYBoard() > Board::GetInstance()->getPositionPlayer().y) {
1635 			setForceAnimation("stop_up");
1636 		}
1637 		setForcePosition(getPosition());
1638 		//Board::GetInstance()->nextTurn();
1639 		return false;
1640 	}
1641 
1642 	return false;
1643 }
1644 
stop()1645 void Enemy::stop() {
1646 	if ( (Board::GetInstance()->getTurn()==TURN2_ENEMY)
1647 		|| (Board::GetInstance()->getTurn()==TURN_ENEMY3) ) {
1648 		if (getDirection()==TILE_RIGHT) {
1649 		   setForceAnimation("stop_right");
1650 		} else if (getDirection()==TILE_LEFT) {
1651 		   setForceAnimation("stop_left");
1652 		} else if (getDirection()==TILE_UP) {
1653 		   setForceAnimation("stop_up");
1654 		} else if (getDirection()==TILE_DOWN) {
1655 		   setForceAnimation("stop_down");
1656 		}
1657 	}
1658 	isMoving = false;
1659 	removeTypeMovement();
1660 }
1661 
1662 /*****************************
1663 **
1664 ** CLASE Door
1665 **
1666 ******************************/
1667 
1668 
Door()1669 Door::Door() : ElementIsometric () {
1670 
1671 	//addFrameFileFromData("/board/door_h.png");
1672 	addRangeFramesFromData("board/door_h", "png", 0, 3);
1673 
1674 	//addFrameFileFromData("/board/door_v.png");
1675 	addRangeFramesFromData("board/door_v", "png", 0, 3);
1676 
1677 	Animation a;
1678 	a.setName("horizontal");
1679 	a.setCyclesBetweenFrames(16);
1680 	a.addRangeFrame(0, 3, 1);
1681 	addAnimation(a);
1682 	Animation b;
1683 	b.setName("vertical");
1684 	b.setCyclesBetweenFrames(16);
1685 	b.addRangeFrame(4, 7, 1);
1686 	addAnimation(b);
1687 
1688 	setDoorPosition(OTHER);
1689 
1690 }
1691 
~Door()1692 Door::~Door () {
1693 	unLoad();
1694 }
1695 
setDoorPosition(int p)1696 void Door::setDoorPosition(int p) {
1697 	doorPosition = p;
1698 	if (doorPosition == DOWN) {
1699 		setAlign(ALIGN_RIGHT);
1700 		setHeightElement(95*getCurrentScaleX());
1701 		setForceAnimation("horizontal");
1702 	} else if (doorPosition == UP) {
1703 		setAlign(ALIGN_LEFT);
1704 		setHeightElement(125*getCurrentScaleX());
1705 		setForceAnimation("horizontal");
1706 	} else if (doorPosition == LEFT) {
1707 		setAlign(ALIGN_RIGHT);
1708 		setHeightElement(125*getCurrentScaleX());
1709 		setForceAnimation("vertical");
1710 	} else if (doorPosition == RIGHT) {
1711 		setAlign(ALIGN_LEFT);
1712 		setHeightElement(95*getCurrentScaleX());
1713 		setForceAnimation("vertical");
1714 	}
1715 }
1716 
setForcePosition(BoardPosition p)1717 bool Door::setForcePosition(BoardPosition p) {
1718 	if (!p.out) {
1719 		if ( (p.x==0) || (p.x==Board::GetInstance()->getCols()-1)
1720 				|| (p.y==0) || (p.y==Board::GetInstance()->getRows()-1) ) {
1721 			xBoard = p.x;
1722 			yBoard = p.y;
1723 
1724 			if (p.x==0) {
1725 				setDoorPosition(LEFT);
1726 				setX(Plot::GetInstance()->getX(p.x,p.y)+(20*getCurrentScaleX()));
1727 				setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1728 			}
1729 			if (p.x==Board::GetInstance()->getCols()-1) {
1730 				setDoorPosition(RIGHT);
1731 				setX(Plot::GetInstance()->getX(p.x,p.y)-(30*getCurrentScaleX()));
1732 				setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1733 			}
1734 			if (p.y==0) {
1735 				setDoorPosition(UP);
1736 				setX(Plot::GetInstance()->getX(p.x,p.y)-(23*getCurrentScaleX()));
1737 				setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1738 			}
1739 			if (p.y==Board::GetInstance()->getRows()-1) {
1740 				setDoorPosition(DOWN);
1741 				setX(Plot::GetInstance()->getX(p.x,p.y)+(25*getCurrentScaleX()));
1742 				setY(Plot::GetInstance()->getY(p.x,p.y)-heightElement);
1743 			}
1744 
1745 			return true;
1746 
1747 		} else {
1748 			setDoorPosition(OTHER);
1749 			return false;
1750 		}
1751 	}
1752 	setDoorPosition(OTHER);
1753 	return false;
1754 }
1755 
setForcePosition(int x,int y)1756 bool Door::setForcePosition(int x, int y) {
1757 	BoardPosition p;
1758 	p.x=x;
1759 	p.y=y;
1760 	p.out = !Board::GetInstance()->positionIsOnBoard(p);
1761 	return setForcePosition(p);
1762 }
1763 
1764 /*****************************
1765 **
1766 ** CLASE Board
1767 **
1768 ******************************/
1769 
1770 Board* Board::instance = NULL;
1771 
Board()1772 Board::Board() : Group () {
1773 	multiLanguage = false;
1774 	mlPause = 0;
1775 	rows = 6;
1776 	cols = 6;
1777 	typeFloor = 0;
1778 	typeWall = 0;
1779 	typePavement = 0;
1780 	indexz = 0;
1781 	posX = World::width/2;
1782 	posY = POSITION_Y_BOARD;
1783 	typeSize = BOARD_6X6;
1784 	hasEnemy1 = false;
1785 	canChangeInHistory_Enemy1 = false;
1786 	hasEnemy2 = false;
1787 	canChangeInHistory_Enemy2 = false;
1788 	hasEnemy3 = false;
1789 	canChangeInHistory_Enemy3 = false;
1790 	hasBlackHole0 = false;
1791 	canChangeInHistory_BlackHole0 = false;
1792 	hasBlackHole1 = false;
1793 	canChangeInHistory_BlackHole1 = false;
1794 	teletransport = false;
1795 	doorKeyClosed = true;
1796 	hasTrap = false;
1797 	hasKey = false;
1798 	canChangeInHistory_Key = false;
1799 	hasTray = false;
1800 	canChangeInHistory_Tray = false;
1801 	directionKey = "right";
1802 	todoNextTurn = false;
1803 	catchedEnemy1 = false;
1804     catchedEnemy2 = false;
1805     inExplosion = false;
1806     paddingExplosion = 0;
1807 	turn = TURN_PLAYER;
1808 
1809 	town.name="";
1810 	town.province="";
1811 	town.nickname="";
1812 
1813 	infoTray.title="";
1814 	infoTray.text="";
1815 
1816 	isAnimationWaiting = false;
1817 	typeAnimationWaiting = 0;
1818 
1819 	wallsOfLevel = false;
1820 	terrainOfLevel = false;
1821 	wallScennaryOfLevel = false;
1822 	musicOfLevel = false;
1823 	pavementOfLevel = false;
1824 	floorOfLevel = false;
1825 	tiledOfLevel = false;
1826 
1827 	totalHistory = 0;
1828 	currentHistory = 0;
1829 
1830 }
1831 
GetInstance()1832 Board* Board::GetInstance () {
1833 	if ( instance == NULL ) {
1834 		instance = new Board();
1835 	}
1836 	return instance;
1837 }
1838 
setTypeWall(int t)1839 void Board::setTypeWall(int t) {
1840 
1841     if ( Missions::GetInstance()->currentLevel->existFile("wall_h.png")
1842         && Missions::GetInstance()->currentLevel->existFile("wall_v.png") ) {
1843 
1844     	wallBottom->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/wall_h.png");
1845     	wallRight->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/wall_v.png");
1846     	wallsOfLevel = true;
1847 
1848     } else {
1849     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_WALL")) {
1850     		typeWall = t;
1851     	} else {
1852     		typeWall = 0;
1853     	}
1854 
1855     	char tmp[128];
1856 
1857     	sprintf(tmp, "/board/wall_%d_h.png", typeWall);
1858     	wallBottom->changeFrameFileFromData(0, tmp);
1859 
1860     	sprintf(tmp, "/board/wall_%d_v.png", typeWall);
1861     	wallRight->changeFrameFileFromData(0, tmp);
1862 
1863     	wallsOfLevel = false;
1864     }
1865 
1866 
1867 }
1868 
setTypeFloor(int t)1869 void Board::setTypeFloor(int t) {
1870 
1871     if ( Missions::GetInstance()->currentLevel->existFile("floor0.png")
1872         && Missions::GetInstance()->currentLevel->existFile("floor1.png") ) {
1873     	floor0->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/floor0.png");
1874     	floor1->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/floor1.png");
1875     	floorOfLevel = true;
1876 
1877     } else {
1878     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_FLOOR")) {
1879     		typeFloor = t;
1880     	} else {
1881     		typeFloor = 0;
1882     	}
1883 
1884     	floor0->changeFrameFileFromData(0, "/board/floor", "png", typeFloor*2);
1885     	floor1->changeFrameFileFromData(0, "/board/floor", "png", (typeFloor*2)+1);
1886     	floorOfLevel = false;
1887     }
1888 }
1889 
setTypePavement(int t)1890 void Board::setTypePavement(int t) {
1891 
1892     if ( Missions::GetInstance()->currentLevel->existFile("pavement_bottom.png")
1893         && Missions::GetInstance()->currentLevel->existFile("pavement_top.png") ) {
1894 
1895     	pavementTop->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/pavement_top.png");
1896     	pavementBottom->changeFrameFile(0, Missions::GetInstance()->currentLevel->getPath()+"/pavement_bottom.png");
1897     	pavementOfLevel = true;
1898 
1899     } else {
1900     	if (t<BoardInfo::GetInstance()->getValueToInt("NTYPES_PAVEMENT")) {
1901     		typePavement = t;
1902     	} else {
1903     		typePavement = 0;
1904     	}
1905 
1906     	char tmp[128];
1907 
1908     	sprintf(tmp, "/board/pavement_%d_top.png", typePavement);
1909     	pavementTop->changeFrameFileFromData(0, tmp);
1910 
1911     	sprintf(tmp, "/board/pavement_%d_bottom.png", typePavement);
1912     	pavementBottom->changeFrameFileFromData(0, tmp);
1913 
1914     	pavementOfLevel = false;
1915 
1916     }
1917 }
1918 
getScaleByTypeSize()1919 float Board::getScaleByTypeSize() {
1920 	switch(typeSize) {
1921 		case BOARD_6X6:
1922 			return 1.0;
1923 		break;
1924 		case BOARD_7X7:
1925 			return 0.86;
1926 		break;
1927 		case BOARD_8X8:
1928 			return 0.76;
1929 		break;
1930 		case BOARD_9X9:
1931 			return 0.67;
1932 		break;
1933 		case BOARD_10X10:
1934 			return 0.61;
1935 		break;
1936 	}
1937 	return 1.0;
1938 }
1939 
setTypeSize(int t)1940 void Board::setTypeSize(int t) {
1941 	float s = 1.0;
1942 	typeSize = t;
1943 	switch(t) {
1944 		case BOARD_6X6:
1945 			pavementBottom->setY(179);
1946 			s = getScaleByTypeSize();
1947 			BoardScale::GetInstance()->setScale(s);
1948 			cols = 6;
1949 			rows = 6;
1950 		break;
1951 		case BOARD_7X7:
1952 			pavementBottom->setY(178);
1953 			s = getScaleByTypeSize();
1954 			BoardScale::GetInstance()->setScale(s);
1955 			cols = 7;
1956 			rows = 7;
1957 		break;
1958 		case BOARD_8X8:
1959 			pavementBottom->setY(182);
1960 			s = getScaleByTypeSize();
1961 			BoardScale::GetInstance()->setScale(s);
1962 			cols = 8;
1963 			rows = 8;
1964 		break;
1965 		case BOARD_9X9:
1966 			pavementBottom->setY(177);
1967 			s = getScaleByTypeSize();
1968 			BoardScale::GetInstance()->setScale(s);
1969 			cols = 9;
1970 			rows = 9;
1971 		break;
1972 		case BOARD_10X10:
1973 			pavementBottom->setY(178);
1974 			s = getScaleByTypeSize();
1975 			BoardScale::GetInstance()->setScale(s);
1976 			cols = 10;
1977 			rows = 10;
1978 		break;
1979 	}
1980 
1981 	floor0->setCurrentScale(s);
1982 	floor1->setCurrentScale(s);
1983 	trap->setCurrentScale(s);
1984 
1985 	particlesTrap->setCurrentScale(s);
1986 	particlesTrap->setScaleHeightElement(s);
1987 
1988 	scenary->scaleTiles(s);
1989 
1990 	wallRight->setCurrentScale(s);
1991 	wallRight->setScaleHeightElement(s);
1992 	wallRight->setScaleDisplacementX(s);
1993 
1994 	wallBottom->setCurrentScale(s);
1995 	wallBottom->setScaleHeightElement(s);
1996 	wallBottom->setScaleDisplacementX(s);
1997 
1998 	nimuh->setCurrentScale(s);
1999 	nimuh->setScaleHeightElement(s);
2000 
2001 	enemy1->setCurrentScale(s);
2002 	enemy1->setScaleHeightElement(s);
2003 
2004 	enemy2->setCurrentScale(s);
2005 	enemy2->setScaleHeightElement(s);
2006 
2007 	enemy3->setCurrentScale(s);
2008 	enemy3->setScaleHeightElement(s);
2009 
2010 	blackHole0->setCurrentScale(s);
2011 	blackHole0->setScaleHeightElement(s);
2012 
2013 	blackHole1->setCurrentScale(s);
2014 	blackHole1->setScaleHeightElement(s);
2015 
2016 	particlesBlackHole0->setCurrentScale(s);
2017 	particlesBlackHole0->setScaleHeightElement(s);
2018 
2019 	particlesBlackHole1->setCurrentScale(s);
2020 	particlesBlackHole1->setScaleHeightElement(s);
2021 
2022 	arrow->setCurrentScale(s);
2023 	arrow->setScaleHeightElement(s);
2024 
2025 	door->setCurrentScale(s);
2026 	door->setScaleHeightElement(s);
2027 
2028 	key->setCurrentScale(s);
2029 	key->setScaleHeightElement(s);
2030 
2031 	tray->setCurrentScale(s);
2032 	tray->setScaleHeightElement(s);
2033 
2034 	doorKeyBottom->setCurrentScale(s);
2035 	doorKeyBottom->setScaleHeightElement(s);
2036 
2037 	doorKeyRight->setCurrentScale(s);
2038 	doorKeyRight->setScaleHeightElement(s);
2039 
2040 	floorOn->setCurrentScale(s);
2041 	floorOn->setScaleHeightElement(s);
2042 
2043 	paddingExplosion = (int)(30*s);
2044 
2045 	MouseMapTile::GetInstance()->createMap();
2046 
2047 }
2048 
loadLevel()2049 void Board::loadLevel() {
2050 	clear();
2051 	parseScreenXML((char*)Missions::GetInstance()->pathXMLLevel().c_str());
2052 	addStateHistory();
2053     scenary->hide();
2054 }
2055 
clear()2056 void Board::clear() {
2057 
2058 	history.clear();
2059 	totalHistory = 0;
2060 	currentHistory = 0;
2061 
2062     scenary->clear();
2063 
2064 	wallsOfLevel = false;
2065 	terrainOfLevel = false;
2066 	wallScennaryOfLevel = false;
2067 	musicOfLevel = false;
2068 	pavementOfLevel = false;
2069 	floorOfLevel = false;
2070 	tiledOfLevel = false;
2071 
2072 	for (int i=0; i<MAX_TILES_INLINE*MAX_TILES_INLINE; i++) {
2073 		walls[i].wallBottom = false;
2074 		walls[i].wallRight = false;
2075 	}
2076 
2077 	floor0->setAlpha(0);
2078 	floor1->setAlpha(0);
2079 
2080 	pavementTop->setAlpha(0);
2081 	pavementBottom->setAlpha(0);
2082 
2083 	wallBottom->setAlpha(0);
2084 	wallRight->setAlpha(0);
2085 
2086 	nimuh->setForcePosition(0,0);
2087 	nimuh->clearHistoryPositions();
2088 	nimuh->stop();
2089 	nimuh->setForceAnimation("stop_right");
2090 	nimuh->removeTypeMovement();
2091 	nimuh->removeScale();
2092 	nimuh->removeRotation();
2093 	nimuh->removeAlpha();
2094 
2095 	nimuh->hide();
2096 
2097 	enemy1->setForcePosition(0,0);
2098 	enemy1->clearHistoryPositions();
2099 	enemy1->stop();
2100 	enemy1->setForceAnimation("stop_right");
2101 	enemy1->removeTypeMovement();
2102 	enemy1->setStopped(false);
2103 
2104 	enemy1->hide();
2105 
2106 	enemy2->setForcePosition(0,0);
2107 	enemy2->clearHistoryPositions();
2108 	enemy2->stop();
2109 	enemy2->setForceAnimation("stop_right");
2110 	enemy2->removeTypeMovement();
2111 	enemy2->setStopped(false);
2112 
2113 	enemy2->hide();
2114 
2115 	enemy3->setForcePosition(0,0);
2116 	enemy3->clearHistoryPositions();
2117 	enemy3->stop();
2118 	enemy3->setForceAnimation("stop_right");
2119 	enemy3->removeTypeMovement();
2120 	enemy3->setStopped(false);
2121 
2122 	enemy3->hide();
2123 
2124 	hasEnemy1 = false;
2125 	canChangeInHistory_Enemy1 = false;
2126 	hasEnemy2 = false;
2127 	canChangeInHistory_Enemy2 = false;
2128 	hasEnemy3 = false;
2129 	canChangeInHistory_Enemy3 = false;
2130 
2131 	hasBlackHole0 = false;
2132 	canChangeInHistory_BlackHole0 = false;
2133 	hasBlackHole1 = false;
2134 	canChangeInHistory_BlackHole1 = false;
2135 
2136 	blackHole0->hide();
2137 	blackHole1->hide();
2138 	particlesBlackHole0->hide();
2139 	particlesBlackHole1->hide();
2140 
2141 	hasKey = false;
2142 	canChangeInHistory_Key = false;
2143 	directionKey = "right";
2144 	doorKeyClosed = true;
2145 
2146 	key->hide();
2147 	doorKeyRight->hide();
2148 	doorKeyBottom->hide();
2149 
2150 	doorKeyRight->setForceAnimation("closed");
2151 	doorKeyBottom->setForceAnimation("closed");
2152 
2153 	tray->removeScale();
2154 	tray->removeTypeMovement();
2155 	tray->removeRotation();
2156 	tray->removeAlpha();
2157 	tray->hide();
2158 
2159 	hasTray = false;
2160 	canChangeInHistory_Tray = false;
2161 
2162 	hasTrap = false;
2163 
2164 	todoNextTurn = false;
2165 
2166 	inExplosion = false;
2167 
2168 	turn = TURN_PLAYER;
2169 
2170 	beginAnimationWaiting();
2171 
2172 }
2173 
swapDoorKey()2174 void Board::swapDoorKey() {
2175 	if (hasKey) {
2176 		if (doorKeyClosed) {
2177 			doorKeyBottom->setForceAnimation("open", 1);
2178 			doorKeyRight->setForceAnimation("open", 1);
2179 		} else {
2180 			doorKeyBottom->setForceAnimation("close", 1);
2181 			doorKeyRight->setForceAnimation("close", 1);
2182 		}
2183 		Sounds::GetInstance()->getSound("doorkey")->play(3, 0);
2184 		pause(800);
2185 		doorKeyClosed = !doorKeyClosed;
2186 	}
2187 }
2188 
getPositionPlayer()2189 BoardPosition Board::getPositionPlayer() {
2190 	return nimuh->getPosition();
2191 }
2192 
getPositionEnemy1()2193 BoardPosition Board::getPositionEnemy1() {
2194 	return enemy1->getPosition();
2195 }
2196 
getPositionEnemy2()2197 BoardPosition Board::getPositionEnemy2() {
2198 	return enemy2->getPosition();
2199 }
2200 
getPositionEnemy3()2201 BoardPosition Board::getPositionEnemy3() {
2202 	return enemy3->getPosition();
2203 }
2204 
setBlackHole0(int x,int y)2205 void Board::setBlackHole0(int x, int y) {
2206 	BoardPosition p;
2207 	p.x = x;
2208 	p.y = y;
2209 	p.out = !positionIsOnBoard(p);
2210 	blackHole0->setForcePosition(p);
2211 	particlesBlackHole0->setInitialPosition(p);
2212 	hasBlackHole0 = true;
2213 	canChangeInHistory_BlackHole0 = true;
2214 }
2215 
setBlackHole1(int x,int y)2216 void Board::setBlackHole1(int x, int y) {
2217 	BoardPosition p;
2218 	p.x = x;
2219 	p.y = y;
2220 	p.out = !positionIsOnBoard(p);
2221 	blackHole1->setForcePosition(p);
2222 	particlesBlackHole1->setInitialPosition(p);
2223 	hasBlackHole1 = true;
2224 	canChangeInHistory_BlackHole1 = true;
2225 }
2226 
setKey(int x,int y)2227 void Board::setKey(int x, int y) {
2228 	BoardPosition p;
2229 	p.x = x;
2230 	p.y = y;
2231 	p.out = !positionIsOnBoard(p);
2232 	key->setInitialPosition(p);
2233 	hasKey = true;
2234 	canChangeInHistory_Key = true;
2235 }
2236 
setDoorKey(int x,int y,string dk)2237 void Board::setDoorKey(int x, int y, string dk) {
2238 	BoardPosition p;
2239 	p.x = x;
2240 	p.y = y;
2241 	p.out = !positionIsOnBoard(p);
2242 	doorKeyBottom->setInitialPosition(p);
2243 	doorKeyRight->setInitialPosition(p);
2244 	directionKey = dk;
2245 	hasKey = true;
2246 	canChangeInHistory_Key = true;
2247 }
2248 
setPositionPlayer(int x,int y)2249 void Board::setPositionPlayer(int x, int y) {
2250 	BoardPosition p;
2251 	p.x = x;
2252 	p.y = y;
2253 	p.out = !positionIsOnBoard(p);
2254 	nimuh->setInitialPosition(p);
2255 }
2256 
setPositionEnemy1(int x,int y)2257 void Board::setPositionEnemy1(int x, int y) {
2258 	BoardPosition p;
2259 	p.x = x;
2260 	p.y = y;
2261 	p.out = !positionIsOnBoard(p);
2262 	enemy1->setInitialPosition(p);
2263 	hasEnemy1 = true;
2264 	canChangeInHistory_Enemy1 = true;
2265 }
2266 
setPositionEnemy2(int x,int y)2267 void Board::setPositionEnemy2(int x, int y) {
2268 	BoardPosition p;
2269 	p.x = x;
2270 	p.y = y;
2271 	p.out = !positionIsOnBoard(p);
2272 	enemy2->setInitialPosition(p);
2273 	hasEnemy2 = true;
2274 	canChangeInHistory_Enemy2 = true;
2275 }
2276 
setPositionEnemy3(int x,int y)2277 void Board::setPositionEnemy3(int x, int y) {
2278 	BoardPosition p;
2279 	p.x = x;
2280 	p.y = y;
2281 	p.out = !positionIsOnBoard(p);
2282 	enemy3->setInitialPosition(p);
2283 	hasEnemy3 = true;
2284 	canChangeInHistory_Enemy3 = true;
2285 }
2286 
setPositionExit(int x,int y)2287 void Board::setPositionExit(int x, int y) {
2288 	BoardPosition p;
2289 	p.x = x;
2290 	p.y = y;
2291 	p.out = !positionIsOnBoard(p);
2292 	door->setInitialPosition(p);
2293 }
2294 
setPositionTrap(int x,int y)2295 void Board::setPositionTrap(int x, int y) {
2296 	posTrap.x = x;
2297 	posTrap.y = y;
2298 	posTrap.out = !positionIsOnBoard(posTrap);
2299 	particlesTrap->setInitialPosition(posTrap);
2300 	hasTrap = true;
2301 }
2302 
setPositionTray(int x,int y)2303 void Board::setPositionTray(int x, int y) {
2304 	BoardPosition p;
2305 	p.x = x;
2306 	p.y = y;
2307 	p.out = !positionIsOnBoard(p);
2308 	tray->setInitialPosition(p);
2309 	hasTray = true;
2310 	canChangeInHistory_Tray = true;
2311 }
2312 
getPositionMouse()2313 BoardPosition Board::getPositionMouse() {
2314 
2315 	int w = BoardScale::GetInstance()->getWTile();
2316 	int h = BoardScale::GetInstance()->getHTile();
2317 
2318 	int mouse_x, mouse_y;
2319 	SDL_PumpEvents();
2320 	SDL_GetMouseState(&mouse_x, &mouse_y);
2321 
2322 	// impares con una columna menos
2323 	int ncol = getCols() - (getCols()%2);
2324 
2325 	int x = ( ( mouse_x - ( (int)getX() - (w/2) - ((w/2)*ncol) ) ) / w ) - ncol/2;
2326 
2327 	int y = ( mouse_y - (int)getY() ) / h;
2328 
2329 	int off_x = ( ( mouse_x - ( (int)getX() - (w/2) - ((w/2)*ncol) ) ) % w );
2330 
2331 	int off_y = ( mouse_y - (int)getY() ) % h;
2332 
2333 	int off = MouseMapTile::GetInstance()->getPosMouse(off_x,off_y);
2334 
2335 	int x0 = x+y;
2336 
2337 	int y0;
2338 	if (x==y+1) {
2339 		y0 = -1;
2340 	} else if (x>y+1) {
2341 		y0 = -2;
2342 	} else {
2343 		y0 = abs(x-y);
2344 	}
2345 
2346 	if (off==TILE_RIGHT) {
2347 		x0++;
2348 	} else if (off==TILE_LEFT) {
2349     	if ( (x0==0) && (y0==0) ) {
2350         	BoardPosition p;
2351         	p.x = -1;
2352         	p.y = -1;
2353         	p.out = true;
2354         	return p;
2355         }
2356 		x0--;
2357 	} else if (off==TILE_UP) {
2358     	if ( (x0==0) && (y0==0) ) {
2359         	BoardPosition p;
2360         	p.x = -1;
2361         	p.y = -1;
2362         	p.out = true;
2363         	return p;
2364         }
2365 		y0--;
2366 	} else if (off==TILE_DOWN) {
2367 		y0++;
2368 	}
2369 
2370 	BoardPosition p;
2371 	p.x = x0;
2372 	p.y = y0;
2373 	p.out = !positionIsOnBoard(p);
2374 
2375 	return p;
2376 }
2377 
positionIsOnBoard(BoardPosition p)2378 bool Board::positionIsOnBoard(BoardPosition p) {
2379 	if ( (p.x<0) || (p.x>=getRows()) ) {
2380 		return false;
2381 	}
2382 	if ( (p.y<0) || (p.y>=getCols()) ) {
2383 		return false;
2384 	}
2385 	return true;
2386 }
2387 
hasWallBottom(int x,int y,bool verifyDoorKey)2388 bool Board::hasWallBottom(int x, int y, bool verifyDoorKey) {
2389 	if (positionIsOnBoard(getPositionFromXY(x,y))) {
2390 		if (verifyDoorKey && hasKey && doorKeyClosed && directionKey != "right") {
2391 			if (verifyPositions(getPositionFromXY(x,y), doorKeyBottom->getPosition())) {
2392 				return true;
2393 			}
2394 		}
2395 		return walls[(y*MAX_TILES_INLINE)+x].wallBottom;
2396 	} else {
2397 		return false;
2398 	}
2399 }
2400 
hasWallRight(int x,int y,bool verifyDoorKey)2401 bool Board::hasWallRight(int x, int y, bool verifyDoorKey) {
2402 	if (positionIsOnBoard(getPositionFromXY(x,y))) {
2403 		if (verifyDoorKey && hasKey && doorKeyClosed && directionKey == "right") {
2404 			if (verifyPositions(getPositionFromXY(x,y), doorKeyRight->getPosition())) {
2405 				return true;
2406 			}
2407 		}
2408 		return walls[(y*MAX_TILES_INLINE)+x].wallRight;
2409 	} else {
2410 		return false;
2411 	}
2412 }
2413 
hasWallBetweenCells(BoardPosition p1,BoardPosition p2,bool verifyDoorKey)2414 bool Board::hasWallBetweenCells(BoardPosition p1, BoardPosition p2, bool verifyDoorKey) {
2415      if (p1.x+1 == p2.x) { // right
2416          return hasWallRight(p1.x, p1.y, verifyDoorKey);
2417      } else if (p1.x-1 == p2.x) { // left
2418          return hasWallRight(p2.x, p2.y, verifyDoorKey);
2419      } else if (p1.y+1 == p2.y) { // down
2420          return hasWallBottom(p1.x, p1.y, verifyDoorKey);
2421      } else if (p1.y-1 == p2.y) { // up
2422          return hasWallBottom(p2.x, p2.y, verifyDoorKey);
2423      }
2424      return false;
2425 }
2426 
load()2427 void Board::load() {
2428 
2429 	multiLanguage = false;
2430 
2431 	wallsOfLevel = false;
2432 	terrainOfLevel = false;
2433 	wallScennaryOfLevel = false;
2434 	musicOfLevel = false;
2435 	pavementOfLevel = false;
2436 	floorOfLevel = false;
2437 
2438 	town.name="";
2439 	town.province="";
2440 	town.nickname="";
2441 
2442 	infoTray.title="";
2443 	infoTray.text="";
2444 
2445 	floorShadow = new Element();
2446 	floorShadow->setY(-50);
2447     floorShadow->setAlign(ALIGN_CENTER);
2448 	floorShadow->addFrameFileFromData("/board/floor_shadow.png");
2449 	floorShadow->show();
2450     floorShadow->setGroup(this);
2451 
2452 	floor0 = new Element();
2453     floor0->setAlign(ALIGN_CENTER);
2454 	floor0->addFrameFileFromData("/board/floor_0000.png");
2455 	floor0->show();
2456     floor0->setGroup(this);
2457 
2458 	floor1 = new Element();
2459     floor1->setAlign(ALIGN_CENTER);
2460 	floor1->addFrameFileFromData("/board/floor_0001.png");
2461 	floor1->show();
2462     floor1->setGroup(this);
2463 
2464     pavementTop = new Element();
2465     pavementTop->setAlign(ALIGN_CENTER);
2466     pavementTop->setVAlign(VALIGN_BOTTOM);
2467     pavementTop->setY(186);
2468 	pavementTop->addFrameFileFromData("/board/pavement_0_top.png");
2469 	pavementTop->show();
2470     pavementTop->setGroup(this);
2471 
2472     pavementBottom = new Element();
2473     pavementBottom->setAlign(ALIGN_CENTER);
2474     pavementBottom->setY(180);
2475 	pavementBottom->addFrameFileFromData("/board/pavement_0_bottom.png");
2476 	pavementBottom->show();
2477     pavementBottom->setGroup(this);
2478 
2479 	trap = new AnimatedElement();
2480     trap->setAlign(ALIGN_CENTER);
2481 	trap->addRangeFramesFromData("board/trap", "png", 0, 3);
2482     trap->setGroup(this);
2483 
2484 	Animation atrap;
2485 	atrap.setName("move");
2486 	atrap.setCyclesBetweenFrames(20);
2487 	atrap.addRangeFrame(0, 3, 1);
2488 	atrap.addFrame(2);
2489 	atrap.addFrame(1);
2490 	trap->addAnimation(atrap);
2491 
2492 	particlesTrap = new ElementIsometric();
2493 	particlesTrap->setHeightElement(40);
2494 	particlesTrap->setAlign(ALIGN_CENTER);
2495 
2496 	particlesTrap->addRangeFramesFromData("objects/trap_particles", "png", 0, 3);
2497 
2498 	particlesTrap->setGroup(this);
2499 
2500 	Animation p_trap;
2501 	p_trap.setName("move");
2502 	p_trap.setCyclesBetweenFrames(8);
2503 	p_trap.addFrame(0);
2504 	p_trap.addFrame(1);
2505 	p_trap.addFrame(2);
2506 	p_trap.addFrame(3);
2507 	p_trap.addFrame(1);
2508 	p_trap.addFrame(2);
2509 	p_trap.addFrame(1);
2510 	particlesTrap->addAnimation(p_trap);
2511 
2512 	TypeObjectsScenary::GetInstance()->load();
2513 
2514 	explosionEnemies = new ParticlesSystem(100);
2515 	explosionEnemies->setFrame((string)DATA_DIR + "/particles/smoke.png");
2516 	explosionEnemies->setMlLife(500, 700);
2517 
2518 	blackHole0 = new ElementIsometric();
2519 	blackHole0->setHeightElement(2);
2520 	blackHole0->setAlign(ALIGN_CENTER);
2521 
2522 	blackHole0->addRangeFramesFromData("objects/blackhole", "png", 0, 3);
2523 
2524 	blackHole1 = new ElementIsometric();
2525 	blackHole1->setHeightElement(2);
2526 	blackHole1->setAlign(ALIGN_CENTER);
2527 
2528 	blackHole1->addRangeFramesFromData("objects/blackhole", "png", 0, 3);
2529 
2530 	Animation bh_a;
2531 	bh_a.setName("move");
2532 	bh_a.addRangeFrame(0, 3, 1);
2533 	blackHole0->addAnimation(bh_a);
2534 	blackHole1->addAnimation(bh_a);
2535 
2536 	blackHole0->setGroup(this);
2537 	blackHole1->setGroup(this);
2538 
2539 	particlesBlackHole0 = new ElementIsometric();
2540 	particlesBlackHole0->setHeightElement(40);
2541 	particlesBlackHole0->setAlign(ALIGN_CENTER);
2542 
2543 	particlesBlackHole0->addRangeFramesFromData("objects/blackhole_particles", "png", 0, 3);
2544 
2545 	particlesBlackHole1 = new ElementIsometric();
2546 	particlesBlackHole1->setHeightElement(50);
2547 	particlesBlackHole1->setAlign(ALIGN_CENTER);
2548 
2549 	particlesBlackHole1->addRangeFramesFromData("objects/blackhole_particles", "png", 0, 3);
2550 
2551 	Animation pbh_a;
2552 	pbh_a.setName("move");
2553 	pbh_a.setCyclesBetweenFrames(8);
2554 	pbh_a.addFrame(0);
2555 	pbh_a.addFrame(1);
2556 	pbh_a.addFrame(2);
2557 	pbh_a.addFrame(3);
2558 	pbh_a.addFrame(1);
2559 	pbh_a.addFrame(2);
2560 	pbh_a.addFrame(1);
2561 	particlesBlackHole0->addAnimation(pbh_a);
2562 
2563 	Animation pbh_a1;
2564 	pbh_a1.setName("move");
2565 	pbh_a1.setCyclesBetweenFrames(7);
2566 	pbh_a1.addFrame(0);
2567 	pbh_a1.addFrame(1);
2568 	pbh_a1.addFrame(2);
2569 	pbh_a1.addFrame(3);
2570 	pbh_a1.addFrame(2);
2571 	pbh_a1.addFrame(1);
2572 	particlesBlackHole1->addAnimation(pbh_a);
2573 
2574 	particlesBlackHole0->setGroup(this);
2575 	particlesBlackHole1->setGroup(this);
2576 
2577 	doorKeyBottom = new ElementIsometric();
2578 	doorKeyBottom->addRangeFramesFromData("objects/grate", "png", 0, 4);
2579 	doorKeyBottom->setAlign(ALIGN_RIGHT);
2580 	doorKeyBottom->setHeightElement(60);
2581 	doorKeyBottom->setDisplacementX(8);
2582 	doorKeyBottom->setGroup(this);
2583 
2584 	doorKeyRight = new ElementIsometric();
2585 	doorKeyRight->setFlip(true);
2586 	doorKeyRight->addRangeFramesFromData("objects/grate", "png", 0, 4);
2587 	doorKeyRight->setHeightElement(60);
2588 	doorKeyRight->setDisplacementX(-8);
2589 	doorKeyRight->setGroup(this);
2590 
2591 	Animation dk_a;
2592 	dk_a.setName("open");
2593 	dk_a.addRangeFrame(0, 4, 1);
2594 	doorKeyRight->addAnimation(dk_a);
2595 	doorKeyBottom->addAnimation(dk_a);
2596 
2597 	Animation dk_b;
2598 	dk_b.setName("close");
2599 	dk_b.addRangeFrame(4, 0, -1);
2600 	doorKeyRight->addAnimation(dk_b);
2601 	doorKeyBottom->addAnimation(dk_b);
2602 
2603 	Animation dk_c;
2604 	dk_c.setName("opened");
2605 	dk_c.addFrame(4);
2606 	doorKeyRight->addAnimation(dk_c);
2607 	doorKeyBottom->addAnimation(dk_c);
2608 
2609 	Animation dk_d;
2610 	dk_d.setName("closed");
2611 	dk_d.addFrame(0);
2612 	doorKeyRight->addAnimation(dk_d);
2613 	doorKeyBottom->addAnimation(dk_d);
2614 
2615 	doorKeyRight->setAnimation("closed");
2616 	doorKeyBottom->setAnimation("closed");
2617 
2618 	key = new ElementIsometric();
2619 	key->setHeightElement(20);
2620 	key->setAlign(ALIGN_CENTER);
2621 
2622 	key->addFrameFileFromData("/objects/key.png");
2623 
2624 	key->setGroup(this);
2625 
2626 	tray = new ElementIsometric();
2627 	tray->setForceRotateCenter(true);
2628 	tray->setHeightElement(20);
2629 	tray->setAlign(ALIGN_CENTER);
2630 
2631 	tray->addFrameFileFromData("/objects/tray.png");
2632 
2633 	tray->setGroup(this);
2634 
2635 	scenary = new Scenary();
2636 	scenary->setAlpha(0);
2637 
2638 	door = new Door();
2639 	door->setGroup(this);
2640 
2641 	wallBottom = new ElementIsometric();
2642 	wallBottom->addFrameFileFromData("/board/wall_0_h.png");
2643 	wallBottom->setAlign(ALIGN_RIGHT);
2644 	wallBottom->setHeightElement(25);
2645 	wallBottom->setDisplacementX(10);
2646 	wallBottom->setGroup(this);
2647 
2648 	wallRight = new ElementIsometric();
2649 	wallRight->addFrameFileFromData("/board/wall_0_v.png");
2650 	wallRight->setHeightElement(25);
2651 	wallRight->setDisplacementX(-10);
2652 	wallRight->setGroup(this);
2653 
2654 	floorOn = new ElementIsometric();
2655 	floorOn->setHeightElement(0);
2656 	floorOn->setForcePosition(0,0);
2657 	floorOn->setAlign(ALIGN_CENTER);
2658 	floorOn->addFrameFileFromData("/editgame/floor_on.png");
2659 	floorOn->setGroup(this);
2660 
2661 	// arrow
2662 
2663 	arrow = new ElementIsometric();
2664 	arrow->addFrameFileFromData("/board/circle.png");
2665 	arrow->addFrameFileFromData("/board/arrow_l.png");
2666 	arrow->addFrameFileFromData("/board/arrow_u.png");
2667 	arrow->addFrameFileFromData("/board/arrow_d.png");
2668 	arrow->addFrameFileFromData("/board/arrow_r.png");
2669 
2670 	Animation ar_a;
2671 	ar_a.setName("center");
2672 	ar_a.addFrame(0);
2673 	arrow->addAnimation(ar_a);
2674 	Animation ar_b;
2675 	ar_b.setName("left");
2676 	ar_b.addFrame(1);
2677 	arrow->addAnimation(ar_b);
2678 	Animation ar_c;
2679 	ar_c.setName("up");
2680 	ar_c.addFrame(2);
2681 	arrow->addAnimation(ar_c);
2682 	Animation ar_d;
2683 	ar_d.setName("down");
2684 	ar_d.addFrame(3);
2685 	arrow->addAnimation(ar_d);
2686 	Animation ar_e;
2687 	ar_e.setName("right");
2688 	ar_e.addFrame(4);
2689 	arrow->addAnimation(ar_e);
2690 
2691 	arrow->setAlign(ALIGN_CENTER);
2692 	arrow->setHeightElement(-5);
2693 	arrow->setGroup(this);
2694 
2695 	// objects and people
2696 
2697 	nimuh = new Player();
2698 	nimuh->setForceRotateCenter(true);
2699 	nimuh->setHeightElement(120);
2700 	nimuh->setAlign(ALIGN_CENTER);
2701 
2702 	nimuh->addRangeFramesFromData("nimuh/nimuh", "png", 0, 27);
2703 
2704 	nimuh->addRangeFramesFromData("nimuh/jail", "png", 0, 3);
2705 
2706 	nimuh->addRangeFramesFromData("nimuh/clean0", "png", 0, 5);
2707 	nimuh->addRangeFramesFromData("nimuh/clean1", "png", 0, 1);
2708 
2709 	nimuh->addRangeFramesFromData("nimuh/greeting", "png", 0, 4);
2710 
2711 	nimuh->addRangeFramesFromData("nimuh/scratch0", "png", 0, 1);
2712 	nimuh->addRangeFramesFromData("nimuh/scratch1", "png", 0, 2);
2713 
2714 	Animation jail;
2715 	jail.setName("jail");
2716 	jail.addRangeFrame(28, 31, 1);
2717 	nimuh->addAnimation(jail);
2718 
2719 	Animation clean0;
2720 	clean0.setCyclesBetweenFrames(8);
2721 	clean0.setName("clean0");
2722 	clean0.addRangeFrame(32, 37, 1);
2723 	nimuh->addAnimation(clean0);
2724 
2725 	Animation cleanremove;
2726 	cleanremove.setCyclesBetweenFrames(8);
2727 	cleanremove.setName("cleanremove");
2728 	cleanremove.addRangeFrame(37, 32, -1);
2729 	nimuh->addAnimation(cleanremove);
2730 
2731 	Animation clean1;
2732 	clean1.setCyclesBetweenFrames(15);
2733 	clean1.setName("clean1");
2734 	clean1.addRangeFrame(38, 39, 1);
2735 	nimuh->addAnimation(clean1);
2736 
2737 	Animation greeting;
2738 	greeting.setName("greeting");
2739 	greeting.addRangeFrame(40, 44, 1);
2740 	nimuh->addAnimation(greeting);
2741 
2742 	Animation greetingremove;
2743 	greetingremove.setName("greetingremove");
2744 	greetingremove.addRangeFrame(44, 40, -1);
2745 	nimuh->addAnimation(greetingremove);
2746 
2747 	Animation scratch0;
2748 	scratch0.setName("scratch0");
2749 	scratch0.addRangeFrame(45, 46, 1);
2750 	nimuh->addAnimation(scratch0);
2751 
2752 	Animation scratchremove;
2753 	scratchremove.setName("scratchremove");
2754 	scratchremove.addRangeFrame(46, 45, -1);
2755 	nimuh->addAnimation(scratchremove);
2756 
2757 	Animation scratch1;
2758 	scratch1.setName("scratch1");
2759 	scratch1.addRangeFrame(47, 49, 1);
2760 	nimuh->addAnimation(scratch1);
2761 
2762 	enemy1 = new Enemy();
2763 	enemy1->setHeightElement(110);
2764 	enemy1->setAlign(ALIGN_CENTER);
2765 
2766 	enemy1->addRangeFramesFromData("enemy1/enemy1", "png", 0, 27);
2767 
2768 	enemy1->addRangeFramesFromData("enemy1/eating", "png", 0, 3);
2769 
2770 	enemy1->addRangeFramesFromData("enemy1/crying", "png", 0, 2);
2771 
2772 	enemy1->addRangeFramesFromData("enemy1/impatient", "png", 0, 3);
2773 
2774 	enemy1->addRangeFramesFromData("enemy1/proud0", "png", 0, 3);
2775 	enemy1->addRangeFramesFromData("enemy1/proud1", "png", 0, 5);
2776 
2777 	Animation e1_impatient;
2778 	e1_impatient.setName("impatient");
2779 	e1_impatient.addRangeFrame(35, 38, 1);
2780 	enemy1->addAnimation(e1_impatient);
2781 
2782 	Animation e1_proud0;
2783 	e1_proud0.setName("proud0");
2784 	e1_proud0.addRangeFrame(39, 42, 1);
2785 	enemy1->addAnimation(e1_proud0);
2786 
2787 	Animation e1_proudremove;
2788 	e1_proudremove.setName("proudremove");
2789 	e1_proudremove.addRangeFrame(42, 39, -1);
2790 	enemy1->addAnimation(e1_proudremove);
2791 
2792 	Animation e1_proud1;
2793 	e1_proud1.setName("proud1");
2794 	e1_proud1.addRangeFrame(43, 48, 1);
2795 	enemy1->addAnimation(e1_proud1);
2796 
2797 	enemy2 = new Enemy();
2798 	enemy2->setFirstMovementHorizontal(false);
2799 	enemy2->setHeightElement(100);
2800 	enemy2->setAlign(ALIGN_CENTER);
2801 
2802 	enemy2->addRangeFramesFromData("enemy2/enemy2", "png", 0, 27);
2803 
2804 	enemy2->addRangeFramesFromData("enemy2/eating", "png", 0, 3);
2805 
2806 	enemy2->addRangeFramesFromData("enemy2/crying", "png", 0, 2);
2807 
2808 	enemy2->addRangeFramesFromData("enemy2/cazzo0", "png", 0, 1);
2809 	enemy2->addRangeFramesFromData("enemy2/cazzo1", "png", 0, 2);
2810 
2811 	enemy2->addRangeFramesFromData("enemy2/peeing", "png", 0, 12);
2812 
2813 	Animation eating;
2814 	eating.setCyclesBetweenFrames(15);
2815 	eating.setName("eating");
2816 	eating.addRangeFrame(28, 31, 1);
2817 	enemy1->addAnimation(eating);
2818 	enemy2->addAnimation(eating);
2819 
2820 	Animation crying;
2821 	crying.setCyclesBetweenFrames(15);
2822 	crying.setName("crying");
2823 	crying.addRangeFrame(32, 34, 1);
2824 	crying.addFrame(33);
2825 	enemy1->addAnimation(crying);
2826 	enemy2->addAnimation(crying);
2827 
2828 	Animation e2_cazzo0;
2829 	e2_cazzo0.setCyclesBetweenFrames(6);
2830 	e2_cazzo0.setName("cazzo0");
2831 	e2_cazzo0.addRangeFrame(35, 36, 1);
2832 	enemy2->addAnimation(e2_cazzo0);
2833 
2834 	Animation e2_cazzoremove;
2835 	e2_cazzoremove.setCyclesBetweenFrames(6);
2836 	e2_cazzoremove.setName("cazzoremove");
2837 	e2_cazzoremove.addRangeFrame(36, 35, -1);
2838 	enemy2->addAnimation(e2_cazzoremove);
2839 
2840 	Animation e2_cazzo1;
2841 	e2_cazzo1.setCyclesBetweenFrames(6);
2842 	e2_cazzo1.setName("cazzo1");
2843 	e2_cazzo1.addRangeFrame(37, 39, 1);
2844 	enemy2->addAnimation(e2_cazzo1);
2845 
2846 	Animation e2_peeing;
2847 	e2_peeing.setCyclesBetweenFrames(8);
2848 	e2_peeing.setName("peeing");
2849 	e2_peeing.addRangeFrame(40, 43, 1);
2850 	e2_peeing.addRangeFrame(42, 43, 1);
2851 	e2_peeing.addRangeFrame(42, 43, 1);
2852 	e2_peeing.addRangeFrame(42, 43, 1);
2853 	e2_peeing.addRangeFrame(44, 45, 1);
2854 	e2_peeing.addFrame(48);
2855 	e2_peeing.addRangeFrame(46, 47, 1);
2856 	e2_peeing.addRangeFrame(46, 47, 1);
2857 	e2_peeing.addRangeFrame(46, 47, 1);
2858 	e2_peeing.addRangeFrame(46, 47, 1);
2859 	e2_peeing.addRangeFrame(46, 47, 1);
2860 	e2_peeing.addFrame(49);
2861 	e2_peeing.addRangeFrame(48, 52, 1);
2862 	enemy2->addAnimation(e2_peeing);
2863 
2864 	Animation a;
2865 	a.setName("stop_right");
2866 	a.addFrame(12);
2867 	nimuh->addAnimation(a);
2868 	enemy1->addAnimation(a);
2869 	enemy2->addAnimation(a);
2870 
2871 	Animation b;
2872 	b.setName("stop_left");
2873 	b.addFrame(6);
2874 	nimuh->addAnimation(b);
2875 	enemy1->addAnimation(b);
2876 	enemy2->addAnimation(b);
2877 
2878 	Animation c;
2879 	c.setName("stop_up");
2880 	c.addFrame(0);
2881 	nimuh->addAnimation(c);
2882 	enemy1->addAnimation(c);
2883 	enemy2->addAnimation(c);
2884 
2885 	Animation d;
2886 	d.setName("stop_down");
2887 	d.addFrame(18);
2888 	nimuh->addAnimation(d);
2889 	enemy1->addAnimation(d);
2890 	enemy2->addAnimation(d);
2891 
2892 	Animation e;
2893 	e.setName("walk_right");
2894 	e.addRangeFrame(12, 17, 1);
2895 	nimuh->addAnimation(e);
2896 	enemy1->addAnimation(e);
2897 	enemy2->addAnimation(e);
2898 
2899 	Animation f;
2900 	f.setName("walk_left");
2901 	f.addRangeFrame(6, 11, 1);
2902 	nimuh->addAnimation(f);
2903 	enemy1->addAnimation(f);
2904 	enemy2->addAnimation(f);
2905 
2906 	Animation g;
2907 	g.setName("walk_up");
2908 	g.addRangeFrame(0, 5, 1);
2909 	nimuh->addAnimation(g);
2910 	enemy1->addAnimation(g);
2911 	enemy2->addAnimation(g);
2912 
2913 	Animation h;
2914 	h.setName("walk_down");
2915 	h.addRangeFrame(18, 23, 1);
2916 	nimuh->addAnimation(h);
2917 	enemy1->addAnimation(h);
2918 	enemy2->addAnimation(h);
2919 
2920 	Animation k;
2921 	k.setName("captured_up");
2922 	k.addFrame(24);
2923 	nimuh->addAnimation(k);
2924 	enemy1->addAnimation(k);
2925 	enemy2->addAnimation(k);
2926 
2927 	Animation m;
2928 	m.setName("captured_left");
2929 	m.addFrame(25);
2930 	nimuh->addAnimation(m);
2931 	enemy1->addAnimation(m);
2932 	enemy2->addAnimation(m);
2933 
2934 	Animation n;
2935 	n.setName("captured_right");
2936 	n.addFrame(26);
2937 	nimuh->addAnimation(n);
2938 	enemy1->addAnimation(n);
2939 	enemy2->addAnimation(n);
2940 
2941 	Animation o;
2942 	o.setName("captured_down");
2943 	o.addFrame(27);
2944 	nimuh->addAnimation(o);
2945 	enemy1->addAnimation(o);
2946 	enemy2->addAnimation(o);
2947 
2948 	nimuh->setDirection(TILE_RIGHT);
2949 	nimuh->setGroup(this);
2950 
2951 	enemy1->setDirection(TILE_RIGHT);
2952 	enemy1->setGroup(this);
2953 
2954 	enemy2->setDirection(TILE_RIGHT);
2955 	enemy2->setGroup(this);
2956 
2957 	enemy3 = new Enemy();
2958 	enemy3->setHeightElement(110);
2959 	enemy3->setAlign(ALIGN_CENTER);
2960 
2961 	enemy3->addRangeFramesFromData("enemy3/enemy3", "png", 0, 11);
2962 
2963 	enemy3->addRangeFramesFromData("enemy3/eating", "png", 0, 3);
2964 
2965 	enemy3->addRangeFramesFromData("enemy3/crying", "png", 0, 3);
2966 
2967 	Animation eating3;
2968 	eating3.setCyclesBetweenFrames(15);
2969 	eating3.setName("eating");
2970 	eating3.addRangeFrame(12, 15, 1);
2971 	enemy3->addAnimation(eating3);
2972 
2973 	Animation crying3;
2974 	crying3.setCyclesBetweenFrames(15);
2975 	crying3.setName("crying");
2976 	crying3.addRangeFrame(16, 19, 1);
2977 	enemy3->addAnimation(crying3);
2978 
2979 	Animation a3;
2980 	a3.setName("stop_right");
2981 	a3.addFrame(4);
2982 	enemy3->addAnimation(a3);
2983 
2984 	Animation b3;
2985 	b3.setName("stop_left");
2986 	b3.addFrame(2);
2987 	enemy3->addAnimation(b3);
2988 
2989 	Animation c3;
2990 	c3.setName("stop_up");
2991 	c3.addFrame(0);
2992 	enemy2->addAnimation(c3);
2993 
2994 	Animation d3;
2995 	d3.setName("stop_down");
2996 	d3.addFrame(6);
2997 	enemy3->addAnimation(d3);
2998 
2999 	Animation e3;
3000 	e3.setName("walk_right");
3001 	e3.addRangeFrame(4, 5, 1);
3002 	enemy3->addAnimation(e3);
3003 
3004 	Animation f3;
3005 	f3.setName("walk_left");
3006 	f3.addRangeFrame(2, 3, 1);
3007 	enemy3->addAnimation(f3);
3008 
3009 	Animation g3;
3010 	g3.setName("walk_up");
3011 	g3.addRangeFrame(0, 1, 1);
3012 	enemy3->addAnimation(g3);
3013 
3014 	Animation h3;
3015 	h3.setName("walk_down");
3016 	h3.addRangeFrame(6, 7, 1);
3017 	enemy3->addAnimation(h3);
3018 
3019 	Animation k3;
3020 	k3.setName("captured_up");
3021 	k3.addFrame(8);
3022 	enemy3->addAnimation(k3);
3023 
3024 	Animation m3;
3025 	m3.setName("captured_left");
3026 	m3.addFrame(9);
3027 	enemy3->addAnimation(m3);
3028 
3029 	Animation n3;
3030 	n3.setName("captured_right");
3031 	n3.addFrame(10);
3032 	enemy3->addAnimation(n3);
3033 
3034 	Animation o3;
3035 	o3.setName("captured_down");
3036 	o3.addFrame(11);
3037 	enemy3->addAnimation(o3);
3038 
3039 	enemy3->setDirection(TILE_RIGHT);
3040 	enemy3->setGroup(this);
3041 
3042 	turn = TURN_PLAYER;
3043 
3044 }
3045 
changeBoardWithCurrentHistory()3046 void Board::changeBoardWithCurrentHistory() {
3047 
3048 	int index = currentHistory-1;
3049 
3050 	nimuh->setForcePosition(history[index].nimuh);
3051 	enemy1->setForcePosition(history[index].enemy1);
3052 	enemy2->setForcePosition(history[index].enemy2);
3053 	enemy3->setForcePosition(history[index].enemy3);
3054 
3055 	if (history[index].hasEnemy1 != hasEnemy1) hasEnemy1 = !hasEnemy1;
3056 	if (history[index].hasEnemy2 != hasEnemy2) hasEnemy2 = !hasEnemy2;
3057 	if (history[index].hasEnemy3 != hasEnemy3) hasEnemy3 = !hasEnemy3;
3058 
3059 	if (canChangeInHistory_Tray) {
3060 
3061 		if (history[index].hasTray != hasTray) {
3062 			hasTray = !hasTray;
3063 
3064 			int indexEnemy = 0;
3065 			if (hasTray) {
3066 				indexEnemy = index+1;
3067 			} else {
3068 				indexEnemy = index;
3069 			}
3070 
3071 			if (indexEnemy < (int)history.size()) {
3072 				if (history[indexEnemy].enemy!=NULL) {
3073 					if (hasTray) {
3074 						history[indexEnemy].enemy->setStopped(false);
3075 						history[indexEnemy].enemy->setForceAnimation("stop_right");
3076 						Sounds::GetInstance()->getSound("eating")->stop();
3077 					} else {
3078 						history[indexEnemy].enemy->setStopped(true);
3079 						history[indexEnemy].enemy->setForceAnimation("eating");
3080 						Sounds::GetInstance()->getSound("eating")->play(5, -1);
3081 					}
3082 				}
3083 			}
3084 		}
3085 
3086 	}
3087 
3088 	if (canChangeInHistory_Enemy2) {
3089 
3090 		if (history[index].hasEnemy2 != hasEnemy2) {
3091 
3092 			hasEnemy2 = !hasEnemy2;
3093 
3094 		}
3095 
3096 	}
3097 
3098 	if (canChangeInHistory_Enemy3) {
3099 
3100 		if (history[index].hasEnemy3 != hasEnemy3) {
3101 
3102 			hasEnemy3 = !hasEnemy3;
3103 
3104 		}
3105 
3106 	}
3107 
3108 	if (canChangeInHistory_Key) {
3109 
3110 		doorKeyClosed = history[index].doorKeyClosed;
3111 
3112 		if (doorKeyClosed) {
3113 			doorKeyRight->setForceAnimation("closed");
3114 			doorKeyBottom->setForceAnimation("closed");
3115 		} else {
3116 			doorKeyRight->setForceAnimation("opened");
3117 			doorKeyBottom->setForceAnimation("opened");
3118 		}
3119 
3120 	}
3121 
3122 }
3123 
nextHistory()3124 void Board::nextHistory() {
3125 	beginAnimationWaiting();
3126 	if (currentHistory<totalHistory) {
3127 		currentHistory++;
3128 		changeBoardWithCurrentHistory();
3129 	}
3130 }
3131 
3132 
previousHistory()3133 void Board::previousHistory() {
3134 	beginAnimationWaiting();
3135 	if (currentHistory>1) {
3136 		currentHistory--;
3137 		changeBoardWithCurrentHistory();
3138 	}
3139 }
3140 
addStateHistory()3141 void Board::addStateHistory() {
3142 	if (currentHistory<totalHistory) {
3143 		for (int i=(int)history.size()-1; i>=0; i--) {
3144 			if (history[i].number>currentHistory) {
3145 				history.erase(history.begin()+i);
3146 			}
3147 		}
3148 		currentHistory++;
3149 		totalHistory = currentHistory;
3150 	} else {
3151 		currentHistory++;
3152 		totalHistory++;
3153 	}
3154 	addHistory();
3155 }
3156 
addHistory()3157 void Board::addHistory() {
3158 	History h;
3159 	h.number = currentHistory;
3160 	h.nimuh = nimuh->getPosition();
3161 	h.enemy1 = enemy1->getPosition();
3162 	h.enemy2 = enemy2->getPosition();
3163 	h.enemy3 = enemy3->getPosition();
3164 
3165 	h.enemy = NULL;
3166 
3167 	if (canChangeInHistory_Tray) {
3168 		h.hasTray = hasTray;
3169 		if ( (enemy1->getStopped()) || (enemy2->getStopped()) || (enemy3->getStopped()) ) {
3170 			if ((int)history.size()>0) {
3171 				if ( history[(int)history.size()-1].hasTray ) {
3172 					h.hasTray = false;
3173 					if (enemy1->getStopped()) h.enemy = enemy1;
3174 					if (enemy2->getStopped()) h.enemy = enemy2;
3175 					if (enemy3->getStopped()) h.enemy = enemy3;
3176 				}
3177 			}
3178 		}
3179 	}
3180 
3181 	if (canChangeInHistory_Enemy2) {
3182 		h.hasEnemy2 = hasEnemy2;
3183 		if ((int)history.size()>0) {
3184 			if ( history[(int)history.size()-1].hasEnemy2 != hasEnemy2 ) {
3185 				h.enemy = enemy2;
3186 			}
3187 		}
3188 	}
3189 
3190 	if (canChangeInHistory_Enemy3) {
3191 		h.hasEnemy3 = hasEnemy3;
3192 		if ((int)history.size()>0) {
3193 			if ( history[(int)history.size()-1].hasEnemy3 != hasEnemy3 ) {
3194 				h.enemy = enemy3;
3195 			}
3196 		}
3197 	}
3198 
3199 	if (canChangeInHistory_Key) {
3200 		h.doorKeyClosed = doorKeyClosed;
3201 	}
3202 
3203 	h.hasEnemy1 = hasEnemy1;
3204 	h.hasEnemy2 = hasEnemy2;
3205 	h.hasEnemy3 = hasEnemy3;
3206 
3207 	h.doorKeyClosed = doorKeyClosed;
3208 	history.push_back(h);
3209 }
3210 
verifyPositions(BoardPosition p1,BoardPosition p2)3211 bool Board::verifyPositions(BoardPosition p1, BoardPosition p2) {
3212      if ( (p1.x == p2.x) && (p1.y == p2.y) ) {
3213           return true;
3214      } else {
3215           return false;
3216      }
3217 }
3218 
referencePositions(BoardPosition p1,BoardPosition p2)3219 int Board::referencePositions(BoardPosition p1, BoardPosition p2) {
3220      if ( (p1.x-1 == p2.x) && (p1.y == p2.y) ) {
3221           return TILE_LEFT;
3222      } else if ( (p1.x+1 == p2.x) && (p1.y == p2.y) ) {
3223           return TILE_RIGHT;
3224      } else if ( (p1.x == p2.x) && (p1.y-1 == p2.y) ) {
3225           return TILE_UP;
3226      } else if ( (p1.x == p2.x) && (p1.y+1 == p2.y) ) {
3227           return TILE_DOWN;
3228      } else if ( (p1.x == p2.x) && (p1.y == p2.y) ) {
3229           return TILE_CENTER;
3230      } else {
3231           return TILE_OTHER;
3232      }
3233 }
3234 
verifyAnimationWaiting()3235 void Board::verifyAnimationWaiting() {
3236      if ( (!isAnimationWaiting) && (turn == TURN_PLAYER) ) {
3237          if (Chronometer::GetInstance()->getTime("animationwaiting") > 15000) {
3238 			  Chronometer::GetInstance()->setTime("animationwaiting");
3239               isAnimationWaiting = true;
3240               bool hasPersonage = false;
3241               while (!hasPersonage) {
3242                   typeAnimationWaiting = rand()%7;
3243                   switch (typeAnimationWaiting) {
3244                        case 0:
3245                        case 1:
3246                        case 2:
3247                             hasPersonage = true;
3248                        break;
3249                        case 3:
3250                        case 4:
3251                             if ( (hasEnemy1) && (!enemy1->getStopped()) ) hasPersonage = true;
3252                        break;
3253                        case 5:
3254                        case 6:
3255                             if ( (hasEnemy2) && (!enemy2->getStopped()) )  hasPersonage = true;
3256                        break;
3257                   }
3258               }
3259               switch (typeAnimationWaiting) {
3260                      case 0:
3261                           nimuh->setForceAnimation("clean0");
3262                           nimuh->setAnimation("clean1");
3263 					 	  Sounds::GetInstance()->getSound("nimuh_clean")->play(12, 0);
3264                      break;
3265                      case 1:
3266                           nimuh->setForceAnimation("greeting", 1);
3267                      break;
3268                      case 2:
3269                           nimuh->setForceAnimation("scratch0");
3270                           nimuh->setAnimation("scratch1");
3271                           Sounds::GetInstance()->getSound("nimuh_scratch")->play(12, 0);
3272                      break;
3273                      case 3:
3274                           enemy1->setForceAnimation("impatient");
3275                           Sounds::GetInstance()->getSound("enemy1_impatient")->play(12, 0);
3276                      break;
3277                      case 4:
3278                           enemy1->setForceAnimation("proud0");
3279                           enemy1->setAnimation("proud1");
3280 					 	  Sounds::GetInstance()->getSound("enemy1_proud")->play(12, 0);
3281                      break;
3282                      case 5:
3283                           enemy2->setForceAnimation("cazzo0");
3284                           enemy2->setAnimation("cazzo1");
3285 					 	  Sounds::GetInstance()->getSound("enemy2_cazzo")->play(12, 0);
3286                      break;
3287                      case 6:
3288                           enemy2->setForceAnimation("peeing", 1);
3289 					 	  Sounds::GetInstance()->getSound("enemy2_peeing")->play(12, 0);
3290                      break;
3291               }
3292          }
3293      }
3294 }
3295 
verifyStopAnimationWaiting()3296 void Board::verifyStopAnimationWaiting() {
3297      if (isAnimationWaiting) {
3298          if (Chronometer::GetInstance()->getTime("animationwaiting") > 4000) {
3299 			  beginAnimationWaiting();
3300               isAnimationWaiting = false;
3301               switch (typeAnimationWaiting) {
3302                      case 0:
3303                           nimuh->setForceAnimation("cleanremove");
3304 					 	  nimuh->setAnimation("stop_right");
3305                      break;
3306                      case 1:
3307                           nimuh->setForceAnimation("greetingremove");
3308 					 	  nimuh->setAnimation("stop_right");
3309                      break;
3310                      case 2:
3311                           nimuh->setForceAnimation("scratchremove");
3312                           nimuh->setAnimation("stop_right");
3313                      break;
3314                      case 3:
3315                           enemy1->setForceAnimation("stop_right");
3316                      break;
3317                      case 4:
3318                           enemy1->setForceAnimation("proudremove");
3319                           enemy1->setAnimation("stop_right");
3320                      break;
3321                      case 5:
3322                           enemy2->setForceAnimation("cazzoremove");
3323                           enemy2->setAnimation("stop_right");
3324                      break;
3325                      case 6:
3326                           enemy2->setAnimation("stop_right");
3327                      break;
3328               }
3329          }
3330      }
3331 }
3332 
verifyWinPlayer()3333 void Board::verifyWinPlayer() {
3334 
3335 	if ( verifyPositions(nimuh->getPosition(), door->getPosition()) ) {
3336 		nimuh->setScaleW(0,1500);
3337 		nimuh->setMovementNormalY (nimuh->getYWithoutDisplacementByGroups()+((nimuh->getHeight()/2)*nimuh->getCurrentScaleY()), 1500);
3338 		nimuh->setRotationNormal(1080,1500);
3339 		nimuh->setFadeOut(1500);
3340 		Sounds::GetInstance()->getSound("out_door")->play(6, 0);
3341 		Sounds::GetInstance()->getSound("crying")->play(7, 0);
3342 		Chronometer::GetInstance()->setTime("winning");
3343 		scenegame->setStatusScene(SCENE_GAME_WINNING);
3344 		if (hasEnemy1) enemy1->setForceAnimation("crying");
3345 		if (hasEnemy2) enemy2->setForceAnimation("crying");
3346 		if (hasEnemy3) enemy3->setForceAnimation("crying");
3347 	}
3348 
3349 }
3350 
createExplosion(Enemy * e)3351 void Board::createExplosion(Enemy *e) {
3352 	explosionEnemies->setPositions(posX + Plot::GetInstance()->getX(e->getPosition().x, e->getPosition().y) - ((int)floor0->getWidth()/2) + paddingExplosion, posY+Plot::GetInstance()->getY(e->getPosition().x,e->getPosition().y)-e->getHeightElement() + paddingExplosion, e->getWidth() - paddingExplosion, e->getHeight() - paddingExplosion);
3353 	explosionEnemies->start();
3354 	Sounds::GetInstance()->getSound("colision")->play(4, 0);
3355 	if (e->getAnimation()->getName()=="eating") {
3356 		Sounds::GetInstance()->getSound("eating")->stop();
3357 	}
3358 	inExplosion = true;
3359 }
3360 
verifyStartToEat(Enemy * e)3361 void Board::verifyStartToEat(Enemy *e) {
3362 	if (hasTray) {
3363 		if ( verifyPositions(e->getPosition(), tray->getPosition()) ) {
3364 			e->setStopped(true);
3365 			e->setForceAnimation("eating");
3366 			Sounds::GetInstance()->getSound("open_tray")->play(9, 0);
3367 			tray->setScaleGL(3.5, 1500);
3368 			tray->setRotationNormal(360, 1500);
3369 			tray->setMovementNormalY(tray->getYWithoutDisplacementByGroups()-(tray->getHeight()*2), 1500);
3370 			Chronometer::GetInstance()->setTime("starttray");
3371 			scenegame->setStatusScene(SCENE_GAME_STARTTRAY);
3372 		}
3373 	}
3374 }
3375 
nextTurn()3376 void Board::nextTurn() {
3377 	if (mlPause>0) return;
3378     if (inExplosion) return;
3379 
3380 	if (turn < TURN2_ENEMY) {
3381 		turn++;
3382 	} else {
3383 		turn = 0;
3384 	}
3385     //printf("nextturn: %d\n", turn);
3386 
3387     if (turn == TURN_PLAYER) {
3388 		addStateHistory();
3389 		beginAnimationWaiting();
3390     }
3391 
3392  	if (turn == TURN_ENEMY3) {
3393        if (!hasEnemy3) {
3394           todoNextTurn = true;
3395        } else {
3396           if ( ( referencePositions(enemy3->getPosition(), nimuh->getPosition()) != TILE_OTHER )
3397 				&& ( !hasWallBetweenCells(enemy3->getPosition(), nimuh->getPosition()) ) && (!enemy3->getStopped()) ) { // atrapado
3398 			if (referencePositions(nimuh->getPosition(), enemy3->getPosition()) == TILE_RIGHT) {
3399 				nimuh->setForceAnimation("captured_right");
3400 			} else if (referencePositions(nimuh->getPosition(), enemy3->getPosition()) == TILE_LEFT) {
3401 				nimuh->setForceAnimation("captured_left");
3402 			} else if (referencePositions(nimuh->getPosition(), enemy3->getPosition()) == TILE_UP) {
3403 				nimuh->setForceAnimation("captured_up");
3404 			} else if (referencePositions(nimuh->getPosition(), enemy3->getPosition()) == TILE_DOWN) {
3405 				nimuh->setForceAnimation("captured_down");
3406 			}
3407 			if (referencePositions(enemy3->getPosition(), nimuh->getPosition()) == TILE_RIGHT) {
3408 				enemy3->setForceAnimation("captured_right");
3409 			} else if (referencePositions(enemy3->getPosition(), nimuh->getPosition()) == TILE_LEFT) {
3410 				enemy3->setForceAnimation("captured_left");
3411 			} else if (referencePositions(enemy3->getPosition(), nimuh->getPosition()) == TILE_UP) {
3412 				enemy3->setForceAnimation("captured_up");
3413 			} else if (referencePositions(enemy3->getPosition(), nimuh->getPosition()) == TILE_DOWN) {
3414 				enemy3->setForceAnimation("captured_down");
3415 			}
3416 			enemy1->stop();
3417 			if (hasEnemy2) enemy2->stop();
3418 			Sounds::GetInstance()->getSound("gun")->play(12, 0);
3419 			Chronometer::GetInstance()->setTime("captured");
3420 			scenegame->setStatusScene(SCENE_GAME_CAPTURED);
3421 		  } else {
3422 			if (!enemy3->move()) {
3423 				todoNextTurn = true;
3424 			} else {
3425 				Sounds::GetInstance()->getSound("steps_enemy3")->play(2, 0);
3426 			}
3427           }
3428        }
3429     }
3430 
3431 	if ( (turn != TURN_PLAYER) && (turn != TURN_ENEMY3) ) {
3432 		if ( (hasEnemy1) && ( referencePositions(enemy1->getPosition(), nimuh->getPosition()) != TILE_OTHER )
3433 				&& ( !hasWallBetweenCells(enemy1->getPosition(), nimuh->getPosition()) ) && (!enemy1->getStopped()) ) { // atrapado
3434 			if (referencePositions(nimuh->getPosition(), enemy1->getPosition()) == TILE_RIGHT) {
3435 				nimuh->setForceAnimation("captured_right");
3436 			} else if (referencePositions(nimuh->getPosition(), enemy1->getPosition()) == TILE_LEFT) {
3437 				nimuh->setForceAnimation("captured_left");
3438 			} else if (referencePositions(nimuh->getPosition(), enemy1->getPosition()) == TILE_UP) {
3439 				nimuh->setForceAnimation("captured_up");
3440 			} else if (referencePositions(nimuh->getPosition(), enemy1->getPosition()) == TILE_DOWN) {
3441 				nimuh->setForceAnimation("captured_down");
3442 			}
3443 			if (referencePositions(enemy1->getPosition(), nimuh->getPosition()) == TILE_RIGHT) {
3444 				enemy1->setForceAnimation("captured_right");
3445 			} else if (referencePositions(enemy1->getPosition(), nimuh->getPosition()) == TILE_LEFT) {
3446 				enemy1->setForceAnimation("captured_left");
3447 			} else if (referencePositions(enemy1->getPosition(), nimuh->getPosition()) == TILE_UP) {
3448 				enemy1->setForceAnimation("captured_up");
3449 			} else if (referencePositions(enemy1->getPosition(), nimuh->getPosition()) == TILE_DOWN) {
3450 				enemy1->setForceAnimation("captured_down");
3451 			}
3452 			if (hasEnemy2) enemy2->stop();
3453 			if (hasEnemy3) enemy3->stop();
3454 			Sounds::GetInstance()->getSound("gun")->play(12, 0);
3455 			Chronometer::GetInstance()->setTime("captured");
3456 			scenegame->setStatusScene(SCENE_GAME_CAPTURED);
3457 		} else if ( (hasEnemy2) && ( referencePositions(enemy2->getPosition(), nimuh->getPosition()) != TILE_OTHER )
3458 				&& ( !hasWallBetweenCells(enemy2->getPosition(), nimuh->getPosition()) ) && (!enemy2->getStopped()) ) { // atrapado
3459 			if (referencePositions(nimuh->getPosition(), enemy2->getPosition()) == TILE_RIGHT) {
3460 				nimuh->setForceAnimation("captured_right");
3461 			} else if (referencePositions(nimuh->getPosition(), enemy2->getPosition()) == TILE_LEFT) {
3462 				nimuh->setForceAnimation("captured_left");
3463 			} else if (referencePositions(nimuh->getPosition(), enemy2->getPosition()) == TILE_UP) {
3464 				nimuh->setForceAnimation("captured_up");
3465 			} else if (referencePositions(nimuh->getPosition(), enemy2->getPosition()) == TILE_DOWN) {
3466 				nimuh->setForceAnimation("captured_down");
3467 			}
3468 			if (referencePositions(enemy2->getPosition(), nimuh->getPosition()) == TILE_RIGHT) {
3469 				enemy2->setForceAnimation("captured_right");
3470 			} else if (referencePositions(enemy2->getPosition(), nimuh->getPosition()) == TILE_LEFT) {
3471 				enemy2->setForceAnimation("captured_left");
3472 			} else if (referencePositions(enemy2->getPosition(), nimuh->getPosition()) == TILE_UP) {
3473 				enemy2->setForceAnimation("captured_up");
3474 			} else if (referencePositions(enemy2->getPosition(), nimuh->getPosition()) == TILE_DOWN) {
3475 				enemy2->setForceAnimation("captured_down");
3476 			}
3477 			enemy1->stop();
3478 			if (hasEnemy3) enemy3->stop();
3479 			Sounds::GetInstance()->getSound("gun")->play(12, 0);
3480 			Chronometer::GetInstance()->setTime("captured");
3481 			scenegame->setStatusScene(SCENE_GAME_CAPTURED);
3482 		} else {
3483 			if (hasEnemy1 && hasEnemy2) {
3484                 catchedEnemy1 = !enemy1->move();
3485                 catchedEnemy2 = !enemy2->move();
3486 				if (!catchedEnemy1) Sounds::GetInstance()->getSound("steps_enemy1")->play(2, 0);
3487 				if (!catchedEnemy2) Sounds::GetInstance()->getSound("steps_enemy2")->play(2, 0);
3488 			} else if (hasEnemy1 && !hasEnemy2) {
3489     			if (!enemy1->move()) {
3490     				verifyWinPlayer();
3491     			    todoNextTurn = true;
3492     			} else {
3493 					Sounds::GetInstance()->getSound("steps_enemy1")->play(2, 0);
3494 				}
3495             } else if (!hasEnemy1 && hasEnemy2) {
3496     			if (!enemy2->move()) {
3497     				verifyWinPlayer();
3498     			    todoNextTurn = true;
3499     			} else {
3500 					Sounds::GetInstance()->getSound("steps_enemy2")->play(2, 0);
3501 				}
3502             }
3503 		}
3504 	}
3505 }
3506 
stopAnimations()3507 void Board::stopAnimations() {
3508 
3509 	verifyAnimationWaiting();
3510 	verifyStopAnimationWaiting();
3511 
3512 	if (mlPause>0) {
3513 		if (Chronometer::GetInstance()->getTime("pause-board") > mlPause) {
3514 			mlPause = 0;
3515 			todoNextTurn = true;
3516 		} else {
3517 			return;
3518 		}
3519 	}
3520 
3521     if (turn == TURN_PLAYER) {
3522 
3523         if (teletransport) {
3524 
3525             if (nimuh->getFadeOut()<=0) {
3526                  nimuh->setForcePosition(posTeletransport);
3527                  nimuh->setFadeIn(1000);
3528                  pause(1500);
3529                  teletransport = false;
3530             }
3531 
3532         } else {
3533 
3534     		if (nimuh->getIsMovement()) {
3535     			if (nimuh->getNCyclesMovementNormal()/2 == nimuh->getCountCyclesMovementNormal()) {
3536     				nimuh->setNextBoardPositionInElement();
3537     			}
3538     		}
3539             if ( (!nimuh->getIsMovement()) && (nimuh->getIsMoving()) ) {
3540         		nimuh->stop();
3541         		teletransport = false;
3542         		if ( hasBlackHole0 && hasBlackHole1 ) {
3543                      if ( verifyPositions(nimuh->getPosition(), blackHole0->getPosition()) ) {
3544                           posTeletransport = blackHole1->getPosition();
3545                           teletransport = true;
3546 						  Sounds::GetInstance()->getSound("teletransport")->play(3, 0);
3547                      }
3548                      if ( verifyPositions(nimuh->getPosition(), blackHole1->getPosition()) ) {
3549                           posTeletransport = blackHole0->getPosition();
3550                           teletransport = true;
3551 						  Sounds::GetInstance()->getSound("teletransport")->play(3, 0);
3552                      }
3553                 }
3554 
3555 				if (hasKey) {
3556 					if ( verifyPositions(nimuh->getPosition(), key->getPosition()) ) {
3557 						swapDoorKey();
3558 					}
3559 				}
3560 
3561                 if (teletransport) {
3562                     nimuh->setFadeOut(1000);
3563                 }
3564 
3565                 todoNextTurn = !teletransport;
3566 
3567 				if (hasTrap) {
3568 					if ( verifyPositions(nimuh->getPosition(), posTrap) ) {
3569                         Sounds::GetInstance()->getSound("jail")->play(12, 0);
3570 						nimuh->setForceAnimation("jail", 1);
3571 						Chronometer::GetInstance()->setTime("captured");
3572 						scenegame->setStatusScene(SCENE_GAME_CAPTURED);
3573 						todoNextTurn = false;
3574 					}
3575 				}
3576 
3577             }
3578 
3579         }
3580     }
3581 
3582     if ( (hasEnemy3) && (turn == TURN_ENEMY3) ) {
3583 		if (enemy3->getIsMovement()) {
3584 			if (enemy3->getNCyclesMovementNormal()/2 == enemy3->getCountCyclesMovementNormal()) {
3585 				enemy3->setNextBoardPositionInElement();
3586 				if ( ( verifyPositions(enemy3->getPosition(), enemy2->getPosition()) )
3587                      || ( verifyPositions(enemy3->getPosition(), enemy1->getPosition()) ) ) {
3588 					createExplosion(enemy3);
3589                 }
3590 			}
3591 		}
3592     	if ( (!enemy3->getIsMovement()) && (enemy3->getIsMoving()) ) {
3593             enemy3->stop();
3594     		if (turn == TURN2_ENEMY) {
3595     			verifyWinPlayer();
3596     		}
3597 			if (hasKey) {
3598 				if ( verifyPositions(enemy3->getPosition(), key->getPosition()) ) {
3599 					swapDoorKey();
3600 				}
3601 			}
3602 
3603 			verifyStartToEat(enemy3);
3604 
3605     		todoNextTurn = true;
3606 			// enemy3 is death??
3607 			if ( verifyPositions(enemy3->getPosition(), enemy2->getPosition()) ) {
3608 				hasEnemy3 = false;
3609 			}
3610 			if ( verifyPositions(enemy3->getPosition(), enemy1->getPosition()) ) {
3611 				hasEnemy3 = false;
3612 			}
3613             if (inExplosion) {
3614                 todoNextTurn = false;
3615             }
3616         }
3617     }
3618 
3619     if ( (turn != TURN_PLAYER) && (turn != TURN_ENEMY3) ) {
3620 
3621         if (hasEnemy1 && hasEnemy2) {
3622 			if (enemy2->getIsMovement()) {
3623 				if (enemy2->getNCyclesMovementNormal()/2 == enemy2->getCountCyclesMovementNormal()) {
3624 					enemy2->setNextBoardPositionInElement();
3625 					BoardPosition pe1;
3626 					if (enemy1->getIsMoving()) {
3627                          pe1 = enemy1->getNextBoardPositionInElement();
3628                     } else {
3629                          pe1 = enemy1->getPosition();
3630                     }
3631 					if ( verifyPositions(enemy2->getPosition(), pe1) ) {
3632                         createExplosion(enemy2);
3633                     }
3634                     if ( (hasEnemy3) && ( verifyPositions(enemy2->getPosition(), enemy3->getPosition()) ) ) {
3635                         createExplosion(enemy3);
3636                     }
3637 				}
3638 			}
3639 			if (enemy1->getIsMovement()) {
3640 				if (enemy1->getNCyclesMovementNormal()/2 == enemy1->getCountCyclesMovementNormal()) {
3641 					enemy1->setNextBoardPositionInElement();
3642 					BoardPosition pe2;
3643 					if (enemy2->getIsMoving()) {
3644                          pe2 = enemy2->getNextBoardPositionInElement();
3645                     } else {
3646                          pe2 = enemy2->getPosition();
3647                     }
3648 					//printf("hola: %d %d - %d %d\n", enemy1->getPosition().x, enemy1->getPosition().y, pe2.x, pe2.y);
3649 					if ( verifyPositions(enemy1->getPosition(), pe2) ) {
3650                         createExplosion(enemy2);
3651                     }
3652                     if ( (hasEnemy3) && ( verifyPositions(enemy1->getPosition(), enemy3->getPosition()) ) ) {
3653                         createExplosion(enemy3);
3654                     }
3655 				}
3656 			}
3657             if (!catchedEnemy1 && !catchedEnemy2) {
3658             	if ( (!enemy2->getIsMovement()) && (enemy2->getIsMoving()) &&
3659                    (!enemy1->getIsMovement()) && (enemy1->getIsMoving()) ) {
3660             		enemy1->stop();
3661             		enemy2->stop();
3662         			if (hasKey) {
3663         				if ( verifyPositions(enemy1->getPosition(), key->getPosition()) ) {
3664         					swapDoorKey();
3665         				}
3666         			}
3667         			if (hasKey) {
3668         				if ( verifyPositions(enemy2->getPosition(), key->getPosition()) ) {
3669         					swapDoorKey();
3670         				}
3671         			}
3672 
3673             		if (turn == TURN2_ENEMY) {
3674             			verifyWinPlayer();
3675             		}
3676             		todoNextTurn = true;
3677 					verifyStartToEat(enemy1);
3678 					verifyStartToEat(enemy2);
3679                 }
3680             }
3681             if (catchedEnemy1 && !catchedEnemy2) {
3682             	if ( (!enemy2->getIsMovement()) && (enemy2->getIsMoving()) ) {
3683             		enemy2->stop();
3684         			if (hasKey) {
3685         				if ( verifyPositions(enemy2->getPosition(), key->getPosition()) ) {
3686         					swapDoorKey();
3687         				}
3688         			}
3689             		if (turn == TURN2_ENEMY) {
3690             			verifyWinPlayer();
3691             		}
3692             		todoNextTurn = true;
3693 					verifyStartToEat(enemy2);
3694                 }
3695             }
3696             if (!catchedEnemy1 && catchedEnemy2) {
3697             	if ( (!enemy1->getIsMovement()) && (enemy1->getIsMoving()) ) {
3698             		enemy1->stop();
3699         			if (hasKey) {
3700         				if ( verifyPositions(enemy1->getPosition(), key->getPosition()) ) {
3701         					swapDoorKey();
3702         				}
3703         			}
3704             		if (turn == TURN2_ENEMY) {
3705             			verifyWinPlayer();
3706             		}
3707             		todoNextTurn = true;
3708 					verifyStartToEat(enemy1);
3709                 }
3710             }
3711             if (catchedEnemy1 && catchedEnemy2) {
3712            		todoNextTurn = true;
3713         		if (turn == TURN2_ENEMY) {
3714         			verifyWinPlayer();
3715         		}
3716             }
3717 			// enemy2 is death??
3718 			if ( verifyPositions(enemy2->getPosition(), enemy1->getPosition()) && todoNextTurn ) {
3719 				hasEnemy2 = false;
3720 			}
3721 			// enemy3 is death??
3722 			if ( verifyPositions(enemy2->getPosition(), enemy3->getPosition()) && todoNextTurn ) {
3723 				hasEnemy3 = false;
3724 			}
3725 			if ( verifyPositions(enemy1->getPosition(), enemy3->getPosition()) && todoNextTurn ) {
3726 				hasEnemy3 = false;
3727 			}
3728 
3729             if (inExplosion) {
3730                 todoNextTurn = false;
3731             }
3732         } if (hasEnemy1 && !hasEnemy2) {
3733 			if (enemy1->getIsMovement()) {
3734 				if (enemy1->getNCyclesMovementNormal()/2 == enemy1->getCountCyclesMovementNormal()) {
3735 					enemy1->setNextBoardPositionInElement();
3736                     if ( (hasEnemy3) && ( verifyPositions(enemy1->getPosition(), enemy3->getPosition()) ) ) {
3737                         createExplosion(enemy3);
3738                     }
3739 				}
3740 			}
3741         	if ( (!enemy1->getIsMovement()) && (enemy1->getIsMoving()) ) {
3742         		enemy1->stop();
3743         		if (turn == TURN2_ENEMY) {
3744         			verifyWinPlayer();
3745         		}
3746     			if (hasKey) {
3747     				if ( verifyPositions(enemy1->getPosition(), key->getPosition()) ) {
3748     					swapDoorKey();
3749     				}
3750     			}
3751 				verifyStartToEat(enemy1);
3752         		todoNextTurn = true;
3753             }
3754 
3755 			// enemy3 is death??
3756 			if ( verifyPositions(enemy1->getPosition(), enemy3->getPosition()) && todoNextTurn ) {
3757 				hasEnemy3 = false;
3758 			}
3759 
3760             if (inExplosion) {
3761                 todoNextTurn = false;
3762             }
3763 
3764         } if (!hasEnemy1 && hasEnemy2) {
3765 			if (enemy2->getIsMovement()) {
3766 				if (enemy2->getNCyclesMovementNormal()/2 == enemy2->getCountCyclesMovementNormal()) {
3767 					enemy2->setNextBoardPositionInElement();
3768                     if ( (hasEnemy3) && ( verifyPositions(enemy2->getPosition(), enemy3->getPosition()) ) ) {
3769                         createExplosion(enemy3);
3770                     }
3771 				}
3772 			}
3773         	if ( (!enemy2->getIsMovement()) && (enemy2->getIsMoving()) ) {
3774         		enemy2->stop();
3775         		if (turn == TURN2_ENEMY) {
3776         			verifyWinPlayer();
3777         		}
3778     			if (hasKey) {
3779     				if ( verifyPositions(enemy2->getPosition(), key->getPosition()) ) {
3780     					swapDoorKey();
3781     				}
3782     			}
3783 				verifyStartToEat(enemy2);
3784         		todoNextTurn = true;
3785             }
3786 
3787 			// enemy3 is death??
3788 			if ( verifyPositions(enemy2->getPosition(), enemy3->getPosition()) && todoNextTurn ) {
3789 				hasEnemy3 = false;
3790 			}
3791 
3792             if (inExplosion) {
3793                 todoNextTurn = false;
3794             }
3795 
3796         }
3797 
3798    }
3799 }
3800 
movePlayer(int pos)3801 void Board::movePlayer(int pos) {
3802 	if (turn == TURN_PLAYER) {
3803 		if ( (!nimuh->getIsMovement()) && (!nimuh->getIsMoving()) && (mlPause<=0) && (!teletransport) ) {
3804 			beginAnimationWaiting();
3805 			BoardPosition place;
3806 			if (pos==TILE_CENTER) {
3807 				place.x = nimuh->getPosition().x;
3808 				place.y = nimuh->getPosition().y;
3809 			} else if (pos==TILE_LEFT) {
3810 				place.x = nimuh->getPosition().x-1;
3811 				place.y = nimuh->getPosition().y;
3812 			} else if (pos==TILE_RIGHT) {
3813 				place.x = nimuh->getPosition().x+1;
3814 				place.y = nimuh->getPosition().y;
3815 			} else if (pos==TILE_DOWN) {
3816 				place.x = nimuh->getPosition().x;
3817 				place.y = nimuh->getPosition().y+1;
3818 			} else if (pos==TILE_UP) {
3819 				place.x = nimuh->getPosition().x;
3820 				place.y = nimuh->getPosition().y-1;
3821 			}
3822 			place.out = !positionIsOnBoard(place);
3823 
3824 			bool tileCenter = true;
3825 			if (referencePositions(place, nimuh->getPosition()) != TILE_CENTER) {
3826 				tileCenter = false;
3827 			}
3828 			if (nimuh->setPosition(place)) {
3829 				//addStateHistory();
3830 				if (!tileCenter) {
3831 					Sounds::GetInstance()->getSound("steps_nimuh")->play(2, 0);
3832 				}
3833 			}
3834 		}
3835 	}
3836 }
3837 
verifyClickOnBoard()3838 void Board::verifyClickOnBoard() {
3839 	if (turn == TURN_PLAYER) {
3840         beginAnimationWaiting();
3841 		if ( (!nimuh->getIsMovement()) && (!nimuh->getIsMoving()) && (mlPause<=0) && (!teletransport) ) {
3842 			bool tileCenter = true;
3843 			if (referencePositions(getPositionMouse(), nimuh->getPosition()) != TILE_CENTER) {
3844 				tileCenter = false;
3845 			}
3846 			if (nimuh->setPosition(getPositionMouse())) {
3847 				//addStateHistory();
3848 				if (!tileCenter) {
3849 					Sounds::GetInstance()->getSound("steps_nimuh")->play(2, 0);
3850 				}
3851 			}
3852 		}
3853 	}
3854 }
3855 
3856 
drawArrow()3857 void Board::drawArrow() {
3858 
3859 	//arrow
3860 
3861 	if ( (turn == TURN_PLAYER) && (!nimuh->getIsMoving()) && scenegame->getActive()
3862 			&& (scenegame->getStatusScene() == SCENE_GAME_MAIN) ) {
3863 
3864 		BoardPosition bpmouse = getPositionMouse();
3865 		if (!bpmouse.out) {
3866 			int rel = referencePositions(nimuh->getPosition(), bpmouse);
3867 
3868 			if (rel!=TILE_OTHER) {
3869 				arrow->setForcePosition(bpmouse);
3870 				floorOn->setForcePosition(bpmouse);
3871 				bool drawArrow = true;
3872 				if (rel==TILE_CENTER) {
3873 					arrow->setForceAnimation("center");
3874 				} else if (rel==TILE_RIGHT) {
3875 					if (hasWallRight(nimuh->getPosition().x, nimuh->getPosition().y)) {
3876 						drawArrow = false;
3877 					} else {
3878 						arrow->setForceAnimation("right");
3879 					}
3880 				} else if (rel==TILE_LEFT) {
3881 					if (hasWallRight(nimuh->getPosition().x-1, nimuh->getPosition().y)) {
3882 						drawArrow = false;
3883 					} else {
3884 						arrow->setForceAnimation("left");
3885 					}
3886 				} else if (rel==TILE_UP) {
3887 					if (hasWallBottom(nimuh->getPosition().x, nimuh->getPosition().y-1)) {
3888 						drawArrow = false;
3889 					} else {
3890 						arrow->setForceAnimation("up");
3891 					}
3892 				} else if (rel==TILE_DOWN) {
3893 					if (hasWallBottom(nimuh->getPosition().x, nimuh->getPosition().y)) {
3894 						drawArrow = false;
3895 					} else {
3896 						arrow->setForceAnimation("down");
3897 					}
3898 				}
3899 				if (drawArrow) {
3900 					floorOn->draw();
3901 					arrow->draw();
3902 				}
3903 			}
3904 		}
3905 	}
3906 }
3907 
draw()3908 void Board::draw() {
3909 
3910 	if (!scenegame->getActive()) {
3911 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 0) ) {
3912 			floor0->setFadeIn(500);
3913 			floor1->setFadeIn(500);
3914 			trap->setFadeIn(500);
3915 			floor0->setRotationNormal(360, 500);
3916 			floor1->setRotationNormal(360, 500);
3917 			trap->setRotationNormal(360, 500);
3918 		}
3919 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 500) ) {
3920             scenary->show();
3921 			wallBottom->setFadeIn(500);
3922 			wallRight->setFadeIn(500);
3923 			pavementTop->setFadeIn(500);
3924 			pavementBottom->setFadeIn(500);
3925         	if (hasBlackHole0) {
3926 				blackHole0->show();
3927 				particlesBlackHole0->show();
3928 				blackHole0->setFadeIn(500);
3929 				particlesBlackHole0->setFadeIn(500);
3930             }
3931         	if (hasBlackHole1) {
3932 				blackHole1->show();
3933 				particlesBlackHole1->show();
3934 				blackHole1->setFadeIn(500);
3935 				particlesBlackHole1->setFadeIn(500);
3936             }
3937 			if (hasKey) {
3938 				key->show();
3939 				doorKeyBottom->show();
3940 				doorKeyRight->show();
3941 				key->setFadeIn(500);
3942 				doorKeyBottom->setFadeIn(500);
3943 				doorKeyRight->setFadeIn(500);
3944             }
3945 			if (hasTray) {
3946 				tray->show();
3947 				tray->setFadeIn(500);
3948             }
3949 		}
3950 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 1000) ) {
3951 			if (hasEnemy3) {
3952 				Sounds::GetInstance()->getSound("pop")->play(2, 0);
3953 				enemy3->show();
3954 			}
3955 		}
3956 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 1500) ) {
3957 			if (hasEnemy2) {
3958 				Sounds::GetInstance()->getSound("pop")->play(2, 0);
3959 				enemy2->show();
3960 			}
3961 		}
3962 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 2000) ) {
3963 			if (hasEnemy1) {
3964 				Sounds::GetInstance()->getSound("pop")->play(2, 0);
3965 				enemy1->show();
3966 			}
3967 		}
3968 		if ( Chronometer::GetInstance()->verifyTime("intro_board", 2500) ) {
3969 			Sounds::GetInstance()->getSound("pop")->play(2, 0);
3970 			nimuh->show();
3971 		}
3972 	}
3973 
3974 	// scenary
3975 	scenary->work();
3976 	scenary->draw();
3977 
3978 	pavementTop->draw();
3979 
3980     int x, y;
3981 
3982 	// floor
3983 
3984 	bool todoParticlesTrap = false;
3985 
3986 	for (y=0; y<rows; y++) {
3987 
3988         for (x=0; x<cols; x++) {
3989 			int xs0 = 0;
3990 			int xs1 = 1;
3991 			if (hasTrap) {
3992 				if ( (posTrap.x == 0) && (posTrap.y == 0) ) {
3993 					xs0 = 2;
3994 				}
3995 				if ( (posTrap.x == 1) && (posTrap.y == 0) ) {
3996 					xs1 = 3;
3997 				}
3998 			}
3999 			if ( (y==0) && ( (x==xs0) || (x==xs1) ) ) {
4000 				floor0->setToDoAllEffects(true);
4001 				floor1->setToDoAllEffects(true);
4002 			} else {
4003 				floor0->setToDoAllEffects(false);
4004 				floor1->setToDoAllEffects(false);
4005 			}
4006 
4007 			int res = 0;
4008 			if (y%2==0) {
4009 				res = 0;
4010 			} else {
4011 				res = 1;
4012 			}
4013 			bool todoFloor = true;
4014 			if (hasTrap) {
4015 				if ( (posTrap.x == x) && (posTrap.y == y) ) {
4016 					trap->setX(Plot::GetInstance()->getX(x, y));
4017 					trap->setY(Plot::GetInstance()->getY(x, y)-2);
4018 					trap->draw();
4019 					todoParticlesTrap = true;
4020 					todoFloor = false;
4021 				}
4022 			}
4023 			if (todoFloor) {
4024 				if (x%2==res) {
4025 					floor0->setX(Plot::GetInstance()->getX(x, y));
4026 					floor0->setY(Plot::GetInstance()->getY(x, y));
4027 					floor0->draw();
4028 				} else {
4029 					floor1->setX(Plot::GetInstance()->getX(x, y));
4030 					floor1->setY(Plot::GetInstance()->getY(x, y));
4031 					floor1->draw();
4032 				}
4033 			}
4034         }
4035 
4036 	}
4037 
4038 	floorShadow->draw();
4039 
4040 	drawArrow();
4041 
4042 	// door
4043 	if ( (door->getDoorPosition()==UP) || (door->getDoorPosition()==LEFT) ) {
4044 		door->draw();
4045 	}
4046 
4047 	// objects
4048 
4049 	bool firstWallRight = true;
4050 	bool firstWallBottom = true;
4051 
4052 	for (int s=0; s<=(rows-1)*2; s++) {
4053         for (int n=0; n<=s; n++) {
4054             x = n;
4055             y = s-n;
4056             if (positionIsOnBoard(getPositionFromXY(x,y))) {
4057 
4058                 if (hasBlackHole0) {
4059                     if ( (blackHole0->getXBoard() == x) && (blackHole0->getYBoard() == y) ) {
4060             			blackHole0->draw();
4061             		}
4062                 }
4063 
4064                 if (hasBlackHole1) {
4065             		if ( (blackHole1->getXBoard() == x) && (blackHole1->getYBoard() == y) ) {
4066             			blackHole1->draw();
4067             		}
4068                 }
4069 
4070         		//drawArrow(x, y);
4071 
4072         		if ( (nimuh->getXBoard() == x) && (nimuh->getYBoard() == y) ) {
4073         			nimuh->draw();
4074         		}
4075 				if ( (enemy1->getXBoard() == x) && (enemy1->getYBoard() == y) && hasEnemy1 ) {
4076         			enemy1->draw();
4077         		}
4078 				if ( (enemy2->getXBoard() == x) && (enemy2->getYBoard() == y) && hasEnemy2 ) {
4079         			enemy2->draw();
4080         		}
4081 				if ( (enemy3->getXBoard() == x) && (enemy3->getYBoard() == y) && hasEnemy3 ) {
4082         			enemy3->draw();
4083         		}
4084 
4085 				if (todoParticlesTrap) {
4086 					if ( (posTrap.x == x) && (posTrap.y == y) ) {
4087 						particlesTrap->draw();
4088 					}
4089 				}
4090 
4091                 if (hasBlackHole0) {
4092                     if ( (particlesBlackHole0->getXBoard() == x) && (particlesBlackHole0->getYBoard() == y) ) {
4093             			particlesBlackHole0->draw();
4094             		}
4095                 }
4096 
4097                 if (hasBlackHole1) {
4098             		if ( (particlesBlackHole1->getXBoard() == x) && (particlesBlackHole1->getYBoard() == y) ) {
4099             			particlesBlackHole1->draw();
4100             		}
4101                 }
4102 
4103 				if (hasKey) {
4104 					if ( (key->getXBoard() == x) && (key->getYBoard() == y) ) {
4105 						key->draw();
4106 					}
4107                 }
4108 
4109 				if (hasKey) {
4110 
4111                     if (directionKey == "right") {
4112 						if ( (doorKeyRight->getXBoard() == x) && (doorKeyRight->getYBoard() == y) ) {
4113 							doorKeyRight->draw();
4114 						}
4115 					} else {
4116 						if ( (doorKeyBottom->getXBoard() == x) && (doorKeyBottom->getYBoard() == y) ) {
4117 							doorKeyBottom->draw();
4118 						}
4119 					}
4120                 }
4121 
4122 				if (hasTray) {
4123 					if ( (tray->getXBoard() == x) && (tray->getYBoard() == y) ) {
4124 						tray->draw();
4125 					}
4126                 }
4127 
4128     			// right wall
4129     			if ( ( hasWallRight(x, y, false) ) && (x<getCols()) ) {
4130 					if (firstWallRight) {
4131 						wallRight->setToDoAllEffects(true);
4132 						firstWallRight = false;
4133 					} else {
4134 						wallRight->setToDoAllEffects(false);
4135 					}
4136     				wallRight->setForcePosition(x,y);
4137     				wallRight->draw();
4138     			}
4139 
4140     			// bottom wall
4141     			if ( ( hasWallBottom(x, y, false) ) && (y<getRows()) ) {
4142 					if (firstWallBottom) {
4143 						wallBottom->setToDoAllEffects(true);
4144 						firstWallBottom= false;
4145 					} else {
4146 						wallBottom->setToDoAllEffects(false);
4147 					}
4148     				wallBottom->setForcePosition(x,y);
4149     				wallBottom->draw();
4150     			}
4151 
4152 
4153             }
4154 
4155         }
4156     } // final objects
4157 
4158 	if (inExplosion) {
4159        if (!explosionEnemies->isVisible()) {
4160             inExplosion = false;
4161             todoNextTurn = true;
4162        }
4163        explosionEnemies->draw();
4164     }
4165 
4166 	pavementBottom->draw();
4167 
4168 	// door
4169 	if ( (door->getDoorPosition()==DOWN) || (door->getDoorPosition()==RIGHT) ) {
4170 		door->draw();
4171 	}
4172 
4173 
4174 }
4175 
unLoad()4176 void Board::unLoad() {
4177 	history.clear();
4178 	delete door;
4179 	delete scenary;
4180 	delete arrow;
4181 	delete floor0;
4182 	delete floor1;
4183 	delete floorShadow;
4184 	delete wallBottom;
4185 	delete wallRight;
4186 	delete nimuh;
4187 	delete enemy1;
4188 	delete enemy2;
4189 	delete enemy3;
4190 	delete explosionEnemies;
4191 
4192 	delete blackHole0;
4193 	delete blackHole1;
4194 	delete particlesBlackHole0;
4195 	delete particlesBlackHole1;
4196 	delete trap;
4197 	delete particlesTrap;
4198 	delete tray;
4199 
4200 	delete pavementTop;
4201     delete pavementBottom;
4202 	delete floorOn;
4203 }
4204 
4205 
4206 /*****************************
4207 **
4208 ** FUNCIONES PARA TRABAJAR
4209 ** CON ARCHIVO XML
4210 **
4211 ******************************/
4212 
4213 
4214 /*****************************
4215 ** FUNCIONES PARA CARGAR SCREEN
4216 ******************************/
4217 
4218 
4219 typedef struct
4220 {
4221 	int x, y;
4222 	bool bottom, right;
4223 	string direction;
4224 } DataScreenXML;
4225 
4226 
initDataScreenXML(DataScreenXML * dataScreen)4227 void initDataScreenXML(DataScreenXML *dataScreen) {
4228 	dataScreen->x = 0;
4229 	dataScreen->y = 0;
4230 	dataScreen->bottom = false;
4231 	dataScreen->right = false;
4232 }
4233 
startScreenXML(void * userData,const char * el,const char ** attr)4234 static void startScreenXML(void *userData, const char *el, const char **attr) {
4235 
4236 	DataScreenXML* dataScreen = (DataScreenXML*)userData;
4237 	int i;
4238 
4239 	if (strcmp(el, "multilanguage") == 0) {
4240 		for (i = 0; attr[i]; i += 2) {
4241 			if ( (strcmp(attr[i], "value") == 0) && (strcmp(attr[i+1], "true") == 0) ) {
4242 				Board::GetInstance()->multiLanguage = true;
4243 			}
4244 		}
4245 	} else if (strcmp(el, "animation-end") == 0) {
4246 		for (i = 0; attr[i]; i += 2) {
4247 			if (strcmp(attr[i], "fromdata") == 0) {
4248 				Board::GetInstance()->getSceneGame()->setAnimationEnd(string(DATA_DIR) + "/" + attr[i+1]);
4249 			} else if (strcmp(attr[i], "fromlevel") == 0) {
4250 				Board::GetInstance()->getSceneGame()->setAnimationEnd(Missions::GetInstance()->currentLevel->getPath() + "/" + attr[i+1]);
4251 			}
4252 		}
4253 	} else if (strcmp(el, "help-first-level") == 0) {
4254 		for (i = 0; attr[i]; i += 2) {
4255 			if ( (strcmp(attr[i], "value") == 0) && (strcmp(attr[i+1], "true") == 0) ) {
4256 				Board::GetInstance()->getSceneGame()->createHelpFirstLevel();
4257 			}
4258 		}
4259 	} else if (strcmp(el, "player") == 0) {
4260 		for (i = 0; attr[i]; i += 2) {
4261 			if (strcmp(attr[i], "x") == 0) {
4262 				dataScreen->x = atoi(attr[i+1]);
4263 			} else if (strcmp(attr[i], "y") == 0) {
4264 				dataScreen->y = atoi(attr[i+1]);
4265 			}
4266 		}
4267 	} else if (strcmp(el, "enemy1") == 0) {
4268 		for (i = 0; attr[i]; i += 2) {
4269 			if (strcmp(attr[i], "x") == 0) {
4270 				dataScreen->x = atoi(attr[i+1]);
4271 			} else if (strcmp(attr[i], "y") == 0) {
4272 				dataScreen->y = atoi(attr[i+1]);
4273 			}
4274 		}
4275 	} else if (strcmp(el, "enemy2") == 0) {
4276 		for (i = 0; attr[i]; i += 2) {
4277 			if (strcmp(attr[i], "x") == 0) {
4278 				dataScreen->x = atoi(attr[i+1]);
4279 			} else if (strcmp(attr[i], "y") == 0) {
4280 				dataScreen->y = atoi(attr[i+1]);
4281 			}
4282 		}
4283 	} else if (strcmp(el, "enemy3") == 0) {
4284 		for (i = 0; attr[i]; i += 2) {
4285 			if (strcmp(attr[i], "x") == 0) {
4286 				dataScreen->x = atoi(attr[i+1]);
4287 			} else if (strcmp(attr[i], "y") == 0) {
4288 				dataScreen->y = atoi(attr[i+1]);
4289 			}
4290 		}
4291 	} else if (strcmp(el, "exit") == 0) {
4292 		for (i = 0; attr[i]; i += 2) {
4293 			if (strcmp(attr[i], "x") == 0) {
4294 				dataScreen->x = atoi(attr[i+1]);
4295 			} else if (strcmp(attr[i], "y") == 0) {
4296 				dataScreen->y = atoi(attr[i+1]);
4297 			}
4298 		}
4299 	} else if (strcmp(el, "blackhole0") == 0) {
4300 		for (i = 0; attr[i]; i += 2) {
4301 			if (strcmp(attr[i], "x") == 0) {
4302 				dataScreen->x = atoi(attr[i+1]);
4303 			} else if (strcmp(attr[i], "y") == 0) {
4304 				dataScreen->y = atoi(attr[i+1]);
4305 			}
4306 		}
4307 	} else if (strcmp(el, "blackhole1") == 0) {
4308 		for (i = 0; attr[i]; i += 2) {
4309 			if (strcmp(attr[i], "x") == 0) {
4310 				dataScreen->x = atoi(attr[i+1]);
4311 			} else if (strcmp(attr[i], "y") == 0) {
4312 				dataScreen->y = atoi(attr[i+1]);
4313 			}
4314 		}
4315 	} else if (strcmp(el, "key") == 0) {
4316 		for (i = 0; attr[i]; i += 2) {
4317 			if (strcmp(attr[i], "x") == 0) {
4318 				dataScreen->x = atoi(attr[i+1]);
4319 			} else if (strcmp(attr[i], "y") == 0) {
4320 				dataScreen->y = atoi(attr[i+1]);
4321 			}
4322 		}
4323 	} else if (strcmp(el, "trap") == 0) {
4324 		for (i = 0; attr[i]; i += 2) {
4325 			if (strcmp(attr[i], "x") == 0) {
4326 				dataScreen->x = atoi(attr[i+1]);
4327 			} else if (strcmp(attr[i], "y") == 0) {
4328 				dataScreen->y = atoi(attr[i+1]);
4329 			}
4330 		}
4331 	} else if (strcmp(el, "tray") == 0) {
4332 		for (i = 0; attr[i]; i += 2) {
4333 			if (strcmp(attr[i], "x") == 0) {
4334 				dataScreen->x = atoi(attr[i+1]);
4335 			} else if (strcmp(attr[i], "y") == 0) {
4336 				dataScreen->y = atoi(attr[i+1]);
4337 			}
4338 		}
4339 	} else if (strcmp(el, "door-key") == 0) {
4340 		for (i = 0; attr[i]; i += 2) {
4341 			if (strcmp(attr[i], "x") == 0) {
4342 				dataScreen->x = atoi(attr[i+1]);
4343 			} else if (strcmp(attr[i], "y") == 0) {
4344 				dataScreen->y = atoi(attr[i+1]);
4345 			} else if (strcmp(attr[i], "direction") == 0) {
4346 				dataScreen->direction = attr[i+1];
4347 			}
4348 		}
4349 	} else if (strcmp(el, "town") == 0) {
4350 		for (i = 0; attr[i]; i += 2) {
4351 			if (strcmp(attr[i], "name") == 0) {
4352 				Board::GetInstance()->town.name = attr[i+1];
4353 			} else if (strcmp(attr[i], "province") == 0) {
4354 				Board::GetInstance()->town.province = attr[i+1];
4355 			} else if (strcmp(attr[i], "nickname") == 0) {
4356 				Board::GetInstance()->town.nickname = attr[i+1];
4357 			}
4358 		}
4359 	} else if (strcmp(el, "help") == 0) {
4360 		for (i = 0; attr[i]; i += 2) {
4361 			if (strcmp(attr[i], "name") == 0) {
4362 				Board::GetInstance()->getSceneGame()->loadHelp(attr[i+1]);
4363 			}
4364 		}
4365 	} else if (strcmp(el, "music") == 0) {
4366 		for (i = 0; attr[i]; i += 2) {
4367 			if (strcmp(attr[i], "type") == 0) {
4368 				Board::GetInstance()->getSceneGame()->loadMusic(atoi(attr[i+1]));
4369 			}
4370 		}
4371 	} else if (strcmp(el, "info-tray") == 0) {
4372 		string titleTray="", textTray="", lang="";
4373 		for (i = 0; attr[i]; i += 2) {
4374 			if (strcmp(attr[i], "title") == 0) {
4375 				titleTray = attr[i+1];
4376 			} else if (strcmp(attr[i], "text") == 0) {
4377 				textTray = attr[i+1];
4378 			} else if (strcmp(attr[i], "lang") == 0) {
4379 				lang = attr[i+1];
4380 			}
4381 		}
4382 
4383 		bool todo =false;
4384 
4385 		if (Board::GetInstance()->multiLanguage) {
4386 			if (lang == Language::GetInstance()->getLang()) {
4387 				todo = true;
4388 			}
4389 		} else {
4390 			todo = true;
4391 		}
4392 		if (todo) {
4393 			Board::GetInstance()->infoTray.title = titleTray;
4394 			Board::GetInstance()->infoTray.text = textTray;
4395 			Board::GetInstance()->infoTray.lang = lang;
4396 		}
4397 
4398 	} else if (strcmp(el, "board") == 0) {
4399 		bool tb = false;
4400 		for (i = 0; attr[i]; i += 2) {
4401 			if ( (strcmp(attr[i], "type") == 0) && (strcmp(attr[i+1], "6x6") == 0) ) {
4402 				Board::GetInstance()->setTypeSize(BOARD_6X6);
4403 				tb = true;
4404 			}
4405 			if ( (strcmp(attr[i], "type") == 0) && (strcmp(attr[i+1], "7x7") == 0) ) {
4406 				Board::GetInstance()->setTypeSize(BOARD_7X7);
4407 				tb = true;
4408 			}
4409 			if ( (strcmp(attr[i], "type") == 0) && (strcmp(attr[i+1], "8x8") == 0) ) {
4410 				Board::GetInstance()->setTypeSize(BOARD_8X8);
4411 				tb = true;
4412 			}
4413 			if ( (strcmp(attr[i], "type") == 0) && (strcmp(attr[i+1], "9x9") == 0) ) {
4414 				Board::GetInstance()->setTypeSize(BOARD_9X9);
4415 				tb = true;
4416 			}
4417 			if ( (strcmp(attr[i], "type") == 0) && (strcmp(attr[i+1], "10x10") == 0) ) {
4418 				Board::GetInstance()->setTypeSize(BOARD_10X10);
4419 				tb = true;
4420 			}
4421 			if (strcmp(attr[i], "floor") == 0) {
4422 				Board::GetInstance()->setTypeFloor(atoi(attr[i+1]));
4423 			}
4424 			if (strcmp(attr[i], "wall") == 0) {
4425 				Board::GetInstance()->setTypeWall(atoi(attr[i+1]));
4426 			}
4427 			if (strcmp(attr[i], "pavement") == 0) {
4428 				Board::GetInstance()->setTypePavement(atoi(attr[i+1]));
4429 			}
4430 		}
4431 		if (!tb) Board::GetInstance()->setTypeSize(BOARD_6X6);
4432 	} else if (strcmp(el, "wall") == 0) {
4433 		for (i = 0; attr[i]; i += 2) {
4434 			if (strcmp(attr[i], "x") == 0) {
4435 				dataScreen->x = atoi(attr[i+1]);
4436 			} else if (strcmp(attr[i], "y") == 0) {
4437 				dataScreen->y = atoi(attr[i+1]);
4438 			} else if (strcmp(attr[i], "right") == 0) {
4439 				if (strcmp(attr[i+1], "true")==0) {
4440 					dataScreen->right = true;
4441 			  	}
4442 			} else if (strcmp(attr[i], "bottom") == 0) {
4443 				if (strcmp(attr[i+1], "true")==0) {
4444 					dataScreen->bottom = true;
4445 			  	}
4446 			}
4447 		}
4448 	} else if (strcmp(el, "scenary") == 0) {
4449 		int r = 255, g = 255, b = 255, ntiles = 0, ttiled = 0, tterrain = 0, twallscenary = 0;
4450 		for (i = 0; attr[i]; i += 2) {
4451 			if (strcmp(attr[i], "type") == 0) {
4452 				if (strcmp(attr[i+1], "opened") == 0) {
4453 					Board::GetInstance()->getScenary()->setType(SCENARY_OPENED);
4454 				} else {
4455 					Board::GetInstance()->getScenary()->setType(SCENARY_CLOSED);
4456 				}
4457 			}
4458 			if (strcmp(attr[i], "red") == 0) {
4459 				r = atoi(attr[i+1]);
4460 			}
4461 			if (strcmp(attr[i], "green") == 0) {
4462 				g = atoi(attr[i+1]);
4463 			}
4464 			if (strcmp(attr[i], "blue") == 0) {
4465 				b = atoi(attr[i+1]);
4466 			}
4467 			if (strcmp(attr[i], "ntiles") == 0) {
4468 				ntiles = atoi(attr[i+1]);
4469 			}
4470 			if (strcmp(attr[i], "type-tiled") == 0) {
4471 				ttiled = atoi(attr[i+1]);
4472 			}
4473 			if (strcmp(attr[i], "type-terrain") == 0) {
4474 				tterrain = atoi(attr[i+1]);
4475 			}
4476 			if (strcmp(attr[i], "type-wall") == 0) {
4477 				twallscenary = atoi(attr[i+1]);
4478 			}
4479 		}
4480 		if (Board::GetInstance()->getScenary()->getType() == SCENARY_CLOSED) {
4481 			Board::GetInstance()->getScenary()->setColorInWalls(r, g, b);
4482 			Board::GetInstance()->getScenary()->setNVerticalTiles(ntiles);
4483 			Board::GetInstance()->getScenary()->setTypeTiled(ttiled);
4484 			Board::GetInstance()->getScenary()->setTypeWall(twallscenary);
4485 		} else {
4486 			Board::GetInstance()->getScenary()->setNVerticalTiles(ntiles);
4487 			Board::GetInstance()->getScenary()->setTypeTiled(ttiled);
4488 			Board::GetInstance()->getScenary()->setTypeTerrain(tterrain);
4489         }
4490 	} else if (strcmp(el, "object") == 0) {
4491 		char name[32];
4492 		int x = 30;
4493 		int y = 0;
4494 		int type = TYPEOBJECT_3D;
4495 		float scale = 1.0;
4496 		bool flip = false;
4497 		int origen = ORIGENOBJECT_GAME;
4498 		string text = "";
4499 		string font = "";
4500 		int r = 255, g = 255, b = 255;
4501 		int align = ALIGN_CENTER;
4502 		for (i = 0; attr[i]; i += 2) {
4503 			if (strcmp(attr[i], "name") == 0) {
4504 				strcpy(name, attr[i+1]);
4505 			}
4506 			if (strcmp(attr[i], "x") == 0) {
4507 				x = atoi(attr[i+1]);
4508 			}
4509 			if (strcmp(attr[i], "y") == 0) {
4510 				y = atoi(attr[i+1]);
4511 			}
4512 			if (strcmp(attr[i], "scale") == 0) {
4513 				scale = atof(attr[i+1]);
4514 			}
4515 			if ( (strcmp(attr[i], "flip") == 0) && (strcmp(attr[i+1], "true") == 0) ) {
4516 				flip = true;
4517 			}
4518 			if ( (strcmp(attr[i], "origen") == 0) && (strcmp(attr[i+1], "personal") == 0) ) {
4519 				origen = ORIGENOBJECT_PERSONAL;
4520 			}
4521 			if (strcmp(attr[i], "text") == 0) {
4522 				text = attr[i+1];
4523 			}
4524 			if (strcmp(attr[i], "font") == 0) {
4525 				font = attr[i+1];
4526 			}
4527 			if (strcmp(attr[i], "align") == 0) {
4528 				align = atoi(attr[i+1]);
4529 			}
4530 			if (strcmp(attr[i], "r") == 0) {
4531 				r = atoi(attr[i+1]);
4532 			}
4533 			if (strcmp(attr[i], "g") == 0) {
4534 				g = atoi(attr[i+1]);
4535 			}
4536 			if (strcmp(attr[i], "b") == 0) {
4537 				b = atoi(attr[i+1]);
4538 			}
4539 			if (strcmp(attr[i], "type") == 0) {
4540 				if (strcmp(attr[i+1], "2d") == 0) {
4541 					type = TYPEOBJECT_2D;
4542 				} else if (strcmp(attr[i+1], "3d") == 0) {
4543 					type = TYPEOBJECT_3D;
4544 				} else if (strcmp(attr[i+1], "text") == 0) {
4545 					type = TYPEOBJECT_TEXT;
4546 				}
4547 			}
4548 		}
4549 		if (type == TYPEOBJECT_TEXT) {
4550 			Color c;
4551 			c.r = r;
4552 			c.g = g;
4553 			c.b = b;
4554 			Board::GetInstance()->getScenary()->addTextObject(text, font, align, c, x, y, scale, flip, false);
4555 		} else {
4556 			Board::GetInstance()->getScenary()->addObject(type, origen, name, x, y, scale, flip);
4557 		}
4558 	}
4559 
4560 }
4561 
endScreenXML(void * userData,const char * el)4562 static void endScreenXML(void *userData, const char *el)
4563 {
4564 	DataScreenXML* dataScreen = (DataScreenXML*)userData;
4565 
4566 	if (strcmp(el, "player") == 0) {
4567 		Board::GetInstance()->setPositionPlayer(dataScreen->x, dataScreen->y);
4568 		initDataScreenXML(dataScreen);
4569 	} else if (strcmp(el, "enemy1") == 0) {
4570 		Board::GetInstance()->setPositionEnemy1(dataScreen->x, dataScreen->y);
4571 		initDataScreenXML(dataScreen);
4572 	} else if (strcmp(el, "enemy2") == 0) {
4573 		Board::GetInstance()->setPositionEnemy2(dataScreen->x, dataScreen->y);
4574 		initDataScreenXML(dataScreen);
4575 	} else if (strcmp(el, "enemy3") == 0) {
4576 		Board::GetInstance()->setPositionEnemy3(dataScreen->x, dataScreen->y);
4577 		initDataScreenXML(dataScreen);
4578 	} else if (strcmp(el, "exit") == 0) {
4579 		Board::GetInstance()->setPositionExit(dataScreen->x, dataScreen->y);
4580 		initDataScreenXML(dataScreen);
4581 	} else if (strcmp(el, "blackhole0") == 0) {
4582 		Board::GetInstance()->setBlackHole0(dataScreen->x, dataScreen->y);
4583 		initDataScreenXML(dataScreen);
4584 	} else if (strcmp(el, "blackhole1") == 0) {
4585 		Board::GetInstance()->setBlackHole1(dataScreen->x, dataScreen->y);
4586 		initDataScreenXML(dataScreen);
4587 	} else if (strcmp(el, "key") == 0) {
4588 		Board::GetInstance()->setKey(dataScreen->x, dataScreen->y);
4589 		initDataScreenXML(dataScreen);
4590 	} else if (strcmp(el, "trap") == 0) {
4591 		Board::GetInstance()->setPositionTrap(dataScreen->x, dataScreen->y);
4592 		initDataScreenXML(dataScreen);
4593 	} else if (strcmp(el, "tray") == 0) {
4594 		Board::GetInstance()->setPositionTray(dataScreen->x, dataScreen->y);
4595 		initDataScreenXML(dataScreen);
4596 	} else if (strcmp(el, "door-key") == 0) {
4597 		Board::GetInstance()->setDoorKey(dataScreen->x, dataScreen->y, dataScreen->direction);
4598 		initDataScreenXML(dataScreen);
4599 	} else if (strcmp(el, "wall") == 0) {
4600 		if (dataScreen->bottom) {
4601 			Board::GetInstance()->setWallBottom( dataScreen->x, dataScreen->y, true);
4602 		}
4603 		if (dataScreen->right) {
4604 			Board::GetInstance()->setWallRight( dataScreen->x, dataScreen->y, true);
4605 		}
4606 		initDataScreenXML(dataScreen);
4607 	}
4608 
4609 
4610 }
4611 
parseScreenXML(char fileXML[256])4612 void parseScreenXML(char fileXML[256]) {
4613 	char buffer[8192];
4614 	int done;
4615 
4616 	DataScreenXML dataScreen;
4617 	initDataScreenXML(&dataScreen);
4618 
4619 	XML_Parser p = XML_ParserCreate(NULL);
4620 	if (! p) {
4621 		printf("Podra no tener suficiente memoria para el parser\n");
4622 	}
4623 
4624 	XML_SetUserData(p, &dataScreen);
4625 	XML_SetElementHandler(p, startScreenXML, endScreenXML);
4626 
4627   	FILE *file = fopen(fileXML, "r");
4628 	if(!file)
4629 		printf("Error abriendo archivo XML: %s\n", fileXML);
4630 
4631 	do
4632 	{
4633 		size_t len = fread(buffer, 1, sizeof(buffer), file);
4634 		done = len < sizeof(buffer);
4635 		//printf("%s\n", buffer);
4636 		if(!XML_Parse(p, buffer, len, done)){
4637 			printf("Error realizando el parse\n");
4638 		}
4639 			//parse_error(&data, XML_ErrorString(XML_GetErrorCode(data.parser)));
4640 	}
4641 	while(!done);
4642 	fclose(file);
4643 }
4644