Better collisions and entity handling
This commit is contained in:
parent
84b87d1a67
commit
349b8210ed
|
@ -1,2 +1,63 @@
|
|||
reated by https://www.gitignore.io/api/vim,c++,linux
|
||||
|
||||
### C++ ###
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.sw*
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
### Linux ###
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
### Vim ###
|
||||
# swap
|
||||
[._]*.s[a-v][a-z]
|
||||
[._]*.sw[a-p]
|
||||
[._]s[a-v][a-z]
|
||||
[._]sw[a-p]
|
||||
# session
|
||||
Session.vim
|
||||
# temporary
|
||||
.netrwhist
|
||||
# auto-generated tag files
|
||||
tags
|
||||
|
|
7
Makefile
7
Makefile
|
@ -1,6 +1,6 @@
|
|||
#-*- Makefile -*-
|
||||
|
||||
OBJS = main.o player.o dt.o block.o entity.o camera.o texture.o powerup.o
|
||||
OBJS = main.o player.o dt.o block.o entity.o camera.o texture.o powerup.o core.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
|
||||
main.o: main.cpp core.o
|
||||
$(CC) -c main.cpp $(LINKER_FLAGS)
|
||||
|
||||
player.o: player.cpp player.h entity.o texture.o
|
||||
|
@ -37,3 +37,6 @@ texture.o: texture.cpp texture.h
|
|||
|
||||
powerup.o: powerup.cpp powerup.h
|
||||
$(CC) -c powerup.cpp $(LINKER_FLAGS)
|
||||
|
||||
core.o: core.cpp core.h
|
||||
$(CC) -c core.cpp $(LINKER_FLAGS)
|
||||
|
|
2
block.h
2
block.h
|
@ -12,7 +12,7 @@ class Entity;
|
|||
|
||||
class Block: public Entity{
|
||||
public:
|
||||
Block(int x,int y,int w,int h,SDL_Renderer** render, Player* player);
|
||||
Block(int x,int y,int w,int h,SDL_Renderer** render, Player* player,int);
|
||||
void print(int cameraX);
|
||||
protected:
|
||||
Player* posu;
|
||||
|
|
2
camera.h
2
camera.h
|
@ -3,6 +3,8 @@
|
|||
#ifndef __CAMERA_H_INCLUDED__
|
||||
#define __CAMERA_H_INCLUDED__
|
||||
|
||||
#include"player.h"
|
||||
|
||||
class Camera{
|
||||
public:
|
||||
Camera(int mapWidth,int screenWidth);
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
//Core class body
|
||||
|
||||
#include"core.h"
|
||||
|
||||
void Core::init(){
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
|
||||
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
|
||||
|
||||
sz = SCREEN_WIDTH/16;
|
||||
}
|
||||
|
||||
void Core::close(){
|
||||
SDL_DestroyRenderer(gRenderer);
|
||||
SDL_DestroyWindow(gWindow);
|
||||
gWindow = NULL;
|
||||
gRenderer = NULL;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int Core::coreInit(){
|
||||
init();
|
||||
|
||||
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);
|
||||
|
||||
posweg.print(100,100,camera.getPosX());
|
||||
|
||||
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());
|
||||
|
||||
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
|
||||
|
||||
SDL_RenderPresent(gRenderer);
|
||||
}
|
||||
close();
|
||||
return 0;
|
||||
};
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
//Core class header
|
||||
|
||||
#ifndef __CORE_H_INCLUDED__
|
||||
#define __CORE_H_INCLUDED__
|
||||
|
||||
#include<iostream>
|
||||
#include<SDL2/SDL.h>
|
||||
#include"entity.h"
|
||||
#include"player.h"
|
||||
#include"block.h"
|
||||
#include"powerup.h"
|
||||
#include"camera.h"
|
||||
|
||||
class Core{
|
||||
public:
|
||||
int coreInit();
|
||||
private:
|
||||
void init();
|
||||
void close();
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
int sz;
|
||||
|
||||
SDL_Window* gWindow = NULL;
|
||||
SDL_Renderer* gRenderer = NULL;
|
||||
};
|
||||
|
||||
#endif
|
82
main.cpp
82
main.cpp
|
@ -1,88 +1,14 @@
|
|||
//main file
|
||||
|
||||
#include<iostream>
|
||||
#include<SDL2/SDL.h>
|
||||
#include"entity.h"
|
||||
#include"player.h"
|
||||
#include"block.h"
|
||||
#include"powerup.h"
|
||||
#include"camera.h"
|
||||
#include"core.h"
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
SDL_Window* gWindow = NULL;
|
||||
SDL_Renderer* gRenderer = NULL;
|
||||
|
||||
//12 sz of height and 16 of width
|
||||
const int sz = SCREEN_WIDTH/16;
|
||||
|
||||
void init();
|
||||
void close();
|
||||
|
||||
void init(){
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
gWindow = SDL_CreateWindow("Platform Test!",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,SDL_WINDOW_SHOWN);
|
||||
gRenderer = SDL_CreateRenderer(gWindow,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
SDL_SetRenderDrawColor(gRenderer,0xFF,0xFF,0xFF,0xFF);
|
||||
}
|
||||
|
||||
void close(){
|
||||
SDL_DestroyRenderer(gRenderer);
|
||||
SDL_DestroyWindow(gWindow);
|
||||
gWindow = NULL;
|
||||
gRenderer = NULL;
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
Core core;
|
||||
|
||||
int main(int argc, char* args[]){
|
||||
init();
|
||||
|
||||
bool quit = false;
|
||||
SDL_Event e;
|
||||
|
||||
Camera camera(36*sz,SCREEN_WIDTH);
|
||||
|
||||
Player posweg(100,100,sz,sz,&gRenderer);
|
||||
//if(argc == 2) std::cout << "Initializing..." << std::endl;
|
||||
|
||||
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){
|
||||
if(e.type == SDL_QUIT){
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
SDL_SetRenderDrawColor(gRenderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(gRenderer);
|
||||
|
||||
camera.update(posweg.getRectangle().x,posweg.getRectangle().y);
|
||||
core.coreInit();
|
||||
|
||||
posweg.print(camera.getPosX());
|
||||
|
||||
wallA.print(camera.getPosX());
|
||||
wallB.print(camera.getPosX());
|
||||
wallC.print(camera.getPosX());
|
||||
ground.print(camera.getPosX());
|
||||
ground2.print(camera.getPosX());
|
||||
powerup.print(camera.getPosX());
|
||||
|
||||
//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(ePwrUp);
|
||||
|
||||
SDL_RenderPresent(gRenderer);
|
||||
}
|
||||
close();
|
||||
return 0;
|
||||
}
|
||||
|
|
70
player.cpp
70
player.cpp
|
@ -2,43 +2,43 @@
|
|||
|
||||
#include"player.h"
|
||||
|
||||
void Player::print(int cameraX){
|
||||
void Player::print(int x,int y,int cameraX){
|
||||
if(first == true){
|
||||
first = false;
|
||||
posX = x;
|
||||
posY = y;
|
||||
}
|
||||
|
||||
oldPosX = posX;
|
||||
oldPosY = posY;
|
||||
|
||||
rect.x = posX;
|
||||
rect.y = posY;
|
||||
|
||||
SDL_Rect cameraFix = rect;
|
||||
cameraFix.x -= cameraX;
|
||||
|
||||
if(false){
|
||||
std::cout << power << "ye" << std::endl;
|
||||
power--;
|
||||
}
|
||||
|
||||
|
||||
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
|
||||
if(power == 0)ply.render(&cameraFix,&plyFrame[0]);
|
||||
else{
|
||||
ply.render(&cameraFix,&plyRun);
|
||||
power--;
|
||||
}
|
||||
//ply.render(currentFrame);
|
||||
if(ifRunning or power > 0)ply.render(&cameraFix,&plyFrame[1]);
|
||||
|
||||
if(ifRunning or power > 0)ply.render(&cameraFix,&plyFrame[1]);
|
||||
|
||||
oldPosX = posX;
|
||||
oldPosY = posY;
|
||||
move();
|
||||
ground = false;
|
||||
topCollision = false;
|
||||
};
|
||||
|
||||
Player::Player(int x,int y, int w, int h, SDL_Renderer** render){
|
||||
posX = x;
|
||||
posY = y;
|
||||
szW = w;
|
||||
szH = h;
|
||||
Player::Player(SDL_Renderer** render){
|
||||
szW = 40;
|
||||
szH = 40;
|
||||
rect.w = szW;
|
||||
rect.h = szH;
|
||||
renderer = render;
|
||||
|
||||
oldPosX = posX;
|
||||
oldPosY = posY;
|
||||
|
||||
ply.setRenderer(renderer);
|
||||
loadMedia();
|
||||
};
|
||||
|
@ -67,7 +67,8 @@ int Player::intVelX(){
|
|||
};
|
||||
|
||||
void Player::move(){
|
||||
float dt = dTime.getDt();
|
||||
//float dt = dTime.getDt();
|
||||
float dt = 0.016;
|
||||
|
||||
//Set keyboard variable
|
||||
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||
|
@ -139,14 +140,11 @@ void Player::move(){
|
|||
//Convert and set new int position
|
||||
posX += static_cast<int>(x);
|
||||
posY -= static_cast<int>(y+0.5);
|
||||
|
||||
rect = {posX,posY,szW,szH};
|
||||
};
|
||||
|
||||
int Player::check(SDL_Rect rectA, int type){
|
||||
//Initialize and reset collision type
|
||||
int collision = 0;
|
||||
|
||||
|
||||
//Set B rectangle variables
|
||||
int bX = rectA.x;
|
||||
|
@ -156,23 +154,22 @@ int Player::check(SDL_Rect rectA, int type){
|
|||
|
||||
//Float-ize the position
|
||||
float movX = posX, movY = posY;
|
||||
|
||||
//Clacule the opposite od the player direction angle
|
||||
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))
|
||||
and ((movY >= bY and movY < b2Y)
|
||||
or (movY + szH > bY and movY + szH <= b2Y)
|
||||
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 += cos(angle) / 2;
|
||||
//movY += sin(angle) / 2;
|
||||
movX -= (posX - oldPosX)/2;
|
||||
movY -= (posY - oldPosY)/2;
|
||||
collision = 1;
|
||||
}
|
||||
|
||||
|
||||
//Correct possible position issues
|
||||
if(collision == 1){
|
||||
//Vertical adjustement
|
||||
|
@ -193,17 +190,19 @@ int Player::check(SDL_Rect rectA, int type){
|
|||
or (movY < bY and movY + szH > b2Y)){
|
||||
while(movX + szW < bX){
|
||||
movX++;
|
||||
velocityX = 0;
|
||||
}
|
||||
while(posX > b2X){
|
||||
while(movX > b2X){
|
||||
movX--;
|
||||
velocityX = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Set and int-ize the position
|
||||
//Set and int-ize the position*/
|
||||
posX = static_cast<int>(movX);
|
||||
posY = static_cast<int>(movY);
|
||||
|
||||
|
||||
//Check collsion type and reset velocities
|
||||
//Vertical collisions
|
||||
if((posX >= bX and posX < b2X)
|
||||
|
@ -214,6 +213,7 @@ int Player::check(SDL_Rect rectA, int type){
|
|||
ground = true;
|
||||
collision = 2;
|
||||
velocityY = 0;
|
||||
//posY -= 40;
|
||||
}
|
||||
//Bottom collision
|
||||
else if(posY == b2Y){
|
||||
|
|
5
player.h
5
player.h
|
@ -14,9 +14,9 @@ class Entity;
|
|||
|
||||
class Player: public Entity{
|
||||
public:
|
||||
void print(int cameraX);
|
||||
void print(int x, int y,int cameraX);
|
||||
int check(SDL_Rect rectA,int type);
|
||||
Player(int x, int y,int w, int h,SDL_Renderer** render);
|
||||
Player(SDL_Renderer** render);
|
||||
private:
|
||||
void loadMedia();
|
||||
void move();
|
||||
|
@ -31,6 +31,7 @@ class Player: public Entity{
|
|||
bool isRunning = false;
|
||||
bool ifRunning = false;
|
||||
int oldPosX, oldPosY;
|
||||
bool first;
|
||||
int power = 0;
|
||||
|
||||
PosuTexture ply;
|
||||
|
|
Loading…
Reference in New Issue