Powerup prototype and "improved" collision hadling
This commit is contained in:
parent
1d85fd6e70
commit
880ce752d4
|
@ -5,3 +5,6 @@
|
|||
*.swk
|
||||
*.swl
|
||||
*.swm
|
||||
*.swi
|
||||
*.swh
|
||||
*.swj
|
||||
|
|
7
Makefile
7
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)
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
|
|
7
block.h
7
block.h
|
@ -6,15 +6,16 @@
|
|||
#include"entity.h"
|
||||
#include<SDL2/SDL.h>
|
||||
#include<iostream>
|
||||
#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
|
||||
|
|
5
dt.h
5
dt.h
|
@ -1,5 +1,8 @@
|
|||
//Delta time class header
|
||||
|
||||
#ifndef __DT_H_INCLUDED__
|
||||
#define __DT_H_INCLUDED__
|
||||
|
||||
#include<SDL2/SDL.h>
|
||||
|
||||
class DeltaTime{
|
||||
|
@ -9,3 +12,5 @@ class DeltaTime{
|
|||
private:
|
||||
float now, last, dt;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
5
entity.h
5
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
|
||||
|
|
26
main.cpp
26
main.cpp
|
@ -2,8 +2,10 @@
|
|||
|
||||
#include<iostream>
|
||||
#include<SDL2/SDL.h>
|
||||
#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);
|
||||
}
|
||||
|
|
26
player.cpp
26
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<int>(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;
|
||||
};
|
||||
|
|
7
player.h
7
player.h
|
@ -1,7 +1,7 @@
|
|||
//Player class header
|
||||
|
||||
#ifndef __PLAYER_H_INCLUDED__
|
||||
#define __PLAYER_H_INLCUDED__
|
||||
#define __PLAYER_H_INCLUDED__
|
||||
|
||||
#include<SDL2/SDL.h>
|
||||
#include<iostream>
|
||||
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
};
|
|
@ -0,0 +1,21 @@
|
|||
//Powerup class header
|
||||
|
||||
#ifndef __POWERUP_H_INCLUDED__
|
||||
#define __POWERUP_H_INCLUDED__
|
||||
|
||||
#include<SDL2/SDL.h>
|
||||
#include<iostream>
|
||||
#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
|
Loading…
Reference in New Issue