I recently started working on my 2d top down zombie shooter again. I am having some issues trying to get my collision between zombies to work correctly. The collision between zombies is handled in Main.cpp. I'm right now just trying to make sure that they both are hitting. Currently with this code, if 2 zombies overlap, then 1 of them has "HIT" above their head. However, I want both to have that, so I can move both of them after they collide. Here\'s my code:
Main.cpp
//ZOMBIE SHOOTER BY DAVID KRAMER
//WHEN ADDING MAP, ADD IMAGE IN setupGame() AND loadimages()!
#include "DarkGDK.h"
#include "Player.h"
#include "Zombie.h"
void DarkGDK ( void )
{
//======================FUNCTION PROTOTYPES===============//
void healthBar(float playerNumHealth);
void loadImages();
void loadSounds();
void setupGame();
//==========================================================//
//========================GAME SETUP========================//
//Call first!
setupGame();
//Zombie amount constant
const int ZOMBIE_AMOUNT = 6;
//Arrays
Zombie* zombArray = new Zombie[ZOMBIE_AMOUNT];
//Player object
Player Pone(1);
Pone.Xpos = 300;
Pone.Ypos = 300;
Pone.name = "Dave";
dbSetSpritePriority(Pone.spriteNum,2);
//Zombie set init positions
for(int i = 0; i <= ZOMBIE_AMOUNT; i++)
{
zombArray[i].Xpos = (rand() % dbScreenWidth());
zombArray[i].Ypos = (rand() % dbScreenHeight());
dbSetSpritePriority(zombArray[i].spriteNum,1);
}
//==========================================================//
while ( LoopGDK ( ) )
{
//Player controls
Pone.moveSprite();
//Zombie Sprite
for(int i = 0; i <= ZOMBIE_AMOUNT - 1; i++)
{
if (dbSpriteCollision(zombArray[i].spriteNum, Pone.spriteNum))
{
//Dec health and stop moving
if( Pone.health >= 0 )
{
Pone.health = Pone.health - .25;
}
//moan
zombArray[i].moan();
}
//Collision between 1 zombie against all other zombies
for(int k = 0; k <= ZOMBIE_AMOUNT - 1; k++)
{
if (dbSpriteCollision(zombArray[k].spriteNum, zombArray[i].spriteNum))
{
if( zombArray[k].spriteNum != zombArray[i].spriteNum )
{
zombArray[i].text = "HIT";
zombArray[k].text = "HIT";
/*
if( zombArray[i].Xpos >= zombArray[k].Xpos && zombArray[i].Ypos >= zombArray[k].Ypos )
{
zombArray[k].Xpos = zombArray[k].Xpos + zombArray[k].speed;
zombArray[k].Ypos = zombArray[k].Ypos + zombArray[k].speed;
}else if( zombArray[i].Xpos <= zombArray[k].Xpos && zombArray[i].Ypos <= zombArray[k].Ypos )
{
zombArray[k].Xpos = zombArray[k].Xpos - zombArray[k].speed;
zombArray[k].Ypos = zombArray[k].Ypos - zombArray[k].speed;
}else if ( zombArray[i].Xpos <= zombArray[k].Xpos && zombArray[i].Ypos >= zombArray[k].Ypos )
{
zombArray[k].Xpos = zombArray[k].Xpos - zombArray[k].speed;
zombArray[k].Ypos = zombArray[k].Ypos + zombArray[k].speed;
}else if ( zombArray[i].Xpos >= zombArray[k].Xpos && zombArray[i].Ypos <= zombArray[k].Ypos )
{
zombArray[k].Xpos = zombArray[k].Xpos + zombArray[k].speed;
zombArray[k].Ypos = zombArray[k].Ypos - zombArray[k].speed;
}
*/
}else{
zombArray[i].text = "";
zombArray[k].text = "";
}
}
}
zombArray[i].moveSprite(Pone.Xpos,Pone.Ypos);
}
//Health Bar
healthBar(Pone.health);
//Sync
dbSync();
}
return;
}
void healthBar(float playerNumHealth)
{
//Health bar function
dbBox(10,10,playerNumHealth,20,dbRGB(255,0,0),dbRGB(255,0,0),dbRGB(0,255,0),dbRGB(0,255,0));
dbText(35,25,dbStr(playerNumHealth));
dbText(10,25,"HP: ");
}
void loadImages()
{
//load images
dbLoadImage("images/playerbase.png",1);
dbLoadImage("images/zombiebase.png",2);
dbLoadImage("images/level1.png",3);
}
void loadSounds()
{
//load images
dbLoadSound("sounds/zombie groan 1.wav",1);
}
void setupGame()
{
//GDK setup
dbSyncOn ( );
dbSyncRate ( 60 );
dbRandomize ( dbTimer ( ) );
//dbHideMouse();
dbSetDisplayMode(800,600,32);
dbSetWindowSize(800,600);
dbSetWindowTitle("ZOMBIE SHOOTER");
//Draw Sprites First
dbDrawSpritesFirst();
//load the images
loadImages();
//load sounds
loadSounds();
//Background Image
dbSprite(2,0,0,3);
//Drawn first
//dbSetSpritePriority(2,0);
}
Zombie.cpp
#include "Zombie.h"
#include "DarkGDK.h"
Zombie::Zombie(void)
{
//Constructor. Instantiate all base numbers here like health
//Finding free sprite number
int i = 1;
do
{
i++;
}while(dbSpriteExist(i));
//A way around just using 2 for zombie image?
spriteNum = i;
zombieImage = 2;
dbSprite (spriteNum, 500, 500, 2);
dbOffsetSprite(spriteNum,dbSpriteWidth(spriteNum)/2,dbSpriteHeight(spriteNum)/2);
health = 100;
angle = (rand() % 360 + 1);
speed = (float)rand()/(float)RAND_MAX;
dbRotateSprite(spriteNum, angle);
dbSprite(spriteNum, Xpos, Ypos, zombieImage);
}
Zombie::~Zombie(void)
{
}
void Zombie::moveSprite(float playerx, float playery)
{
//Moves the zombies relative to player location
angle = dbAtanFull(dbSpriteX(spriteNum)-dbSpriteX(1),dbSpriteY(1)-dbSpriteY(spriteNum))-90;
dbRotateSprite(spriteNum, angle + 180);
if( playerx >= Xpos && playery >= Ypos )
{
Xpos = Xpos + speed;
Ypos = Ypos + speed;
}else if( playerx <= Xpos && playery <= Ypos )
{
Xpos = Xpos - speed;
Ypos = Ypos - speed;
}else if ( playerx <= Xpos && playery >= Ypos )
{
Xpos = Xpos - speed;
Ypos = Ypos + speed;
}else if ( playerx >= Xpos && playery <= Ypos )
{
Xpos = Xpos + speed;
Ypos = Ypos - speed;
}
dbSprite(spriteNum, Xpos, Ypos, zombieImage);
//Print text above sprite
dbText(Xpos,Ypos,text);
}
void Zombie::moan()
{
if(!dbSoundPlaying(1))
{
dbPlaySound(1,0);
}
}
// This function decides on what object the zombie should be moving to.
int Zombie::decideObjective()
{
return 1;
}
Player.cpp
#include "Player.h"
#include "DarkGDK.h"
Player::Player(int imageNum)
{
//Constructor. Instantiate all base numbers here like health
playerImage = imageNum;
spriteNum = 1;
dbSprite (1, 300, 250, imageNum);
dbOffsetSprite(1,dbSpriteWidth(1)/2,dbSpriteHeight(1)/2);
health = 200.0;
defaultShootingDistance = 0;
curLevel = 1;
speed = 1.5;
}
Player::~Player(void)
{
}
void Player::moveSprite()
{
//Movement for sprite
if (dbScanCode() == 17)
{
//w
Ypos -= speed;
}
if (dbScanCode() == 31)
{
//s
Ypos += speed;
}
if (dbScanCode() == 30)
{
//a
Xpos -= speed;
}
if (dbScanCode() == 32)
{
//d
Xpos += speed;
}
if (dbKeyState(17) && dbKeyState(30))
{
//w and a
Ypos -= speed/2;
Xpos -= speed/2;
}else if (dbKeyState(17) && dbKeyState(32))
{
//w and d
Ypos -= speed/2;
Xpos += speed/2;
}else if (dbKeyState(31) && dbKeyState(30))
{
//s and a
Ypos += speed/2;
Xpos -= speed/2;
}else if (dbKeyState(31) && dbKeyState(32))
{
//s and d
Ypos += speed/2;
Xpos += speed/2;
}
angle = dbAtanFull(Ypos - dbMouseY(), Xpos - dbMouseX());
dbRotateSprite(1, angle + 180);
dbSprite(1, Xpos, Ypos, playerImage);
//Player name
dbText(Xpos - (dbTextWidth(name) / 2),Ypos - (dbSpriteHeight(spriteNum) - dbTextHeight(name)) , name);
}
Any help is appreciated.
Image of what I'm talking about.
http://i51.tinypic.com/n6s3dk.jpg Those sprites are touching, but only one of them has the text above them.