File reading and improved map initialization
It has some bugs tho
This commit is contained in:
parent
d5343a2cdd
commit
786fb12847
|
@ -0,0 +1,6 @@
|
|||
01 07
|
||||
-08 07 02 02
|
||||
04 09 01 02
|
||||
06 09 01 02
|
||||
00 11 16 01
|
||||
20 11 16 01
|
Binary file not shown.
|
@ -32,10 +32,10 @@ int Core::coreInit(){
|
|||
gamestate = menu(gRenderer);
|
||||
break;
|
||||
case 1:
|
||||
gamestate = maps.map1();
|
||||
gamestate = maps.map("overworld");
|
||||
break;
|
||||
case 2:
|
||||
gamestate = maps.map2();
|
||||
/*case 2:
|
||||
gamestate = maps.map2();*/
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,6 @@ class Core{
|
|||
void init();
|
||||
void close();
|
||||
|
||||
int map1();
|
||||
void setRectSize(int rect[],int x, int y, int w, int h);
|
||||
|
||||
const int SCREEN_WIDTH = 640;
|
||||
|
|
|
@ -2,24 +2,26 @@
|
|||
|
||||
#include"player.h"
|
||||
|
||||
Player::Player(int x,int y, int lvlH, SDL_Renderer* render){
|
||||
levelHeight = lvlH;
|
||||
|
||||
posX = x;
|
||||
posY = y;
|
||||
|
||||
Player::Player(){
|
||||
szW = 40;
|
||||
szH = 40;
|
||||
rect.w = szW;
|
||||
rect.h = szH;
|
||||
power = 0;
|
||||
|
||||
ground = false;
|
||||
};
|
||||
|
||||
void Player::set(int x,int y, int lvlH, SDL_Renderer* render){
|
||||
levelHeight = lvlH;
|
||||
|
||||
posX = x;
|
||||
posY = y;
|
||||
|
||||
renderer = render;
|
||||
|
||||
ply.setRenderer(renderer);
|
||||
loadMedia();
|
||||
|
||||
ground = false;
|
||||
};
|
||||
|
||||
int Player::print(int cameraX){
|
||||
|
|
|
@ -14,7 +14,8 @@ class Entity;
|
|||
|
||||
class Player: public Entity{
|
||||
public:
|
||||
Player(int x,int y,int lvlH,SDL_Renderer* render);
|
||||
Player();
|
||||
void set(int x,int y,int lvlH,SDL_Renderer* render);
|
||||
int print(int cameraX);
|
||||
int check(SDL_Rect rectA,int type);
|
||||
private:
|
||||
|
|
131
source/maps.cpp
131
source/maps.cpp
|
@ -12,33 +12,83 @@ Maps::Maps(SDL_Renderer* render){
|
|||
|
||||
//Set keyboard input
|
||||
currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||
|
||||
//Set player default position
|
||||
playerPosX = 1*sz;
|
||||
playerPosY = 1*sz;
|
||||
};
|
||||
|
||||
int Maps::map1(){
|
||||
//Set the map dimensions
|
||||
int mapWidth = 36*sz;
|
||||
int mapHeight = screenHeight;
|
||||
Maps::~Maps(){
|
||||
//Deallocate rows
|
||||
for(int i = 0; i < blockTotal; ++i) delete blockRect[i];
|
||||
//Deallocate
|
||||
delete blockRect;
|
||||
};
|
||||
|
||||
void Maps::loadMap(std::string path){
|
||||
//Open the map to read it
|
||||
std::ifstream map(path.c_str());
|
||||
|
||||
//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;
|
||||
std::cout << playerPosX << " " << playerPosY << std::endl;
|
||||
|
||||
//Get the total of blocks
|
||||
blockTotal = std::count(std::istreambuf_iterator<char>(map), std::istreambuf_iterator<char>(), '\n');
|
||||
std::cout << blockTotal << std::endl;
|
||||
|
||||
map.clear();
|
||||
map.seekg(0,map.beg);
|
||||
map.ignore(std::numeric_limits<std::streamsize>::max(),'-');
|
||||
|
||||
// dynamically allocate memory using new
|
||||
blockRect = new int*[blockTotal];
|
||||
//Build rows
|
||||
for(int i = 0; i < blockTotal; ++i) blockRect[i] = new int[4];
|
||||
|
||||
//Initialize size of the map variables
|
||||
int mH = 0,mW = 0;
|
||||
|
||||
//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;
|
||||
std::cout << blockRect[i][j] << " ";
|
||||
}
|
||||
if(blockRect[i][0]+blockRect[i][2] > mW) mW = blockRect[i][0]+blockRect[i][2];
|
||||
if(blockRect[i][1]+blockRect[i][3] > mH) mH = blockRect[i][1]+blockRect[i][3];
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
//Very descriptive by itself
|
||||
map.close();
|
||||
|
||||
//Initialize the player, set its position, pass the map's size and set the renderer
|
||||
Player player(40,8*sz,mapHeight,renderer);
|
||||
player.set(playerPosX,playerPosY,mH,renderer);
|
||||
};
|
||||
|
||||
int Maps::map(std::string mapName){
|
||||
//Load map
|
||||
loadMap(mapName + ".map");
|
||||
|
||||
int mapHeight = 12*sz;
|
||||
int mapWidth = 36*sz;
|
||||
|
||||
//Set the camera and pass the map and screen's dimensions
|
||||
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
|
||||
|
||||
//Initialize the block class and set the player and the renderer
|
||||
Block ground(renderer,&player);
|
||||
|
||||
//Set the quantity and size of the blocks
|
||||
int blockRect[5][4];
|
||||
setRectSize(blockRect[0], 8*sz, 7*sz, 2*sz,2*sz);
|
||||
setRectSize(blockRect[1], 4*sz, 9*sz, sz,2*sz);
|
||||
setRectSize(blockRect[2], 6*sz, 9*sz, sz,2*sz);
|
||||
setRectSize(blockRect[3], 0 ,11*sz,16*sz, sz);
|
||||
setRectSize(blockRect[4],20*sz,11*sz,16*sz, sz);
|
||||
|
||||
//Initialize the powerup, set his position, pass the renderer and the player
|
||||
Powerup powerup(13*sz,8*sz,renderer,&player);
|
||||
|
||||
//Initialize the block class and set the player and the renderer
|
||||
Block ground(renderer,&player);
|
||||
|
||||
bool quit = false;
|
||||
SDL_Event e;
|
||||
|
||||
|
@ -59,10 +109,10 @@ int Maps::map1(){
|
|||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
//Print player
|
||||
if(player.print(camera.getPosX()) == 1) map1();
|
||||
if(player.print(camera.getPosX()) == 1) return 1;
|
||||
|
||||
//Print the blocks the corresponding dimensions and positions and check collisions
|
||||
for(int i = 0; i < 5; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
|
||||
for(int i = 0; i < blockTotal; i++) ground.printAndCheck(blockRect[i],camera.getPosX());
|
||||
|
||||
//Print the poweup and check collisions
|
||||
powerup.printAndCheck(camera.getPosX());
|
||||
|
@ -70,49 +120,6 @@ int Maps::map1(){
|
|||
//Render
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
return -1;
|
||||
};
|
||||
|
||||
int Maps::map2(){
|
||||
int mapWidth = screenWidth;
|
||||
int mapHeight = screenHeight;
|
||||
|
||||
Player player(40,8*sz,mapHeight,renderer);
|
||||
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
|
||||
Block ground(renderer,&player);
|
||||
Powerup powerup(13*sz,8*sz,renderer,&player);
|
||||
|
||||
int blockRect[4];
|
||||
setRectSize(blockRect, 0 ,11*sz,16*sz, sz);
|
||||
|
||||
bool quit = false;
|
||||
SDL_Event e;
|
||||
|
||||
while(quit == false){
|
||||
while(SDL_PollEvent(&e)!=0){
|
||||
if(e.type == SDL_QUIT){
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
if(currentKeyStates[SDL_SCANCODE_ESCAPE]) return 0;
|
||||
|
||||
SDL_SetRenderDrawColor(renderer,0,0,100,0xFF);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||
|
||||
if(player.print(camera.getPosX()) == 1) map2();
|
||||
ground.printAndCheck(blockRect,camera.getPosX());
|
||||
powerup.printAndCheck(camera.getPosX());
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
|
||||
void Maps::setRectSize(int rect[],int x, int y, int w, int h){
|
||||
rect[0] = x;
|
||||
rect[1] = y;
|
||||
rect[2] = w;
|
||||
rect[3] = h;
|
||||
}
|
||||
|
|
|
@ -7,19 +7,25 @@
|
|||
#include"entity/block.h"
|
||||
#include"entity/powerup.h"
|
||||
#include"camera.h"
|
||||
#include<fstream>
|
||||
#include<algorithm>
|
||||
|
||||
class Maps{
|
||||
public:
|
||||
Maps(SDL_Renderer* render);
|
||||
int map1();
|
||||
int map2();
|
||||
~Maps();
|
||||
int map(std::string mapName);
|
||||
private:
|
||||
void setRectSize(int rect[], int x, int y,int w,int h);
|
||||
void loadMap(std::string path);
|
||||
|
||||
SDL_Renderer* renderer;
|
||||
Player player;
|
||||
|
||||
int blockTotal;
|
||||
int** blockRect;
|
||||
int sz;
|
||||
int screenWidth, screenHeight;
|
||||
int playerPosX, playerPosY;
|
||||
const Uint8* currentKeyStates;
|
||||
};
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ int menu(SDL_Renderer* renderer){
|
|||
}
|
||||
if(currentKeyStates[SDL_SCANCODE_SPACE]){
|
||||
if(select == 0) return 1;
|
||||
else if(select == 1) return 2;
|
||||
else if(select == 1) return 0;
|
||||
else if(select == 2) break;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue