update collision detection method

Using initializer_list to implement variable parameters. Add InputListener & CollisionListener.
This commit is contained in:
Nomango 2018-03-31 18:12:01 +08:00
parent e78f8795e7
commit 2daa4d49ad
25 changed files with 893 additions and 585 deletions

View File

@ -100,119 +100,3 @@ void e2d::Action::_resetTime()
{
m_fLast = Time::getTotalTime();
}
e2d::ActionMoveBy * e2d::action::MoveBy(double duration, Vector vector)
{
return new (std::nothrow) ActionMoveBy(duration, vector);
}
e2d::ActionMoveTo * e2d::action::MoveTo(double duration, Point pos)
{
return new (std::nothrow) ActionMoveTo(duration, pos);
}
e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scale)
{
return new (std::nothrow) ActionScaleBy(duration, scale);
}
e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scaleX, double scaleY)
{
return new (std::nothrow) ActionScaleBy(duration, scaleX, scaleY);
}
e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scale)
{
return new (std::nothrow) ActionScaleTo(duration, scale);
}
e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scaleX, double scaleY)
{
return new (std::nothrow) ActionScaleTo(duration, scaleX, scaleY);
}
e2d::ActionOpacityBy * e2d::action::OpacityBy(double duration, double opacity)
{
return new (std::nothrow) ActionOpacityBy(duration, opacity);
}
e2d::ActionOpacityTo * e2d::action::OpacityTo(double duration, double opacity)
{
return new (std::nothrow) ActionOpacityTo(duration, opacity);
}
e2d::ActionFadeIn * e2d::action::FadeIn(double duration)
{
return new (std::nothrow) ActionFadeIn(duration);
}
e2d::ActionFadeOut * e2d::action::FadeOut(double duration)
{
return new (std::nothrow) ActionFadeOut(duration);
}
e2d::ActionRotateBy * e2d::action::RotateBy(double duration, double rotation)
{
return new (std::nothrow) ActionRotateBy(duration, rotation);
}
e2d::ActionRotateTo * e2d::action::RotateTo(double duration, double rotation)
{
return new (std::nothrow) ActionRotateTo(duration, rotation);
}
e2d::ActionTwo * e2d::action::Two(Action * pActionFirst, Action * pActionSecond, bool bAtSameTime)
{
return new (std::nothrow) ActionTwo(pActionFirst, pActionSecond, bAtSameTime);
}
e2d::ActionSequence * e2d::action::Sequence(int number, Action * action1, ...)
{
auto action = new (std::nothrow) ActionSequence();
if (action)
{
Action ** ppAction = &action1;
while (number > 0)
{
ASSERT((*ppAction) != nullptr, "ActionSequence NULL pointer exception!");
action->add(*ppAction);
ppAction++;
number--;
}
}
return action;
}
e2d::ActionDelay * e2d::action::Delay(double duration)
{
return new (std::nothrow) ActionDelay(duration);
}
e2d::ActionLoop * e2d::action::Loop(Action * action, int times)
{
return new (std::nothrow) ActionLoop(action, times);
}
e2d::Animation * e2d::action::Animate(double interval, int number, Image * frame, ...)
{
auto animation = new (std::nothrow) Animation(interval);
if (animation)
{
Image ** ppImage = &frame;
while (number > 0)
{
ASSERT((*ppImage) != nullptr, "Animation NULL pointer exception!");
animation->addKeyframe(*ppImage);
ppImage++;
number--;
}
}
return animation;
}
e2d::ActionFunc * e2d::action::Func(Function func)
{
return new (std::nothrow) ActionFunc(func);
}

View File

@ -5,18 +5,10 @@ e2d::ActionSequence::ActionSequence() :
{
}
e2d::ActionSequence::ActionSequence(int number, Action * action1, ...) :
e2d::ActionSequence::ActionSequence(std::initializer_list<Action*>& vActions) :
m_nActionIndex(0)
{
Action ** ppAction = &action1;
while (number > 0)
{
ASSERT((*ppAction) != nullptr, "ActionSequence NULL pointer exception!");
this->add(*ppAction);
ppAction++;
number--;
}
this->add(vActions);
}
e2d::ActionSequence::~ActionSequence()
@ -90,6 +82,14 @@ void e2d::ActionSequence::add(Action * action)
}
}
void e2d::ActionSequence::add(std::initializer_list<Action*>& vActions)
{
for (const auto &action : vActions)
{
this->add(action);
}
}
e2d::ActionSequence * e2d::ActionSequence::clone() const
{
auto a = new ActionSequence();

View File

@ -12,19 +12,9 @@ e2d::Animation::Animation(double interval)
{
}
e2d::Animation::Animation(int number, Image * frame, ...)
: m_nFrameIndex(0)
, m_fInterval(1)
e2d::Animation::Animation(std::initializer_list<Image*>& vImages)
{
Image ** ppImage = &frame;
while (number > 0)
{
ASSERT((*ppImage) != nullptr, "Animation NULL pointer exception!");
this->addKeyframe(*ppImage);
ppImage++;
number--;
}
this->add(vImages);
}
e2d::Animation::~Animation()
@ -78,7 +68,7 @@ void e2d::Animation::reset()
m_nFrameIndex = 0;
}
void e2d::Animation::addKeyframe(Image * frame)
void e2d::Animation::add(Image * frame)
{
if (frame)
{
@ -87,12 +77,20 @@ void e2d::Animation::addKeyframe(Image * frame)
}
}
void e2d::Animation::add(std::initializer_list<Image*>& vImages)
{
for (const auto &image : vImages)
{
this->add(image);
}
}
e2d::Animation * e2d::Animation::clone() const
{
auto a = new Animation(m_fInterval);
for (auto frame : m_vFrames)
{
a->addKeyframe(frame);
a->add(frame);
}
return a;
}

View File

@ -1,4 +1,6 @@
#include "..\ebase.h"
#include "..\etools.h"
#include "..\emanagers.h"
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
@ -15,9 +17,6 @@ static DIMOUSESTATE s_MouseState; //
static DIMOUSESTATE s_MouseRecordState; // 報炎佚連屈雫産喝
static POINT s_MousePosition; // 報炎了崔贋刈潤更悶
// ¼àÌýÆ÷ÈÝÆ÷
static std::vector<Listener*> s_vListeners;
bool Input::__init()
{
@ -100,21 +99,7 @@ void Input::__uninit()
void e2d::Input::__update()
{
Input::__updateDeviceState();
for (size_t i = 0; i < s_vListeners.size(); i++)
{
auto pListener = s_vListeners[i];
// ¸üмàÌýÆ÷
if (pListener->m_bClear)
{
pListener->release();
s_vListeners.erase(s_vListeners.begin() + i);
}
else if (pListener->isRunning())
{
pListener->update();
}
}
InputManager::__update();
}
void Input::__updateDeviceState()
@ -157,115 +142,6 @@ void Input::__updateDeviceState()
ScreenToClient(Window::getHWnd(), &s_MousePosition);
}
void e2d::Input::__add(Listener * pListener)
{
WARN_IF(pListener == nullptr, "Listener NULL pointer exception!");
if (pListener)
{
auto findListener = [](Listener * pListener) -> bool
{
for (const auto &l : s_vListeners)
{
if (pListener == l)
{
return true;
}
}
return false;
};
bool bHasListener = findListener(pListener);
WARN_IF(bHasListener, "The listener is already added, cannot be added again!");
if (!bHasListener)
{
pListener->retain();
s_vListeners.push_back(pListener);
}
}
}
void e2d::Input::add(Function func, String name)
{
(new Listener(func, name))->start();
}
void e2d::Input::start(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->start();
}
}
}
void e2d::Input::stop(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stop();
}
}
}
void e2d::Input::clear(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stopAndClear();
}
}
}
void e2d::Input::startAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->start();
}
}
void e2d::Input::stopAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stop();
}
}
void e2d::Input::clearAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stopAndClear();
}
}
std::vector<Listener*> e2d::Input::get(String name)
{
std::vector<Listener*> vListeners;
for (auto pListener : s_vListeners)
{
if (pListener->getName() == name)
{
vListeners.push_back(pListener);
}
}
return std::move(vListeners);
}
std::vector<Listener*> e2d::Input::getAll()
{
return s_vListeners;
}
bool Input::isKeyDown(int nKeyCode)
{
if (s_KeyBuffer[nKeyCode] & 0x80)

View File

@ -1,8 +1,6 @@
#include "..\ebase.h"
#include "..\enodes.h"
#include "..\emanagers.h"
#include "..\etools.h"
#include "..\eactions.h"
e2d::Scene::Scene()
: m_bWillSave(true)

View File

@ -0,0 +1,227 @@
#include "..\emanagers.h"
#include "..\enodes.h"
#include "..\eshape.h"
#include "..\etools.h"
// 形状集合
static std::vector<e2d::Shape*> s_vShapes;
// 监听器容器
static std::vector<e2d::CollisionListener*> s_vListeners;
// 碰撞触发状态
static bool s_bCollisionEnable = false;
void e2d::CollisionManager::setEnable(bool bEnable)
{
s_bCollisionEnable = bEnable;
}
void e2d::CollisionManager::__update()
{
for (size_t i = 0; i < s_vListeners.size(); i++)
{
auto pListener = s_vListeners[i];
// 更新监听器
if (pListener->m_bClear)
{
pListener->release();
s_vListeners.erase(s_vListeners.begin() + i);
}
else if (pListener->isRunning())
{
pListener->_update();
}
}
}
void e2d::CollisionManager::__updateShape(e2d::Shape * pActiveShape)
{
// 判断碰撞触发是否打开
if (!s_bCollisionEnable)
return;
Node* pActiveNode = pActiveShape->m_pParentNode;
if (pActiveNode)
{
// 获取节点所在场景
Scene* pCurrentScene = pActiveNode->getParentScene();
// 判断与其他形状的交集情况
for (size_t i = 0; i < s_vShapes.size(); i++)
{
auto pPassiveShape = s_vShapes[i];
// 判断两个形状是否是同一个对象
if (pActiveShape == pPassiveShape)
continue;
// 获取被碰撞节点
Node* pPassiveNode = pPassiveShape->m_pParentNode;
// 判断两节点是否处于同一场景中
if (pPassiveNode &&
pPassiveNode->getParentScene() == pCurrentScene)
{
// 判断两物体是否是相互冲突的物体
auto IsCollideWith = [](Node * active, unsigned int hash)
{
for (auto collider : active->m_vColliders)
if (collider == hash)
return true;
return false;
};
if (IsCollideWith(pActiveNode, pPassiveNode->getHashName()))
{
// 判断两形状交集情况
int relation = pActiveShape->getRelationWith(pPassiveShape);
// 忽略 UNKNOWN 和 DISJOINT 情况
if (relation != Relation::UNKNOWN && relation != Relation::DISJOINT)
{
pActiveNode->onCollide(pPassiveNode, relation);
pPassiveNode->onCollide(pActiveNode, pPassiveShape->getRelationWith(pActiveShape));
pCurrentScene->onCollide(pActiveNode, pPassiveNode);
CollisionManager::__update();
}
}
}
}
}
}
void e2d::CollisionManager::__add(CollisionListener * pListener)
{
WARN_IF(pListener == nullptr, "CollisionListener NULL pointer exception!");
if (pListener)
{
auto findListener = [](CollisionListener * pListener) -> bool
{
for (const auto &l : s_vListeners)
{
if (pListener == l)
{
return true;
}
}
return false;
};
bool bHasListener = findListener(pListener);
WARN_IF(bHasListener, "The CollisionListener is already added, cannot be added again!");
if (!bHasListener)
{
pListener->retain();
s_vListeners.push_back(pListener);
}
}
}
void e2d::CollisionManager::add(Function func, String name)
{
(new CollisionListener(func, name))->start();
}
void e2d::CollisionManager::start(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->start();
}
}
}
void e2d::CollisionManager::stop(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stop();
}
}
}
void e2d::CollisionManager::clear(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stopAndClear();
}
}
}
void e2d::CollisionManager::startAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->start();
}
}
void e2d::CollisionManager::stopAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stop();
}
}
void e2d::CollisionManager::clearAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stopAndClear();
}
}
std::vector<e2d::CollisionListener*> e2d::CollisionManager::get(String name)
{
std::vector<CollisionListener*> vListeners;
for (auto pListener : s_vListeners)
{
if (pListener->getName() == name)
{
vListeners.push_back(pListener);
}
}
return std::move(vListeners);
}
std::vector<e2d::CollisionListener*> e2d::CollisionManager::getAll()
{
return s_vListeners;
}
void e2d::CollisionManager::__addShape(Shape * pShape)
{
if (pShape)
{
if (pShape->m_pParentNode)
{
WARN_IF(true, "CollisionManager::__add Failed! The shape is already added.");
return;
}
pShape->retain();
s_vShapes.push_back(pShape);
}
}
void e2d::CollisionManager::__removeShape(Shape * pShape)
{
if (pShape)
{
for (size_t i = 0; i < s_vShapes.size(); i++)
{
if (s_vShapes[i] == pShape)
{
SafeRelease(&pShape);
s_vShapes.erase(s_vShapes.begin() + i);
return;
}
}
}
}

View File

@ -0,0 +1,133 @@
#include "..\emanagers.h"
#include "..\etools.h"
// ¼àÌýÆ÷ÈÝÆ÷
static std::vector<e2d::InputListener*> s_vListeners;
void e2d::InputManager::__update()
{
for (size_t i = 0; i < s_vListeners.size(); i++)
{
auto pListener = s_vListeners[i];
// ¸üмàÌýÆ÷
if (pListener->m_bClear)
{
pListener->release();
s_vListeners.erase(s_vListeners.begin() + i);
}
else if (pListener->isRunning())
{
pListener->_update();
}
}
}
void e2d::InputManager::__add(InputListener * pListener)
{
WARN_IF(pListener == nullptr, "InputListener NULL pointer exception!");
if (pListener)
{
auto findListener = [](InputListener * pListener) -> bool
{
for (const auto &l : s_vListeners)
{
if (pListener == l)
{
return true;
}
}
return false;
};
bool bHasListener = findListener(pListener);
WARN_IF(bHasListener, "The InputListener is already added, cannot be added again!");
if (!bHasListener)
{
pListener->retain();
s_vListeners.push_back(pListener);
}
}
}
void e2d::InputManager::add(Function func, String name)
{
(new InputListener(func, name))->start();
}
void e2d::InputManager::start(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->start();
}
}
}
void e2d::InputManager::stop(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stop();
}
}
}
void e2d::InputManager::clear(String name)
{
for (const auto & pListener : s_vListeners)
{
if (pListener->getName() == name)
{
pListener->stopAndClear();
}
}
}
void e2d::InputManager::startAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->start();
}
}
void e2d::InputManager::stopAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stop();
}
}
void e2d::InputManager::clearAll()
{
for (const auto & pListener : s_vListeners)
{
pListener->stopAndClear();
}
}
std::vector<e2d::InputListener*> e2d::InputManager::get(String name)
{
std::vector<InputListener*> vListeners;
for (auto pListener : s_vListeners)
{
if (pListener->getName() == name)
{
vListeners.push_back(pListener);
}
}
return std::move(vListeners);
}
std::vector<e2d::InputListener*> e2d::InputManager::getAll()
{
return s_vListeners;
}

View File

@ -1,87 +0,0 @@
#include "..\emanagers.h"
#include "..\enodes.h"
#include "..\eshape.h"
// 形状集合
static std::vector<e2d::Shape*> s_vShapes;
// 碰撞触发状态
static bool s_bCollisionEnable = false;
void e2d::ShapeManager::setCollisionEnable(bool bEnable)
{
s_bCollisionEnable = bEnable;
}
void e2d::ShapeManager::__updateShape(e2d::Shape * pActiveShape)
{
// 判断碰撞触发是否打开
if (!s_bCollisionEnable)
return;
Node* pActiveNode = pActiveShape->m_pParentNode;
if (pActiveNode)
{
// 获取节点所在场景
Scene* pCurrentScene = pActiveNode->getParentScene();
// 判断与其他形状的交集情况
for (size_t i = 0; i < s_vShapes.size(); i++)
{
auto pPassiveShape = s_vShapes[i];
// 判断两个形状是否是同一个对象
if (pActiveShape == pPassiveShape)
return;
// 判断两物体是否是相互冲突的物体
if (pActiveShape->m_nCollisionBitmask & pPassiveShape->m_nCategoryBitmask)
{
// 获取被碰撞节点
Node* pPassiveNode = pPassiveShape->m_pParentNode;
// 判断两节点是否处于同一场景中
if (pPassiveNode &&
pPassiveNode->getParentScene() == pCurrentScene)
{
// 判断两形状交集情况
int relation = pActiveShape->getRelationWith(pPassiveShape);
// 忽略 UNKNOWN 和 DISJOINT 情况
if (relation != Relation::UNKNOWN && relation != Relation::DISJOINT)
{
pActiveNode->onCollide(pPassiveNode, relation);
pPassiveNode->onCollide(pActiveNode, pPassiveShape->getRelationWith(pActiveShape));
pCurrentScene->onCollide(pActiveNode, pPassiveNode);
}
}
}
}
}
}
void e2d::ShapeManager::__add(Shape * pShape)
{
if (pShape)
{
if (pShape->m_pParentNode)
{
WARN_IF(true, "ShapeManager::__add Failed! The shape is already added.");
return;
}
pShape->retain();
s_vShapes.push_back(pShape);
}
}
void e2d::ShapeManager::__remove(Shape * pShape)
{
if (pShape)
{
for (size_t i = 0; i < s_vShapes.size(); i++)
{
if (s_vShapes[i] == pShape)
{
SafeRelease(&pShape);
s_vShapes.erase(s_vShapes.begin() + i);
return;
}
}
}
}

View File

@ -27,7 +27,7 @@ e2d::Button::Button(Node * normal, Function func)
, m_pDisabled(nullptr)
{
this->setNormal(normal);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::Button::Button(Node * normal, Node * selected, Function func)
@ -42,7 +42,7 @@ e2d::Button::Button(Node * normal, Node * selected, Function func)
{
this->setNormal(normal);
this->setSelected(selected);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Function func)
@ -58,7 +58,7 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Function f
this->setNormal(normal);
this->setMouseOver(mouseover);
this->setSelected(selected);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * disabled, Function func)
@ -75,7 +75,7 @@ e2d::Button::Button(Node * normal, Node * mouseover, Node * selected, Node * dis
this->setMouseOver(mouseover);
this->setSelected(selected);
this->setDisabled(disabled);
this->setFunction(func);
this->setClickFunc(func);
}
bool e2d::Button::isEnable() const
@ -170,7 +170,7 @@ void e2d::Button::setEnable(bool bEnable)
}
}
void e2d::Button::setFunction(Function func)
void e2d::Button::setClickFunc(Function func)
{
WARN_IF(m_pNormal == nullptr, "Button cannot work without anything to show. Please set its normal displayed.");

View File

@ -28,7 +28,7 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, F
{
this->setNormal(toggleOnNormal);
this->setNormalOff(toggleOffNormal);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnSelected, Node * toggleOffSelected, Function func)
@ -47,7 +47,7 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N
this->setNormalOff(toggleOffNormal);
this->setSelected(toggleOnSelected);
this->setSelectedOff(toggleOffSelected);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Function func)
@ -68,7 +68,7 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N
this->setMouseOverOff(toggleOffMouseOver);
this->setSelected(toggleOnSelected);
this->setSelectedOff(toggleOffSelected);
this->setFunction(func);
this->setClickFunc(func);
}
e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, Node * toggleOnMouseOver, Node * toggleOffMouseOver, Node * toggleOnSelected, Node * toggleOffSelected, Node * toggleOnDisabled, Node * toggleOffDisabled, Function func)
@ -91,7 +91,7 @@ e2d::ButtonToggle::ButtonToggle(Node * toggleOnNormal, Node * toggleOffNormal, N
this->setSelectedOff(toggleOffSelected);
this->setDisabled(toggleOnDisabled);
this->setDisabledOff(toggleOffDisabled);
this->setFunction(func);
this->setClickFunc(func);
}
bool e2d::ButtonToggle::getState() const

View File

@ -47,7 +47,7 @@ e2d::Node::Node()
e2d::Node::~Node()
{
ActionManager::__clearAllBindedWith(this);
ShapeManager::__remove(m_pShape);
CollisionManager::__removeShape(m_pShape);
for (auto child : m_vChildren)
{
SafeRelease(&child);
@ -288,6 +288,11 @@ e2d::String e2d::Node::getName() const
return m_sName;
}
unsigned int e2d::Node::getHashName() const
{
return m_nHashName;
}
double e2d::Node::getPosX() const
{
return m_fPosX;
@ -575,9 +580,9 @@ void e2d::Node::setShape(Shape::TYPE type)
void e2d::Node::setShape(Shape * pShape)
{
// 删除旧的形状
ShapeManager::__remove(m_pShape);
CollisionManager::__removeShape(m_pShape);
// 添加新的形状
ShapeManager::__add(pShape);
CollisionManager::__addShape(pShape);
if (pShape)
{
@ -591,6 +596,26 @@ void e2d::Node::setShape(Shape * pShape)
}
}
void e2d::Node::addCollider(String collliderName)
{
unsigned int hash = collliderName.getHashCode();
m_vColliders.insert(hash);
}
void e2d::Node::addCollider(std::initializer_list<String>& vCollliderName)
{
for (const auto &name : vCollliderName)
{
this->addCollider(name);
}
}
void e2d::Node::removeCollider(String collliderName)
{
unsigned int hash = collliderName.getHashCode();
m_vColliders.erase(hash);
}
void e2d::Node::addChild(Node * child, int order /* = 0 */)
{
WARN_IF(child == nullptr, "Node::addChild NULL pointer exception.");
@ -631,6 +656,14 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */)
}
}
void e2d::Node::addChild(std::initializer_list<Node*>& vNodes, int order)
{
for (const auto &node : vNodes)
{
this->addChild(node, order);
}
}
e2d::Node * e2d::Node::getParent() const
{
return m_pParent;

View File

@ -3,9 +3,7 @@
#include "..\enodes.h"
e2d::Shape::Shape()
: m_nCategoryBitmask(0)
, m_nCollisionBitmask(0)
, m_bIsVisiable(true)
: m_bIsVisiable(true)
, m_nColor(Color::RED)
, m_fOpacity(1)
, m_pParentNode(nullptr)
@ -25,26 +23,6 @@ e2d::Node * e2d::Shape::getParentNode() const
return m_pParentNode;
}
UINT32 e2d::Shape::getCategoryBitmask() const
{
return m_nCategoryBitmask;
}
UINT32 e2d::Shape::getCollisionBitmask() const
{
return m_nCollisionBitmask;
}
void e2d::Shape::setCategoryBitmask(UINT32 mask)
{
m_nCategoryBitmask = mask;
}
void e2d::Shape::setCollisionBitmask(UINT32 mask)
{
m_nCollisionBitmask = mask;
}
void e2d::Shape::setEnable(bool bEnable)
{
m_bEnable = bEnable;
@ -126,6 +104,6 @@ void e2d::Shape::_transform()
&m_pTransformedShape
);
ShapeManager::__updateShape(this);
CollisionManager::__updateShape(this);
}
}

View File

@ -0,0 +1,30 @@
#include "..\etools.h"
#include "..\emanagers.h"
e2d::CollisionListener::CollisionListener()
: Listener()
{
CollisionManager::__add(this);
}
e2d::CollisionListener::CollisionListener(Function func)
: Listener()
, m_callback(func)
{
CollisionManager::__add(this);
}
e2d::CollisionListener::CollisionListener(Function func, String name)
: Listener(name)
, m_callback(func)
{
CollisionManager::__add(this);
}
void e2d::CollisionListener::_update()
{
if (m_callback)
{
m_callback();
}
}

View File

@ -0,0 +1,36 @@
#include "..\etools.h"
#include "..\emanagers.h"
e2d::InputListener::InputListener()
: Listener()
, m_callback(nullptr)
{
InputManager::__add(this);
}
e2d::InputListener::InputListener(Function func)
: Listener()
, m_callback(func)
{
InputManager::__add(this);
}
e2d::InputListener::InputListener(Function func, String name)
: Listener(name)
, m_callback(func)
{
InputManager::__add(this);
}
void e2d::InputListener::setFunc(Function func)
{
m_callback = func;
}
void e2d::InputListener::_update()
{
if (m_callback)
{
m_callback();
}
}

View File

@ -1,29 +1,16 @@
#include "..\ecommon.h"
#include "..\ebase.h"
#include "..\etools.h"
e2d::Listener::Listener()
: m_bRunning(false)
, m_callback(nullptr)
, m_bClear(false)
{
Input::__add(this);
}
e2d::Listener::Listener(Function func)
: m_bRunning(false)
, m_callback(func)
, m_bClear(false)
{
Input::__add(this);
}
e2d::Listener::Listener(Function func, String name)
e2d::Listener::Listener(String name)
: m_bRunning(false)
, m_sName(name)
, m_callback(func)
, m_bClear(false)
{
Input::__add(this);
}
void e2d::Listener::start()
@ -56,16 +43,3 @@ void e2d::Listener::setName(String name)
{
m_sName = name;
}
void e2d::Listener::setFunction(Function func)
{
m_callback = func;
}
void e2d::Listener::update()
{
if (m_callback)
{
m_callback();
}
}

View File

@ -14,7 +14,7 @@ e2d::Timer::Timer(Function func, String name, double interval /* = 0 */, int upd
, m_bClear(true)
{
this->setName(name);
this->setFunction(func);
this->setFunc(func);
this->setUpdateTimes(updateTimes);
this->setInterval(interval);
m_bAutoRelease = autoRelease;
@ -59,7 +59,7 @@ void e2d::Timer::setInterval(double interval)
m_fInterval = max(interval, 0);
}
void e2d::Timer::setFunction(Function func)
void e2d::Timer::setFunc(Function func)
{
m_Callback = func;
}

View File

@ -402,16 +402,19 @@ public:
// 创建顺序动作
ActionSequence(
int number, /* 顺序动作数量 */
Action * action, /* 第一个动作 */
...
std::initializer_list<Action*>& vActions /* 动作数组 */
);
virtual ~ActionSequence();
// 向顺序动作中添加动作
// 在结尾添加动作
void add(
Action * action /* 将动作添加至顺序动作尾部 */
Action * action
);
// 在结尾添加多个动作
void add(
std::initializer_list<Action*>& vActions /* 动作数组 */
);
// 获取该动作的拷贝对象
@ -514,18 +517,21 @@ public:
// 创建帧动画
Animation(
int number, /* 帧数量 */
Image * frame, /* 第一帧 */
...
std::initializer_list<Image*>& vImages
);
virtual ~Animation();
// 添加关键帧
void addKeyframe(
void add(
Image * frame /* 关键帧 */
);
// 添加多个关键帧
void add(
std::initializer_list<Image*>& vImages /* 关键帧数组 */
);
// 设置每一帧的时间间隔
void setInterval(
double interval /* 帧间隔(秒) */
@ -664,9 +670,7 @@ namespace e2d
// 创建顺序动作
ActionSequence* Sequence(
int number, /* 顺序动作数量 */
Action * action1, /* 第一个动作 */
...
std::initializer_list<Action*>& vActions /* 动作数组 */
);
// 创建延时动作
@ -682,10 +686,8 @@ namespace e2d
// 创建特定帧间隔的帧动画
Animation* Animate(
double interval, /* 帧间隔(秒) */
int number, /* 帧数量 */
Image * frame, /* 第一帧 */
...
double interval, /* 帧间隔(秒) */
std::initializer_list<Image*>& vFrames /* 关键帧数组 */
);
// 创建执行函数对象的动作
@ -693,4 +695,104 @@ namespace e2d
Function func /* 函数对象 */
);
}
inline e2d::ActionMoveBy * e2d::action::MoveBy(double duration, Vector vector)
{
return new (std::nothrow) ActionMoveBy(duration, vector);
}
inline e2d::ActionMoveTo * e2d::action::MoveTo(double duration, Point pos)
{
return new (std::nothrow) ActionMoveTo(duration, pos);
}
inline e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scale)
{
return new (std::nothrow) ActionScaleBy(duration, scale);
}
inline e2d::ActionScaleBy * e2d::action::ScaleBy(double duration, double scaleX, double scaleY)
{
return new (std::nothrow) ActionScaleBy(duration, scaleX, scaleY);
}
inline e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scale)
{
return new (std::nothrow) ActionScaleTo(duration, scale);
}
inline e2d::ActionScaleTo * e2d::action::ScaleTo(double duration, double scaleX, double scaleY)
{
return new (std::nothrow) ActionScaleTo(duration, scaleX, scaleY);
}
inline e2d::ActionOpacityBy * e2d::action::OpacityBy(double duration, double opacity)
{
return new (std::nothrow) ActionOpacityBy(duration, opacity);
}
inline e2d::ActionOpacityTo * e2d::action::OpacityTo(double duration, double opacity)
{
return new (std::nothrow) ActionOpacityTo(duration, opacity);
}
inline e2d::ActionFadeIn * e2d::action::FadeIn(double duration)
{
return new (std::nothrow) ActionFadeIn(duration);
}
inline e2d::ActionFadeOut * e2d::action::FadeOut(double duration)
{
return new (std::nothrow) ActionFadeOut(duration);
}
inline e2d::ActionRotateBy * e2d::action::RotateBy(double duration, double rotation)
{
return new (std::nothrow) ActionRotateBy(duration, rotation);
}
inline e2d::ActionRotateTo * e2d::action::RotateTo(double duration, double rotation)
{
return new (std::nothrow) ActionRotateTo(duration, rotation);
}
inline e2d::ActionTwo * e2d::action::Two(Action * pActionFirst, Action * pActionSecond, bool bAtSameTime)
{
return new (std::nothrow) ActionTwo(pActionFirst, pActionSecond, bAtSameTime);
}
inline e2d::ActionSequence * e2d::action::Sequence(std::initializer_list<Action*>& vActions)
{
auto action = new (std::nothrow) ActionSequence();
if (action)
{
action->add(vActions);
}
return action;
}
inline e2d::ActionDelay * e2d::action::Delay(double duration)
{
return new (std::nothrow) ActionDelay(duration);
}
inline e2d::ActionLoop * e2d::action::Loop(Action * action, int times)
{
return new (std::nothrow) ActionLoop(action, times);
}
inline e2d::Animation * e2d::action::Animate(double interval, std::initializer_list<Image*>& vFrames)
{
auto animation = new (std::nothrow) Animation(interval);
if (animation)
{
animation->add(vFrames);
}
return animation;
}
inline e2d::ActionFunc * e2d::action::Func(Function func)
{
return new (std::nothrow) ActionFunc(func);
}
}

View File

@ -148,47 +148,8 @@ private:
class Input
{
friend Game;
friend Listener;
public:
// 添加输入监听
static void add(
Function func, /* 监听到消息时的执行函数 */
String name = L"" /* 监听器名称 */
);
// 启动输入监听
static void start(
String name
);
// 停止输入监听
static void stop(
String name
);
// 清除输入监听
static void clear(
String name
);
// 启动所有监听器
static void startAll();
// 停止所有监听器
static void stopAll();
// 清除所有监听器
static void clearAll();
// 获取监听器
static std::vector<Listener*> get(
String name
);
// 获取全部监听器
static std::vector<Listener*> getAll();
// 检测键盘某按键是否正被按下
static bool isKeyDown(
int nKeyCode
@ -259,11 +220,6 @@ private:
// 刷新设备状态
static void __updateDeviceState();
// 添加输入监听
static void __add(
Listener * pListener
);
// 卸载 DirectInput
static void __uninit();
};

View File

@ -1,5 +1,6 @@
#pragma once
#include "emacros.h"
#include <set>
#include <stack>
#include <vector>
#include <functional>
@ -520,7 +521,6 @@ protected:
class Node;
class Action;
class SceneManager;
// 场景
@ -592,69 +592,13 @@ protected:
};
class Input;
// 监听器
class Listener
: public Object
{
friend Input;
public:
Listener();
Listener(
Function func /* 监听到消息时的执行函数 */
);
Listener(
Function func, /* 监听到消息时的执行函数 */
String name /* 监听器名称 */
);
// 启动
void start();
// 停止
void stop();
// 停止并清除
void stopAndClear();
// 获取运行状态
bool isRunning();
// 获取名称
String getName();
// 修改名称
void setName(
String name
);
// 设置监听到消息时的执行函数
void setFunction(
Function func
);
// 更新
void update();
protected:
String m_sName;
bool m_bRunning;
bool m_bClear;
Function m_callback;
};
class ShapeManager;
class CollisionManager;
// 形状
class Shape :
public Object
{
friend ShapeManager;
friend CollisionManager;
friend Node;
public:
@ -679,22 +623,6 @@ public:
// 获取父节点
Node * getParentNode() const;
// 获取类别掩码
UINT32 getCategoryBitmask() const;
// 获取冲突掩码
UINT32 getCollisionBitmask() const;
// 设置类别掩码
void setCategoryBitmask(
UINT32 mask
);
// 设置冲突掩码
void setCollisionBitmask(
UINT32 mask
);
// 启用或关闭该形状
virtual void setEnable(
bool bEnable
@ -737,8 +665,6 @@ protected:
bool m_bEnable;
bool m_bIsVisiable;
bool m_bAutoResize;
UINT32 m_nCategoryBitmask;
UINT32 m_nCollisionBitmask;
UINT32 m_nColor;
float m_fOpacity;
Node * m_pParentNode;

View File

@ -6,15 +6,15 @@ namespace e2d
{
class Game;
class Input;
class Renderer;
class Object;
class Scene;
class Node;
class Timer;
class Action;
class Music;
class Shape;
class Transition;
class InputListener;
class CollisionListener;
// 对象管理器
class ObjectManager
@ -294,31 +294,134 @@ private:
};
class ShapeManager
// 键盘和鼠标消息管理器
class InputManager
{
friend Game;
friend Node;
friend Shape;
friend Input;
friend InputListener;
public:
// 开启或禁用碰撞触发 onCollide 函数
static void setCollisionEnable(
// 添加输入监听
static void add(
Function func, /* 监听到用户输入时的执行函数 */
String name = L"" /* 监听器名称 */
);
// 启动输入监听
static void start(
String name
);
// 停止输入监听
static void stop(
String name
);
// 清除输入监听
static void clear(
String name
);
// 启动所有监听器
static void startAll();
// 停止所有监听器
static void stopAll();
// 清除所有监听器
static void clearAll();
// 获取监听器
static std::vector<InputListener*> get(
String name
);
// 获取全部监听器
static std::vector<InputListener*> getAll();
private:
// 添加输入监听
static void __add(
InputListener * pListener
);
// 更新监听器
static void __update();
};
// 碰撞管理器
class CollisionManager
{
friend Node;
friend Shape;
friend CollisionListener;
public:
// 开启或关闭碰撞监听功能(默认关闭)
static void setEnable(
bool bEnable
);
// 添加碰撞监听
static void add(
Function func, /* 监听到碰撞时的执行函数 */
String name = L"" /* 监听器名称 */
);
// 启动碰撞监听
static void start(
String name
);
// 停止碰撞监听
static void stop(
String name
);
// 清除碰撞监听
static void clear(
String name
);
// 启动所有监听器
static void startAll();
// 停止所有监听器
static void stopAll();
// 清除所有监听器
static void clearAll();
// 获取监听器
static std::vector<CollisionListener*> get(
String name
);
// 获取全部监听器
static std::vector<CollisionListener*> getAll();
private:
// 添加碰撞监听
static void __add(
CollisionListener * pListener
);
// 更新监听器
static void __update();
// 更新形状
static void __updateShape(
Shape * pActiveShape
);
// 添加形状
static void __add(
static void __addShape(
Shape * pShape
);
// 删除已绑定的形状
static void __remove(
static void __removeShape(
Shape * pShape
);
};

View File

@ -7,6 +7,7 @@ namespace e2d
class Action;
class Transition;
class CollisionManager;
class Node :
public Object
@ -14,6 +15,7 @@ class Node :
friend Scene;
friend Shape;
friend Transition;
friend CollisionManager;
public:
Node();
@ -57,6 +59,9 @@ public:
// 获取节点名称
virtual String getName() const;
// 获取节点名称的 Hash 值
virtual unsigned int getHashName() const;
// 获取节点绘图顺序
virtual int getOrder() const;
@ -316,10 +321,31 @@ public:
Shape * pShape
);
// 添加可碰撞节点的名称
virtual void addCollider(
String collliderName
);
// 添加多个可碰撞节点的名称
virtual void addCollider(
std::initializer_list<String>& vCollliderName /* 名称数组 */
);
// 移除可碰撞节点的名称
virtual void removeCollider(
String collliderName
);
// 添加子节点
virtual void addChild(
Node * child,
int order = 0
int order = 0 /* 渲染顺序 */
);
// 添加多个子节点
virtual void addChild(
std::initializer_list<Node*>& vNodes, /* 节点数组 */
int order = 0 /* 渲染顺序 */
);
// 执行动画
@ -435,6 +461,7 @@ protected:
Node * m_pParent;
D2D1::Matrix3x2F m_MatriInitial;
D2D1::Matrix3x2F m_MatriFinal;
std::set<unsigned int> m_vColliders;
std::vector<Node*> m_vChildren;
};
@ -700,7 +727,7 @@ public:
);
// 设置按钮点击后的执行函数
void setFunction(
void setClickFunc(
Function func
);

View File

@ -7,6 +7,7 @@ namespace e2d
class TimerManager;
class MusicManager;
class InputManager;
// 随机数产生器
class Random
@ -88,7 +89,7 @@ public:
);
// 设置定时器的执行函数
void setFunction(
void setFunc(
Function func
);
@ -116,6 +117,107 @@ protected:
};
// 监听器
class Listener
: public Object
{
public:
Listener();
Listener(
String name /* 监听器名称 */
);
// 启动
virtual void start();
// 停止
virtual void stop();
// 停止并清除
virtual void stopAndClear();
// 获取运行状态
virtual bool isRunning();
// 获取名称
virtual String getName();
// 修改名称
virtual void setName(
String name
);
protected:
// 更新监听器状态
virtual void _update() = 0;
protected:
String m_sName;
bool m_bRunning;
bool m_bClear;
};
// 输入监听器
class InputListener
: public Listener
{
friend InputManager;
public:
InputListener();
InputListener(
Function func /* 监听到用户输入时的执行函数 */
);
InputListener(
Function func, /* 监听到用户输入时的执行函数 */
String name /* 监听器名称 */
);
// 设置监听到用户输入时的执行函数
virtual void setFunc(
Function func
);
protected:
// 更新监听器状态
virtual void _update();
protected:
Function m_callback;
};
// 碰撞监听器
class CollisionListener
: public Listener
{
friend CollisionManager;
public:
CollisionListener();
CollisionListener(
Function func /* 监听到用户输入时的执行函数 */
);
CollisionListener(
Function func, /* 监听到用户输入时的执行函数 */
String name /* 监听器名称 */
);
protected:
// 更新监听器状态
virtual void _update();
protected:
Function m_callback;
};
// 数据管理工具
class Data
{

View File

@ -191,7 +191,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\core\Action\Action.cpp" />
<ClCompile Include="..\..\core\Action\ActionCallback.cpp" />
<ClCompile Include="..\..\core\Action\ActionFunc.cpp" />
<ClCompile Include="..\..\core\Action\ActionDelay.cpp" />
<ClCompile Include="..\..\core\Action\ActionMoveBy.cpp" />
<ClCompile Include="..\..\core\Action\Animation.cpp" />
@ -212,7 +212,6 @@
<ClCompile Include="..\..\core\Base\Time.cpp" />
<ClCompile Include="..\..\core\Base\Window.cpp" />
<ClCompile Include="..\..\core\Common\Font.cpp" />
<ClCompile Include="..\..\core\Common\Listener.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" />
@ -220,9 +219,10 @@
<ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" />
<ClCompile Include="..\..\core\Manager\InputManager.cpp" />
<ClCompile Include="..\..\core\Manager\MusicManager.cpp" />
<ClCompile Include="..\..\core\Manager\ObjectManager.cpp" />
<ClCompile Include="..\..\core\Manager\ShapeManager.cpp" />
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp" />
<ClCompile Include="..\..\core\Manager\SceneManager.cpp" />
<ClCompile Include="..\..\core\Manager\TimerManager.cpp" />
<ClCompile Include="..\..\core\Node\Button.cpp" />
@ -235,7 +235,10 @@
<ClCompile Include="..\..\core\Shape\ShapeEllipse.cpp" />
<ClCompile Include="..\..\core\Shape\ShapeRectangle.cpp" />
<ClCompile Include="..\..\core\Shape\Shape.cpp" />
<ClCompile Include="..\..\core\Tool\CollisionListener.cpp" />
<ClCompile Include="..\..\core\Tool\Data.cpp" />
<ClCompile Include="..\..\core\Tool\InputListener.cpp" />
<ClCompile Include="..\..\core\Tool\Listener.cpp" />
<ClCompile Include="..\..\core\Tool\Path.cpp" />
<ClCompile Include="..\..\core\Tool\Music.cpp" />
<ClCompile Include="..\..\core\Tool\Random.cpp" />

View File

@ -36,9 +36,6 @@
<ClCompile Include="..\..\core\Action\Action.cpp">
<Filter>Action</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Action\ActionCallback.cpp">
<Filter>Action</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Action\ActionDelay.cpp">
<Filter>Action</Filter>
</ClCompile>
@ -147,9 +144,6 @@
<ClCompile Include="..\..\core\Manager\MusicManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\ShapeManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Shape\Shape.cpp">
<Filter>Shape</Filter>
</ClCompile>
@ -159,9 +153,6 @@
<ClCompile Include="..\..\core\Common\Size.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Listener.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Transition\Transition.cpp">
<Filter>Transition</Filter>
</ClCompile>
@ -189,6 +180,24 @@
<ClCompile Include="..\..\core\Shape\ShapeCircle.cpp">
<Filter>Shape</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\InputListener.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\CollisionManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\CollisionListener.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Manager\InputManager.cpp">
<Filter>Manager</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Tool\Listener.cpp">
<Filter>Tool</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Action\ActionFunc.cpp">
<Filter>Action</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\core\etools.h" />