Better collisions and entity handling

Signed-off-by: Pòsweg <posweg@gmail.com>
This commit is contained in:
Pòsweg 2017-05-21 20:30:17 +02:00
parent 349b8210ed
commit c03f5db6dd
8 changed files with 40 additions and 45 deletions

View File

@ -7,7 +7,6 @@ Block::Block(int x, int y, int w, int h,SDL_Renderer** render, Player* player, i
//Set the renderer pointer
renderer = render;
posu = player;
type = 1;
//New SDL_Rect to make the objects follow the camera
@ -18,18 +17,5 @@ Block::Block(int x, int y, int w, int h,SDL_Renderer** render, Player* player, i
SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF);
SDL_RenderFillRect(*renderer,&cameraFix);
posu->check(rect,type);
player->check(rect,type);
}
/*
void Block::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
SDL_SetRenderDrawColor(*renderer,0,0xFF,0,0xFF);
SDL_RenderFillRect(*renderer,&cameraFix);
posu->check(rect,type);
};*/

View File

@ -26,36 +26,53 @@ int Core::coreInit(){
bool quit = false;
SDL_Event e;
Player posweg(&gRenderer);
Powerup powerup(13*sz,SCREEN_HEIGHT-sz*4,&gRenderer,&posweg);
while(!quit){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
Camera camera(36*sz,SCREEN_WIDTH);
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
SDL_RenderClear(gRenderer);
SDL_RenderPresent(gRenderer);
}
map1();
close();
return 0;
};
void Core::map1(){
Player posweg(&gRenderer);
bool quit = false;
SDL_Event e;
while(quit == false){
while(SDL_PollEvent(&e)!=0){
if(e.type == SDL_QUIT){
quit = true;
}
}
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
SDL_RenderClear(gRenderer);
Camera camera(36*sz,SCREEN_WIDTH);
posweg.print(100,100,camera.getPosX());
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
Block wallA(8*sz,7*sz,sz*2,sz*2,&gRenderer,&posweg,camera.getPosX());
Block wallB(4*sz,9*sz,sz,sz*2,&gRenderer,&posweg,camera.getPosX());
Block wallC(6*sz,9*sz,sz,sz*2,&gRenderer,&posweg,camera.getPosX());
Block ground(0,11*sz,16*sz,sz,&gRenderer,&posweg,camera.getPosX());
Block ground2(sz*20,sz*11,sz*16,sz,&gRenderer,&posweg,camera.getPosX());
Powerup powerup(13*sz,8*sz,&gRenderer,&posweg,camera.getPosX());
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
SDL_RenderPresent(gRenderer);
}
close();
return 0;
};

4
core.h
View File

@ -17,6 +17,8 @@ class Core{
private:
void init();
void close();
void map1();
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
@ -24,6 +26,8 @@ class Core{
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//Player posweg(&gRenderer);
};
#endif

BIN
main

Binary file not shown.

View File

@ -24,7 +24,6 @@ void Player::print(int x,int y,int cameraX){
ply.render(&cameraFix,&plyRun);
power--;
}
if(ifRunning or power > 0)ply.render(&cameraFix,&plyFrame[1]);
move();
@ -41,6 +40,7 @@ Player::Player(SDL_Renderer** render){
ply.setRenderer(renderer);
loadMedia();
//first = true;
};
void Player::loadMedia(){
@ -163,8 +163,6 @@ int Player::check(SDL_Rect rectA, int type){
and ((movY > bY and movY < b2Y)
or (movY + szH > bY and movY + szH < b2Y)
or (movY < bY and movY + szH > b2Y))){
//movX += cos(angle) / 2;
//movY += sin(angle) / 2;
movX -= (posX - oldPosX)/2;
movY -= (posY - oldPosY)/2;
collision = 1;

View File

@ -2,28 +2,19 @@
#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};
Powerup::Powerup(int x, int y, SDL_Renderer** render,Player* player, int cameraX){
rect = {x,y,20,20};
renderer = render;
posu = player;
};
type = 2;
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){
if(player->check(rect,type)==0){
SDL_SetRenderDrawColor(*renderer,0xFF,0xFF,0,0xFF);
SDL_RenderFillRect(*renderer,&cameraFix);
}

View File

@ -12,8 +12,7 @@ class Entity;
class Powerup: public Entity{
public:
Powerup(int x, int y, SDL_Renderer** render, Player* player);
void print(int cameraX);
Powerup(int x, int y, SDL_Renderer** render, Player* player,int);
protected:
Player* posu;
};

0
texture.cpp Executable file → Normal file
View File