Implementing Weapons Into Main

This commit is contained in:
Nedko Boshkilov 2014-04-12 21:14:58 +03:00
parent 1faf1d0881
commit 5b0d656068
4 changed files with 101 additions and 88 deletions

View File

@ -18,7 +18,7 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
yVel=((*it).y+(*it).h+(size_/2))-y; hit_=true;
} }
} }
if(yVel==0) if(yVel==0)
@ -46,23 +46,10 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
if(y-(size_/2)!=(*it).y+(*it).h) hit_=true;
{
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x))
{
xVel=((*it).x-(size_/2))-x;
}
}
else if(x+(size_/2)!=(*it).x)
{
if((y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{
yVel=((*it).y+(*it).h+(size_/2))-y;
}
}
} }
} }
if((yVel==0)||(xVel==0)) if((xVel==0)||(yVel==0))
{ {
hit_=true; hit_=true;
} }
@ -83,7 +70,7 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
xVel=((*it).x-(size_/2))-x; hit_=true;
} }
} }
if(xVel==0) if(xVel==0)
@ -111,23 +98,10 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
if(y+(size_/2)!=(*it).y) hit_=true;
{
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x))
{
xVel=((*it).x-(size_/2))-x;
}
}
else if(x+(size_/2)!=(*it).x)
{
if((y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{
yVel=((*it).y-(size_/2))-y;
}
}
} }
} }
if((yVel==0)||(xVel==0)) if((xVel==0)||(yVel==0))
{ {
hit_=true; hit_=true;
} }
@ -148,7 +122,7 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
yVel=((*it).y-(size_/2))-y; hit_=true;
} }
} }
if(yVel==0) if(yVel==0)
@ -176,23 +150,10 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
if(y+(size_/2)!=(*it).y) hit_=true;
{
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x))
{
xVel=((*it).x+(*it).w+(size_/2))-x;
}
}
else if(x-(size_/2)!=(*it).x+(*it).w)
{
if((y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{
yVel=((*it).y-(size_/2))-y;
}
}
} }
} }
if((yVel==0)||(xVel==0)) if((xVel==0)||(yVel==0))
{ {
hit_=true; hit_=true;
} }
@ -213,7 +174,7 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
xVel=((*it).x+(*it).w+(size_/2))-x; hit_=true;
} }
} }
if(xVel==0) if(xVel==0)
@ -241,23 +202,10 @@ bool BaseProjectile::hitScan() {
{ {
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y)) if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x)&&(y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{ {
if(y-(size_/2)!=(*it).y+(*it).h) hit_=true;
{
if((x+xVel-(size_/2)<(*it).x+(*it).w)&&(x+xVel+(size_/2)>(*it).x))
{
xVel=((*it).x+(*it).w+(size_/2))-x;
}
}
else if(x-(size_/2)!=(*it).x+(*it).w)
{
if((y+yVel-(size_/2)<(*it).y+(*it).h)&&(y+yVel+(size_/2)>(*it).y))
{
yVel=((*it).y+(*it).h+(size_/2))-y;
}
}
} }
} }
if((yVel==0)||(xVel==0)) if((xVel==0)||(yVel==0))
{ {
hit_=true; hit_=true;
} }

View File

@ -7,6 +7,7 @@
#include "entities.h" #include "entities.h"
#include "projectiles.h" #include "projectiles.h"
#include "mainfunctions.h" #include "mainfunctions.h"
#include "weapons.h"
#include <vector> #include <vector>
#include <string> #include <string>
@ -27,7 +28,11 @@ Mix_Chunk *bulletSound = NULL;
SDL_Rect playerSlice[8]; SDL_Rect playerSlice[8];
Player playerObject; Player playerObject;
std::vector<BaseProjectile*> bulletObjects; Weapon primary;
Weapon secondary;
Weapon melee;
std::vector<BaseProjectile*> projectiles;
std::vector<SDL_Rect> obstacles; std::vector<SDL_Rect> obstacles;
bool init() bool init()
@ -143,6 +148,9 @@ bool load_files()
Point temp2(map->w,map->h); Point temp2(map->w,map->h);
playerObject.init(temp,playerSize,0,temp2,&obstacles,2); playerObject.init(temp,playerSize,0,temp2,&obstacles,2);
primary.init(0,0,5,4,0,&projectiles);
secondary.init(1,0,2,2,0,&projectiles);
melee.init(2,0,1,1,0,&projectiles);
if(bulletSound==NULL) if(bulletSound==NULL)
{ {
@ -169,10 +177,10 @@ void cleanup()
SDL_FreeSurface(map); SDL_FreeSurface(map);
SDL_FreeSurface(player); SDL_FreeSurface(player);
for(std::vector<BaseProjectile*>::iterator it=bulletObjects.begin();it!=bulletObjects.end();) for(std::vector<BaseProjectile*>::iterator it=projectiles.begin();it!=projectiles.end();)
{ {
delete *it; delete *it;
bulletObjects.erase(it); projectiles.erase(it);
} }
Mix_FreeChunk(bulletSound); Mix_FreeChunk(bulletSound);
TTF_CloseFont(font); TTF_CloseFont(font);
@ -185,11 +193,13 @@ int main(int argc, char* args[])
{ {
bool quit=false; bool quit=false;
int moveDirection; int moveDirection;
int currentWeapon=0;
bool fireSound=false;
Uint8 *keyStates=SDL_GetKeyState(NULL); Uint8 *keyStates=SDL_GetKeyState(NULL);
Uint32 moveTimer = 0; Uint32 moveTimer = 0;
Uint32 fps = 0; Uint32 fps = 0;
Uint32 shootTimer = 0; Uint32 changeWeapon=0;
if(init()==false) if(init()==false)
{ {
@ -209,6 +219,36 @@ int main(int argc, char* args[])
{ {
quit=true; quit=true;
} }
if(event.type==SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_1:
if(currentWeapon!=0)
{
changeWeapon=SDL_GetTicks();
}
currentWeapon=0;
break;
case SDLK_2:
if(currentWeapon!=1)
{
changeWeapon=SDL_GetTicks();
}
currentWeapon=1;
break;
case SDLK_3:
if(currentWeapon!=2)
{
changeWeapon=SDL_GetTicks();
}
currentWeapon=2;
break;
}
}
} }
if((SDL_GetTicks()-moveTimer)>=50) if((SDL_GetTicks()-moveTimer)>=50)
@ -258,24 +298,45 @@ int main(int argc, char* args[])
moveDirection=-1; moveDirection=-1;
} }
} }
if(keyStates[SDLK_ESCAPE]) if(keyStates[SDLK_ESCAPE])
{ {
quit=true; quit=true;
} }
if(SDL_GetTicks()-shootTimer>=250)
if(keyStates[SDLK_SPACE])
{ {
if(keyStates[SDLK_SPACE]) Point temp(map->w,map->h);
if(currentWeapon==0)
{
if(SDL_GetTicks()-changeWeapon>=500)
{
fireSound=primary.fire(playerObject,temp,bullet->w,&obstacles);
}
}
else if(currentWeapon==1)
{
if(SDL_GetTicks()-changeWeapon>=500)
{
fireSound=secondary.fire(playerObject,temp,bullet->w,&obstacles);
}
}
else if(currentWeapon==2)
{
if(SDL_GetTicks()-changeWeapon>=500)
{
fireSound=melee.fire(playerObject,temp,bullet->w,&obstacles);
}
}
if(fireSound==true)
{ {
shootTimer=SDL_GetTicks();
Point temp(map->w,map->h);
bulletObjects.push_back(new BaseProjectile(playerObject,0,temp,bullet->w,0,&obstacles,4));
if(Mix_PlayChannel(-1,bulletSound,0)==-1) if(Mix_PlayChannel(-1,bulletSound,0)==-1)
{ {
return 1; return 1;
} }
} }
} }
SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00)); SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));
if(moveDirection!=-1) if(moveDirection!=-1)
{ {
@ -284,13 +345,13 @@ int main(int argc, char* args[])
apply_surface(((SCREEN_WIDTH/2)-((playerObject.getPosition()).getX())),((SCREEN_HEIGHT/2)-((playerObject.getPosition()).getY())),map,screen); apply_surface(((SCREEN_WIDTH/2)-((playerObject.getPosition()).getX())),((SCREEN_HEIGHT/2)-((playerObject.getPosition()).getY())),map,screen);
for(std::vector<BaseProjectile*>::iterator it=bulletObjects.begin();it!=bulletObjects.end();it++) for(std::vector<BaseProjectile*>::iterator it=projectiles.begin();it!=projectiles.end();it++)
{ {
apply_surface((((SCREEN_WIDTH/2)-((playerObject.getPosition().getX())-((*(*it)).getPosition().getX())))-((*(*it)).getSize()/2)),(((SCREEN_HEIGHT/2)-((playerObject.getPosition().getY())-((*(*it)).getPosition().getY())))-((*(*it)).getSize()/2)),bullet,screen); apply_surface((((SCREEN_WIDTH/2)-((playerObject.getPosition().getX())-((*(*it)).getPosition().getX())))-((*(*it)).getSize()/2)),(((SCREEN_HEIGHT/2)-((playerObject.getPosition().getY())-((*(*it)).getPosition().getY())))-((*(*it)).getSize()/2)),bullet,screen);
if((*(*it)).hitScan()==true) if((*(*it)).hitScan()==true)
{ {
delete *it; delete *it;
bulletObjects.erase(it); projectiles.erase(it);
it--; it--;
} }
} }
@ -301,6 +362,7 @@ int main(int argc, char* args[])
{ {
return 1; return 1;
} }
if((SDL_GetTicks()-fps)<(1000/FRAMES_PER_SECOND)) if((SDL_GetTicks()-fps)<(1000/FRAMES_PER_SECOND))
{ {
SDL_Delay((1000/FRAMES_PER_SECOND)-(SDL_GetTicks()-fps)); SDL_Delay((1000/FRAMES_PER_SECOND)-(SDL_GetTicks()-fps));

View File

@ -9,10 +9,12 @@ void Weapon::init(int type, int pType, int pSpeed, int rof, int dmg, std::vector
projectiles_ = projectiles; projectiles_ = projectiles;
} }
void Weapon::fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles) { bool Weapon::fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles) {
(*projectiles_).push_back(new BaseProjectile(player, damage_, map, size, projectileType_, obstacles, projectileSpeed_)); if((SDL_GetTicks()-rofTimer_)>=(1000/rof_))
} {
rofTimer_=SDL_GetTicks();
int Weapon::getRof() { (*projectiles_).push_back(new BaseProjectile(player, damage_, map, size, projectileType_, obstacles, projectileSpeed_));
return rof_; return true;
}
return false;
} }

View File

@ -4,6 +4,7 @@
#include "point.h" #include "point.h"
#include "projectiles.h" #include "projectiles.h"
#include "entities.h" #include "entities.h"
#include "SDL/SDL.h"
#include <vector> #include <vector>
@ -14,6 +15,7 @@ class Weapon {
int rof_; int rof_;
int damage_; int damage_;
std::vector<BaseProjectile*> *projectiles_; std::vector<BaseProjectile*> *projectiles_;
Uint32 rofTimer_;
public: public:
Weapon() Weapon()
@ -22,12 +24,11 @@ public:
projectileSpeed_(10), projectileSpeed_(10),
rof_(4), rof_(4),
damage_(0), damage_(0),
projectiles_(NULL) projectiles_(NULL),
rofTimer_(0)
{} {}
void init(int type, int pType, int pSpeed, int rof, int dmg, std::vector<BaseProjectile*> *projectiles); void init(int type, int pType, int pSpeed, int rof, int dmg, std::vector<BaseProjectile*> *projectiles);
void fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles); bool fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles);
int getRof(); };
}
#endif #endif