Riyyi
10 years ago
14 changed files with 324 additions and 194 deletions
@ -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/) |
@ -1,9 +0,0 @@ |
|||||||
Book |
|
||||||
~~~~ |
|
||||||
|
|
||||||
Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010 |
|
||||||
|
|
||||||
Video |
|
||||||
~~~~ |
|
||||||
|
|
||||||
https://www.youtube.com/user/geraldmcalister/videos |
|
After Width: | Height: | Size: 425 B |
@ -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; |
||||||
|
} |
@ -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<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* EntityManager::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 EntityManager::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 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(); |
||||||
|
} |
@ -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() |
||||||
|
{ |
||||||
|
|
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
#ifndef PLAYER_H |
||||||
|
#define PLAYER_H |
||||||
|
|
||||||
|
#include <iostream> |
||||||
|
#include <SFML/Graphics.hpp> |
||||||
|
|
||||||
|
#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
|
Loading…
Reference in new issue