独自一人的手游开发之路(持续更新)(cocos2dx-开发)(IOS)

dongli19921221 2016-02-21 07:12:31
这个坑是我2013年的时候挖的,当时正在看设计模式,想着写个游戏来实践下,于是就有了这个项目。那时候我天真的以为我可以很快就完成它,然而事实并非如此,这两年发生了太多事,加上之后大病一场,这个项目也就不了了之了。
不知道你们有没有在某个夜晚整宿整宿的睡不着,心里的空虚像潮水般涌来,觉得自己似乎从未真正的做成过某件事,这种没来由的遗憾塞满了我的心,这些辗转反复无法入眠的夜晚给了我一些不一样的感觉,让我觉得,有些事,既然决定了就要做完,哪怕只有孤独伴随。我很喜欢的一句话,送给我自己,也送给所有那些心中有未完成梦想的人。
正确的事情,什么时候做都不晚。
言归正传,说说这个项目。
策划上这个游戏是要做成横版的动作策略游戏,资源全套用的都是网络上的,冒险岛素材。这是一个召唤怪物出来替你打怪过关的游戏,点击下方的怪物卡牌,就会召唤出相应的士兵,这些士兵具有一定程度的AI,会自动锁定攻击最近的怪物,能根据你输入的策略符文改变打法,能触发技能,技能触发之后士兵头上会显示一个技能的图标,点击之后就能发出技能。召唤士兵和使用技能都要用能量,能量系统类似于植物大战僵尸,需要在其产生后手动点击收集,战斗中杀死敌人也会得到能量。士兵的技能可以通过配置画面更换,士兵的属性也可以提升,总之就是一款正宗的横版过关手游。然而这只是我脑子中的想法,并没有写成策划案,系统是想到哪写到哪。说说代码吧,整个项目的继承树由CGameObjectAbstract基类开始衍生,继承出CGameImageObject负责具有渲染的子类,CGameComponentObject负责没有渲染的功能,图形接口类总有CGameMonster,CGameSoidier等等。


主要使用的还是聚合模式,用自己的类封装了cocos2dx的功能。其他使用的设计模式还有工厂方法,单例,策略,桥接等等乱七八糟的都在用。先看自己抽象的CGameInstance类,涌来封装整个游戏的启动逻辑。
[code=c]
//
// CGameInstanceIOS.h
// RPGMapstory
//
// Created by Ling Dong on 13-8-27.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#ifndef RPGMapstory_CGameInstanceIOS_h
#define RPGMapstory_CGameInstanceIOS_h


static void fun();

#include "CGameInstanceAbstract.h"
#include "cocos2d.h"
using namespace cocos2d;
class CGameInstanceIOS:public CGameInstanceAbstract
{
protected:
CGameInstanceIOS()
{
std::cout<<"CGameInstanceIOS is create"<<std::endl;
}
CGameInstanceIOS(const CGameInstanceIOS& rhs)
{

}
const CGameInstanceIOS& operator=(const CGameInstanceIOS& rhs)
{

}
public:
virtual ~CGameInstanceIOS()
{
std::cout<<"CGameInstance is destory"<<std::endl;
}

static CGameInstanceIOS * sharedGameInstanceIOS();

virtual void InitGameInstance();

virtual void InitGameSetting();

virtual void CleareGameInstance();

virtual void PauseGameInstance();

virtual void ResumeGameInstance();

virtual void StartRunGameInstance();

private:
CCScene * m_pGameScene;

};

#endif

//
// CGameInstance.cpp
// RPGMapstory
//
// Created by Ling Dong on 13-9-4.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "CGameInstanceIOS.h"
#include "CGameDataStruct.h"
#include "cocos2d.h"
#include "CGameScene.h"
using namespace cocos2d;
static CGameInstanceIOS * theGameInstnace = 0;

CGameInstanceIOS * CGameInstanceIOS::sharedGameInstanceIOS()
{
if(!theGameInstnace)
{
theGameInstnace = new CGameInstanceIOS();
if (theGameInstnace)
{
theGameInstnace->InitGameInstance();
return theGameInstnace;
}
}

return theGameInstnace;

}


void CGameInstanceIOS::InitGameInstance()
{

}

void CGameInstanceIOS::InitGameSetting()
{
CCDirector::sharedDirector()->setOpenGLView(CCEGLView::sharedOpenGLView());
CCDirector::sharedDirector()->setDisplayStats(true);
CCDirector::sharedDirector()->setAnimationInterval(GAME_INTERVAL);

}

void CGameInstanceIOS::CleareGameInstance()
{

}

void CGameInstanceIOS::StartRunGameInstance()
{
m_pGameScene = CGameScene::GetScene();
CCDirector::sharedDirector()->runWithScene(m_pGameScene);
}

void CGameInstanceIOS::PauseGameInstance()
{
m_iGameStateValue = GAME_STATE_PAUSE;
}

void CGameInstanceIOS::ResumeGameInstance()
{
m_iGameStateValue = GAME_STATE_RESUME;
}

然后是自己继承自CCLayer的CGameScene涌来封装游戏流程。

//
// CGameScene.h
// RPGMapstory
//
// Created by Ling Dong on 13-9-4.
// Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#ifndef RPGMapstory_CGameScene_h
#define RPGMapstory_CGameScene_h

#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class CGameScene:public CCLayer
{
public:
CGameScene()
{
std::cout<<"CGameScene is create"<<std::endl;
}

virtual ~CGameScene()
{
std::cout<<"CGameScene is destory"<<std::endl;
}

static CCScene * GetScene();

virtual bool init();

CREATE_FUNC(CGameScene);

void InitGameScene();
/*
* init game
*/
void RunGameWithFrame();
/*
* 游戏帧循环
*/
void CleareGameScene();
/*
* 清除游戏资源
*/
void OnGameLogoAnimation();
/*
* logo动画事件处理器
*/
void OnGameTitle();
/*
* 标题画面处理器
*/
void OnGameSelectScene();
/*
* 选择关卡画面处理器
*/
void OnGameSceneLogic();
/*
* 场景逻辑处理器
*/
void OnGameConfiguration();
/*
* 配置角色画面处理器
*/
void OnGamePause();

void OnGameResume();

bool ccTouchBegan(CCTouch * pTouch, CCEvent * pEvent);

void SetTouchPoint(const CCPoint& Point)
{
m_TouchPoint = Point;
}
CCPoint GetTouchPoint()const
{
return m_TouchPoint;
}

void Func()
{
int a;
};
private:
int m_iGameRuningStateValue;
bool m_bFirstProtect;
bool m_bGameRuning;
bool m_bIsNeedLoadResource;
bool m_bHasSaveData;
bool m_bHasTouch;
bool m_bIsNewGame;
bool m_bTitleProtect;
std::string m_szCurrentMapName;
CCPoint m_TouchPoint;
};

#endif
然后是自己定义的所有数据结构。


...全文
940 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_35203541 2016-06-02
  • 打赏
  • 举报
回复
好牛啊 关键是坚持下来
baseball11 2016-03-12
  • 打赏
  • 举报
回复
自己写游戏真难啊,支持下
dongli19921221 2016-02-21
  • 打赏
  • 举报
回复
CGameDataStruct 定义了所有的游戏数据结构

//
//  CGameDataStruct.h
//  RPGMapstory
//
//  Created by Ling Dong on 13-4-14.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#ifndef RPGMapstory_CGameDataStruct_h
#define RPGMapstory_CGameDataStruct_h
#include <iostream>
#include <vector>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
extern void PrintMessage(const string& szMessage);
extern bool WaitingSec(int iSec);
static bool CANPRINT = 1;

/*
 *  当前的怪物ID
 *  Monster_JiaYang
 *  Monster_HongWoNiu
 *  Monster_WanJuXiong
 *  Monster_BaiXueRen
    Monster_LanLing
    Monster_PangXieBing
 Monster_TuZiBing
 Monster_ZhaoHuanShi
 Monster_DaTuZiBing
 Monster_DunPaiBing
 Monster_HongDaFa
 Monster_HuoFa
 Monster_LanChui
 Monster_LanLangFu
 */
typedef unsigned int UINT;
class CGameScene;
const int VALUE_TYPE_STRING = 1;
const int VALUE_TYPE_INT = 2;
const int VALUE_TYPE_DOUBLE = 3;
const int VALUE_TYPE_BOOL = 4;
const int VALUE_TYPE_FLOAT = 5;

const int MAP_INDEX_CITY = 1;
const int MAP_INDEX_COUNTRY = 2;

const int GAME_STATE_START_RUN = 1;
const int GAME_STATE_NEW_GAME = 2;
const int GAME_STATE_PAUSE = 3;
const int GAME_STATE_RESUME = 4;
const int GAME_STATE_TITLE = 5;
const int GAME_STATE_LOGO = 6;
const int GAME_STATE_LOGIC = 7;
const int GAME_STATE_SELECT = 8;
const int GAME_STATE_CONFIGURATION = 9;
const float GAME_INTERVAL = 1.0/60;
const float GAME_TRANS_INTERVAL = 1;

/** 主角纸娃娃系统各部位的偏移值,基于主角坐标
 *
 */
const int OFFSET_HEAD_X = 1;
const int OFFSET_HEAD_Y = 25;
const int OFFSET_FACE_X = -2;
const int OFFSET_FACE_Y = 25;
const int OFFSET_BODY_X = 0;
const int OFFSET_BODY_Y = 0;
const int OFFSET_HAIR_X = -1;
const int OFFSET_HAIR_Y = 39;
const int OFFSET_TOP_1HWEAPONSWING_X = -1;
const int OFFSET_TOP_1HWEAPONSWING_Y = 3;
const int OFFSET_BOTTOM_X = -3;
const int OFFSET_BOTTOM_Y = 3;
const int OFFSET_SHOES_X = -1;
const int OFFSET_SHOES_Y = 0;
const int OFFSET_CAPES_X = 0;
const int OFFSET_CAPES_Y = 0;
const int OFFSET_GLOUES_X = -2;
const int OFFSET_GLOUES_Y = 8;
const int OFFSET_HAT_X = -1;
const int OFFSET_HAT_Y = 41;
const int OFFSET_SHIELD_X = -9;
const int OFFSET_SHIELD_Y = 0;
const int OFFSET_TOP_WALKING1_X = 1;
const int OFFSET_TOP_WALKING1_Y = 5;
const int CHARACTAR_INIT_ATTACK = 10;
const int CHARACTAR_INIT_DEFINESE = 10;
const int CHARACTAR_INIT_LEVEL = 1;
const int CHARACTAR_INIT_LIFE = 100;
const int CHARACTAR_INIT_MANA = 100;


/** 主角各部位的深度值
 *
 */
const int EQUIPMENT_DEEP_GLOUES = 100;
const int EQUIPMENT_DEEP_WEAPON_BLADE = EQUIPMENT_DEEP_GLOUES - 1;
const int EQUIPMENT_DEEP_WEAPON_HANDLE = EQUIPMENT_DEEP_GLOUES - 2;
const int EQUIPMENT_DEEP_HAT = EQUIPMENT_DEEP_GLOUES - 3;
const int EQUIPMENT_DEEP_HAIR = EQUIPMENT_DEEP_GLOUES - 4;
const int EQUIPMENT_DEEP_FACE = EQUIPMENT_DEEP_GLOUES - 5;
const int EQUIPMENT_DEEP_HEAD = EQUIPMENT_DEEP_GLOUES - 6;
const int EQUIPMENT_DEEP_SHOES = EQUIPMENT_DEEP_GLOUES - 7;
const int EQUIPMENT_DEEP_BOTTOM = EQUIPMENT_DEEP_GLOUES - 8;
const int EQUIPMENT_DEEP_TOP = EQUIPMENT_DEEP_GLOUES - 9;
const int EQUIPMENT_DEEP_SHIELD = EQUIPMENT_DEEP_GLOUES - 13;
const int EQUIPMENT_DEEP_CAPES = EQUIPMENT_DEEP_GLOUES - 11;
const int EQUIPMENT_DEEP_BODY = EQUIPMENT_DEEP_GLOUES - 12;

const float DEFAULT_ANIMATION_DELAY = 0.12;
const float MONSTER_ANIMATION_DELAY = 0.12;
const float EQU_ANIMATION_DELAY = 0.13;
const float EFFECT_ANIMATION_DELAY = 0.1;
const int DEFAULT_ANIMAITON_LOOPS = -1;
const int MONSTER_ANIMATION_LOOPS = -1;
const int EQU_ANIMATION_LOOPS = -1;
const int EFFECT_ANIMATION_LOOPS = -1;
const int MONSTER_ORIENTATION_LEFT = 1;
const int MONSTER_ORIENTATION_RIGHT = 2;

const int DEEP_MONSTER = 50;
const int DEEP_UI = 150;
const int DEEP_MAP = 40;


const int ORIENTATION_DOWN = 4;
const int ORIENTATION_UP = 2;
const int ORIENTATION_LEFT = 0;
const int ORIENTATION_RIGHT = 1;

const int MONSTER_EVENT_MOVE_LEFT = 0;
const int MONSTER_EVENT_MOVE_RIGHT = 1;
const int MONSTER_EVENT_MOVE_STAND = 2;
const int MONSTER_EVENT_ATTACKA = 3;

const int MONSTER_TYPE_NORMAL = 1;
const int MONSTER_TYPE_BOSS = 2;

const int DAMEGE_TYPE_NORMAL = 1;
const int DAMEGE_TYPE_CRUSH = 2;
const int SCROLL_TYPE_NORMAL = 1;


const int SOLIDER_ANIMATION_STATE_ATTACK = 1;
const int SOLIDER_ANIMATION_STATE_MOVE = 2;
const int SOLIDER_ANIMATION_STATE_STAND = 3;
const int SOLIDER_ANIMATION_STATE_DIE = 4;
const int SOLIDER_ANIMATION_STATE_HIT = 5;

enum enumMonsterState
{
    MONSTER_STATE_STAND = 1,
    MONSTER_STATE_MOVE,
    MONSTER_STATE_DIE,
    MONSTER_STATE_ATTACKA,
    MONSTER_STATE_MOVE_LEFT,
    MONSTER_STATE_MOVE_RIGHT,
    MONSTER_STATE_HIT
};

enum enumEquipmentPart 
{
    EQU_PART_TOP = 1,
    EQU_PART_BOTTOM,
    EQU_PART_HAT,
    EQU_PART_HEAD,
    EQU_PART_FACE,
    EQU_PART_HAIR,
    EQU_PART_BODY,
    EQU_PART_GLOUES,
    EQU_PART_SHOES,
    EQU_PART_SINGLE_WEAPON,
    EQU_PART_SHIELD,
};

enum enumAnimationType
{
    ANIMATION_TYPE_MONSTER = 1,
    ANIMATION_TYPE_EQU,
    ANIMATION_TYPE_EFFECT
};

enum enumAnimationFlags
{
    FLAGS_STANDING = 1,
    FLAGS_WALKINGA,
    FLAGS_HIT,
    FLAGS_ALERT,
    FLAGS_JUMP,
    FLAGS_DIE,
    FLAGS_
};

typedef struct TagAnimationStruct
{
    int                 AnimationFrameCount;
    string              AnimationID;
    string              AnimationFrontName;
    string              AnimaitonResourceImageName;
    string              AnimationResourcePlistName;
    string              AnimationType;
    string              AnimationFlags;
    vector<string>      AnimationFrameNameVec;
}STRUCT_ANIMATION,*STRUCT_ANIMTIONP;

/** 动画数据结构体,该结构包含所有动画的描述信息,所有动画都拥有一个此结构体描述
 * 
 * - AnimationID                动画id,根据该id查找其他信息。
 * - AnimationFrontName         动画前缀,根据该前缀获取所有与此动画相似的其他动画。
 * - AnimationResourceImageName 动画所在资源文件名
 * - AnimaitonResourcePlistName 动画描述文件名
 * - AnimaitonFrameNameVec      该动画所有的的帧的名字容器。
 * - AnimationFrameCount        该动画的帧的数量
 */

typedef struct TagSaveDataStruct
{
    string ValueString;
    string KeyString;
    
}STRUCT_SAVEDATA,*STRUCT_SAVEDATAP;


typedef union unKeyValueType
{
    int             ValueInteger;
    bool            ValueBool;
    float           ValueFloat;
    double          ValueDouble;
    const char      *ValueString;
    
}UNION_KEYVALUE_TYPE;

typedef struct TagKeyValueStruct
{
    int          ValueType;
    //int          ValueInteger;
    //bool         ValueBool;
    //float        ValueFloat;
    //double       ValueDouble;
    string       KeyString;
    const char * ValueString;
    UNION_KEYVALUE_TYPE unKeyValue;
    ;
    
}STRUCT_KEYVALUE,*STRUCT_KEYVALUEP;

/** 键值对结构体,用于存储所有经由ResourceManager对象获取的数据。
 *
 * - KeyString 关键字字符串,通过该串查找值
 * - ValueString 值字符串,获得后根据具体情况转换为其他类型。
 *
 */



typedef struct TagResourceLoadStruct
{
    string              CurrentMapName;
    string              CurrentMapPlistName;
    string              CurrentMonsterPlistName;
    string              CurrentBGM;
    string              CurrentNPCPlistName;
    string              CurrentMapImageName;
    
}STRUCT_RESLOAD,*STRUCT_RESLOADP;

/** 当前地图所需要的资源名称结构
 * 
 *
 */


enum enumOrientation
{
    Left = 1,
    LeftUp,
    Up,
    RightUp,
    Right,
    RightDown,
    Down,
    LeftDown
    
};


typedef union unMonsterDataType
{
    const char*  StringType;
    int    IntegerType;
    
}UNION_MONSTERDATA_TYPE;


typedef struct TagMonsterKeyValue
{
    string KeyString;
    UNION_MONSTERDATA_TYPE unValue;
    
}STRUCT_MONSTER_KEY_VALUE;


typedef struct TagMonsterDataKeyValueStrucnt
{
    
    string KeyString;
    string ValueString;
    
}STRUCT_MONSTER_DATA_KEYVALUE;


typedef struct TagMonsterDataStrcut
{
    string          MonsterName;
    /*
    vector<int>     MonsterItemVec;
    int             MonsterLevel;
    int             MonsterSpeedValue;
    int             MonsterAttackValue;
    int             MonsterMoney;
    int             MonsterItemNumber;
    int             MonsterDefenseValue;
    int             MonsterType;
    int             MonsterMoveHeight;
    int             MonsterMoveWidth;
    int             MonsterStandHeight;
    int             MonsterStandWidth;
    int             MonsterDieWidth;
    int             MonsterDieHeight;
    int             MonsterAttackAHeight;
    int             MonsterAttackAWidth;
    int             MonsterHitWidth;
    int             MonsterHitHeight;
    int             MonsterWidth;
    int             MonsterSkillNumber;
    int             MonsterExpValue;
    int             MonsterHPValue;
    */
    vector<STRUCT_MONSTER_KEY_VALUE> KeyValueVector;
    
    
    
}STRUCT_MONSTER_DATA,*STRUCT_MONSTER_DATAP;


typedef struct TagDamegeStruct
{
    int        DamegeIndex;
    int        DamegeNumber;
    int        DamegeType;
    bool       IsAddScene;
    bool       IsRunAction;
    bool       IsActionOver;
    bool       IsFadeOut;
    string     DamegeString;
    CCPoint    Position;
    CCLabelBMFont * DamegeLabel;
    
}STRUCT_DAMEGE,* STRUCT_DAMEGEP;


typedef struct TagBrickStruct
{
    int BrickPointX;
    int BrickPointY;
    int BrickIndex;
    
}STRUCT_BRICK,* STRUCT_BRICKP;

typedef struct TagMapStruct
{
    int                     MapHeight;
    int                     MapWidth;
    string                  MapName;
    string                  MapPngName;
    string                  MapPlistName;
    vector<STRUCT_BRICK>    MapBrickVec;
    
}STRUCT_MAP,* STRUCT_MAPP;



extern CGameScene * GetGameScene();

extern long long GetCurrentMSec();



#endif

dongli19921221 2016-02-21
  • 打赏
  • 举报
回复
代码错了问题 重新贴一下


//
//  CGameInstanceIOS.h
//  RPGMapstory
//
//  Created by Ling Dong on 13-8-27.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#ifndef RPGMapstory_CGameInstanceIOS_h
#define RPGMapstory_CGameInstanceIOS_h


static void fun();

#include "CGameInstanceAbstract.h"
#include "cocos2d.h"
using namespace cocos2d;
class CGameInstanceIOS:public CGameInstanceAbstract
{
protected:
    CGameInstanceIOS()
    {
        std::cout<<"CGameInstanceIOS is create"<<std::endl;
    }
    CGameInstanceIOS(const CGameInstanceIOS& rhs)
    {
    
    }
    const CGameInstanceIOS& operator=(const CGameInstanceIOS& rhs)
    {
    
    }
public:
    virtual ~CGameInstanceIOS()
    {
        std::cout<<"CGameInstance is destory"<<std::endl;
    }
    
    static CGameInstanceIOS * sharedGameInstanceIOS();
    
    virtual void InitGameInstance();
    
    virtual void InitGameSetting();
    
    virtual void CleareGameInstance();
    
    virtual void PauseGameInstance();
    
    virtual void ResumeGameInstance();
    
    virtual void StartRunGameInstance();
    
private:
    CCScene * m_pGameScene;
    
};

#endif

//
//  CGameInstance.cpp
//  RPGMapstory
//
//  Created by Ling Dong on 13-9-4.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include "CGameInstanceIOS.h"
#include "CGameDataStruct.h"
#include "cocos2d.h"
#include "CGameScene.h"
using namespace cocos2d;
static CGameInstanceIOS * theGameInstnace = 0;

CGameInstanceIOS * CGameInstanceIOS::sharedGameInstanceIOS()
{
    if(!theGameInstnace)
    {
        theGameInstnace = new CGameInstanceIOS();
        if (theGameInstnace)
        {
            theGameInstnace->InitGameInstance();
            return theGameInstnace;
        }
    }
    
    return theGameInstnace;
    
}


void CGameInstanceIOS::InitGameInstance()
{
    
}

void CGameInstanceIOS::InitGameSetting()
{
    CCDirector::sharedDirector()->setOpenGLView(CCEGLView::sharedOpenGLView());
     CCDirector::sharedDirector()->setDisplayStats(true);
    CCDirector::sharedDirector()->setAnimationInterval(GAME_INTERVAL);
   
}

void CGameInstanceIOS::CleareGameInstance()
{
    
}

void CGameInstanceIOS::StartRunGameInstance()
{
    m_pGameScene = CGameScene::GetScene();
    CCDirector::sharedDirector()->runWithScene(m_pGameScene);
}

void CGameInstanceIOS::PauseGameInstance()
{
    m_iGameStateValue = GAME_STATE_PAUSE;
}

void CGameInstanceIOS::ResumeGameInstance()
{
    m_iGameStateValue = GAME_STATE_RESUME;
}

然后是自己继承自CCLayer的CGameScene涌来封装游戏流程。

//
//  CGameScene.h
//  RPGMapstory
//
//  Created by Ling Dong on 13-9-4.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#ifndef RPGMapstory_CGameScene_h
#define RPGMapstory_CGameScene_h

#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class CGameScene:public CCLayer
{
public:
    CGameScene()
    {
        std::cout<<"CGameScene is create"<<std::endl;
    }
  
    virtual ~CGameScene()
    {
        std::cout<<"CGameScene is destory"<<std::endl;
    }
    
    static CCScene * GetScene();
    
    virtual bool init();
    
    CREATE_FUNC(CGameScene);
    
    void InitGameScene();
    /*
     *  init game
     */
    void RunGameWithFrame();
    /*
     *  游戏帧循环
     */
    void CleareGameScene();
    /*
     * 清除游戏资源
     */
    void OnGameLogoAnimation();
    /*
     * logo动画事件处理器
     */
    void OnGameTitle();
    /*
     * 标题画面处理器
     */
    void OnGameSelectScene();
    /*
     * 选择关卡画面处理器
     */
    void OnGameSceneLogic();
    /*
     * 场景逻辑处理器
     */
    void OnGameConfiguration();
    /*
     * 配置角色画面处理器
     */
    void OnGamePause();
    
    void OnGameResume();
    
    bool ccTouchBegan(CCTouch * pTouch, CCEvent * pEvent);
    
    void SetTouchPoint(const CCPoint& Point)
    {
        m_TouchPoint = Point;
    }
    CCPoint GetTouchPoint()const
    {
        return m_TouchPoint;
    }
    
    void Func()
    {
        int a;
    };
private:
    int             m_iGameRuningStateValue;
    bool            m_bFirstProtect;
    bool            m_bGameRuning;
    bool            m_bIsNeedLoadResource;
    bool            m_bHasSaveData;
    bool            m_bHasTouch;
    bool            m_bIsNewGame;
    bool            m_bTitleProtect;
    std::string     m_szCurrentMapName;
    CCPoint         m_TouchPoint;
};

#endif

dongli19921221 2016-02-21
  • 打赏
  • 举报
回复
CGameScene用来封装游戏流程

// 


//  CGameScene.cpp
//  RPGMapstory
//
//  Created by Ling Dong on 13-9-4.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#include <iostream>
#include <fstream>
#include "CGameScene.h"
#include "cocos2d.h"
#include "CGameDataStruct.h"
#include "CGameInstanceIOS.h"
#include "CGameDataManager.h"
#include "CGameSceneManager.h"
#include "CGameDamegeSystem.h"
#include "CGameResourceManager.h"
#include "CGameDataLibrary.h"
#include "CGameMonster.h"
#include "CGameAnimationManager.h"
#include "CGameSoldier.h"
#include "CGameTitle.h"
static CGameScene * pGameScene = NULL;
using namespace cocos2d;

CGameScene * GetGameScene()
{
    return pGameScene;
}



CCScene * CGameScene::GetScene()
{
    CCScene * pScene = CCScene::create();
    
    CGameScene * pGameScene = CGameScene::create();
    
    pScene->addChild(pGameScene);
  
    return pScene;
}

bool CGameScene::init()
{
    if(!CCLayer::init())
    {
        return false;
    }
    InitGameScene();
    return true;
}

void CGameScene::InitGameScene()
{
    schedule(schedule_selector(CGameScene::RunGameWithFrame));
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_LOGO);
    setTouchEnabled(true);
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
    pGameScene            = this;
    m_bIsNewGame          = true;
    m_bHasSaveData        = false;
    m_bFirstProtect       = false;
    m_bIsNeedLoadResource  = true;
}

void CGameScene::RunGameWithFrame()
{
    int iGameStateValue = CGameInstanceIOS::sharedGameInstanceIOS()->GetGameStateValue();
    
    switch(iGameStateValue)
    {
        case GAME_STATE_LOGO:
            OnGameLogoAnimation();
            break;
            
        case GAME_STATE_TITLE:
            OnGameTitle();
            break;
            
        case GAME_STATE_SELECT:
            OnGameSelectScene();
            break;
            
        case GAME_STATE_CONFIGURATION:
            OnGameConfiguration();
            break;
            
        case GAME_STATE_LOGIC:
            OnGameSceneLogic();
            break;
        case GAME_STATE_PAUSE:
            break;
            
        case GAME_STATE_RESUME:
            break;
            
        default:
            break;
    }
}

void CGameScene::OnGameTitle()
{
    /*
    if(m_bTitleProtect == false)
    {
        m_bTitleProtect = true;
        CGameTitle::sharedTitle();
        
    }
    CGameTitle::sharedTitle()->DrawGameObject();
    
    if(m_bHasTouch)
    {
        CGameTitle::sharedGameTitle()->EndTitileScreen();
        if(CGameSceneManager::sharedSceneManager()->ShowSwitchSceneScreen() == true)
        {
            CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_SELECT);
        }
        
    }
    */
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_SELECT);

}

void CGameScene::OnGameLogoAnimation()
{
   
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_TITLE);
}

void CGameScene::OnGameSceneLogic()
{

   CGameDataManager * pData = CGameDataManager::sharedDataManager();
   CGameResourceManager * pResManager = CGameResourceManager::sharedGameRerourceMnager();
    
    
   if(!m_bFirstProtect)
   {
       bool bIsNeedNewData = CGameDataManager::sharedDataManager()->GetIsNeedNewSaveData();
       PrintMessage("bIsNeedNewData not find");
       if(bIsNeedNewData)
       {
           CGameDataManager::sharedDataManager()->CreateNewSaveData();
       }
       else
       {
           CGameDataManager::sharedDataManager()->LoadGameData();
           
       }
              //pMonster->MoveMonsterByRange(ORIENTATION_LEFT, 40);
       m_bFirstProtect = true;
       
   }

   else
   {
       if(m_bIsNeedLoadResource)
       {
           m_bIsNeedLoadResource = false;
           //load resource
           
           //根据配置文件创建所有地图,怪物,角色,事件等等。
           CGameSceneManager * pSceneManager = CGameSceneManager::sharedSceneManager();
           
           string szCurrentScene = pData->GetCurrentSceneName();
           
           pResManager->LoadFirstData();
           
           pSceneManager->CreateNPCByIndex();
           pSceneManager->CreatePlayerControlBuIndex();
           pSceneManager->CreateMapByIndex();
           pSceneManager->CreateAudioByIndex();
           pSceneManager->CreateEventMachine();
           pSceneManager->CreateEnemyByIndex();
           pSceneManager->CreateNPCByIndex();
           pSceneManager->CreateUIByIndex(); 
           //pSceneManager->CreateSummonSystemByIndex();
           
           CGameDataManager * pData = CGameDataManager::sharedDataManager();
           UNION_KEYVALUE_TYPE a;
           a.ValueInteger = 12;
           pData->SaveDataByKeyValue(VALUE_TYPE_INT, "MonsterID", a);
           int b = pData->GetDataByValueKey(VALUE_TYPE_INT, "MonsterID").ValueInteger;
           cout<<b<<endl;
           
           
           CGameSoidler * ps = CGameSoidler::CreateSoidlerByID("Soidier_ChaZiBing");
           ps->SetGameObjectPosition(500, 500);
           CGameSceneManager::sharedSceneManager()->AddSoidierToList(ps);
           
                      
       }
       
       else
       {
           CGameSceneManager::sharedSceneManager()->DrawGameMonsterModule();
           CGameSceneManager::sharedSceneManager()->DrawGameMapModlue();
           CGameSceneManager::sharedSceneManager()->DrawGameUIModule();
           CGameSceneManager::sharedSceneManager()->DrawGameDropModule();
           CGameSceneManager::sharedSceneManager()->DrawGameNPCModule();
           CGameSceneManager::sharedSceneManager()->RunTouchGame();
           CGameDamegeSystem::sharedDamegeSystem()->DrawGameObject();
           CGameSceneManager::sharedSceneManager()->DrawSoidierModule();
           //CGameSceneManager::sharedSceneManager()->DrawSummonSystemMoudle();
       }
       
   }
    
    
}


bool WaitingSec(int iSec)
{
    static bool bCanNewTime = true;
    static long long iLastTime = 0;
    if(bCanNewTime)
    {
        bCanNewTime = false;
        iLastTime = GetCurrentMSec();
    }
    if(GetCurrentMSec() - iLastTime > iSec *1000)
    {
        bCanNewTime = true;
        return true;
    }
    return false;
}

bool CGameScene::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    GetGameScene()->SetTouchPoint( pTouch->getLocation());
    CCPoint Point;
    
    Point = pTouch->getLocation();
    cout<<endl;
    cout<<"point.x "<<Point.x<<endl;
    cout<<"Point.y "<<Point.y<<endl;
    cout<<endl;
    static int i = 1;
    
    CGameSoidler * pSoidier = CGameSceneManager::sharedSceneManager()->GetStackTopSoidier();
    
    if(pSoidier->GetIsInRect(Point))
    {
        
        if(i > 5)
        {
            i = 1;
        }
        pSoidier->SetSoidlerAnimationState(i++);
    }
    
    
    if(m_iGameRuningStateValue == GAME_STATE_TITLE)
    {
        m_bHasTouch = true;
    }
    CGameDamegeSystem::sharedDamegeSystem()->ShowDamegeNumber(500+i , Point, DAMEGE_TYPE_CRUSH);
    return false;
}
void CGameScene::OnGameConfiguration()
{
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_LOGIC);
}

void CGameScene::OnGameSelectScene()
{
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_CONFIGURATION);
}

void CGameScene::OnGamePause()
{
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_PAUSE);
    
}

void CGameScene::OnGameResume()
{
    CGameInstanceIOS::sharedGameInstanceIOS()->SetGameStateValue(GAME_STATE_RESUME);
}

void CGameScene::CleareGameScene()
{

}


721

社区成员

发帖
与我相关
我的任务
社区描述
Cocos2d-x相关内容讨论专区
社区管理员
  • Cocos2d-x
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧