14 changed files with 378 additions and 27 deletions
			
			
		| Before Width: | Height: | Size: 301 KiB After Width: | Height: | Size: 301 KiB | 
| After Width: | Height: | Size: 388 B | 
| 
 | 
| @ -0,0 +1,74 @@ | |||||||
|  | #ifndef ENTITY_H | ||||||
|  | #define ENTITY_H | ||||||
|  | 
 | ||||||
|  | #include <string> | ||||||
|  | #include <SFML/Graphics.hpp> | ||||||
|  | 
 | ||||||
|  | class Entity : public sf::Sprite | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     Entity() { | ||||||
|  |         this->active = 1; | ||||||
|  |         this->texture = new sf::Texture(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     Entity(std::string fileName) { | ||||||
|  |         this->active = 1; | ||||||
|  |         this->texture = new sf::Texture; | ||||||
|  |         this->Load(fileName); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     Entity(std::string fileName, sf::IntRect rect) { | ||||||
|  |         this->active = 1; | ||||||
|  |         this->texture = new sf::Texture(); | ||||||
|  |         this->Load(fileName, rect); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void Load(std::string fileName) { | ||||||
|  |         this->texture->loadFromFile(fileName, sf::IntRect()); | ||||||
|  |         this->setTexture(*this->texture); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void Load(std::string fileName, sf::IntRect rect) { | ||||||
|  |         this->texture->loadFromFile(fileName, rect); | ||||||
|  |         this->setTexture(*this->texture); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     bool Collision(Entity *entity) { | ||||||
|  |         if(entity != NULL) { | ||||||
|  |             return this->getGlobalBounds().intersects(entity->getGlobalBounds()); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void SetActive(int active) { | ||||||
|  |         this->active = active; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     int Active() { | ||||||
|  |         return this->active; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     int Group() { | ||||||
|  |         return this->groupId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     virtual void Update() { | ||||||
|  |         this->move(this->velocity); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ~Entity() { | ||||||
|  |         delete this->texture; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     sf::Vector2f velocity; | ||||||
|  | 
 | ||||||
|  | protected: | ||||||
|  |     int active, groupId; | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     sf::Texture* texture; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | #endif // ENTITY_H
 | ||||||
| @ -0,0 +1,98 @@ | |||||||
|  | #ifndef ENTITYMANAGER | ||||||
|  | #define ENTITYMANAGER | ||||||
|  | 
 | ||||||
|  | #include <unordered_map> | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
|  | #include "entity.h" | ||||||
|  | 
 | ||||||
|  | typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB); | ||||||
|  | 
 | ||||||
|  | class EntityManager | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     EntityManager() { | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void SetCollisionMethod(CollisionUpdateEvent collisionsEvent) { | ||||||
|  |         this->collisionsEvent = collisionsEvent; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void AddEntity(std::string name, Entity* entity) { | ||||||
|  |         std::unordered_map<std::string, Entity*>::const_iterator found = this->entities.find(name); | ||||||
|  |         while(found != this->entities.end()) { | ||||||
|  |             name += "0"; | ||||||
|  |             found = this->entities.find(name); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         this->entities.insert(std::make_pair(name, entity)); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     Entity* Get(std::string name) { | ||||||
|  |         std::unordered_map<std::string, Entity*>::const_iterator found = this->entities.find(name); | ||||||
|  |         if(found != this->entities.end()) { | ||||||
|  |             return found->second; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         return NULL; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     void Update() { | ||||||
|  |         std::vector<std::string> toRemove; | ||||||
|  | 
 | ||||||
|  |         for (auto& iterator : this->entities) { | ||||||
|  |             if (iterator.second != NULL) { | ||||||
|  |                 if (this->collisionsEvent != NULL) { | ||||||
|  |                     for (auto& iterator2 : this->entities) { | ||||||
|  |                         if (iterator != iterator2) { | ||||||
|  |                             if (iterator.second->Collision(iterator2.second)) { | ||||||
|  |                                 this->collisionsEvent(iterator.second, iterator2.second); | ||||||
|  |                             } | ||||||
|  |                         } | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 if (iterator.second->Active()) { | ||||||
|  |                     iterator.second->Update(); | ||||||
|  |                 } | ||||||
|  |                 else { | ||||||
|  |                     toRemove.push_back(iterator.first); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         while (toRemove.size() > 0) { | ||||||
|  |             this->entities.erase(toRemove[toRemove.size() - 1]); | ||||||
|  |             toRemove.pop_back(); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         toRemove.clear(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     void Render(sf::RenderWindow* window) { | ||||||
|  |         for (auto& iterator : this->entities) { | ||||||
|  |             if (iterator.second != NULL) { | ||||||
|  |                 if (iterator.second->Active()) { | ||||||
|  |                     window->draw(*iterator.second); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     ~EntityManager() { | ||||||
|  |         for (auto& iterator : this->entities) { | ||||||
|  |             delete iterator.second; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         this->entities.clear(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | private: | ||||||
|  |     std::unordered_map<std::string, Entity*> entities; | ||||||
|  |     CollisionUpdateEvent* collisionsEvent; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | #endif // ENTITYMANAGER
 | ||||||
|  | 
 | ||||||
									
										Binary file not shown.
									
								
							
						| @ -0,0 +1,98 @@ | |||||||
|  | #include "inputmanager.h" | ||||||
|  | 
 | ||||||
|  | InputManager::InputManager() { | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | InputManager::~InputManager() { | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | sf::Keyboard::Key InputManager::KeyboardConfiguration(Input key) { | ||||||
|  | //    Left = 0,
 | ||||||
|  | //    Right,
 | ||||||
|  | //    Up,
 | ||||||
|  | //    Down,
 | ||||||
|  | 
 | ||||||
|  |     if(key == Left) return sf::Keyboard::A; | ||||||
|  |     if(key == Right) return sf::Keyboard::E; | ||||||
|  |     if(key == Up) return sf::Keyboard::Comma; | ||||||
|  |     if(key == Down) return sf::Keyboard::O; | ||||||
|  | 
 | ||||||
|  |     return sf::Keyboard::Unknown; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | int InputManager::JoystickConfiguration(Input key) { | ||||||
|  | //    if(key == Left) return 2;
 | ||||||
|  | //    if(key == Right) return 1;
 | ||||||
|  | //    if(key == Up) return 3;
 | ||||||
|  | //    if(key == Down) return 0;
 | ||||||
|  | 
 | ||||||
|  |     return -1; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | void InputManager::JoystickAxisConfiguration(Input key, sf::Joystick::Axis& axis, float& position) { | ||||||
|  |     if(key == Left) { | ||||||
|  |         axis = sf::Joystick::PovY; | ||||||
|  |         position = -100; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if(key == Right)  { | ||||||
|  |         axis = sf::Joystick::PovY; | ||||||
|  |         position = 100; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if(key == Up)  { | ||||||
|  |         axis = sf::Joystick::PovX; | ||||||
|  |         position = 100; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if(key == Down)  { | ||||||
|  |         axis = sf::Joystick::PovX; | ||||||
|  |         position = -100; | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool InputManager::IsKeyBoardPressed(sf::Keyboard::Key key) { | ||||||
|  |     return sf::Keyboard::isKeyPressed(key); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool InputManager::IsJoystickPressed(unsigned int joystick, unsigned int button) { | ||||||
|  |     return sf::Joystick::isButtonPressed(joystick, button); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool InputManager::IsJoystickAxis(unsigned int joystick, sf::Joystick::Axis axis, float position) { | ||||||
|  |     return sf::Joystick::getAxisPosition(joystick, axis) == position; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool InputManager::IsPressed(Input key, unsigned int joystick) { | ||||||
|  |     sf::Keyboard::Key myKeyboardKey = KeyboardConfiguration(key); | ||||||
|  |     if(myKeyboardKey != sf::Keyboard::Unknown) { | ||||||
|  |         if(IsKeyBoardPressed(myKeyboardKey)) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     int myJoystickKey = JoystickConfiguration(key); | ||||||
|  |     if(myJoystickKey != -1) { | ||||||
|  |         if(IsJoystickPressed(joystick, myJoystickKey)) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     sf::Joystick::Axis axis; | ||||||
|  |     float position = 0; | ||||||
|  |     JoystickAxisConfiguration(key, axis, position); | ||||||
|  |     if(position != 0) { | ||||||
|  |         if(IsJoystickAxis(joystick, axis, position)) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | //bool sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
 | ||||||
|  | //bool sf::Joystick::isButtonPressed(0, 1);
 | ||||||
|  | //float sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
 | ||||||
| @ -0,0 +1,39 @@ | |||||||
|  | #ifndef INPUTMANAGER_H | ||||||
|  | #define INPUTMANAGER_H | ||||||
|  | 
 | ||||||
|  | #include <SFML/Window/Keyboard.hpp> | ||||||
|  | #include <SFML/Window/Joystick.hpp> | ||||||
|  | 
 | ||||||
|  | class InputManager | ||||||
|  | { | ||||||
|  | public: | ||||||
|  |     InputManager(); | ||||||
|  |     ~InputManager(); | ||||||
|  | 
 | ||||||
|  |     enum Input | ||||||
|  |     { | ||||||
|  |         Left = 0, | ||||||
|  |         Right, | ||||||
|  |         Up, | ||||||
|  |         Down, | ||||||
|  | 
 | ||||||
|  |         KeyCount      ///< Keep last -- the total number of inputs
 | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     sf::Keyboard::Key KeyboardConfiguration(Input key); | ||||||
|  |     int JoystickConfiguration(Input key); | ||||||
|  |     void JoystickAxisConfiguration(Input key, sf::Joystick::Axis& axis, float& position); | ||||||
|  |     bool IsKeyBoardPressed(sf::Keyboard::Key); | ||||||
|  |     bool IsJoystickPressed(unsigned int joystick, unsigned int button); | ||||||
|  |     bool IsJoystickAxis(unsigned int joystick, sf::Joystick::Axis axis, float position); | ||||||
|  |     bool IsPressed(Input key, unsigned int joystick = 0); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | extern InputManager inputManager; | ||||||
|  | 
 | ||||||
|  | #endif // INPUTMANAGER_H
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //bool sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
 | ||||||
|  | //bool sf::Joystick::isButtonPressed(0, 1);
 | ||||||
|  | //float sf::Joystick::getAxisPosition(0, sf::Joystick::Y);
 | ||||||
| @ -1,12 +0,0 @@ | |||||||
| #include "maploader.h" |  | ||||||
| 
 |  | ||||||
| MapLoader::MapLoader() |  | ||||||
| { |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| MapLoader::~MapLoader() |  | ||||||
| { |  | ||||||
| 
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| @ -1,12 +0,0 @@ | |||||||
| #ifndef MAPLOADER_H |  | ||||||
| #define MAPLOADER_H |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| class MapLoader |  | ||||||
| { |  | ||||||
| public: |  | ||||||
|     MapLoader(); |  | ||||||
|     ~MapLoader(); |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| #endif // MAPLOADER_H
 |  | ||||||
					Loading…
					
					
				
		Reference in new issue