Reorganized classes & namespaces. Made sprite a component.

This commit is contained in:
2016-12-02 22:20:04 +02:00
parent f255905c73
commit 33de4a8d1f
51 changed files with 648 additions and 580 deletions

View File

@@ -1,69 +0,0 @@
/*
* DebugController.cpp
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <GameState.h>
#include <base/Camera.h>
#include <controller/DebugController.h>
#include <input/Input.h>
#include <iostream>
using namespace farmlands::input;
namespace farmlands {
namespace controller {
static const float ScaleVelocity = 0.5f;
static const float ScaleShiftVelocity = 2.0f;
DebugController::DebugController()
: m_camera(nullptr)
{
}
DebugController::~DebugController()
{
}
base::Component* DebugController::clone()
{
return new DebugController();
}
void DebugController::onInitialize()
{
m_camera = GameState::current().renderContext.camera();
}
void DebugController::onUpdateLogic()
{
// Compute velocity
float vel = ScaleVelocity;
if (Input::instance().pressed(GameKey::Run))
vel = ScaleShiftVelocity;
// Time independent
vel *= GameState::current().elapsedTime;
if (Input::instance().pressed(GameKey::Debug_ZoomIn))
m_camera->scale *= 1 + vel;
if (Input::instance().pressed(GameKey::Debug_ZoomOut))
m_camera->scale *= 1 - vel;
}
void DebugController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: DebugController\n";
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -1,36 +0,0 @@
/*
* DebugController.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef CONTROLLER_DEBUGCONTROLLER_H_
#define CONTROLLER_DEBUGCONTROLLER_H_
#include <base/Component.h>
#include <base/Camera.h>
namespace farmlands {
namespace controller {
class DebugController: public base::Component
{
public:
DebugController();
virtual ~DebugController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual void onUpdateLogic() override;
private:
base::Camera* m_camera;
};
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_DEBUGCONTROLLER_H_ */

View File

@@ -5,13 +5,9 @@
* Author: tibi
*/
#include <base/GameObject.h>
#include <base/Camera.h>
#include <controller/DebugController.h>
#include <model/GameObject.h>
#include <controller/FarmlandsGame.h>
#include <graphics/backend/SdlRenderer.h>
#include <graphics/BackgroundRenderer.h>
#include <graphics/SpriteRenderer.h>
#include <resources/Resources.h>
#include <SDL2/SDL.h>
@@ -20,7 +16,6 @@
#include <unistd.h>
#include <iostream>
using namespace farmlands::base;
using namespace farmlands::graphics;
using namespace farmlands::graphics::backend;
using namespace farmlands::resources;

View File

@@ -1,87 +0,0 @@
/*
* GuiController.cpp
*
* Created on: Nov 26, 2016
* Author: tibi
*/
#include <GameState.h>
#include <controller/GuiController.h>
#include <gui/widgets/TextArea.h>
#include <iostream>
namespace farmlands {
namespace controller {
GuiController::GuiController()
: m_canvas()
{
}
GuiController::~GuiController()
{
}
base::Component* GuiController::clone()
{
return new GuiController();
}
void GuiController::onInitialize()
{
m_context = &GameState::current().renderContext;
// Set up canvas
m_canvas.setSize(m_context->viewport.width, m_context->viewport.height);
// Add a text element
auto text = new gui::widgets::TextArea();
text->setText("Hello world!");
text->setSize(50, 5);
text->setPosition(100, 10);
text->setColor(0, 1, 0);
text->setBackColor(0.5f, 0, 0, 0.5f);
text->setTextSize(11);
text->setHorizontalWrap(gui::widgets::TextHorizontalWrapping::Ellipsis);
text->setVerticalWrap(gui::widgets::TextVerticalWrapping::Trim);
text->setAlignment(gui::widgets::TextAlign::BottomRight);
m_canvas.addChild(text);
}
bool GuiController::onEvent(SDL_Event& event)
{
bool handled = m_canvas.handleEvent(event);
float currentW = m_canvas.child(0)->width();
float currentH = m_canvas.child(0)->height();
if (event.type == SDL_EventType::SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_HOME)
{
m_canvas.child(0)->setSize(currentW + 5, currentH);
}
if (event.type == SDL_EventType::SDL_KEYDOWN && event.key.keysym.scancode == SDL_SCANCODE_END)
{
m_canvas.child(0)->setSize(currentW - 5, currentH);
}
return handled;
}
void GuiController::onRender()
{
// Compute render context
// Render
m_canvas.render(m_context);
}
void GuiController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: DebugController\n";
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -1,44 +0,0 @@
/*
* GuiController.h
*
* Created on: Nov 26, 2016
* Author: tibi
*/
#ifndef CONTROLLER_GUICONTROLLER_H_
#define CONTROLLER_GUICONTROLLER_H_
#include <base/Component.h>
#include <base/RenderContext.h>
#include <gui/layout/Canvas.h>
#include <SDL2/SDL.h>
namespace farmlands {
namespace controller {
class GuiController : public base::Component
{
public:
GuiController();
virtual ~GuiController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
/**
* Initializes game renderer
*/
virtual void onInitialize() override;
virtual bool onEvent(SDL_Event& event) override;
virtual void onRender() override;
private:
gui::layout::Canvas m_canvas;
base::RenderContext* m_context;
};
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_GUICONTROLLER_H_ */

View File

@@ -1,50 +0,0 @@
/*
* Giftable.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <controller/items/Giftable.h>
#include <iostream>
namespace farmlands {
namespace controller {
namespace items {
Giftable::Giftable()
{
}
Giftable::~Giftable()
{
}
base::Component* Giftable::clone()
{
return new Giftable();
}
bool Giftable::canGift(float x, float y, model::Direction d)
{
// TODO: implement Giftable::canGift
return false;
}
void Giftable::offerGift(float x, float y, model::Direction d)
{
// TODO: implement Giftable::offerGift
}
void Giftable::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Giftable\n";
}
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -1,35 +0,0 @@
/*
* Giftable.h
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#ifndef CONTROLLER_ITEMS_GIFTABLE_H_
#define CONTROLLER_ITEMS_GIFTABLE_H_
#include <base/Component.h>
#include <model/Direction.h>
namespace farmlands {
namespace controller {
namespace items {
class Giftable: public base::Component
{
public:
Giftable();
virtual ~Giftable();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
bool canGift(float x, float y, model::Direction d);
void offerGift(float x, float y, model::Direction d);
};
} /* namespace items */
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_GIFTABLE_H_ */

View File

@@ -1,83 +0,0 @@
/*
* Weapon.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <GameState.h>
#include <controller/items/Weapon.h>
#include <iostream>
namespace farmlands {
namespace controller {
namespace items {
Weapon::Weapon()
: damage(0),
critProbability(0),
critDamage(0),
attackDuration(1.0f),
m_spriteRenderer(nullptr),
m_attackTimeLeft(0)
{
}
Weapon::~Weapon()
{
}
base::Component* Weapon::clone()
{
Weapon* clone = new Weapon();
clone->damage = damage;
clone->critProbability = critProbability;
clone->critDamage = critDamage;
clone->attackDuration = attackDuration;
return clone;
}
void Weapon::onInitialize()
{
m_spriteRenderer = gameObject->component<graphics::SpriteRenderer>();
}
void Weapon::onPreRender()
{
m_spriteRenderer->sprite->setState(m_attackTimeLeft > 0);
}
void Weapon::onUpdateLogic()
{
if (m_attackTimeLeft > 0)
{
m_attackTimeLeft -= GameState::current().elapsedTime;
}
}
void Weapon::performAttack(float x, float y, model::Direction d)
{
if (m_attackTimeLeft <= 0)
{
m_attackTimeLeft = attackDuration;
}
}
void Weapon::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Transform ";
std::cout << "damage="<<damage<<" ";
std::cout << "critProbability="<<critProbability<<" ";
std::cout << "critDamage="<<critDamage<<" ";
std::cout << "attackDuration="<<attackDuration<<"\n";
}
}
} /* namespace model */
} /* namespace farmlands */

View File

@@ -1,53 +0,0 @@
/*
* Weapon.h
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#ifndef CONTROLLER_ITEMS_WEAPON_H_
#define CONTROLLER_ITEMS_WEAPON_H_
#include <base/Component.h>
#include <graphics/SpriteRenderer.h>
#include <model/Direction.h>
namespace farmlands {
namespace controller {
namespace items {
class Weapon: public base::Component
{
public:
Weapon();
virtual ~Weapon();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual void onUpdateLogic() override;
virtual void onPreRender() override;
/**
* Performs everything required to attacking an enemy with a weapon. To be called from player controller.
*/
void performAttack(float x, float y, model::Direction d);
// Properties
float damage;
float critProbability;
float critDamage;
float attackDuration; // In seconds
private:
graphics::SpriteRenderer* m_spriteRenderer;
float m_attackTimeLeft;
};
}
} /* namespace model */
} /* namespace farmlands */
#endif /* CONTROLLER_ITEMS_WEAPON_H_ */

View File

@@ -1,240 +0,0 @@
/*
* PlayerController.cpp
*
* Created on: Nov 27, 2016
* Author: tibi
*/
#include <GameState.h>
#include <controller/items/Giftable.h>
#include <controller/items/Weapon.h>
#include <controller/player/PlayerController.h>
#include <graphics/SpriteRenderer.h>
#include <input/Input.h>
#include <model/Item.h>
#include <utils/Assert.h>
#include <iostream>
using namespace farmlands::base;
using namespace farmlands::controller::items;
using namespace farmlands::graphics;
using namespace farmlands::input;
using namespace farmlands::model;
namespace farmlands {
namespace controller {
namespace player {
static const float PlayerWalkVelocity = 2.0f; // The default velocity of the player when walking (units/sec).
static const float PlayerRunVelocity = 4.0f; // The default velocity of the player when running (units/sec).
static const float PlayerAttackVelocity = 0.1f; // Movement speed when attacking.
/**
* Direction enum based on sign of velocity on x and y
*/
static const Direction VelocitySignDirections[3][3] =
{
{ Direction::NorthWest, Direction::West, Direction::SouthWest },
{ Direction::North, Direction::None, Direction::South },
{ Direction::NorthEast, Direction::East, Direction::SouthEast },
};
PlayerController::PlayerController()
: m_transform(nullptr),
m_facingDirection(Direction::South),
m_walking(false),
m_running(false),
m_currentItem(nullptr)
{
}
PlayerController::~PlayerController()
{
}
base::Component* PlayerController::clone()
{
return new PlayerController();
}
void PlayerController::onInitialize()
{
m_transform = gameObject->component<base::Transform>();
}
bool PlayerController::onEvent(SDL_Event& event)
{
handleAttackEvents(event);
handleInventoryEvents(event);
return false;
}
void PlayerController::handleAttackEvents(SDL_Event& event)
{
bool attack = (Input::instance().down(GameKey::Action, event));
if (attack && m_currentItem != nullptr)
{
// Weapon behavior
Weapon* weapon = m_currentItem->component<Weapon>();
if (weapon)
weapon->performAttack(m_transform->x, m_transform->y, m_facingDirection);
// Gift behavior
Giftable* giftable = m_currentItem->component<Giftable>();
if (giftable)
giftable->offerGift(m_transform->x, m_transform->y, m_facingDirection);
}
}
void PlayerController::handleInventoryEvents(SDL_Event& event)
{
// See what key was pressed
int slot = -1;
if (Input::instance().down(GameKey::Inventory1, event)) slot = 1;
if (Input::instance().down(GameKey::Inventory2, event)) slot = 2;
if (Input::instance().down(GameKey::Inventory3, event)) slot = 3;
if (Input::instance().down(GameKey::Inventory4, event)) slot = 4;
if (Input::instance().down(GameKey::Inventory5, event)) slot = 5;
if (Input::instance().down(GameKey::Inventory6, event)) slot = 6;
if (Input::instance().down(GameKey::Inventory7, event)) slot = 7;
if (Input::instance().down(GameKey::Inventory8, event)) slot = 8;
if (Input::instance().down(GameKey::Inventory9, event)) slot = 9;
if (Input::instance().down(GameKey::Inventory10, event)) slot = 10;
// For now we don't have an inventory, so just instantiate some object
if (0 <= slot && slot <= GameState::current().itemPrefabs.size())
{
GameObject* itemPrefab = GameState::current().itemPrefabs[slot - 1];
// Get rid of old object
if (m_currentItem != nullptr)
gameObject->destroyChild(m_currentItem);
// Instantiate new object
m_currentItem = GameObject::instantiate(itemPrefab, "Current item", gameObject);
// Print some information
// Item* item = itemPrefab->component<Item>();
// std::string name = (item == nullptr) ? "unknown" : item->name;
// std::cout << "Instantiated object " << slot << " (" << name << ")\n";
}
else if (0 <= slot)
{
std::cout << "Slot " << slot << "empty\n";
}
}
void PlayerController::onUpdateLogic()
{
m_running = false;
m_walking = false;
// Compute movement velocity
float velMultiplier = PlayerWalkVelocity;
// if (m_attackTimeLeft)
// {
// velMultiplier = PlayerAttackVelocity;
// --m_attackTimeLeft;
// } else
if (Input::instance().pressed(GameKey::Run))
{
velMultiplier = PlayerRunVelocity;
m_running = true;
}
// Make movement time independent
velMultiplier *= GameState::current().elapsedTime;
// Get velocity of axes
float vx = Input::instance().getX() * velMultiplier;
float vy = Input::instance().getY() * velMultiplier;
// Check if we can move to the new position
float newX = m_transform->x + vx;
float newY = m_transform->y + vy;
if ((vx || vy) && canMove(newX, newY))
{
m_walking = true;
m_transform->x = newX;
m_transform->y = newY;
m_facingDirection = getDirection(vx, vy);
}
}
void PlayerController::onPreRender()
{
// Get sprite
SpriteRenderer* spriteRenderer = gameObject->component<SpriteRenderer>();
// Compute current state
std::string stateName = (m_walking) ? "Walking " : "Idle ";
if (m_facingDirection & Direction::East)
stateName += "right";
else if (m_facingDirection & Direction::West)
stateName += "left";
else if (m_facingDirection & Direction::North)
stateName += "up";
else
stateName += "down";
spriteRenderer->sprite->setState(stateName);
// Set animation velocity
float animVelocity = (m_running) ? 1.0f : 0.7f;
// if (m_attackTimeLeft)
// animVelocity = 0.1f;
spriteRenderer->sprite->setAnimationVelocity(animVelocity);
// Set weapon
if (m_currentItem)
{
Transform* itemTransf = m_currentItem->component<Transform>();
itemTransf->x = 0.2f;
itemTransf->y = -0.8f;
}
// Set camera
base::Transform* cam = GameState::current().renderContext.cameraTransform();
cam->x = m_transform->x;
cam->y = m_transform->y - 1;
}
bool PlayerController::canMove(float x, float y)
{
// TODO: check collisions & stuff. For now, nothing
return true;
}
Direction PlayerController::getDirection(float vx, float vy)
{
int xx = (0 < vx) - (vx < 0);
int yy = (0 < vy) - (vy < 0);
return VelocitySignDirections[xx + 1][yy + 1];
}
void PlayerController::attack()
{
// For now - nothing
}
void PlayerController::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: PlayerController\n";
}
}
} /* namespace controller */
} /* namespace farmlands */

View File

@@ -1,55 +0,0 @@
/*
* PlayerController.h
*
* Created on: Nov 27, 2016
* Author: tibi
*/
#ifndef CONTROLLER_PLAYERCONTROLLER_H_
#define CONTROLLER_PLAYERCONTROLLER_H_
#include <base/Component.h>
#include <base/Transform.h>
#include <model/Direction.h>
#include <SDL2/SDL.h>
namespace farmlands {
namespace controller {
namespace player {
class PlayerController : public base::Component
{
public:
PlayerController();
virtual ~PlayerController();
virtual base::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onInitialize() override;
virtual bool onEvent(SDL_Event& event) override;
virtual void onUpdateLogic() override;
virtual void onPreRender() override;
private:
static model::Direction getDirection(float vx, float vy);
void handleAttackEvents(SDL_Event& event);
void handleInventoryEvents(SDL_Event& event);
bool canMove(float x, float y);
void attack();
base::Transform* m_transform;
model::Direction m_facingDirection;
bool m_walking, m_running;
base::GameObject* m_currentItem;
};
}
} /* namespace controller */
} /* namespace farmlands */
#endif /* CONTROLLER_PLAYERCONTROLLER_H_ */