void Mecha::_animationEventHandler(cocos2d::EventCustom * event) { const auto eventObject = (dragonBones::EventObject*)event->getUserData(); if (eventObject->type == dragonBones::EventObject::FADE_IN_COMPLETE) { if (eventObject->animationState->getName() == "jump_1") { _isJumpingB = true; _speedY = -JUMP_SPEED; _armature->getAnimation().fadeIn("jump_2", -1.f, -1, 0, NORMAL_ANIMATION_GROUP); } else if (eventObject->animationState->getName() == "jump_4") { _updateAnimation(); } } else if (eventObject->type == dragonBones::EventObject::FADE_OUT_COMPLETE) { if (eventObject->animationState->getName() == "attack_01") { _isAttackingB = false; _attackState = nullptr; } } }
void Skeleton::_updateAnimation(float animation_time, int joint_idx , int anim_idx,glm::mat4 parent_transform) { Joint* j = &joint_tree_[joint_idx]; glm::mat4 node_transform = j->transform; if (anim_idx<j->num_of_pose) { const std::vector<JointPose>& sample = animations_[anim_idx].samples; int pose_idx = j->pose_id[anim_idx]; glm::quat Q; calcInterpolatedRotation(animation_time, sample[pose_idx], Q); glm::vec3 V; calcInterpolatedTranslation(animation_time, sample[pose_idx], V); glm::mat4 T = glm::translate(glm::mat4(1.0f), V); glm::mat4 R = glm::toMat4(Q); node_transform = T * R; } glm::mat4 world_transform = parent_transform*node_transform; if (j->effective_joint_id >= 0) this->animation_matrix_[j->effective_joint_id] = world_transform*effective_[j->effective_joint_id].offset; for (size_t i = 0; i != j->children_num; ++i) { _updateAnimation(animation_time, j->children_begin + i, anim_idx, world_transform); } }
void Mecha::squat(bool isSquating) { if (_isSquating == isSquating) { return; } _isSquating = isSquating; _updateAnimation(); }
void Mecha::move(int dir) { if (_moveDir == dir) { return; } _moveDir = dir; _updateAnimation(); }
std::vector<glm::mat4>& Skeleton::updateAnimation(float time_in_second) { assert(!animations_.empty()); float time_in_ticks = animations_[0].ticks_per_second*time_in_second; float animation_time = fmodf(time_in_ticks, animations_[0].duration); if (effective_.size() != animation_matrix_.size()) animation_matrix_.resize(effective_.size()); _updateAnimation(animation_time, 0, 0,glm::mat4(1.0f)); return animation_matrix_; }
Mecha::Mecha() : _isJumpingA(false), _isJumpingB(false), _isSquating(false), _isAttackingA(false), _isAttackingB(false), _weaponRIndex(0), _weaponLIndex(0), _faceDir(1), _aimDir(0), _moveDir(0), _aimRadian(0.f), _speedX(0.f), _speedY(0.f), _armature(nullptr), _armatureDisplay(nullptr), _weaponR(nullptr), _weaponL(nullptr), _aimState(nullptr), _walkState(nullptr), _attackState(nullptr), _target() { _armature = CoreElementGame::instance->factory.buildArmature("mecha_1502b"); _armatureDisplay = (dragonBones::CCArmatureDisplay*)_armature->getDisplay(); _armatureDisplay->setPosition(480.f, CoreElementGame::GROUND); _armatureDisplay->setScale(1.f); _armatureDisplay->getEventDispatcher()->setEnabled(true); _armatureDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::FADE_IN_COMPLETE, std::bind(&Mecha::_animationEventHandler, this, std::placeholders::_1)); _armatureDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::FADE_OUT_COMPLETE, std::bind(&Mecha::_animationEventHandler, this, std::placeholders::_1)); _armature->getSlot("effects_1")->displayController = NORMAL_ANIMATION_GROUP; _armature->getSlot("effects_2")->displayController = NORMAL_ANIMATION_GROUP; _weaponR = _armature->getSlot("weapon_r")->getChildArmature(); _weaponL = _armature->getSlot("weapon_l")->getChildArmature(); const auto weaponRDisplay = (dragonBones::CCArmatureDisplay*)_weaponR->getDisplay(); const auto weaponLDisplay = (dragonBones::CCArmatureDisplay*)_weaponL->getDisplay(); weaponRDisplay->getEventDispatcher()->setEnabled(true); weaponLDisplay->getEventDispatcher()->setEnabled(true); weaponRDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::FRAME_EVENT, std::bind(&Mecha::_frameEventHandler, this, std::placeholders::_1)); weaponLDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::FRAME_EVENT, std::bind(&Mecha::_frameEventHandler, this, std::placeholders::_1)); _updateAnimation(); dragonBones::WorldClock::clock.add(_armature); CoreElementGame::instance->addChild(_armatureDisplay); }
void Hero::move(int dir) { if (_moveDir == dir) { return; } _moveDir = dir; if (_moveDir) { if (_faceDir != _moveDir) { _faceDir = _moveDir; _armatureDisplay->setScaleX(-_armatureDisplay->getScaleX()); } } _updateAnimation(); }
void Mecha::_updatePosition() { const auto& position = _armatureDisplay->getPosition(); if (_speedX != 0.f) { _armatureDisplay->setPosition(position.x + _speedX, position.y); if (position.x < 0.f) { _armatureDisplay->setPosition(0.f, position.y); } else if (position.x > 960.f) { _armatureDisplay->setPosition(960.f, position.y); } } if (_speedY != 0.f) { if (_speedY > 5.f && _speedY + CoreElementGame::G <= 5.f) { _armature->getAnimation().fadeIn("jump_3", -1, -1, 0, NORMAL_ANIMATION_GROUP); } _speedY += CoreElementGame::G; _armatureDisplay->setPosition(position.x, position.y + _speedY); if (position.y < CoreElementGame::GROUND) { _armatureDisplay->setPosition(position.x, CoreElementGame::GROUND); _isJumpingA = false; _isJumpingB = false; _speedY = 0.f; _speedX = 0.f; _armature->getAnimation().fadeIn("jump_4", -1, -1, 0, NORMAL_ANIMATION_GROUP); if (_isSquating || _moveDir) { _updateAnimation(); } } } }
Hero::Hero() : _isJumping(false), _isAttacking(false), _hitCount(0), _weaponIndex(0), _weaponName(""), _weaponsLevel(), _faceDir(1), _moveDir(0), _speedX(0.f), _speedY(0.f), _armature(nullptr), _armatureDisplay(nullptr), _armArmature(nullptr) { WEAPON_LIST.push_back("sword"); WEAPON_LIST.push_back("pike"); WEAPON_LIST.push_back("axe"); WEAPON_LIST.push_back("bow"); _weaponName = WEAPON_LIST[_weaponIndex]; _weaponsLevel.resize(4, 0); _armature = KnightGame::instance->factory.buildArmature("knight"); _armatureDisplay = (dragonBones::CCArmatureDisplay*)_armature->getDisplay(); _armatureDisplay->setPosition(480.f, KnightGame::GROUND); _armatureDisplay->setScale(1.f); _armArmature = _armature->getSlot("armOutside")->getChildArmature(); const auto armArmatureDisplay = (dragonBones::CCArmatureDisplay*)_armArmature->getDisplay(); armArmatureDisplay->getEventDispatcher()->setEnabled(true); armArmatureDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::COMPLETE, std::bind(&Hero::_armEventHandler, this, std::placeholders::_1)); armArmatureDisplay->getEventDispatcher()->addCustomEventListener(dragonBones::EventObject::FRAME_EVENT, std::bind(&Hero::_armEventHandler, this, std::placeholders::_1)); _updateAnimation(); dragonBones::WorldClock::clock.add(_armature); KnightGame::instance->addChild(_armatureDisplay); }
void Hero::_updatePosition() { const auto& position = _armatureDisplay->getPosition(); if (_speedX != 0.f) { _armatureDisplay->setPosition(position.x + _speedX, position.y); if (position.x < 0.f) { _armatureDisplay->setPosition(0.f, position.y); } else if (position.x > 960.f) { _armatureDisplay->setPosition(960.f, position.y); } } if (_speedY != 0.f) { if (_speedY > 0.f && _speedY + KnightGame::G <= 0.f) { _armature->getAnimation().fadeIn("fall"); } _speedY += KnightGame::G; _armatureDisplay->setPosition(position.x, position.y + _speedY); if (position.y < KnightGame::GROUND) { _armatureDisplay->setPosition(position.x, KnightGame::GROUND); _isJumping = false; _speedY = 0.f; _speedX = 0.f; _updateAnimation(); } } }
void Mecha::_updateAim() { if (_aimDir == 0) { return; } const auto& position = _armatureDisplay->getPosition(); _faceDir = _target.x > position.x ? 1 : -1; if (_armatureDisplay->getScaleX() * _faceDir < 0) { _armatureDisplay->setScaleX(-_armatureDisplay->getScaleX()); if (_moveDir) { _updateAnimation(); } } const auto aimOffsetY = _armature->getBone("chest")->global.y; if (_faceDir > 0) { _aimRadian = std::atan2(-(_target.y - position.y + aimOffsetY), _target.x - position.x); } else { _aimRadian = dragonBones::PI - std::atan2(-(_target.y - position.y + aimOffsetY), _target.x - position.x); if (_aimRadian > dragonBones::PI) { _aimRadian -= dragonBones::PI * 2.f; } } int aimDir = 0; if (_aimRadian > 0.f) { aimDir = -1; } else { aimDir = 1; } if (_aimDir != aimDir) { _aimDir = aimDir; // Animation Mixing. if (_aimDir >= 0) { _aimState = _armature->getAnimation().fadeIn( "aimUp", 0.f, 1, 0, AIM_ANIMATION_GROUP, dragonBones::AnimationFadeOutMode::SameGroup ); } else { _aimState = _armature->getAnimation().fadeIn( "aimDown", 0.f, 1, 0, AIM_ANIMATION_GROUP, dragonBones::AnimationFadeOutMode::SameGroup ); } // Add bone Mask. //_aimState->addBoneMask("pelvis"); } _aimState->weight = std::abs(_aimRadian / dragonBones::PI * 2.f); //_armature->invalidUpdate("pelvis"); // Only Update bone Mask. _armature->invalidUpdate(); }