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

天行健 君子當自強而不息

游戲中物件的定義與使用(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久久久久久久久久久久 | 国产日产高清欧美一区二区三区| 新67194成人永久网站| 亚洲欧美日韩一区二区| 国产一区二区三区免费不卡 | 久久野战av| 免费成人美女女| 中文av一区二区| 午夜在线一区二区| 亚洲激情中文1区| 一本色道**综合亚洲精品蜜桃冫| 国产精品嫩草影院一区二区| 久久蜜桃精品| 欧美—级a级欧美特级ar全黄| 亚洲天堂黄色| 久久精品国产成人| 一本综合久久| 久久久www成人免费无遮挡大片 | 亚洲精品一级| 亚洲天天影视| 亚洲激情自拍| 亚洲一区免费视频| 亚洲国产日韩综合一区| 中国日韩欧美久久久久久久久| 韩国在线一区| 亚洲天堂av电影| 亚洲人成高清| 午夜久久久久| 亚洲天堂成人在线视频| 久久久午夜精品| 欧美一区二区三区视频免费| 免费成人小视频| 久久综合中文字幕| 国产精品激情电影| 亚洲国产日韩欧美在线99| 国产精品v一区二区三区| 欧美成人三级在线| 国产乱码精品一区二区三区av| 亚洲国产毛片完整版| 国产亚洲精品高潮| 中日韩午夜理伦电影免费| 亚洲黄色在线观看| 久久精品动漫| 香蕉成人伊视频在线观看| 欧美精选在线| 亚洲大胆人体视频| 极品av少妇一区二区| 亚洲综合另类| 午夜欧美视频| 国产精品福利在线| 一本色道久久综合| 夜久久久久久| 欧美激情精品久久久久久| 免费不卡中文字幕视频| 狠狠噜噜久久| 欧美一区二区三区在线| 久久精品国产96久久久香蕉| 国产精品盗摄久久久| 日韩一区二区久久| 一本大道久久精品懂色aⅴ| 欧美成人高清| 亚洲国产成人高清精品| 亚洲国产精品尤物yw在线观看| 久久er精品视频| 久久综合五月| 亚洲激情网站免费观看| 美日韩在线观看| 亚洲国产欧美一区二区三区丁香婷| 亚洲国产精品www| 欧美成人四级电影| 亚洲精品免费一区二区三区| 99视频国产精品免费观看| 欧美激情国产日韩| 在线亚洲欧美| 久久久国产精品亚洲一区 | 欧美日韩亚洲高清| 亚洲丝袜av一区| 久久riav二区三区| 精品999成人| 欧美大片免费观看| 中日韩男男gay无套| 久久精品国产v日韩v亚洲| 激情久久综艺| 欧美激情视频网站| 亚洲无线视频| 噜噜噜91成人网| 亚洲毛片一区| 国产日韩欧美一区| 久久综合色一综合色88| 99视频精品全国免费| 欧美在线一二三四区| 亚洲电影免费在线观看| 欧美精品日韩www.p站| 亚洲免费视频在线观看| 久久亚洲电影| 亚洲视频在线视频| 国内精品视频在线播放| 欧美国产日韩一区二区| 亚洲在线观看视频网站| 欧美a级片网站| 亚洲女同在线| 亚洲人成亚洲人成在线观看| 国产裸体写真av一区二区| 久热精品视频在线| 亚洲一二三区在线| 亚洲欧洲一级| 久久一区二区精品| 亚洲视频1区| 91久久夜色精品国产九色| 国产精品免费视频观看| 美脚丝袜一区二区三区在线观看| 中文网丁香综合网| 亚洲成色777777女色窝| 久久九九国产精品| 亚洲私人影院| 99国产精品国产精品毛片| 国产亚洲一级高清| 国产精品va在线播放我和闺蜜| 久久夜色精品国产噜噜av| 亚洲欧美成人网| 亚洲久久一区二区| 欧美成人午夜免费视在线看片| 欧美在线观看视频| 亚洲综合清纯丝袜自拍| 999在线观看精品免费不卡网站| 国内精品伊人久久久久av影院| 国产精品午夜av在线| 欧美日韩国产页| 欧美国产欧美亚洲国产日韩mv天天看完整| 欧美伊人久久久久久久久影院 | 久久久精品久久久久| 亚洲欧美日韩国产精品| 一本色道久久88亚洲综合88| 亚洲精品在线视频观看| 亚洲激情视频网站| 亚洲国产精品久久久久秋霞影院 | 一区视频在线| 黄色成人片子| 在线观看av一区| 在线成人欧美| 亚洲国产成人午夜在线一区| 一区在线播放| 在线看片日韩| 最新成人av网站| 亚洲精品一区二区三区婷婷月| 亚洲激情午夜| 日韩午夜在线播放| 在线综合亚洲欧美在线视频| 一卡二卡3卡四卡高清精品视频| 日韩一本二本av| 亚洲视频一区二区| 亚洲在线视频一区| 欧美一区二区免费| 久久综合精品国产一区二区三区| 另类av导航| 亚洲国产精品小视频| 91久久精品一区| 一区二区高清视频| 亚洲欧美在线一区二区| 久久成人资源| 欧美不卡视频一区发布| 欧美日产国产成人免费图片| 国产精品播放| 狠狠色丁香久久婷婷综合丁香| 伊人狠狠色j香婷婷综合| 亚洲黑丝一区二区| 一本久久综合亚洲鲁鲁五月天| 欧美一区二区三区四区在线| 久久久久久久久久久一区 | 亚洲青色在线| 亚洲永久精品大片| 久久久久久久久伊人| 欧美激情影院| 国产日本欧洲亚洲| 日韩天堂av| 久久久久国产一区二区三区四区 | 亚洲黄色一区| 亚洲欧美国产精品桃花| 免费人成精品欧美精品| 国产精品激情| 亚洲人成人一区二区三区| 欧美一区二区黄色| 亚洲国产精品久久精品怡红院| 亚洲自拍另类| 欧美国产在线视频| 国产专区精品视频| 国产精品99久久不卡二区| 美女精品在线观看| 亚洲男女自偷自拍| 欧美精品www在线观看| 国产一区欧美| 午夜精品免费|