Implementing Weapons Into Main
This commit is contained in:
parent
1faf1d0881
commit
5b0d656068
@ -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;
|
||||
hit_=true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
@ -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;
|
||||
hit_=true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
@ -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;
|
||||
hit_=true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
@ -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;
|
||||
hit_=true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
@ -7,6 +7,7 @@
|
||||
#include "entities.h"
|
||||
#include "projectiles.h"
|
||||
#include "mainfunctions.h"
|
||||
#include "weapons.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@ -27,7 +28,11 @@ Mix_Chunk *bulletSound = NULL;
|
||||
SDL_Rect playerSlice[8];
|
||||
|
||||
Player playerObject;
|
||||
std::vector<BaseProjectile*> bulletObjects;
|
||||
Weapon primary;
|
||||
Weapon secondary;
|
||||
Weapon melee;
|
||||
|
||||
std::vector<BaseProjectile*> projectiles;
|
||||
std::vector<SDL_Rect> 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<BaseProjectile*>::iterator it=bulletObjects.begin();it!=bulletObjects.end();)
|
||||
for(std::vector<BaseProjectile*>::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])
|
||||
{
|
||||
shootTimer=SDL_GetTicks();
|
||||
Point temp(map->w,map->h);
|
||||
bulletObjects.push_back(new BaseProjectile(playerObject,0,temp,bullet->w,0,&obstacles,4));
|
||||
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)
|
||||
{
|
||||
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<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);
|
||||
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));
|
||||
|
||||
@ -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<SDL_Rect> *obstacles) {
|
||||
bool Weapon::fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles) {
|
||||
if((SDL_GetTicks()-rofTimer_)>=(1000/rof_))
|
||||
{
|
||||
rofTimer_=SDL_GetTicks();
|
||||
(*projectiles_).push_back(new BaseProjectile(player, damage_, map, size, projectileType_, obstacles, projectileSpeed_));
|
||||
return true;
|
||||
}
|
||||
|
||||
int Weapon::getRof() {
|
||||
return rof_;
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
#include "point.h"
|
||||
#include "projectiles.h"
|
||||
#include "entities.h"
|
||||
#include "SDL/SDL.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@ -14,6 +15,7 @@ class Weapon {
|
||||
int rof_;
|
||||
int damage_;
|
||||
std::vector<BaseProjectile*> *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<BaseProjectile*> *projectiles);
|
||||
void fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles);
|
||||
int getRof();
|
||||
}
|
||||
|
||||
bool fire(Player player, Point map, int size, std::vector<SDL_Rect> *obstacles);
|
||||
};
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user