创建战斗序列DEMO(增加音效)
下载源码和工程
增加的关键代码以红色标示:
class cApp;
class cGameCharController : public cCharController
{
private:
cApp* m_app;
private:
virtual void play_action_sound(sCharacter* character);
public:
void set_data(cApp* app)
{
m_app = app;
}
};
/*************************************************************************************************/
#define MAX_PLAY_SOUND 8
#define SOUND_ATTACK 0
#define SOUND_FIRE 1
#define SOUND_GROUNDBALL 2
#define SOUND_ICE 3
#define SOUND_POISON 4
#define SOUND_BIGBOMB 5
#define SOUND_HEAL 6
#define SOUND_LARGECURE 7
class cApp : public cFramework
{
private :
cCamera m_camera;
cTextWindow m_char_stats; // text window for HP/MP stats
cTextWindow m_spell_options; // text window for spells
cInput m_input;
cInputDevice m_keyboard;
cInputDevice m_mouse;
cMesh m_terrain_mesh;
cObject m_terrain_obj;
cGameCharController m_gc_controller;
cSpellController m_spell_controller;
ID3DXFont* m_font;
IDirect3DVertexBuffer9* m_target_vb;
IDirect3DTexture9* m_button;
sItem m_mil[1024]; // the master item list
cSound m_sound;
cSoundData m_sound_data[MAX_PLAY_SOUND];
cSoundChannel m_sound_channel[MAX_PLAY_SOUND];
////////////////////////////////////////////////////////////////////////////// //
public :
cApp()
{
m_font = NULL;
m_target_vb = NULL;
m_button = NULL;
ZeroMemory(m_mil, sizeof (m_mil));
}
void play_attack()
{
if (!m_sound_channel[SOUND_ATTACK].is_playing ())
m_sound_channel[SOUND_ATTACK].play(&m_sound_data[SOUND_ATTACK], 100, 1);
}
void play_fire()
{
if (!m_sound_channel[SOUND_FIRE].is_playing ())
m_sound_channel[SOUND_FIRE].play(&m_sound_data[SOUND_FIRE], 100, 1);
}
void play_groundball()
{
if (!m_sound_channel[SOUND_GROUNDBALL].is_playing ())
m_sound_channel[SOUND_GROUNDBALL].play(&m_sound_data[SOUND_GROUNDBALL], 100, 1);
}
void play_ice()
{
if (!m_sound_channel[SOUND_ICE].is_playing ())
m_sound_channel[SOUND_ICE].play(&m_sound_data[SOUND_ICE], 100, 1);
}
void play_poison()
{
if (!m_sound_channel[SOUND_POISON].is_playing ())
m_sound_channel[SOUND_POISON].play(&m_sound_data[SOUND_POISON], 100, 1);
}
void play_bigbomb()
{
if (!m_sound_channel[SOUND_BIGBOMB].is_playing ())
m_sound_channel[SOUND_BIGBOMB].play(&m_sound_data[SOUND_BIGBOMB], 100, 1);
}
void play_heal()
{
if (!m_sound_channel[SOUND_HEAL].is_playing ())
m_sound_channel[SOUND_HEAL].play(&m_sound_data[SOUND_HEAL], 100, 1);
}
void play_largecure()
{
if (!m_sound_channel[SOUND_LARGECURE].is_playing ())
m_sound_channel[SOUND_LARGECURE].play(&m_sound_data[SOUND_LARGECURE], 100, 1);
}
private :
long get_char_at( long x_pos, long y_pos);
public :
bool init();
bool frame();
};
class cGameCharController : public cCharController
{
private:
cApp* m_app;
private:
virtual void play_action_sound(sCharacter* character);
public:
void set_data(cApp* app)
{
m_app = app;
}
};
/*************************************************************************************************/
#define MAX_PLAY_SOUND 8
#define SOUND_ATTACK 0
#define SOUND_FIRE 1
#define SOUND_GROUNDBALL 2
#define SOUND_ICE 3
#define SOUND_POISON 4
#define SOUND_BIGBOMB 5
#define SOUND_HEAL 6
#define SOUND_LARGECURE 7
class cApp : public cFramework
{
private :
cCamera m_camera;
cTextWindow m_char_stats; // text window for HP/MP stats
cTextWindow m_spell_options; // text window for spells
cInput m_input;
cInputDevice m_keyboard;
cInputDevice m_mouse;
cMesh m_terrain_mesh;
cObject m_terrain_obj;
cGameCharController m_gc_controller;
cSpellController m_spell_controller;
ID3DXFont* m_font;
IDirect3DVertexBuffer9* m_target_vb;
IDirect3DTexture9* m_button;
sItem m_mil[1024]; // the master item list
cSound m_sound;
cSoundData m_sound_data[MAX_PLAY_SOUND];
cSoundChannel m_sound_channel[MAX_PLAY_SOUND];
////////////////////////////////////////////////////////////////////////////// //
public :
cApp()
{
m_font = NULL;
m_target_vb = NULL;
m_button = NULL;
ZeroMemory(m_mil, sizeof (m_mil));
}
void play_attack()
{
if (!m_sound_channel[SOUND_ATTACK].is_playing ())
m_sound_channel[SOUND_ATTACK].play(&m_sound_data[SOUND_ATTACK], 100, 1);
}
void play_fire()
{
if (!m_sound_channel[SOUND_FIRE].is_playing ())
m_sound_channel[SOUND_FIRE].play(&m_sound_data[SOUND_FIRE], 100, 1);
}
void play_groundball()
{
if (!m_sound_channel[SOUND_GROUNDBALL].is_playing ())
m_sound_channel[SOUND_GROUNDBALL].play(&m_sound_data[SOUND_GROUNDBALL], 100, 1);
}
void play_ice()
{
if (!m_sound_channel[SOUND_ICE].is_playing ())
m_sound_channel[SOUND_ICE].play(&m_sound_data[SOUND_ICE], 100, 1);
}
void play_poison()
{
if (!m_sound_channel[SOUND_POISON].is_playing ())
m_sound_channel[SOUND_POISON].play(&m_sound_data[SOUND_POISON], 100, 1);
}
void play_bigbomb()
{
if (!m_sound_channel[SOUND_BIGBOMB].is_playing ())
m_sound_channel[SOUND_BIGBOMB].play(&m_sound_data[SOUND_BIGBOMB], 100, 1);
}
void play_heal()
{
if (!m_sound_channel[SOUND_HEAL].is_playing ())
m_sound_channel[SOUND_HEAL].play(&m_sound_data[SOUND_HEAL], 100, 1);
}
void play_largecure()
{
if (!m_sound_channel[SOUND_LARGECURE].is_playing ())
m_sound_channel[SOUND_LARGECURE].play(&m_sound_data[SOUND_LARGECURE], 100, 1);
}
private :
long get_char_at( long x_pos, long y_pos);
public :
bool init();
bool frame();
};
void cGameCharController::play_action_sound(sCharacter* character)
{
if (character ->action == CHAR_ATTACK)
m_app->play_attack();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_FIRE)
m_app->play_fire();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_GROUNDBALL)
m_app->play_groundball();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_ICE)
m_app->play_ice();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_POISON)
m_app->play_poison();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_BIGBOMB)
m_app->play_bigbomb();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_HEAL)
m_app->play_heal();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_LARGECURE)
m_app->play_largecure();
}
{
if (character ->action == CHAR_ATTACK)
m_app->play_attack();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_FIRE)
m_app->play_fire();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_GROUNDBALL)
m_app->play_groundball();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_ICE)
m_app->play_ice();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_POISON)
m_app->play_poison();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_BIGBOMB)
m_app->play_bigbomb();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_HEAL)
m_app->play_heal();
if (character ->action == CHAR_SPELL && character->spell_index == SPELL_LARGECURE)
m_app->play_largecure();
}
bool
cApp::init()
{
create_display(g_hwnd, CLIENT_WIDTH, CLIENT_HEIGHT, 16, true , true );
set_perspective(D3DX_PI/4, 1.3333f, 1.0f, 10000.0f);
create_font(&m_font, "Arial", 16, true , false );
m_input.create(g_hwnd, get_window_inst());
m_keyboard.create_keyboard(&m_input);
m_mouse.create_mouse(&m_input, true );
m_sound.init(g_hwnd, 22050, 1, 16, DSSCL_PRIORITY);
m_sound_data[SOUND_ATTACK].load_wav("..\\data\\attack.wav");
m_sound_data[SOUND_FIRE].load_wav("..\\data\\fire.wav");
m_sound_data[SOUND_GROUNDBALL].load_wav("..\\data\\groundball.wav");
m_sound_data[SOUND_ICE].load_wav("..\\data\\ice.wav");
m_sound_data[SOUND_POISON].load_wav("..\\data\\poison.wav");
m_sound_data[SOUND_BIGBOMB].load_wav("..\\data\\BigBomb.wav");
m_sound_data[SOUND_HEAL].load_wav("..\\data\\heal.wav");
m_sound_data[SOUND_LARGECURE].load_wav("..\\data\\LargeCure.wav");
for(int i = 0; i < MAX_PLAY_SOUND; i++)
{
m_sound_channel[i].create(&m_sound,
m_sound_data[i].get_frequency(),
m_sound_data[i].get_channels(),
m_sound_data[i].get_bits_per_sample());
}
create_vertex_buffer(&m_target_vb, 6, sizeof (sTargetVertex), TARGET_VERTEX_FVF);
fill_in_vertex_buffer(m_target_vb, 0, 6, g_target_verts);
load_texture_from_file(&m_button, "..\\Data\\Buttons.bmp", 0, D3DFMT_UNKNOWN);
m_terrain_mesh.load("..\\Data\\Battle.x", "..\\Data\\");
m_terrain_obj.create(&m_terrain_mesh);
// create character stats and spell options text window
m_char_stats.create(m_font);
m_char_stats.move(508, 400, 128, 48, -1, -1, COLOR_DARK_BLUE, COLOR_ARGENTINE);
m_spell_options.create(m_font);
m_spell_options.move(4, 4, 632, 328, -1, -1, COLOR_DARK_BLUE, COLOR_ARGENTINE);
// load in master item list
FILE* fp = fopen("..\\Data\\Default.mil", "rb");
if (fp)
{
fread(m_mil, 1, sizeof (m_mil), fp);
fclose(fp);
}
m_gc_controller.init(m_font, "..\\Data\\Default.mcl",
m_mil, m_spell_controller.get_spell_list(),
array_num(g_char_mesh_names), g_char_mesh_names,
"..\\Data\\", "..\\Data\\",
array_num(g_char_anim_infos), g_char_anim_infos);
m_spell_controller.init("..\\Data\\Default.msl",
array_num(g_spell_mesh_names), g_spell_mesh_names,
"..\\Data\\");
m_gc_controller.attach(&m_spell_controller);
m_spell_controller.attach(&m_gc_controller);
m_gc_controller.set_data( this );
// add player
m_gc_controller.add_char(0, 0, CHAR_PC, CHAR_STAND, 200.0f, 0.0f, 0.0f, 4.71f);
// hardcoded - add monster
m_gc_controller.add_char(1, 1, CHAR_MONSTER, CHAR_STAND, -200.0f, 0.0f, 0.0f, 1.57f);
m_gc_controller.add_char(2, 1, CHAR_MONSTER, CHAR_STAND, -100.0f, 0.0f, -200.0f, 1.57f);
m_gc_controller.add_char(3, 1, CHAR_MONSTER, CHAR_STAND, 0.0f, 0.0f, 100.0f, 1.57f);
// give an axe to one of the monsters
m_gc_controller.equip(m_gc_controller.get_char(1), 8, WEAPON, true );
return true ;
}
{
create_display(g_hwnd, CLIENT_WIDTH, CLIENT_HEIGHT, 16, true , true );
set_perspective(D3DX_PI/4, 1.3333f, 1.0f, 10000.0f);
create_font(&m_font, "Arial", 16, true , false );
m_input.create(g_hwnd, get_window_inst());
m_keyboard.create_keyboard(&m_input);
m_mouse.create_mouse(&m_input, true );
m_sound.init(g_hwnd, 22050, 1, 16, DSSCL_PRIORITY);
m_sound_data[SOUND_ATTACK].load_wav("..\\data\\attack.wav");
m_sound_data[SOUND_FIRE].load_wav("..\\data\\fire.wav");
m_sound_data[SOUND_GROUNDBALL].load_wav("..\\data\\groundball.wav");
m_sound_data[SOUND_ICE].load_wav("..\\data\\ice.wav");
m_sound_data[SOUND_POISON].load_wav("..\\data\\poison.wav");
m_sound_data[SOUND_BIGBOMB].load_wav("..\\data\\BigBomb.wav");
m_sound_data[SOUND_HEAL].load_wav("..\\data\\heal.wav");
m_sound_data[SOUND_LARGECURE].load_wav("..\\data\\LargeCure.wav");
for(int i = 0; i < MAX_PLAY_SOUND; i++)
{
m_sound_channel[i].create(&m_sound,
m_sound_data[i].get_frequency(),
m_sound_data[i].get_channels(),
m_sound_data[i].get_bits_per_sample());
}
create_vertex_buffer(&m_target_vb, 6, sizeof (sTargetVertex), TARGET_VERTEX_FVF);
fill_in_vertex_buffer(m_target_vb, 0, 6, g_target_verts);
load_texture_from_file(&m_button, "..\\Data\\Buttons.bmp", 0, D3DFMT_UNKNOWN);
m_terrain_mesh.load("..\\Data\\Battle.x", "..\\Data\\");
m_terrain_obj.create(&m_terrain_mesh);
// create character stats and spell options text window
m_char_stats.create(m_font);
m_char_stats.move(508, 400, 128, 48, -1, -1, COLOR_DARK_BLUE, COLOR_ARGENTINE);
m_spell_options.create(m_font);
m_spell_options.move(4, 4, 632, 328, -1, -1, COLOR_DARK_BLUE, COLOR_ARGENTINE);
// load in master item list
FILE* fp = fopen("..\\Data\\Default.mil", "rb");
if (fp)
{
fread(m_mil, 1, sizeof (m_mil), fp);
fclose(fp);
}
m_gc_controller.init(m_font, "..\\Data\\Default.mcl",
m_mil, m_spell_controller.get_spell_list(),
array_num(g_char_mesh_names), g_char_mesh_names,
"..\\Data\\", "..\\Data\\",
array_num(g_char_anim_infos), g_char_anim_infos);
m_spell_controller.init("..\\Data\\Default.msl",
array_num(g_spell_mesh_names), g_spell_mesh_names,
"..\\Data\\");
m_gc_controller.attach(&m_spell_controller);
m_spell_controller.attach(&m_gc_controller);
m_gc_controller.set_data( this );
// add player
m_gc_controller.add_char(0, 0, CHAR_PC, CHAR_STAND, 200.0f, 0.0f, 0.0f, 4.71f);
// hardcoded - add monster
m_gc_controller.add_char(1, 1, CHAR_MONSTER, CHAR_STAND, -200.0f, 0.0f, 0.0f, 1.57f);
m_gc_controller.add_char(2, 1, CHAR_MONSTER, CHAR_STAND, -100.0f, 0.0f, -200.0f, 1.57f);
m_gc_controller.add_char(3, 1, CHAR_MONSTER, CHAR_STAND, 0.0f, 0.0f, 100.0f, 1.57f);
// give an axe to one of the monsters
m_gc_controller.equip(m_gc_controller.get_char(1), 8, WEAPON, true );
return true ;
}