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);
|
gamestate = menu(gRenderer);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
gamestate = maps.map1();
|
gamestate = maps.map("overworld");
|
||||||
break;
|
break;
|
||||||
case 2:
|
/*case 2:
|
||||||
gamestate = maps.map2();
|
gamestate = maps.map2();*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ class Core{
|
||||||
void init();
|
void init();
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
int map1();
|
|
||||||
void setRectSize(int rect[],int x, int y, int w, int h);
|
void setRectSize(int rect[],int x, int y, int w, int h);
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 640;
|
const int SCREEN_WIDTH = 640;
|
||||||
|
|
|
@ -2,24 +2,26 @@
|
||||||
|
|
||||||
#include"player.h"
|
#include"player.h"
|
||||||
|
|
||||||
Player::Player(int x,int y, int lvlH, SDL_Renderer* render){
|
Player::Player(){
|
||||||
levelHeight = lvlH;
|
|
||||||
|
|
||||||
posX = x;
|
|
||||||
posY = y;
|
|
||||||
|
|
||||||
szW = 40;
|
szW = 40;
|
||||||
szH = 40;
|
szH = 40;
|
||||||
rect.w = szW;
|
rect.w = szW;
|
||||||
rect.h = szH;
|
rect.h = szH;
|
||||||
power = 0;
|
power = 0;
|
||||||
|
|
||||||
renderer = render;
|
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);
|
ply.setRenderer(renderer);
|
||||||
loadMedia();
|
loadMedia();
|
||||||
|
|
||||||
ground = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int Player::print(int cameraX){
|
int Player::print(int cameraX){
|
||||||
|
|
|
@ -14,7 +14,8 @@ class Entity;
|
||||||
|
|
||||||
class Player: public Entity{
|
class Player: public Entity{
|
||||||
public:
|
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 print(int cameraX);
|
||||||
int check(SDL_Rect rectA,int type);
|
int check(SDL_Rect rectA,int type);
|
||||||
private:
|
private:
|
||||||
|
|
133
source/maps.cpp
133
source/maps.cpp
|
@ -12,33 +12,83 @@ Maps::Maps(SDL_Renderer* render){
|
||||||
|
|
||||||
//Set keyboard input
|
//Set keyboard input
|
||||||
currentKeyStates = SDL_GetKeyboardState(NULL);
|
currentKeyStates = SDL_GetKeyboardState(NULL);
|
||||||
|
|
||||||
|
//Set player default position
|
||||||
|
playerPosX = 1*sz;
|
||||||
|
playerPosY = 1*sz;
|
||||||
};
|
};
|
||||||
|
|
||||||
int Maps::map1(){
|
Maps::~Maps(){
|
||||||
//Set the map dimensions
|
//Deallocate rows
|
||||||
int mapWidth = 36*sz;
|
for(int i = 0; i < blockTotal; ++i) delete blockRect[i];
|
||||||
int mapHeight = screenHeight;
|
//Deallocate
|
||||||
|
delete blockRect;
|
||||||
|
};
|
||||||
|
|
||||||
//Initialize the player, set its position, pass the map's size and set the renderer
|
void Maps::loadMap(std::string path){
|
||||||
Player player(40,8*sz,mapHeight,renderer);
|
//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.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
|
//Set the camera and pass the map and screen's dimensions
|
||||||
Camera camera(mapWidth,mapHeight,screenWidth,screenHeight);
|
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
|
//Initialize the powerup, set his position, pass the renderer and the player
|
||||||
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
|
||||||
|
Block ground(renderer,&player);
|
||||||
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
|
||||||
|
@ -59,10 +109,10 @@ int Maps::map1(){
|
||||||
camera.update(player.getRectangle().x,player.getRectangle().y);
|
camera.update(player.getRectangle().x,player.getRectangle().y);
|
||||||
|
|
||||||
//Print player
|
//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
|
//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
|
//Print the poweup and check collisions
|
||||||
powerup.printAndCheck(camera.getPosX());
|
powerup.printAndCheck(camera.getPosX());
|
||||||
|
@ -70,49 +120,6 @@ int Maps::map1(){
|
||||||
//Render
|
//Render
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
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/block.h"
|
||||||
#include"entity/powerup.h"
|
#include"entity/powerup.h"
|
||||||
#include"camera.h"
|
#include"camera.h"
|
||||||
|
#include<fstream>
|
||||||
|
#include<algorithm>
|
||||||
|
|
||||||
class Maps{
|
class Maps{
|
||||||
public:
|
public:
|
||||||
Maps(SDL_Renderer* render);
|
Maps(SDL_Renderer* render);
|
||||||
int map1();
|
~Maps();
|
||||||
int map2();
|
int map(std::string mapName);
|
||||||
private:
|
private:
|
||||||
void setRectSize(int rect[], int x, int y,int w,int h);
|
void loadMap(std::string path);
|
||||||
|
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
|
Player player;
|
||||||
|
|
||||||
|
int blockTotal;
|
||||||
|
int** blockRect;
|
||||||
int sz;
|
int sz;
|
||||||
int screenWidth, screenHeight;
|
int screenWidth, screenHeight;
|
||||||
|
int playerPosX, playerPosY;
|
||||||
const Uint8* currentKeyStates;
|
const Uint8* currentKeyStates;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,7 @@ int menu(SDL_Renderer* renderer){
|
||||||
}
|
}
|
||||||
if(currentKeyStates[SDL_SCANCODE_SPACE]){
|
if(currentKeyStates[SDL_SCANCODE_SPACE]){
|
||||||
if(select == 0) return 1;
|
if(select == 0) return 1;
|
||||||
else if(select == 1) return 2;
|
else if(select == 1) return 0;
|
||||||
else if(select == 2) break;
|
else if(select == 2) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue