Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/objects/quest/QuestEffectBeacon.cc @ 2209

Last change on this file since 2209 was 2209, checked in by dafrick, 15 years ago

Resolved some issues with triggers.

File size: 5.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "QuestEffectBeacon.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "core/Event.h"
35#include "core/EventIncludes.h"
36
37#include "orxonox/objects/infos/PlayerInfo.h"
38#include "orxonox/objects/worldentities/ControllableEntity.h"
39#include "orxonox/objects/worldentities/triggers/PlayerTrigger.h"
40#include "QuestEffect.h"
41
42namespace orxonox {
43
44    CreateFactory(QuestEffectBeacon);
45
46    QuestEffectBeacon::QuestEffectBeacon(BaseObject* creator) : PositionableEntity(creator)
47    {
48        RegisterObject(QuestEffectBeacon);
49       
50        this->status_ = QuestEffectBeaconStatus::active;
51        this->times_ = -1;
52        this->trigger_ = NULL;
53    }
54
55    QuestEffectBeacon::~QuestEffectBeacon()
56    {
57    }
58   
59    void QuestEffectBeacon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(QuestEffectBeacon, XMLPort, xmlelement, mode);
62
63        XMLPortParam(QuestEffectBeacon, "times", setTimes, getTimes, xmlelement, mode);
64        XMLPortObject(QuestEffectBeacon, QuestEffect, "", addEffect, getEffect, xmlelement, mode);
65        XMLPortObject(QuestEffectBeacon, PlayerTrigger, "", addTrigger, getTrigger, xmlelement, mode);
66    }
67   
68    void QuestEffectBeacon::processEvent(Event& event)
69    {
70        SetSubclassEvent(QuestEffectBeacon, "execute", execute, event, PlayerTrigger);
71    }
72   
73    bool QuestEffectBeacon::execute(bool b, PlayerTrigger* trigger)
74    {
75        if(!b)
76        {
77            COUT(2) << "b is false." << std::endl;
78        }
79        if(!(this->isActive()))
80        {
81            COUT(3) << "The QuestEffectBeacon is inactive." << std::endl;
82            return false;
83        }
84
85        ControllableEntity* entity = trigger->getTriggeringPlayer();
86
87        if(entity == NULL)
88        {
89            COUT(2) << "No one triggered the beacon? Curious!" << std::endl;
90            return false;
91        }
92       
93        PlayerInfo* player = entity->getPlayer();
94       
95        if(player == NULL)
96        {
97            COUT(3) << "The PlayerInfo* is NULL." << std::endl;
98            return false;
99        }
100
101        COUT(3) << "QuestEffectBeacon executed on player: " << player << " ." << std::endl;       
102
103        bool check = QuestEffect::invokeEffects(player, this->effects_);
104        if(check)
105        {
106            this->decrementTimes();
107            return true;
108        }
109       
110        return false;
111    }
112   
113    bool QuestEffectBeacon::isActive(void)
114    {
115        return this->status_ == QuestEffectBeaconStatus::active;
116    }
117   
118    bool QuestEffectBeacon::decrementTimes(void)
119    {
120        if(!(this->isActive()))
121        {
122            return false;
123        }
124        if(this->getTimes() == -1)
125        {
126            return true;
127        }
128       
129        this->times_ = this->times_ - 1;
130        if(this->getTimes() == 0)
131        {
132            this->status_ = QuestEffectBeaconStatus::inactive;
133        }
134       
135        return true;
136    }
137   
138   
139    bool QuestEffectBeacon::setTimes(const int & n)
140    {
141        if(n < -1)
142        {
143            return false;
144        }
145       
146        this->times_ = n;
147        return true;
148    }
149   
150   
151    /**
152    @brief
153
154    */
155    bool QuestEffectBeacon::addEffect(QuestEffect* effect)
156    {
157        if(effect == NULL)
158        {
159            COUT(2) << "A NULL-QuestEffect was trying to be added" << std::endl;
160            return false;
161        }
162
163        this->effects_.push_back(effect);
164
165        COUT(3) << "A QuestEffect was added to a QuestEffectBeacon." << std::endl;
166        return true;
167    }
168   
169    bool QuestEffectBeacon::addTrigger(PlayerTrigger* trigger)
170    {
171        if(this->trigger_ != NULL)
172        {
173           COUT(2) << "A Trigger was trying to be added, where one was already set." << std::endl;
174           return false;
175        }
176        if(trigger == NULL)
177        {
178            COUT(2) << "A NULL-Trigger was trying to be added." << std::endl;
179            return false;
180        }
181       
182        COUT(3) << "A Trigger was added to a QuestEffectBeacon." << std::endl;
183        this->trigger_ = trigger;
184        return true;
185    }
186   
187     /**
188    @brief
189
190    */
191    const QuestEffect* QuestEffectBeacon::getEffect(unsigned int index) const
192    {
193        int i = index;
194        for (std::list<QuestEffect*>::const_iterator effect = this->effects_.begin(); effect != this->effects_.end(); ++effect)
195        {
196            if(i == 0)
197            {
198               return *effect;
199            }
200            i--;
201        }
202        return NULL;
203    }
204
205    const PlayerTrigger* QuestEffectBeacon::getTrigger(unsigned int index) const
206    {
207        if(index == 0)
208        {
209            return this->trigger_;
210        }
211       
212        return NULL;
213    }
214
215}
Note: See TracBrowser for help on using the repository browser.