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