Skip to content

Commit

Permalink
Merge pull request #72 from MyEpitech/refactorMap/victor
Browse files Browse the repository at this point in the history
Refactor map/victor
  • Loading branch information
victorpalle authored Jun 2, 2022
2 parents a024437 + af3a2ee commit bd8e18c
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 175 deletions.
22 changes: 11 additions & 11 deletions project/Save/Maps/random.map
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
XXXXXXXXXXXXX
X X
X x x x x x X
X X
X x x x x x X
X X
X x x x x x X
X X
X x x x x x X
X X
X x x x x x X
X X
X OO OOOO X
X xOxOxOxOx X
XOOO OOOO OOX
XOxOxOxOxOxOX
XOO OOOOOOOOX
XOxOxOxOxOxOX
XOOOOOOOO OX
XOxOxOxOxOx X
XOOOOOOOOOOOX
X x xOxOxOx X
X O OO OO X
XXXXXXXXXXXXX
13 changes: 9 additions & 4 deletions project/Source/Objects/3DObject/AThreeDimensionObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,29 @@

#include "AThreeDimensionObject.hpp"

Object::AThreeDimensionObject::AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, Position const &position) : _position(position),
Object::AThreeDimensionObject::AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS type) :
_isEnable(true),
_position(position), _type(type),
_model(LoadModel(pathToRessources.first.c_str())),
_texture(LoadTexture(pathToRessources.second.c_str())),
_isAnimated(false), _scale(0.5f)
{
_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = _texture;
}

Object::AThreeDimensionObject::AThreeDimensionObject(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position) :
Object::AThreeDimensionObject::AThreeDimensionObject(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS type) :
_isEnable(true), _position(position),
_type(type),
_model(pathToModel.getModel()),
_texture(pathToTexture.getTexture()),
_isAnimated(false), _scale(0.5f)
{
_model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = _texture;
}

Object::AThreeDimensionObject::AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, std::string const &pathToAnimation, unsigned int nbAnimation, Position const &position) :
Object::AThreeDimensionObject::AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, std::string const &pathToAnimation, unsigned int nbAnimation, Position const &position, Object::MAP_OBJECTS type) :
_isEnable(true), _position(position),
_type(type),
_model(LoadModel(pathToRessources.first.c_str())),
_texture(LoadTexture(pathToRessources.second.c_str())),
_animsCount(nbAnimation),
Expand All @@ -35,8 +39,9 @@ Object::AThreeDimensionObject::AThreeDimensionObject(std::pair<std::string, std:
SetMaterialTexture(&_model.materials[0], MATERIAL_MAP_DIFFUSE, _texture);
}

Object::AThreeDimensionObject::AThreeDimensionObject(Object::Render::MyModel &pathToModel, Object::Render::MyTexture &pathToRessources, Object::Render::MyAnimation &pathToAnimation, unsigned int numberOfAnimations, Position const &position) :
Object::AThreeDimensionObject::AThreeDimensionObject(Object::Render::MyModel &pathToModel, Object::Render::MyTexture &pathToRessources, Object::Render::MyAnimation &pathToAnimation, unsigned int numberOfAnimations, Position const &position, Object::MAP_OBJECTS type) :
_isEnable(true), _position(position),
_type(type),
_model(pathToModel.getModel()),
_texture(pathToRessources.getTexture()),
_animsCount(numberOfAnimations),
Expand Down
11 changes: 7 additions & 4 deletions project/Source/Objects/3DObject/AThreeDimensionObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ namespace Object
{
public:
// Non Animated
AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, Position const &position);
AThreeDimensionObject(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position);
AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS type);
AThreeDimensionObject(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS type);

// Animated
AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, std::string const &pathToAnimation, unsigned int nbAnimation, Position const &position);
AThreeDimensionObject(Object::Render::MyModel &pathToModel, Object::Render::MyTexture &pathToTexture, Object::Render::MyAnimation &pathToAnimation, unsigned int numberOfAnimations, Position const &position);
AThreeDimensionObject(std::pair<std::string, std::string> const &pathToRessources, std::string const &pathToAnimation, unsigned int nbAnimation, Position const &position, Object::MAP_OBJECTS type);
AThreeDimensionObject(Object::Render::MyModel &pathToModel, Object::Render::MyTexture &pathToTexture, Object::Render::MyAnimation &pathToAnimation, unsigned int numberOfAnimations, Position const &position, Object::MAP_OBJECTS type);

// Via JSON
AThreeDimensionObject(nlohmann::json const &jsonData);
Expand All @@ -54,6 +54,8 @@ namespace Object

void setScale(float scale);

Object::MAP_OBJECTS getType() const { return _type; };

protected:
bool _isEnable;
Texture2D _texture;
Expand All @@ -66,6 +68,7 @@ namespace Object
unsigned int _animsCount = 0;
int _animFrameCounter = 0;
ModelAnimation *_anims;
Object::MAP_OBJECTS _type;

float _scale = 0.5f;
private:
Expand Down
4 changes: 2 additions & 2 deletions project/Source/Objects/3DObject/BombObject/Bomb/Bomb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include "Bomb.hpp"

Object::Bomb::Bomb(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::PLAYER_ORDER player, float lifeTime, std::size_t range) :
AThreeDimensionObject(pathToRessources, position)
Object::Bomb::Bomb(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::PLAYER_ORDER player, float lifeTime, std::size_t range, Object::MAP_OBJECTS type) :
AThreeDimensionObject(pathToRessources, position, type)
{
_bombClock.start();
_player = player;
Expand Down
2 changes: 1 addition & 1 deletion project/Source/Objects/3DObject/BombObject/Bomb/Bomb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Object
{
class Bomb : public AThreeDimensionObject {
public:
Bomb(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::PLAYER_ORDER player, float lifeTime, std::size_t range);
Bomb(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::PLAYER_ORDER player, float lifeTime, std::size_t range, Object::MAP_OBJECTS type);
~Bomb() override;

void draw() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace Object {
class Explosion : public AThreeDimensionObject {
public:
Explosion(std::string const &pathToRessources) : AThreeDimensionObject(std::make_pair(pathToRessources, "Ressources/Fire_baseColor.png"), "Ressources/explosionAnim.iqm", 1, Position(0, 20, 0)) {
Explosion(std::string const &pathToRessources) : AThreeDimensionObject(std::make_pair(pathToRessources, "Ressources/Fire_baseColor.png"), "Ressources/explosionAnim.iqm", 1, Position(0, 20, 0), Object::MAP_OBJECTS::EMPTY) {
// _model = LoadModel(pathToRessources.c_str());
// _texture = LoadTexture("Ressources/Fire_baseColor.png");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "Bonus.hpp"
#include "raymath.h"

Object::Bonus::Bonus(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::BONUS_OBJECTS mapObject) : AThreeDimensionObject(pathToRessources, position), _bonusObject(mapObject)
Object::Bonus::Bonus(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::BONUS_OBJECTS mapObject, Object::MAP_OBJECTS type) : AThreeDimensionObject(pathToRessources, position, type), _bonusObject(mapObject)
{
_bonusScale = 8;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Object {
class Bonus : public AThreeDimensionObject
{
public:
Bonus(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::BONUS_OBJECTS bonusObject);
Bonus(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::BONUS_OBJECTS bonusObject, Object::MAP_OBJECTS type);
~Bonus() override;

void draw() override;
Expand Down
12 changes: 12 additions & 0 deletions project/Source/Objects/3DObject/IThreeDimensionObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

namespace Object
{
enum class MAP_OBJECTS {
GROUND = 'A',
WALL_MIDDLE = 'x',
WALL_SIDE = 'X',
BOX = 'O',
EMPTY = ' ',
BOMB = 'B',
BONUS = 'b',
PLAYER = 'P',
// EXPLOSION = 'E,
};

class IThreeDimensionObject : public IObject {
public:
virtual ~IThreeDimensionObject() = default;
Expand Down
13 changes: 4 additions & 9 deletions project/Source/Objects/3DObject/MapObject/Block/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#include "Block.hpp"

Object::Block::Block(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS mapObject) :
AThreeDimensionObject(pathToRessources, position), _mapObject(mapObject)
Object::Block::Block(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS type) :
AThreeDimensionObject(pathToRessources, position, type)
{
_blockScale = 0.5f;
}

Object::Block::Block(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS mapObjects) :
AThreeDimensionObject(pathToModel, pathToTexture, position), _mapObject(mapObjects)
Object::Block::Block(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS type) :
AThreeDimensionObject(pathToModel, pathToTexture, position, type), _type(type)
{
_blockScale = 0.5f;
}
Expand All @@ -34,8 +34,3 @@ void Object::Block::draw()
if (_isEnable)
DrawModel(getModel(), modelPosition, _blockScale, WHITE);
}

Object::MAP_OBJECTS Object::Block::getMapObject() const
{
return _mapObject;
}
17 changes: 3 additions & 14 deletions project/Source/Objects/3DObject/MapObject/Block/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,21 @@

namespace Object
{
enum class MAP_OBJECTS {
GROUND = 'A',
WALL_MIDDLE = 'x',
WALL_SIDE = 'X',
BOX = 'O',
EMPTY = ' '
};

class Block : public AThreeDimensionObject
{
public:
Block(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS mapObject);
Block(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS mapObjects);
Block(std::pair<std::string, std::string> const &pathToRessources, Position const &position, Object::MAP_OBJECTS type);
Block(Object::Render::MyModel pathToModel, Object::Render::MyTexture pathToTexture, Position const &position, Object::MAP_OBJECTS type);
~Block() override;

void draw() override;

Object::MAP_OBJECTS getMapObject() const;

void setBlockScale(float blockScale) { _blockScale = blockScale; };
float getBlockScale() { return _blockScale; };

protected:
private:
Object::MAP_OBJECTS _mapObject;
std::string _type = "block";
Object::MAP_OBJECTS _type;
float _blockScale;
};
}
Expand Down
106 changes: 77 additions & 29 deletions project/Source/Objects/3DObject/MapObject/Map/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ std::vector<Position> Object::Map::getMapCorners(std::size_t width, std::size_t
return corners;
}

void Object::Map::generate(const std::string &filename, std::size_t width, std::size_t height)
void Object::Map::generate(const std::string &filename, std::size_t width, std::size_t height, std::size_t percentageDrop)
{
srand(time(NULL));
std::size_t randomNumber = 1 + (rand() % 100);

if ((width % 2) == 0 || (height % 2) == 0)
throw Error::Errors("Height and Width are not compatible !");
createFile(filename);
Expand All @@ -63,8 +66,13 @@ void Object::Map::generate(const std::string &filename, std::size_t width, std::
for (size_t y = 0; y < width; y++) {
if (x % 2 && y % 2)
_file << static_cast<char>(MAP_OBJECTS::WALL_MIDDLE);
else
_file << " ";
else {
randomNumber = 1 + (rand() % 100);
if (randomNumber > percentageDrop || (x <= 1 || x >= height - 2) && (y <= 1 || y >= width - 2))
_file << static_cast<char>(MAP_OBJECTS::EMPTY);
else
_file << static_cast<char>(MAP_OBJECTS::BOX);
}
}
_file << static_cast<char>(MAP_OBJECTS::WALL_SIDE);
_file << std::endl;
Expand All @@ -74,9 +82,18 @@ void Object::Map::generate(const std::string &filename, std::size_t width, std::

void Object::Map::draw()
{
if (_isEnable)
for (auto &mapObject : _mapObjects)
mapObject->draw();
for (int index = 0; index < _mapPositionsObjects.size(); index++) {
for (int idx = 0; idx < _mapPositionsObjects[index].size(); idx++) {
_mapPositionsObjects[index][idx]->draw();

}
}
for (int index = 0; index < _groundMap.size(); index++) {
for (int idx = 0; idx < _groundMap[index].size(); idx++) {
_groundMap[index][idx]->draw();

}
}
}

std::vector<std::string> Object::Map::load(std::string const &pathToFile)
Expand All @@ -97,7 +114,7 @@ std::vector<std::string> Object::Map::load(std::string const &pathToFile)

void Object::Map::removeBlock(std::size_t index)
{
if (_mapObjects.at(index)->getMapObject() == MAP_OBJECTS::BOX)
if (_mapObjects.at(index)->getType() == MAP_OBJECTS::BOX)
_mapObjects.erase(_mapObjects.begin() + index);
}

Expand All @@ -107,14 +124,12 @@ void Object::Map::process(std::string const &pathToFile)

std::vector<std::string> mapLayout = load(_pathToMap);


std::cout << "Models size : " << _mapModels.size() << std::endl;

static const std::map<Object::MAP_OBJECTS, std::pair<Object::Render::MyModel, Object::Render::MyTexture>> keyMap = {
{MAP_OBJECTS::WALL_MIDDLE, {_mapModels.at(4), _mapTextures.at(6)}},
{MAP_OBJECTS::GROUND, {_mapModels.at(5), _mapTextures.at(7)}},
{MAP_OBJECTS::WALL_SIDE, {_mapModels.at(6), _mapTextures.at(8)}},
{MAP_OBJECTS::BOX, {_mapModels.at(7), _mapTextures.at(9)}}
{MAP_OBJECTS::BOX, {_mapModels.at(7), _mapTextures.at(9)}},
{MAP_OBJECTS::EMPTY, {_mapModels.at(8), _mapTextures.at(10)}}
};

srand(time(NULL));
Expand All @@ -128,30 +143,63 @@ void Object::Map::process(std::string const &pathToFile)
Vector3 tilePosition = {0, 0, 0};

for (std::size_t line = 0; line < mapLayout.size(); line += 1) {
std::vector<std::shared_ptr<AThreeDimensionObject>> tempVector;
std::vector<std::shared_ptr<AThreeDimensionObject>> tempGrass;
for (std::size_t col = 0; col < mapLayout.at(line).size(); col++) {
if (mapLayout.at(line).at(col) == static_cast<char>(Object::MAP_OBJECTS::WALL_SIDE))
_mapObjects.emplace_back(std::make_shared<Object::Block>(keyMap.at(MAP_OBJECTS::WALL_SIDE).first, keyMap.at(MAP_OBJECTS::WALL_SIDE).second,
(Position){tilePosition.x, tilePosition.y - _blockSize, tilePosition.z}, MAP_OBJECTS::WALL_SIDE));
else if ((col >= 3 && col <= mapLayout.at(line).size() - 4) || ( line >= 3 && line <= mapLayout.size() - 4)) {
if (((rand() % 8) + 1) != 1) {
_mapObjects.emplace_back(std::make_shared<Object::Block>(
keyMap.at(MAP_OBJECTS::BOX).first, keyMap.at(MAP_OBJECTS::BOX).second,
(Position){tilePosition.x, tilePosition.y, tilePosition.z}, MAP_OBJECTS::BOX));
}
}

if (keyMap.find(static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))) != keyMap.end())
_mapObjects.emplace_back(std::make_shared<Object::Block>(
keyMap.at(static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))).first, keyMap.at(static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))).second,
(Position){tilePosition.x, tilePosition.y, tilePosition.z}, static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))));
tempVector.emplace_back(std::make_shared<Object::Block>(keyMap.at(static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))).first, keyMap.at(static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))).second, (Position){tilePosition.x, tilePosition.y, tilePosition.z}, static_cast<MAP_OBJECTS>(mapLayout.at(line).at(col))));
if (mapLayout.at(line).at(col) == static_cast<char>(Object::MAP_OBJECTS::WALL_SIDE))
tempGrass.emplace_back(std::make_shared<Object::Block>(keyMap.at(MAP_OBJECTS::WALL_SIDE).first, keyMap.at(MAP_OBJECTS::WALL_SIDE).second, (Position){tilePosition.x, tilePosition.y - _blockSize, tilePosition.z}, MAP_OBJECTS::WALL_SIDE));
else
_mapObjects.emplace_back(std::make_shared<Object::Block>(
keyMap.at(MAP_OBJECTS::GROUND).first, keyMap.at(MAP_OBJECTS::GROUND).second,
(Position){tilePosition.x, tilePosition.y - (_blockSize - 1), tilePosition.z}, MAP_OBJECTS::GROUND));

tempGrass.emplace_back(std::make_shared<Object::Block>(keyMap.at(MAP_OBJECTS::GROUND).first, keyMap.at(MAP_OBJECTS::GROUND).second, (Position){tilePosition.x, tilePosition.y - (_blockSize - 1), tilePosition.z}, MAP_OBJECTS::GROUND));
tilePosition.x += _blockSize;
}
_mapPositionsObjects.emplace_back(tempVector);
_groundMap.emplace_back(tempGrass);
tilePosition.z += _blockSize;
tilePosition.x = 0;
tempVector.emplace_back(std::make_shared<Object::Block>(keyMap.at(MAP_OBJECTS::BOX).first, keyMap.at(MAP_OBJECTS::BOX).second, (Position){tilePosition.x, tilePosition.y, tilePosition.z}, MAP_OBJECTS::BOX));
}
}

int Object::Map::roundUp(int nb, int multiple)
{
if (multiple == 0)
return nb;

int remainder = abs(nb) % multiple;

if (remainder == 0)
return nb;

if (nb < 0)
return (-(abs(nb) - remainder));
else
return (nb + multiple - remainder);
}

Object::MAP_OBJECTS Object::Map::isColliding(Position &direction, Position playerPosition)
{
Position temppos = playerPosition;
temppos += direction;

std::pair<int, int> position = transposeFrom3Dto2D(temppos);
return (_mapPositionsObjects.at(position.second).at(position.first)->getType());
}

std::pair<int, int> Object::Map::transposeFrom3Dto2D(Position &position)
{
std::cout << position << std::endl;
int x = roundUp(static_cast<int>(position.getX()), (_blockSize / 2));
int z = roundUp(static_cast<int>(position.getZ()), (_blockSize / 2));

if (x % 10 == (_blockSize / 2))
x -= 5;
if (z % 10 == (_blockSize / 2))
z -= 5;
return {x / 10, z / 10};
}

void Object::Map::exploseBomb(Position const &position, int radius)
{
}
Loading

0 comments on commit bd8e18c

Please sign in to comment.