Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptimprovements/src/world_entities/npcs/mover.cc @ 10620

Last change on this file since 10620 was 10519, checked in by patrick, 18 years ago

emitter fix

File size: 7.5 KB
Line 
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*/
15
16#include "sound/resource_sound_buffer.h"
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
19#include "debug.h"
20
21#include "mover.h"
22#include "state.h"
23#include "camera.h"
24
25
26ObjectListDefinition(Mover);
27CREATE_FACTORY(Mover);
28
29/**
30 *  initializes the Mover from a XmlElement
31*/
32Mover::Mover(const TiXmlElement* root)
33{
34    this->registerObject(this, Mover::_objectList);
35    this->toList(OM_ENVIRON);
36
37    this->targetCoordinates = Vector(0, 0, 0);
38    this->originCoordinates = Vector(0, 87, -256);
39    this->actionRadius = 40.0;
40    this->actionTime = 1.0;
41
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);
56}
57
58Mover::~Mover()
59{
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();
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 */
72void Mover::loadParams(const TiXmlElement* root)
73{
74    WorldEntity::loadParams(root);
75
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");
94}
95
96void Mover::setOpeningSoundFile(const std::string& fileName)
97{
98    this->soundBuffer_opening = OrxSound::ResourceSoundBuffer(fileName);
99}
100
101void Mover::setOpenedSoundFile(const std::string& fileName)
102{
103    this->soundBuffer_opened = OrxSound::ResourceSoundBuffer(fileName);
104}
105
106void Mover::setMovingSoundFile(const std::string& fileName)
107{
108    this->soundBuffer_moving = OrxSound::ResourceSoundBuffer(fileName);
109}
110
111void Mover::setClosingSoundFile(const std::string& fileName)
112{
113    this->soundBuffer_closing = OrxSound::ResourceSoundBuffer(fileName);
114}
115
116void Mover::setClosedSoundFile(const std::string& fileName)
117{
118    this->soundBuffer_closed = OrxSound::ResourceSoundBuffer(fileName);
119}
120
121/**
122 * tick function
123 */
124void Mover::tick(float dt)
125{
126    if (this->state == Closed)
127    {
128        if (this->checkPlayerInActionRadius())
129        {
130            this->state = Opening;
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);
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);
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();
147        }
148    }
149    else if (this->state == Open)
150    {
151        if (!this->checkPlayerInActionRadius())
152        {
153            this->state = Closing;
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);
158        }
159    }
160    else if (this->state == Closing)
161    {
162        if (this->checkClosed(dt))
163        {
164            this->state = Closed;
165            this->setAbsCoor(this->originCoordinates);
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();
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 */
190bool 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 */
201bool 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 */
214bool Mover::checkPlayerInActionRadius()
215{
216    WorldEntity* entity;
217    float distance;
218
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        {
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
231            return true;
232        }
233    }
234
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    }
244
245    return false;
246}
Note: See TracBrowser for help on using the repository browser.