Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(SOURCE_FILES
engine/common/enums.hxx
engine/common/platform.hxx
engine/common/JsonSerialization.hxx
engine/events/GameEvents.{hxx,cxx}
engine/GameObjects/MapNode.{hxx,cxx}
engine/map/MapFunctions.{hxx,cxx}
engine/GameObjects/MapGrid.hxx
Expand Down
42 changes: 26 additions & 16 deletions src/MainMenu.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ bool mainMenu()
int logoTexW, logoTexH;
SDL_QueryTexture(logoTex, nullptr, nullptr, &logoTexW, &logoTexH);

auto beginFrame = [] {
auto beginFrame = []
{
SDL_RenderClear(WindowManager::instance().getRenderer());

WindowManager::instance().newImGuiFrame();
Expand All @@ -66,10 +67,13 @@ bool mainMenu()
ui::SetNextWindowSize(ui::GetIO().DisplaySize);

bool open = true;
ui::Begin("MainWnd", &open, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
ui::Begin("MainWnd", &open,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse);
};

auto renderFrame = [] {
auto renderFrame = []
{
ui::End();

WindowManager::instance().renderScreen();
Expand All @@ -82,7 +86,7 @@ bool mainMenu()
{
beginFrame();

// break the loop if an event occurs
// break the loop if an event occurs
const bool has_event = SDL_PollEvent(&event) != 0;
if (has_event && event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_KEYDOWN)
opacity = 254;
Expand All @@ -93,7 +97,6 @@ bool mainMenu()
ui::Image(logoTex, ImVec2(logoTexW, logoTexH), ImVec2(0, 0), ImVec2(1, 1), ImVec4{op, op, op, op});
ui::PopStyleVar(1);


renderFrame();
}

Expand All @@ -102,10 +105,12 @@ bool mainMenu()
{
beginFrame();

while (SDL_PollEvent(&event) != 0) {
while (SDL_PollEvent(&event) != 0)
{
ImGui_ImplSDL2_ProcessEvent(&event);

if (event.type == SDL_QUIT) {
if (event.type == SDL_QUIT)
{
startGame = false;
mainMenuLoop = false;
}
Expand All @@ -123,41 +128,46 @@ bool mainMenu()
constexpr int buttonInterval = 20;
ImVec2 buttonPos(screenWidth / 2 - buttonSize.x / 2, screenHeight / 2 - buttonSize.y);
ui::SetCursorPos(buttonPos);
if (ui::ButtonCt("New Game", { 200, 40 })) {
#ifdef USE_AUDIO
if (ui::ButtonCt("New Game", {200, 40}))
{
#ifdef USE_AUDIO
playAudioMajorSelection();
#endif // USE_AUDIO
#endif // USE_AUDIO
mainMenuLoop = false;
SignalMediator::instance().signalNewGame.emit(true);
}

buttonPos.y += buttonSize.y + buttonInterval;
ui::SetCursorPos(buttonPos);
if (ui::ButtonCt("Load Game", { 200, 40 })) {
#ifdef USE_AUDIO
if (ui::ButtonCt("Load Game", {200, 40}))
{
#ifdef USE_AUDIO
playAudioMajorSelection();
#endif // USE_AUDIO
#endif // USE_AUDIO
SignalMediator::instance().signalLoadGame.emit("save.cts");
mainMenuLoop = false;
}

buttonPos.y += buttonSize.y + buttonInterval;
ui::SetCursorPos(buttonPos);
if (ui::ButtonCt("Quit Game", { 200, 40 })) {
if (ui::ButtonCt("Quit Game", {200, 40}))
{
startGame = false;
mainMenuLoop = false;
}

constexpr int xOffset = 5, btnSize = 32;
ImVec2 leftBottom(xOffset, screenHeight - btnSize - xOffset * 2);
ui::SetCursorPos(leftBottom);
if (ui::ImageButton(discordTex, ImVec2(btnSize, btnSize))) {
if (ui::ImageButton(discordTex, ImVec2(btnSize, btnSize)))
{
OSystem::openDir("https://discord.gg/MG3tgYV6ce");
}

leftBottom.x += xOffset * 2 + btnSize; // xOffset * 2 because, need interval between buttons
ui::SetCursorPos(leftBottom);
if (ui::ImageButton(githubTex, ImVec2(btnSize, btnSize))) {
if (ui::ImageButton(githubTex, ImVec2(btnSize, btnSize)))
{
OSystem::openDir("https://github.com/CytopiaTeam/Cytopia/issues/new");
}

Expand Down
91 changes: 38 additions & 53 deletions src/engine/EventManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "Sprite.hxx"
#include "UIManager.hxx"
#include <MapFunctions.hxx>
// #include <GameEvents.hxx>
#include "events/GameEvents.hxx"

#include "LOG.hxx"

Expand Down Expand Up @@ -54,21 +56,21 @@ void EventManager::pickTileUnderCursor(Point mouseIsoCoords)
// update placement mode
switch (topMostActiveLayer)
{
case Layer::BUILDINGS:
GameStates::instance().placementMode = PlacementMode::SINGLE;
break;
case Layer::ROAD:
case Layer::POWERLINES:
case Layer::UNDERGROUND:
GameStates::instance().placementMode = PlacementMode::LINE;
break;
case Layer::GROUND_DECORATION:
case Layer::WATER:
case Layer::ZONE:
GameStates::instance().placementMode = PlacementMode::RECTANGLE;
break;
default:
break;
case Layer::BUILDINGS:
GameStates::instance().placementMode = PlacementMode::SINGLE;
break;
case Layer::ROAD:
case Layer::POWERLINES:
case Layer::UNDERGROUND:
GameStates::instance().placementMode = PlacementMode::LINE;
break;
case Layer::GROUND_DECORATION:
case Layer::WATER:
case Layer::ZONE:
GameStates::instance().placementMode = PlacementMode::RECTANGLE;
break;
default:
break;
}
mapNodeData = node.getMapNodeData();
tileToPlace = mapNodeData[topMostActiveLayer].tileID;
Expand All @@ -88,6 +90,9 @@ void EventManager::checkEvents(SDL_Event &event)
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL2_ProcessEvent(&event);
//TODO: Handle UI Events before game

GameEvents::instance().processEvents(event);

switch (event.type)
{
Expand Down Expand Up @@ -302,31 +307,14 @@ void EventManager::checkEvents(SDL_Event &event)
{
// clear highlighting
unHighlightNodes();
// GameEvents::instance().unHighlightNodes;

// if we're panning, move the camera and break
if (m_panning)
{
if ((event.motion.xrel == 0) && (event.motion.yrel == 0))
{
return;
}
Camera::instance().moveCamera(event.motion.xrel, event.motion.yrel);
}
// check if we should highlight tiles and if we're in placement mode
if (highlightSelection)
{
mouseScreenCoords = {event.button.x, event.button.y};
mouseIsoCoords = convertScreenToIsoCoordinates(mouseScreenCoords);

// if it's a multi-node tile, get the origin corner point
Point origCornerPoint =
MapFunctions::instance().getNodeOrigCornerPoint(mouseIsoCoords, TileManager::instance().getTileLayer(tileToPlace));

if (origCornerPoint == Point::INVALID())
{
origCornerPoint = mouseIsoCoords;
}

// canceling transparent buildings
for (const auto &it : m_transparentBuildings)
{
Expand Down Expand Up @@ -420,12 +408,13 @@ void EventManager::checkEvents(SDL_Event &event)
}

m_placementAllowed = MapFunctions::instance().isPlacementOnAreaAllowed(m_nodesToHighlight, tileToPlace);

// Finally highlight all the tiles we've found
// Set highlighted tiles that can be placed and can't be placed different color
for (const auto &highlitNode : m_nodesToHighlight)
{
if (!MapFunctions::instance().isPlacementOnNodeAllowed(highlitNode, tileToPlace) || demolishMode)
if (!m_placementAllowed || demolishMode)
// if (!MapFunctions::instance().isPlacementOnNodeAllowed(highlitNode, tileToPlace) || demolishMode)
{
// mark red
MapFunctions::instance().highlightNode(highlitNode, SpriteHighlightColor::RED);
Expand Down Expand Up @@ -454,7 +443,7 @@ void EventManager::checkEvents(SDL_Event &event)
}
break;
case SDL_MOUSEBUTTONDOWN:
m_placementAllowed = false;
// m_placementAllowed = false;
m_skipLeftClick = false;
// check for UI events first
for (const auto &it : uiManager.getAllUiElementsForEventHandling())
Expand All @@ -465,14 +454,13 @@ void EventManager::checkEvents(SDL_Event &event)
break;
}
}

if (event.button.button == SDL_BUTTON_RIGHT)
switch (event.button.button)
{
case SDL_BUTTON_RIGHT:
m_panning = true;
m_cancelTileSelection = true;
}
else if (event.button.button == SDL_BUTTON_LEFT)
{
break;
case SDL_BUTTON_LEFT:
// game event handling
mouseScreenCoords = {event.button.x, event.button.y};
mouseIsoCoords = convertScreenToIsoCoordinates(mouseScreenCoords);
Expand All @@ -487,6 +475,7 @@ void EventManager::checkEvents(SDL_Event &event)
{
if (!coordinate.isWithinMapBoundaries())
{
LOG(LOG_ERROR) << "not canPlac e";
canPlaceTileID = false;
break;
}
Expand All @@ -496,7 +485,6 @@ void EventManager::checkEvents(SDL_Event &event)
if (canPlaceTileID)
{
m_clickDownCoords = mouseIsoCoords;
m_placementAllowed = true;

// Nodes to place are collected during the mouse move.
// In case of multiple left clicks without moving the mouse, node to place will be the node of the mouse click.
Expand All @@ -506,6 +494,11 @@ void EventManager::checkEvents(SDL_Event &event)
}
m_isPuttingTile = true;
}
else
{
LOG(LOG_ERROR) << "boeser else";
m_placementAllowed = false;
}
}
break;

Expand Down Expand Up @@ -601,7 +594,8 @@ void EventManager::checkEvents(SDL_Event &event)
if (highlightSelection)
{
m_nodesToHighlight.push_back(mouseIsoCoords);
if (!tileToPlace.empty() && !MapFunctions::instance().setTileID(tileToPlace, mouseIsoCoords))
if (!tileToPlace.empty() && !MapFunctions::instance().isPlacementOnAreaAllowed(m_nodesToHighlight, tileToPlace))
// if (!tileToPlace.empty() && !MapFunctions::instance().setTileID(tileToPlace, mouseIsoCoords))
{
MapFunctions::instance().highlightNode(mouseIsoCoords, SpriteHighlightColor::RED);
}
Expand All @@ -613,16 +607,7 @@ void EventManager::checkEvents(SDL_Event &event)

break;
}
case SDL_MOUSEWHEEL:
if (event.wheel.y > 0)
{
Camera::instance().increaseZoomLevel();
}
else if (event.wheel.y < 0)
{
Camera::instance().decreaseZoomLevel();
}
break;


default:
break;
Expand Down
Loading