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