Large refactoring. Also, reimplemented resource manager to use parsers. Changed from json to xml (it allows comments!!!).
This commit is contained in:
35
src/base/Camera.cpp
Normal file
35
src/base/Camera.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Camera.cpp
|
||||
*
|
||||
* Created on: Dec 1, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <GameState.h>
|
||||
#include <base/Camera.h>
|
||||
#include <base/Component.h>
|
||||
|
||||
namespace farmlands {
|
||||
namespace base {
|
||||
|
||||
Camera::Camera()
|
||||
: scale(1),
|
||||
mainCamera(true)
|
||||
{
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
{
|
||||
}
|
||||
|
||||
void Camera::onCreate()
|
||||
{
|
||||
if (mainCamera)
|
||||
{
|
||||
GameState::current().renderContext.setCamera(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,9 +13,17 @@
|
||||
namespace farmlands {
|
||||
namespace base {
|
||||
|
||||
struct Camera : public Component
|
||||
class Camera : public Component
|
||||
{
|
||||
public:
|
||||
Camera();
|
||||
virtual ~Camera();
|
||||
|
||||
virtual void onCreate();
|
||||
|
||||
float scale;
|
||||
bool mainCamera;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -52,6 +52,9 @@ namespace base {
|
||||
void onPostRender();
|
||||
void onDestroy();
|
||||
|
||||
// Other properties
|
||||
std::string name;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::type_index, Component*> m_components;
|
||||
|
||||
|
25
src/base/Scene.h
Normal file
25
src/base/Scene.h
Normal file
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Scene.h
|
||||
*
|
||||
* Created on: Dec 1, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#ifndef MODEL_SCENE_H_
|
||||
#define MODEL_SCENE_H_
|
||||
|
||||
#include <base/GameObject.h>
|
||||
|
||||
namespace farmlands {
|
||||
namespace base {
|
||||
|
||||
struct Scene
|
||||
{
|
||||
GameObject root;
|
||||
uint32_t cellWidth, cellHeight;
|
||||
};
|
||||
|
||||
} /* namespace model */
|
||||
} /* namespace farmlands */
|
||||
|
||||
#endif /* MODEL_SCENE_H_ */
|
@ -19,7 +19,8 @@ Sprite::Sprite()
|
||||
m_states(),
|
||||
m_currentState(0),
|
||||
m_currentFrame(0),
|
||||
m_currentFrameTimeLeft(0)
|
||||
m_currentFrameTimeLeft(0),
|
||||
m_animationVelocity(1)
|
||||
{
|
||||
}
|
||||
|
||||
@ -53,7 +54,7 @@ void Sprite::setState(size_t stateId)
|
||||
|
||||
m_currentState = stateId;
|
||||
m_currentFrame = 0;
|
||||
m_currentFrameTimeLeft = currentFrame().duration * 1;
|
||||
m_currentFrameTimeLeft = currentFrame().duration;
|
||||
}
|
||||
|
||||
void Sprite::setState(const std::string& name)
|
||||
@ -62,28 +63,19 @@ void Sprite::setState(const std::string& name)
|
||||
setState(m_stateNames.at(name));
|
||||
}
|
||||
|
||||
void Sprite::advanceTime(uint32_t steps)
|
||||
void Sprite::advanceTime(float fractions)
|
||||
{
|
||||
Assert(m_states.size() > 0, "Sprite must have at least one state!");
|
||||
|
||||
while (steps > 0)
|
||||
m_currentFrameTimeLeft -= fractions * m_animationVelocity;
|
||||
|
||||
while (m_currentFrameTimeLeft <= 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;
|
||||
}
|
||||
// Move to the next frame
|
||||
if (++m_currentFrame >= currentState().frames.size())
|
||||
m_currentFrame = 0;
|
||||
|
||||
if (m_currentFrameTimeLeft == 0)
|
||||
{
|
||||
// Move to the next frame
|
||||
if (++m_currentFrame >= currentState().frames.size())
|
||||
m_currentFrame = 0;
|
||||
|
||||
m_currentFrameTimeLeft = currentFrame().duration * 1;
|
||||
}
|
||||
m_currentFrameTimeLeft += currentFrame().duration;
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,6 +85,11 @@ SpriteState& Sprite::currentState()
|
||||
return m_states.at(m_currentState);
|
||||
}
|
||||
|
||||
void Sprite::setAnimationVelocity(float velocity)
|
||||
{
|
||||
m_animationVelocity = velocity;
|
||||
}
|
||||
|
||||
Frame& Sprite::currentFrame()
|
||||
{
|
||||
Assert(currentState().frames.size() > 0, "State must have at least one frame!");
|
||||
|
@ -63,8 +63,12 @@ namespace base {
|
||||
/**
|
||||
* Advances the current frame
|
||||
*/
|
||||
void advanceTime(uint32_t steps);
|
||||
void advanceTime(float fractions);
|
||||
|
||||
/**
|
||||
* Sets the animation velocity. Using this property, animation can be slowed or accelerated.
|
||||
*/
|
||||
void setAnimationVelocity(float velocity);
|
||||
|
||||
// Getters
|
||||
SpriteState& currentState();
|
||||
@ -80,7 +84,8 @@ namespace base {
|
||||
|
||||
size_t m_currentState;
|
||||
size_t m_currentFrame;
|
||||
uint32_t m_currentFrameTimeLeft;
|
||||
float m_currentFrameTimeLeft;
|
||||
float m_animationVelocity;
|
||||
};
|
||||
|
||||
} /* namespace model */
|
||||
|
Reference in New Issue
Block a user