File: HCLevel.cpp

package info (click to toggle)
holotz-castle 1.3.14-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 14,632 kB
  • ctags: 4,120
  • sloc: cpp: 21,634; makefile: 160
file content (897 lines) | stat: -rw-r--r-- 17,706 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/*
 * Holotz's Castle
 * Copyright (C) 2004 Juan Carlos Seijo Prez
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 59 
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Juan Carlos Seijo Prez
 * jacob@mainreactor.net
 */

/** Game level. Includes a map and its enemies.
 * @file    HCLevel.cpp
 * @author  Juan Carlos Seijo Prez
 * @date    29/05/2004
 * @version 0.0.1 - 29/05/2004 - First version
 */

#include <HCLevel.h>

#ifndef HC_DATA_DIR
#define HC_DATA_DIR "res/"
#endif

HCLevel::HCLevel()  : numEnemies(0), enemies(0), numRopes(0), 
											ropes(0), remainingObjects(0), numObjects(0), objects(0),
											scripted(false), narrative(0), wideMap(false), highMap(false),
											maxTime(1), timerFont(0)
{
}

bool HCLevel::Init()
{
	// Initializes the character
	character.Init(theme.MainChar(character.Subtype()), &map, ropes, numRopes);
	character.State(HCCS_STOP);

	// Creates an empty background
	bg.Destroy();
	if (!bg.Create(1, 1, 0))
	{
		fprintf(stderr, "Error creating empty level background.\n");
		return false;
	}

	// Loads sounds
	soundObjectAcquired.Destroy();
	musicEndLevel.Destroy();

	if (JApp::App()->SoundEnabled() && 
			(!soundObjectAcquired.LoadWave(HC_DATA_DIR "sound/HCObjectAcquired.wav") ||
			 !musicEndLevel.LoadWave(HC_DATA_DIR "sound/HCEndLevel.wav") ||
			 !soundExitUnlocked.LoadWave(HC_DATA_DIR "sound/HCExitUnlocked.wav")))
	{
		fprintf(stderr, "Error loading sounds, check installation.\n");
	}

	// Initializes the exit
	levelExit.Lock();
	levelExit.Init(&map, 100);
	
	// Initializes the timer
	levelTimer.Init(maxTime, timerFont);
	levelTimer.Pos(0, 0);

	return true;
}

s32 HCLevel::Update()
{
	s32 ret = 0;

	s32 i;
	
	// Updates the map
	map.Update();
	
	// Updates the ropes
	for (i = 0; i < numRopes; ++i)
	{
		ropes[i]->Update();
	}
	
	// Updates the character unless it's exiting the level
	if (!scripted)
	{
		switch (LevelExit()->State())
		{
		default:
		case HCEXITSTATE_LOCKED:
			{
				if (IsExitUnlocked())
				{
					LevelExit()->Unlock();
					
					if (JApp::App()->SoundEnabled())
					{
						soundExitUnlocked.Play();
					}
				}
				character.Update();
			}
			break;

		case HCEXITSTATE_UNLOCKED:
			{
				character.Update();
			
				// Checks if the character is in the exit sight and swallows it if so
				if (Character()->X() >= LevelExit()->X() && 
						Character()->X() <= LevelExit()->X1() && 
						Character()->Y() >= LevelExit()->Y() && 
						Character()->Y() <= LevelExit()->Y1())
				{
					// Time stops
					levelTimer.Pause();
					
					// Inside, swallow it!
					LevelExit()->Swallow(Character());

					if (JApp::App()->SoundEnabled())
					{
						musicEndLevel.Play();
					}

					for (i = 0; i < NumEnemies(); ++i)
					{
						Enemies()[i]->State(HCCS_DIE);
					}
				}
			}
			break;
		
		case HCEXITSTATE_SWALLOWED:
			{
				// Level achieved!!
				if (!JApp::App()->SoundEnabled() || !musicEndLevel.IsPlaying())
				{
					ret = 2;
				}
			}
			break;

		case HCEXITSTATE_SWALLOWING:
			break;
		}
	}
	else
	{
		character.Update();
	}
	
	// Updates the objects
	for (i = 0; i < numObjects; ++i)
	{
		objects[i]->Update();
	}

	// Checks for objects to pick up
	CheckObjects();

	// Positions the level according to the character position so it's always visible
	// but it can be overriden if any script exists
	spot = character.Pos();

	// Updates the enemies
	if (scripted)
	{
		for (i = 0; i < numEnemies; ++i)
		{
			// Updates the enemies as if they were normal characters
			enemies[i]->HCCharacter::Update();
		}
	}
	else
	{
		bool collide = false;
		
		for (i = 0; !collide && i < numEnemies; ++i)
		{
			enemies[i]->Update();

			if (HCPreferences::Prefs()->Difficulty() != HCPREFERENCES_TOY)
			{
				// Checks collisions with enemies, except in toy mode
				if (!collide && Collide(&character, enemies[i]))
				{
					character.State(HCCS_DIE);
					collide = true;
					ret = 1;
				}
			}
		}

		// Updates the time remaining, except in toy mode
		if (levelTimer.Update() <= 0)
		{
			if (HCPreferences::Prefs()->Difficulty() != HCPREFERENCES_TOY)
			{
				// Time up! (Except in toy mode)
				character.State(HCCS_DIE);
				ret = 1;
			}
		}
	}
	
	if (narrative)
	{
		narrative->Update();
	}

	if (!scripted)
	{
		levelExit.Update();
	}

	// Updates the level position according to the spot position
	s32 x = (s32)pos.x, y = (s32)pos.y;
	if (wideMap)
	{
		s32 w2 = JApp::App()->Width()/2;

		if (spot.x < (w2 - viewRadius))
		{
			// Spot outside viewing spot, moves towards the spot
			s32 vx = (s32)JMin((w2 - viewRadius - spot.x)/4, map.CellWidth());
					
			if (vx <= 1) vx = 1;
			
			x += vx;
		}
		else
		if (spot.x > (w2 + viewRadius))
		{
			// Spot outside viewing spot, moves towards the spot
			s32 vx = (s32)JMin((spot.x - (w2 + viewRadius))/4, map.CellWidth());
					
			if (vx <= 1) vx = 1;
			
			x -= vx;
		}
	}
	
	if (highMap)
	{
		s32 h2 = JApp::App()->Height()/2;

		if (spot.y < (h2 - viewRadius))
		{
			// Spot outside viewing spot, moves towards the spot
			s32 vy = (s32)JMin((h2 - viewRadius - spot.y)/5, map.CellHeight());
					
			if (vy <= 1) vy = 1;
			
			y += vy;
		}
		else
		if (spot.y > (h2 + viewRadius))
		{
			// Spot outside viewing spot, moves towards the spot
			s32 vy = (s32)JMin((spot.y - (h2 + viewRadius))/5, map.CellHeight());
					
			if (vy <= 1) vy = 1;
			
			y -= vy;
		}
	}
	
	if (wideMap || highMap)
	{
		Pos(x, y);
	}

	return ret;
}

void HCLevel::Draw()
{
	s32 i;

	// Draws the background
	bg.Draw();

	// Draws the map
	map.Draw();
	
	// Draws the objects
	for (i = 0; i < numObjects; ++i)
	{
		objects[i]->Draw();
	}

	// Draws the enemies
	for (i = 0; i < numEnemies; ++i)
	{
		enemies[i]->Draw();
	}
	
	// Draws the ropes
	for (i = 0; i < numRopes; ++i)
	{
		ropes[i]->Draw();
	}

	// Draws the main character
	character.Draw();

	if (narrative)
	{
		narrative->Draw();
	}

	if (!scripted)
	{
		levelExit.Draw();

		if (HCPreferences::Prefs()->Difficulty() != HCPREFERENCES_TOY)
		{
			// Draws the time remaining
			levelTimer.Draw();
		}

		// Draws the remaining objects to complete the level
		float qw = float(JApp::App()->Width())/4.0f, dx = qw/float(numObjects + 1);
		for (i = 0; i < numObjects; ++i)
		{
			objects[i]->Draw();
			
			if (objects[i]->State() == HCOBJECTSTATE_NORMAL)
			{
				((JImage *)objects[i]->Normal().Frame(0))->Draw(s32(qw * 3.0f + dx * float(i)), 0);
			}
		}
	}
}

u32 HCLevel::Load(JRW &file, const char *filename)
{
	Destroy();

	// Maximum time to complete the level, 0 means no timed
	if (0 == file.ReadLE32(&maxTime))
	{
		fprintf(stderr, "Error reading the level completion time.\n");
		perror("");

		return 1;
	}
	
	// Adjusts max time according to the level of difficulty (x1, x2, x3)
	maxTime *= HCPreferences::Prefs()->Difficulty();
	
	// Theme name
	JString str;
	if (0 != str.Load(file))
	{
		fprintf(stderr, "Error loading the theme name.\n");

		return 2;
	}

	// Loads the theme
	if (!theme.Load(str))
	{
		fprintf(stderr, "Error loading the theme %s.\n", theme.Name());

		return 2;
	}

	// Initializes and loads the map
	if (!map.Init(theme) ||
			0 != map.Load(file))
	{
		fprintf(stderr, "Error loading the level map.\n");

		return 2;
	}

	// Main character
	if (0 != character.Load(file))
	{
		fprintf(stderr, "Error loading main character.\n");

		return 1;
	}

	// Initializes enemie's maximum global veloccities
	HCEnemy::MaxXVeloccity(character.MaxVeloccity().x);
	HCEnemy::MaxYVeloccity(character.MaxVeloccity().y);
		
	// Loads the enemies
	if (0 == file.ReadLE32(&numEnemies))
	{
		fprintf(stderr, "Error reading the number of enemies.\n");

		return 1;
	}
	
	if (numEnemies > 0)
	{
		enemies = new HCEnemy *[numEnemies];
	
		s32 enemyType;

		for (s32 i = 0; i < numEnemies; ++i)
		{
			if (0 != file.ReadLE32(&enemyType))
			{
        // Lets the file at ist original position
        file.Seek(-4, SEEK_CUR);

				// Peeps the type of enemy
				switch (enemyType)
				{
				default:
				case HCENEMYTYPE_BALL:
					enemies[i] = new HCEnemyBall;
					break;

				case HCENEMYTYPE_RANDOM:
					enemies[i] = new HCEnemyRandom;
					break;

				case HCENEMYTYPE_STATIC:
					enemies[i] = new HCEnemyStatic;
					break;

				case HCENEMYTYPE_MAKER:
					enemies[i] = new HCEnemyMaker;
					break;

				case HCENEMYTYPE_CHASER:
					enemies[i] = new HCEnemyChaser;
					break;
				}
			
				if (0 != enemies[i]->Load(file, theme, &map))
				{
					Destroy();
				
					fprintf(stderr, "Error loading the enemies.\n");
					return 2;
				}
			}
		}
	}
	
	// Loads the objects
	if (0 == file.ReadLE32(&numObjects))
	{
		fprintf(stderr, "Error reading the number of objects.\n");

		return 1;
	}
	
	if (numObjects > 0)
	{
		remainingObjects = numObjects;
		objects = new HCObject *[numObjects];
	
		for (s32 i = 0; i < numObjects; ++i)
		{
			objects[i] = new HCObject;
			
			if (0 != objects[i]->Load(file))
			{
				Destroy();
				
				fprintf(stderr, "Error loading the objects.\n");
				return 2;
			}

			objects[i]->Init(&theme);
		}
	}

	// Loads the ropes
	if (0 == file.ReadLE32(&numRopes))
	{
		fprintf(stderr, "Error reading the number of ropes.\n");

		return 1;
	}
	
	if (numRopes > 0)
	{
		ropes = new HCRope *[numRopes];
	
		for (s32 i = 0; i < numRopes; ++i)
		{
			ropes[i] = new HCRope();

			if (0 != ropes[i]->Load(file, theme))
			{
				Destroy();
				
				fprintf(stderr, "Error loading the ropes.\n");
				return 2;
			}
		}
	}

	if (!Init())
	{
		fprintf(stderr, "Error initializing the level.\n");
		return 2;
	}

	// Checks if a background named as the file and ended in '.tga' exists
	str.Format("%s.tga", filename);
	
	if (JFile::Exists(str))
	{
		bg.Destroy();

		if (!bg.Load(str))
		{
			fprintf(stderr, "Error loading level background %s.\n", str.Str());
			return 1;
		}

		bg.Pos(0, 0);
	}
	else
	{
		// Creates an empty background
		if (!bg.Create(1, 1, 0))
		{
			fprintf(stderr, "Error creating empty level background.\n");
			return 2;
		}
	}

	// Checks for chaser enemies in the level
	for (s32 i = 0; i < numEnemies; ++i)
	{
		if (enemies[i]->Type() == HCENEMYTYPE_CHASER)
		{
			((HCEnemyChaser *)enemies[i])->Chase(&character);
		}
	}

	// Initializes the viewing parameters
	s32 x = 0, y = 0;
	viewRadius = 0;
	
	if (map.Width() > JApp::App()->Width())
	{
		wideMap = true;
		
		// Defaults the viewing square size to a quarter of the application width
		viewRadius = JApp::App()->Width()/8;
	}
	else
	{
		x = (JApp::App()->Width() - map.Width())/2;
		wideMap = false;
	}

	if (map.Height() > JApp::App()->Height())
	{
		highMap = true;

		// Defaults the viewing square size to a quarter of the application height or
		// the viewRadius, the greatest
		if (viewRadius != 0)
		{
			viewRadius = (s32)JMin(JApp::App()->Height()/6, viewRadius);
		}
		else
		{
			viewRadius = JApp::App()->Height()/6;
		}
	}
	else
	{
		y = (JApp::App()->Height() - map.Height())/2;
		highMap = false;
	}
	
	Pos(x, y);

	return 0;
}

u32 HCLevel::Save(JRW &file)
{
	s32 x, y;

	// Time to complete the level
	if (0 == file.WriteLE32(&maxTime))
	{
		fprintf(stderr, "Error writing the completion time.\n");

		return 1;
	}
	
	// Theme name
	JString str;
	str = theme.Name();
	if (0 != str.Save(file))
	{
		fprintf(stderr, "Error saving the theme name.\n");

		return 2;
	}

	if (0 != map.Save(file))
	{
		fprintf(stderr, "Error saving the level map.\n");

		return 2;
	}

	// Main character
	x = (s32)character.X();
	y = (s32)character.Y();
	character.Pos(x - (s32)map.X(), y - (s32)map.Y());

	if (0 != character.Save(file))
	{
		fprintf(stderr, "Error saving main character.\n");

		return 1;
	}
	
	character.Pos(x, y);
	
	// Stores the objects, enemies, ropes relative to the map top-left corner
	if (0 == file.WriteLE32(&numEnemies))
	{
		fprintf(stderr, "Error writing the number of enemies.\n");

		return 1;
	}
	
	for (s32 i = 0; i < numEnemies; ++i)
	{
		x = (s32)enemies[i]->X();
		y = (s32)enemies[i]->Y();
		enemies[i]->Pos(x - (s32)map.X(), y - (s32)map.Y());

		if (0 != enemies[i]->Save(file))
		{
			fprintf(stderr, "Error saving the enemies.\n");
			return 2;
		}
		
		enemies[i]->Pos(x, y);
	}

	if (0 == file.WriteLE32(&numObjects))
	{
		fprintf(stderr, "Error writing the number of objects.\n");

		return 1;
	}
	
	for (s32 i = 0; i < numObjects; ++i)
	{
		x = (s32)objects[i]->X();
		y = (s32)objects[i]->Y();
		objects[i]->Pos(x - (s32)map.X(), y - (s32)map.Y());

		if (0 != objects[i]->Save(file))
		{
			fprintf(stderr, "Error saving the objects.\n");
			return 2;
		}

		objects[i]->Pos(x, y);
	}

	if (0 == file.WriteLE32(&numRopes))
	{
		fprintf(stderr, "Error writing the number of ropes.\n");

		return 1;
	}
	
	for (s32 i = 0; i < numRopes; ++i)
	{
		x = (s32)ropes[i]->X();
		y = (s32)ropes[i]->Y();
		ropes[i]->Pos(x - (s32)map.X(), y - (s32)map.Y());

		if (0 != ropes[i]->Save(file))
		{
			fprintf(stderr, "Error saving the ropes.\n");
			return 2;
		}

		ropes[i]->Pos(x, y);
	}

	return 0;
}

void HCLevel::Pos(float xPos, float yPos)
{
	float xMap, yMap, xBg, yBg, dx, dy;
	float xMapOrg = map.X();
	float yMapOrg = map.Y();

	pos.x = xPos;
	pos.y = yPos;

	if (map.Width() > bg.Width())
	{
		xMap = xPos;
		xBg = xPos + ((map.Width() - bg.Width())/2);
	}
	else
	{
		xBg = xPos;
		xMap = xPos + ((bg.Width() - map.Width())/2);
	}
		
	if (map.Height() > bg.Height())
	{
		yMap = yPos;
		yBg = yPos + ((map.Height() - bg.Height())/2);
	}
	else
	{
		yBg = yPos;
		yMap = yPos + ((bg.Height() - map.Height())/2);
	}

	map.Pos(xMap, yMap);
	bg.Pos(xBg, yBg);

	dx = xMap - xMapOrg;
	dy = yMap - yMapOrg;

	// Now move the objects
	for (s32 i = 0; i < numObjects; ++i)
	{
		
		objects[i]->Pos(objects[i]->X() + (s32)dx, 
										objects[i]->Y() + (s32)dy);
	}

	// Now move the ropes
	for (s32 i = 0; i < numRopes; ++i)
	{
		ropes[i]->Pos(ropes[i]->X() + (s32)dx, 
									ropes[i]->Y() + (s32)dy);
	}

	// Now move the enemies
	for (s32 i = 0; i < numEnemies; ++i)
	{
		enemies[i]->Pos(enemies[i]->X() + (s32)dx, 
										enemies[i]->Y() + (s32)dy);
	}
	
	// Move the level exit
	levelExit.Pos(levelExit.X() + (s32)dx, (s32)levelExit.Y() + (s32)dy);

	// And finally the main character
	character.Pos(character.X() + (s32)dx,
								character.Y() + (s32)dy);
}

void HCLevel::CheckObjects()
{
	JVector vc, vo;
	vc = character.Pos();

	// Y Offset of the center of the character
	JImage * img = (JImage *)character.CurSprite()->Frame(character.CurSprite()->CurFrame());
	vc.y += img->Pos().y + (img->Width()/2);

	for (s32 i = 0; i < numObjects; ++i)
	{
		if (objects[i]->State() == HCOBJECTSTATE_NORMAL)
		{
			vo = objects[i]->Pos();
			
			// Y Offset of the center of the character
			img = (JImage *)objects[i]->Normal().Frame(objects[i]->Normal().CurFrame());
			vo.y += img->Pos().y + (img->Width()/2);

			if (vo == vc)
			{
				// 0 length case
				ObjectAcquired(objects[i]);

				if (JApp::App()->SoundEnabled())
				{
					soundObjectAcquired.Play();
				}
			}
			else
			{
				if ((vo - vc).Length() < (0.9f * character.CurSprite()->MaxW()))
				{
					ObjectAcquired(objects[i]);

					if (JApp::App()->SoundEnabled())
					{
						soundObjectAcquired.Play();
					}
				}
			}
		}
	}
}

void HCLevel::ObjectAcquired(HCObject *object)
{
	if (object)
	{
		--remainingObjects;

		object->Acquire();
	}
}

bool HCLevel::Collide(HCCharacter *c1, HCCharacter *c2)
{
	float px1, py1, px2, py2, pw, ph;
	float ex1, ey1, ex2, ey2, ew, eh;
	JImage *cfp, *cfe;
	float difficulty = 0.75;
	cfp = (JImage *)c1->CurSprite()->Frame(c1->CurSprite()->CurFrame());
	cfe = (JImage *)c2->CurSprite()->Frame(c2->CurSprite()->CurFrame());

	pw = cfp->Width() * difficulty;
	ph = cfp->Height() * difficulty;
	px1 = c1->CurSprite()->Pos().x + cfp->X() + (cfp->Width()/2) - (pw/2);
	px2 = px1 + pw;
	py1 = c1->CurSprite()->Pos().y + cfp->Y() + (cfp->Height()/2) - (ph/2);
	py2 = py1 + ph;
	
	ew = cfe->Width() * difficulty;
	eh = cfe->Height() * difficulty;
	ex1 = c2->CurSprite()->Pos().x + cfe->X() + (cfe->Width()/2) - (ew/2);
	ex2 = ex1 + ew;
	ey1 = c2->CurSprite()->Pos().y + cfe->Y() + (cfe->Height()/2) - (eh/2);
	ey2 = ey1 + eh;

// 	SDL_Rect rcp = {px1, py1, pw, ph};
// 	SDL_Rect rce = {ex1, ey1, ew, eh};
// 	SDL_FillRect(SDL_GetVideoSurface(), &rce, 0xffffffff);
// 	SDL_FillRect(SDL_GetVideoSurface(), &rcp, 0xffffffff);
// 	SDL_Flip(SDL_GetVideoSurface());

	// Approximate collisions using half the width and height, centered of the characters
	if ((px1 > ex1 && px1 < ex2) ||
			(px2 > ex1 && px2 < ex2) ||
			(px1 < ex1 && px2 > ex2) ||
			(px1 > ex1 && px2 < ex2))
	{
		if ((py1 > ey1 && py1 < ey2) ||
				(py2 > ey1 && py2 < ey2) ||
				(py1 < ey1 && py2 > ey2) ||
				(py1 > ey1 && py2 < ey2))
		{
			// Collide!
			return true;
		}
	}
	
	return false;
}

void HCLevel::Destroy()
{
	JDELETE_POINTER_ARRAY(enemies, numEnemies);
	numEnemies = 0;
	JDELETE_POINTER_ARRAY(ropes, numRopes);
	numRopes = 0;
	JDELETE_POINTER_ARRAY(objects, numObjects);
	numObjects = 0;
	map.Destroy();
	theme.Destroy();
	levelExit.Destroy();
	bg.Destroy();
	character.Destroy();
	narrative = 0;
	levelTimer.Destroy();
	soundObjectAcquired.Destroy();
	musicEndLevel.Destroy();
}