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