1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #ifndef __EVENT__ #define __EVENT__ #include <irr/irrlicht.h> using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace gui; using namespace io; class MyReceiver : public IEventReceiver { public: virtual bool OnEvent(const SEvent& event) { if(event.EventType == irr::EET_KEY_INPUT_EVENT) { keys[event.KeyInput.Key] = event.KeyInput.PressedDown; return false; } return false; } bool keys[KEY_KEY_CODES_COUNT]; }; #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <irr/irrlicht.h> #include "event.h" using namespace irr; using namespace core; using namespace scene; using namespace video; using namespace gui; using namespace io; IrrlichtDevice *device; ISceneManager* smgr; IVideoDriver* driver; vector3df playerLastPosition; bool playerMove; int main() { MyReceiver receiver; device = createDevice(EDT_OPENGL, dimension2d<s32>(800, 600),32, false, true, true); driver = device->getVideoDriver(); smgr = device->getSceneManager(); ICameraSceneNode *camera = smgr->addCameraSceneNode(); IAnimatedMesh* mesh = smgr->getMesh("data\\game\\players\\1\\1.md2"); IAnimatedMeshSceneNode *player = smgr->addAnimatedMeshSceneNode(mesh); this->device->setEventReceiver(receiver); camera->setParent(player); while(this->device->run()) { this->driver->beginScene(true, true, SColor(255,100,101,140)); smgr->drawAll(); driver->endScene(); //==============================================================// //=== Recuperation de la position du player pour l'animation ===// //==============================================================// playerLastPosition = player->getPosition(); //==============================================================// //=== Actualisation de la camera ===// //==============================================================// camera->setTarget(player->getPosition()); camera->setPosition(vector3df(-60,50,0)); //=============================================================// //=== Deplacement du personnage ===// //=============================================================// if (receiver.keys[KEY_UP]) //Avance { vector3df facing(cos(player->getRotation().Y * PI/180.0f), 0, -sin(player->getRotation().Y * PI/180.0f)); facing.normalize(); vector3df newPos = player->getPosition() + (facing - (facing * (f32)0.5)); player->setPosition(newPos); } if (receiver.keys[KEY_DOWN]) //Recule { vector3df facing(cos(player->getRotation().Y * PI/180.0f),0,-sin(player->getRotation().Y * PI/180.0f)); facing.normalize(); vector3df newPos = player->getPosition() - (facing- (facing * (f32)0.7)); player->setPosition(newPos); playerMove = true; } if (receiver.keys[KEY_LEFT]) //Rotation vers la gauche { player->setRotation(player->getRotation() - vector3df(0,1,0)); } if (receiver.keys[KEY_RIGHT]) //Rotation vers la droite { player->setRotation(player->getRotation() + vector3df(0,1,0)); } //==============================================================// //=== Animation du personnage ===// //==============================================================// if (playerLastPosition != player->getPosition()) //Si le joueur bouge { if (playerMove == false) //Si l'animation de deplacement n'est pas activer { player->setMD2Animation(EMAT_RUN); //Change l'animation du joueur playerMove = true; } } else //Si le joueur ne bouge plus { if (playerMove == true) //Si l'animation "courire" a ete activer { player->setMD2Animation(EMAT_STAND); //Change l'animation du joueur playerMove = false; } } if (receiver.keys[KEY_ESCAPE]) { exit(0); } } return 0; } |
Voila si vous avez des question demander !!