• <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>

            天行健 君子當自強而不息

            Working with Maps and Levels(5)

            cTrigger::cTrigger and cTrigger::~cTrigger

            Just as does every C++ class, cTrigger has a constructor and a destructor that set up
            and free the data contained within the class. The only data tracked by the trigger
            class that is not contained with the linked list is the number of triggers currently
            held in the linked list and a pointer to that linked list. The constructor and
            destructor ensure that the class is prepared for using those two variables to free
            the class's data when destroyed (by calling the free function).

             

            cTrigger::load and cTrigger::save

            You typically design maps with a set of triggers all in their proper locations.
            Loading a list of those triggers is the main priority of the trigger class. Once a list
            of triggers is created or loaded, you also have the option to save that list of triggers
            (to save the game state, for example).

            The load function opens a text file and repeatedly reads in lines of text that define
            the type, identification, location, and special properties of each trigger (as described
            in the earlier section “Creating a Trigger Class”). When the end of file is reached,
            the load function returns. Take a look at the load function code to see just what I’m
            talking about:

            bool cTrigger::load(const char* filename)
            {
                free();

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

                
            // start looping, reading in until EOF reached.
                for(;;)
                {
                    
            long id = get_next_long_2(fp);
                    
            if(id == -1)
                        
            break;

                    
            long type    = get_next_long_2(fp);
                    
            bool enabled = get_next_long_2(fp) ? true : false;

                    
            float x1, y1, z1, x2, y2, z2, x3, z3, radius;

                    
            // read in rest depending on type
                    switch(type)
                    {
                    
            case TRIGGER_SPHERE:
                        x1     = get_next_float_2(fp);
                        y1     = get_next_float_2(fp);
                        z1     = get_next_float_2(fp);
                        radius = get_next_float_2(fp);

                        add_sphere(id, enabled, x1, y1, z1, radius);
                        
            break;

                    
            case TRIGGER_BOX:
                        x1 = get_next_float_2(fp);
                        y1 = get_next_float_2(fp);
                        z1 = get_next_float_2(fp);
                        x2 = get_next_float_2(fp);
                        y2 = get_next_float_2(fp);
                        z2 = get_next_float_2(fp);

                        add_box(id, enabled, x1, y1, z1, x2, y2, z2);
                        
            break;

                    
            case TRIGGER_CYLINDER:
                        x1     = get_next_float_2(fp);
                        y1     = get_next_float_2(fp);
                        z1     = get_next_float_2(fp);
                        radius = get_next_float_2(fp);
                        y2     = get_next_float_2(fp);

                        add_cylinder(id, enabled, x1, y1, z1, radius, y2);
                        
            break;

                    
            case TRIGGER_TRIANGLE:
                        x1 = get_next_float_2(fp);
                        z1 = get_next_float_2(fp);
                        x2 = get_next_float_2(fp);
                        z2 = get_next_float_2(fp);
                        x3 = get_next_float_2(fp);
                        z3 = get_next_float_2(fp);
                        y1 = get_next_float_2(fp);
                        y2 = get_next_float_2(fp);

                        add_triangle(id, enabled, x1, z1, x2, z2, x3, z3, y1, y2);
                        
            break;

                    
            default:    // some error occurred
                        fclose(fp);
                        free();
                        
            return false;
                    }
                }

                fclose(fp);
                
            return true;
            }

            At this point, the trigger data file is open and ready to begin reading in a list of
            trigger definitions. For each trigger, remember that the text line uses the following
            order: the trigger identification number, the type (0=sphere, 1=box, and so on), the
            default enabled status (0=trigger disabled, 1=enabled), and the specific data based on
            the type of trigger being read.

            Once past reading in the identification number, type, and enabled flag of each trigger,
            a single switch...case code block takes care of loading in each trigger type’s data.
            As each trigger’s data is read in, a separate function is called (based on the trigger’s
            type) to insert the trigger into the linked list. Those functions are add_sphere, add_box,
            add_cylinder, and add_triangle.

            Moving past the load function, you see the save function, which scans the linked list
            of triggers and saves each trigger’s data to a file, using the same format for each
            line of text that defines a trigger. Take a look:

            bool cTrigger::save(const char* filename)
            {
                
            if(m_num_triggers == 0)
                    
            return false;

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

                
            // write out all triggers
                for(sTrigger* trigger = m_root_trigger; trigger != NULL; trigger = trigger->next)
                {
                    
            int enabled = trigger->enabled ? 1 : 0;

                    
            switch(trigger->type)
                    {
                    
            case TRIGGER_SPHERE:
                        fprintf(fp, "%lu %lu %lu %lf %lf %lf %lf\r\n",
                                trigger->id, trigger->type, enabled,
                                trigger->x1, trigger->y1, trigger->z1,
                                trigger->radius);

                        
            break;

                    
            case TRIGGER_BOX:
                        fprintf(fp, "%lu %lu %lu %lf %lf %lf %lf %lf %lf\r\n",
                                trigger->id, trigger->type, enabled,
                                trigger->x1, trigger->y1, trigger->z1,
                                trigger->x2, trigger->y2, trigger->z2);

                        
            break;

                    
            case TRIGGER_CYLINDER:
                        fprintf(fp, "%lu %lu %lu %lf %lf %lf %lf %lf\r\n",
                                trigger->id, trigger->type, enabled,
                                trigger->x1, trigger->y1, trigger->z1,
                                trigger->radius, trigger->y2);

                        
            break;

                    
            case TRIGGER_TRIANGLE:
                        fprintf(fp, "%lu %lu %lu %lf %lf %lf %lf %lf %lf %lf %lf\r\n",
                                trigger->id, trigger->type, enabled,
                                trigger->x1, trigger->z1,
                                trigger->x2, trigger->z2,
                                trigger->x3, trigger->z3,
                                trigger->y1, trigger->y2);

                        
            break;
                    }
                }

                fclose(fp);
                
            return true;
            }

            posted on 2007-12-09 16:11 lovedday 閱讀(188) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久婷婷五月综合97色一本一本| 国产69精品久久久久APP下载| 国产精品久久午夜夜伦鲁鲁| 久久最近最新中文字幕大全 | 久久国产精品99久久久久久老狼| 伊人久久精品无码av一区| 国产精品美女久久久久av爽| 伊人久久大香线蕉综合网站| 久久不射电影网| 69SEX久久精品国产麻豆| 婷婷综合久久中文字幕蜜桃三电影 | 亚洲国产精品成人AV无码久久综合影院| 九九久久99综合一区二区| 亚洲成色WWW久久网站| 2021国产精品久久精品| 久久99热这里只有精品66| 久久久精品视频免费观看| 国内精品久久久久久久亚洲 | 久久精品国产精品亚洲| 99久久精品这里只有精品 | 久久久久九国产精品| 久久99精品久久久久久秒播| 亚洲精品无码久久久久去q| 亚洲va国产va天堂va久久| 久久偷看各类wc女厕嘘嘘| 久久精品无码专区免费东京热 | 狠色狠色狠狠色综合久久| 精品久久久久久久| 久久丝袜精品中文字幕| 久久国产精品无码HDAV| 亚洲va久久久噜噜噜久久天堂| 漂亮人妻被黑人久久精品| 久久精品无码一区二区无码 | 国产精品久久久久aaaa| 久久精品国产精品亚洲艾草网美妙| 久久久久亚洲AV成人网| 伊人久久大香线蕉无码麻豆| 亚洲国产精品狼友中文久久久 | 久久影院综合精品| 久久香蕉国产线看观看猫咪?v| 色老头网站久久网|