]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/switches.cpp
Updated PNG icon files.
[quix0rs-blobwars.git] / src / switches.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 "switches.h"
22
23 void checkSwitchContact(Entity *ent)
24 {
25         Switch *swt = (Switch*)map.switchList.getHead();
26         bool okayToActivate = false;
27
28         char message[256];
29
30         while (swt->next != NULL)
31         {
32                 swt = (Switch*)swt->next;
33                 
34                 if (swt->type == SWT_USED)
35                         continue;
36
37                 if (swt->type == SWT_NORMAL)
38                 {
39                         if ((ent != &player) && (!(ent->flags & ENT_BULLET)))
40                                 continue;
41
42                         if ((ent->flags & ENT_BULLET) && (ent->id != WP_GRENADES))
43                                 continue;
44                 }
45                 
46                 if ((swt->type == SWT_RESET) && (ent != &player))
47                         continue;
48                         
49                 if ((swt->type == SWT_WATERLEVEL) && (ent != &player))
50                         continue;
51
52                 if (Collision::collision(ent, swt))
53                 {
54                         okayToActivate = false;
55
56                         if (strcmp(swt->requiredObjectName, "@none@") == 0)
57                         {
58                                 okayToActivate = true;
59                         }
60                         else if (ent == &player)
61                         {
62                                 if (carryingItem(swt->requiredObjectName))
63                                 {
64                                         okayToActivate = true;
65                                         
66                                         if (swt->type == SWT_PRESSURE)
67                                         {
68                                                 strlcpy(swt->requiredObjectName, "@none@", sizeof swt->requiredObjectName);
69                                         }
70                                         
71                                         checkObjectives(swt->name, true);
72                                         strlcpy(swt->name, "Switch", sizeof swt->name);
73                                 }
74                                 else
75                                 {
76                                         snprintf(message, sizeof message, "%s required", swt->requiredObjectName);
77                                         engine.setInfoMessage(message, 1, INFO_HINT);
78                                 }
79                         }
80
81                         if (okayToActivate)
82                         {       
83                                 if (swt->health == 0)
84                                 {
85                                         if ((swt->type == SWT_NORMAL) || (swt->type == SWT_WATERLEVEL))
86                                         {
87                                                 audio.playSound(SND_SWITCH1, CH_TOUCH);
88                                                 swt->activated = !swt->activated;
89                                                 activateTrigger(swt->linkName, swt->activateMessage, swt->activated);
90                                                 swt->health = 1;
91                                                 swt->type = SWT_USED;
92                                         }
93                                         else if (swt->type == SWT_TOGGLE)
94                                         {
95                                                 audio.playSound(SND_SWITCH1, CH_TOUCH);
96                                                 activateTrigger(swt->linkName, swt->activateMessage, !swt->activated);
97                                                 swt->activated = !swt->activated;
98                                         }
99                                         else if (swt->type == SWT_PRESSURE)
100                                         {
101                                                 audio.playSound(SND_SWITCH1, CH_TOUCH);
102                                                 swt->activated = true;
103                                                 activateTrigger(swt->linkName, swt->activateMessage, true);
104                                                 swt->health = 2;
105                                         }
106                                         else if ((swt->type == SWT_TIMED) || (swt->type == SWT_RESET))
107                                         {
108                                                 audio.playSound(SND_SWITCH1, CH_TOUCH);
109                                                 activateTrigger(swt->linkName, swt->activateMessage, true);
110                                                 swt->activated = !swt->activated;
111                                                 swt->health = 240;
112                                         }
113                                 }
114
115                                 if ((swt->type == SWT_TOGGLE) || (swt->type == SWT_PRESSURE))
116                                         swt->health = 2;
117                         }
118                 }
119         }
120 }
121
122 void doSwitches()
123 {
124         Switch *swt = (Switch*)map.switchList.getHead();
125
126         int x, y, absX, absY;
127
128         while (swt->next != NULL)
129         {
130                 swt = (Switch*)swt->next;
131
132                 x = (int)(swt->x - engine.playerPosX);
133                 y = (int)(swt->y - engine.playerPosY);
134
135                 absX = abs(x);
136                 absY = abs(y);
137
138                 if ((absX < 800) && (absY < 600))
139                 {
140                         if (!swt->activated)
141                                 graphics.blit(graphics.getSprite("SwitchOff", true)->getCurrentFrame(), x, y, graphics.screen, false);
142                         else
143                                 graphics.blit(graphics.getSprite("SwitchOn", true)->getCurrentFrame(), x, y, graphics.screen, false);
144                 }
145
146                 if ((swt->type != SWT_NORMAL) && (swt->type != SWT_WATERLEVEL) && (swt->type != SWT_USED))
147                 {
148                         if (swt->health > 0)
149                         {
150                                 swt->health--;
151                                 if (swt->health == 0)
152                                 {
153                                         if (swt->type != SWT_TOGGLE)
154                                         {
155                                                 swt->activated = false;
156                                                 activateTrigger(swt->linkName, swt->activateMessage, false);
157                                         }
158                                 }
159                         }
160                 }
161         }
162 }