Browse Source

Add Player class, Camera update, some clean up

@Todo Camera stop following player at screen edges
master
Riyyi 9 years ago
parent
commit
f575d6b487
  1. 15
      SOURCE.md
  2. 9
      source.txt
  3. 5
      src/camera.cpp
  4. 1
      src/camera.h
  5. BIN
      src/data/gfx/player.png
  6. 58
      src/entity.cpp
  7. 65
      src/entity.h
  8. 78
      src/entitymanager.cpp
  9. 84
      src/entitymanager.h
  10. 99
      src/player.cpp
  11. 30
      src/player.h
  12. 8
      src/rpg.pro
  13. 60
      src/state/maingame.cpp
  14. 6
      src/state/maingame.h

15
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/)

9
source.txt

@ -1,9 +0,0 @@
Book
~~~~
Michael Dawson - Beginning C++ Through Game Programming, Third Edition - 2010
Video
~~~~
https://www.youtube.com/user/geraldmcalister/videos

5
src/camera.cpp

@ -19,3 +19,8 @@ void Camera::MoveCamera(sf::RenderWindow *window, sf::Vector2f move) {
this->view.move(move.x, move.y); this->view.move(move.x, move.y);
window->setView(view); window->setView(view);
} }
void Camera::SetCenter(sf::RenderWindow *window, sf::Vector2f position) {
this->view.setCenter(position.x, position.y);
window->setView(view);
}

1
src/camera.h

@ -10,6 +10,7 @@ public:
~Camera(); ~Camera();
void SetNewView(sf::RenderWindow *window); void SetNewView(sf::RenderWindow *window);
void MoveCamera(sf::RenderWindow *window, sf::Vector2f move); void MoveCamera(sf::RenderWindow *window, sf::Vector2f move);
void SetCenter(sf::RenderWindow *window, sf::Vector2f position);
private: private:
//sf::Vector2f position; //sf::Vector2f position;

BIN
src/data/gfx/player.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 425 B

58
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;
}

65
src/entity.h

@ -7,60 +7,17 @@
class Entity : public sf::Sprite class Entity : public sf::Sprite
{ {
public: public:
Entity() { Entity();
this->active = 1; Entity(std::string fileName);
this->texture = new sf::Texture(); Entity(std::string fileName, sf::IntRect rect);
} void Load(std::string fileName);
void Load(std::string fileName, sf::IntRect rect);
Entity(std::string fileName) { bool Collision(Entity *entity);
this->active = 1; void SetActive(int active);
this->texture = new sf::Texture; int Active();
this->Load(fileName); int Group();
} virtual void Update();
~Entity();
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; sf::Vector2f velocity;

78
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<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();
}

84
src/entitymanager.h

@ -11,83 +11,13 @@ typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB);
class EntityManager class EntityManager
{ {
public: public:
EntityManager() { EntityManager();
void SetCollisionMethod(CollisionUpdateEvent collisionsEvent);
} void AddEntity(std::string name, Entity* entity);
Entity* Get(std::string name);
void SetCollisionMethod(CollisionUpdateEvent collisionsEvent) { void Update();
this->collisionsEvent = collisionsEvent; void Render(sf::RenderWindow* window);
} ~EntityManager();
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: private:
std::unordered_map<std::string, Entity*> entities; std::unordered_map<std::string, Entity*> entities;

99
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()
{
}

30
src/player.h

@ -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

8
src/rpg.pro

@ -22,7 +22,10 @@ SOURCES += main.cpp \
state/maingame.cpp \ state/maingame.cpp \
mapload.cpp \ mapload.cpp \
map.cpp \ map.cpp \
camera.cpp camera.cpp \
player.cpp \
entity.cpp \
entitymanager.cpp
HEADERS += \ HEADERS += \
inputmanager.h \ inputmanager.h \
@ -32,4 +35,5 @@ HEADERS += \
state/maingame.h \ state/maingame.h \
mapload.h \ mapload.h \
map.h \ map.h \
camera.h camera.h \
player.h

60
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->AddEntity("test", new Entity("data/gfx/test.png"));
this->entityManager->Get("test0")->velocity.x = 0.5; this->entityManager->Get("test0")->velocity.x = 0.5;
this->entityManager->Get("test")->setPosition(sf::Vector2f(50, 50)); 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(); this->map = new Map();
MapLoad mapLoad; MapLoad mapLoad;
mapLoad.Load(this->map, "data/map/level1.json"); mapLoad.Load(this->map, "data/map/level1.json");
// Load Camera
this->camera = new Camera(); this->camera = new Camera();
this->camera->SetNewView(window); 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) { void MainGame::Update(sf::RenderWindow* window) {
this->entityManager->Get("test0")->velocity.x = 0; this->player->Update(window, inputManager);
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->entityManager->Update(); this->entityManager->Update();
if(inputManager.IsPressed(InputManager::LoadMap)) { if(inputManager.IsPressed(InputManager::LoadMap)) {
@ -89,4 +47,6 @@ void MainGame::Render(sf::RenderWindow* window) {
void MainGame::Destroy(sf::RenderWindow* window) { void MainGame::Destroy(sf::RenderWindow* window) {
delete this->entityManager; delete this->entityManager;
delete this->map;
delete this->camera;
} }

6
src/state/maingame.h

@ -3,11 +3,12 @@
#include <iostream> #include <iostream>
#include "gamestate.h" #include "camera.h"
#include "entitymanager.h" #include "entitymanager.h"
#include "gamestate.h"
#include "inputmanager.h" #include "inputmanager.h"
#include "mapload.h" #include "mapload.h"
#include "camera.h" #include "player.h"
class MainGame : public GameState class MainGame : public GameState
{ {
@ -21,6 +22,7 @@ private:
EntityManager* entityManager; EntityManager* entityManager;
Map* map; Map* map;
Camera* camera; Camera* camera;
Player* player;
}; };
#endif // MAINGAME_H #endif // MAINGAME_H

Loading…
Cancel
Save