[Minimap] Added.

master
Quentin Bazin 2020-07-26 21:49:47 +02:00
parent 7e308bc988
commit 82f170d654
8 changed files with 183 additions and 2 deletions

View File

@ -35,7 +35,7 @@
#include "HUD.hpp"
HUD::HUD(ClientPlayer &player, ClientWorld &world, ClientCommandHandler &client)
: m_hotbar(player, client),
: m_player(player), m_hotbar(player, client),
m_blockCursor(player, world, client),
m_debugOverlay(player, world),
m_chat(client.client())
@ -62,6 +62,9 @@ void HUD::setup() {
m_crosshair.setup();
m_chat.setPosition(2, Config::screenHeight / Config::guiScale - 50);
m_minimap.setPosition(Config::screenWidth / Config::guiScale - Minimap::minimapSize - 15, 15);
// m_minimap.setPosition(Config::screenWidth / Config::guiScale - Minimap::minimapSize - 10 - Minimap::minimapSize / 2, 10 - Minimap::minimapSize / 2);
}
void HUD::onEvent(const SDL_Event &event) {
@ -99,6 +102,8 @@ void HUD::update() {
if (m_blockCursor.currentBlock() != m_blockInfoWidget.currentBlock())
m_blockInfoWidget.setCurrentBlock(m_blockCursor.currentBlock());
}
m_minimap.update(m_player);
}
void HUD::draw(gk::RenderTarget &target, gk::RenderStates states) const {
@ -123,6 +128,8 @@ void HUD::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (Config::isFpsCounterEnabled)
target.draw(m_fpsText, states);
target.draw(m_minimap, states);
target.draw(m_chat, states);
states.transform = gk::Transform::Identity;

View File

@ -35,6 +35,7 @@
#include "Crosshair.hpp"
#include "DebugOverlay.hpp"
#include "Hotbar.hpp"
#include "Minimap.hpp"
struct GuiScaleChangedEvent;
@ -55,9 +56,13 @@ class HUD : public gk::Transformable, public gk::Drawable {
Chat &chat() { return m_chat; }
Minimap &minimap() { return m_minimap; }
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
ClientPlayer &m_player;
gk::Shader m_shader;
glm::mat4 m_orthoMatrix;
@ -74,6 +79,8 @@ class HUD : public gk::Transformable, public gk::Drawable {
Text m_fpsText;
Chat m_chat;
Minimap m_minimap;
};
#endif // HUD_HPP_

View File

@ -0,0 +1,76 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include "ClientPlayer.hpp"
#include "Minimap.hpp"
Minimap::Minimap() {
m_border.setFillColor(gk::Color::Transparent);
m_border.setOutlineColor(gk::Color{224, 224, 224});
m_border.setOutlineThickness(1);
m_border.setSize(minimapSize, minimapSize);
m_playerChunk.setSize(chunkSize, chunkSize);
m_playerChunk.setFillColor(gk::Color::Red);
}
void Minimap::update(const ClientPlayer &player) {
m_playerChunkPos = gk::Vector3i{
((int)player.x() & -CHUNK_WIDTH) / CHUNK_WIDTH,
((int)player.y() & -CHUNK_DEPTH) / CHUNK_DEPTH,
((int)player.z() & -CHUNK_HEIGHT) / CHUNK_HEIGHT
};
m_playerChunk.setPosition(m_playerChunkPos.x * (chunkSize + 2), m_playerChunkPos.y * (chunkSize + 2));
}
void Minimap::onChunkCreatedEvent(const ChunkCreatedEvent &event) {
auto &rect = m_chunks[event.chunkPos];
rect.setSize(chunkSize, chunkSize);
rect.setPosition(event.chunkPos.x * (chunkSize + 2), event.chunkPos.y * (chunkSize + 2));
rect.setFillColor(event.isLoaded ? gk::Color{224, 224, 224} : gk::Color{127, 127, 127});
rect.setOutlineThickness(1);
rect.setOutlineColor(gk::Color::Transparent);
}
void Minimap::onChunkRemovedEvent(const ChunkRemovedEvent &event) {
m_chunks.erase(event.chunkPos);
}
void Minimap::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();
target.draw(m_border, states);
states.transform.translate(-m_playerChunkPos.x * (chunkSize + 2) + minimapSize / 2.f, -m_playerChunkPos.y * (chunkSize + 2) + minimapSize / 2.f);
for (auto &it : m_chunks)
if (it.first.z == m_playerChunkPos.z)
target.draw(it.second, states);
target.draw(m_playerChunk, states);
}

View File

@ -0,0 +1,61 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef MINIMAP_HPP_
#define MINIMAP_HPP_
#include <gk/gl/Drawable.hpp>
#include <gk/gl/Transformable.hpp>
#include <gk/graphics/RectangleShape.hpp>
#include "Events.hpp"
class ClientPlayer;
class Minimap : public gk::Drawable, public gk::Transformable {
public:
Minimap();
void update(const ClientPlayer &player);
void onChunkCreatedEvent(const ChunkCreatedEvent &event);
void onChunkRemovedEvent(const ChunkRemovedEvent &event);
static const u16 chunkSize = 2;
static const u16 minimapSize = 50;
private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
std::unordered_map<gk::Vector3i, gk::RectangleShape> m_chunks;
gk::RectangleShape m_border;
gk::Vector3i m_playerChunkPos;
gk::RectangleShape m_playerChunk;
};
#endif // MINIMAP_HPP_

View File

@ -68,7 +68,12 @@ GameState::GameState()
}
void GameState::init() {
m_world.setEventHandler(*m_eventHandler);
m_eventHandler->addListener<GuiScaleChangedEvent>(&GameState::onGuiScaleChanged, this);
m_eventHandler->addListener<ChunkCreatedEvent>(&Minimap::onChunkCreatedEvent, &m_hud.minimap());
m_eventHandler->addListener<ChunkRemovedEvent>(&Minimap::onChunkRemovedEvent, &m_hud.minimap());
}
void GameState::onStateInactive() {

View File

@ -34,6 +34,7 @@
#include "ClientCommandHandler.hpp"
#include "ClientPlayer.hpp"
#include "ClientWorld.hpp"
#include "Events.hpp"
#include "Registry.hpp"
#include "Sky.hpp"
#include "TextureAtlas.hpp"
@ -157,6 +158,9 @@ void ClientWorld::receiveChunkData(Network::Packet &packet) {
chunk->setInitialized(true);
if (m_eventHandler)
m_eventHandler->emplaceEvent<ChunkCreatedEvent>(gk::Vector3i{cx, cy, cz}, true);
// if (cx == 2 && cy == 0 && cz == 1)
// std::cout << "Chunk at (" << cx << ", " << cy << ", " << cz << ") received" << std::endl;
}
@ -171,6 +175,9 @@ void ClientWorld::removeChunk(ChunkMap::iterator &it) {
(ClientChunk *)it->second->getSurroundingChunk(5)
};
if (m_eventHandler)
m_eventHandler->emplaceEvent<ChunkRemovedEvent>(gk::Vector3i{it->second->x(), it->second->y(), it->second->z()});
it = m_chunks.erase(it);
for (u8 i = 0 ; i < 6 ; ++i) {
@ -212,6 +219,9 @@ void ClientWorld::createChunkNeighbours(ClientChunk *chunk) {
if (!neighbour) {
auto it = m_chunks.emplace(gk::Vector3i{scx, scy, scz}, new ClientChunk(scx, scy, scz, *m_dimension, *this, m_textureAtlas));
neighbour = it.first->second.get();
if (m_eventHandler)
m_eventHandler->emplaceEvent<ChunkCreatedEvent>(gk::Vector3i{scx, scy, scz}, false);
}
chunk->setSurroundingChunk(i, neighbour);

View File

@ -29,8 +29,9 @@
#include <unordered_map>
#include <gk/gl/Camera.hpp>
#include <gk/core/Vector4.hpp>
#include <gk/core/EventHandler.hpp>
#include <gk/gl/Camera.hpp>
#include "ClientChunk.hpp"
#include "ClientScene.hpp"
@ -65,6 +66,7 @@ class ClientWorld : public World, public gk::Drawable {
void setClient(ClientCommandHandler &client) { m_client = &client; }
void setCamera(gk::Camera &camera) { m_camera = &camera; m_scene.setCamera(camera); }
void setEventHandler(gk::EventHandler &eventHandler) { m_eventHandler = &eventHandler; }
std::size_t loadedChunkCount() const { return m_chunks.size(); }
@ -85,6 +87,7 @@ class ClientWorld : public World, public gk::Drawable {
ClientCommandHandler *m_client = nullptr;
gk::Camera *m_camera = nullptr;
gk::EventHandler *m_eventHandler = nullptr;
mutable gk::Vector4d m_closestInitializedChunk{0, 0, 0, 1000000};

View File

@ -28,14 +28,26 @@
#define EVENTS_HPP_
#include <gk/core/IntTypes.hpp>
#include <gk/core/Vector3.hpp>
struct ServerOnlineEvent {
bool isOnline;
int port;
};
// Client-only events
struct GuiScaleChangedEvent {
u8 guiScale;
};
struct ChunkCreatedEvent {
gk::Vector3<s32> chunkPos;
bool isLoaded;
};
struct ChunkRemovedEvent {
gk::Vector3<s32> chunkPos;
};
#endif // EVENTS_HPP_