Added Input and Enity
Added InputManager Added Entity Added EntityManager
This commit is contained in:
|
Before Width: | Height: | Size: 301 KiB After Width: | Height: | Size: 301 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 388 B |
@@ -0,0 +1,25 @@
|
|||||||
|
0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,-1,-1,-1,-1,-1
|
||||||
|
96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,-1,-1,-1,-1,-1
|
||||||
|
192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,-1,-1,-1,-1,-1
|
||||||
|
288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,-1,-1,-1,-1,-1
|
||||||
|
384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,-1,-1,-1,-1,-1
|
||||||
|
2152,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2171,-1,-1,-1,-1,-1
|
||||||
|
2248,2249,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2267,-1,-1,-1,-1,-1
|
||||||
|
2344,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2363,-1,-1,-1,-1,-1
|
||||||
|
2440,2441,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2459,-1,-1,-1,-1,-1
|
||||||
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
||||||
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
||||||
|
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
||||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-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
|
||||||
|
@@ -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);
|
||||||
+40
-1
@@ -1,6 +1,15 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
#include "inputmanager.h"
|
||||||
|
#include "entitymanager.h"
|
||||||
|
|
||||||
|
InputManager inputManager;
|
||||||
|
|
||||||
|
void UpdateCollisions(Entity* entityA, Entity* entityB) {
|
||||||
|
std::cout << "Collisions!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "Hello World!" << std::endl;
|
std::cout << "Hello World!" << std::endl;
|
||||||
@@ -17,6 +26,17 @@ int main()
|
|||||||
sf::Clock timer;
|
sf::Clock timer;
|
||||||
sf::Time timeElapsed;
|
sf::Time timeElapsed;
|
||||||
|
|
||||||
|
EntityManager entityManager;
|
||||||
|
entityManager.AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
|
entityManager.AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
|
entityManager.SetCollisionMethod(UpdateCollisions);
|
||||||
|
|
||||||
|
// Entity entity;
|
||||||
|
// entity.Load("data\\gfx\\test.png");
|
||||||
|
|
||||||
|
// Entity entity2;
|
||||||
|
// entity2.Load("data\\gfx\\test.png");
|
||||||
|
|
||||||
// Run the program as long as the window is open
|
// Run the program as long as the window is open
|
||||||
while (window.isOpen())
|
while (window.isOpen())
|
||||||
{
|
{
|
||||||
@@ -25,8 +45,9 @@ int main()
|
|||||||
while (window.pollEvent(event))
|
while (window.pollEvent(event))
|
||||||
{
|
{
|
||||||
// Close window : exit
|
// Close window : exit
|
||||||
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
|
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||||
window.close();
|
window.close();
|
||||||
|
}
|
||||||
|
|
||||||
// Enter fullscreen mode : key combination : Alt+Enter
|
// Enter fullscreen mode : key combination : Alt+Enter
|
||||||
if (event.type == sf::Event::KeyPressed) {
|
if (event.type == sf::Event::KeyPressed) {
|
||||||
@@ -44,9 +65,27 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(inputManager.IsPressed(InputManager::Left)) {
|
||||||
|
std::cout << "LEFT" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(inputManager.IsPressed(InputManager::Down)) {
|
||||||
|
std::cout << "DOWN" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if(entity.Collision(&entity2)) {
|
||||||
|
// std::cout << "COLLISION!" << std::endl;
|
||||||
|
// }
|
||||||
|
|
||||||
window.clear(sf::Color(72, 152, 72));
|
window.clear(sf::Color(72, 152, 72));
|
||||||
|
|
||||||
|
// window.draw(entity);
|
||||||
|
// window.draw(entity2);
|
||||||
|
|
||||||
|
entityManager.Render(&window);
|
||||||
|
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
timeElapsed = timer.getElapsedTime();
|
timeElapsed = timer.getElapsedTime();
|
||||||
|
|||||||
@@ -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
|
|
||||||
+4
-2
@@ -18,8 +18,10 @@ DEPENDPATH += $$PWD/include
|
|||||||
TEMPLATE = app
|
TEMPLATE = app
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
maploader.cpp
|
inputmanager.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
maploader.h
|
inputmanager.h \
|
||||||
|
entity.h \
|
||||||
|
entitymanager.h
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user