Jump to content


Eielef

Member Since 04 Dec 2012
Offline Last Active Dec 15 2012 04:56 PM
-----

Topics I've Started

Help with std::deque list

04 December 2012 - 05:30 PM

Hi everyone! I'm programming a top down space shooter with Allegro 5 in C++, and in the game class I have two deques, one for the enemies and other for the weapons. I also have a function called HandleCollisions(), who takes the two deques, and check the collisions between them. Well, it doesn't work much, so I'm pretty sure I did something wrong accesing the lists. Here is the code for the function:
void GS_Juego::HandleCollisions() {
unsigned i=0,j=0;
//Manejar colisión con armas de la nave
while(i< EnemyQueue.size()) {
  while(j<WeaponsQueue.size()) {
   if(EnemyQueue[i]->CheckCollision(WeaponsQueue[j])) {
	//crear explosión en (GetX(),GetY())   <-----¡¡¡EXPLOSIóN AQUÍ!!!
	delete EnemyQueue[i];
	delete WeaponsQueue[j];
	EnemyQueue.erase(EnemyQueue.begin()+i);
	WeaponsQueue.erase(WeaponsQueue.begin()+j);
   }
   else {
	j++;
   }
  }
  i++;
}
i=0;
//Manejar colisión con nave
while(i<EnemyQueue.size()) {
  if(EnemyQueue[i]->CheckCollision(Player1)) {
   //crear explosión en (GetX(),GetY())   <-----¡¡¡EXPLOSIóN AQUÍ!!!
   EnemyQueue.erase(EnemyQueue.begin()+i);
   //Player1->addLives(-1);
   //DESTRUIR ENEMIGOS
   while(!EnemyQueue.empty()){
	//crear explosion en (Get(),GetY()) <<---¡¡AquÍ otra!!
	EnemyQueue.erase(EnemyQueue.begin());
   }
  }
  i++;
}
}

This is the header with the declaration of the function and the two deques in black:
#ifndef GAMESTATE_JUEGO
#define GAMESTATE_JUEGO
#include "c_gamestate.h"

#include <allegro5\allegro5.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_image.h>
#include <allegro5\allegro_audio.h>
#include <allegro5\allegro_acodec.h>
#include "Constants.h"

#include "EnemyChaser.h"
#include "Player.h"
#include "WeaponLaser.h"

#include <deque>
using namespace std;

class GS_Juego :
	public C_GameState
{
public:
	void Init();
	void Run();
	void Shutdown();
	GS_Juego(ALLEGRO_DISPLAY *display);
private:
	void drawBackground();
	void HandleEvents();
	[b]void HandleCollisions();[/b]
	void UpdateElements();
	void RenderElements();
	void ShootWeapon();
	void SpawnEnemy(int EnemyType);
	int create_random_number(const int min, const int max );

	ALLEGRO_DISPLAY *display;
	ALLEGRO_BITMAP *b_background, *b_enemychaser, *b_enemychaser_mask, *b_player1, *b_rotationBuffer, *b_laser;
	ALLEGRO_SAMPLE *s_music;
	ALLEGRO_EVENT_QUEUE *event_queue;
	ALLEGRO_EVENT evento;
	ALLEGRO_TIMER *timer;
	//Background Effect
	int background_Y;
	//THINGS
	bool done;
	bool redraw;
	bool calculateLogic;
	bool keys[NUM_OF_KEYS];
	int tc_weaponsDelay;
	int SelectedWeapon;

[b]	deque<C_Enemy*> EnemyQueue;
	deque<C_Weapons*> WeaponsQueue;[/b]

	Player *Player1;
};
#endif


I'd really aprecciate any feedback at all, because as far as I know, it should work perfectly, and I'm hitting my head against the wall repeatedly trying to find a solution. I checked the function collision and it works fine. The problem is that when it detects a collision with a bullet that it's not the first in the queue the program crashes, and if there is more than one enemy in the screen, if it's not the first one in the queue and I try to hit it with a bullet, it doesn't detect the collision. I'm sure the solution it's super silly, but I just can't find it. Please help! :-S