Skip to content

Commit

Permalink
Merge pull request #348 from MartinFillon/last-fixes
Browse files Browse the repository at this point in the history
Last fixes
  • Loading branch information
ManuelR-T authored Jun 23, 2024
2 parents b270ca2 + 1c615e3 commit 8efcb64
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 212 deletions.
188 changes: 8 additions & 180 deletions gui/src/GUI/Data/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
*/

#include "Map.hpp"
#include <cstddef>
#include <memory>
#include <raylib.h>
#include <vector>
#include "../Raylib.hpp"
#include "Inventory.hpp"
#include "../UI/InfoBox.hpp"

namespace GUI {
namespace Data {
Expand Down Expand Up @@ -68,6 +64,11 @@ std::vector<std::shared_ptr<Player>> &Map::getPlayers()
return m_players;
}

const std::vector<std::shared_ptr<Player>> &Map::getPlayers() const
{
return m_players;
}

std::vector<std::shared_ptr<Egg>> &Map::getEggs()
{
return m_eggs;
Expand Down Expand Up @@ -110,190 +111,17 @@ void Map::resize(const Pos<int, 2> &size)
}
}

void Map::checkCollision(UI::InfoBox &infoBox) const
{
int mapWidth = end_x - x;
int mapHeight = end_y - y;
float tileSize = std::min(mapWidth / m_size.x(), mapHeight / m_size.y());

for (auto player : m_players) {
float playerCenterX = player->getPos().x() * tileSize + x + tileSize / 2;
float playerCenterY = player->getPos().y() * tileSize + y + tileSize / 2;
if (Raylib::checkCollisionMouseCircle(playerCenterX, playerCenterY, tileSize / 6)) {
auto &item = infoBox.getItem();
if (item == player) {
infoBox.setPrint(!infoBox.isPrint());
} else {
item = player;
infoBox.setPosTile(0.25, 0.25, 0.25);
infoBox.setSize(0.5);
}
return;
}
}
for (auto tile : m_map) {
float tileX = tile->getPos().x() * tileSize + x;
float tileY = tile->getPos().y() * tileSize + y;
if (Raylib::checkCollisionMouseSquare(tileX, tileY, tileSize)) {
auto &item = infoBox.getItem();
if (item == tile) {
infoBox.setPrint(!infoBox.isPrint());
} else {
item = tile;
infoBox.setPosTile(0, 0, 0);
infoBox.setSize(1);
}
}
}
}

void Map::checkCollision3D(UI::InfoBox &infoBox, const Camera3D &cam) const
std::vector<std::shared_ptr<Tile>> &Map::getTiles()
{
float tileSize = 1.0f;
Ray ray = Raylib::GetMouseRay(cam);
RayCollision collision = {};
RayCollision collisionTmp = {};
UI::InfoBox tmpInfo = infoBox;

for (auto player : m_players) {
float playerCenterX = player->getPos().x() * tileSize + tileSize / 2.0f;
float playerCenterZ = player->getPos().y() * tileSize + tileSize / 2.0f;
collisionTmp = Raylib::getRayCollisionSphere(
ray, (Vector3){playerCenterX, tileSize / 6.0f + tileSize / 2.0f, playerCenterZ}, tileSize / 6.0f
);
if (collisionTmp.hit && (!collision.hit || collisionTmp.distance < collision.distance)) {
collision = collisionTmp;
tmpInfo.setItem(player);
tmpInfo.setPosTile(0.0f, 0.67f, 0.0f);
tmpInfo.setSize(0.4f);
}
}
for (auto tile : m_map) {
float tileX = tile->getPos().x() * tileSize + tileSize / 2.0f;
float tileZ = tile->getPos().y() * tileSize + tileSize / 2.0f;
collisionTmp = Raylib::getRayCollisionCube(ray, {tileX, 0, tileZ}, tileSize);
if (collisionTmp.hit && (!collision.hit || collisionTmp.distance < collision.distance)) {
collision = collisionTmp;
tmpInfo.setItem(tile);
tmpInfo.setPosTile(0.0f, 0.0f, 0.0f);
tmpInfo.setSize(1.0f);
}
}
auto &tmpItem = tmpInfo.getItem();
if (tmpItem != nullptr) {
auto &item = infoBox.getItem();
if (item == tmpItem) {
infoBox.setPrint(!infoBox.isPrint());
return;
}
infoBox = tmpInfo;
infoBox.setPrint(true);
}
return m_map;
}

void Map::displayTacticalView(int start_x, int start_y, int end_x, int end_y, const UI::InfoBox &info) const
void Map::set2DDisplay(int start_x, int start_y, int end_x, int end_y)
{
this->x = start_x;
this->y = start_y;
this->end_x = end_x;
this->end_y = end_y;
int mapWidth = end_x - start_x;
int mapHeight = end_y - start_y;
float tileSize = std::min(mapWidth / m_size.x(), mapHeight / m_size.y());

for (int y = 0; y < m_size.y(); y++) {
for (int x = 0; x < m_size.x(); x++) {
auto &ressources = getTile(x, y).getInventory();

float tileX = x * tileSize + start_x;
float tileY = y * tileSize + start_y;

Raylib::drawSquare(tileX, tileY, tileSize, BROWN);
for (int i = 0; i < 7; ++i) {
if (ressources[i] == 0)
continue;
Color color = (ressources[i] <= SIZE_STEP_1) ? RED : (ressources[i] <= SIZE_STEP_2) ? ORANGE : GREEN;
float ressourceX = tileX + (i % 3) * (tileSize / 3.0f);
float ressourceY = tileY + (i / 3) * (tileSize / 3.0f);
Raylib::drawSquare(ressourceX, ressourceY, tileSize / 3.0f, color);
}
Raylib::drawSquareLines(tileX, tileY, tileSize, BLACK);
}
}
for (const auto &player : m_players) {
if (!player->isHatched())
continue;
int playerX = player->getPos().x() * tileSize + start_x + tileSize / 2;
int playerY = player->getPos().y() * tileSize + start_y + tileSize / 2;

Raylib::drawCircle(playerX, playerY, tileSize / 6, Color{0, 121, 241, 200});
}
for (const auto &egg : m_eggs) {
if (egg == nullptr) {
continue;
}
int eggX = egg->getPosition().x() * tileSize + start_x + tileSize / 2;
int eggY = egg->getPosition().y() * tileSize + start_y + tileSize / 2;

Raylib::drawCircle(eggX, eggY, tileSize / 8, Color{253, 249, 0, 200});
}
if (info.isPrint() && info.getItem() != nullptr) {
auto item = info.getItem();
float itemX = (item->getPos().x() + info.getPosTile().x()) * tileSize + start_x;
float itemZ = (item->getPos().y() + info.getPosTile().y()) * tileSize + start_y;
Raylib::drawSquareLines(itemX, itemZ, tileSize * info.getSize(), GREEN);
}
}

void Map::displayTacticalView3D(const UI::InfoBox &info) const
{
float tileSize = 1.0f;

if (qm.getSize() == 0) {
qm.init();
}

for (auto tile : m_map)
{
float tileX = tile->getPos().x() * tileSize + tileSize / 2;
float tileZ = tile->getPos().y() * tileSize + tileSize / 2;
qm.DrawGrass({tileX, 0.5, tileZ});
Raylib::drawCubeWires({tileX, 0.02, tileZ}, tileSize, (Color){100, 100, 100, 150});
Inventory inv = tile->getInventory();
for (size_t i = 0; i < inv.inv.size(); i++) {
if (inv.inv[i] == 0) {
continue;
}
int size = (inv.inv[i] <= SIZE_STEP_1) ? 0 : (inv.inv[i] <= SIZE_STEP_2) ? 1 : 2;
qm.Draw(size, i, tileX, tileZ);
}
}

for (const auto &egg : m_eggs) {
if (egg == nullptr)
continue;
float eggX = egg->getPosition().x() * tileSize + tileSize / 2;
float eggZ = egg->getPosition().y() * tileSize + tileSize / 2;
qm.DrawEgg({eggX, tileSize / 2, eggZ});
}

for (const auto &player : m_players) {
if (!player || !player->isHatched())
continue;
float playerX = player->getPos().x() * tileSize + tileSize / 2;
float playerZ = player->getPos().y() * tileSize + tileSize / 2;
qm.DrawPlayer({playerX, tileSize / 2, playerZ}, player->getOrientation());
}

if (info.isPrint() && info.getItem() != nullptr) {
auto item = info.getItem();
float itemX = (item->getPos().x() + info.getPosTile().x()) * tileSize + tileSize / 2;
float itemY = info.getPosTile().y() * tileSize;
float itemZ = (item->getPos().y() + info.getPosTile().z()) * tileSize + tileSize / 2;
float plus = tileSize / 10.0f;
float sizeCube = tileSize * info.getSize() + 2 * plus;
Raylib::drawCubeWires({itemX, itemY, itemZ}, sizeCube, GREEN);
}
}

} // namespace Data
Expand Down
26 changes: 14 additions & 12 deletions gui/src/GUI/Data/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
#pragma once

#include <memory>
#include <vector>
#include <raylib.h>
#include <vector>

#include "../UI/InfoBox.hpp"
#include "../ModelManager/RessourcesManager.hpp"
#include "Egg.hpp"
#include "Player.hpp"
#include "Pos.hpp"
#include "Tile.hpp"
#include "../ModelManager/RessourcesManager.hpp"

#define SIZE_STEP_1 1
#define SIZE_STEP_2 2
#define SIZE_STEP_1 3
#define SIZE_STEP_2 5

namespace GUI {
namespace Data {
Expand All @@ -34,24 +33,27 @@ class Map {
Tile getTile(const Pos<int, 2> &pos) const;
Tile getTile(int x, int y) const;
std::vector<std::shared_ptr<Player>> &getPlayers();
const std::vector<std::shared_ptr<Player>> &getPlayers() const;
std::vector<std::shared_ptr<Egg>> &getEggs();
std::vector<std::shared_ptr<Tile>> &getTiles();

Pos<int, 2> getSize() const;
void resize(int x, int y);
void resize(const Pos<int, 2> &size);

void checkCollision(UI::InfoBox &infoBox) const;
void checkCollision3D(UI::InfoBox &infoBox, const Camera3D &cam) const;
void displayTacticalView(int start_x, int start_y, int end_x, int end_y, const UI::InfoBox &info) const;
void displayTacticalView3D(const UI::InfoBox &info) const;
void set2DDisplay(int start_x, int start_y, int end_x, int end_y);

private:
std::vector<std::shared_ptr<Tile>> m_map;
std::vector<std::shared_ptr<Player>> m_players;
std::vector<std::shared_ptr<Egg>> m_eggs;
Pos<int, 2> m_size;

public:
mutable int x, y, end_x, end_y;
mutable RessourcesManager qm;

private:
std::vector<std::shared_ptr<Tile>> m_map;
std::vector<std::shared_ptr<Player>> m_players;
std::vector<std::shared_ptr<Egg>> m_eggs;
};

} // namespace Data
Expand Down
Loading

0 comments on commit 8efcb64

Please sign in to comment.