| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 5 |  | 
|---|
| 6 |    Copyright (C) 2004 orx | 
|---|
| 7 |  | 
|---|
| 8 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 9 |    it under the terms of the GNU General Public License as published by | 
|---|
| 10 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 11 |    any later version. | 
|---|
| 12 |  | 
|---|
| 13 |    ### File Specific: | 
|---|
| 14 |    main-programmer: Patrick Boenzli | 
|---|
| 15 |    co-programmer: | 
|---|
| 16 | */ | 
|---|
| 17 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | #include "attractor_mine.h" | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | #include "shader.h" | 
|---|
| 24 | #include "state.h" | 
|---|
| 25 | #include "debug.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "player.h" | 
|---|
| 28 | #include "playable.h" | 
|---|
| 29 |  | 
|---|
| 30 | #include "loading/factory.h" | 
|---|
| 31 | #include "loading/load_param.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "effects/explosion.h" | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | ObjectListDefinition(AttractorMine); | 
|---|
| 37 | CREATE_FACTORY(AttractorMine); | 
|---|
| 38 | #include "script_class.h" | 
|---|
| 39 | CREATE_SCRIPTABLE_CLASS(AttractorMine, | 
|---|
| 40 |                         addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName)) | 
|---|
| 41 |                         //Coordinates | 
|---|
| 42 |                         ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor)) | 
|---|
| 43 |                         ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX)) | 
|---|
| 44 |                         ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY)) | 
|---|
| 45 |                         ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ)) | 
|---|
| 46 |                        ); | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | AttractorMine::AttractorMine(const TiXmlElement* root) | 
|---|
| 52 |     : NPC(NULL) | 
|---|
| 53 | { | 
|---|
| 54 |   this->registerObject(this, AttractorMine::_objectList); | 
|---|
| 55 |  | 
|---|
| 56 |   this->toList(OM_GROUP_02); | 
|---|
| 57 |  | 
|---|
| 58 |   if ((float)rand()/RAND_MAX > .5f) | 
|---|
| 59 |     this->loadModel("models/ships/noxon_battle_drone.obj", .3); | 
|---|
| 60 |   else | 
|---|
| 61 |     this->loadModel("models/ships/noxon_battle_drone.obj", .2); | 
|---|
| 62 |  | 
|---|
| 63 |   this->setDamage(30.0f); | 
|---|
| 64 |   this->subscribeReaction(CoRe::CREngine::CR_OBJECT_DAMAGE, Playable::staticClassID()); | 
|---|
| 65 |  | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 |   this->shader = NULL; | 
|---|
| 69 |   //if (likely(Shader::checkShaderAbility())) | 
|---|
| 70 | //    this->shader = Shader::getShader("shaders/toon.vert", "shaders/toon.frag"); | 
|---|
| 71 |  | 
|---|
| 72 |   this->randomRotAxis = VECTOR_RAND(1); | 
|---|
| 73 |  | 
|---|
| 74 |   if (root != NULL) | 
|---|
| 75 |     this->loadParams(root); | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 |  | 
|---|
| 79 | AttractorMine::~AttractorMine () | 
|---|
| 80 | { | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void AttractorMine::loadParams(const TiXmlElement* root) | 
|---|
| 84 | { | 
|---|
| 85 |   NPC::loadParams(root); | 
|---|
| 86 |  | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 |  | 
|---|
| 90 | void AttractorMine::destroy(WorldEntity* killer) | 
|---|
| 91 | { | 
|---|
| 92 |   Explosion::explode(this, Vector(10,10,10)); | 
|---|
| 93 |   this->toList(OM_DEAD); | 
|---|
| 94 |  | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 |  *  the entity is drawn onto the screen with this function | 
|---|
| 101 |  * | 
|---|
| 102 |  * This is a central function of an entity: call it to let the entity painted to the screen. | 
|---|
| 103 |  * Just override this function with whatever you want to be drawn. | 
|---|
| 104 |  */ | 
|---|
| 105 | void AttractorMine::draw() const | 
|---|
| 106 | { | 
|---|
| 107 |   glMatrixMode(GL_MODELVIEW); | 
|---|
| 108 |   glPushMatrix(); | 
|---|
| 109 |   float matrix[4][4]; | 
|---|
| 110 |  | 
|---|
| 111 |   /* translate */ | 
|---|
| 112 |   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 113 |                 this->getAbsCoor ().y, | 
|---|
| 114 |                 this->getAbsCoor ().z); | 
|---|
| 115 |   /* rotate */ | 
|---|
| 116 |   this->getAbsDir ().matrix (matrix); | 
|---|
| 117 |   glMultMatrixf((float*)matrix); | 
|---|
| 118 |  | 
|---|
| 119 |   //   if (this->shader != NULL && this->shader != Shader::getActiveShader()) | 
|---|
| 120 |   //     shader->activateShader(); | 
|---|
| 121 |  | 
|---|
| 122 |   this->getModel()->draw(); | 
|---|
| 123 |   //   shader->deactivateShader(); | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 |   /*  if (this->model) | 
|---|
| 127 |       this->model->draw();*/ | 
|---|
| 128 |   glPopMatrix(); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | void AttractorMine::tick(float dt) | 
|---|
| 133 | { | 
|---|
| 134 |   PNode* attractNode = State::getPlayer()->getPlayable(); | 
|---|
| 135 |   if (attractNode != NULL) | 
|---|
| 136 |   { | 
|---|
| 137 |     if (this->distance(attractNode) < 80) | 
|---|
| 138 |     { | 
|---|
| 139 |       Vector dist = (attractNode->getAbsCoor() -  this->getAbsCoor()); | 
|---|
| 140 |       float distance = dist.len(); | 
|---|
| 141 |       this->velocity += dist * (( 250.0 - distance) / distance * dt); | 
|---|
| 142 |     } | 
|---|
| 143 |   } | 
|---|
| 144 |  | 
|---|
| 145 |   this->shiftCoor(this->velocity * dt); | 
|---|
| 146 |  | 
|---|
| 147 |   //  Vector direction = (State::getCameraTarget()->getAbsCoor() - this->getAbsCoor()); | 
|---|
| 148 |  | 
|---|
| 149 |   //if (directin.len() < 100) | 
|---|
| 150 |   //  this->shiftCoor(direction *dt * 5 * exp(-direction.len() / 30.0)); | 
|---|
| 151 |   this->shiftDir(Quaternion(.5 * dt, this->randomRotAxis)); | 
|---|
| 152 |  | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|