Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added gui/assets/minus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gui/assets/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gui/assets/quickMenu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gui/assets/speedButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions gui/src/Buttons/Button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "Buttons/Button.hpp"
#include <iostream>
namespace Zappy
{
Button::Button(Texture &texture, float x, float y, float width, float height, std::function<void()> function) : _function(function), _width(width), _height(height), _x(x), _y(y), _hovered(false)
{
_sprite = std::make_unique<Sprite>(texture);
_sprite->setPosition(x, y);
_sprite->scale = Zappy::Math::vec3(width, height, 1.0f);


}

void Button::draw(Shader &shader)
{
if (_sprite) {
Zappy::Math::mat4 orthoProjection = Zappy::Math::ortho(0.0f, WIDTH, HEIGHT, 0.0f, -1.0f, 1.0f);
Zappy::Math::mat4 view;
_sprite->draw(shader, view, orthoProjection);
}
}

void Button::setPosition(float x, float y)
{
_x = x;
_y = y;
}

void Button::update(const std::vector<Zappy::Event> &events)
{
for (const auto &event : events)
{
if (event.type == EventType::MouseMoved){
if (event.mouseX >= _x - _width / 2 && event.mouseX <= _x + _width / 2 && event.mouseY >= _y - _height / 2 && event.mouseY <= _y + _height / 2){
_hovered = true;
} else {
_hovered = false;
}
}
if (event.type == EventType::MousePressed && event.button == 1) {
if (_hovered) {
_function();
}
}
}
}
}// namespace Zappy
26 changes: 26 additions & 0 deletions gui/src/Buttons/Button.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include "Buttons/IButton.hpp"
#include "Sprite/Sprite.hpp"
#include "IScene/IScene.hpp"
#include <functional>
#include <memory>

namespace Zappy
{
class Button : public IButton {
private:
std::unique_ptr<Sprite> _sprite;
std::function<void()> _function;
float _width;
float _height;
float _x;
float _y;
bool _hovered;
public:
Button(Texture& texture, float x, float y, float width, float height, std::function<void()> function);
void draw(Shader &shader);
void setPosition(float x, float y);
void update(const std::vector<Zappy::Event> &events);
};

} // namespace Z
16 changes: 16 additions & 0 deletions gui/src/Buttons/IButton.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Core/Shader/Shader.hpp"
#include "Event.hpp"
#include <vector>

namespace Zappy
{
class IButton {
private:
public:
virtual ~IButton() = default;
virtual void draw(Shader &shader) = 0;
virtual void setPosition(float x, float y) = 0;
virtual void update(const std::vector<Zappy::Event> &events) = 0;
};
}
14 changes: 14 additions & 0 deletions gui/src/Core/Shader/ui.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 330 core
in vec2 TexCoords;
out vec4 FragColor;

uniform sampler2D ourTexture;

void main()
{
vec4 texColor = texture(ourTexture, TexCoords);

if (texColor.a < 0.1)
discard;
FragColor = texColor;
}
13 changes: 13 additions & 0 deletions gui/src/Core/Shader/ui.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoords;

out vec2 TexCoords;

uniform mat4 u_MVP;

void main()
{
TexCoords = vec2(aTexCoords.x, 1.0 - aTexCoords.y);
gl_Position = u_MVP * vec4(aPos, 0.0, 1.0);
}
2 changes: 1 addition & 1 deletion gui/src/Network/INetworkClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class INetworkClient {
public:
virtual ~INetworkClient() = default;
virtual bool connectToServer(const std::string &host, int port) = 0;
virtual void sendCommand(const std::string &cmd) = 0;
virtual std::vector<std::string> fetchLines() = 0;
virtual void sendCommand(const std::string &cmd) = 0;
};
11 changes: 11 additions & 0 deletions gui/src/Network/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,15 @@ void NetworkManager::handleSbp(const std::vector<std::string> &args) {
_eventQueue.push_back({NetworkEventType::SERVER_ERROR, args});
}

void NetworkManager::sendSst(int time)
{
std::string format = "sst " + std::to_string(time) + "\n";
this->sendCommand(format);
}

void NetworkManager::sendSgt()
{
this->sendCommand(std::string("sgt\n"));
}

} // namespace Zappy
2 changes: 2 additions & 0 deletions gui/src/Network/NetworkManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class NetworkManager {
void processLine(const std::string &line);
std::vector<std::string> splitString(const std::string &str, char delimiter);
int parseId(const std::string &idStr);
void sendSst(int time);
void sendSgt();

void handleWelcome();
void handleMsz(const std::vector<std::string> &args);
Expand Down
30 changes: 29 additions & 1 deletion gui/src/Scene/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ Zappy::SceneState Zappy::GameScene::update(
if (_isMapBuilt) {
float offsetX = gameState.map.width / 2.0f;
float offsetZ = gameState.map.height / 2.0f;
bool isSpacePressed = false;

for (const auto &event : events) {
if (event.type == Zappy::EventType::KeyPressed && event.keyCode == Zappy::Key::Space) {
isSpacePressed = true;
}
}
if (isSpacePressed && !_wasSpacePressed) {
if (_quickMenu) {
_quickMenu->onExit();
_quickMenu.reset();
} else {
_quickMenu = std::make_unique<quickMenu>(_texManager, _networkManager);
_quickMenu->onEnter();
}
}
_wasSpacePressed = isSpacePressed;
if (_quickMenu) {
SceneState quickMenuState = _quickMenu->update(events, gameState, netEvents, deltaTime);
if (quickMenuState != SceneState::NONE)
return quickMenuState;
} else
_camera.update(events);

for (const auto &netEvent : netEvents) {

Expand Down Expand Up @@ -103,7 +126,7 @@ Zappy::SceneState Zappy::GameScene::update(
return SceneState::NONE;
}

void Zappy::GameScene::draw(Shader &) {
void Zappy::GameScene::draw(Shader &shader) {
if (_renderer && _isMapBuilt && _floor) {
std::vector<std::reference_wrapper<Sprite>> resourcesToDraw;

Expand All @@ -116,6 +139,11 @@ void Zappy::GameScene::draw(Shader &) {
}
_renderer->render(_camera, *_floor, _players, resourcesToDraw);
}
if (_quickMenu) {
glDisable(GL_DEPTH_TEST);
_quickMenu->draw(shader);
glEnable(GL_DEPTH_TEST);
}
}

void Zappy::GameScene::onExit() {
Expand Down
12 changes: 8 additions & 4 deletions gui/src/Scene/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#include "Logger.hpp"
#include "Sprite/InstancedGrid.hpp"
#include "Sprite/Sprite.hpp"
#include "Scene/QuickMenu.hpp"
#include "Texture/TextureManager.hpp"
#include "Utils/math.hpp"

#include "Network/NetworkManager.hpp"
#include "Render/Camera.hpp"
#include "Render/Render.hpp"

Expand All @@ -19,16 +20,19 @@ class GameScene : public IScene {
private:
TextureManager &_texManager;
std::unique_ptr<Renderer> _renderer;

Camera _camera;
std::unique_ptr<InstancedGrid> _floor;
std::vector<std::unique_ptr<Sprite>> _players;
std::map<int, Sprite *> _playerMap;

std::unique_ptr<IScene> _quickMenu = nullptr;
bool _wasSpacePressed = false;

bool _isMapBuilt;
Zappy::NetworkManager &_networkManager;

public:
GameScene(TextureManager &tm) : _texManager(tm), _isMapBuilt(false) {}
GameScene(TextureManager &tm, Zappy::NetworkManager &nm) : _texManager(tm), _isMapBuilt(false), _networkManager(nm) {}

void onEnter() override;

Expand Down
83 changes: 83 additions & 0 deletions gui/src/Scene/QuickMenu.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include "IScene/IScene.hpp"
#include "Texture/TextureManager.hpp"
#include "Sprite/Sprite.hpp"
#include "Logger.hpp"
#include "Buttons/Button.hpp"
#include "Network/NetworkManager.hpp"
#include "Render/Render.hpp"
#include <memory>
#include <vector>
#include <map>
#include "Utils/math.hpp"
#include <algorithm>

namespace Zappy {
class quickMenu : public IScene {
private:
TextureManager &_texManager;
std::unique_ptr<Sprite> _backgroundSprite;
Zappy::NetworkManager &_networkManager;
std::vector<std::unique_ptr<Zappy::IButton>> _buttons;
std::unique_ptr<Shader> _uiShader;
int _speed;
public:
quickMenu(TextureManager &tm, Zappy::NetworkManager &nm) : _texManager(tm), _networkManager(nm) {
_speed = 0;
}
void onEnter() override {
Texture& menuTex = _texManager.get("gui/assets/quickMenu.png");
Texture& speedButtonTex = _texManager.get("gui/assets/speedButton.png");
Texture& decreaseButtonTex = _texManager.get("gui/assets/minus.png");
Texture& increaseButtonTex = _texManager.get("gui/assets/plus.png");
_uiShader = std::make_unique<Shader>("gui/src/Core/Shader/ui.vert", "gui/src/Core/Shader/ui.frag");
_backgroundSprite = std::make_unique<Sprite>(menuTex);
_backgroundSprite->setPosition(960.0f, 332.5f);
_networkManager.sendCommand("sgt\n");
auto helper = []() {
std::cout << "You can decrease / increase the number of ticks per seconds." << std::endl;
};
auto decrease = [this]() {
int newSpeed = std::max(1, this->_speed - 5);
this->_networkManager.sendCommand("sst " + std::to_string(newSpeed) + "\n");
std::cout << "Decrease requested: " << newSpeed << std::endl;
};
auto increase = [this]() {
int newSpeed = this->_speed + 5;
this->_networkManager.sendCommand("sst " + std::to_string(newSpeed) + "\n");
std::cout << "Increase requested: " << newSpeed << std::endl;
};
_buttons.push_back(std::make_unique<Zappy::Button>(speedButtonTex, 950.0f, 450.0f, 80.0f, 80.0f, helper));
_buttons.push_back(std::make_unique<Zappy::Button>(increaseButtonTex, 1050.0f, 465.0f, 50.0f, 50.0f, increase));
_buttons.push_back(std::make_unique<Zappy::Button>(decreaseButtonTex, 850.0f, 465.0f, 50.0f, 50.0f, decrease));
_backgroundSprite->scale = Zappy::Math::vec3(415.0f, 415.0f, 1.0f);
}
SceneState update(const std::vector<Zappy::Event> &events, const Zappy::GameState &gameState, const std::vector<Zappy::NetworkEvent> &netEvents, float deltaTime) override
{
_speed = gameState.map.timeUnit;
for (auto& button : _buttons) {
button->update(events);
}
return SceneState::NONE;
}
void draw(Shader &shader) override {
if (!_backgroundSprite || !_uiShader)
return;
glDisable(GL_DEPTH_TEST);
_uiShader->bind();
Zappy::Math::mat4 orthoProjection = Zappy::Math::ortho(0.0f, WIDTH, HEIGHT, 0.0f, -1.0f, 1.0f);
Zappy::Math::mat4 view;
_backgroundSprite->draw(*_uiShader, view, orthoProjection);
for (auto &button : _buttons) {
button->draw(*_uiShader);
}
glEnable(GL_DEPTH_TEST);
}
void onExit() override {
_buttons.clear();
_backgroundSprite.reset();
_uiShader.reset();
}
};
}
2 changes: 1 addition & 1 deletion gui/src/SceneManager/SceneManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SceneManager {
changeScene(std::make_unique<MainTitle>(_textureManager, _audios));
break;
case SceneState::MENU:
changeScene(std::make_unique<GameScene>(_textureManager));
changeScene(std::make_unique<GameScene>(_textureManager, networkManager));
break;
case SceneState::LOAD_NETWORK:
changeScene(
Expand Down
Loading