找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4060|回复: 0

如何设计游戏中的道具功能(三)

[复制链接]
发表于 2014-2-21 15:04:38 | 显示全部楼层 |阅读模式
对于道具的交换,我原则上是鼓励使用另一个容器去实现。
所有要交易的道具可以放在交易盒子中进行交换。
  1. #ifndef _ITEMDEALBOX_H
  2. #define _ITEMDEALBOX_H
  3. #include "Bag.h"
  4. //交易盒子
  5. //add by freeeyes
  6. #define MAX_DEALBOX_COUNT 10
  7. //交易盒子内容
  8. struct _ItemDealBox
  9. {
  10.         CBag*  m_pFromBag;         //来源Bag
  11.         CBag*  m_pToBag;           //目的Bag
  12.         CItem* m_pItem;            //道具信息
  13.         _ItemDealBox()
  14.         {
  15.                 m_pFromBag = NULL;
  16.                 m_pToBag   = NULL;
  17.                 m_pItem    = NULL;
  18.         }
  19. };
  20. class CItemDealBox : public CContainer<_ItemDealBox>
  21. {
  22. public:
  23.         CItemDealBox(uint16 u2Count = MAX_DEALBOX_COUNT);
  24.         virtual ~CItemDealBox();
  25.         /*
  26.         *    @Description: 添加进入交易盒子
  27.         */
  28.         bool AddDealBox(CBag* pFromBag, CBag* pToBag, CItem* pItem);
  29.         /*
  30.         *    @Description: 交换盒子
  31.         */
  32.         bool ExangeItem();
  33.         /*
  34.         *    @Description: 回滚操作
  35.         */
  36.         void RollBack();
  37.         /*
  38.         *    @Description: 序列化写入,此类暂不实现
  39.         */
  40.         bool ReadFromBuffer(const char* pFileName);
  41.         /*
  42.         *    @Description: 从容器序列化入数据流,需要从子类去实现,此类暂不实现
  43.         */
  44.         bool WriteToBuffer(const char* pFileName);
  45. };
  46. #endif
复制代码

双方通过AddDealBox这个函数,将要交易的数据放入盒子。
然后调用ExangeItem函数进行交换,或者调用RollBack函数回滚交易。

关于邮件,很多时候我们需要对邮件总量进行控制。
于是,我把邮件设置成了一个环
  1. #ifndef _MAIL_H
  2. #define _MAIL_H
  3. //邮件具体结构
  4. //add by freeeyes
  5. #include <time.h>
  6. #include "Object.h"
  7. #define MAX_USERMIAL_NAME    50
  8. #define MAX_USERMIAL_CONTANT 1000
  9. class CUserMail : public CBaseObject
  10. {
  11. public:
  12.         CUserMail();
  13.         ~CUserMail();
  14.         /*
  15.         *    @Description: 初始化一个邮件
  16.         */
  17.         void Init();
  18.         /*
  19.         *    @Description: 设置一个邮件内容
  20.         */
  21.         void SetMailInfo(const char* pFromName, const char* pToName, const char* pContant);
  22.         /*
  23.         *    @Description: 得到来源名
  24.         */
  25.         char* const GetFromName();
  26.         /*
  27.         *    @Description: 得到目标名
  28.         */
  29.         char* const GetToName();
  30.         /*
  31.         *    @Description: 得到内容
  32.         */
  33.         char* const GetContant();
  34.         /*
  35.         *    @Description: 得到时间
  36.         */
  37.         uint32 GetDate();
  38. //邮件存取相关方法
  39. public:
  40.         /*
  41.         *    @Description: 从数据流还原Item对象
  42.         */
  43.         bool ReadFromBuffer(char* pBuffer, int& nSize);
  44.         /*
  45.         *    @Description: 序列化入数据流
  46.         */
  47.         bool WriteToBuffer(char* pBuffer, int& nSize);
  48. private:
  49.         char   m_szFromUserName[MAX_USERMIAL_NAME];
  50.         char   m_szToUserName[MAX_USERMIAL_NAME];
  51.         char   m_szMailContant[MAX_USERMIAL_CONTANT];
  52.         uint32 m_u4Date;
  53. };
  54. #endif
复制代码
邮件继承自CBaseObject,并提供相关方法。顺便,把邮件列表做成一个环,用数据去管理提高效率。
  1. #ifndef _MAILLIST_H
  2. #define _MAILLIST_H
  3. //邮件列表
  4. //add by freeeyes
  5. #include "Mail.h"
  6. #define MAX_MIALLIST_COUNT 10
  7. #define MAILLIST_FILE_NAME "maillist.pmf"
  8. class CMailList
  9. {
  10. public:
  11.         CMailList(uint16 u2MailtCount = MAX_MIALLIST_COUNT);
  12.         ~CMailList();
  13.         /*
  14.         *    @Description: 添加一个邮件内容
  15.         */
  16.         void AddMailInfo(const char* pFromName, const char* pToName, const char* pContant);
  17.         /*
  18.         *    @Description: 回收邮件列表
  19.         */
  20.         void Close();
  21.         /*
  22.         *    @Description: 从数据流还原容器对象,需要从子类去实现
  23.         */
  24.         bool ReadFromBuffer(const char* pFileName);
  25.         /*
  26.         *    @Description: 从数据流还原容器对象,需要从子类去实现
  27.         */
  28.         bool WriteToBuffer(const char* pFileName);
  29.         /*
  30.         *    @Description: 得到当前邮件个数
  31.         */
  32.         uint16 GetMailCount();
  33.         /*
  34.         *    @Description: 得到当前最大个数
  35.         */
  36.         uint16 GetMaxCount();
  37.         /*
  38.         *    @Description: 得到倒序的邮件列表
  39.         */
  40.         CUserMail* GetMailBySort(uint16 u2Index);
  41.         /*
  42.         *    @Description: 得到当前的列表数据
  43.         */
  44.         CUserMail* GetMail(uint16 u2Index);
  45. private:
  46.         /*
  47.         *    @Description: 根据相对位置换算成数组绝对下标
  48.         */
  49.         uint16 GetListPos(uint16 u2Index);
  50. private:
  51.         CUserMail* m_pMailList;
  52.         uint16     m_u2MaxCount;
  53.         uint16     m_u2CurrIndex;
  54.         uint16     m_u2CurrCount;
  55.         bool       m_blTurnover;
  56. };
  57. #endif
复制代码
好了,整个代码差不多就是这样啦。
你可以下载本用例的全部代码。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?用户注册

×
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-4 13:10 , Processed in 0.030136 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表