zoukankan      html  css  js  c++  java
  • 根据点中坐标,调整怪物动作方向

    #ifndef __FIGTH_H__
    #define __FIGTH_H__
    
    #include "cocos-ext.h"
    #include "include/ILayer.h"
    #include "kernel/BaseDefine.h"
    
    USING_NS_CC;
    USING_NS_CC_EXT;
    
    class CFigthLayer : public CBaseLayer
    {
    private:
        enum E_FIGHT
        {
            FIGHT_NIL            = 0,
            FIGHT_NORTH            = 4,    //
            FIGHT_NORTHEAST        = 5,    // 东北
            FIGHT_EAST            = 6,    //
            FIGHT_EASTSOUTH        = 7,    // 东南
            FIGHT_SOUTH            = 8,    //
            FIGHT_WESTSOUTH        = 1,    // 西南
            FIGHT_WEST            = 2,    // 西
            FIGHT_WESTNORTH        = 3,    // 西北
    
            FIGHT_ACTION        = 101,    // 标记动作
            FIGHT_IMG_RUN        = 102,
            FIGHT_RUNACTION        = 103,
        };
    
    public:
        virtual bool Create();
        virtual void OnTick(){}
        virtual void onEnter();
        virtual void onEnterTransitionDidFinish();
        virtual void onExit();
    
        virtual void registerWithTouchDispatcher();
        virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
        virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
    
    private:
        void SetDir(float x, float y);
        void RunDirect(E_FIGHT eFight);
        void RunActionFight(int nImgStart, int nImgEnd);
    private:
        CCPoint                m_tPosCurrent;        // 用于存放怪物的当前位置
        CCPoint                m_tPosEnd;            // 这两个用于判断
        CCAnimate*            m_pAction;            // 跑的动作 
        E_FIGHT                m_eFight;
        bool                m_bRun;
    };
    
    #endif // __FIGTH_H__
    #include "FightLayer.h"
    #include <math.h>
    
    #include "kernel/GlobalMgr.h"
    #include "kernel/UIMgr.h" 
    
    bool CFigthLayer::Create()
    {
        if (!CBaseLayer::Create())
            return false;
    
        m_eFight            = FIGHT_NIL;
        m_pAction            = NULL;
        m_bRun                = false;
    
        setTouchEnabled(true);
    }
    
    void CFigthLayer::onEnter()
    {
        CBaseLayer::onEnter();
    }
    
    void CFigthLayer::onEnterTransitionDidFinish()
    {
        CBaseLayer::onExitTransitionDidStart();
    
        std::string g_ImgPath(CGlobalMgr::GetInstance()->GetResourcesEx());
        std::string strPath;
    
        strPath = g_ImgPath + "rpg000.jpg";
        CCSprite* pImgBg = CCSprite::create(strPath.c_str());
        CC_ERROR(pImgBg, "【CFigthLayer::onEnterTransitionDidFinish】pImgBg为空")
        pImgBg->setPosition(ccp(320, 480));
        pImgBg->setAnchorPoint(ccp(0.5, 0.5));
        this->addChild(pImgBg);
    
        strPath = g_ImgPath + "image8.png";
        CCSprite* pImgRun = CCSprite::create(strPath.c_str());
        CC_ERROR(pImgRun, "【CFigthLayer::onEnterTransitionDidFinish】pImgRun为空")
        pImgRun->setPosition(ccp(320, 480));
        pImgRun->setAnchorPoint(ccp(0.5, 0.5));
        pImgRun->setTag(FIGHT_IMG_RUN);
        this->addChild(pImgRun);
    
        m_tPosCurrent = pImgBg->getPosition();
    }
    
    void CFigthLayer::onExit()
    {
        CBaseLayer::onExit();
    }
    
    void CFigthLayer::registerWithTouchDispatcher()
    {
        CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    }
    
    bool CFigthLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
    {
        return true;
    }
    
    void CFigthLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
    {
        m_tPosEnd = pTouch->getLocation();
        this->SetDir(m_tPosEnd.x, m_tPosEnd.y);
    }
    
    void CFigthLayer::SetDir(float x, float y)
    {
        CCPoint pos = m_tPosCurrent;
        if (pos.equals(ccp(x, y)))
            return;
    
        if (x > pos.x)
        {
            float t = (y - pos.y) / (x - pos.x);
    
            if (t > tan(- M_PI_4 / 2) && t <= tan(M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)2;
            }
            else if (t > tan(M_PI_4 - M_PI_4 / 2) && t <= tan(M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)3;
            }
            else if (t > tan(M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)4;
            }
            else if (t > tan(- M_PI_4 - M_PI_4 / 2) && t <= tan(- M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)1;
            }
            else if (t <= tan(- M_PI_4 - M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)8;
            }
        }
        else if (x < pos.x)
        {
            float t = (y - pos.y) / (x - pos.x);
    
            if (t > tan(M_PI - M_PI_4 / 2) && t <= tan(M_PI + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)6;
            }
            else if (t > tan(M_PI - M_PI_4 - M_PI_4 / 2) && t <= tan(M_PI - M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)5;
            }
            else if (t <= tan(M_PI - M_PI_4 - M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)4;
            }
            else if (t > tan(M_PI + M_PI_4 - M_PI_4 / 2) && t <= tan(M_PI + M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)7;
            }
            else if (t > tan(M_PI + M_PI_4 + M_PI_4 / 2))
            {
                m_eFight = (E_FIGHT)8;
            }
        }
        this->RunDirect(m_eFight);
    }
    
    void CFigthLayer::RunDirect(E_FIGHT eFight)
    {
        switch (eFight)
        {
        case CFigthLayer::FIGHT_NORTH:
            {
                this->RunActionFight(25, 28);
            }
            break;
        case CFigthLayer::FIGHT_NORTHEAST:
            {
                this->RunActionFight(21, 24);
            }
            break;
        case CFigthLayer::FIGHT_EAST:
            {
                this->RunActionFight(16, 20);
            }
            break;
        case CFigthLayer::FIGHT_EASTSOUTH:
            {
                this->RunActionFight(12, 15);
            }
            break;
        case CFigthLayer::FIGHT_SOUTH:
            {
                this->RunActionFight(8, 11);
            }
            break;
        case CFigthLayer::FIGHT_WESTSOUTH:
            {
                this->RunActionFight(74, 77);
            }
            break;
        case CFigthLayer::FIGHT_WEST:
            {
                this->RunActionFight(12, 15);
            }
            break;
        case CFigthLayer::FIGHT_WESTNORTH:
            {
                this->RunActionFight(12, 15);
            }
            break;
        default:
            break;
        }
    }
    
    void CFigthLayer::RunActionFight(int nImgStart, int nImgEnd)
    {
        std::string g_ImgPath(CGlobalMgr::GetInstance()->GetResourcesEx());
        std::string strPath;
    
        CCSprite* pImgRun = (CCSprite*)this->getChildByTag(FIGHT_IMG_RUN);
        CC_ERROR(pImgRun, "【CFigthLayer::RunActionFight】pImgRun为空")
        pImgRun->stopAllActions();
        CCActionManager* pActionManager = CCDirector::sharedDirector()->getActionManager();
        CC_ERROR(pActionManager, "【CFigthLayer::RunActionFight】pActionManager为空")
        if (m_bRun == true)
        {
            pActionManager->removeActionByTag(FIGHT_ACTION, m_pAction);
        }
    
        m_bRun = true;
    
        CCAnimation* pAnimation = CCAnimation::create();
        CC_ERROR(pAnimation, "【CFigthLayer::RunActionFight】pAnimation为空")
        for( int i = nImgStart; i < nImgEnd + 1; i++)
        {
            CCString* pStrPath = CCString::createWithFormat("%simage%d.png", g_ImgPath.c_str(), i);
            CC_ERROR(pStrPath, "【CFigthLayer::RunActionFight】pStrPath为空")
            pAnimation->addSpriteFrameWithFileName(pStrPath->getCString());
        }
        pAnimation->setDelayPerUnit(0.4f);
        pAnimation->setRestoreOriginalFrame(true);
        m_pAction = CCAnimate::create(pAnimation);
        m_pAction->setTag(FIGHT_ACTION);
        CC_ERROR(m_pAction, "【CFigthLayer::RunActionFight】m_pAction为空")
        pImgRun->runAction(CCRepeatForever::create(m_pAction));
    }
  • 相关阅读:
    .NET面试题系列(五)数据结构(Array、List、Queue、Stack)及线程安全问题
    一个使用 Go 的思维来帮助您构建并开发 Go 应用程序的开源框架
    UML类图学习
    服务器防攻击手段
    .NET面试题系列(四)计算机硬件知识
    .NET面试题系列(三)排序算法
    ASP.NET MVC学习(五)之MVC原理解析
    socketWriter.go
    multiWriter.go
    timeCache.go
  • 原文地址:https://www.cnblogs.com/newlist/p/3532833.html
Copyright © 2011-2022 走看看