Better map handling
This commit is contained in:
parent
fd18bb5df8
commit
4debeb7b3a
Binary file not shown.
|
@ -1,13 +1,15 @@
|
||||||
#include"block.h"
|
#include"block.h"
|
||||||
|
|
||||||
Block::Block(SDL_Renderer* render, Player* ply){
|
Block::Block(int x,int y,int w,int h,SDL_Renderer* render, Player* ply){
|
||||||
renderer = render; //Set the renderer
|
renderer = render; //Set the renderer
|
||||||
player = ply; //Set the player class to check
|
player = ply; //Set the player class to check
|
||||||
|
|
||||||
type = 1; //Set the entity type (1 = block)
|
type = 1; //Set the entity type (1 = block)
|
||||||
|
|
||||||
|
rect = {x,y,w,h};
|
||||||
};
|
};
|
||||||
|
|
||||||
void Block::printAndCheck(int dimensions[], int cameraX){
|
/*void Block::printAndCheck(int dimensions[], int cameraX){
|
||||||
//Set the block's rect's dimensions and position
|
//Set the block's rect's dimensions and position
|
||||||
rect = {dimensions[0],dimensions[1],dimensions[2],dimensions[3]};
|
rect = {dimensions[0],dimensions[1],dimensions[2],dimensions[3]};
|
||||||
|
|
||||||
|
@ -20,4 +22,18 @@ void Block::printAndCheck(int dimensions[], int cameraX){
|
||||||
//Set render color and render the rectangle
|
//Set render color and render the rectangle
|
||||||
SDL_SetRenderDrawColor(renderer,169,145,73,0xFF);
|
SDL_SetRenderDrawColor(renderer,169,145,73,0xFF);
|
||||||
SDL_RenderFillRect(renderer,&rect);
|
SDL_RenderFillRect(renderer,&rect);
|
||||||
|
};*/
|
||||||
|
|
||||||
|
void Block::printAndCheck(int cameraX){
|
||||||
|
//Check if it collides with the player
|
||||||
|
player->check(rect,type);
|
||||||
|
|
||||||
|
//Adjust de SDL_Rect x to follow the camera
|
||||||
|
rect.x -= cameraX;
|
||||||
|
|
||||||
|
//Set render color and render the rectangle
|
||||||
|
/*SDL_SetRenderDrawColor(renderer,0,0xFF,0,0xFF);
|
||||||
|
SDL_RenderDrawRect(renderer,&rect);*/
|
||||||
|
|
||||||
|
rect.x += cameraX;
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,8 +12,9 @@ class Entity;
|
||||||
|
|
||||||
class Block: public Entity{
|
class Block: public Entity{
|
||||||
public:
|
public:
|
||||||
Block(SDL_Renderer* render, Player* ply);
|
Block(int x,int y,int w,int h,SDL_Renderer* render, Player* ply);
|
||||||
void printAndCheck(int dimensions[], int cameraX);
|
//Block(SDL_Renderer* render, Player* ply);
|
||||||
|
void printAndCheck(int cameraX);
|
||||||
protected:
|
protected:
|
||||||
Player* player;
|
Player* player;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
|
|
|
@ -185,8 +185,8 @@ int Player::check(SDL_Rect rectA, int type){
|
||||||
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 -= (posX - oldPosX)/2;
|
movX -= (posX - oldPosX)/2;
|
||||||
movY -= (posY - oldPosY)/2;
|
movY -= (posY - oldPosY)/2;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "maps.h"
|
#include "maps.h"
|
||||||
|
|
||||||
Maps::Maps(SDL_Renderer* render){
|
Maps::Maps(SDL_Renderer* render){
|
||||||
int sound = 1;
|
int sound = false;
|
||||||
|
|
||||||
if(sound == true){
|
if(sound == true){
|
||||||
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
|
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
|
||||||
|
@ -39,57 +39,28 @@ Maps::~Maps(){
|
||||||
gMusic = NULL;
|
gMusic = NULL;
|
||||||
};
|
};
|
||||||
|
|
||||||
void Maps::loadMap(std::string path){
|
int Maps::map(std::string mapName){
|
||||||
//Open the map to read it
|
//Load map
|
||||||
std::ifstream map(path.c_str());
|
playerPosX = 1*sz;
|
||||||
|
playerPosY = 4*sz;
|
||||||
|
|
||||||
//Check if the map is open
|
mapWidth = sz*32;
|
||||||
if(map == NULL) std::cout << "Unable to load map" << std::endl;
|
mapHeight = screenHeight;
|
||||||
|
|
||||||
map >> playerPosX;
|
|
||||||
map >> playerPosY;
|
|
||||||
playerPosX *= sz;
|
|
||||||
playerPosY *= sz;
|
|
||||||
|
|
||||||
//Get the total of blocks
|
|
||||||
map >> blockTotal;
|
|
||||||
|
|
||||||
blockRect = new int*[blockTotal];
|
|
||||||
//Build rows
|
|
||||||
for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4];
|
|
||||||
|
|
||||||
//Load the block information
|
|
||||||
for(int i = 0; i < blockTotal; i++){
|
|
||||||
for(int j = 0; j < 4; j++){
|
|
||||||
map >> blockRect[i][j];
|
|
||||||
blockRect[i][j] *= 40;
|
|
||||||
}
|
|
||||||
if(blockRect[i][0]+blockRect[i][2] > mapWidth) mapWidth = blockRect[i][0]+blockRect[i][2];
|
|
||||||
if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3];
|
|
||||||
}
|
|
||||||
|
|
||||||
gMusic = Mix_LoadMUS( "assets/sapce-odyssey.ogg" );
|
gMusic = Mix_LoadMUS( "assets/sapce-odyssey.ogg" );
|
||||||
if( gMusic == NULL ){
|
if( gMusic == NULL ){
|
||||||
std::cout << "Failed to load beat music! SDL_mixer Error: " << Mix_GetError() << std::endl;
|
std::cout << "Failed to load beat music! SDL_mixer Error: " << Mix_GetError() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Close the opened file
|
|
||||||
map.close();
|
|
||||||
};
|
|
||||||
|
|
||||||
int Maps::map(std::string mapName){
|
|
||||||
//Load map
|
|
||||||
loadMap(mapName + ".map");
|
|
||||||
|
|
||||||
Texture level;
|
Texture level;
|
||||||
level.setRenderer(renderer);
|
level.setRenderer(renderer);
|
||||||
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
||||||
level.loadFromRendererText("level: " + mapName,textColor);
|
level.loadFromRendererText("level: dust3.map",textColor);
|
||||||
|
|
||||||
Texture sandCastle;
|
Texture sandCastle;
|
||||||
sandCastle.setRenderer(renderer);
|
sandCastle.setRenderer(renderer);
|
||||||
sandCastle.loadTexture("textures/castle.png");
|
sandCastle.loadTexture("textures/foreground.png");
|
||||||
SDL_Rect castleQuad = {6*sz,2*sz,sandCastle.getWidth(),sandCastle.getHeight()};
|
SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()};
|
||||||
|
|
||||||
Texture sandMiddle;
|
Texture sandMiddle;
|
||||||
sandMiddle.setRenderer(renderer);
|
sandMiddle.setRenderer(renderer);
|
||||||
|
@ -103,12 +74,6 @@ int Maps::map(std::string mapName){
|
||||||
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
|
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
|
||||||
float starsParallax = 0.2;
|
float starsParallax = 0.2;
|
||||||
|
|
||||||
/*Texture clouds;
|
|
||||||
clouds.setRenderer(renderer);
|
|
||||||
clouds.loadTexture("textures/clouds.png");
|
|
||||||
SDL_Rect cloudsQuad = {sz,sz/2,clouds.getWidth(),clouds.getHeight()};
|
|
||||||
int cloudsParallax = 3;*/
|
|
||||||
|
|
||||||
SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()};
|
SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()};
|
||||||
|
|
||||||
//Initialize the player, set its position, pass the map's size and set the renderer
|
//Initialize the player, set its position, pass the map's size and set the renderer
|
||||||
|
@ -121,7 +86,12 @@ int Maps::map(std::string mapName){
|
||||||
Powerup powerup(13*sz,8*sz,renderer,&player);
|
Powerup powerup(13*sz,8*sz,renderer,&player);
|
||||||
|
|
||||||
//Initialize the block class and set the player and the renderer
|
//Initialize the block class and set the player and the renderer
|
||||||
Block ground(renderer,&player);
|
Block ground(0,sz*11,screenWidth*2,sz*1,renderer,&player);
|
||||||
|
Block ground2(sz*21,sz*10,sz*11,sz*1,renderer,&player);
|
||||||
|
Block ground3(sz*22,sz*9,sz*10,sz*1,renderer,&player);
|
||||||
|
Block ground4(sz*30,sz*8,sz*2,sz*1,renderer,&player);
|
||||||
|
Block ground5(sz*31,0,sz*1,sz*8,renderer,&player);
|
||||||
|
//Block ground(renderer,&player);
|
||||||
|
|
||||||
Mix_PlayMusic( gMusic, -1 );
|
Mix_PlayMusic( gMusic, -1 );
|
||||||
|
|
||||||
|
@ -150,7 +120,6 @@ int Maps::map(std::string mapName){
|
||||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||||
|
|
||||||
stars.render(&starsQuad,camera.getPosX(),starsParallax);
|
stars.render(&starsQuad,camera.getPosX(),starsParallax);
|
||||||
//clouds.render(&cloudsQuad,camera.getPosX(),cloudsParallax,1);
|
|
||||||
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
|
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
|
||||||
sandCastle.render(&castleQuad,camera.getPosX());
|
sandCastle.render(&castleQuad,camera.getPosX());
|
||||||
|
|
||||||
|
@ -158,7 +127,12 @@ int Maps::map(std::string mapName){
|
||||||
if(player.print(camera.getPosX()) == 1) return 1;
|
if(player.print(camera.getPosX()) == 1) return 1;
|
||||||
|
|
||||||
//Print the blocks the corresponding dimensions and positions and check collisions
|
//Print the blocks the corresponding dimensions and positions and check collisions
|
||||||
for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
|
//for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
|
||||||
|
ground.printAndCheck(camera.getPosX());
|
||||||
|
ground2.printAndCheck(camera.getPosX());
|
||||||
|
ground3.printAndCheck(camera.getPosX());
|
||||||
|
ground4.printAndCheck(camera.getPosX());
|
||||||
|
ground5.printAndCheck(camera.getPosX());
|
||||||
|
|
||||||
//Print the poweup and check collisions
|
//Print the poweup and check collisions
|
||||||
powerup.printAndCheck(camera.getPosX());
|
powerup.printAndCheck(camera.getPosX());
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include<algorithm>
|
#include<algorithm>
|
||||||
#include"texture.h"
|
#include"texture.h"
|
||||||
#include<SDL_mixer.h>
|
#include<SDL_mixer.h>
|
||||||
|
#include<vector>
|
||||||
|
|
||||||
class Maps{
|
class Maps{
|
||||||
public:
|
public:
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
Loading…
Reference in New Issue