I'm probably terrible at all of this lol, but I would like some help. I want my zombies to just not run in to each other. I have the collision function returning correctly, I just don't know exactly how to position them so they move correctly with their angle so they don't overlap another zombie. Any help is appreciated
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();
int checkCollision(int sprite1, int sprite2);
//==========================================================//
//========================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;
dbSetSpritePriority(Pone.spriteNum,2);
//Zombie set init positions
for(int i = 0; i <= ZOMBIE_AMOUNT; i++)
{
zombArray[i].Xpos = rand() % dbScreenWidth() + 1;
zombArray[i].Ypos = rand() % dbScreenHeight() + 1;
dbSetSpritePriority(zombArray[i].spriteNum,1);
}
//==========================================================//
while ( LoopGDK ( ) )
{
//Zombie Sprite
for(int i = 0; i <= ZOMBIE_AMOUNT; i++)
{
zombArray[i].moveSprite(Pone.Xpos,Pone.Ypos);
zombArray[i].pasteSprite();
if (checkCollision(zombArray[i].spriteNum, Pone.spriteNum))
{
//Dec health and stop moving
Pone.health = Pone.health - .25;
zombArray[i].Xpos = zombArray[i].Xpos + (dbSin(zombArray[i].Xpos));
zombArray[i].Ypos = zombArray[i].Ypos + (dbCos(zombArray[i].Ypos));
//moan
zombArray[i].moan();
}
//Collision between 1 zombie against all other zombies
for(int k = 0; k <= ZOMBIE_AMOUNT; k++)
{
if (checkCollision(zombArray[i].spriteNum, zombArray[k].spriteNum))
{
zombArray[k].Xpos = zombArray[k].Xpos;
zombArray[k].Ypos = zombArray[k].Ypos;
}
}
}
//Paste and move player
Pone.pasteSprite();
Pone.moveSprite();
//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();
dbSetWindowSize(800,600);
dbSetWindowTitle("ZOMBIE SHOOTER");
//Draw Sprites First
dbDrawSpritesFirst();
//load the images
loadImages();
//load sounds
loadSounds();
dbSprite(2,0,0,3);
//Drawn first
dbSetSpritePriority(2,0);
}
//Collision between 2 sprites
int checkCollision(int sprite1, int sprite2)
{
if (dbSpriteCollision(sprite1,sprite2))
{
return 1;
}else
{
return 0;
}
}
zombie.h
#pragma once
class Zombie
{
private:
float angle;
public:
Zombie(int imageNum);
Zombie(void);
~Zombie(void);
int health;
void pasteSprite();
void moveSprite(float playerx, float playery);
void moan();
int zombieImage;
float Xpos;
float Ypos;
int spriteNum;
float speed;
};
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);
}
Zombie::~Zombie(void)
{
}
void Zombie::pasteSprite()
{
//Pastes the sprite and rotates it towards the player
angle = dbAtanFull(dbSpriteX(spriteNum)-dbSpriteX(1),dbSpriteY(1)-dbSpriteY(spriteNum))-90;
dbRotateSprite(spriteNum, angle);
dbSprite(spriteNum, Xpos, Ypos, zombieImage);
}
void Zombie::moveSprite(float playerx, float playery)
{
//Moves the zombies relative to player location
if (playerx > Xpos)
{
//Player = further left than zombie
//Move zombie left
Xpos += speed;
}
if (playerx < Xpos)
{
//Player = further right than zombie
//Move zombie right
Xpos -= speed;
}
if (playery < Ypos)
{
//Player = higher than zombie
//Move zombie up
Ypos -= speed;
}
if (playery > Ypos)
{
//Player = lower than zombie
//Move zombie down
Ypos += speed;
}
}
void Zombie::moan()
{
if(!dbSoundPlaying(1))
{
dbPlaySound(1,0);
}
}
player.h
#pragma once
class Player
{
private:
double speed;
float angle;
public:
Player(int ImageNum);
~Player(void);
void pasteSprite();
void moveSprite();
int playerImage;
float defaultShootingDistance;
float Xpos;
float Ypos;
float health;
int curLevel;
int spriteNum;
};
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;
}
Player::~Player(void)
{
}
void Player::pasteSprite()
{
angle = dbAtanFull(Ypos - dbMouseY(), Xpos - dbMouseX());
dbRotateSprite(1, angle);
dbSprite(1, Xpos, Ypos, playerImage);
}
void Player::moveSprite()
{
//Movement for sprite
speed = 2;
if (dbScanCode() == 17)
{
//w
Ypos -= speed;
pasteSprite();
}
if (dbScanCode() == 31)
{
//s
Ypos += speed;
pasteSprite();
}
if (dbScanCode() == 30)
{
//a
Xpos -= speed;
pasteSprite();
}
if (dbScanCode() == 32)
{
//d
Xpos += speed;
pasteSprite();
}
if (dbKeyState(17) && dbKeyState(30))
{
//w and a
Ypos -= speed/2;
Xpos -= speed/2;
pasteSprite();
}else if (dbKeyState(17) && dbKeyState(32))
{
//w and d
Ypos -= speed/2;
Xpos += speed/2;
pasteSprite();
}else if (dbKeyState(31) && dbKeyState(30))
{
//s and a
Ypos += speed/2;
Xpos -= speed/2;
pasteSprite();
}else if (dbKeyState(31) && dbKeyState(32))
{
//s and d
Ypos += speed/2;
Xpos += speed/2;
pasteSprite();
}
}