Add Player class, Camera update, some clean up
@Todo Camera stop following player at screen edges
This commit is contained in:
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Binary file not shown.
|
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;
|
||||
}
|
||||
+11
-54
@@ -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;
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
+7
-77
@@ -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<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();
|
||||
}
|
||||
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<std::string, Entity*> entities;
|
||||
|
||||
@@ -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
|
||||
+6
-2
@@ -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
|
||||
|
||||
+10
-50
@@ -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;
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user