From 8739da84630ebfd2b4750a5a73c54f20218fd5f5 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sat, 9 Mar 2024 08:59:39 -0500 Subject: [PATCH] Easy cleanup: delete files from the old Entity & UI system that were replaced in EngJam 2023 & 7DRL 2024 --- src/Animation.cpp | 124 --------------- src/Animation.h | 50 ------- src/Button.cpp | 24 --- src/Button.h | 35 ----- src/Components.h | 138 ----------------- src/Entity.cpp | 25 ---- src/Entity.h | 35 ----- src/EntityManager.cpp | 73 --------- src/EntityManager.h | 25 ---- src/GameEngine.cpp | 15 -- src/GameEngine.h | 2 - src/Grid.cpp | 340 ------------------------------------------ src/Grid.h | 56 ------- src/IndexSprite.cpp | 24 --- src/IndexSprite.h | 13 -- src/McRFPy_API.cpp | 1 - src/McRFPy_API.h | 19 --- src/MenuScene.cpp | 53 ------- src/MenuScene.h | 18 --- src/UIMenu.cpp | 83 ----------- src/UIMenu.h | 38 ----- 21 files changed, 1191 deletions(-) delete mode 100644 src/Animation.cpp delete mode 100644 src/Animation.h delete mode 100644 src/Button.cpp delete mode 100644 src/Button.h delete mode 100644 src/Components.h delete mode 100644 src/Entity.cpp delete mode 100644 src/Entity.h delete mode 100644 src/EntityManager.cpp delete mode 100644 src/EntityManager.h delete mode 100644 src/Grid.cpp delete mode 100644 src/Grid.h delete mode 100644 src/IndexSprite.cpp delete mode 100644 src/IndexSprite.h delete mode 100644 src/MenuScene.cpp delete mode 100644 src/MenuScene.h delete mode 100644 src/UIMenu.cpp delete mode 100644 src/UIMenu.h diff --git a/src/Animation.cpp b/src/Animation.cpp deleted file mode 100644 index 0d024d7..0000000 --- a/src/Animation.cpp +++ /dev/null @@ -1,124 +0,0 @@ -#include "Animation.h" - -Animation::Animation(float _d, std::function _cb, bool _l) -:duration(_d), callback(_cb), loop(_l), elapsed(0.0f) {} - -// linear interpolation constructor -template -LerpAnimation::LerpAnimation(float _d, T _ev, T _sv, std::function _cb, std::function _w, bool _l) -:Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f), -startvalue(_sv), endvalue(_ev), write(_w) {} - -// discrete values constructor -template -DiscreteAnimation::DiscreteAnimation(float _d, std::vector _v, std::function _cb, std::function _w, bool _l) -:Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f), -index(0), nonelapsed(0.0f), values(_v), write(_w) { - timestep = _d / _v.size(); -} - -/* // don't call virtual functions (like cancel()) from base class destructor - * // child classes destructors' are called first anyway -Animation::~Animation() { - // deconstructor sets target to desired end state (no partial values) - cancel(); -} -*/ - -template<> -void LerpAnimation::lerp() { - //*(std::string*)target = ; - write(endvalue.substr(0, endvalue.length() * (elapsed / duration))); -} - -template<> -void LerpAnimation::lerp() { - int delta = endvalue - startvalue; - //*(int*)target = ; - write(startvalue + (elapsed / duration * delta)); -} - -template<> -void LerpAnimation::lerp() { - int delta = endvalue - startvalue; - //*(float*)target = ; - write(startvalue + (elapsed / duration * delta)); -} - -template<> -void LerpAnimation::lerp() { - //std::cout << "sf::Vector2f implementation of lerp." << std::endl; - int delta_x = endvalue.x - startvalue.x; - int delta_y = endvalue.y - startvalue.y; - //std::cout << "Start: " << startvalue.x << ", " << startvalue.y << "; End: " << endvalue.x << ", " << endvalue.y << std::endl; - //std::cout << "Delta: " << delta_x << ", " << delta_y << std::endl; - //((sf::Vector2f*)target)->x = startvalue.x + (elapsed / duration * delta_x); - //((sf::Vector2f*)target)->y = startvalue.y + (elapsed / duration * delta_y); - write(sf::Vector2f(startvalue.x + (elapsed / duration * delta_x), - startvalue.y + (elapsed / duration * delta_y))); -} - -template<> -void LerpAnimation::lerp() { - int delta_x = endvalue.x - startvalue.y; - int delta_y = endvalue.y - startvalue.y; - //((sf::Vector2i*)target)->x = startvalue.x + (elapsed / duration * delta_x); - //((sf::Vector2i*)target)->y = startvalue.y + (elapsed / duration * delta_y); - write(sf::Vector2i(startvalue.x + (elapsed / duration * delta_x), - startvalue.y + (elapsed / duration * delta_y))); -} - -template -void LerpAnimation::step(float delta) { - if (complete) return; - elapsed += delta; - //std::cout << "LerpAnimation step function. Elapsed: " << elapsed < -void DiscreteAnimation::step(float delta) -{ - if (complete) return; - nonelapsed += delta; - //std::cout << "Nonelapsed: " << nonelapsed << " elapsed (pre-add): " << elapsed << " timestep: " << timestep << " duration: " << duration << " index: " << index << std::endl; - if (nonelapsed < timestep) return; - //std::cout << "values size: " << values.size() << " isDone(): " << isDone() << std::endl; - if (elapsed > duration && !complete) {callback(); complete = true; return; } - elapsed += nonelapsed; // or should it be += timestep? - if (index == values.size() - 1) return; - nonelapsed = 0; // or should it -= timestep? - index++; - //*(T*)target = values[index]; - write(values[index]); -} - -template -void LerpAnimation::cancel() { - //*(T*)target = endvalue; - write(endvalue); -} - -template -void DiscreteAnimation::cancel() { - //*(T*)target = values[values.size() - 1]; - write(values[values.size() - 1]); -} - -bool Animation::isDone() { - return elapsed + Animation::EPSILON >= duration; -} - -namespace animation_template_implementations { - // instantiate to compile concrete templates - //LerpAnimation implement_vector2f; - - auto implement_v2f_const = LerpAnimation>(4.0, sf::Vector2(), sf::Vector2f(1,1), [](){}, [](sf::Vector2f v){}, false); - auto implement_disc_i = DiscreteAnimation(3.0, std::vector{0},[](){},[](int){},false); - //LerpAnimation implement_vector2i; - //LerpAnimation implment_int; - //LerpAnimation implment_string; - //LerpAnimation implement_float; - //DiscreteAnimation implement_int_discrete; -} diff --git a/src/Animation.h b/src/Animation.h deleted file mode 100644 index 36c9f6f..0000000 --- a/src/Animation.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include "Common.h" -#include - -class Animation -{ -protected: - static constexpr float EPSILON = 0.05; - float duration, elapsed; - std::function callback; - bool loop; - bool complete=false; -public: - //Animation(float, T, T*, std::function, bool); // lerp - //Animation(float, std::vector, T*, std::function, bool); // discrete - Animation(float, std::function, bool); - //Animation() {}; - virtual void step(float) = 0; - virtual void cancel() = 0; - bool isDone(); -}; - -template -class LerpAnimation: public Animation -{ - T startvalue, endvalue; - std::function write; - void lerp(); -public: - ~LerpAnimation() { cancel(); } - LerpAnimation(float, T, T, std::function, std::function, bool); - //LerpAnimation() {}; - void step(float) override final; - void cancel() override final; -}; - -template -class DiscreteAnimation: public Animation -{ - std::vector values; - std::function write; - float nonelapsed, timestep; - int index; -public: - DiscreteAnimation(float, std::vector, std::function, std::function, bool); - DiscreteAnimation() {}; - ~DiscreteAnimation() { cancel(); } - void step(float) override final; - void cancel() override final; -}; diff --git a/src/Button.cpp b/src/Button.cpp deleted file mode 100644 index bb40fe5..0000000 --- a/src/Button.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "Button.h" - -void Button::render(sf::RenderWindow & window) -{ - window.draw(rect); - window.draw(caption); -} - -Button::Button(int x, int y, int w, int h, - sf::Color _background, sf::Color _textcolor, - const char * _caption, sf::Font & font, - const char * _action) -{ - rect.setPosition(sf::Vector2f(x, y)); - rect.setSize(sf::Vector2f(w, h)); - rect.setFillColor(_background); - - caption.setFillColor(_textcolor); - caption.setPosition(sf::Vector2f(x, y)); - caption.setString(_caption); - caption.setFont(font); - - action = _action; -} diff --git a/src/Button.h b/src/Button.h deleted file mode 100644 index 189e0d3..0000000 --- a/src/Button.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "Common.h" - -class Button -{ - -protected: - -public: - // TODO / JankMode: setter & getter for these three fields - // were protected, but directly changing them should be...fine? - sf::RectangleShape rect; - sf::Text caption; - std::string action; - - Button() {}; - Button(int x, int y, int w, int h, - sf::Color _background, sf::Color _textcolor, - const char * _caption, sf::Font & font, - const char * _action); - void setPosition(sf::Vector2f v) { rect.setPosition(v); caption.setPosition(v); } - void setSize(sf::Vector2f & v) { rect.setSize(v); } - void setBackground(sf::Color c) { rect.setFillColor(c); } - void setCaption(std::string & s) { caption.setString(s); } - void setTextColor(sf::Color c) { caption.setFillColor(c); } - void render(sf::RenderWindow & window); - auto contains(sf::Vector2i p) { return rect.getGlobalBounds().contains(p.x, p.y); } - auto contains(sf::Vector2f rel, sf::Vector2i p) { - return rect.getGlobalBounds().contains(p.x - rel.x, p.y - rel.y); - } - auto getAction() { return action; } - -private: -}; diff --git a/src/Components.h b/src/Components.h deleted file mode 100644 index 1ff7409..0000000 --- a/src/Components.h +++ /dev/null @@ -1,138 +0,0 @@ -#pragma once - -#include "Common.h" -#include "IndexSprite.h" -#include "Grid.h" -//#include "Item.h" -#include "Python.h" -#include - -class CGrid -{ -public: - bool visible; - int x, y; - IndexSprite indexsprite; - Grid* grid; - CGrid(Grid* _g, int _ti, int _si, int _x, int _y, bool _v) - : visible(_v), x(_x), y(_y), grid(_g), indexsprite(_ti, _si, _x, _y, 1.0) {} -}; - -class CInventory -{ -public: - //std::list>; - int x; -}; - -class CBehavior -{ -public: - PyObject* object; - CBehavior(PyObject* p): object(p) {} -}; - -/* -class CCombatant -{ -public: - int hp; - int maxhp; -} - -class CCaster -{ -public: - int mp; - int maxmp; -} - -class CLevel -{ - int constitution; // +HP, resist effects - int strength; // +damage, block/parry - int dexterity; // +speed, dodge - int intelligence; // +MP, spell resist - int wisdom; // +damage, deflect - int luck; // crit, loot -} -*/ - - -/* -class CTransform -{ -public: - Vec2 pos = { 0.0, 0.0 }; - Vec2 velocity = { 0.0, 0.0 }; - float angle = 0; - - CTransform(const Vec2 & p, const Vec2 & v, float a) - : pos(p), velocity(v), angle(a) {} -}; -*/ - -/* -class CShape -{ -public: - sf::CircleShape circle; - - CShape(float radius, int points, const sf::Color & fill, const sf::Color & outline, float thickness) - : circle(radius, points) - { - circle.setFillColor(fill); - circle.setOutlineColor(outline); - circle.setOutlineThickness(thickness); - circle.setOrigin(radius, radius); - } -}; - -class CCollision -{ -public: - float radius = 0; - CCollision(float r) - : radius(r) {} -}; - -class CScore -{ -public: - int score = 0; - CScore(int s) - : score(s) {} -}; - -class CLifespan -{ -public: - int remaining = 0; - int total = 0; - CLifespan(int t) - : remaining(t), total(t) {} -}; - -class CInput -{ -public: - bool up = false; - bool left = false; - bool right = false; - bool down = false; - bool fire = false; - - CInput() {} -}; - -class CSteer -{ -public: - sf::Vector2f position; - sf::Vector2f velocity; - float v_max; - float dv_max; - float theta_max; - float dtheta_max; -}; -*/ diff --git a/src/Entity.cpp b/src/Entity.cpp deleted file mode 100644 index 38ac1b7..0000000 --- a/src/Entity.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "Entity.h" - -Entity::Entity(const size_t i, const std::string & t) - : m_id(i), m_tag(t) {} - -bool Entity::isActive() const -{ - return m_active; -} - -const std::string & Entity::tag() const -{ - return m_tag; -} - -const size_t Entity::id() const -{ - return m_id; -} - -void Entity::destroy() -{ - m_active = false; -} - diff --git a/src/Entity.h b/src/Entity.h deleted file mode 100644 index 9043d44..0000000 --- a/src/Entity.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include "Common.h" - -#include "Components.h" - -class Entity -{ - friend class EntityManager; - - bool m_active = true; - size_t m_id = 0; - std::string m_tag = "default"; - - //constructor and destructor - Entity(const size_t id, const std::string & t); - -public: - // component pointers - //std::shared_ptr cTransform; - //std::shared_ptr cShape; - //std::shared_ptr cCollision; - //std::shared_ptr cInput; - //std::shared_ptr cScore; - //std::shared_ptr cLifespan; - std::shared_ptr cGrid; - std::shared_ptr cInventory; - std::shared_ptr cBehavior; - - //private member access functions - bool isActive() const; - const std::string & tag() const; - const size_t id() const; - void destroy(); -}; diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp deleted file mode 100644 index e364cb6..0000000 --- a/src/EntityManager.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "EntityManager.h" - -EntityManager::EntityManager() - :m_totalEntities(0) {} - -void EntityManager::update() -{ - //TODO: add entities from m_entitiesToAdd to all vector / tag map - removeDeadEntities(m_entities); - - // C++17 way of iterating! - for (auto& [tag, entityVec] : m_entityMap) - { - removeDeadEntities(entityVec); - } - - for (auto& e : m_entitiesToAdd) - { - m_entities.push_back(e); - m_entityMap[e->tag()].push_back(e); - } - //if (m_entitiesToAdd.size()) - // m_entitiesToAdd.erase(m_entitiesToAdd.begin(), m_entitiesToAdd.end()); - m_entitiesToAdd = EntityVec(); - -} - -void EntityManager::removeDeadEntities(EntityVec & vec) -{ - EntityVec survivors; // New vector - for (auto& e : m_entities){ - if (e->isActive()) survivors.push_back(e); // populate new vector - else if (e->cGrid) { // erase vector from grid - for( auto it = e->cGrid->grid->entities.begin(); it != e->cGrid->grid->entities.end(); it++){ - if( *it == e ){ - e->cGrid->grid->entities.erase( it ); - break; - } - } - } - } - //std::cout << "All entities: " << m_entities.size() << " Survivors: " << survivors.size() << std::endl; - m_entities = survivors; // point to new vector - for (auto& [tag, entityVec] : m_entityMap) - { - EntityVec tag_survivors; // New vector - for (auto& e : m_entityMap[tag]) - { - if (e->isActive()) tag_survivors.push_back(e); // populate new vector - } - m_entityMap[tag] = tag_survivors; // point to new vector - //std::cout << tag << " entities: " << m_entityMap[tag].size() << " Survivors: " << tag_survivors.size() << std::endl; - } -} - -std::shared_ptr EntityManager::addEntity(const std::string & tag) -{ - // create the entity shared pointer - auto entity = std::shared_ptr(new Entity(m_totalEntities++, tag)); - m_entitiesToAdd.push_back(entity); - return entity; -} - -const EntityVec & EntityManager::getEntities() -{ - return m_entities; -} - -const EntityVec & EntityManager::getEntities(const std::string & tag) -{ - return m_entityMap[tag]; -} - diff --git a/src/EntityManager.h b/src/EntityManager.h deleted file mode 100644 index 744089e..0000000 --- a/src/EntityManager.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "Common.h" -#include "Entity.h" - -typedef std::vector> EntityVec; -typedef std::map EntityMap; - -class EntityManager -{ - EntityVec m_entities; - EntityVec m_entitiesToAdd; - EntityMap m_entityMap; - size_t m_totalEntities; - - void removeDeadEntities(EntityVec & vec); - -public: - EntityManager(); - void update(); - std::shared_ptr addEntity(const std::string & tag); - const EntityVec & getEntities(); - const EntityVec & getEntities(const std::string & tag); -}; - diff --git a/src/GameEngine.cpp b/src/GameEngine.cpp index 65878a6..554eabf 100644 --- a/src/GameEngine.cpp +++ b/src/GameEngine.cpp @@ -1,9 +1,6 @@ #include "GameEngine.h" -//#include "MenuScene.h" -//#include "UITestScene.h" #include "ActionCode.h" #include "McRFPy_API.h" -//#include "PythonScene.h" #include "PyScene.h" #include "UITestScene.h" #include "Resources.h" @@ -17,24 +14,12 @@ GameEngine::GameEngine() visible = window.getDefaultView(); window.setFramerateLimit(30); scene = "uitest"; - //std::cout << "Constructing MenuScene" << std::endl; - //scenes["menu"] = new MenuScene(this); scenes["uitest"] = new UITestScene(this); - - //std::cout << "Constructed MenuScene" < -#include "Entity.h" - -GridPoint::GridPoint(): - color(0, 0, 0, 0), walkable(false), tilesprite(-1), transparent(false), visible(false), discovered(false), color_overlay(0,0,0,255), tile_overlay(-1), uisprite(-1) -{}; - -void Grid::setSprite(int ti) -{ - int tx = ti % texture_width, ty = ti / texture_width; - sprite.setTextureRect(sf::IntRect(tx * grid_size, ty * grid_size, grid_size, grid_size)); -} - -Grid::Grid(int gx, int gy, int gs, int _x, int _y, int _w, int _h): - grid_size(gs), - grid_x(gx), grid_y(gy), - zoom(1.0f), center_x((gx/2) * gs), center_y((gy/2) * gs), - texture_width(12), texture_height(11), visible(false) -{ - //grid_size = gs; - //zoom = 1.0f; - //grid_x = gx; - //grid_y = gy; - tcodmap = new TCODMap(gx, gy); - points.resize(gx*gy); - box.setSize(sf::Vector2f(_w, _h)); - box.setPosition(sf::Vector2f(_x, _y)); - box.setFillColor(sf::Color(0,0,0,0)); - - renderTexture.create(_w, _h); - - texture.loadFromFile("./assets/kenney_tinydungeon.png"); - texture.setSmooth(false); - sprite.setTexture(texture); - - //output.setSize(box.getSize()); - output.setTextureRect( - sf::IntRect(0, 0, - box.getSize().x, box.getSize().y)); - output.setPosition(box.getPosition()); - // textures are upside-down inside renderTexture - output.setTexture(renderTexture.getTexture()); - - // Show one texture at a time - sprite.setTexture(texture); -} - -void Grid::refreshTCODmap() { - //int total = 0, walkable = 0, transparent = 0; - for (int x = 0; x < grid_x; x++) { - for (int y = 0; y < grid_y; y++) { - auto p = at(x, y); - //total++; if (p.walkable) walkable++; if (p.transparent) transparent++; - tcodmap->setProperties(x, y, p.transparent, p.walkable); - } - } - //std::cout << "Map refreshed: " << total << " squares, " << walkable << "walkable, " << transparent << " transparent" << std::endl; -} -void Grid::refreshTCODsight(int x, int y) { - tcodmap->computeFov(x,y, 0, true, FOV_PERMISSIVE_8); - for (int x = 0; x < grid_x; x++) { - for (int y = 0; y < grid_y; y++) { - auto& p = at(x, y); - if (p.visible && !tcodmap->isInFov(x, y)) { - p.discovered = true; - p.visible = false; - } else if (!p.visible && tcodmap->isInFov(x,y)) { - p.discovered = true; - p.visible = true; - } - } - } -} - -bool Grid::inBounds(int x, int y) { - return (x >= 0 && y >= 0 && x < grid_x && y < grid_y); -} - -void Grid::screenToGrid(int sx, int sy, int& gx, int& gy) { - - float center_x_sq = center_x / grid_size; - float center_y_sq = center_y / grid_size; - - float width_sq = box.getSize().x / (grid_size * zoom); - float height_sq = box.getSize().y / (grid_size * zoom); - float left_edge = center_x_sq - (width_sq / 2.0); - float right_edge = center_x_sq + (width_sq / 2.0); - float top_edge = center_y_sq - (height_sq / 2.0); - float bottom_edge = center_y_sq + (height_sq / 2.0); - - float grid_px = zoom * grid_size; - //std::cout << "##############################" << - // "\nscreen coord: (" << sx << ", " << sy << ")" << std::endl; - - sx -= box.getPosition().x; - sy -= box.getPosition().y; - - //std::cout << "box coord: (" << sx << ", " << sy << ")" << std::endl; - float mouse_x_sq = sx / grid_px; - float mouse_y_sq = sy / grid_px; - - float ans_x = mouse_x_sq + left_edge; - float ans_y = mouse_y_sq + top_edge; - - // compare integer method with this (mostly working) one - //int diff_realpixel_x = box.getSize().x / 2.0 - sx; - //int diff_realpixel_y = box.getSize().y / 2.0 - sy; - int left_spritepixels = center_x - (box.getSize().x / 2.0 / zoom); - int top_spritepixels = center_y - (box.getSize().y / 2.0 / zoom); - - std::cout << "Float method got ans (" << ans_x << ", " << ans_y << ")" - << std::endl << "Int method px (" << left_spritepixels + (sx/zoom) << ", " << - top_spritepixels + (sy/zoom) << ")" << std::endl << - "Int grid (" << (left_spritepixels + (sx/zoom) ) / grid_size << - ", " << (top_spritepixels + (sy/zoom)) / grid_size << ")" << - - std::endl; - - // casting float turns -0.5 to 0; I want any negative coord to be OOB - if (ans_x < 0) ans_x = -1; - if (ans_y < 0) ans_y = -1; - - gx = ans_x; - gy = ans_y; - /* - std::cout << - "C: (" << center_x << ", " << center_y << ")" << std::endl << - "W: " << width_sq << " H: " << height_sq << std::endl << - "L: " << left_edge << " T: " << top_edge << std::endl << - "R: " << right_edge << " B: " << bottom_edge << std::endl << - "Grid Px: " << grid_px << "( zoom: " << zoom << ")" << std::endl << - "answer: G(" << ans_x << ", " << ans_y << ")" << std::endl << - "##############################" << - std::endl; - */ -} - -void Grid::renderPxToGrid(int sx, int sy, int& gx, int& gy) { - // just like screen px coversion, but no offset by grid's position - float center_x_sq = center_x / grid_size; - float center_y_sq = center_y / grid_size; - - float width_sq = box.getSize().x / (grid_size * zoom); - float height_sq = box.getSize().y / (grid_size * zoom); - - int width_px = box.getSize().x / 2.0; - int height_px = box.getSize().y / 2.0; - - float left_edge = center_x_sq - (width_sq / 2.0); - float top_edge = center_y_sq - (height_sq / 2.0); - - float grid_px = zoom * grid_size; - float sx_sq = sx / grid_px; - float sy_sq = sy / grid_px; - - float ans_x = sx_sq + left_edge; - float ans_y = sy_sq + top_edge; - - if (ans_x < 0) ans_x = -1; - if (ans_y < 0) ans_y = -1; - - gx = ans_x; - gy = ans_y; -} - -void Grid::integerGrid(float fx, float fy, int& gx, int& gy) { - if (fx < 0) fx -= 0.5; - if (fy < 0) fy -= 0.5; - gx = fx; - gy = fy; -} - -void Grid::gridToRenderPx(int gx, int gy, int& sx, int& sy) { - // integer grid square (gx, gy) - what pixel (on rendertexture) - // should it's top left corner be at (the sprite's position) - - // eff_gridsize = grid_size * zoom - // if center_x = 161, and grid_size is 16, that's 10 + 1/16 sprites - // center_x - (box.getSize().x / 2 / zoom) = left edge (in px) - // gx * eff_gridsize = pixel position without panning - // pixel_gx - left_edge = grid's render position - - sx = (gx * grid_size * zoom) - (center_x - (box.getSize().x / 2.0 / zoom)); - sy = (gy * grid_size * zoom) - (center_y - (box.getSize().y / 2.0 / zoom)); -} - -void Grid::render(sf::RenderWindow & window) -{ - renderTexture.clear(); - //renderTexture.draw(box); - - // sprites that are visible according to zoom, center_x, center_y, and box width - float center_x_sq = center_x / grid_size; - float center_y_sq = center_y / grid_size; - - float width_sq = box.getSize().x / (grid_size * zoom); - float height_sq = box.getSize().y / (grid_size * zoom); - float left_edge = center_x_sq - (width_sq / 2.0); - //float right_edge = center_x_sq + (width_sq / 2.0); - float top_edge = center_y_sq - (height_sq / 2.0); - //float bottom_edge = center_y_sq + (height_sq / 2.0); - - - int left_spritepixels = center_x - (box.getSize().x / 2.0 / zoom); - int top_spritepixels = center_y - (box.getSize().y / 2.0 / zoom); - - sprite.setScale(sf::Vector2f(zoom, zoom)); - sf::RectangleShape r; // for colors and overlays - r.setSize(sf::Vector2f(grid_size * zoom, grid_size * zoom)); - r.setOutlineThickness(0); - - int x_limit = left_edge + width_sq + 2; - if (x_limit > grid_x) x_limit = grid_x; - - int y_limit = top_edge + height_sq + 2; - if (y_limit > grid_y) y_limit = grid_y; - - //for (float x = (left_edge >= 0 ? left_edge : 0); - for (int x = (left_edge - 1 >= 0 ? left_edge - 1 : 0); - x < x_limit; //x < view_width; - x+=1) - { - //for (float y = (top_edge >= 0 ? top_edge : 0); - for (int y = (top_edge - 1 >= 0 ? top_edge - 1 : 0); - y < y_limit; //y < view_height; - y+=1) - { - // Converting everything to integer pixels to avoid jitter - //auto pixel_pos = sf::Vector2f( - // (x - left_edge) * (zoom * grid_size), - // (y - top_edge) * (zoom * grid_size)); - - // This failed horribly: - //int gx, gy; integerGrid(x, y, gx, gy); - //int px_x, px_y; gridToRenderPx(gx, gy, px_x, px_y); - //auto pixel_pos = sf::Vector2f(px_x, px_y); - - // this draws coherently, but the coordinates - // don't match up with the mouse cursor function - // jitter not eliminated - auto pixel_pos = sf::Vector2f( - (x*grid_size - left_spritepixels) * zoom, - (y*grid_size - top_spritepixels) * zoom ); - - auto gridpoint = at(std::floor(x), std::floor(y)); - - sprite.setPosition(pixel_pos); - - r.setPosition(pixel_pos); - r.setFillColor(gridpoint.color); - renderTexture.draw(r); - - // tilesprite - // if discovered but not visible, set opacity to 90% - // if not discovered... just don't draw it? - if (gridpoint.tilesprite != -1) { - setSprite(gridpoint.tilesprite); - renderTexture.draw(sprite); - } - - - - } - } - - for (auto e : entities) { - auto drawent = e->cGrid->indexsprite.drawable(); - drawent.setScale(zoom, zoom); - auto pixel_pos = sf::Vector2f( - (drawent.getPosition().x*grid_size - left_spritepixels) * zoom, - (drawent.getPosition().y*grid_size - top_spritepixels) * zoom ); - drawent.setPosition(pixel_pos); - renderTexture.draw(drawent); - } - - // loop again and draw on top of entities - for (int x = (left_edge - 1 >= 0 ? left_edge - 1 : 0); - x < x_limit; //x < view_width; - x+=1) - { - //for (float y = (top_edge >= 0 ? top_edge : 0); - for (int y = (top_edge - 1 >= 0 ? top_edge - 1 : 0); - y < y_limit; //y < view_height; - y+=1) - { - - auto pixel_pos = sf::Vector2f( - (x*grid_size - left_spritepixels) * zoom, - (y*grid_size - top_spritepixels) * zoom ); - - auto gridpoint = at(std::floor(x), std::floor(y)); - - sprite.setPosition(pixel_pos); - - r.setPosition(pixel_pos); - - // visible & discovered layers for testing purposes - if (!gridpoint.discovered) { - r.setFillColor(sf::Color(16, 16, 20, 192)); // 255 opacity for actual blackout - renderTexture.draw(r); - } else if (!gridpoint.visible) { - r.setFillColor(sf::Color(32, 32, 40, 128)); - renderTexture.draw(r); - } - - // overlay - - // uisprite - } - } - // grid lines for testing & validation - /* - sf::Vertex line[] = - { - sf::Vertex(sf::Vector2f(0, 0), sf::Color::Red), - sf::Vertex(box.getSize(), sf::Color::Red), - - }; - - renderTexture.draw(line, 2, sf::Lines); - sf::Vertex lineb[] = - { - sf::Vertex(sf::Vector2f(0, box.getSize().y), sf::Color::Blue), - sf::Vertex(sf::Vector2f(box.getSize().x, 0), sf::Color::Blue), - - }; - - renderTexture.draw(lineb, 2, sf::Lines); - */ - - // render to window - renderTexture.display(); - window.draw(output); -} - -GridPoint& Grid::at(int x, int y) -{ - return points[y * grid_x + x]; -} diff --git a/src/Grid.h b/src/Grid.h deleted file mode 100644 index 95755de..0000000 --- a/src/Grid.h +++ /dev/null @@ -1,56 +0,0 @@ -#pragma once -#include "Common.h" -#include "libtcod.h" - -//#include "Entity.h" -class Entity; // forward declare - -class GridPoint -{ -public: - // Layers: color, walkable, tilesprite, transparent, visible, discovered, overlay, uisprite - sf::Color color; - bool walkable; - int tilesprite; - bool transparent, visible, discovered; - sf::Color color_overlay; - int tile_overlay, uisprite; - GridPoint(); -}; - -class Grid -{ -private: -public: - Grid(); - sf::RectangleShape box; // view on window - bool visible; - sf::Texture texture; - sf::Sprite sprite, output; - sf::RenderTexture renderTexture; - TCODMap* tcodmap; - void setSprite(int); - const int texture_width, texture_height; - auto contains(sf::Vector2i p) { return box.getGlobalBounds().contains(p.x, p.y); } - - Grid(int gx, int gy, int gs, int _x, int _y, int _w, int _h); - int grid_x, grid_y; // rectangle map size (integer - sprites) - int grid_size; // pixel size of 1 sprite - float zoom; - int center_x, center_y; // center in 1.0x Pixels - - std::vector points; // grid visible contents - std::vector> entities; - void render(sf::RenderWindow&); // draw to screen - GridPoint& at(int, int); - bool inBounds(int, int); - void screenToGrid(int, int, int&, int&); - - void renderPxToGrid(int, int, int&, int&); - void gridToRenderPx(int, int, int&, int&); - void integerGrid(float, float, int&, int&); - - void refreshTCODmap(); - void refreshTCODsight(int, int); - TCODDijkstra *dijkstra; //= new TCODDijkstra(myMap); -}; diff --git a/src/IndexSprite.cpp b/src/IndexSprite.cpp deleted file mode 100644 index 526c1e2..0000000 --- a/src/IndexSprite.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "IndexSprite.h" -#include "GameEngine.h" - -//int texture_index, sprite_index, x, y; - -GameEngine* IndexSprite::game; - -sf::Sprite IndexSprite::drawable() -{ - sf::Sprite s; - auto& tex = IndexSprite::game->textures[texture_index]; - s.setTexture(tex.texture); - s.setScale(sf::Vector2f(scale, scale)); - s.setPosition(sf::Vector2f(x, y)); - //std::cout << "Drawable position: " << x << ", " << y << " -> " << s.getPosition().x << ", " << s.getPosition().y << std::endl; - s.setTextureRect(tex.spriteCoordinates(sprite_index)); - return s; -} - -IndexSprite::IndexSprite(int _ti, int _si, float _x, float _y, float _s): - texture_index(_ti), sprite_index(_si), x(_x), y(_y), scale(_s) { - //std::cout << "IndexSprite constructed with x, y " << _x << ", " << _y << std::endl; - //std::cout << " * Stored x, y " << x << ", " << y << std::endl; -} diff --git a/src/IndexSprite.h b/src/IndexSprite.h deleted file mode 100644 index 76b053b..0000000 --- a/src/IndexSprite.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "Common.h" -class GameEngine; // forward declare - -class IndexSprite { -public: - int texture_index, sprite_index; - float x, y; - float scale; - static GameEngine* game; - sf::Sprite drawable(); - IndexSprite(int, int, float, float, float); -}; diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index d655686..5aa61e1 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -1,7 +1,6 @@ #include "McRFPy_API.h" #include "platform.h" #include "GameEngine.h" -#include "Grid.h" #include "UI.h" #include "Resources.h" diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index ca4a404..588f897 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -1,20 +1,8 @@ #pragma once #include "Common.h" -#include "Entity.h" -//#include "EntityManager.h" -//#include "Scene.h" -//#include "GameEngine.h" // can't - need forward declaration -//#include "ActionCode.h" #include "Python.h" -#include "UIMenu.h" -#include "Grid.h" -#include "IndexSprite.h" -#include "EntityManager.h" #include -// implementation required to link templates -#include "Animation.h" - class GameEngine; // forward declared (circular members) class McRFPy_API @@ -44,17 +32,10 @@ public: static void REPL_device(FILE * fp, const char *filename); static void REPL(); - // Jank mode engage: let the API hold data for Python to hack on - //static std::map menus; - //static EntityManager entities; // this is also kinda good, entities not on the current grid can still act (like monsters following you through doors??) - //static std::map grids; - //static std::list animations; static std::vector soundbuffers; static sf::Music music; static sf::Sound sfx; - static std::shared_ptr player; - static std::map callbacks; static PyObject* _registerPyAction(PyObject*, PyObject*); static PyObject* _registerInputAction(PyObject*, PyObject*); diff --git a/src/MenuScene.cpp b/src/MenuScene.cpp deleted file mode 100644 index eeebed1..0000000 --- a/src/MenuScene.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "MenuScene.h" -#include "ActionCode.h" - -MenuScene::MenuScene(GameEngine* g) : Scene(g) -{ - text.setFont(game->getFont()); - text.setString("McRogueFace Engine - r/RoguelikeDev Tutorial 2023"); - text.setCharacterSize(24); - //std::cout << "MenuScene Initialized. " << game << std::endl; - //std::cout << "Font: " << game->getFont().getInfo().family << std::endl; - - text2.setFont(game->getFont()); - text2.setString("Press 'Spacebar' to run demo"); - text2.setCharacterSize(16); - text2.setPosition(0.0f, 50.0f); - - text3.setFont(game->getFont()); - text3.setString("use 'W' 'A' 'S' 'D' to move (even when blank; it's a bug)"); - text3.setCharacterSize(16); - text3.setPosition(0.0f, 80.0f); - - registerAction(ActionCode::KEY + sf::Keyboard::Space, "start_game"); - registerAction(ActionCode::KEY + sf::Keyboard::Up, "up"); - registerAction(ActionCode::KEY + sf::Keyboard::Down, "down"); -} - -void MenuScene::update() -{ - //std::cout << "MenuScene update" << std::endl; -} - -void MenuScene::doAction(std::string name, std::string type) -{ - //std::cout << "MenuScene doAction: " << name << ", " << type << std::endl; - //if (name.compare("start_game") == 0 and type.compare("start") == 0) - if(ACTION("start_game", "start")) - game->changeScene("py"); - /* - else if(ACTIONONCE("up")) - game->getWindow().setSize(sf::Vector2u(1280, 800)); - else if(ACTIONONCE("down")) - game->getWindow().setSize(sf::Vector2u(1024, 768)); - */ -} - -void MenuScene::sRender() -{ - game->getWindow().clear(); - game->getWindow().draw(text); - game->getWindow().draw(text2); - game->getWindow().draw(text3); - game->getWindow().display(); -} diff --git a/src/MenuScene.h b/src/MenuScene.h deleted file mode 100644 index cc33279..0000000 --- a/src/MenuScene.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "Common.h" -#include "Scene.h" -#include "GameEngine.h" - -class MenuScene: public Scene -{ - sf::Text text; - sf::Text text2; - sf::Text text3; - -public: - MenuScene(GameEngine*); - void update() override final; - void doAction(std::string, std::string) override final; - void sRender() override final; -}; diff --git a/src/UIMenu.cpp b/src/UIMenu.cpp deleted file mode 100644 index 8e3d537..0000000 --- a/src/UIMenu.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "UIMenu.h" -#include "Common.h" -#include "Resources.h" - -UIMenu::UIMenu(sf::Font & _font) -: font(_font) -{ - //font = _font; - box.setSize(sf::Vector2f(300, 400)); - box.setPosition(sf::Vector2f(300, 250)); - box.setFillColor(sf::Color(0,0,255)); -} - -UIMenu::UIMenu() -: font(Resources::font) -{ - box.setSize(sf::Vector2f(300, 400)); - box.setPosition(sf::Vector2f(300, 250)); - box.setFillColor(sf::Color(0,0,255)); -} - -void UIMenu::render(sf::RenderWindow & window) -{ - window.draw(box); - for (auto& s: sprites) { - auto _s = s.drawable(); - //std::cout << "Sprite has values " << s.x << ", " << s.y << std::endl; - //std::cout << "Drawable generated @ " << _s.getPosition().x << ", " << _s.getPosition().y << std::endl; - _s.move(box.getPosition()); - //std::cout << "Moved by " << box.getPosition().x << ", " << box.getPosition().y << std::endl; - //std::cout << "Render UIMenu Sprite @ " << _s.getPosition().x << ", " << _s.getPosition().y << std::endl; - window.draw(_s); - } - for (auto& c : captions) { - //auto s = std::string(c.getString()); - //std::cout << s << std::flush << std::endl; - c.move(box.getPosition()); - window.draw(c); - c.move(-box.getPosition()); - } - for (auto& b : buttons) { - //b.render(window); - b.setPosition(box.getPosition() + b.rect.getPosition()); - //b.caption.setPosition(box.getPosition() + b.caption.getPosition()); - b.render(window); - b.setPosition(b.rect.getPosition() - box.getPosition()); - //b.caption.setPosition(b.caption.getPosition() - box.getPosition()); - } -} - -void UIMenu::refresh() -{ - -} - -void UIMenu::add_caption(const char* text, int tsize, sf::Color color) -{ - auto c = sf::Text(); - //auto bpos = box.getPosition(); - - c.setFillColor(color); - c.setPosition(10, next_text); - next_text += 50; - c.setCharacterSize(tsize); - c.setString(text); - c.setFont(font); - captions.push_back(c); - -} - -void UIMenu::add_button(Button b) -{ - b.setPosition(sf::Vector2f(box.getSize().x / 2.0f, next_button)); - next_button += 50; - buttons.push_back(b); -} - -void UIMenu::add_sprite(IndexSprite s) -{ - //std::cout << "Adding sprite to UIMenu x,y " << s.x << ", " << s.y << std::endl; - sprites.push_back(s); -} - diff --git a/src/UIMenu.h b/src/UIMenu.h deleted file mode 100644 index 9e445db..0000000 --- a/src/UIMenu.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include "Common.h" -#include "Button.h" -#include "IndexSprite.h" - -class UIMenu -{ -public: - //UIMenu() {}; - sf::Font & font; - UIMenu(sf::Font & _font); - UIMenu(); - std::vector captions; - std::vector