Large refactoring, using the Entity-Component model.

This commit is contained in:
2016-11-30 19:50:01 +02:00
parent bcd0a359fc
commit 9c8cbf8518
53 changed files with 1616 additions and 675 deletions

View File

@@ -13,14 +13,15 @@ namespace model {
enum Direction
{
East = 0,
NorthEast = 1,
None = 0,
East = 1,
North = 2,
NorthWest = 3,
West = 4,
SouthWest = 5,
South = 6,
SouthEast = 7
South = 8,
NorthEast = North | East,
NorthWest = North | West,
SouthWest = South | West,
SouthEast = South | East
};
}

View File

@@ -21,6 +21,8 @@ namespace model {
float lastDeltaX, lastDeltaY;
Direction direction;
bool attacking;
uint32_t attackTimeLeft;
int inventorySelection = -1;
int inventory[PLAYER_INVENTORY_SIZE];

View File

@@ -1,102 +0,0 @@
/*
* Sprite.cpp
*
* Created on: Nov 29, 2016
* Author: tibi
*/
#include <model/Sprite.h>
#include <utils/Assert.h>
#include <algorithm>
namespace farmlands {
namespace model {
Sprite::Sprite()
: anchorX(0), anchorY(0),
m_states(),
m_currentState(0),
m_currentFrame(0),
m_currentFrameTimeLeft(0)
{
}
Sprite::~Sprite()
{
}
void Sprite::addState(const SpriteState& state)
{
Assert(state.frames.size() > 0, "State must have at least one frame!");
#ifdef BUILD_DEBUG
uint32_t 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 * 1;
}
void Sprite::setState(const std::string& name)
{
Assert(m_stateNames.count(name) > 0, "Inexistent state.");
setState(m_stateNames.at(name));
}
void Sprite::advanceTime(uint32_t steps)
{
Assert(m_states.size() > 0, "Sprite must have at least one state!");
while (steps > 0)
{
// There is time left in the current frame?
if (m_currentFrameTimeLeft > 0)
{
uint32_t sub = std::min(steps, m_currentFrameTimeLeft);
m_currentFrameTimeLeft -= sub;
steps -= sub;
}
if (m_currentFrameTimeLeft == 0)
{
// Move to the next frame
if (++m_currentFrame >= currentState().frames.size())
m_currentFrame = 0;
m_currentFrameTimeLeft = currentFrame().duration * 1;
}
}
}
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);
}
} /* namespace model */
} /* namespace farmlands */

View File

@@ -1,88 +0,0 @@
/*
* Sprite.h
*
* Created on: Nov 29, 2016
* Author: tibi
*/
#ifndef MODEL_SPRITE_H_
#define MODEL_SPRITE_H_
#include <utils/Exceptions.h>
#include <vector>
#include <unordered_map>
namespace farmlands {
namespace model {
/**
* Defines an animation frame
*/
struct Frame
{
uint32_t tileSetId;
uint32_t tileSetCell;
uint32_t width, height;
uint32_t duration;
};
/**
* Defines a sprite state (aka an animation).
*/
struct SpriteState
{
std::string name;
std::vector<Frame> frames;
};
/**
* Defines a sprite
*/
class Sprite
{
public:
Sprite();
virtual ~Sprite();
/**
* 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);
/**
* Advances the current frame
*/
void advanceTime(uint32_t steps);
// Getters
SpriteState& currentState();
Frame& currentFrame();
// Public fields
float anchorX, anchorY;
private:
std::vector<SpriteState> m_states;
std::unordered_map<std::string, size_t> m_stateNames;
size_t m_currentState;
size_t m_currentFrame;
uint32_t m_currentFrameTimeLeft;
};
} /* namespace model */
} /* namespace farmlands */
#endif /* MODEL_SPRITE_H_ */

28
src/model/TileMask.h Normal file
View File

@@ -0,0 +1,28 @@
/*
* TileMask.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef MODEL_TILEMASK_H_
#define MODEL_TILEMASK_H_
namespace farmlands {
namespace model {
/**
* Tile mask used to determine what tile to use depending on neighboring tiles.
*/
enum class TileMask
{
Right = 1,
Top = 2,
Left = 4,
Bottom = 8
};
}
}
#endif /* MODEL_TILEMASK_H_ */