Various fixes, game running nice.

This commit is contained in:
2025-01-28 19:07:53 -05:00
parent c92d74a20c
commit c069af8ed5
11 changed files with 194 additions and 33 deletions

View File

@@ -43,17 +43,17 @@ CPMAddPackage(
CPMAddPackage(
NAME ReWindow
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-31.zip
URL https://git.redacted.cc/Redacted/ReWindow/archive/Prerelease-32.zip
)
CPMAddPackage(
NAME JGL
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-46.zip
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-47.zip
)
CPMAddPackage(
NAME JUI
URL https://git.redacted.cc/josh/ReJUI/archive/Prerelease-5.6.zip
URL https://git.redacted.cc/josh/ReJUI/archive/Prerelease-5.10.zip
)
CPMAddPackage(

View File

@@ -13,7 +13,7 @@
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include "SceneManager.hpp"
#include <rewindow/types/key.h>
#include <ReWindow/types/Key.h>
namespace CaveGame::Client
{

View File

@@ -0,0 +1,88 @@
#pragma once
#include <JUI/Widgets/Window.hpp>
#include <JUI/Widgets/Slider.hpp>
#include <JUI/Widgets/ListLayout.hpp>
namespace CaveGame::Client
{
using namespace JUI;
class TileTool : public JUI::Window
{
public:
Event<float> BrushSizeChanged;
Event<float> BrushPercentChanged;
explicit TileTool(Widget* parent) : JUI::Window(parent)
{
this->SetTitle("ReWorldEdit v1");
this->MinSize({200, 400});
this->Size({200, 400, 0, 0});
int row_height = 20;
auto* column_layout = new HorizontalListLayout(this->GetViewportInstance());
auto* col_left = new Rect(column_layout);
col_left->BGColor({64, 64, 64, 128});
col_left->Size({0, 0, 0.35f, 1.f});
auto* left_row_layout = new VerticalListLayout(col_left);
auto* tool_size_label = new TextRect(left_row_layout);
tool_size_label->Size({0, row_height, 1, 0});
tool_size_label->SetContent("Size: 8");
tool_size_label->BGColor(Colors::Transparent);
auto* tool_percent_label = new TextRect(left_row_layout);
tool_percent_label->Size({0, row_height, 1, 0});
tool_percent_label->SetContent("Percent: 100%");
tool_percent_label->BGColor(Colors::Transparent);
auto* col_right = new Rect(column_layout);
col_right->BGColor({128, 128, 128, 128});
col_right->Size({0, 0, 0.65f, 1.f});
auto* right_row_layout = new VerticalListLayout(col_right);
brush_size_slider = new Slider(right_row_layout);
brush_size_slider->Size({0, row_height, 1, 0});
brush_size_slider->Maximum(1.f);
brush_size_slider->Minimum(0.01f);
brush_size_slider->Interval(0.001f);
brush_size_slider->ValueChanged += [&, this, tool_size_label] (float val)
{
float newval = val * 50;
tool_size_label->SetContent(std::format("Size: {}", Math::Round(newval, 1)));
BrushSizeChanged(newval);
};
brush_size_slider->CurrentValue(0.5f);
brush_percent_slider = new Slider(right_row_layout);
brush_percent_slider->Size({0, row_height, 1, 0});
brush_percent_slider->Maximum(1.f);
brush_percent_slider->Minimum(0.f);
brush_percent_slider->Interval(0.001f);
brush_percent_slider->ValueChanged += [&, this, tool_percent_label] (float val)
{
float newval = val * 100.f;
tool_percent_label->SetContent(std::format("Percent: {}%", Math::Floor(newval)));
BrushPercentChanged(newval);
};
brush_percent_slider->CurrentValue(1.f);
}
void Enable(bool value)
{
this->enabled = value;
}
protected:
Slider* brush_size_slider;
Slider* brush_percent_slider;
bool enabled = false;
private:
};
}

View File

@@ -1,5 +1,5 @@
#include <Client/Camera2D.hpp>
#include <rewindow/inputservice.hpp>
#include <ReWindow/InputService.h>
namespace CaveGame::Client
{

View File

@@ -2,7 +2,7 @@
#include <Core/Entity.hpp>
#include <Core/Player.hpp>
#include <JGL/JGL.h>
#include <rewindow/inputservice.hpp>
#include <ReWindow/InputService.h>
// TODO: Move this shit to Client/Player.cpp
void CaveGame::Core::Player::Draw() {

View File

@@ -4,7 +4,7 @@
#include <JUI/Widgets/ListLayout.hpp>
#include <JUI/UDim.hpp>
#include <JUI/UDim2.hpp>
#include <rewindow/inputservice.hpp>
#include <ReWindow/InputService.h>
#include "JUI/Widgets/TextRect.hpp"
#include "JUI/UDim.hpp"
@@ -167,6 +167,9 @@ void CaveGame::Client::GameSession::UnloadHUD() {
}
CaveGame::Core::TileID CaveGame::Client::GameSession::GetTileUnderMouse() {
Vector2 coords = world->camera.ScreenToWorld(InputService::GetMousePosition());
auto ipos = InputService::GetMousePosition();
Vector2 pos = Vector2(ipos.x, ipos.y);
Vector2 coords = world->camera.ScreenToWorld(pos);
return world->GetTile(coords.x, coords.y);
}

View File

@@ -0,0 +1 @@
#include <Client/TileTool.hpp>

View File

@@ -25,7 +25,7 @@ target_include_directories(CaveClientApp PUBLIC
target_include_directories(CaveClientApp PUBLIC "include")
target_link_libraries(CaveClientApp PUBLIC CaveClient ReWindowLibrary J3ML JGL )
target_link_libraries(CaveClientApp PUBLIC CaveClient ReWindow J3ML JGL )
target_compile_definitions(CaveClientApp PUBLIC CAVE_CLIENT)

View File

@@ -8,11 +8,12 @@
#include <Client/GameSession.hpp>
#include <JUI/Widgets/ListLayout.hpp>
#include <JUI/UDim.hpp>
#include <rewindow/types/window.h>
#include <ReWindow/types/Window.h>
#include <JUI/Widgets/Window.hpp>
#include <Client/Scene.hpp>
#include <Client/SceneManager.hpp>
#include <Client/Console.hpp>
#include <Client/TileTool.hpp>
namespace CaveGame::ClientApp
{
@@ -22,7 +23,7 @@ namespace CaveGame::ClientApp
/// The main program class. Everything originates here.
/// This class is derived from RWindow class.
class CaveGameWindow : public ReWindow::RWindow, public SceneManager
class CaveGameWindow : public ReWindow::OpenGLWindow, public SceneManager
{
public:
[[nodiscard]] bool InGameSession() const;
@@ -34,6 +35,7 @@ namespace CaveGame::ClientApp
JUI::Scene* wm;
JUI::Window* settings_window;
JUI::Window* credits_window;
Client::TileTool* tile_tool;
Client::Console* console_window;
JUI::Window* stats_window;
//Scene* current_scene = nullptr;
@@ -48,6 +50,14 @@ namespace CaveGame::ClientApp
~CaveGameWindow();
Vector2 GetMouseV2() const;
Vector2 GetSizeV2() const
{
auto isize = GetSize();
return Vector2(isize.x, isize.y);
}
bool InGame() const;
/// This function runs the totality of the game loop, start to finish.
@@ -103,7 +113,10 @@ namespace CaveGame::ClientApp
void InGameControls(float elapsed);
float tool_radius = 8.f;
float tool_percent = 100.f;
void OnMouseWheel(const ReWindow::MouseWheelEvent &) override;
void create_tool_window();
};
}

View File

@@ -16,7 +16,7 @@
#include <JGL/JGL.h>
#include <ClientApp/CaveGameWindow.hpp>
#include <Core/Loggers.hpp>
#include <rewindow/logger/logger.h>
#include <ReWindow/Logger.h>
#include <Core/Tile.hpp>
#include <JGL/logger/logger.h>

View File

@@ -5,12 +5,12 @@
#include <Core/Explosion.hpp>
#include <Core/Loggers.hpp>
#include <Core/Player.hpp>
#include <rewindow/inputservice.hpp>
#include <ReWindow/InputService.h>
#include <Core/Macros.hpp>
namespace CaveGame::ClientApp
{
CaveGameWindow::CaveGameWindow(const std::string& title, int width, int height): ReWindow::RWindow(title, width, height)
CaveGameWindow::CaveGameWindow(const std::string& title, int width, int height): ReWindow::OpenGLWindow(title, width, height, 2, 1)
{
Logs::Info("Creating game window.");
@@ -73,7 +73,7 @@ namespace CaveGame::ClientApp
void CaveGameWindow::Run()
{
this->SetRenderer(RenderingAPI::OPENGL);
//this->SetRenderer(RenderingAPI::OPENGL);
this->Open();
this->Init();
this->SetResizable(true);
@@ -107,12 +107,27 @@ namespace CaveGame::ClientApp
settings_window->Visible(false);
}
void CaveGameWindow::create_tool_window()
{
tile_tool = new Client::TileTool(wm);
tile_tool->BrushSizeChanged += [this] (float value)
{
tool_radius = value;
};
tile_tool->BrushPercentChanged += [this] (float value)
{
tool_percent = value;
};
}
void CaveGameWindow::create_window_widgets()
{
create_console_window();
credits_window = Client::CreateCreditsWindowWidget(wm);
create_stats_window();
create_settings_window();
create_tool_window();
}
void CaveGameWindow::Init()
@@ -121,9 +136,11 @@ namespace CaveGame::ClientApp
gladLoadGL();
// Init Josh Graphics
bool result = JGL::Init(GetSize(), 0.f, 0.f);
auto size = GetSize();
Vector2 vsize = Vector2(size.x, size.y);
bool result = JGL::Init(vsize, 0.f, 0.f);
//JGL::InitTextEngine();
JGL::Update(GetSize());
JGL::Update(vsize);
// Fetch core assets
// TODO: Asset manager? Need to share references and pointers of assets between many files..
@@ -134,6 +151,9 @@ namespace CaveGame::ClientApp
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
// TODO: Delete when we update to the next release of JGL
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // NOTE: This MUST be called for text rendering to work properly!!!
// Construct menu elements
create_window_widgets();
}
@@ -141,6 +161,8 @@ namespace CaveGame::ClientApp
void CaveGameWindow::tile_draw(int x, int y, int radius, Core::TileID tile)
{
RNG rng (x + y);
game_ctx->world->SetTile(x, y, tile);
for (int dx = -radius; dx <= radius; ++dx)
@@ -148,8 +170,13 @@ namespace CaveGame::ClientApp
for (int dy = -radius; dy <= radius; ++dy)
{
//if (Math::Abs(dx)+Math::Abs(dy) < (radius)/(Math::PiOverTwo))
if ( Math::Abs(dx*dx) + Math::Abs(dy*dy) < (radius*radius))
game_ctx->world->SetTile(x+dx, y+dy, tile);
if (tool_percent >= 100 || tool_percent > rng.Float(0, 99))
{
if ( Math::Abs(dx*dx) + Math::Abs(dy*dy) < (radius*radius))
game_ctx->world->SetTile(x+dx, y+dy, tile);
}
//if ( Math::Abs(dx*dx) + Math::Abs(dy*dy) < (radius*radius))
//game_ctx->world->SetTile(x+dx, y+dy, tile);
}
}
}
@@ -206,17 +233,23 @@ namespace CaveGame::ClientApp
// Tile placer tool code
// Temp hack until Input Focus Priority is implemented.
for (auto window : wm->GetChildren())
if (window->IsMouseInside())
return;
if (IsMouseButtonDown(MouseButtons::Left))
{
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseCoordinates()) - Vector2{0.5f, 0.5f};
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseV2()) - Vector2{0.5f, 0.5f};
tile_draw(transformed.x, transformed.y, tool_radius, Core::TileID::AIR);
}
if (IsMouseButtonDown(MouseButtons::Right))
{
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseCoordinates()) - Vector2{0.5f, 0.5f};
Vector2 transformed = game_ctx->world->camera.ScreenToWorld(GetMouseV2()) - Vector2{0.5f, 0.5f};
tile_draw(transformed.x, transformed.y, tool_radius, game_ctx->GetHotbarTile());
@@ -226,6 +259,9 @@ namespace CaveGame::ClientApp
void CaveGameWindow::Update(float elapsed)
{
// Update floating windows.
wm->Update(elapsed);
// TODO: We can't tell the game to change scenes from within a scene, currently.
if (current_scene == splash_ctx && splash_ctx->SplashComplete())
ChangeScene(menu_ctx);
@@ -233,26 +269,33 @@ namespace CaveGame::ClientApp
// Update Current Scene
if (current_scene != nullptr)
{
current_scene->PassWindowSize(GetSize());
auto isize = GetSize();
Vector2 size = Vector2(isize.x, isize.y);
current_scene->PassWindowSize(size);
current_scene->Update(elapsed);
// TODO: current_scene->PassInputState(...); ?
}
// Update floating windows.
wm->Update(elapsed);
if (current_scene == game_ctx)
{
game_ctx->world->mouse_pos = GetMouseCoordinates();
game_ctx->world->mouse_pos = GetMouseV2();
InGameControls(elapsed);
}
tile_tool->Visible(InGame());
tile_tool->Enable(InGame());
}
void CaveGameWindow::Draw()
{
JGL::Update(GetSize());
auto isize = GetSize();
Vector2 size = Vector2(isize.x, isize.y);
JGL::Update(size);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
@@ -309,7 +352,9 @@ namespace CaveGame::ClientApp
glTranslatef(-camera.Position().x, -camera.Position().y, 0);
auto pos = camera.ScreenToWorld(currentMouse.Position);
auto mpos = Vector2(currentMouse.Position.x, currentMouse.Position.y);
auto pos = camera.ScreenToWorld(mpos);
JGL::J2D::OutlineCircle(Colors::Red, pos, tool_radius);
@@ -330,7 +375,7 @@ namespace CaveGame::ClientApp
if (glError != GL_NO_ERROR) {
Logs::Error(std::format("OpenGL Error: {}", glError));
}
GLSwapBuffers();
SwapBuffers();
}
@@ -377,7 +422,12 @@ namespace CaveGame::ClientApp
}
if (ev.key == Keys::P) {
auto coords = game_ctx->world->camera.ScreenToWorld(InputService::GetMousePosition());
auto ipos = InputService::GetMousePosition();
auto pos = Vector2(ipos.x, ipos.y);
auto coords = game_ctx->world->camera.ScreenToWorld(pos);
auto* plr = new Core::Explosion(game_ctx->world, coords, 1.f, tool_radius);
//auto* plr2 = new Core::Player(coords);
//game_ctx->world->AddEntity(plr2);
@@ -434,13 +484,14 @@ namespace CaveGame::ClientApp
RWindow::OnMouseButtonUp(ev);
}
void CaveGameWindow::OnMouseMove(const ReWindow::MouseMoveEvent& ev)
{
void CaveGameWindow::OnMouseMove(const ReWindow::MouseMoveEvent& ev) {
Vector2 pos = Vector2(ev.Position.x, ev.Position.y);
if (current_scene != nullptr)
current_scene->PassMouseMovement(ev.Position);
current_scene->PassMouseMovement(pos);
wm->ObserveMouseMovement(ev.Position);
wm->ObserveMouseMovement(pos);
RWindow::OnMouseMove(ev);
}
@@ -500,6 +551,11 @@ namespace CaveGame::ClientApp
return current_scene == game_ctx;
}
Vector2 CaveGameWindow::GetMouseV2() const {
auto coords = GetMouseCoordinates();
return Vector2(coords.x, coords.y);
}
}