[10423] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Fabian 'x3n' Landau |
---|
| 13 | co-programmer: |
---|
| 14 | */ |
---|
[10487] | 15 | |
---|
| 16 | #include "sound/resource_sound_buffer.h" |
---|
[10423] | 17 | #include "util/loading/load_param.h" |
---|
| 18 | #include "util/loading/factory.h" |
---|
| 19 | #include "debug.h" |
---|
| 20 | |
---|
| 21 | #include "mover.h" |
---|
[10519] | 22 | #include "state.h" |
---|
| 23 | #include "camera.h" |
---|
[10423] | 24 | |
---|
| 25 | |
---|
| 26 | ObjectListDefinition(Mover); |
---|
| 27 | CREATE_FACTORY(Mover); |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * initializes the Mover from a XmlElement |
---|
| 31 | */ |
---|
| 32 | Mover::Mover(const TiXmlElement* root) |
---|
| 33 | { |
---|
[10487] | 34 | this->registerObject(this, Mover::_objectList); |
---|
[10519] | 35 | this->toList(OM_ENVIRON); |
---|
[10423] | 36 | |
---|
[10487] | 37 | this->targetCoordinates = Vector(0, 0, 0); |
---|
| 38 | this->originCoordinates = Vector(0, 87, -256); |
---|
| 39 | this->actionRadius = 40.0; |
---|
| 40 | this->actionTime = 1.0; |
---|
[10423] | 41 | |
---|
[10487] | 42 | if (root != NULL) |
---|
| 43 | this->loadParams(root); |
---|
| 44 | |
---|
| 45 | this->updateNode(0.001); |
---|
| 46 | this->originCoordinates = this->getAbsCoor(); |
---|
| 47 | this->state = Closed; |
---|
| 48 | |
---|
| 49 | this->soundSource_starting.setSourceNode(this); |
---|
| 50 | this->soundSource_moving.setSourceNode(this); |
---|
| 51 | this->soundSource_ending.setSourceNode(this); |
---|
| 52 | |
---|
| 53 | // this->soundSource_starting.setRolloffFactor(0.0); |
---|
| 54 | // this->soundSource_moving.setRolloffFactor(0.0); |
---|
| 55 | // this->soundSource_ending.setRolloffFactor(0.0); |
---|
[10423] | 56 | } |
---|
| 57 | |
---|
| 58 | Mover::~Mover() |
---|
| 59 | { |
---|
[10487] | 60 | if (this->soundSource_starting.isPlaying()) |
---|
| 61 | this->soundSource_starting.stop(); |
---|
| 62 | if (this->soundSource_moving.isPlaying()) |
---|
| 63 | this->soundSource_moving.stop(); |
---|
| 64 | if (this->soundSource_ending.isPlaying()) |
---|
| 65 | this->soundSource_ending.stop(); |
---|
[10423] | 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * loads the Settings of the Mover from an XML-element. |
---|
| 70 | * @param root the XML-element to load the Movers's properties from |
---|
| 71 | */ |
---|
| 72 | void Mover::loadParams(const TiXmlElement* root) |
---|
| 73 | { |
---|
[10487] | 74 | WorldEntity::loadParams(root); |
---|
[10423] | 75 | |
---|
[10487] | 76 | LoadParam(root, "rel-target-coor", this, Mover, setTargetCoordinates) |
---|
| 77 | .describe("sets the relative target coordinates of the door"); |
---|
| 78 | LoadParam(root, "action-radius", this, Mover, setActionRadius) |
---|
| 79 | .describe("sets the action radius of the door") |
---|
| 80 | .defaultValues(40.0); |
---|
| 81 | LoadParam(root, "action-time", this, Mover, setActionTime) |
---|
| 82 | .describe("sets the action time of the door") |
---|
| 83 | .defaultValues(1.0); |
---|
| 84 | LoadParam(root, "opening-sound", this, Mover, setOpeningSoundFile) |
---|
| 85 | .describe("Sets the file of the opening sound source"); |
---|
| 86 | LoadParam(root, "opened-sound", this, Mover, setOpenedSoundFile) |
---|
| 87 | .describe("Sets the file of the opened sound source"); |
---|
| 88 | LoadParam(root, "moving-sound", this, Mover, setMovingSoundFile) |
---|
| 89 | .describe("Sets the file of the moving sound source"); |
---|
| 90 | LoadParam(root, "closing-sound", this, Mover, setClosingSoundFile) |
---|
| 91 | .describe("Sets the file of the closing sound source"); |
---|
| 92 | LoadParam(root, "closed-sound", this, Mover, setClosedSoundFile) |
---|
| 93 | .describe("Sets the file of the closed sound source"); |
---|
[10423] | 94 | } |
---|
| 95 | |
---|
[10487] | 96 | void Mover::setOpeningSoundFile(const std::string& fileName) |
---|
| 97 | { |
---|
| 98 | this->soundBuffer_opening = OrxSound::ResourceSoundBuffer(fileName); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void Mover::setOpenedSoundFile(const std::string& fileName) |
---|
| 102 | { |
---|
| 103 | this->soundBuffer_opened = OrxSound::ResourceSoundBuffer(fileName); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void Mover::setMovingSoundFile(const std::string& fileName) |
---|
| 107 | { |
---|
| 108 | this->soundBuffer_moving = OrxSound::ResourceSoundBuffer(fileName); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void Mover::setClosingSoundFile(const std::string& fileName) |
---|
| 112 | { |
---|
| 113 | this->soundBuffer_closing = OrxSound::ResourceSoundBuffer(fileName); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void Mover::setClosedSoundFile(const std::string& fileName) |
---|
| 117 | { |
---|
| 118 | this->soundBuffer_closed = OrxSound::ResourceSoundBuffer(fileName); |
---|
| 119 | } |
---|
| 120 | |
---|
[10423] | 121 | /** |
---|
| 122 | * tick function |
---|
| 123 | */ |
---|
| 124 | void Mover::tick(float dt) |
---|
| 125 | { |
---|
| 126 | if (this->state == Closed) |
---|
| 127 | { |
---|
| 128 | if (this->checkPlayerInActionRadius()) |
---|
| 129 | { |
---|
| 130 | this->state = Opening; |
---|
[10487] | 131 | if (this->soundBuffer_opening.loaded()) |
---|
| 132 | this->soundSource_starting.play(this->soundBuffer_opening); |
---|
| 133 | if (this->soundBuffer_moving.loaded()) |
---|
| 134 | this->soundSource_moving.play(this->soundBuffer_moving, 1.0, true); |
---|
[10423] | 135 | } |
---|
| 136 | } |
---|
| 137 | else if (this->state == Opening) |
---|
| 138 | { |
---|
| 139 | if (this->checkOpen(dt)) |
---|
| 140 | { |
---|
| 141 | this->state = Open; |
---|
| 142 | this->setAbsCoor(this->originCoordinates + this->targetCoordinates); |
---|
[10487] | 143 | if (this->soundBuffer_opened.loaded()) |
---|
| 144 | this->soundSource_ending.play(this->soundBuffer_opened); |
---|
| 145 | if (this->soundBuffer_moving.loaded()) |
---|
| 146 | this->soundSource_moving.stop(); |
---|
[10423] | 147 | } |
---|
| 148 | } |
---|
| 149 | else if (this->state == Open) |
---|
| 150 | { |
---|
| 151 | if (!this->checkPlayerInActionRadius()) |
---|
| 152 | { |
---|
| 153 | this->state = Closing; |
---|
[10487] | 154 | if (this->soundBuffer_closing.loaded()) |
---|
| 155 | this->soundSource_starting.play(this->soundBuffer_closing); |
---|
| 156 | if (this->soundBuffer_moving.loaded()) |
---|
| 157 | this->soundSource_moving.play(this->soundBuffer_moving, 1.0, true); |
---|
[10423] | 158 | } |
---|
| 159 | } |
---|
| 160 | else if (this->state == Closing) |
---|
| 161 | { |
---|
| 162 | if (this->checkClosed(dt)) |
---|
| 163 | { |
---|
| 164 | this->state = Closed; |
---|
| 165 | this->setAbsCoor(this->originCoordinates); |
---|
[10487] | 166 | if (this->soundBuffer_closed.loaded()) |
---|
| 167 | this->soundSource_ending.play(this->soundBuffer_closed); |
---|
| 168 | if (this->soundBuffer_moving.loaded()) |
---|
| 169 | this->soundSource_moving.stop(); |
---|
[10423] | 170 | } |
---|
| 171 | if (this->checkPlayerInActionRadius()) |
---|
| 172 | { |
---|
| 173 | this->state = Opening; |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if (this->state == Opening) |
---|
| 178 | { |
---|
| 179 | this->shiftCoor(this->targetCoordinates / this->actionTime * dt); |
---|
| 180 | } |
---|
| 181 | else if (this->state == Closing) |
---|
| 182 | { |
---|
| 183 | this->shiftCoor(this->targetCoordinates * (-1) / this->actionTime * dt); |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * checks if the door is open |
---|
| 189 | */ |
---|
| 190 | bool Mover::checkOpen(float dt) |
---|
| 191 | { |
---|
| 192 | if (fabs((this->originCoordinates - (this->getAbsCoor() - this->targetCoordinates)).len()) < fabs(2 * (this->targetCoordinates / this->actionTime * dt).len())) |
---|
| 193 | return true; |
---|
| 194 | |
---|
| 195 | return false; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * checks if the door is closed |
---|
| 200 | */ |
---|
| 201 | bool Mover::checkClosed(float dt) |
---|
| 202 | { |
---|
| 203 | if (fabs((this->getAbsCoor() - this->originCoordinates).len()) < fabs(2 * (this->targetCoordinates / this->actionTime * dt).len())) |
---|
| 204 | return true; |
---|
| 205 | |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | #include "playable.h" |
---|
| 210 | #include "generic_npc.h" |
---|
| 211 | /** |
---|
| 212 | * checks if the player is within the action radius |
---|
| 213 | */ |
---|
| 214 | bool Mover::checkPlayerInActionRadius() |
---|
| 215 | { |
---|
[10487] | 216 | WorldEntity* entity; |
---|
| 217 | float distance; |
---|
[10423] | 218 | |
---|
[10487] | 219 | for (ObjectList<Playable>::const_iterator it = Playable::objectList().begin(); |
---|
| 220 | it != Playable::objectList().end(); |
---|
| 221 | ++it) |
---|
| 222 | // for all players |
---|
| 223 | { |
---|
| 224 | entity = (*it); |
---|
| 225 | distance = (this->originCoordinates - entity->getAbsCoor()).len(); |
---|
| 226 | if (distance < this->actionRadius) |
---|
| 227 | { |
---|
[10519] | 228 | this->soundSource_starting.setSourceNode((PNode*)State::getCamera()); // bad hack! |
---|
| 229 | this->soundSource_moving.setSourceNode((PNode*)State::getCamera()); // TODO: make the sound louder without |
---|
| 230 | this->soundSource_ending.setSourceNode((PNode*)State::getCamera()); // attaching him to the player |
---|
[10487] | 231 | return true; |
---|
| 232 | } |
---|
| 233 | } |
---|
[10423] | 234 | |
---|
[10487] | 235 | for (ObjectList<GenericNPC>::const_iterator it = GenericNPC::objectList().begin(); |
---|
| 236 | it != GenericNPC::objectList().end(); |
---|
| 237 | ++it) |
---|
| 238 | { |
---|
| 239 | entity = (*it); |
---|
| 240 | distance = (this->originCoordinates - entity->getAbsCoor()).len(); |
---|
| 241 | if (distance < this->actionRadius) |
---|
| 242 | return true; |
---|
| 243 | } |
---|
[10423] | 244 | |
---|
[10487] | 245 | return false; |
---|
[10423] | 246 | } |
---|