[8870] | 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 "util/loading/factory.h" |
---|
| 21 | #include "util/loading/load_param.h" |
---|
| 22 | |
---|
[8874] | 23 | #include "interactive_model.h" |
---|
[8870] | 24 | |
---|
[8880] | 25 | |
---|
[8874] | 26 | #include "door.h" |
---|
| 27 | #include "class_list.h" |
---|
[8870] | 28 | |
---|
| 29 | |
---|
| 30 | using namespace std; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | CREATE_FACTORY(Door, CL_DOOR); |
---|
| 34 | |
---|
| 35 | |
---|
[8880] | 36 | |
---|
| 37 | //! list of all different animations a std md2model supports |
---|
| 38 | sAnim Door::animationList[2] = |
---|
| 39 | { |
---|
| 40 | // begin, end, fps, interruptable |
---|
[8901] | 41 | { 0, 15, 30, 0 }, //!< OPEN |
---|
| 42 | { 15, 29, 30, 0 } //!< CLOSE |
---|
[8880] | 43 | }; |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
[8870] | 47 | Door::Door () |
---|
| 48 | { |
---|
| 49 | this->init(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | Door::Door(const TiXmlElement* root) |
---|
| 54 | { |
---|
[8883] | 55 | |
---|
[8890] | 56 | this->setClassID(CL_DOOR, "Door"); |
---|
| 57 | this->scale = 1.0f; |
---|
| 58 | |
---|
[8883] | 59 | if( root != NULL) |
---|
[8870] | 60 | this->loadParams(root); |
---|
[8890] | 61 | |
---|
| 62 | this->toList(OM_COMMON); |
---|
| 63 | this->bLocked = false; |
---|
| 64 | |
---|
| 65 | this->loadMD2Texture("maps/doors.jpg"); |
---|
| 66 | this->loadModel("models/creatures/doors.md2", this->scale); |
---|
[8901] | 67 | |
---|
[8890] | 68 | this->setAnimation(DOOR_CLOSE, MD2_ANIM_ONCE); |
---|
[8870] | 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | Door::~Door () |
---|
| 73 | {} |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * loads the Settings of a MD2Creature from an XML-element. |
---|
| 79 | * @param root the XML-element to load the MD2Creature's properties from |
---|
| 80 | */ |
---|
| 81 | void Door::loadParams(const TiXmlElement* root) |
---|
| 82 | { |
---|
| 83 | WorldEntity::loadParams(root); |
---|
| 84 | |
---|
[8890] | 85 | LoadParam(root, "action-radius", this, Door, setActionRadius) |
---|
[8876] | 86 | .describe("sets the action radius of the door") |
---|
[8883] | 87 | .defaultValues(3.0); |
---|
[8890] | 88 | LoadParam(root, "scale", this, Door, setScale) |
---|
| 89 | .describe("sets the scale of the door") |
---|
| 90 | .defaultValues(1.0); |
---|
[8870] | 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
[8878] | 94 | /** |
---|
| 95 | * sets the animatin of this entity |
---|
| 96 | */ |
---|
[8880] | 97 | void Door::setAnimation(int animNum, int playbackMode) |
---|
[8870] | 98 | { |
---|
| 99 | if( likely(this->getModel(0) != NULL)) |
---|
[8881] | 100 | ((InteractiveModel*)this->getModel(0))->setAnimation(animationList[animNum].firstFrame, |
---|
| 101 | animationList[animNum].lastFrame, |
---|
| 102 | animationList[animNum].fps, |
---|
| 103 | animationList[animNum].bStoppable, |
---|
| 104 | playbackMode); |
---|
[8870] | 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
[8878] | 108 | /** |
---|
| 109 | * ticks the door |
---|
| 110 | * @param time: time since last tick |
---|
| 111 | */ |
---|
[8870] | 112 | void Door::tick (float time) |
---|
| 113 | { |
---|
| 114 | if( likely(this->getModel(0) != NULL)) |
---|
| 115 | ((InteractiveModel*)this->getModel(0))->tick(time); |
---|
| 116 | |
---|
[8901] | 117 | if( !this->bOpen) |
---|
| 118 | { |
---|
| 119 | if( this->checkOpen()) |
---|
| 120 | { |
---|
| 121 | this->open(); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | if( !this->checkOpen()) |
---|
| 127 | { |
---|
| 128 | this->close(); |
---|
| 129 | } |
---|
| 130 | } |
---|
[8890] | 131 | |
---|
[8870] | 132 | } |
---|
| 133 | |
---|
| 134 | |
---|
[8876] | 135 | /** |
---|
| 136 | * open the door |
---|
| 137 | */ |
---|
| 138 | void Door::open() |
---|
| 139 | { |
---|
| 140 | if( this->bLocked) |
---|
| 141 | return; |
---|
[8870] | 142 | |
---|
[8880] | 143 | this->setAnimation(DOOR_OPEN, MD2_ANIM_ONCE); |
---|
[8890] | 144 | this->bOpen = true; |
---|
[8876] | 145 | } |
---|
| 146 | |
---|
| 147 | |
---|
[8872] | 148 | /** |
---|
[8876] | 149 | * close the door |
---|
| 150 | */ |
---|
| 151 | void Door::close() |
---|
| 152 | { |
---|
[8890] | 153 | this->setAnimation(DOOR_CLOSE, MD2_ANIM_ONCE); |
---|
| 154 | this->bOpen = false; |
---|
[8876] | 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | /** |
---|
[8872] | 160 | * checks if the door is open |
---|
| 161 | */ |
---|
[8874] | 162 | bool Door::checkOpen() |
---|
[8870] | 163 | { |
---|
| 164 | |
---|
[8874] | 165 | std::list<BaseObject*>::const_iterator it; |
---|
| 166 | const std::list<BaseObject*>* list = ClassList::getList(CL_PLAYABLE); |
---|
| 167 | WorldEntity* entity; |
---|
[8875] | 168 | float distance; |
---|
[8870] | 169 | |
---|
[8874] | 170 | if( list == NULL) |
---|
| 171 | return false; |
---|
| 172 | |
---|
| 173 | // for all players |
---|
| 174 | for( it = list->begin(); it != list->end(); it++) |
---|
| 175 | { |
---|
| 176 | entity = dynamic_cast<WorldEntity*>(*it); |
---|
| 177 | |
---|
[8875] | 178 | distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len()); |
---|
| 179 | if( distance < this->actionRadius) |
---|
| 180 | return true; |
---|
[8874] | 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | list = ClassList::getList(CL_GENERIC_NPC); |
---|
| 185 | if( list == NULL) |
---|
| 186 | return false; |
---|
| 187 | for( it = list->begin(); it != list->end(); it++) |
---|
| 188 | { |
---|
[8875] | 189 | entity = dynamic_cast<WorldEntity*>(*it); |
---|
[8874] | 190 | |
---|
[8875] | 191 | distance = fabs((this->getAbsCoor() - entity->getAbsCoor()).len()); |
---|
| 192 | if( distance < this->actionRadius) |
---|
| 193 | return true; |
---|
[8874] | 194 | } |
---|
| 195 | |
---|
| 196 | return false; |
---|
[8870] | 197 | } |
---|
| 198 | |
---|
[8872] | 199 | |
---|
| 200 | |
---|