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

天行健 君子當自強而不息

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

 

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

 

cMapIcs::get_next_long,cMapIcs::get_next_float

//----------------------------------------------------------------------------
// Return long value from next line in file.
//----------------------------------------------------------------------------
long get_next_long(FILE* fp)
{
    
char buf[1024];
    
long pos = 0;
    
int c;

    
// Read until EOF or EOL
    while(1) 
    {
        
if((c = fgetc(fp)) == EOF || c == '\n' || pos == sizeof(buf)-1)
            
break;

        
if((c >= '0' && c <= '9') || c == '.' || c == '-')
            buf[pos++] = c;
    }

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

    buf[pos] = 0;

    
return atol(buf);
}

//----------------------------------------------------------------------------
// Return float value from next line in file.
//----------------------------------------------------------------------------
float get_next_float(FILE* fp)
{
    
char buf[1024];
    
long pos = 0;
    
int c;

    
// Read until EOF or EOL
    while(1) 
    {
        
if((c = fgetc(fp)) == EOF || c == '\n' || pos == sizeof(buf)-1)
            
break;

        
if((c >= '0' && c <= '9') || c == '.' || c == '-')
            buf[pos++] = c;
    }

    buf[pos] = 0;

    
return (float)atof(buf);
}
 

構造函數、析構函數

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

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

cMapIcs::load, cMapIcs::save, and cMapIcs::free

As their names suggest, this trio of functions loads, saves, and frees a list of items
that belong to a map. The first of the three, Load, loads and creates a list of items.
For simplicity, store all items in a text file, using the following format:


MIL_ItemNum
Quantity
XPos
YPos
ZPos
ParentID


Each item uses six lines of text, and each entry (group of six lines) is numbered
sequentially (the first item in the file is item #0, the second item is #1, and so on).
Here’s a sample file that contains two items:


// Item #0 as follows:
10 // MIL Item # (long value)
1 // Quantity (long value)
10.0 // XPos (float value)
0.0 // YPos (float value)
600.0 // ZPos (float value)
-1 // Owner (-1 = none, index # otherwise)
// Item #1 as follows:
1 // MIL Item #
1 // ...
10.0
0.0
600.0
0 // belongs to item #0 (first item in file)

The preceding comments are for clarification; the actual storage file does not use
them. When reading in a list of items such as the preceding ones, the Load function
converts the text into usable numbers. Using those numbers, it creates a sMapItem
structure for each item in the map to be loaded, constructing a linked list as the
items are loaded. After every item is read in, all objects that belong to another are
matched up (using the Parent pointer in the sMapItem structure).


There’s really nothing too difficult here, so jump right into the cMapIcs::load code:

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

    FILE* fp;

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

    
long item_index;

    sMapItemPtr item;
    sMapItemPtr item_ptr = NULL;

    
// loop forever reading in items
    while(1)
    {
        
// 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 sMapItem;

        
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->x_pos       = get_next_float(fp);
        item->y_pos       = get_next_float(fp);
        item->z_pos       = get_next_float(fp);        
        item->owner_index = 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_index == -1)
            
continue;

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

    
return true;
}

Save takes an internal list of items and, using the filename you specify, saves that list
to a file on disk. The Save function is typically used to update the game data, because
players might consistently pick up and drop items.

The Save function first assigns an index value to each sMapItem structure in the
linked list (based on their order). The first item in the linked list is 0 (zero), the
second item is 1, and so on. Each child item’s Owner variable is updated as well at
this point, and finally all data is written to a file:


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

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

    sMapItemPtr item;
    
long index = 0;

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

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

        fprintf(fp, "%lu\r\n%lu\r\n%lf\r\n%lf\r\n%lf\r\n%ld\r\n",
                item->item_index, item->quantity,
                item->x_pos, item->y_pos, item->z_pos,
                item->owner_index);
    }
    
    fclose(fp);

    
return true;
}

Finally, you use the Free function when destroying the class (thus, deleting the
linked list of items). Here’s the code for free:


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

  delete m_root_item;  
}

You’re just deleting the item linked list and getting the class ready for further use.

 

cMapIcs::Add and cMapIcs::Remove

As items are added to the map (as the result of a player dropping them, for example),
you need to call add to make sure those dropped items make it into the list of
map objects. The add function does this by first allocating a sMapItem structure, filling
it with the appropriate item information that you give it and then linking it into the
map’s list of items:

//----------------------------------------------------------------------------
// Add map item into list.
//----------------------------------------------------------------------------
void cMapIcs::add(long item_index, long quantity, 
                  
float x_pos, float y_pos, float z_pos, 
                  sMapItemPtr owner_item)
{
    sMapItemPtr item = 
new sMapItem;

    
// fill the item structure
    item->item_index = item_index;
    item->quantity   = quantity;
    item->x_pos      = x_pos;
    item->y_pos      = y_pos;
    item->z_pos      = z_pos;
    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;
}

Just as the Add function is used to add objects to the map’s list of items, you’ll need
to use Remove to remove items from a map. You call Remove using the item’s identifier
that you wish to remove from the map’s list. Remove also deletes the allocated item
structure and takes care of items that belong to the removed item:


 
//----------------------------------------------------------------------------
// Remove map item from list.
//----------------------------------------------------------------------------
void cMapIcs::remove(sMapItemPtr item)
{
    sMapItemPtr item_ptr;
    sMapItemPtr next_item;

    
// remove child objects first
    for(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;
}

 

cMapIcs::get_num_items,cMapIcs::get_parent_item,and cMapIcs::get_item

You use these three functions to retrieve the number of items that belong to the map and to retrieve the parent
sMapItem or specified item structure in the linked list. The first two of the following three functions return a single variable while the third function does the hard work by scanning through the linked list of objects, returning the specified item in the list:

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

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

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

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

    
return item;
}

 

cMapIcs類的使用

Every map in your game will have an associated list of items that belongs to it. The
map ICS will load those items and provide them to your engine whenever it needs
to render the map or add a specific item to a player’s inventory (when an item
contained in the map is picked up).


Take a look at the following code bit, which loads a sample list of items, adds a
single item, removes another item, and saves the list of items:

cMapIcs MapICS;

MapICS.load(“sample.mi”); // Load the file

// Add 1 of item # 10
MapICS.add(10, 1, 0.0f, 0.0f, 100.0f, NULL);

// Remove 2nd item from list
MapICS.remove(MapICS.GetItem(1));

// Save list back out
MapICS.save(“sample.mi”);

Although this is a simple example of modifying a map’s item list, why not go ahead
and see just how complicated it can become.


The MapICS demo (see following snap) contains the full cMapICS class and the sItem structure from the MIL edit program.  You use the map ICS and MIL to render a list of objects spread around a simple level.

The MapICS loads the map items and uses their coordinate data to draw
a generic item mesh in the scene. Whenever you approach an item, a
targeting icon appears and displays the name of the item.

NOTE
The MapICS demo allows you to walk around a simple level by using the
arrow keys and mouse.You can pick up items by standing in front of one and
pressing the left mouse button. Pressing the right mouse button
causes you to drop a random item.

Once the level is rendered out, each item in the map is scanned and drawn if in
view. The closest item is detected, and its name is printed to the screen. The code
is well commented, so you should have no problem breezing through it.

 

posted on 2007-11-07 19:28 lovedday 閱讀(307) 評論(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>
            欧美日韩国产精品一区| 欧美成人三级在线| 国产一区二区三区精品久久久| 欧美日韩视频在线一区二区观看视频 | 亚洲视频日本| 亚洲午夜久久久| 久久精品一区二区三区四区| 久久久久久久综合| 亚洲国产欧美在线人成| 夜夜嗨av一区二区三区| 亚洲福利免费| 亚洲影视综合| 农村妇女精品| 亚洲一区精品在线| 欧美成人69| 国产欧美亚洲精品| 亚洲黄色小视频| 久久国产加勒比精品无码| 欧美激情91| 久久久噜噜噜久久狠狠50岁| 欧美日韩一区二区免费在线观看| 国产亚洲一本大道中文在线| 亚洲一区二区三区涩| 亚洲电影有码| 久久精品观看| 国产日韩亚洲欧美综合| 亚洲在线黄色| 亚洲精品在线观看视频| 欧美高清在线视频| 亚洲国产成人久久| 亚洲激情视频网站| 免费91麻豆精品国产自产在线观看| 国产日韩在线亚洲字幕中文| 亚洲欧美国产三级| 欧美亚洲视频在线观看| 国产精品高清免费在线观看| 亚洲欧美视频一区| 欧美亚洲一区二区在线观看| 国产欧美日韩精品在线| 欧美专区在线观看| 亚洲大片在线观看| 一区二区日本视频| 国产精品自拍在线| 亚洲国产精品嫩草影院| 欧美国产日韩一区二区| 美女尤物久久精品| 亚洲美女免费视频| 久久国产精品久久w女人spa| 亚洲大片在线观看| 欧美亚洲一级片| 亚洲精品午夜| 亚洲欧美日韩国产综合| 最新国产成人av网站网址麻豆| 亚洲最新合集| 亚洲激情视频网站| 久久亚洲视频| 久久久精品动漫| 欧美特黄一级大片| 亚洲日本一区二区| 亚洲人午夜精品| 久久看片网站| 久久国产综合精品| 国产精品久久久免费| 日韩网站在线观看| 日韩视频在线永久播放| 久久在线播放| 亚洲国产欧美日韩| 一本色道久久综合一区| 欧美高清视频一区二区| 蜜臀av性久久久久蜜臀aⅴ四虎| 国产亚洲a∨片在线观看| 亚洲欧美第一页| 蜜臀99久久精品久久久久久软件| 激情综合五月天| 久久久久久9999| 亚洲国产欧美日韩另类综合| 亚洲国产成人精品女人久久久 | 亚洲国产第一| 亚洲天堂成人在线视频| 午夜免费久久久久| 一区二区三区在线视频播放 | 亚洲三级电影在线观看| 午夜精品一区二区三区电影天堂 | 亚洲一区二区视频在线| 欧美亚洲一区二区在线观看| 狠狠狠色丁香婷婷综合久久五月 | 日韩视频免费观看高清在线视频 | 亚洲人在线视频| 欧美一区二区成人| 亚洲黄色三级| 国内精品免费在线观看| 欧美精品久久久久久久| 亚洲伊人伊色伊影伊综合网 | 亚洲第一免费播放区| 欧美日韩视频在线一区二区观看视频| 亚洲欧美中文字幕| 一本色道久久综合亚洲精品不卡| 久久精品一区二区三区不卡| 一区二区三区波多野结衣在线观看| 国产网站欧美日韩免费精品在线观看| 久久精品国产免费观看| 这里是久久伊人| 亚洲精品在线免费| 亚洲精品日韩在线观看| 美女999久久久精品视频| 久久精品日产第一区二区| 亚洲性图久久| 黄色成人av| 国产日韩欧美一区二区| 国产精品尤物福利片在线观看| 欧美波霸影院| 欧美日韩一区二| 国产精品theporn| 国产精品久久久久久久久久久久久| 欧美日韩1234| 国产精品一二| 在线观看日韩av电影| 亚洲二区在线观看| 一区二区高清在线观看| 亚洲天堂网在线观看| 欧美亚洲日本一区| 久热精品视频在线观看| 亚洲欧洲精品天堂一级| 亚洲午夜精品国产| 久久综合国产精品台湾中文娱乐网| 久久福利一区| 欧美三级电影大全| 黑人巨大精品欧美黑白配亚洲| 亚洲片在线资源| 在线亚洲国产精品网站| 欧美一区视频在线| 中文在线资源观看网站视频免费不卡 | 一本色道久久综合狠狠躁篇怎么玩| 亚洲综合成人在线| 欧美呦呦网站| 亚洲精品社区| 欧美高清在线视频| 国产一区二区丝袜高跟鞋图片| 亚洲精品美女91| 蜜桃精品一区二区三区| 亚洲深夜激情| 欧美精品色综合| 亚洲经典视频在线观看| 欧美在线一级视频| 国产精品久久福利| 亚洲视频综合| 一区二区电影免费观看| 欧美性猛交一区二区三区精品| 在线视频日韩| 亚洲综合社区| 韩国女主播一区二区三区| 欧美激情在线免费观看| 亚洲欧美在线aaa| 亚洲国产精品第一区二区| 亚洲国产精品成人综合| 国产日韩精品一区二区| 老鸭窝毛片一区二区三区| 欧美国产日本高清在线| 久久精品夜色噜噜亚洲a∨ | 欧美在线精品免播放器视频| 久久久久久穴| 一区二区三区欧美在线观看| 久久久久国产免费免费| 一本一本久久a久久精品综合妖精| 一区二区三区视频在线看| 精品动漫3d一区二区三区免费版| 亚洲精品美女| 亚洲美女视频| 久久米奇亚洲| 欧美在线看片| 欧美深夜福利| 亚洲肉体裸体xxxx137| 一本在线高清不卡dvd| 欧美日本在线视频| 91久久夜色精品国产九色| 国产精品www色诱视频| 亚洲欧洲精品一区二区三区不卡| 免费毛片一区二区三区久久久| 在线观看三级视频欧美| 欧美精品福利在线| 久久日韩精品| 亚洲一区二区三区高清| 国产在线播放一区二区三区| 久久xxxx精品视频| 美脚丝袜一区二区三区在线观看| 亚洲国产成人av| 麻豆久久精品| 在线观看欧美| 欧美另类亚洲| 久久成人综合视频| 欧美国产一区二区在线观看| 亚洲国产岛国毛片在线| 欧美精品在线一区| 亚洲一区三区电影在线观看| 久久精品国产亚洲一区二区| 尤物yw午夜国产精品视频| 老司机aⅴ在线精品导航| 亚洲综合欧美日韩| 亚洲高清网站| 久久婷婷蜜乳一本欲蜜臀|