青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

游戲中物件的定義與使用(8)

 

本篇是游戲中物件的定義與使用(7)的續篇。


Developing a Character ICS

Although developing a character’s inventory system might make you cringe at first,
let me reassure you that it’s not much different from developing a map inventory
control system. You have the ability to add and remove items, but you don’t have the
problem of dealing with the item coordinates on the map. Instead, a player’s ICS
keeps track of order, which means that players can rearrange the items as they see fit.


Of course, this ordering is just a matter of arranging the linked list, but what about
the items that can hold other items, such as backpacks? As long as you properly categorize
the items as containers in the MIL Editor, you don’t need to worry.


Speaking of categorizing, the real magic happens when you want to equip or use
an item. Because each item is categorized, the character ICS can quickly determine
what to do with the item in question. If the item is a weapon, the character ICS can
ask to equip the item. If it’s a healing item, the player can consume it. Beginning
to get the idea?

Finally, a character ICS should allow the player to examine objects, which is the reason
for the mesh and image parameters in the MIL Editor. Whenever the game’s
player examines the object, the specific mesh or image is loaded and displayed.
Now, turn your attention to putting together a character ICS and example by using
the ICS.

 

Defining the cCharICS Class

Interface:

//==================================================================================
// This structure contains char item information list.
//==================================================================================
typedef struct sCharItem
{
    
long    item_index;     // MIL item index
    long    quantity;       // quantity of item (ie coins)    

    sCharItem*   prev;
    sCharItem*   next;

    
long    index;           // map item index
    long    owner;

    sCharItem*   parent;     
// parent of a contained item
    
    sCharItem()
    {
        memset(
this, 0, sizeof(*this));
        owner = -1;
    }

    ~sCharItem()
    {
        delete next;   
    }
} *sCharItemPtr;

//==================================================================================
// This class encapsulate char inventory contrl system for character.
//==================================================================================
typedef class cCharIcs
{
private:
    
long         m_num_items;
    sCharItemPtr m_root_item;

private:
    
long    get_next_long(FILE* fp);
    
float   get_next_float(FILE* fp);

public:
    cCharIcs();
    ~cCharIcs();

    
bool load(const char* filename);
    
bool save(const char* filename);
    
void free();

    
void add(long item_index, long quantity, sCharItemPtr owner_item);
    
void remove(sCharItemPtr item);

    
long get_num_items();
    sCharItemPtr get_root_item();
    sCharItemPtr get_item(
long index);

    
void sort();
    
bool move_up(sCharItemPtr item);
    
bool move_down(sCharItemPtr item);
} *cCharIcsPtr;
 

Much like the cMapICS class, the cCharICS class uses a special structure (sCharItem) that
tracks the MIL item numbers and quantity and maintains a linked list. Unlike the
sMapItem structure, however, sCharItem doesn’t care about the item’s coordinates.

Implement:

//----------------------------------------------------------------------------
// Get numeric value from file.
//----------------------------------------------------------------------------
static void get_numeric_value(FILE* fp, char* buf, int size)
{
    
long pos;
    
int c;

    
// read until EOF or EOL or over buffer
    for(pos = 0; (c = fgetc(fp)) != EOF && c != 0x0a && pos != size-1; pos++)
    {
        
if((c >= '0' && c <= '9') || c == '.' || c == '-')
            buf[pos] = c;
    }

    buf[pos] = 0;
}

//----------------------------------------------------------------------------
// Constructor, initialize member data.
//----------------------------------------------------------------------------
cCharIcs::cCharIcs()
{
    memset(
this, 0, sizeof(*this));
}

//----------------------------------------------------------------------------
// Destructor, free allocated resource. 
//----------------------------------------------------------------------------
cCharIcs::~cCharIcs()
{
  free();
}

//----------------------------------------------------------------------------
// free allocate resource.
//----------------------------------------------------------------------------
void cCharIcs::free()
{
  m_num_items = 0;

  delete m_root_item;  
}

//----------------------------------------------------------------------------
// Load all char items from file.
//----------------------------------------------------------------------------
bool cCharIcs::load(const char* filename)
{
    free();     
// free a prior set

    FILE* fp;

    
if((fp = fopen(filename, "rb")) == NULL)
        
return false;    

    sCharItemPtr item;
    sCharItemPtr item_ptr = NULL;

    
// loop forever reading in items
    while(1)
    {
        
long item_index;

        
// get next item number (break if no more items, which is represented by a return value of -1).
        if((item_index = get_next_long(fp)) == -1)
            
break;

        
// create a new map item and link it in

        item = 
new sCharItem;

        
if(item_ptr == NULL)
            m_root_item = item;
        
else
        {
            item->prev = item_ptr;
            item_ptr->next = item;
        }

        item_ptr = item;

        item->item_index  = item_index;
        item->quantity    = get_next_long(fp);      
        item->owner       = get_next_long(fp);
        item->index       = m_num_items++;
    }

    fclose(fp);

    
// match objects that belong to others
    for(item_ptr = m_root_item; item_ptr != NULL; item_ptr = item_ptr->next)
    {
        
// check if this item belongs to another
        if(item_ptr->owner == -1)
            
continue;

        
// find matching parent item
        for(item = m_root_item; item != NULL; item = item->next)
        {
            
if(item_ptr->owner == item->index)
            {
                
// a match, point to parent and stop scanning for parents.
                item_ptr->parent = item;
                
break;
            }
        }
    }

    
return true;
}

//----------------------------------------------------------------------------
// Save all char items to file.
//----------------------------------------------------------------------------
bool cCharIcs::save(const char* filename)
{
    FILE* fp;

    
if((fp = fopen(filename, "wb")) == NULL)
        
return false;
     
    
long index = 0;

    
for(sCharItemPtr item = m_root_item; item != NULL; item = item->next)
    {
        item->index = index++;

        
// match child items to parents
        if(item->parent)
            item->owner = item->parent->index;
        
else
            item->owner = -1;

        fprintf(fp, "%lu\r\n%lu\r\n%ld\r\n", item->item_index, item->quantity, item->owner);
    }
    
    fclose(fp);

    
return true;
}

//----------------------------------------------------------------------------
// Add char item into list.
//----------------------------------------------------------------------------
void cCharIcs::add(long item_index, long quantity, sCharItemPtr owner_item)
{
    sCharItemPtr item = 
new sCharItem;

    
// fill the item structure
    item->item_index = item_index;
    item->quantity   = quantity;
    item->parent     = owner_item;

    
// insert into top of list

    item->next = m_root_item;

    
if(m_root_item)
        m_root_item->prev = item;

    m_root_item = item;
}

//----------------------------------------------------------------------------
// Remove char item from list.
//----------------------------------------------------------------------------
void cCharIcs::remove(sCharItemPtr item)
{    
    sCharItemPtr next_item;

    
// remove child objects first
    for(sCharItemPtr item_ptr = m_root_item; item_ptr != NULL; item_ptr = next_item)
    {
        next_item = item_ptr->next;

        
if(item_ptr->parent == item)
            remove(item_ptr);
    }

    
// remove from linked list and reset root if it's the current head of list.
    
    
if(item->prev)
        item->prev->next = item->next;
    
else
        m_root_item = item->next;

    
if(item->next)
        item->next->prev = item->prev;

    item->prev = item->next = NULL;
    delete item;
}

//----------------------------------------------------------------------------
// Return number of map items.
//----------------------------------------------------------------------------
long cCharIcs::get_num_items()
{
    
return m_num_items;
}

//----------------------------------------------------------------------------
// Return root map item.
//----------------------------------------------------------------------------
sCharItemPtr cCharIcs::get_root_item()
{
    
return m_root_item;
}

//----------------------------------------------------------------------------
// Return map item with specified index.
//----------------------------------------------------------------------------
sCharItemPtr cCharIcs::get_item(long index)
{
    sCharItemPtr item;

    
// loop until reached item index
    for(item = m_root_item; item != NULL && index != 0; item = item->next)
        index--;

    
return item;
}

//----------------------------------------------------------------------------
// Return long value from next line in file.
//----------------------------------------------------------------------------
long cCharIcs::get_next_long(FILE *fp)
{
    
char buf[1024];
    get_numeric_value(fp, buf, 
sizeof(buf));

    
if(strlen(buf) == 0)    // if there is no long value in file
        return -1;

    
return atol(buf);
}

//----------------------------------------------------------------------------
// Return float value from next line in file.
//----------------------------------------------------------------------------
float cCharIcs::get_next_float(FILE *fp)
{
    
char buf[1024];
    get_numeric_value(fp, buf, 
sizeof(buf));

    
return (float)atof(buf);
}

The cCharICS class, again, is much like its cMapICS counterpart, except for the addition
of three more public functions— sort, move_up, and move_down. You use these functions
to sort the character’s list of items. Their code is as follows:


//----------------------------------------------------------------------------
// Arrange char ics.
//----------------------------------------------------------------------------
void cCharIcs::sort()
{
    
// start at top of linked list and float each item up that has a lesser item_index,
    // break if past bottom of list.
    for(sCharItemPtr item = m_root_item; item != NULL; item = item->next)
    {
        
// keep floating up while previous item has a lesser item_index value or
        // until top of list has been rearched.
        for(sCharItemPtr prev_item = item->prev; prev_item != NULL; prev_item = item->prev)
        {
            
// break if no more to float up
            if(prev_item->item_index <= item->item_index)
                
break;

            
// swap element

            
if(prev_item->prev)
                prev_item->prev->next = item;

            
if((prev_item->next = item->next) != NULL)
                item->next->prev = prev_item;

            
if((item->prev = prev_item->prev) == NULL)
                m_root_item = item;
            
            prev_item->prev = item;
            item->next = prev_item;
        }
    }
}

//----------------------------------------------------------------------------
// Move one char item up.
//----------------------------------------------------------------------------
bool cCharIcs::move_up(sCharItemPtr item)
{
    sCharItemPtr prev_item = item->prev;

    
if(prev_item == NULL)
        
return FALSE;

    
// swap item and item before it

    
if(prev_item->prev)
        prev_item->prev->next = item;

    
if((prev_item->next = item->next) != NULL)
        item->next->prev = prev_item;

    
if((item->prev = prev_item->prev) == NULL)
        m_root_item = item;

    prev_item->prev = item;
    item->next = prev_item;

    
return TRUE;
}

//----------------------------------------------------------------------------
// Move one char item down.
//----------------------------------------------------------------------------
bool cCharIcs::move_down(sCharItemPtr item)
{
    sCharItemPtr next_item = item->next;

    
if(next_item == NULL)
        
return FALSE;

    
// swap item and item after it

    
if((item->next = next_item->next) != NULL)
        next_item->next->prev = item;

    
if((next_item->prev = item->prev) != NULL)
        item->prev->next = next_item;
    
else
        m_root_item = next_item;

    next_item->next = item;
    item->prev = next_item;

    
return TRUE;
}

Function 'sort' sorts the linked list of items based on each item’s MIL item number, from
lowest to highest. If, on the other hand, you want to specifically order the list yourself,
you can utilize the move_up and move_down functions, which take a pointer to a
sCharItem structure that is already contained in the list.

The move_up function moves the specified sItem structure up in the linked list, and
move_down moves the specified structure down in the linked list. Figure 15.10 illustrates
the concept of using the Arrange, move_up, and move_down functions on a sample
linked list of items.

The rest of the functions in cCharICS are identical in their functionality to the
cMapICS class, with the obvious exclusion of the item coordinates used when adding
an item to the list. Even the storage format for character items is identical to the
map item format, except for the coordinates.

 

Using the cCharICS Class

To demonstrate the use of the character ICS system, I created a demo application
named CharICS that maintains a list of items contained in the default.mil master
item list file. When you start the demo, you’ll see the
Inventory dialog box (shown in following snap). In the Inventory dialog box, the list
box contains an imaginary character’s inventory, which you can manipulate by
using the buttons on the dialog box.

To use the CharICS demo, follow these steps:


1. Click the Add Item button. The CharICS demo will add a random item from
the MIL to the inventory list.


2. Select an item from the list. Items are classified, so the Use and Equip buttons
are enabled or disabled depending on which item you select from the
list. Clicking Equip has no effect; it comes into play later when you deal with
characters. Clicking Use, however, removes an item if it is flagged as USEONCE.

3. Click the Drop button to drop an item (remove the item from the inventory
list) if it is flagged as CANDROP.


4. In order to arrange the items in the inventory, you can click an item and
then click either Sort Up or Sort Down. The selected item then moves up or
down in the list. To arrange the items by their item number in the list, click
Sort.


As a last, special treat, items that have matching meshes appear in the box on the
right in the demo. The 3-D object spins around slowly so that you have a full view
of all sides. Now, that’s cool!


posted on 2007-11-08 15:05 lovedday 閱讀(436) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美另类视频| 国产精品99免视看9| 激情伊人五月天久久综合| 久久成人这里只有精品| 午夜精品一区二区三区在线视 | 久久资源av| 一区免费观看视频| 欧美顶级少妇做爰| 欧美精品一区在线播放| 亚洲视频综合| 午夜精品美女久久久久av福利| 国产啪精品视频| 久久久久久亚洲精品中文字幕 | 亚洲欧美综合v| 亚洲欧洲精品成人久久奇米网| 欧美大片免费久久精品三p | 亚洲精品久久久一区二区三区| 亚洲国产国产亚洲一二三| 欧美极品在线观看| 亚洲一区二区三区777| 性欧美videos另类喷潮| 亚洲大片一区二区三区| 99视频日韩| 激情五月婷婷综合| 亚洲精品午夜| 国内成+人亚洲| 亚洲精品一区在线观看香蕉| 国产精品国产三级国产普通话99| 久久免费黄色| 欧美日韩在线一区| 欧美fxxxxxx另类| 欧美性事在线| 欧美成人久久| 国产日韩在线看片| 亚洲精品美女在线观看播放| 国产午夜精品福利| 亚洲伦理在线观看| 亚洲第一精品在线| 亚洲一区二区三区777| 亚洲国产精品一区二区第一页 | 亚洲制服av| 亚洲精品久久久久中文字幕欢迎你| 中文在线资源观看网站视频免费不卡 | 欧美成人精品一区二区| 国产精品免费一区二区三区观看| 欧美成人国产| 99精品国产一区二区青青牛奶| 国产区精品视频| 夜夜狂射影院欧美极品| 亚洲国产乱码最新视频| 欧美一区二区三区免费观看| 亚洲一二三区在线| 欧美精品在线免费播放| 亚洲电影天堂av| 在线电影国产精品| 小黄鸭视频精品导航| 国产精品99久久久久久www| 欧美成人乱码一区二区三区| 蜜臀a∨国产成人精品| 国产一区二区三区在线观看免费| 亚洲一区三区视频在线观看 | 狠狠久久亚洲欧美| 新67194成人永久网站| 午夜久久福利| 国产精品亚洲一区| 中日韩视频在线观看| 亚洲午夜未删减在线观看| 欧美人成在线| 亚洲三级视频| 99在线热播精品免费99热| 欧美国产日本| 99精品国产在热久久下载| 亚洲午夜一级| 国产精品一区久久久| 亚洲欧美精品在线| 久久久精品日韩欧美| 国模套图日韩精品一区二区| 久久精品国产综合精品| 欧美~级网站不卡| 久久aⅴ国产紧身牛仔裤| 欧美一区二区播放| 国产综合自拍| 狼人社综合社区| 亚洲人成77777在线观看网| 夜夜夜久久久| 国产精品jvid在线观看蜜臀| 亚洲综合精品一区二区| 久久人人爽人人爽爽久久| 在线精品视频一区二区三四| 免费久久99精品国产| 亚洲精品乱码久久久久久日本蜜臀| 夜久久久久久| 国产麻豆综合| 免费中文日韩| 一本色道久久加勒比88综合| 亚洲一区二区三区四区在线观看| 国产精品久久一级| 久久九九国产精品怡红院| 亚洲黄网站黄| 欧美一区亚洲一区| 亚洲国产精品久久久久秋霞蜜臀| 欧美日韩国产高清| 午夜在线精品偷拍| 国内精品久久久久久久97牛牛| 欧美wwwwww| 亚洲欧美日韩国产一区| 欧美电影免费观看大全| 亚洲一区在线免费| 亚洲高清免费在线| 国产毛片精品视频| 欧美人妖另类| 久久激情一区| 在线视频日韩| 亚洲国产精品久久久久| 欧美亚洲免费在线| 99精品欧美| 在线日韩av片| 国产人妖伪娘一区91| 欧美激情精品久久久久久久变态| 亚洲一区二区三区中文字幕| 亚洲国产成人tv| 久久久久国产精品一区| 亚洲一区二区在线看| 亚洲欧洲日韩女同| 国产日韩欧美视频在线| 欧美日韩国产限制| 麻豆av一区二区三区| 香蕉国产精品偷在线观看不卡| 亚洲肉体裸体xxxx137| 欧美mv日韩mv国产网站app| 亚久久调教视频| 亚洲午夜视频在线| 艳女tv在线观看国产一区| 亚洲国产视频直播| **性色生活片久久毛片| 韩国一区二区在线观看| 国产欧美一区二区三区在线老狼| 欧美日韩久久久久久| 免费一级欧美片在线播放| 久久激情久久| 欧美亚洲视频| 欧美亚洲视频在线看网址| 午夜精彩视频在线观看不卡 | 亚洲制服av| 一区二区日本视频| 日韩午夜免费视频| av成人激情| 99热这里只有成人精品国产| 日韩视频在线观看免费| 亚洲日本一区二区| 亚洲作爱视频| 一区二区三区毛片| 亚洲影院免费观看| 亚欧成人精品| 久久综合久久88| 欧美激情精品久久久久久免费印度| 免费久久99精品国产自在现线| 牛人盗摄一区二区三区视频| 猛干欧美女孩| 亚洲国产精品视频一区| 91久久精品网| 在线亚洲欧美专区二区| 亚洲免费视频观看| 久久国产精品久久w女人spa| 久久免费视频在线观看| 欧美a级一区二区| 欧美午夜不卡视频| 国产日韩欧美精品| 136国产福利精品导航网址应用| 亚洲激情在线播放| 亚洲一区二区在线免费观看| 久久久久9999亚洲精品| 欧美xart系列高清| 洋洋av久久久久久久一区| 午夜精品剧场| 欧美激情视频免费观看| 国产麻豆视频精品| 亚洲日本视频| 久久精品五月婷婷| 亚洲精品国久久99热| 亚洲免费在线观看| 欧美~级网站不卡| 国产精品一区二区三区四区| 日韩一区二区精品葵司在线| 亚洲一区三区视频在线观看 | 性欧美video另类hd性玩具| 狂野欧美一区| 国产精品区一区二区三| 亚洲国产精彩中文乱码av在线播放| 一区二区三区欧美视频| 久久夜色精品国产噜噜av| 一本大道av伊人久久综合| 久久精品国产久精国产思思| 欧美日韩国产精品| 狠狠干综合网| 午夜一级在线看亚洲| 亚洲国产一区二区视频| 亚洲欧美电影院| 欧美日韩国产综合在线| 在线观看亚洲视频啊啊啊啊|