From 5b0d6560685149e21afd62576767b5d1235396dd Mon Sep 17 00:00:00 2001 From: Nedko Boshkilov Date: Sat, 12 Apr 2014 21:14:58 +0300 Subject: [PATCH] Implementing Weapons Into Main --- source/baseprojectile.cpp | 76 ++++++--------------------------- source/main.cpp | 88 +++++++++++++++++++++++++++++++++------ source/weapons.cpp | 14 ++++--- source/weapons.h | 11 ++--- 4 files changed, 101 insertions(+), 88 deletions(-) diff --git a/source/baseprojectile.cpp b/source/baseprojectile.cpp index 5a2dca6..8725857 100644 --- a/source/baseprojectile.cpp +++ b/source/baseprojectile.cpp @@ -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)) { - yVel=((*it).y+(*it).h+(size_/2))-y; + hit_=true; } } 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(y-(size_/2)!=(*it).y+(*it).h) - { - 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; - } - } + hit_=true; } } - if((yVel==0)||(xVel==0)) + if((xVel==0)||(yVel==0)) { 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)) { - xVel=((*it).x-(size_/2))-x; + hit_=true; } } 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(y+(size_/2)!=(*it).y) - { - 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; - } - } + hit_=true; } } - if((yVel==0)||(xVel==0)) + if((xVel==0)||(yVel==0)) { 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)) { - yVel=((*it).y-(size_/2))-y; + hit_=true; } } 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(y+(size_/2)!=(*it).y) - { - 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; - } - } + hit_=true; } } - if((yVel==0)||(xVel==0)) + if((xVel==0)||(yVel==0)) { 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)) { - xVel=((*it).x+(*it).w+(size_/2))-x; + hit_=true; } } 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(y-(size_/2)!=(*it).y+(*it).h) - { - 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; - } - } + hit_=true; } } - if((yVel==0)||(xVel==0)) + if((xVel==0)||(yVel==0)) { hit_=true; } diff --git a/source/main.cpp b/source/main.cpp index 3f34df6..2e746f2 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -7,6 +7,7 @@ #include "entities.h" #include "projectiles.h" #include "mainfunctions.h" +#include "weapons.h" #include #include @@ -27,7 +28,11 @@ Mix_Chunk *bulletSound = NULL; SDL_Rect playerSlice[8]; Player playerObject; -std::vector bulletObjects; +Weapon primary; +Weapon secondary; +Weapon melee; + +std::vector projectiles; std::vector obstacles; bool init() @@ -143,6 +148,9 @@ bool load_files() Point temp2(map->w,map->h); 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) { @@ -169,10 +177,10 @@ void cleanup() SDL_FreeSurface(map); SDL_FreeSurface(player); - for(std::vector::iterator it=bulletObjects.begin();it!=bulletObjects.end();) + for(std::vector::iterator it=projectiles.begin();it!=projectiles.end();) { delete *it; - bulletObjects.erase(it); + projectiles.erase(it); } Mix_FreeChunk(bulletSound); TTF_CloseFont(font); @@ -185,11 +193,13 @@ int main(int argc, char* args[]) { bool quit=false; int moveDirection; + int currentWeapon=0; + bool fireSound=false; Uint8 *keyStates=SDL_GetKeyState(NULL); Uint32 moveTimer = 0; Uint32 fps = 0; - Uint32 shootTimer = 0; + Uint32 changeWeapon=0; if(init()==false) { @@ -209,6 +219,36 @@ int main(int argc, char* args[]) { 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) @@ -258,24 +298,45 @@ int main(int argc, char* args[]) moveDirection=-1; } } + if(keyStates[SDLK_ESCAPE]) { 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) { return 1; } - } + } } - SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00)); 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); - for(std::vector::iterator it=bulletObjects.begin();it!=bulletObjects.end();it++) + for(std::vector::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); if((*(*it)).hitScan()==true) { delete *it; - bulletObjects.erase(it); + projectiles.erase(it); it--; } } @@ -301,6 +362,7 @@ int main(int argc, char* args[]) { return 1; } + if((SDL_GetTicks()-fps)<(1000/FRAMES_PER_SECOND)) { SDL_Delay((1000/FRAMES_PER_SECOND)-(SDL_GetTicks()-fps)); diff --git a/source/weapons.cpp b/source/weapons.cpp index 81ac36d..c7cae32 100644 --- a/source/weapons.cpp +++ b/source/weapons.cpp @@ -9,10 +9,12 @@ void Weapon::init(int type, int pType, int pSpeed, int rof, int dmg, std::vector projectiles_ = projectiles; } -void Weapon::fire(Player player, Point map, int size, std::vector *obstacles) { - (*projectiles_).push_back(new BaseProjectile(player, damage_, map, size, projectileType_, obstacles, projectileSpeed_)); -} - -int Weapon::getRof() { - return rof_; +bool Weapon::fire(Player player, Point map, int size, std::vector *obstacles) { + if((SDL_GetTicks()-rofTimer_)>=(1000/rof_)) + { + rofTimer_=SDL_GetTicks(); + (*projectiles_).push_back(new BaseProjectile(player, damage_, map, size, projectileType_, obstacles, projectileSpeed_)); + return true; + } + return false; } diff --git a/source/weapons.h b/source/weapons.h index 43f32d9..c1496cd 100644 --- a/source/weapons.h +++ b/source/weapons.h @@ -4,6 +4,7 @@ #include "point.h" #include "projectiles.h" #include "entities.h" +#include "SDL/SDL.h" #include @@ -14,6 +15,7 @@ class Weapon { int rof_; int damage_; std::vector *projectiles_; + Uint32 rofTimer_; public: Weapon() @@ -22,12 +24,11 @@ public: projectileSpeed_(10), rof_(4), damage_(0), - projectiles_(NULL) + projectiles_(NULL), + rofTimer_(0) {} void init(int type, int pType, int pSpeed, int rof, int dmg, std::vector *projectiles); - void fire(Player player, Point map, int size, std::vector *obstacles); - int getRof(); -} - + bool fire(Player player, Point map, int size, std::vector *obstacles); +}; #endif