diff --git a/SFMLengine_alpha/modules/components/draw_component.h b/SFMLengine_alpha/modules/components/draw_component.h index 9b51b56..50efc01 100644 --- a/SFMLengine_alpha/modules/components/draw_component.h +++ b/SFMLengine_alpha/modules/components/draw_component.h @@ -2,13 +2,13 @@ #include #include"component.h" -class DrawComponent : public Component, public sf::Drawable +class DrawComponent : public Component, public sf::Drawable, public sf::Transformable { protected: sf::Drawable* object; public: - void draw(sf::RenderTarget& target, sf::RenderStates states) const; + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; - void set(sf::Drawable* obj); + void set(sf::Drawable& obj); }; \ No newline at end of file diff --git a/SFMLengine_alpha/modules/components/soul_component.h b/SFMLengine_alpha/modules/components/soul_component.h index c1ae971..d176ea6 100644 --- a/SFMLengine_alpha/modules/components/soul_component.h +++ b/SFMLengine_alpha/modules/components/soul_component.h @@ -8,8 +8,12 @@ class SoulComponent :public Component Object* obj; public: + template - void set(Game* game, Scence* scence, Entity* entity); + void set(Game* game, Scence* scence, Entity* entity) { + obj = new T(); + obj->set(game, scence, entity); + } void logic(); }; \ No newline at end of file diff --git a/SFMLengine_alpha/modules/main_srtuctures/Entity.h b/SFMLengine_alpha/modules/main_srtuctures/Entity.h index 8d15ee8..b36c267 100644 --- a/SFMLengine_alpha/modules/main_srtuctures/Entity.h +++ b/SFMLengine_alpha/modules/main_srtuctures/Entity.h @@ -12,14 +12,21 @@ class Entity:public sf::Drawable sf::Vector2f pos; template - void add_component(); + void add_component() { + components.push_back(new T()); + } template - T& get_component() const; + T& get_component() const { + for (auto& comp : components) + if (typeid(*comp) == typeid(T)) + return static_cast(*comp); + } //душа - объект получает все ссылки template - void set_soul(Game* game, Scence* scence); - + void set_soul(Game* game, Scence* scence) { + get_component().set(game, scence, this); + } void logic(); diff --git a/SFMLengine_alpha/modules/main_srtuctures/Game.h b/SFMLengine_alpha/modules/main_srtuctures/Game.h index 9071f33..2477e5a 100644 --- a/SFMLengine_alpha/modules/main_srtuctures/Game.h +++ b/SFMLengine_alpha/modules/main_srtuctures/Game.h @@ -14,6 +14,8 @@ class Game sf::Texture texture; sf::Event event; +private: + sf::CircleShape sh; public: Game(); diff --git a/SFMLengine_alpha/modules/main_srtuctures/Scence.h b/SFMLengine_alpha/modules/main_srtuctures/Scence.h index 3611681..59ae51a 100644 --- a/SFMLengine_alpha/modules/main_srtuctures/Scence.h +++ b/SFMLengine_alpha/modules/main_srtuctures/Scence.h @@ -18,9 +18,9 @@ class Scence : public sf::Drawable void logic(); //здесь передаются все указатели для задание внутренности объектов template - void set(Game* game,int numb=0); + void set(Game* game, int numb) { + entities[numb].set_soul(game, this); + } //временно -private: - sf::CircleShape sh; }; \ No newline at end of file diff --git a/SFMLengine_alpha/src/components/draw_component.cpp b/SFMLengine_alpha/src/components/draw_component.cpp index 6b3ddc1..0e8f69c 100644 --- a/SFMLengine_alpha/src/components/draw_component.cpp +++ b/SFMLengine_alpha/src/components/draw_component.cpp @@ -2,9 +2,7 @@ void DrawComponent::draw(sf::RenderTarget& target, sf::RenderStates states)const { target.draw(*object, states); - } - -void DrawComponent::set(sf::Drawable* obj) { - this->object = obj; +void DrawComponent::set(sf::Drawable& obj) { + this->object = &obj; } \ No newline at end of file diff --git a/SFMLengine_alpha/src/components/soul_component.cpp b/SFMLengine_alpha/src/components/soul_component.cpp index d26c46d..947d042 100644 --- a/SFMLengine_alpha/src/components/soul_component.cpp +++ b/SFMLengine_alpha/src/components/soul_component.cpp @@ -3,12 +3,6 @@ //#include"../../modules/main_srtuctures/Entity.h" //#include"../../modules/components/soul_component.h" -template -void SoulComponent::set(Game* game, Scence* scence, Entity* entity) { - obj = new T(); - obj->set(game, scence, entity); -} - void SoulComponent::logic() { obj->logic(); } \ No newline at end of file diff --git a/SFMLengine_alpha/src/main_structures/Entity.cpp b/SFMLengine_alpha/src/main_structures/Entity.cpp index 3dfeddf..dcb608d 100644 --- a/SFMLengine_alpha/src/main_structures/Entity.cpp +++ b/SFMLengine_alpha/src/main_structures/Entity.cpp @@ -1,23 +1,5 @@ #include"../../modules/main_srtuctures/Game.h" - -template -void Entity::add_component(){ - components.push_back(new T()); -} - -template -T& Entity::get_component() const { - for (auto& comp : components) - if (typeid(*comp) == typeid(T)) - return static_cast(*comp); -} - -template -void Entity::set_soul(Game* game, Scence* scence) { - get_component().set(game, scence, this); -} - void Entity::logic() { get_component().logic(); } diff --git a/SFMLengine_alpha/src/main_structures/Game.cpp b/SFMLengine_alpha/src/main_structures/Game.cpp index 6a2bc8f..7efb85a 100644 --- a/SFMLengine_alpha/src/main_structures/Game.cpp +++ b/SFMLengine_alpha/src/main_structures/Game.cpp @@ -5,6 +5,10 @@ Game::Game() { window.setFramerateLimit(60); add_scence(); + sh.setFillColor(sf::Color::Green); + sh.setPosition(sf::Vector2f(200, 200)); + sh.setRadius(40); + scences[0].get()->get_component().set(sh); } @@ -14,8 +18,9 @@ void Game::add_scence() { } void Game::draw() { - + window.clear(); window.draw(*n_sc); + window.display(); } void Game::logic() { @@ -29,7 +34,9 @@ void Game::even() { } void Game::main_loop() { - even(); - logic(); - draw(); + while (window.isOpen()) { + even(); + logic(); + draw(); + } } \ No newline at end of file diff --git a/SFMLengine_alpha/src/main_structures/Scence.cpp b/SFMLengine_alpha/src/main_structures/Scence.cpp index 4258c1d..4b3c2fd 100644 --- a/SFMLengine_alpha/src/main_structures/Scence.cpp +++ b/SFMLengine_alpha/src/main_structures/Scence.cpp @@ -5,10 +5,7 @@ Scence::Scence(Game* game) { entities[0].add_component(); entities[0].add_component(); entities[0].set_soul(game, this); - sh.setFillColor(sf::Color::Green); - sh.setPosition(sf::Vector2f(200, 200)); - sh.setRadius(40); - entities[0].get_component().set(&sh); + } void Scence::add_entity() { @@ -25,7 +22,3 @@ void Scence::logic() { ent.logic(); } -template -void Scence::set(Game* game,int numb) { - entities[numb].set_soul(game, this); -} \ No newline at end of file