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
|
*.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 -*-
|
#-*- 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++
|
CC = g++
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ OBJ_NAME = main
|
||||||
all: $(OBJS)
|
all: $(OBJS)
|
||||||
$(CC) $(OBJS) $(COMPLER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
$(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)
|
$(CC) -c main.cpp $(LINKER_FLAGS)
|
||||||
|
|
||||||
player.o: player.cpp player.h entity.o texture.o
|
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
|
powerup.o: powerup.cpp powerup.h
|
||||||
$(CC) -c powerup.cpp $(LINKER_FLAGS)
|
$(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{
|
class Block: public Entity{
|
||||||
public:
|
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);
|
void print(int cameraX);
|
||||||
protected:
|
protected:
|
||||||
Player* posu;
|
Player* posu;
|
||||||
|
|
2
camera.h
2
camera.h
|
@ -3,6 +3,8 @@
|
||||||
#ifndef __CAMERA_H_INCLUDED__
|
#ifndef __CAMERA_H_INCLUDED__
|
||||||
#define __CAMERA_H_INCLUDED__
|
#define __CAMERA_H_INCLUDED__
|
||||||
|
|
||||||
|
#include"player.h"
|
||||||
|
|
||||||
class Camera{
|
class Camera{
|
||||||
public:
|
public:
|
||||||
Camera(int mapWidth,int screenWidth);
|
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
|
//main file
|
||||||
|
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<SDL2/SDL.h>
|
#include"core.h"
|
||||||
#include"entity.h"
|
|
||||||
#include"player.h"
|
|
||||||
#include"block.h"
|
|
||||||
#include"powerup.h"
|
|
||||||
#include"camera.h"
|
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 640;
|
Core core;
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* args[]){
|
int main(int argc, char* args[]){
|
||||||
init();
|
//if(argc == 2) std::cout << "Initializing..." << std::endl;
|
||||||
|
|
||||||
bool quit = false;
|
core.coreInit();
|
||||||
SDL_Event e;
|
|
||||||
|
|
||||||
Camera camera(36*sz,SCREEN_WIDTH);
|
|
||||||
|
|
||||||
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){
|
|
||||||
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);
|
|
||||||
|
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
62
player.cpp
62
player.cpp
|
@ -2,43 +2,43 @@
|
||||||
|
|
||||||
#include"player.h"
|
#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;
|
SDL_Rect cameraFix = rect;
|
||||||
cameraFix.x -= cameraX;
|
cameraFix.x -= cameraX;
|
||||||
|
|
||||||
if(false){
|
|
||||||
std::cout << power << "ye" << std::endl;
|
|
||||||
power--;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
|
SDL_SetRenderDrawColor(*renderer,0xFF,0,0,0xFF);
|
||||||
if(power == 0)ply.render(&cameraFix,&plyFrame[0]);
|
if(power == 0)ply.render(&cameraFix,&plyFrame[0]);
|
||||||
else{
|
else{
|
||||||
ply.render(&cameraFix,&plyRun);
|
ply.render(&cameraFix,&plyRun);
|
||||||
power--;
|
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();
|
move();
|
||||||
ground = false;
|
ground = false;
|
||||||
topCollision = false;
|
topCollision = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
Player::Player(int x,int y, int w, int h, SDL_Renderer** render){
|
Player::Player(SDL_Renderer** render){
|
||||||
posX = x;
|
szW = 40;
|
||||||
posY = y;
|
szH = 40;
|
||||||
szW = w;
|
rect.w = szW;
|
||||||
szH = h;
|
rect.h = szH;
|
||||||
renderer = render;
|
renderer = render;
|
||||||
|
|
||||||
oldPosX = posX;
|
|
||||||
oldPosY = posY;
|
|
||||||
|
|
||||||
ply.setRenderer(renderer);
|
ply.setRenderer(renderer);
|
||||||
loadMedia();
|
loadMedia();
|
||||||
};
|
};
|
||||||
|
@ -67,7 +67,8 @@ int Player::intVelX(){
|
||||||
};
|
};
|
||||||
|
|
||||||
void Player::move(){
|
void Player::move(){
|
||||||
float dt = dTime.getDt();
|
//float dt = dTime.getDt();
|
||||||
|
float dt = 0.016;
|
||||||
|
|
||||||
//Set keyboard variable
|
//Set keyboard variable
|
||||||
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||||
|
@ -139,15 +140,12 @@ void Player::move(){
|
||||||
//Convert and set new int position
|
//Convert and set new int position
|
||||||
posX += static_cast<int>(x);
|
posX += static_cast<int>(x);
|
||||||
posY -= static_cast<int>(y+0.5);
|
posY -= static_cast<int>(y+0.5);
|
||||||
|
|
||||||
rect = {posX,posY,szW,szH};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int Player::check(SDL_Rect rectA, int type){
|
int Player::check(SDL_Rect rectA, int type){
|
||||||
//Initialize and reset collision type
|
//Initialize and reset collision type
|
||||||
int collision = 0;
|
int collision = 0;
|
||||||
|
|
||||||
|
|
||||||
//Set B rectangle variables
|
//Set B rectangle variables
|
||||||
int bX = rectA.x;
|
int bX = rectA.x;
|
||||||
int bY = rectA.y;
|
int bY = rectA.y;
|
||||||
|
@ -157,19 +155,18 @@ int Player::check(SDL_Rect rectA, int type){
|
||||||
//Float-ize the position
|
//Float-ize the position
|
||||||
float movX = posX, movY = posY;
|
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
|
//Move the player out of the rectangle
|
||||||
if(type == 1){
|
if(type == 1){
|
||||||
while(((movX >= bX and movX < b2X)
|
while(((movX >= bX and movX < b2X)
|
||||||
or (movX + szW > bX and movX + szW <= b2X)
|
or (movX + szW > bX and movX + szW <= b2X)
|
||||||
or (movX < bX and movX + szW > b2X))
|
or (movX < bX and movX + szW > b2X))
|
||||||
and ((movY >= bY and movY < b2Y)
|
and ((movY > bY and movY < b2Y)
|
||||||
or (movY + szH > bY and movY + szH <= b2Y)
|
or (movY + szH > bY and movY + szH < b2Y)
|
||||||
or (movY < bY and movY + szH > b2Y))){
|
or (movY < bY and movY + szH > b2Y))){
|
||||||
movX += cos(angle) / 2;
|
//movX += cos(angle) / 2;
|
||||||
movY += sin(angle) / 2;
|
//movY += sin(angle) / 2;
|
||||||
|
movX -= (posX - oldPosX)/2;
|
||||||
|
movY -= (posY - oldPosY)/2;
|
||||||
collision = 1;
|
collision = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,14 +190,16 @@ int Player::check(SDL_Rect rectA, int type){
|
||||||
or (movY < bY and movY + szH > b2Y)){
|
or (movY < bY and movY + szH > b2Y)){
|
||||||
while(movX + szW < bX){
|
while(movX + szW < bX){
|
||||||
movX++;
|
movX++;
|
||||||
|
velocityX = 0;
|
||||||
}
|
}
|
||||||
while(posX > b2X){
|
while(movX > b2X){
|
||||||
movX--;
|
movX--;
|
||||||
|
velocityX = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set and int-ize the position
|
//Set and int-ize the position*/
|
||||||
posX = static_cast<int>(movX);
|
posX = static_cast<int>(movX);
|
||||||
posY = static_cast<int>(movY);
|
posY = static_cast<int>(movY);
|
||||||
|
|
||||||
|
@ -214,6 +213,7 @@ int Player::check(SDL_Rect rectA, int type){
|
||||||
ground = true;
|
ground = true;
|
||||||
collision = 2;
|
collision = 2;
|
||||||
velocityY = 0;
|
velocityY = 0;
|
||||||
|
//posY -= 40;
|
||||||
}
|
}
|
||||||
//Bottom collision
|
//Bottom collision
|
||||||
else if(posY == b2Y){
|
else if(posY == b2Y){
|
||||||
|
|
5
player.h
5
player.h
|
@ -14,9 +14,9 @@ class Entity;
|
||||||
|
|
||||||
class Player: public Entity{
|
class Player: public Entity{
|
||||||
public:
|
public:
|
||||||
void print(int cameraX);
|
void print(int x, int y,int cameraX);
|
||||||
int check(SDL_Rect rectA,int type);
|
int check(SDL_Rect rectA,int type);
|
||||||
Player(int x, int y,int w, int h,SDL_Renderer** render);
|
Player(SDL_Renderer** render);
|
||||||
private:
|
private:
|
||||||
void loadMedia();
|
void loadMedia();
|
||||||
void move();
|
void move();
|
||||||
|
@ -31,6 +31,7 @@ class Player: public Entity{
|
||||||
bool isRunning = false;
|
bool isRunning = false;
|
||||||
bool ifRunning = false;
|
bool ifRunning = false;
|
||||||
int oldPosX, oldPosY;
|
int oldPosX, oldPosY;
|
||||||
|
bool first;
|
||||||
int power = 0;
|
int power = 0;
|
||||||
|
|
||||||
PosuTexture ply;
|
PosuTexture ply;
|
||||||
|
|
Loading…
Reference in New Issue