diff --git a/SOURCE.md b/SOURCE.md new file mode 100644 index 0000000..cd1efe5 --- /dev/null +++ b/SOURCE.md @@ -0,0 +1,15 @@ +## Content Sources + +### Programming + +##### Book + +- Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010 + +##### Video + +- https://www.youtube.com/user/geraldmcalister/videos + +### Art + +- [The Legend of Zelda - Link to the Past Tilesheet](http://www.spriters-resource.com/snes/legendofzeldaalinktothepast/sheet/7640/) diff --git a/source.txt b/source.txt deleted file mode 100644 index ed3904d..0000000 --- a/source.txt +++ /dev/null @@ -1,9 +0,0 @@ -Book -~~~~ - -Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010 - -Video -~~~~ - -https://www.youtube.com/user/geraldmcalister/videos \ No newline at end of file diff --git a/src/camera.cpp b/src/camera.cpp index 0b48789..b10e27f 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -19,3 +19,8 @@ void Camera::MoveCamera(sf::RenderWindow *window, sf::Vector2f move) { this->view.move(move.x, move.y); window->setView(view); } + +void Camera::SetCenter(sf::RenderWindow *window, sf::Vector2f position) { + this->view.setCenter(position.x, position.y); + window->setView(view); +} diff --git a/src/camera.h b/src/camera.h index 861756a..a53c362 100644 --- a/src/camera.h +++ b/src/camera.h @@ -10,6 +10,7 @@ public: ~Camera(); void SetNewView(sf::RenderWindow *window); void MoveCamera(sf::RenderWindow *window, sf::Vector2f move); + void SetCenter(sf::RenderWindow *window, sf::Vector2f position); private: //sf::Vector2f position; diff --git a/src/data/gfx/player.png b/src/data/gfx/player.png new file mode 100644 index 0000000..6251dc2 Binary files /dev/null and b/src/data/gfx/player.png differ diff --git a/src/entity.cpp b/src/entity.cpp new file mode 100644 index 0000000..9a30e4f --- /dev/null +++ b/src/entity.cpp @@ -0,0 +1,58 @@ +#include "entity.h" + +Entity::Entity() { + this->active = 1; + this->texture = new sf::Texture(); +} + +Entity::Entity(std::string fileName) { + this->active = 1; + this->texture = new sf::Texture; + this->Load(fileName); + this->setOrigin((this->texture->getSize().x / 2), (this->texture->getSize().y / 2)); +} + +Entity::Entity(std::string fileName, sf::IntRect rect) { + this->active = 1; + this->texture = new sf::Texture(); + this->Load(fileName, rect); + this->setOrigin((this->texture->getSize().x / 2), (this->texture->getSize().y / 2)); +} + +void Entity::Load(std::string fileName) { + this->texture->loadFromFile(fileName, sf::IntRect()); + this->setTexture(*this->texture); +} + +void Entity::Load(std::string fileName, sf::IntRect rect) { + this->texture->loadFromFile(fileName, rect); + this->setTexture(*this->texture); +} + +bool Entity::Collision(Entity *entity) { + if(entity != NULL) { + return this->getGlobalBounds().intersects(entity->getGlobalBounds()); + } + + return false; +} + +void Entity::SetActive(int active) { + this->active = active; +} + +int Entity::Active() { + return this->active; +} + +int Entity::Group() { + return this->groupId; +} + +void Entity::Update() { + this->move(this->velocity); +} + +Entity::~Entity() { + delete this->texture; +} diff --git a/src/entity.h b/src/entity.h index 22580b8..80cbe07 100644 --- a/src/entity.h +++ b/src/entity.h @@ -7,60 +7,17 @@ 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; - } + Entity(); + Entity(std::string fileName); + Entity(std::string fileName, sf::IntRect rect); + void Load(std::string fileName); + void Load(std::string fileName, sf::IntRect rect); + bool Collision(Entity *entity); + void SetActive(int active); + int Active(); + int Group(); + virtual void Update(); + ~Entity(); sf::Vector2f velocity; diff --git a/src/entitymanager.cpp b/src/entitymanager.cpp new file mode 100644 index 0000000..4ad14fd --- /dev/null +++ b/src/entitymanager.cpp @@ -0,0 +1,78 @@ +#include "entitymanager.h" + +EntityManager::EntityManager() { +} + +void EntityManager::SetCollisionMethod(CollisionUpdateEvent collisionsEvent) { + this->collisionsEvent = collisionsEvent; +} + +void EntityManager::AddEntity(std::string name, Entity* entity) { + std::unordered_map::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* EntityManager::Get(std::string name) { + std::unordered_map::const_iterator found = this->entities.find(name); + if(found != this->entities.end()) { + return found->second; + } + + return NULL; +} + +void EntityManager::Update() { + std::vector 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 EntityManager::Render(sf::RenderWindow* window) { + for (auto& iterator : this->entities) { + if (iterator.second != NULL) { + if (iterator.second->Active()) { + window->draw(*iterator.second); + } + } + } +} + +EntityManager::~EntityManager() { + for (auto& iterator : this->entities) { + delete iterator.second; + } + + this->entities.clear(); +} diff --git a/src/entitymanager.h b/src/entitymanager.h index c2fe48f..0f40047 100644 --- a/src/entitymanager.h +++ b/src/entitymanager.h @@ -11,83 +11,13 @@ 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::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::const_iterator found = this->entities.find(name); - if(found != this->entities.end()) { - return found->second; - } - - return NULL; - } - - void Update() { - std::vector 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(); - } + EntityManager(); + void SetCollisionMethod(CollisionUpdateEvent collisionsEvent); + void AddEntity(std::string name, Entity* entity); + Entity* Get(std::string name); + void Update(); + void Render(sf::RenderWindow* window); + ~EntityManager(); private: std::unordered_map entities; diff --git a/src/player.cpp b/src/player.cpp new file mode 100644 index 0000000..2055099 --- /dev/null +++ b/src/player.cpp @@ -0,0 +1,99 @@ +#include "player.h" + +Player::Player(EntityManager* entityManager, Camera *camera, float x, float y) { + this->entityManager = entityManager; + this->camera = camera; + + this->Load("data/gfx/player.png"); + this->setPosition(x, y); + this->speed = 1.5f; +} + +void Player::Update(sf::RenderWindow* window, InputManager inputManager) { + // Store currect location of the player for the Camera to use + sf::Vector2f cameraPosition = sf::Vector2f(this->getPosition().x, this->getPosition().y); + + // Update player velocity + this->velocity.x = inputManager.IsPressed(InputManager::Right) * this->speed - + inputManager.IsPressed(InputManager::Left) * this->speed; + this->velocity.y = inputManager.IsPressed(InputManager::Down) * this->speed - + inputManager.IsPressed(InputManager::Up) * this->speed; + + // Update camera position + // VERSION 1! +// if(this->getPosition().x + 16 > window->getSize().x / 2) { +// this->camera->MoveCamera(window, sf::Vector2f(this->velocity.x, 0)); +// } +// else { +// camera->SetCenter(window, sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2)); +// } + +// if(this->getPosition().y + 16 > window->getSize().y / 2) { +// this->camera->MoveCamera(window, sf::Vector2f(0, this->velocity.y)); +// } +// else { +// camera->SetCenter(window, sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2)); +// } + // + + // VERSION 2! +// if(this->getPosition().x < window->getSize().x / 2 +// && this->getPosition().y < window->getSize().y / 2) { +// camera->SetCenter(window, sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2)); +// } + +// if(this->getPosition().x> window->getSize().x / 2 +// && this->getPosition().y> window->getSize().y / 2) { +// //this->camera->MoveCamera(window, sf::Vector2f(this->velocity.x, this->velocity.y)); +// camera->SetCenter(window, sf::Vector2f(this->getPosition().x, this->getPosition().y)); +// } +// else if(this->getPosition().x> window->getSize().x / 2) { +// this->camera->MoveCamera(window, sf::Vector2f(this->velocity.x, 0)); +// } +// else if(this->getPosition().y> window->getSize().y / 2) { +// this->camera->MoveCamera(window, sf::Vector2f(0, this->velocity.y)); +// } + // + + // VERSION 3! + if(this->getPosition().x < window->getSize().x / 2 + && this->getPosition().y < window->getSize().y / 2) { + camera->SetCenter(window, sf::Vector2f(window->getSize().x / 2, window->getSize().y / 2)); + } + + if(this->getPosition().x> window->getSize().x / 2 + && this->getPosition().y> window->getSize().y / 2) { + //this->camera->MoveCamera(window, sf::Vector2f(this->velocity.x, this->velocity.y)); + camera->SetCenter(window, sf::Vector2f(this->getPosition().x, this->getPosition().y)); + } + else { + if(this->getPosition().x> window->getSize().x / 2) { + this->camera->MoveCamera(window, sf::Vector2f(this->velocity.x, 0)); + } + + if(this->getPosition().y> window->getSize().y / 2) { + this->camera->MoveCamera(window, sf::Vector2f(0, this->velocity.y)); + } + } + // + + // VERSION 4! + // @Todo + // + +// sf::Vertex2f tempPlayerLoc; +// if(playerLoc.x != edge X) +// { +// tempPlayerLoc.x = playerLoc.x +// } +// if(playerLoc.y != edge Y) +// { +// tempPlayerLoc.y = playerLoc.y +// } +// view->setCenter(playerLoc) +} + +Player::~Player() +{ + +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..0ca384c --- /dev/null +++ b/src/player.h @@ -0,0 +1,30 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include +#include + +#include "camera.h" +#include "entitymanager.h" +#include "inputmanager.h" + +class Player : public Entity +{ +public: + Player(EntityManager* entityManager, Camera* camera, float x = 0, float y = 0); + void Update(sf::RenderWindow *window, InputManager inputManager); + float SetSpeed(); + int GetHealth(); + int GetMaxHealth(); + ~Player(); + +private: + EntityManager* entityManager; + Camera* camera; + int health; + int maxHealth; + float speed; + //float direction; +}; + +#endif // PLAYER_H diff --git a/src/rpg.pro b/src/rpg.pro index 203b0a6..e8a3335 100644 --- a/src/rpg.pro +++ b/src/rpg.pro @@ -22,7 +22,10 @@ SOURCES += main.cpp \ state/maingame.cpp \ mapload.cpp \ map.cpp \ - camera.cpp + camera.cpp \ + player.cpp \ + entity.cpp \ + entitymanager.cpp HEADERS += \ inputmanager.h \ @@ -32,4 +35,5 @@ HEADERS += \ state/maingame.h \ mapload.h \ map.h \ - camera.h + camera.h \ + player.h diff --git a/src/state/maingame.cpp b/src/state/maingame.cpp index fe2f5b8..10156e4 100644 --- a/src/state/maingame.cpp +++ b/src/state/maingame.cpp @@ -13,66 +13,24 @@ void MainGame::Initialize(sf::RenderWindow* window) { this->entityManager->AddEntity("test", new Entity("data/gfx/test.png")); this->entityManager->Get("test0")->velocity.x = 0.5; this->entityManager->Get("test")->setPosition(sf::Vector2f(50, 50)); + this->entityManager->Get("test0")->setPosition(sf::Vector2f(50, 300)); - // Load map + // Load Map this->map = new Map(); MapLoad mapLoad; mapLoad.Load(this->map, "data/map/level1.json"); + // Load Camera this->camera = new Camera(); this->camera->SetNewView(window); + + // Load Player + this->player = new Player(this->entityManager, this->camera, 100, 100); + this->entityManager->AddEntity("Player", this->player); } void MainGame::Update(sf::RenderWindow* window) { - this->entityManager->Get("test0")->velocity.x = 0; - this->entityManager->Get("test0")->velocity.y = 0; - - if(inputManager.IsPressed(InputManager::Left)) { - std::cout << "LEFT" << std::endl; - - this->entityManager->Get("test0")->velocity.x = -1.5; - this->camera->MoveCamera(window, sf::Vector2f(-1.5, 0)); - } - - if(inputManager.IsPressed(InputManager::Right)) { - std::cout << "RIGHT" << std::endl; - - this->entityManager->Get("test0")->velocity.x = 1.5; - this->camera->MoveCamera(window, sf::Vector2f(1.5, 0)); - } - - if(inputManager.IsPressed(InputManager::Up)) { - std::cout << "UP" << std::endl; - - this->entityManager->Get("test0")->velocity.y = -1.5; - this->camera->MoveCamera(window, sf::Vector2f(0, -1.5)); - } - - if(inputManager.IsPressed(InputManager::Down)) { - std::cout << "DOWN" << std::endl; - - this->entityManager->Get("test0")->velocity.y = 1.5; - this->camera->MoveCamera(window, sf::Vector2f(0, 1.5)); - } - - if(inputManager.IsPressed(InputManager::Up) - && inputManager.IsPressed(InputManager::Down)) { - this->entityManager->Get("test0")->velocity.x = 0; - this->entityManager->Get("test0")->velocity.y = 0; - } - - if(inputManager.IsPressed(InputManager::Left) - && inputManager.IsPressed(InputManager::Right)) { - this->entityManager->Get("test0")->velocity.x = 0; - this->entityManager->Get("test0")->velocity.y = 0; - } - -// if(inputManager.IsPressed(InputManager::Up) -// && inputManager.IsPressed(InputManager::Right)) { -// this->entityManager->Get("test0")->velocity.y = -1.125; -// this->entityManager->Get("test0")->velocity.x = 1.125; -// } - + this->player->Update(window, inputManager); this->entityManager->Update(); if(inputManager.IsPressed(InputManager::LoadMap)) { @@ -89,4 +47,6 @@ void MainGame::Render(sf::RenderWindow* window) { void MainGame::Destroy(sf::RenderWindow* window) { delete this->entityManager; + delete this->map; + delete this->camera; } diff --git a/src/state/maingame.h b/src/state/maingame.h index d711b2a..614822b 100644 --- a/src/state/maingame.h +++ b/src/state/maingame.h @@ -3,11 +3,12 @@ #include -#include "gamestate.h" +#include "camera.h" #include "entitymanager.h" +#include "gamestate.h" #include "inputmanager.h" #include "mapload.h" -#include "camera.h" +#include "player.h" class MainGame : public GameState { @@ -21,6 +22,7 @@ private: EntityManager* entityManager; Map* map; Camera* camera; + Player* player; }; #endif // MAINGAME_H