]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CTrap.cpp
Update copyright statements.
[quix0rs-blobwars.git] / src / CTrap.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "headers.h"
23
24 Trap::Trap()
25 {
26         currentAction = TRAP_WAIT2;
27         active = true;
28         thinktime = 60;
29         x = y = dx = dy = 0;
30 }
31
32 void Trap::setName(const char *name)
33 {
34         strlcpy(this->name, name, sizeof this->name);
35 }
36
37 void Trap::setTrapType(int type)
38 {
39         this->type = type;
40 }
41
42 void Trap::setSpeed(int speed)
43 {
44         this->speed = speed;
45 }
46
47 void Trap::setWaitTimes(int time1, int time2)
48 {
49         thinktime = time1;
50         this->waitTime[0] = time1;
51         this->waitTime[1] = time2;
52 }
53
54 void Trap::setSprite(Sprite *sprite)
55 {
56         this->sprite = sprite;
57
58         this->width = sprite->image[0]->w;
59         this->height = sprite->image[0]->h;
60 }
61
62 void Trap::setDestinations(int startX, int startY, int endX, int endY)
63 {
64         this->startX = startX;
65         this->startY = startY;
66         this->endX = endX;
67         this->endY = endY;
68         
69         x = endX;
70         y = endY;
71         
72         if (type == TRAP_TYPE_SWING)
73         {
74                 x = startX;
75                 y = startY;
76                 speed = Math::rrand(-2, 2);
77                 dx = 0.05;
78                 dy = Math::rrand(0, 45);
79         }
80 }
81
82 void Trap::setDamage(int damage)
83 {
84         this->damage = damage;
85 }
86
87 bool Trap::performSwingAction()
88 {
89         if (!active)
90         {
91                 x = startX;
92                 y = startY + endY;
93                 return false;
94         }
95
96         x = (0 * cos((dy * PI) / 180)) - (1 * sin((dy * PI) / 180));
97         y = (1 * cos((dy * PI) / 180)) + (0 * sin((dy * PI) / 180));
98
99         x *= endX;
100         y *= endY;
101
102         x += startX;
103         y += startY;
104
105         speed += dx;
106
107         Math::limitFloat(&speed, -2.5, 2.5);
108
109         dy += speed;
110
111         if (dy <= -10)
112         {
113                 dx = 0.05;
114         }
115         else if (dy >= 10)
116         {
117                 dx = -0.05;
118         }
119
120         if (((int)dy >= -2) && ((int)dy <= 2))
121                 return true;
122
123         return false;
124 }
125
126 bool Trap::performSpikeAction()
127 {
128         if (currentAction == TRAP_FIRSTACTION)
129         {
130                 if (!active)
131                         return false;
132
133                 Math::limitFloat(&(x -= speed), startX, endX);
134                 Math::limitFloat(&(y -= speed), startY, endY);
135
136                 if ((x == startX) && (y == startY))
137                 {
138                         Math::wrapChar(&(++currentAction), 0, 3);
139                         thinktime = waitTime[0];
140                 }
141         }
142
143         if (currentAction == TRAP_SECONDACTION)
144         {
145                 Math::limitFloat(&(++x), startX, endX);
146                 Math::limitFloat(&(++y), startY, endY);
147
148                 if ((x == endX) && (y == endY))
149                 {
150                         Math::wrapChar(&(++currentAction), 0, 3);
151                         thinktime = waitTime[1];
152                 }
153         }
154         
155         return false;
156 }
157
158 bool Trap::performBarrierAction()
159 {
160         thinktime--;
161
162         if (thinktime == 0)
163         {
164                 currentAction = TRAP_WAIT2;
165                 thinktime = waitTime[1];
166                 if (thinktime == 0)
167                 {
168                         currentAction = TRAP_FIRSTACTION;
169                         thinktime = waitTime[0];
170                 }
171         }
172
173         if (thinktime >= waitTime[0] - 3)
174                 return true;
175
176         return false;
177 }
178
179 bool Trap::performFlameAction()
180 {
181         thinktime--;
182
183         if (thinktime == 0)
184         {
185                 currentAction = TRAP_WAIT2;
186                 thinktime = waitTime[1];
187                 if (thinktime == 0)
188                 {
189                         currentAction = TRAP_FIRSTACTION;
190                         thinktime = waitTime[0];
191                 }
192         }
193
194         if (thinktime >= waitTime[0] - 3)
195                 return true;
196
197         return false;
198 }
199
200 bool Trap::think()
201 {
202         if (type == TRAP_TYPE_MINE)
203         {
204                 dy += 0.5;
205                 y += dy;
206                 return false;
207         }
208
209         if ((currentAction == TRAP_WAIT1) || (currentAction == TRAP_WAIT2))
210         {
211                 thinktime--;
212
213                 if (thinktime > 0)
214                         return false;
215
216                 Math::wrapChar(&(++currentAction), 0, 3);
217                 thinktime = 60;
218                 
219                 if ((type == TRAP_TYPE_BARRIER) || (type == TRAP_TYPE_FLAME))
220                         thinktime = waitTime[0];
221         }
222         else
223         {
224                 switch (type)
225                 {
226                         case TRAP_TYPE_SPIKE:
227                                 return performSpikeAction();
228                                 break;
229                         case TRAP_TYPE_SWING:
230                                 return performSwingAction();
231                                 break;
232                         case TRAP_TYPE_BARRIER:
233                                 return performBarrierAction();
234                                 break;
235                         case TRAP_TYPE_FLAME:
236                                 return performBarrierAction();
237                                 break;
238                 }
239         }
240         
241         return false;
242 }