diff --git a/.gitignore b/.gitignore index fe736d0..a9e50e9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ *.swk *.swl *.swm +*.swi +*.swh +*.swj diff --git a/Makefile b/Makefile index a1c9692..425d00a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ #-*- Makefile -*- -OBJS = main.o player.o dt.o block.o entity.o camera.o texture.o +OBJS = main.o player.o dt.o block.o entity.o camera.o texture.o powerup.o CC = g++ @@ -14,7 +14,7 @@ OBJ_NAME = main all: $(OBJS) $(CC) $(OBJS) $(COMPLER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) -main.o: main.cpp texture.o +main.o: main.cpp $(CC) -c main.cpp $(LINKER_FLAGS) player.o: player.cpp player.h entity.o texture.o @@ -34,3 +34,6 @@ camera.o: camera.cpp camera.h texture.o: texture.cpp texture.h $(CC) -c -lSDL2_image texture.cpp + +powerup.o: powerup.cpp powerup.h + $(CC) -c powerup.cpp $(LINKER_FLAGS) diff --git a/block.cpp b/block.cpp index 9dbcad4..f39a6c4 100644 --- a/block.cpp +++ b/block.cpp @@ -1,11 +1,14 @@ #include"block.h" -Block::Block(int x, int y, int w, int h,SDL_Renderer** render){ +Block::Block(int x, int y, int w, int h,SDL_Renderer** render, Player* player){ //set the rectangle dimensions rect = {x,y,w,h}; //Set the renderer pointer renderer = render; + + posu = player; + type = 1; } void Block::print(int cameraX){ @@ -16,4 +19,6 @@ void Block::print(int cameraX){ //Set render color and render the rectangle SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF); SDL_RenderFillRect(*renderer,&cameraFix); + + posu->check(rect,type); }; diff --git a/block.h b/block.h index b09dc48..90757ad 100644 --- a/block.h +++ b/block.h @@ -6,15 +6,16 @@ #include"entity.h" #include #include +#include"player.h" class Entity; class Block: public Entity{ public: - Block(int x,int y,int w,int h,SDL_Renderer** render); + Block(int x,int y,int w,int h,SDL_Renderer** render, Player* player); void print(int cameraX); - private: - SDL_Renderer** renderer; + protected: + Player* posu; }; #endif diff --git a/dt.h b/dt.h index f91419b..e208992 100644 --- a/dt.h +++ b/dt.h @@ -1,5 +1,8 @@ //Delta time class header +#ifndef __DT_H_INCLUDED__ +#define __DT_H_INCLUDED__ + #include class DeltaTime{ @@ -9,3 +12,5 @@ class DeltaTime{ private: float now, last, dt; }; + +#endif diff --git a/entity.cpp b/entity.cpp index 0d102af..e11f5f8 100644 --- a/entity.cpp +++ b/entity.cpp @@ -1,8 +1,7 @@ -//Virtual base class for entities +//Base class for entities #include"entity.h" SDL_Rect Entity::getRectangle(){ - //std::cout << rect.w << std::endl; return rect; }; diff --git a/entity.h b/entity.h index feb6c57..908c207 100644 --- a/entity.h +++ b/entity.h @@ -1,4 +1,4 @@ -//Virutal base class for entities +//Base class for entities #ifndef __ENTITY_H_INCLUDED__ #define __ENTITY_H_INCLUDED__ @@ -11,8 +11,9 @@ class Entity{ SDL_Rect getRectangle(); protected: SDL_Rect rect; + SDL_Renderer** renderer; int posX, posY, szW, szH; - + int type; }; #endif diff --git a/main b/main index d97feb2..94d6ed8 100755 Binary files a/main and b/main differ diff --git a/main.cpp b/main.cpp index 4ab3f90..d9a878e 100644 --- a/main.cpp +++ b/main.cpp @@ -2,8 +2,10 @@ #include #include +#include"entity.h" #include"player.h" #include"block.h" +#include"powerup.h" #include"camera.h" const int SCREEN_WIDTH = 640; @@ -48,17 +50,16 @@ int main(int argc, char* args[]){ bool quit = false; SDL_Event e; - Player posweg; Camera camera(36*sz,SCREEN_WIDTH); - - Block wallA(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,&gRenderer); - Block wallB(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); - Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer); - Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); - Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer); - Block powerup(13*sz,SCREEN_HEIGHT-sz*4,20,20,&gRenderer); - posweg.init(100,100,sz,sz,&gRenderer); + Player posweg(100,100,sz,sz,&gRenderer); + + Block wallA(8*sz,SCREEN_HEIGHT-sz*5,sz*2,sz*2,&gRenderer,&posweg); + Block wallB(4*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer,&posweg); + Block wallC(6*sz,SCREEN_HEIGHT-sz*3,sz,sz*2,&gRenderer,&posweg); + Block ground(0,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer,&posweg); + Block ground2(SCREEN_WIDTH+sz*4,SCREEN_HEIGHT-sz,SCREEN_WIDTH,sz,&gRenderer,&posweg); + Powerup powerup(13*sz,SCREEN_HEIGHT-sz*4,&gRenderer,&posweg); while(!quit){ while(SDL_PollEvent(&e)!=0){ @@ -80,12 +81,13 @@ int main(int argc, char* args[]){ ground2.print(camera.getPosX()); powerup.print(camera.getPosX()); - posweg.check(wallA.getRectangle()); + //Entity *ePwrUp = &powerup; + /*posweg.check(wallA.getRectangle()); posweg.check(wallB.getRectangle()); posweg.check(wallC.getRectangle()); posweg.check(ground.getRectangle()); - posweg.check(ground2.getRectangle()); - posweg.check(powerup.getRectangle()); + posweg.check(ground2.getRectangle());*/ + //posweg.check(ePwrUp); SDL_RenderPresent(gRenderer); } diff --git a/player.cpp b/player.cpp index 3eb2a47..bbf2635 100644 --- a/player.cpp +++ b/player.cpp @@ -7,6 +7,11 @@ void Player::print(int cameraX){ SDL_Rect cameraFix = rect; cameraFix.x -= cameraX; + if(power > 0){ + std::cout << power << "ye" << std::endl; + power--; + } + SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF); ply.render(&cameraFix,currentFrame,renderer); if(ifRunning)ply.render(&cameraFix,&plyFrame[5],renderer); @@ -20,7 +25,7 @@ void Player::print(int cameraX){ topCollision = false; }; -void Player::init(int x,int y, int w, int h, SDL_Renderer** render){ +Player::Player(int x,int y, int w, int h, SDL_Renderer** render){ posX = x; posY = y; szW = w; @@ -111,7 +116,7 @@ void Player::move(){ isRunning = false; if(currentKeyStates[SDL_SCANCODE_SPACE] and !topCollision){ velocityY = jump; - if(run!=1){ + if(currentKeyStates[SDL_SCANCODE_LSHIFT]){ isRunning = true; } } @@ -134,9 +139,10 @@ void Player::move(){ rect = {posX,posY,szW,szH}; }; -int Player::check(SDL_Rect rectA){ +int Player::check(SDL_Rect rectA, int type){ //Initialize and reset collision type int collision = 0; + //Set B rectangle variables int bX = rectA.x; @@ -151,6 +157,7 @@ int Player::check(SDL_Rect rectA){ float angle = atan2(oldPosY - posY, oldPosX - posX); //Move the player out of the rectangle + if(type == 1){ while(((movX >= bX and movX < b2X) or (movX + szW > bX and movX + szW <= b2X) or (movX < bX and movX + szW > b2X)) @@ -233,4 +240,17 @@ int Player::check(SDL_Rect rectA){ velocityX -= static_cast(velocityX); } } + } + else if(type == 2){ + if(((movX >= bX and movX < b2X) + or (movX + szW > bX and movX + szW <= b2X) + or (movX < bX and movX + szW > b2X)) + and ((movY >= bY and movY < b2Y) + or (movY + szH > bY and movY + szH <= b2Y) + or (movY < bY and movY + szH > b2Y))){ + //power = 10 / dTime.getDt(); + power = 360; + } + } + return power; }; diff --git a/player.h b/player.h index d59f8ac..ca51e12 100644 --- a/player.h +++ b/player.h @@ -1,7 +1,7 @@ //Player class header #ifndef __PLAYER_H_INCLUDED__ -#define __PLAYER_H_INLCUDED__ +#define __PLAYER_H_INCLUDED__ #include #include @@ -15,8 +15,8 @@ class Entity; class Player: public Entity{ public: void print(int cameraX); - int check(SDL_Rect rectB); - void init(int x, int y,int w, int h,SDL_Renderer** render); + int check(SDL_Rect rectA,int type); + Player(int x, int y,int w, int h,SDL_Renderer** render); private: int* coll; bool loadMedia(); @@ -32,6 +32,7 @@ class Player: public Entity{ bool isRunning = false; bool ifRunning = false; int oldPosX, oldPosY; + int power = 0; PosuTexture ply; SDL_Rect* currentFrame; diff --git a/powerup.cpp b/powerup.cpp new file mode 100644 index 0000000..efc8ace --- /dev/null +++ b/powerup.cpp @@ -0,0 +1,31 @@ +//Powerup class body + +#include"powerup.h" + +Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player){ + posX = x; + posY = y; + szW = 20; + szH = 20; + + type = 2; + + rect = {posX, posY, szW, szH}; + + renderer = render; + + posu = player; +}; + +void Powerup::print(int cameraX){ + //New SDL_Rect to make the objects follow the camera + SDL_Rect cameraFix = rect; + cameraFix.x -= cameraX; + + //Set render color and render the rectangle + if(posu->check(rect,type)==0){ + SDL_SetRenderDrawColor(*renderer,0xFF,0xFF,0,0xFF); + SDL_RenderFillRect(*renderer,&cameraFix); + } + //posu->check(rect, type); +}; diff --git a/powerup.h b/powerup.h new file mode 100644 index 0000000..bebcb7c --- /dev/null +++ b/powerup.h @@ -0,0 +1,21 @@ +//Powerup class header + +#ifndef __POWERUP_H_INCLUDED__ +#define __POWERUP_H_INCLUDED__ + +#include +#include +#include"entity.h" +#include"player.h" + +class Entity; + +class Powerup: public Entity{ + public: + Powerup(int x, int y, SDL_Renderer** render, Player* player); + void print(int cameraX); + protected: + Player* posu; +}; + +#endif