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

@ -0,0 +1,60 @@
/*
* Camera.cpp
*
* Created on: Dec 1, 2016
* Author: tibi
*/
#include <GameState.h>
#include <components/basic/Camera.h>
#include <model/Component.h>
#include <iostream>
using namespace farmlands::model;
namespace farmlands {
namespace components {
namespace basic {
Camera::Camera()
: scale(1),
mainCamera(true)
{
}
Camera::~Camera()
{
}
Component* Camera::clone()
{
Camera* clone = new Camera();
clone->mainCamera = mainCamera;
clone->scale = scale;
return clone;
}
void Camera::onCreate()
{
if (mainCamera)
{
GameState::current().renderContext.setCamera(gameObject);
}
}
void Camera::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Camera ";
std::cout << "main="<<mainCamera<<" ";
std::cout << "scale="<<scale<<"\n";
}
}
}
}

View File

@ -0,0 +1,36 @@
/*
* Camera.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef MODEL_CAMERA_H_
#define MODEL_CAMERA_H_
#include <model/Component.h>
namespace farmlands {
namespace components {
namespace basic {
class Camera : public model::Component
{
public:
Camera();
virtual ~Camera();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onCreate();
float scale;
bool mainCamera;
};
}
}
}
#endif /* MODEL_CAMERA_H_ */

View File

@ -0,0 +1,134 @@
/*
* Sprite.cpp
*
* Created on: Nov 29, 2016
* Author: tibi
*/
#include <GameState.h>
#include <components/basic/Sprite.h>
#include <utils/Assert.h>
#include <algorithm>
#include <iostream>
namespace farmlands {
namespace components {
namespace basic {
Sprite::Sprite()
: name(),
anchorX(0), anchorY(0),
animationVelocity(1.0f),
m_states(),
m_stateNames(),
m_currentState(0),
m_currentFrame(0),
m_currentFrameTimeLeft(0)
{
}
Sprite::~Sprite()
{
}
model::Component* Sprite::clone()
{
Sprite* clone = new Sprite();
// Copy public fields
clone->name = name;
clone->anchorX = anchorX;
clone->anchorY = anchorY;
clone->animationVelocity = animationVelocity;
// Copy private memberes
std::copy(m_states.begin(), m_states.end(), std::back_inserter(clone->m_states));
std::copy(m_stateNames.begin(), m_stateNames.end(), std::inserter(clone->m_stateNames, clone->m_stateNames.end()));
clone->m_currentState = m_currentState;
clone->m_currentFrame = m_currentFrame;
clone->m_currentFrameTimeLeft = m_currentFrameTimeLeft;
return clone;
}
void Sprite::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Sprite ";
std::cout << "name="<<name<<"\n";
}
void Sprite::onPreRender()
{
advanceTime(GameState::current().elapsedTime);
}
void Sprite::addState(const SpriteState& state)
{
Assert(state.frames.size() > 0, "State must have at least one frame!");
#ifdef BUILD_DEBUG
float totalDuration = 0;
for (auto frame : state.frames)
totalDuration += frame.duration;
Assert(totalDuration > 0, "State must have a frame which last at least one tick.");
#endif
Assert(m_stateNames.count(state.name) == 0, "A state with the same name already added!");
m_states.push_back(state);
m_stateNames.emplace(state.name, m_states.size() - 1);
}
void Sprite::setState(size_t stateId)
{
Assert(stateId < m_states.size(), "Inexistent state.");
// Avoid resetting state
if (stateId == m_currentState)
return;
m_currentState = stateId;
m_currentFrame = 0;
m_currentFrameTimeLeft = currentFrame().duration;
}
void Sprite::setState(const std::string& name)
{
Assert(m_stateNames.count(name) > 0, "Inexistent state.");
setState(m_stateNames.at(name));
}
SpriteState& Sprite::currentState()
{
Assert(m_states.size() > 0, "Sprite must have at least one state!");
return m_states.at(m_currentState);
}
Frame& Sprite::currentFrame()
{
Assert(currentState().frames.size() > 0, "State must have at least one frame!");
return currentState().frames.at(m_currentFrame);
}
void Sprite::advanceTime(float fractions)
{
Assert(m_states.size() > 0, "Sprite must have at least one state!");
m_currentFrameTimeLeft -= fractions * animationVelocity;
while (m_currentFrameTimeLeft <= 0)
{
// Move to the next frame
if (++m_currentFrame >= currentState().frames.size())
m_currentFrame = 0;
m_currentFrameTimeLeft += currentFrame().duration;
}
}
}
}
} /* namespace farmlands */

View File

@ -0,0 +1,94 @@
/*
* Sprite.h
*
* Created on: Nov 29, 2016
* Author: tibi
*/
#ifndef MODEL_SPRITE_H_
#define MODEL_SPRITE_H_
#include <model/Component.h>
#include <utils/Exceptions.h>
#include <vector>
#include <unordered_map>
namespace farmlands {
namespace components {
namespace basic {
/**
* Defines an animation frame
*/
struct Frame
{
uint32_t tileSetId;
uint32_t tileSetCell;
uint32_t width, height;
float duration;
};
/**
* Defines a sprite state (i.e. an animation).
*/
struct SpriteState
{
std::string name;
std::vector<Frame> frames;
};
/**
* Defines a sprite
*/
class Sprite : public model::Component
{
public:
Sprite();
virtual ~Sprite();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onPreRender() override;
/**
* Adds a state to the sprite.
*/
void addState(const SpriteState& state);
/**
* Sets the current state.
*/
void setState(size_t stateId);
/**
* Sets the current state.
*/
void setState(const std::string& name);
// Getters
SpriteState& currentState();
Frame& currentFrame();
// Public fields
std::string name;
float anchorX, anchorY;
float animationVelocity;
private:
void advanceTime(float fractions);
std::vector<SpriteState> m_states;
std::unordered_map<std::string, size_t> m_stateNames;
size_t m_currentState;
size_t m_currentFrame;
float m_currentFrameTimeLeft;
};
}
} /* namespace components */
} /* namespace farmlands */
#endif /* MODEL_SPRITE_H_ */

View File

@ -0,0 +1,80 @@
/*
* Transform.cpp
*
* Created on: Dec 2, 2016
* Author: tibi
*/
#include <model/GameObject.h>
#include <components/basic/Transform.h>
#include <iostream>
namespace farmlands {
namespace components {
namespace basic {
Transform::Transform()
: x(0), y(0),
w(0), h(0),
m_parent(nullptr)
{
}
Transform::~Transform()
{
}
model::Component* Transform::clone()
{
Transform* clone = new Transform();
clone->x = x;
clone->y = y;
clone->w = w;
clone->h = h;
return clone;
}
void Transform::onCreate()
{
if (gameObject->parent() != nullptr)
m_parent = gameObject->parent()->component<Transform>();
}
float Transform::globalX() const
{
return (m_parent) ? m_parent->globalX() + x : x;
}
float Transform::globalY() const
{
return (m_parent) ? m_parent->globalY() + y : y;
}
void Transform::setGlobalX(float x)
{
this->x = (m_parent) ? x - m_parent->globalX() : x;
}
void Transform::setGlobalY(float y)
{
this->y = (m_parent) ? y - m_parent->globalY() : y;
}
void Transform::dump(unsigned level)
{
for (unsigned i = 0; i < level; i++)
std::cout<<" ";
std::cout << " .Component: Transform ";
std::cout << "x="<<x<<" ";
std::cout << "y="<<y<<" ";
std::cout << "w="<<w<<" ";
std::cout << "h="<<h<<"\n";
}
}
}
} /* namespace farmlands */

View File

@ -0,0 +1,48 @@
/*
* Transform.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef BASE_TRANSFORM_H_
#define BASE_TRANSFORM_H_
#include <model/Component.h>
namespace farmlands {
namespace components {
namespace basic {
class Transform: public model::Component
{
public:
Transform();
virtual ~Transform();
virtual model::Component* clone() override;
virtual void dump(unsigned level) override;
virtual void onCreate();
// Getters
float globalX() const;
float globalY() const;
// Setters
void setGlobalX(float x);
void setGlobalY(float y);
// Local coordinates (relative to parent)
float x, y;
float w, h;
private:
Transform* m_parent;
};
}
}
} /* namespace farmlands */
#endif /* BASE_TRANSFORM_H_ */