Skip to content

Commit

Permalink
resource changes squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
psyinf committed Nov 18, 2024
1 parent 12dc520 commit a27e39c
Show file tree
Hide file tree
Showing 116 changed files with 2,375 additions and 1,204 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out/*
.vs/*
.vs/*
*.bak
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ add_subdirectory(libs)
add_subdirectory(apps)
add_subdirectory(tests)
add_subdirectory(tools)

install(DIRECTORY
data/
DESTINATION
Expand Down
3 changes: 2 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
},
"cacheVariables": {
"ENABLE_CPPCHECK_DEFAULT": "FALSE",
"ENABLE_CLANG_TIDY_DEFAULT": "FALSE"
"ENABLE_CLANG_TIDY_DEFAULT": "FALSE",
"VCPKG_TARGET_TRIPLET": "x64-windows"
}
},
{
Expand Down
1 change: 1 addition & 0 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ add_subdirectory(textdemo)
add_subdirectory(stellarfield)
add_subdirectory(galaxy)
add_subdirectory(productionSandbox)
add_subdirectory(markov_gen)
1 change: 1 addition & 0 deletions apps/asteroids/components/Entities.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using PassiveCollider = entt::tag<"PassiveCollider"_hs>;
struct Dynamics
{
pg::fVec2 velocity{};
float angularVelocity{};
pg::fVec2 dampening{1.0f, 1.0f};
};

Expand Down
9 changes: 8 additions & 1 deletion apps/asteroids/events/Collision.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#pragma once
#include <pgEngine/math/Vec.hpp>
#include <pgfoundation/NamedTypeRegistrar.hpp>

namespace asteroids { namespace events {

struct Collision
struct Collision : public pgf::TypeRegistrar<Collision, "Collision">
{
Collision(entt::entity c1, entt::entity c2)
: c1(c1)
, c2(c2)
{
}

entt::entity c1;
entt::entity c2;
};
Expand Down
4 changes: 3 additions & 1 deletion apps/asteroids/events/LaserFired.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#include <pgEngine/math/Vec.hpp>
#include <entt/entt.hpp>

#include <pgFoundation/NamedTypeRegistrar.hpp>

namespace asteroids { namespace events {

struct LaserFired
struct LaserFired : pgf::TypeRegistrar<LaserFired, "LaserFired">
{
pg::fVec2 offset;
entt::entity shooter;
Expand Down
35 changes: 22 additions & 13 deletions apps/asteroids/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,36 @@
#include <systems/RenderSystem.hpp>
#include <systems/SoundSystem.hpp>

#include <pgGame/systems/SystemsRegistry.hpp>

int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
try
{
pg::game::Game game;
game.createScene("start", {});
auto& scene = game.switchScene("start");
auto& systems = scene.getSystems();
systems.emplace_back(std::make_unique<asteroids::Lasers>(game));
systems.emplace_back(std::make_unique<asteroids::Player>(game));
systems.emplace_back(std::make_unique<asteroids::Asteroids>(game));
systems.emplace_back(std::make_unique<asteroids::Background>(game));
systems.emplace_back(std::make_unique<asteroids::Collisions>(game));
systems.emplace_back(std::make_unique<asteroids::RenderSystem>(game));
systems.emplace_back(std::make_unique<asteroids::DynamicsSystem>(game));
systems.emplace_back(std::make_unique<asteroids::SoundSystem>(game));

pg::game::SystemsFactory::registerSystem<asteroids::Lasers>("lasers");
pg::game::SystemsFactory::registerSystem<asteroids::Player>("player");
pg::game::SystemsFactory::registerSystem<asteroids::Asteroids>("asteroids");
pg::game::SystemsFactory::registerSystem<asteroids::Background>("background");
pg::game::SystemsFactory::registerSystem<asteroids::Collisions>("collisions");
pg::game::SystemsFactory::registerSystem<asteroids::RenderSystem>("renderSystem");
pg::game::SystemsFactory::registerSystem<asteroids::DynamicsSystem>("dynamicsSystem");
pg::game::SystemsFactory::registerSystem<asteroids::SoundSystem>("soundSystem");
game.createScene("start",
{.systems{"lasers",
"player",
"asteroids",
"background",
"collisions",
"renderSystem",
"dynamicsSystem",
"soundSystem"}});
game.switchScene("start");

// TODO: from external config
game.getCurrentScene().addSingleton<asteroids::RenderConfig>(
asteroids::RenderConfig{.renderBroadPhaseCollisionShapes = true});
scene.start();

game.loop();
return 0;
}
Expand All @@ -46,7 +55,7 @@ catch (std::exception& e)

catch (...)
{
fmt::print("Unhandled exception");
fmt::print("Unhandled exception\n");
errorTrace::printErrorTrace();
return -1;
}
39 changes: 20 additions & 19 deletions apps/asteroids/systems/Asteroids.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ std::optional<std::pair<entt::entity, entt::entity>> getAsteroidWeaponPair(
pg::game::Game& game,
const asteroids::events::Collision& collision)
{
auto isAsteroidE1 = game.getRegistry().all_of<asteroids::Asteroids::tag>(collision.c1);
auto isAsteroidE2 = game.getRegistry().all_of<asteroids::Asteroids::tag>(collision.c2);
auto isLaserE1 = game.getRegistry().all_of<asteroids::Lasers::tag>(collision.c1);
auto isLaserE2 = game.getRegistry().all_of<asteroids::Lasers::tag>(collision.c2);
auto isAsteroidE1 = game.getGlobalRegistry().all_of<asteroids::Asteroids::tag>(collision.c1);
auto isAsteroidE2 = game.getGlobalRegistry().all_of<asteroids::Asteroids::tag>(collision.c2);
auto isLaserE1 = game.getGlobalRegistry().all_of<asteroids::Lasers::tag>(collision.c1);
auto isLaserE2 = game.getGlobalRegistry().all_of<asteroids::Lasers::tag>(collision.c2);
std::pair<entt::entity, entt::entity> retVal;
// TODO: we need a different strategy here.
// 1. we need to get rid of the pair-wise checking (e.g. asteroid vs laser + laser vs asteroid)
Expand All @@ -73,14 +73,14 @@ std::optional<std::pair<entt::entity, entt::entity>> getAsteroidWeaponPair(
return retVal;
}

void asteroids::Asteroids::setup()
void asteroids::Asteroids::setup(std::string_view /*scene_id*/)
{
for ([[maybe_unused]] auto _ : std::views::iota(0, 4))
{
auto [pos, vel] = AsteroidsRandomGen::makeInitial();
createAsteroid(pos, vel, Size::Large);
}
game.getDispatcher().sink<asteroids::events::Collision>().connect<&Asteroids::handleEvent>(this);
_game.getDispatcher().sink<asteroids::events::Collision>().connect<&Asteroids::handleEvent>(this);
}

void asteroids::Asteroids::createAsteroid(const pg::fVec2& position, const pg::fVec2& velocity, Size size)
Expand All @@ -92,6 +92,7 @@ void asteroids::Asteroids::createAsteroid(const pg::fVec2& position, const pg::f
std::uint16_t damage;
} asteroidConf;

float rotationVelocity = 12.0f;
switch (size)
{
case Size::Large: {
Expand All @@ -113,24 +114,24 @@ void asteroids::Asteroids::createAsteroid(const pg::fVec2& position, const pg::f
}

auto sprite =
game.getResource<pg::Sprite, sdl::Renderer&>(std::string(asteroidConf.resource), game.getApp().getRenderer());
_game.getResource<pg::Sprite, sdl::Renderer&>(std::string(asteroidConf.resource), _game.getApp().getRenderer());
auto entity = pg::game::makeEntity<pg::game::Drawable,
pg::Transform2D,
Dynamics,
pg::BoundingSphere,
HitPoints,
Damage,
Size> //
(game.getRegistry(), //
(_game.getGlobalRegistry(), //
pg::game::Drawable{sprite}, //
pg::Transform2D{.pos{position}}, //
{.velocity{velocity}}, //
{.velocity{velocity}, .angularVelocity{rotationVelocity}}, //
{pg::BoundingSphere::fromRectangle(sprite->getDimensions())}, //
{asteroidConf.hitPoints}, //
{asteroidConf.damage}, //
{std::move(size)} //
);
pg::game::addComponents<PassiveCollider, tag>(game.getRegistry(), entity);
pg::game::addComponents<PassiveCollider, tag>(_game.getGlobalRegistry(), entity);
}

void asteroids::Asteroids::handle(const pg::game::FrameStamp&)
Expand All @@ -141,7 +142,7 @@ void asteroids::Asteroids::handle(const pg::game::FrameStamp&)
std::uniform_int_distribution pos(-200, 1024);

// TODO: flag entities that should re-appear after entering a screen border and handle them in a separate system
auto view = game.getRegistry().view<tag, pg::Transform2D, asteroids::Dynamics>();
auto view = _game.getGlobalRegistry().view<tag, pg::Transform2D, asteroids::Dynamics>();
for (auto& entity : view)
{
auto&& [transform, dynamics] = view.get<pg::Transform2D, asteroids::Dynamics>(entity);
Expand All @@ -156,13 +157,13 @@ void asteroids::Asteroids::handle(const pg::game::FrameStamp&)
// handle events:
for (auto&& collision : collisions)
{
auto collisionPair = getAsteroidWeaponPair(game, collision);
auto collisionPair = getAsteroidWeaponPair(_game, collision);
if (!collisionPair.has_value()) { continue; }
auto&& [asteroid, laser] = collisionPair.value();

// TODO: make based on damage vs hitpoints
auto&& [transform, dynamics, size] =
game.getRegistry().get<pg::Transform2D, asteroids::Dynamics, Size>(asteroid);
_game.getGlobalRegistry().get<pg::Transform2D, asteroids::Dynamics, Size>(asteroid);

auto newSize = getNextSmallest(size);
if (newSize.has_value())
Expand All @@ -175,17 +176,17 @@ void asteroids::Asteroids::handle(const pg::game::FrameStamp&)
}
// trigger explosion
createExplosion(transform.pos);
game.getRegistry().destroy(asteroid);
game.getRegistry().destroy(laser);
_game.getGlobalRegistry().destroy(asteroid);
_game.getGlobalRegistry().destroy(laser);
}
}

void asteroids::Asteroids::createExplosion(pg::fVec2& position)
{
auto entity = pg::game::makeEntity<pg::Transform2D>(game.getRegistry(), pg::Transform2D{.pos{position}});
auto entity = pg::game::makeEntity<pg::Transform2D>(_game.getGlobalRegistry(), pg::Transform2D{.pos{position}});

auto animation = std::make_shared<pg::FramedSprite>(pg::SpriteFactory::makeFramedSprite(
game.getApp().getRenderer(),
_game.getApp().getRenderer(),
8,
4,
"../data/effects/explosion_1_8x4.png",
Expand All @@ -194,10 +195,10 @@ void asteroids::Asteroids::createExplosion(pg::fVec2& position)
if (frame_number >= max_frames)
{
//
game.getDispatcher().enqueue<pg::game::events::DestroyEntityEvent>({entity});
_game.getDispatcher().enqueue<pg::game::events::DestroyEntityEvent>({.entity = entity});
}
}));
pg::game::addComponents<pg::game::Drawable>(game.getRegistry(), entity, pg::game::Drawable{animation});
pg::game::addComponents<pg::game::Drawable>(_game.getGlobalRegistry(), entity, pg::game::Drawable{animation});

// pg::game::Drawable,
// pg::game::Drawable{animation},
Expand Down
2 changes: 1 addition & 1 deletion apps/asteroids/systems/Asteroids.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Asteroids : public pg::game::SystemInterface

using tag = entt::tag<"Asteroids"_hs>;

void setup();
void setup(std::string_view scene_id);
/**
* @brief Creates an asteroid entity in the game.
*
Expand Down
14 changes: 7 additions & 7 deletions apps/asteroids/systems/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
void asteroids::Background::handle(const pg::game::FrameStamp&)
{
// TODO: base scrolling speed on the player's velocity
auto& registry = game.getRegistry();
auto view = registry.view<pg::Transform2D, asteroids::Dynamics, backgroundTag>();
auto view = _game.getGlobalRegistry().view<pg::Transform2D, asteroids::Dynamics, backgroundTag>();
}

void asteroids::Background::setup()
void asteroids::Background::setup(std::string_view scene_id)
{
auto background = game.getRegistry().create();
auto& registry = game.getRegistry();
auto backgroundImg = pg::SpriteFactory::makeSprite(game.getApp().getRenderer(), "../data/spr_stars01.png");
auto windowDetails = game.getCurrentScene().getSingleton<pg::game::WindowDetails>();
auto& registry = _game.getGlobalRegistry();
auto background = registry.create();

auto backgroundImg = pg::SpriteFactory::makeSprite(_game.getApp().getRenderer(), "../data/spr_stars01.png");
auto windowDetails = _game.getCurrentScene().getSingleton<pg::game::WindowDetails>();
// TODO add entities as references to the classes
auto backgroundRect = pg::iVec2{windowDetails.windowRect.w, windowDetails.windowRect.h};

Expand Down
2 changes: 1 addition & 1 deletion apps/asteroids/systems/Background.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Background : public pg::game::SystemInterface

using backgroundTag = entt::tag<"BACKGROUND"_hs>;

void setup();
void setup(std::string_view scene_id);

void handle(const pg::game::FrameStamp& frameStamp);
};
Expand Down
9 changes: 5 additions & 4 deletions apps/asteroids/systems/Collisions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
#include <events/Collision.h>
#include <iostream>

void asteroids::Collisions::setup() {}
void asteroids::Collisions::setup(std::string_view /*scene_id*/) {}

void asteroids::Collisions::handle(const pg::game::FrameStamp&)
{
auto active_view = game.getRegistry().view<pg::Transform2D, pg::BoundingSphere, asteroids::ActiveCollider>();
auto passive_view = game.getRegistry().view<pg::Transform2D, pg::BoundingSphere, asteroids::PassiveCollider>();
auto active_view = _game.getGlobalRegistry().view<pg::Transform2D, pg::BoundingSphere, asteroids::ActiveCollider>();
auto passive_view =
_game.getGlobalRegistry().view<pg::Transform2D, pg::BoundingSphere, asteroids::PassiveCollider>();

// actives vs passives
for (auto& entity_a : active_view)
Expand All @@ -29,5 +30,5 @@ void asteroids::Collisions::handle(const pg::game::FrameStamp&)

void asteroids::Collisions::handleCollision(entt::entity id1, entt::entity id2, [[maybe_unused]] float intrusion)
{
game.getDispatcher().trigger<events::Collision>({id1, id2});
_game.getDispatcher().trigger<events::Collision>({id1, id2});
}
2 changes: 1 addition & 1 deletion apps/asteroids/systems/Collisions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Collisions : public pg::game::SystemInterface
public:
using SystemInterface::SystemInterface;

void setup();
void setup(std::string_view scene_id);

void handle(const pg::game::FrameStamp& frameStamp);

Expand Down
5 changes: 3 additions & 2 deletions apps/asteroids/systems/DynamicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
#include "components/Entities.h"
#include <pgGame/core/Game.hpp>

void asteroids::DynamicsSystem::setup() {}
void asteroids::DynamicsSystem::setup(std::string_view /*scene_id*/) {}

void asteroids::DynamicsSystem::handle(const pg::game::FrameStamp& frameStamp)
{
auto view = game.getRegistry().view<pg::Transform2D, asteroids::Dynamics>();
auto view = _game.getGlobalRegistry().view<pg::Transform2D, asteroids::Dynamics>();
for (auto& entity : view)
{
auto&& [transform, dynamics] = view.get<pg::Transform2D, asteroids::Dynamics>(entity);
transform.pos += dynamics.velocity * frameStamp.getFrameDuration_sec();
transform.rotation_deg += dynamics.angularVelocity * frameStamp.getFrameDuration_sec();
dynamics.velocity = dynamics.velocity * dynamics.dampening;
}
}
2 changes: 1 addition & 1 deletion apps/asteroids/systems/DynamicsSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DynamicsSystem : public pg::game::SystemInterface
{
public:
using SystemInterface::SystemInterface;
void setup() override;
void setup(std::string_view scene_id) override;
void handle(const pg::game::FrameStamp& frameStamp) override;
};
} // namespace asteroids
Loading

0 comments on commit a27e39c

Please sign in to comment.