1 /*
2 Copyright (C) 2009-2021 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, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19 
20 #include "../headers.h"
21 
22 #include "../audio/audio.h"
23 #include "../collisions.h"
24 #include "../entity.h"
25 #include "../graphics/animation.h"
26 #include "../hud.h"
27 #include "../inventory.h"
28 #include "../system/error.h"
29 #include "../system/properties.h"
30 #include "tesla_pack.h"
31 
32 extern Entity *self;
33 
34 static void init(void);
35 static void entityWait(void);
36 static void recharge(void);
37 static void setChargeState(void);
38 static void activate(int);
39 static void touch(Entity *);
40 
addTeslaCharger(int x,int y,char * name)41 Entity *addTeslaCharger(int x, int y, char *name)
42 {
43 	Entity *e = getFreeEntity();
44 
45 	if (e == NULL)
46 	{
47 		showErrorAndExit("No free slots to add a Tesla Charger");
48 	}
49 
50 	loadProperties(name, e);
51 
52 	e->x = x;
53 	e->y = y;
54 
55 	e->type = KEY_ITEM;
56 
57 	e->face = RIGHT;
58 
59 	e->action = &init;
60 
61 	e->touch = &touch;
62 
63 	e->activate = &activate;
64 
65 	e->draw = &drawLoopingAnimationToMap;
66 
67 	setEntityAnimation(e, "STAND");
68 
69 	return e;
70 }
71 
init()72 static void init()
73 {
74 	setChargeState();
75 }
76 
recharge()77 static void recharge()
78 {
79 	self->thinkTime--;
80 
81 	if (self->thinkTime <= 0)
82 	{
83 		self->health++;
84 
85 		setChargeState();
86 
87 		playSoundToMap("sound/item/charge_beep", -1, self->x, self->y, 0);
88 
89 		self->thinkTime = 180;
90 	}
91 
92 	checkToMap(self);
93 }
94 
entityWait()95 static void entityWait()
96 {
97 	checkToMap(self);
98 }
99 
setChargeState()100 static void setChargeState()
101 {
102 	switch (self->health)
103 	{
104 		case 0:
105 			setEntityAnimation(self, "STAND");
106 			self->action = &recharge;
107 		break;
108 
109 		case 1:
110 			setEntityAnimation(self, "WALK");
111 			self->action = &recharge;
112 		break;
113 
114 		case 2:
115 			setEntityAnimation(self, "JUMP");
116 			self->action = &recharge;
117 		break;
118 
119 		case 3:
120 			setEntityAnimation(self, "PAIN");
121 			self->action = &entityWait;
122 		break;
123 
124 		default:
125 			setEntityAnimation(self, "DIE");
126 			self->action = &entityWait;
127 		break;
128 	}
129 }
130 
activate(int val)131 static void activate(int val)
132 {
133 	Entity *e;
134 
135 	if (self->health == 3)
136 	{
137 		e = addTeslaPack(0, 0, "item/tesla_pack_full");
138 
139 		addToInventory(e);
140 
141 		self->health = -1;
142 	}
143 
144 	else
145 	{
146 		e = getInventoryItemByObjectiveName("Tesla Pack");
147 
148 		if (e != NULL && e->health == 0)
149 		{
150 			removeInventoryItemByObjectiveName(e->objectiveName);
151 
152 			self->health = 0;
153 
154 			self->thinkTime = 180;
155 		}
156 	}
157 
158 	setChargeState();
159 }
160 
touch(Entity * other)161 static void touch(Entity *other)
162 {
163 	Entity *e;
164 
165 	if (other->type == PLAYER)
166 	{
167 		if (self->health == 3)
168 		{
169 			setInfoBoxMessage(0, 255, 255, 255, _("Press Action to retrieve Tesla Pack"));
170 		}
171 
172 		else if (self->health == -1)
173 		{
174 			e = getInventoryItemByObjectiveName("Tesla Pack");
175 
176 			if (e != NULL && e->health == 0)
177 			{
178 				setInfoBoxMessage(0, 255, 255, 255, _("Press Action to replace Tesla Pack"));
179 			}
180 		}
181 	}
182 }
183