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

📄 g18-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ============================================================================
//  G18-01.cpp
//  Intruder Game Demo
// ============================================================================
#include "SDLGUI.h"
#include "Graph.h"
#include "math.h"
#include <stdlib.h>
#include <time.h>



// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Intruder Game Demo";
const int WIDTH             = 800;
const int HEIGHT            = 600;
const int ITEMS             = 32;
const int ARIAL             = 0;
const float DEFENSEZONE     = 256.0f;
const float VISUALZONE      = 64.0f;
const float ATTACKZONE      = 16.0f;
const float GETZONE         = 8.0f;

const float PLAYERSPEED     = 64.0f;
const float AISPEED         = 48.0f;

// ============================================================================
//  Classes
// ============================================================================
enum AIType
{
    ATTACKER,
    DEFENDER
};

enum AIState
{
    GUARDING,
    ATTACKING,
    FINDINGHEALTH,
    FINDINGBASE
};

enum AIEvent
{
    SEEINTRUDER,
    KILLINTRUDER,
    FOUNDHEALTH,
    FOUNDBASE,
    OUTOFBOUNDS
};

enum HealthState
{
    GOODHEALTH,
    BADHEALTH
};

// find the distance between two points
float Distance( float x1, float y1, float x2, float y2 )
{
    float x = x1 - x2;
    float y = y1 - y2;
    return (float)sqrt( (x * x) + (y * y) );
}



class AI
{
public:
    AIState m_state;        // the state the AI is in
    AIType m_type;          // the type of AI it is
    float m_x, m_y;         // its coordinates
    int m_health;           // its health

    void Init( AIType p_type, float p_x, float p_y, int p_health )
    {
        m_type = p_type;
        m_x = p_x;
        m_y = p_y;
        m_health = p_health;
        m_state = GUARDING;
    }

};


// ============================================================================
//  Global Variables
// ============================================================================
SDLGUI* g_gui;

AIState g_defender[4][5][2];
AIState g_attacker[4][5][2];

// the AI's
AI g_AIs[8];
int g_numAIs = 0;

// player position
float g_x = 700.0f;
float g_y = 500.0f;

// is the player moving in the x or y directions?
int g_dx = 0;
int g_dy = 0;

// player health
int g_health = 75;


// location of the base.
float g_basex = 0;
float g_basey = 0;


// health packs, 8 packs, 2 coords each.
int g_healthPacks[8][2];


int g_timer;
int g_combattimer;
int g_timedelta;

// circles
SDL_Surface* g_dzone;
SDL_Surface* g_vzone;

// ============================================================================
//  Draw Algorithm
// ============================================================================
void Draw()
{
    int i;

    // draw the defense zone perimeter
    g_gui->Blit( g_dzone, g_basex - DEFENSEZONE, g_basey - DEFENSEZONE );

    // draw the AI's
    for( i = 0; i < g_numAIs; i++ )
    {
        if( g_AIs[i].m_type == DEFENDER )
        {
            g_gui->Box( (int)g_AIs[i].m_x, 
                        (int)g_AIs[i].m_y, 
                        8, 8, 
                        GREEN );

            // draw its health bar
            g_gui->Box( 544, i * 4 + 4, (g_AIs[i].m_health * 256) / 100, 2, GREEN );

            // draw its visual zone
            g_gui->Blit( g_vzone, g_AIs[i].m_x - VISUALZONE, 
                         g_AIs[i].m_y - VISUALZONE );
        }
        else
        {
            g_gui->Box( (int)g_AIs[i].m_x, 
                        (int)g_AIs[i].m_y, 
                        8, 8, 
                        RED );

            // draw its health bar
            g_gui->Box( 544, i * 4 + 4, (g_AIs[i].m_health * 256) / 100, 2, RED );

            // draw its visual zone
            g_gui->Blit( g_vzone, g_AIs[i].m_x - VISUALZONE, 
                         g_AIs[i].m_y - VISUALZONE );

        }
    }

    // draw the med packs
    for( i = 0; i < 8; i++ )
    {
        g_gui->Box( g_healthPacks[i][0], g_healthPacks[i][1], 8, 8, BLUE );
    }

    // draw the player
    g_gui->Box( (int)g_x, (int)g_y, 8, 8, BLACK );
}


// ============================================================================
//  Algorithms
// ============================================================================
void AddHealthPack( int p_index )
{
    g_healthPacks[p_index][0] = rand() % WIDTH - 4;
    g_healthPacks[p_index][1] = rand() % HEIGHT - 4;
}

// find out if a health pack is on the coordinates p_x, p_y
int MatchHealthPacks( float p_x, float p_y )
{
    int i;

    // loop through all health packs.
    for( i = 0; i < 8; i++ )
    {
        // if the coordinates are within "getting" distance, return the index
        // of the health pack
        if( Distance( p_x, p_y, g_healthPacks[i][0], g_healthPacks[i][1] ) <= GETZONE )
            return i;
    }

    // no health packs in range
    return -1;
}


// find the closest AI to the player
int FindClosestAI()
{
    float distance;
    float temp;
    int index;
    int i;

    // find the distance to the first AI
    distance = Distance( g_x, g_y, g_AIs[0].m_x, g_AIs[0].m_y );
    index = 0;

    // loop through the rest of the AI's
    for( i = 1; i < g_numAIs; i++ )
    {
        // find the distance
        temp = Distance( g_x, g_y, g_AIs[i].m_x, g_AIs[i].m_y );

        // if the distance is less than the current lowest distance, set the
        // new lowest distance and index.
        if( temp < distance )
        {
            distance = temp;
            index = i;
        }
    }
    return index;
}


// find the closest health pack to some coordinates
int FindClosestHealthPack( float p_x, float p_y )
{
    float distance;
    float temp;
    int index;
    int i;

    // find the distance to the first health pack
    distance = Distance( p_x, p_y, g_healthPacks[0][0], g_healthPacks[0][1] );
    index = 0;

    // loop through the rest of the healthpacks
    for( i = 1; i < 8; i++ )
    {
        // find the distance
        temp = Distance( p_x, p_y, g_healthPacks[i][0], g_healthPacks[i][1] );

        // if the distance is less than the current lowest distance, set the
        // new lowest distance and index.
        if( temp < distance )
        {
            distance = temp;
            index = i;
        }
    }
    return index;
}


void InitMachines()
{
    int state;
    int event;

    // fill the machines with default values first
    for( state = 0; state < 4; state++ )
    {
        for( event = 0; event < 5; event ++ )
        {
            // fill each cell so that every state points to itself.
            g_attacker[state][event][0] = (AIState)state;
            g_attacker[state][event][1] = (AIState)state;
            g_defender[state][event][0] = (AIState)state;
            g_defender[state][event][1] = (AIState)state;
        }
    }


    // now fill the attacker in with the real values
    g_attacker[GUARDING][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_attacker[GUARDING][SEEINTRUDER][BADHEALTH] = ATTACKING;

    g_attacker[ATTACKING][KILLINTRUDER][GOODHEALTH] = FINDINGBASE;
    g_attacker[ATTACKING][KILLINTRUDER][BADHEALTH] = FINDINGHEALTH;

    g_attacker[FINDINGHEALTH][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_attacker[FINDINGHEALTH][SEEINTRUDER][BADHEALTH] = ATTACKING;
    g_attacker[FINDINGHEALTH][FOUNDHEALTH][GOODHEALTH] = FINDINGBASE;
    g_attacker[FINDINGHEALTH][FOUNDHEALTH][BADHEALTH] = FINDINGBASE;

    g_attacker[FINDINGBASE][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_attacker[FINDINGBASE][SEEINTRUDER][BADHEALTH] = ATTACKING;
    g_attacker[FINDINGBASE][FOUNDBASE][GOODHEALTH] = GUARDING;
    g_attacker[FINDINGBASE][FOUNDBASE][BADHEALTH] = GUARDING;

    // fill in the defender machine
    g_defender[GUARDING][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_defender[GUARDING][SEEINTRUDER][BADHEALTH] = ATTACKING;

    g_defender[ATTACKING][KILLINTRUDER][GOODHEALTH] = FINDINGBASE;
    g_defender[ATTACKING][KILLINTRUDER][BADHEALTH] = FINDINGHEALTH;
    g_defender[ATTACKING][OUTOFBOUNDS][GOODHEALTH] = FINDINGBASE;
    g_defender[ATTACKING][OUTOFBOUNDS][BADHEALTH] = FINDINGBASE;

    g_defender[FINDINGHEALTH][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_defender[FINDINGHEALTH][SEEINTRUDER][BADHEALTH] = ATTACKING;
    g_defender[FINDINGHEALTH][FOUNDHEALTH][GOODHEALTH] = FINDINGBASE;
    g_defender[FINDINGHEALTH][FOUNDHEALTH][BADHEALTH] = FINDINGBASE;
    g_defender[FINDINGHEALTH][OUTOFBOUNDS][GOODHEALTH] = FINDINGBASE;
    g_defender[FINDINGHEALTH][OUTOFBOUNDS][BADHEALTH] = FINDINGBASE;

    g_defender[FINDINGBASE][SEEINTRUDER][GOODHEALTH] = ATTACKING;
    g_defender[FINDINGBASE][SEEINTRUDER][BADHEALTH] = ATTACKING;
    g_defender[FINDINGBASE][FOUNDBASE][GOODHEALTH] = GUARDING;
    g_defender[FINDINGBASE][FOUNDBASE][BADHEALTH] = GUARDING;
}


void Event( int p_AI, AIEvent p_event )
{
    // figure out the health state of the AI first
    HealthState health = BADHEALTH;
    if( g_AIs[p_AI].m_health > 50 )
    {
        health = GOODHEALTH;
    }


    // now determine if he's an attacker or defender.
    if( g_AIs[p_AI].m_type == ATTACKER )
    {
        g_AIs[p_AI].m_state = g_attacker[g_AIs[p_AI].m_state][p_event][health];
    }
    else

⌨️ 快捷键说明

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