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 | * Benjamin Knecht |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Trigger.h" |
---|
30 | |
---|
31 | #include "core/CoreIncludes.h" |
---|
32 | |
---|
33 | namespace orxonox |
---|
34 | { |
---|
35 | CreateFactory(Trigger); |
---|
36 | |
---|
37 | Trigger::Trigger() |
---|
38 | { |
---|
39 | RegisterObject(Trigger); |
---|
40 | |
---|
41 | targetMask_.exclude(Class(BaseObject)); |
---|
42 | } |
---|
43 | |
---|
44 | Trigger::~Trigger() |
---|
45 | { |
---|
46 | } |
---|
47 | |
---|
48 | bool Trigger::isTriggered() |
---|
49 | { |
---|
50 | return this->isTriggered(this->mode_); |
---|
51 | } |
---|
52 | |
---|
53 | bool Trigger::isTriggered(TriggerMode mode) |
---|
54 | { |
---|
55 | switch(mode) |
---|
56 | { |
---|
57 | case TM_EventTriggerAnd: |
---|
58 | return checkAnd(); |
---|
59 | break; |
---|
60 | case TM_EventTriggerOr: |
---|
61 | return checkOr(); |
---|
62 | break; |
---|
63 | case TM_DelayTrigger: |
---|
64 | return checkDelay(); |
---|
65 | break; |
---|
66 | case TM_DistanceTrigger: |
---|
67 | return checkDistance(); |
---|
68 | break; |
---|
69 | case TM_DistanceEventTriggerAnd: |
---|
70 | if (checkDistance() && checkAnd()) |
---|
71 | return true; |
---|
72 | else |
---|
73 | return false; |
---|
74 | break; |
---|
75 | case TM_DistanceEventTriggerOr: |
---|
76 | if (checkDistance() && checkOr()) |
---|
77 | return true; |
---|
78 | else |
---|
79 | return false; |
---|
80 | break; |
---|
81 | default: |
---|
82 | return false; |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void Trigger::addTrigger(Trigger* trig) |
---|
88 | { |
---|
89 | if (this != trig) |
---|
90 | this->triggers_.insert(trig); |
---|
91 | } |
---|
92 | |
---|
93 | void Trigger::addTargets(std::string targets) |
---|
94 | { |
---|
95 | Identifier* targetId = ID(targets); |
---|
96 | targetMask_.include(targetId); |
---|
97 | // trigger shouldn't react on itself or other triggers |
---|
98 | targetMask_.exclude(Class(Trigger), true); |
---|
99 | } |
---|
100 | |
---|
101 | bool Trigger::checkAnd() |
---|
102 | { |
---|
103 | std::set<Trigger*>::iterator it; |
---|
104 | for(it = this->triggers_.begin(); it != this->triggers_.end(); it++) |
---|
105 | { |
---|
106 | if(!((*it)->isTriggered())) |
---|
107 | return false; |
---|
108 | } |
---|
109 | return true; |
---|
110 | } |
---|
111 | |
---|
112 | bool Trigger::checkOr() |
---|
113 | { |
---|
114 | std::set<Trigger*>::iterator it; |
---|
115 | for(it = this->triggers_.begin(); it != this->triggers_.end(); it++) |
---|
116 | { |
---|
117 | if((*it)->isTriggered()) |
---|
118 | return true; |
---|
119 | } |
---|
120 | return false; |
---|
121 | } |
---|
122 | |
---|
123 | bool Trigger::checkDelay() |
---|
124 | { |
---|
125 | if (triggingTime_ < actualTime_) |
---|
126 | return true; |
---|
127 | else |
---|
128 | return false; |
---|
129 | } |
---|
130 | |
---|
131 | bool Trigger::checkDistance() |
---|
132 | { |
---|
133 | // Iterate through all WorldEntities |
---|
134 | for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::begin(); it; it++) |
---|
135 | { |
---|
136 | Vector3 distanceVec = it->getNode()->getWorldPosition() - this->getNode()->getWorldPosition(); |
---|
137 | if (distanceVec.length() < radius_) |
---|
138 | return true; |
---|
139 | } |
---|
140 | return false; |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | } |
---|