Better map handling
This commit is contained in:
parent
fd18bb5df8
commit
4debeb7b3a
Binary file not shown.
|
@ -1,13 +1,15 @@
|
|||
#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
|
||||
player = ply; //Set the player class to check
|
||||
|
||||
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
|
||||
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
|
||||
SDL_SetRenderDrawColor(renderer,169,145,73,0xFF);
|
||||
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{
|
||||
public:
|
||||
Block(SDL_Renderer* render, Player* ply);
|
||||
void printAndCheck(int dimensions[], int cameraX);
|
||||
Block(int x,int y,int w,int h,SDL_Renderer* render, Player* ply);
|
||||
//Block(SDL_Renderer* render, Player* ply);
|
||||
void printAndCheck(int cameraX);
|
||||
protected:
|
||||
Player* player;
|
||||
SDL_Renderer* renderer;
|
||||
|
|
|
@ -185,8 +185,8 @@ int Player::check(SDL_Rect rectA, int type){
|
|||
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 -= (posX - oldPosX)/2;
|
||||
movY -= (posY - oldPosY)/2;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "maps.h"
|
||||
|
||||
Maps::Maps(SDL_Renderer* render){
|
||||
int sound = 1;
|
||||
int sound = false;
|
||||
|
||||
if(sound == true){
|
||||
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
|
||||
|
@ -39,57 +39,28 @@ Maps::~Maps(){
|
|||
gMusic = NULL;
|
||||
};
|
||||
|
||||
void Maps::loadMap(std::string path){
|
||||
//Open the map to read it
|
||||
std::ifstream map(path.c_str());
|
||||
int Maps::map(std::string mapName){
|
||||
//Load map
|
||||
playerPosX = 1*sz;
|
||||
playerPosY = 4*sz;
|
||||
|
||||
//Check if the map is open
|
||||
if(map == NULL) std::cout << "Unable to load map" << std::endl;
|
||||
|
||||
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];
|
||||
}
|
||||
mapWidth = sz*32;
|
||||
mapHeight = screenHeight;
|
||||
|
||||
gMusic = Mix_LoadMUS( "assets/sapce-odyssey.ogg" );
|
||||
if( gMusic == NULL ){
|
||||
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;
|
||||
level.setRenderer(renderer);
|
||||
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
||||
level.loadFromRendererText("level: " + mapName,textColor);
|
||||
level.loadFromRendererText("level: dust3.map",textColor);
|
||||
|
||||
Texture sandCastle;
|
||||
sandCastle.setRenderer(renderer);
|
||||
sandCastle.loadTexture("textures/castle.png");
|
||||
SDL_Rect castleQuad = {6*sz,2*sz,sandCastle.getWidth(),sandCastle.getHeight()};
|
||||
sandCastle.loadTexture("textures/foreground.png");
|
||||
SDL_Rect castleQuad = {0,0,sandCastle.getWidth(),sandCastle.getHeight()};
|
||||
|
||||
Texture sandMiddle;
|
||||
sandMiddle.setRenderer(renderer);
|
||||
|
@ -103,12 +74,6 @@ int Maps::map(std::string mapName){
|
|||
SDL_Rect starsQuad = {0,0,stars.getWidth(),stars.getHeight()};
|
||||
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()};
|
||||
|
||||
//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);
|
||||
|
||||
//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 );
|
||||
|
||||
|
@ -150,7 +120,6 @@ int Maps::map(std::string mapName){
|
|||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
stars.render(&starsQuad,camera.getPosX(),starsParallax);
|
||||
//clouds.render(&cloudsQuad,camera.getPosX(),cloudsParallax,1);
|
||||
sandMiddle.render(&middleQuad,camera.getPosX(),sandParallax);
|
||||
sandCastle.render(&castleQuad,camera.getPosX());
|
||||
|
||||
|
@ -158,7 +127,12 @@ int Maps::map(std::string mapName){
|
|||
if(player.print(camera.getPosX()) == 1) return 1;
|
||||
|
||||
//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
|
||||
powerup.printAndCheck(camera.getPosX());
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include<algorithm>
|
||||
#include"texture.h"
|
||||
#include<SDL_mixer.h>
|
||||
#include<vector>
|
||||
|
||||
class Maps{
|
||||
public:
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 26 KiB |
Loading…
Reference in New Issue