Putting Together a Full Game(11)
game_frame:
#define CLIENT_HEIGHT 480
#define MENU_BACK 1
#define MENU_LOAD 2
#define MENU_SAVE 4
#define NEW_GAME 0
#define RETURN_TO_GAME 1
#define LOAD_GAME 2
#define SAVE_GAME 3
#define QUIT_GAME 4
#define MAIN_MENU_TOP 126
#define MAIN_MENU_HEIGHT 64
#define BARTER_TOP_HEIGHT 128
#define BARTER_MENU_HEIGHT 32
#define STATUS_TOP_HEIGHT 128
#define STATUS_MENU_HEIGHT 32
const char * g_char_mesh_files[] = {
"..\\Data\\Warrior1.x",
"..\\Data\\Warrior2.x",
"..\\Data\\Yodan1.x",
"..\\Data\\Yodan2.x",
"..\\Data\\Yodan3.x",
"..\\Data\\Yodan4.x"
};
const sCharAnimInfo g_char_anims[] = {
{ "Idle", true },
{ "Walk", true },
{ "Swing", false },
{ "Spell", false },
{ "Swing", false },
{ "Hurt", false },
{ "Die", false },
{ "Idle", true }
};
const char * g_spell_mesh_files[] = {
"..\\Data\\Fireball.x",
"..\\Data\\Explosion.x",
"..\\Data\\Ice.x",
"..\\Data\\Heal.x",
"..\\Data\\Teleport.x",
"..\\Data\\Groundball.x",
"..\\Data\\Bomb.x",
"..\\Data\\Force.x"
};
const char * g_sound_files[] = {
"..\\Data\\Attack1.wav",
"..\\Data\\Attack2.wav",
"..\\Data\\Fire.wav",
"..\\Data\\Ice.wav",
"..\\Data\\Heal.wav",
"..\\Data\\Teleport.wav",
"..\\Data\\Groundball.wav",
"..\\Data\\Concussion.wav",
"..\\Data\\Evil Force.wav",
"..\\Data\\Roar.wav",
"..\\Data\\Hurt1.wav",
"..\\Data\\Hurt2.wav",
"..\\Data\\Die1.wav",
"..\\Data\\Die2.wav",
"..\\Data\\Beep.wav"
};
const char * g_music_files[] = {
"..\\Data\\Cathedral_Sunrise.mid",
"..\\Data\\Distant_tribe.mid",
"..\\Data\\Escape.mid",
"..\\Data\\Jungle1.mid",
"..\\Data\\Magic_Harp.mid",
"..\\Data\\Medi_Strings.mid",
"..\\Data\\Medi_techno.mid",
"..\\Data\\Song_of_the_sea.mid",
"..\\Data\\Storm.mid"
};
#define MAX_PLAYER_LEVEL 10
#define LEARN_SPELL_TOP_LEVEL 7
// max level 10, begin from level 2.
const long g_level_up_exp[] = { 100, 200, 350, 600, 900, 1300, 1800, 2400, 3100};
typedef struct
{
float x, y, z;
float u, v;
} sMenuVertex;
#define MENU_FVF (D3DFVF_XYZ | D3DFVF_TEX1)
long g_cur_music = -1;
long g_menu_options;
sCharacter* g_player;
char g_barter_ics_file[MAX_PATH];
const char * g_title_name = "The road of warrior";
{
if (purpose != FRAME_PURPOSE) // only process frame stats
return ;
cApp* app = (cApp*) data;
// quit to menu screen if ESC pressed
if (app->m_keyboard.get_key_state(KEY_ESC))
{
g_menu_options = MENU_BACK | MENU_LOAD | MENU_SAVE;
app->m_state_manager.push(menu_frame, app);
return ;
}
// If teleporting, then handle that first and return.
if (app->m_teleport_map != -1)
{
app->load_level(app->m_teleport_map);
app->m_teleport_map = -1;
return ; // no more processing this frame
}
bool is_monster_in_level = false ; // mark no monsters in level
// See if any character are in level.
//
// If any monsters, flag as such and change their AI to wander if their charge is less then 70,
// follow AI otherwise.
//
// Also, process whenever a character reaches a route point.
for (sCharacter* character = app->m_game_chars.get_root_char(); character != NULL; character = character->next)
{
if (character->type == CHAR_MONSTER)
{
is_monster_in_level = true ;
// change AI based on charge
if (character->charge >= 70.0f)
{
character->ai = CHAR_FOLLOW;
character->target_char = g_player;
character->distance = 0.0f;
}
else
character->ai = CHAR_WANDER;
}
else if (character->type == CHAR_NPC && character->ai == CHAR_ROUTE)
{
// check if an NPC character has reached last route point
if (app->last_point_reached(character))
{
// process the route point script for character.
char filename[MAX_PATH];
sprintf(filename, "..\\Data\\EOR%lu.mls", character->id);
app->m_game_script.execute(filename);
return ; // do not process any more this frame
}
}
}
// handle start of combat stuff
if (is_monster_in_level && !app->m_is_monster_last_frame)
app->start_of_combat();
// handle end of combat stuff if combat over
if (!is_monster_in_level && app->m_is_monster_last_frame)
app->end_of_combat();
// remember if monsters where in this frame and reset player's charge to full if no monsters
app->m_is_monster_last_frame = is_monster_in_level;
if (! is_monster_in_level)
g_player->charge = 100.0f;
app->m_game_chars.update(33);
app->m_game_spells.update(33);
long trigger_index = app->m_trigger.get_trigger(g_player->pos_x, g_player->pos_y, g_player->pos_z);
// check for triggers and execute script
if (trigger_index != 0)
{
char filename[MAX_PATH];
sprintf(filename, "..\\Data\\Trig%lu.mls", trigger_index);
app->m_game_script.execute(filename);
return ; // do not process any more this frame
}
set_display_camera(&app->m_camera);
// render everything
clear_display_zbuffer(1.0f);
begin_display_scene();
app->render_frame(33);
// render the player's charge bar, but only during combat.
if (is_monster_in_level)
{
D3DXMATRIX mat_world, mat_view, mat_proj;
D3DVIEWPORT9 viewport;
// get the world, projection, view transformations, viewport.
D3DXMatrixIdentity(&mat_world);
get_display_view_matrix(&mat_view);
get_display_proj_matrix(&mat_proj);
get_display_viewport(&viewport);
// offset charge bar by character's height
float max_y;
g_player-> object .get_bounds(NULL, NULL, NULL, NULL, &max_y, NULL, NULL);
// project coordinates to screen
D3DXVECTOR3 pos_screen;
D3DXVECTOR3 pos_3d(g_player->pos_x, g_player->pos_y + max_y, g_player->pos_z);
D3DXVec3Project(&pos_screen, &pos_3d, &viewport, &mat_proj, &mat_view, &mat_world);
pos_screen.x += 8.0f;
// display charge bar below player
disable_zbuffer();
begin_display_sprite();
RECT rect;
calculate_texture_rect(app->m_charge_bar, 0, 0, 16, 4, &rect);
draw_texture(g_d3d_sprite, app->m_charge_bar, &rect, pos_screen.x, pos_screen.y, 1.0f, 1.0f, COLOR_WHITE);
long width = g_player->charge / 100.0f * 16.0f;
calculate_texture_rect(app->m_charge_bar, 0, 4, width, 4, &rect);
draw_texture(g_d3d_sprite, app->m_charge_bar, &rect, pos_screen.x, pos_screen.y, 1.0f, 1.0f, COLOR_WHITE);
end_display_sprite();
}
// draw the player's stats at top-left
char stats_text[256];
sprintf(stats_text, "%ld/%ld HP\r\n%ld/%ld MP",
g_player->health_points, g_player->char_def.health_points,
g_player->mana_points, g_player->char_def.mana_points);
app->m_text_stats.render(stats_text, COLOR_WHITE);
end_display_scene();
present_display();
}
Because this is a frame state, you can call the game_frame function for one of three
purposes—the state being initialized, the frame being processed, and the state
being shut down. The game_frame function uses only the update-frame purpose, so
processing is returned if any other calling purpose is used.
At the beginning of the game_frame function is a quick check to see whether the Esc
key has been pressed. If so, the main menu state is pushed onto the stack.
In order for the main menu to know which options to display, you declare a global
variable at the beginning of the application. This global variable, g_menu_options, is
bit-encoded and uses the following macros to define them—MENU_BACK to display the
back to game option, MENU_SAVE to display the save game option, and MENU_LOAD to display
the load game option. Once you define the options, the state is pushed.
Whenever the player needs to move from one map to another (such as calling the
teleport_player function with the map number and coordinates to which to move the
player), you set a global variable called m_teleport_map to the correct map number for
teleporting. The preceding bit of code checks each frame to see whether that vari-
able has been set and teleports the player to the appropriate map.
Now comes the real bulk of the game_frame function. At the start of the following
block of code, you set a flag that records whether any monsters are in the map is
cleared. From there, scan the entire list of loaded characters. If you find a monster
character in the list, set the is_monster_in_level flag. Also, for each monster in the map,
change its AI settings based on its action charge. For charges less than 70, set the
monster’s AI to wander (to let the monster wander around the map). If the charge
is over 70, set the monster’s AI type to follow the player (so that the monster is
attempting to attack the player).
If, on the other hand, an NPC is found on the map and that character has its AI set
to follow a route, a separate function is called to determine whether that character
has reached the last route point assigned. If the last point on the route has been
touched, that character’s end-of-route script is executed.
Once you are past the scan-for-characters phase of game_frame, you compare the
is_monster_in_level flag to the same flag that was stored from the last frame. If they do
not match, either combat has started or ended, and the appropriate script is called.
Next comes the point at which all characters and spells are updated. Because the
cApp::frame function is locked to update the game 30 times a second, all controllers
use an update time of 33 milliseconds. Notice that before being updated, the
player's charge meter is set to full if no monsters are in the level.
After the characters are updated, the trigger object comes into play. If the player
walks into an active trigger, the appropriate script is executed.
From this point on, you render the scene by calling the render_frame function from
the application class. The render_frame function renders only the backdrop and
character in the map—it’s up to the rest of this function’s code to draw the status
window and charge meter.
{
// render simplified scene mesh for z-values
enable_zbuffer();
m_scene_object.render();
// draw the backdrop (composed of six textures)
disable_zbuffer();
begin_display_sprite();
for ( int i = 0; i < 2; i++)
{
for ( int j = 0; j < 3; j++)
{
IDirect3DTexture9* texture = m_scene_backdrops[i * 3 + j];
RECT rect;
calculate_texture_rect(texture, 0, 0, 0, 0, &rect);
draw_texture(g_d3d_sprite, texture, &rect, j * 256, i * 256, 1.0f, 1.0f, COLOR_WHITE);
}
}
end_display_sprite();
// draw characters and spells
enable_zbuffer();
m_game_chars.render(elapsed, NULL, 0.0f);
m_game_spells.render(NULL, 0.0f);
}
Again, you first render the scene by using the render_frame function, which takes as
an argument the amount of time (in milliseconds) that the animations should be
updated. You then draw the player's charge meter, but only if monsters are present
on the map.
There are numerous matrix- and vector-related functions at work here—you use
them to calculate the screen coordinates in which to draw the charge meter. To
determine where to draw the meter, using the preceding D3DXVec3Project function,
calculate the 2D coordinates based on the 3D world space coordinates of the player.
During the game-play, you display the player's statistics at the upper-left corner of
the screen—this includes the health and mana points, at their current levels and at
their maximum level.
The game_frame function ends up by calling end_display_scene and displaying the frame to the
user.