diff --git a/core/Action/Action.cpp b/core/Action/Action.cpp index 8e311992..7102b355 100644 --- a/core/Action/Action.cpp +++ b/core/Action/Action.cpp @@ -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); -} diff --git a/core/Action/ActionCallback.cpp b/core/Action/ActionFunc.cpp similarity index 100% rename from core/Action/ActionCallback.cpp rename to core/Action/ActionFunc.cpp diff --git a/core/Action/ActionSequence.cpp b/core/Action/ActionSequence.cpp index da980c3f..dccdbc8f 100644 --- a/core/Action/ActionSequence.cpp +++ b/core/Action/ActionSequence.cpp @@ -5,18 +5,10 @@ e2d::ActionSequence::ActionSequence() : { } -e2d::ActionSequence::ActionSequence(int number, Action * action1, ...) : +e2d::ActionSequence::ActionSequence(std::initializer_list& 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& vActions) +{ + for (const auto &action : vActions) + { + this->add(action); + } +} + e2d::ActionSequence * e2d::ActionSequence::clone() const { auto a = new ActionSequence(); diff --git a/core/Action/Animation.cpp b/core/Action/Animation.cpp index 1f47ede0..425facef 100644 --- a/core/Action/Animation.cpp +++ b/core/Action/Animation.cpp @@ -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& 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& 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; } diff --git a/core/Base/Input.cpp b/core/Base/Input.cpp index 674a28df..9e5ac035 100644 --- a/core/Base/Input.cpp +++ b/core/Base/Input.cpp @@ -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 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 e2d::Input::get(String name) -{ - std::vector vListeners; - for (auto pListener : s_vListeners) - { - if (pListener->getName() == name) - { - vListeners.push_back(pListener); - } - } - return std::move(vListeners); -} - -std::vector e2d::Input::getAll() -{ - return s_vListeners; -} - bool Input::isKeyDown(int nKeyCode) { if (s_KeyBuffer[nKeyCode] & 0x80) diff --git a/core/Common/Scene.cpp b/core/Common/Scene.cpp index 96199995..c9aa7a83 100644 --- a/core/Common/Scene.cpp +++ b/core/Common/Scene.cpp @@ -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) diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp new file mode 100644 index 00000000..204586ff --- /dev/null +++ b/core/Manager/CollisionManager.cpp @@ -0,0 +1,227 @@ +#include "..\emanagers.h" +#include "..\enodes.h" +#include "..\eshape.h" +#include "..\etools.h" + +// 形状集合 +static std::vector s_vShapes; +// 监听器容器 +static std::vector 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::CollisionManager::get(String name) +{ + std::vector vListeners; + for (auto pListener : s_vListeners) + { + if (pListener->getName() == name) + { + vListeners.push_back(pListener); + } + } + return std::move(vListeners); +} + +std::vector 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; + } + } + } +} diff --git a/core/Manager/InputManager.cpp b/core/Manager/InputManager.cpp new file mode 100644 index 00000000..9f8b7871 --- /dev/null +++ b/core/Manager/InputManager.cpp @@ -0,0 +1,133 @@ +#include "..\emanagers.h" +#include "..\etools.h" + +// 监听器容器 +static std::vector 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::InputManager::get(String name) +{ + std::vector vListeners; + for (auto pListener : s_vListeners) + { + if (pListener->getName() == name) + { + vListeners.push_back(pListener); + } + } + return std::move(vListeners); +} + +std::vector e2d::InputManager::getAll() +{ + return s_vListeners; +} \ No newline at end of file diff --git a/core/Manager/ShapeManager.cpp b/core/Manager/ShapeManager.cpp deleted file mode 100644 index c2175b0a..00000000 --- a/core/Manager/ShapeManager.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "..\emanagers.h" -#include "..\enodes.h" -#include "..\eshape.h" - -// 形状集合 -static std::vector 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; - } - } - } -} diff --git a/core/Node/Button.cpp b/core/Node/Button.cpp index a9395f37..437d0893 100644 --- a/core/Node/Button.cpp +++ b/core/Node/Button.cpp @@ -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."); diff --git a/core/Node/ButtonToggle.cpp b/core/Node/ButtonToggle.cpp index f492af49..edc2ecf6 100644 --- a/core/Node/ButtonToggle.cpp +++ b/core/Node/ButtonToggle.cpp @@ -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 diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 007be718..e0dd7d53 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -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& 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& vNodes, int order) +{ + for (const auto &node : vNodes) + { + this->addChild(node, order); + } +} + e2d::Node * e2d::Node::getParent() const { return m_pParent; diff --git a/core/Shape/Shape.cpp b/core/Shape/Shape.cpp index 4843514f..1df9c657 100644 --- a/core/Shape/Shape.cpp +++ b/core/Shape/Shape.cpp @@ -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); } } diff --git a/core/Tool/CollisionListener.cpp b/core/Tool/CollisionListener.cpp new file mode 100644 index 00000000..37105b89 --- /dev/null +++ b/core/Tool/CollisionListener.cpp @@ -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(); + } +} diff --git a/core/Tool/InputListener.cpp b/core/Tool/InputListener.cpp new file mode 100644 index 00000000..da1c5a19 --- /dev/null +++ b/core/Tool/InputListener.cpp @@ -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(); + } +} diff --git a/core/Common/Listener.cpp b/core/Tool/Listener.cpp similarity index 52% rename from core/Common/Listener.cpp rename to core/Tool/Listener.cpp index 0535c540..9b55cc20 100644 --- a/core/Common/Listener.cpp +++ b/core/Tool/Listener.cpp @@ -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(); - } -} diff --git a/core/Tool/Timer.cpp b/core/Tool/Timer.cpp index 82bad345..cdbe5457 100644 --- a/core/Tool/Timer.cpp +++ b/core/Tool/Timer.cpp @@ -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; } diff --git a/core/eactions.h b/core/eactions.h index 5528403c..d38b6185 100644 --- a/core/eactions.h +++ b/core/eactions.h @@ -402,16 +402,19 @@ public: // 创建顺序动作 ActionSequence( - int number, /* 顺序动作数量 */ - Action * action, /* 第一个动作 */ - ... + std::initializer_list& vActions /* 动作数组 */ ); virtual ~ActionSequence(); - // 向顺序动作中添加动作 + // 在结尾添加动作 void add( - Action * action /* 将动作添加至顺序动作尾部 */ + Action * action + ); + + // 在结尾添加多个动作 + void add( + std::initializer_list& vActions /* 动作数组 */ ); // 获取该动作的拷贝对象 @@ -514,18 +517,21 @@ public: // 创建帧动画 Animation( - int number, /* 帧数量 */ - Image * frame, /* 第一帧 */ - ... + std::initializer_list& vImages ); virtual ~Animation(); // 添加关键帧 - void addKeyframe( + void add( Image * frame /* 关键帧 */ ); + // 添加多个关键帧 + void add( + std::initializer_list& vImages /* 关键帧数组 */ + ); + // 设置每一帧的时间间隔 void setInterval( double interval /* 帧间隔(秒) */ @@ -664,9 +670,7 @@ namespace e2d // 创建顺序动作 ActionSequence* Sequence( - int number, /* 顺序动作数量 */ - Action * action1, /* 第一个动作 */ - ... + std::initializer_list& vActions /* 动作数组 */ ); // 创建延时动作 @@ -682,10 +686,8 @@ namespace e2d // 创建特定帧间隔的帧动画 Animation* Animate( - double interval, /* 帧间隔(秒) */ - int number, /* 帧数量 */ - Image * frame, /* 第一帧 */ - ... + double interval, /* 帧间隔(秒) */ + std::initializer_list& 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& 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& 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); + } } \ No newline at end of file diff --git a/core/ebase.h b/core/ebase.h index 27e3fea8..35d42cd4 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -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 get( - String name - ); - - // 获取全部监听器 - static std::vector getAll(); - // 检测键盘某按键是否正被按下 static bool isKeyDown( int nKeyCode @@ -259,11 +220,6 @@ private: // 刷新设备状态 static void __updateDeviceState(); - // 添加输入监听 - static void __add( - Listener * pListener - ); - // 卸载 DirectInput static void __uninit(); }; diff --git a/core/ecommon.h b/core/ecommon.h index 7230e3e3..58d9ff5a 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -1,5 +1,6 @@ #pragma once #include "emacros.h" +#include #include #include #include @@ -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; diff --git a/core/emanagers.h b/core/emanagers.h index fd5e7884..4b5ed438 100644 --- a/core/emanagers.h +++ b/core/emanagers.h @@ -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 get( + String name + ); + + // 获取全部监听器 + static std::vector 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 get( + String name + ); + + // 获取全部监听器 + static std::vector 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 ); }; diff --git a/core/enodes.h b/core/enodes.h index f67d8077..aef41611 100644 --- a/core/enodes.h +++ b/core/enodes.h @@ -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& vCollliderName /* 名称数组 */ + ); + + // 移除可碰撞节点的名称 + virtual void removeCollider( + String collliderName + ); + // 添加子节点 virtual void addChild( Node * child, - int order = 0 + int order = 0 /* 渲染顺序 */ + ); + + // 添加多个子节点 + virtual void addChild( + std::initializer_list& vNodes, /* 节点数组 */ + int order = 0 /* 渲染顺序 */ ); // 执行动画 @@ -435,6 +461,7 @@ protected: Node * m_pParent; D2D1::Matrix3x2F m_MatriInitial; D2D1::Matrix3x2F m_MatriFinal; + std::set m_vColliders; std::vector m_vChildren; }; @@ -700,7 +727,7 @@ public: ); // 设置按钮点击后的执行函数 - void setFunction( + void setClickFunc( Function func ); diff --git a/core/etools.h b/core/etools.h index c6c3b21c..5334623e 100644 --- a/core/etools.h +++ b/core/etools.h @@ -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 { diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj index 0daf09c2..32d153ec 100644 --- a/project/vs2017/Easy2D.vcxproj +++ b/project/vs2017/Easy2D.vcxproj @@ -191,7 +191,7 @@ - + @@ -212,7 +212,6 @@ - @@ -220,9 +219,10 @@ + - + @@ -235,7 +235,10 @@ + + + diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters index 4910b170..3b509560 100644 --- a/project/vs2017/Easy2D.vcxproj.filters +++ b/project/vs2017/Easy2D.vcxproj.filters @@ -36,9 +36,6 @@ Action - - Action - Action @@ -147,9 +144,6 @@ Manager - - Manager - Shape @@ -159,9 +153,6 @@ Common - - Common - Transition @@ -189,6 +180,24 @@ Shape + + Tool + + + Manager + + + Tool + + + Manager + + + Tool + + + Action +