⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outpost.cpp

📁 一个太空大战游戏的源程序及演示程序 VC++ 6.0
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    {
    // test if mine is alive
    if (mines[index].state == MINE_STATE_ALIVE)
        {
        // transform to screen coords
        mines[index].x = mines[index].varsI[INDEX_WORLD_X] - (mines[index].width >> 1) - player_x + (SCREEN_WIDTH/2);
        mines[index].y = mines[index].varsI[INDEX_WORLD_Y] - (mines[index].height >> 1) - player_y + (SCREEN_HEIGHT/2);

        // draw the station
        Draw_BOB(&mines[index],lpddsback);

		// animate the mine
        Animate_BOB(&mines[index]);
        
        } // end if

    } // end for index

} // end Draw_Mines

////////////////////////////////////////////////////////////

void Delete_Mines(void)
{
// this function simply deletes all memory and surfaces
// related to the mines

for (int index=0; index < MAX_MINES; index++)
    Destroy_BOB(&mines[index]);

} // end Delete_Mines

///////////////////////////////////////////////////////////

void Init_Gunships(void)
{
// this function loads and initializes the gunships to a known state
	
int frame;  // looping var

// create the first bob
Create_BOB(&gunships[0],0,0,160,70,32,
            BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
            DDSCAPS_SYSTEMMEMORY);

// load animation frames right
for (frame=0; frame <= 15; frame++)
	{
	// load the rocks imagery 
    Pad_Name("OUTART/GUNSHIPR", "BMP", buffer, frame);
	Load_Bitmap_File(&bitmap8bit, buffer);		

    // load the actual .BMP
    Load_Frame_BOB(&gunships[0],&bitmap8bit,frame,0,0,BITMAP_EXTRACT_MODE_ABS);  

    // unload data infile
    Unload_Bitmap_File(&bitmap8bit);

	} // end if

// load animation frames left
for (frame=0; frame <= 15; frame++)
	{
	// load the rocks imagery 
    Pad_Name("OUTART/GUNSHIPL", "BMP", buffer, frame);
	Load_Bitmap_File(&bitmap8bit, buffer);		

    // load the actual .BMP
    Load_Frame_BOB(&gunships[0],&bitmap8bit,frame+16,0,0,BITMAP_EXTRACT_MODE_ABS);  

    // unload data infile
    Unload_Bitmap_File(&bitmap8bit);

	} // end if

// set state to off
gunships[0].state                       = GUNSHIP_STATE_DEAD;
gunships[0].varsI[INDEX_GUNSHIP_DIR]    = GUNSHIP_RIGHT;
gunships[0].varsI[INDEX_GUNSHIP_TURRET] = 0;
gunships[0].varsI[INDEX_GUNSHIP_DAMAGE] = 0;

// make copies
for (int ship=1; ship < MAX_GUNSHIPS; ship++)
    {
    memcpy(&gunships[ship], &gunships[0], sizeof(BOB));
    } // end for gunship

} // end Init_Gunships

//////////////////////////////////////////////////////////////////////////////////////

void Reset_Gunships(void)
{
// this function resets all the gunships

for (int ship=0; ship < MAX_GUNSHIPS; ship++)
    {
    gunships[ship].state                       = GUNSHIP_STATE_DEAD;
    gunships[ship].varsI[INDEX_GUNSHIP_DIR]    = GUNSHIP_RIGHT;
    gunships[ship].varsI[INDEX_GUNSHIP_TURRET] = 0;
    gunships[ship].varsI[INDEX_GUNSHIP_DAMAGE] = 0;
    } // end for gunship

} // end Reset_Gunships

///////////////////////////////////////////////////////////

void Start_Gunship(int override=0, int x=0, int y=0, int dir=0)
{
// this functions starts a gunship, note that if override = 1
// then the function uses the sent data otherwise it's random

// first find an available gunship
for (int index=0; index < MAX_GUNSHIPS; index++)
	{
	// is this one dead
	if (gunships[index].state  == GUNSHIP_STATE_DEAD)
		{
		// position the gunship on a circle ring randomly from player

		int ang = rand()%16;
		float xpos = GUNSHIP_RANGE_RING*cos_look16[ang];
		float ypos = GUNSHIP_RANGE_RING*sin_look16[ang];

        // set position
		gunships[index].varsI[INDEX_WORLD_X] = player_x+xpos;
		gunships[index].varsI[INDEX_WORLD_Y] = player_y+ypos;

		// set direction
		if ((rand()%2) == 0)
			{
			// set direction to right
			gunships[index].varsI[INDEX_GUNSHIP_DIR] = GUNSHIP_RIGHT;

			// set velocity
			gunships[index].xv = 4+rand()%8;
			gunships[index].yv = RAND_RANGE(-4,4);

			} // end if right
		else 
			{
			// set direction to left
			gunships[index].varsI[INDEX_GUNSHIP_DIR] = GUNSHIP_LEFT;

			// set velocity
			gunships[index].xv = -4-rand()%8;
			gunships[index].yv = RAND_RANGE(-4,4);

			} // end left
		
		// set remaining state variables
		gunships[index].state  = GUNSHIP_STATE_ALIVE;
        gunships[index].varsI[INDEX_GUNSHIP_TURRET] = 0;
        gunships[index].varsI[INDEX_GUNSHIP_DAMAGE] = 0;

		// done so exit
		return;

		} // end if
	
	} // end for index

} // end Start_Gunship

////////////////////////////////////////////////////////////

void Move_Gunships(void)
{
// this function moves all the gunships and does the ai

// turrent center offsets for plasma pulses
static int turret_left[3][2]  = {-60,-19, -63,-9, -3,-24};
static int turret_right[3][2] = { 60,-19,  63,-9,  3,-24};

for (int index=0; index < MAX_GUNSHIPS; index++)
    {
    // test if plasma pulse is in flight
    if (gunships[index].state == GUNSHIP_STATE_ALIVE)
        {
        // move the gunship
        gunships[index].varsI[INDEX_WORLD_X]+=gunships[index].xv;
        gunships[index].varsI[INDEX_WORLD_Y]+=gunships[index].yv;
          
        // test for boundaries
        if (gunships[index].varsI[INDEX_WORLD_X] > UNIVERSE_MAX_X)
           {
  		   gunships[index].varsI[INDEX_WORLD_X] = UNIVERSE_MIN_X;
           } // end if
		else
		if (gunships[index].varsI[INDEX_WORLD_X] < UNIVERSE_MIN_X )
           {
  		   gunships[index].varsI[INDEX_WORLD_X] = UNIVERSE_MAX_X;
           } // end if	    

		if (gunships[index].varsI[INDEX_WORLD_Y] > UNIVERSE_MAX_Y)
           {
		   gunships[index].varsI[INDEX_WORLD_Y] = UNIVERSE_MIN_Y;
           } // end if
		else
		if (gunships[index].varsI[INDEX_WORLD_Y] < UNIVERSE_MIN_Y ) 
           {
		   gunships[index].varsI[INDEX_WORLD_Y] = UNIVERSE_MAX_Y;
           } // end if


       // leave particle trail
    if (gunships[index].varsI[INDEX_GUNSHIP_DAMAGE] > (MAX_GUNSHIP_DAMAGE >> 2) && 
       (rand()%3)==1)
        {
        Start_Particle(PARTICLE_TYPE_FLICKER, PARTICLE_COLOR_WHITE, 30+rand()%25, 
                      gunships[index].varsI[INDEX_WORLD_X]+RAND_RANGE(-4,4), 
                      gunships[index].varsI[INDEX_WORLD_Y]+RAND_RANGE(-4,4), 
                      (gunships[index].xv>>3), (gunships[index].yv>>3));
    
        Start_Particle(PARTICLE_TYPE_FADE, PARTICLE_COLOR_RED, 5, 
                      gunships[index].varsI[INDEX_WORLD_X]+RAND_RANGE(-4,4), 
                      gunships[index].varsI[INDEX_WORLD_Y]+RAND_RANGE(-4,4), 
                      (gunships[index].xv>>3), (gunships[index].yv>>3));


    
        } // end if

		// do ai
		
		// test if player is in range to care
		if (Fast_Distance_2D(player_x-gunships[index].varsI[INDEX_WORLD_X],
			                 player_y-gunships[index].varsI[INDEX_WORLD_Y]) < GUNSHIP_MIN_ATTACK_RANGE)
			{
            // move gunship toward player on x and y axis
                // track x-axis
                if (player_x > gunships[index].varsI[INDEX_WORLD_X])
                    gunships[index].varsI[INDEX_WORLD_X]++;
                else
                if (player_x < gunships[index].varsI[INDEX_WORLD_X])
                    gunships[index].varsI[INDEX_WORLD_X]--;
            
                // track y-axis
                if (player_y > gunships[index].varsI[INDEX_WORLD_Y])
                    gunships[index].varsI[INDEX_WORLD_Y]++;
                else
                if (player_y < gunships[index].varsI[INDEX_WORLD_Y])
                    gunships[index].varsI[INDEX_WORLD_Y]--;


            // turn turret always
            // compute d0, d1, d2
            
        

            // first create a vector point in the direction of the turret
            
            // compute current turrent vector
            int tdir1 = gunships[index].varsI[INDEX_GUNSHIP_TURRET];
            
            float d1x = gunships[index].varsI[INDEX_WORLD_X] + cos_look16[tdir1]*32;
            float d1y = gunships[index].varsI[INDEX_WORLD_Y] + sin_look16[tdir1]*32;

            // compute turret vector plus one
            int tdir2 = gunships[index].varsI[INDEX_GUNSHIP_TURRET]+1;
            
            if (tdir2 > 15) 
                tdir2 = 0;

            float d2x = gunships[index].varsI[INDEX_WORLD_X] + cos_look16[tdir2]*32;
            float d2y = gunships[index].varsI[INDEX_WORLD_Y] + sin_look16[tdir2]*32;
            
            // compute turret vector minus one
            int tdir0 = gunships[index].varsI[INDEX_GUNSHIP_TURRET]-1;
            
            if (tdir0 < 0) 
                tdir0=15;

            float d0x = gunships[index].varsI[INDEX_WORLD_X] + cos_look16[tdir0]*32;
            float d0y = gunships[index].varsI[INDEX_WORLD_Y] + sin_look16[tdir0]*32;

            // now find the min dist
            float dist0 = Fast_Distance_2D(player_x - d0x, player_y - d0y);
            float dist1 = Fast_Distance_2D(player_x - d1x, player_y - d1y);
            float dist2 = Fast_Distance_2D(player_x - d2x, player_y - d2y);

            if (dist0 < dist2 && dist0 < dist1)
                {
                // the negative direction is best        
                gunships[index].varsI[INDEX_GUNSHIP_TURRET] = tdir0;

                } // end if
            else
            if (dist2 < dist0 && dist2 < dist1)
                {
                // the positive direction is best
                gunships[index].varsI[INDEX_GUNSHIP_TURRET] = tdir2;

                } // end if

            // test for firing
            if ((rand()%25)==1)
                {   
                // which direction is ship going?
                if (gunships[index].varsI[INDEX_GUNSHIP_DIR] == GUNSHIP_LEFT)
                    {
                    // select turret to fire from
                    int turret = rand()%3;

                    // compute offsets firing position
                    int plasma_x = gunships[index].varsI[INDEX_WORLD_X] +  turret_left[turret][0];
                    int plasma_y = gunships[index].varsI[INDEX_WORLD_Y] +  turret_left[turret][1];

                    float plasma_xv = gunships[index].xv+cos_look16[gunships[index].varsI[INDEX_GUNSHIP_TURRET]]*PLASMA_SPEED_SLOW;
	                float plasma_yv = gunships[index].yv+sin_look16[gunships[index].varsI[INDEX_GUNSHIP_TURRET]]*PLASMA_SPEED_SLOW;

                    // fire weapon
                    Fire_Plasma(plasma_x, plasma_y, plasma_xv,plasma_yv,PLASMA_ANIM_ENEMY);

                    } // end if
                else
                    {
                    // select turret to fire from
                    int turret = rand()%3;

                    // compute offsets firing position
                    int plasma_x = gunships[index].varsI[INDEX_WORLD_X] +  turret_right[turret][0];
                    int plasma_y = gunships[index].varsI[INDEX_WORLD_Y] +  turret_right[turret][1];

                    float plasma_xv = gunships[index].xv+cos_look16[gunships[index].varsI[INDEX_GUNSHIP_TURRET]]*PLASMA_SPEED_SLOW;
	                float plasma_yv = gunships[index].yv+sin_look16[gunships[index].varsI[INDEX_GUNSHIP_TURRET]]*PLASMA_SPEED_SLOW;

                    // fire weapon
                    Fire_Plasma(plasma_x, plasma_y, plasma_xv,plasma_yv,PLASMA_ANIM_ENEMY);

                    } // end if

                 } // end if fire

			} // end if

		} // end if alive

	} // end for index

} // end Move_Gunships

///////////////////////////////////////////////////////////

void Draw_Gunships(void)
{
// this function moves all the gunships

for (int index=0; index < MAX_GUNSHIPS; index++)
    {
    // test if plasma pulse is in flight
    if (gunships[index].state == GUNSHIP_STATE_ALIVE)
        {
        // transform to screen coords
        gunships[index].x = gunships[index].varsI[INDEX_WORLD_X] - (gunships[index].width >> 1) - player_x + (SCREEN_WIDTH/2);
        gunships[index].y = gunships[index].varsI[INDEX_WORLD_Y] - (gunships[index].height >> 1) - player_y + (SCREEN_HEIGHT/2);

        // select the proper frame
		gunships[index].curr_frame = (gunships[index].varsI[INDEX_GUNSHIP_DIR] << 4) + 
			                         (gunships[index].varsI[INDEX_GUNSHIP_TURRET]);

        // draw the gunship
        Draw_BOB(&gunships[index],lpddsback);
         
        } // end if

    } // end for index

} // end Draw_Gunships

////////////////////////////////////////////////////////////

void Delete_Gunships(void)
{
// this function simply deletes all memory and surfaces
// related to the gunships

for (int index=0; index < MAX_GUNSHIPS; index++)
    Destroy_BOB(&gunships[index]);

} // end Delete_Gunships

/////////////////////////////////////////////////////

void Init_Powerups(void)
{
// this function loads and initializes the powerups to a known state
	
int index;  // looping var

// create the first bob
Create_BOB(&powerups[0],0,0,32,32,2,
            BOB_ATTR_VISIBLE | BOB_ATTR_MULTI_FRAME,
            DDSCAPS_SYSTEMMEMORY);

// load the powerups 
Pad_Name("OUTART/AMMOPU", "BMP", buffer, 0);
Load_Bitmap_File(&bitmap8bit, buffer);		

// load the actual .BMP
Load_Frame_BOB(&powerups[0],&bitmap8bit,0,0,0,BITMAP_EXTRACT_MODE_ABS);  

// unload data infile
Unload_Bitmap_File(&bitmap8bit);

Pad_Name("OUTART/SHLDPU", "BMP", buffer, 0);
Load_Bitmap_File(&bitmap8bit, buffer);		

// load the actual .BMP
Load_Frame_BOB(&powerups[0],&bitmap8bit,1,0,0,BITMAP_EXTRACT_MODE_ABS);  

// unload data infile
Unload_Bitmap_File(&bitmap8bit);

// set state to off
powerups[0].state                     = POWERUP_STATE_DEAD;
powerups[0].varsI[INDEX_POWERUP_TYPE] = POWERUP_TYPE_AMMO;

// make copies
for (index=1; index < MAX_POWERUPS; index++)
    {
    Clone_BOB(&powerups[0], &powerups[index]);
    } // end for index

} // end Init_Powerups

//////////////////////////////////////////////////////////////////////////////////////

void Reset_Powerups(void)
{
// this function resets all the powerups

for (int index=0; index < MAX_POWERUPS; index++)
    {
    powerups[index].state                      = POWERUP_STATE_DEAD;
    powerups[index].varsI[INDEX_POWERUP_TYPE]  = POWERUP_TYPE_AMMO;
    } // end for powerups

} // end Reset_Powerups

//////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -