Sekarang kita telah mendefinisikan kelas dan memastikan bahwa constructorclass KeyController : public Base, public KeyboardListener { };
Kita sekarang harus menimpa fungsi virtual mewarisi terkait dengan dua kelas induk. Buat definisi untuk "OnMessage","KeyPressed", dan "KeyReleased" dan tambahkan variabel anggota dua mewakili karakter dan keyboard yang sesuai.class KeyController : public Base, public KeyboardListener { DECLARE_MANAGEMENT_LAYER(KeyController) private: Character *character; Keyboard *keyboard; public: KeyController(Character *_character, Keyboard *_keyboard) : Base(“KeyController”), character(_character), keyboard(_keyboard) { AddSender(System::Instance()); keyboard->AddKeyboardListener(this); } virtual void OnMessage(MessageData *data) { } virtual void KeyPressed(Keyboard* keyboard, Producer::KeyboardKey key, Producer::KeyCharacter character) { } virtual void KeyReleased(Keyboard *keyboard, Producer::KeyboardKey key, Producer::KeyCharacter character) { } }; IMPLEMENT_MANAGEMENT_LAYER(KeyController)
menangani message passing untuk keyboard. Bila pengguna berinteraksi dengan
keyboard pesan dikirim dan fungsi OnMessage disebut secara implisit.
Berikut ini akan menetapkan fungsi yang diperlukan untuk memutar dan menambah
kecepatan untuk variabel anggota "karakter".
virtual void OnMessage(MessageData *data)
{
if(data->message == "preframe")
{
double delta = *(double*)data->userData;
float rotation = character->GetRotation(),
velocity = 0.0f;
if(keyboard->GetKeyState(Producer::Key_Left))
{
rotation += (float)(delta*90.0);
}
if(keyboard->GetKeyState(Producer::Key_Right))
{
rotation -= (float)(delta*90.0);
}
if(keyboard->GetKeyState(Producer::Key_Up))
{
if(keyboard->GetKeyState(Producer::Key_Shift_R) ||
keyboard->GetKeyState(Producer::Key_Shift_L))
{
velocity += 4.0f;
}
else
{
velocity += 1.0f;
}
}
character->SetRotation(rotation);
character->SetVelocity(velocity);
}
}
Pastikan bahwa aplikasi kita akan keluar saat tombol Escape ditekan.Mengubah fungsi KeyPressed sebagai berikut:
virtual void KeyPressed(Keyboard* keyboard, Producer::KeyboardKey key,
Producer::KeyCharacter character)
{
switch(key)
{
case Producer::Key_Escape:
System::Instance()->Stop();
break;
}
}
Sekarang beralih ke file dan uji Utama kelas KeyController . kita mulaidengan men-setting path dan mendefinisikan Aplikasi, Environment, dan terrain.
Maka akan mengenali elemen-elemen dari environment tutorial sebelumnya.
#include // terrain #include #include #include #include int main() { // set the current path to the data path SetDataFilePathList(GetDeltaDataPathList()); // create our application and environment Application *pApp = new Application("config.xml"); InfiniteTerrain *pTerrain = new InfiniteTerrain; Environment *pEnv = new Environment; }
Tentukan sebuah karakter , Tripod, dan sebuah KeyController. Karakter akan mewakili model dapat dilihat kami.Kelas Tripod berisi metode untuk melampirkan benda Kamera Transformable lain, dalam hal ini Karakter. ObjekKeyController akan memproses input dan menambahkan gerakan Karakter .Tambahkan baris kode berikut di fungsi utama:// create a new character Character *guy = new Character("Horatio"); // create a tripod to attach the camera to the character Tripod *pTripod = new Tripod(pApp->GetCamera(), guy); KeyController *pKC = new KeyController(guy, pApp->GetKeyboard());
Sekarang kita mengatur objek Tripod agar karakter tidak hilang ketika ia berjalan.
pTripod->SetLookAtTarget(guy);
Sekarang kita jalankan model Karakter dan menambahkan objek ke scene.// load in the character model and add to the scene if(!guy->LoadFile("marine\\marine.rbody")) { MessageBox(NULL,"Failed to load character model","Error",MB_OK | MB_ICONSTOP); return -1; } pApp->GetScene()->AddDrawable(pEnv); pApp->GetScene()->AddDrawable(pTerrain); pApp->GetScene()->AddDrawable(guy);
Terakhir,tetapkan model asli, jalankan konfigurasi, dan kemudian jalankan aplikasi.guy->SetTransform(new Transform(0, 0, 0, 0, 0, 0)); pApp->Config(); pApp->Run();
Main terakhir akan terlihat seperti ini:#include "dtCore/dt.h" #include "dtABC/dtabc.h" #include "dtChar/dtchar.h" #include "KeyController.h" using namespace dtCore; using namespace dtABC; using namespace dtChar; int main() { // set the current path to the data path SetDataFilePathList(GetDeltaDataPathList()); // create our application and environment Application *pApp = new Application("config.xml"); InfiniteTerrain *pTerrain = new InfiniteTerrain; Environment *pEnv = new Environment; // create a new character Character *guy = new Character("Horatio"); // create a tripod to attach the camera to // the character Tripod *pTripod = new Tripod(pApp->GetCamera(), guy); KeyController *pKC=new KeyController(guy,pApp->GetKeyboard()); pTripod->SetLookAtTarget(guy); // a basic flat plane will suffice pTerrain->SetVerticalScale(0.1f); pTerrain->Regenerate(); // add some features to the environment pEnv->AddEffect(new SkyDome); pEnv->AddEffect(new CloudPlane(6,0.5,6,1,0.3,0.96,256,1800)); pApp->GetScene()->AddDrawable(pEnv); pApp->GetScene()->AddDrawable(pTerrain); // load in the character model and add to the scene if(!guy->LoadFile("marine\\marine.rbody")) { MessageBox(NULL, "Failed to load character model", "Error", MB_OK | MB_ICONSTOP); return -1; } pApp->GetScene()->AddDrawable(guy); // place the character at the origin guy->SetTransform(new Transform(0, 0, 0, 0, 0, 0)); pApp->Config(); pApp->Run(); delete pApp; return 0; }
Jika di mengkompilasi dan menjalankan akan melihat seperti ini: Kontrol karakter adalah sebagai berikut: Up Arrow - Berjalan Shift - Run Left Arrow - Putar kiri RightArrow - Putar kanan Escape - Keluar source: http://www.delta3d.org/article.php?story=20050126143732749&topic=tutorials
No comments:
Post a Comment