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 "../geometry.h"
26 #include "../graphics/animation.h"
27 #include "../system/error.h"
28 #include "../system/properties.h"
29 #include "../system/random.h"
30 
31 extern Entity *self;
32 
33 static void entityWait(void);
34 static void init(void);
35 static void moveToTarget(void);
36 static void changeRandomTarget(Entity *);
37 static void creditsMove(void);
38 
addFlyingBug(int x,int y,char * name)39 Entity *addFlyingBug(int x, int y, char *name)
40 {
41 	Entity *e = getFreeEntity();
42 
43 	if (e == NULL)
44 	{
45 		showErrorAndExit("No free slots to add a Flying Bug");
46 	}
47 
48 	loadProperties(name, e);
49 
50 	e->x = x;
51 	e->y = y;
52 
53 	e->action = &init;
54 
55 	e->draw = &drawLoopingAnimationToMap;
56 	e->touch = &entityTouch;
57 	e->die = &entityDie;
58 	e->takeDamage = &entityTakeDamageNoFlinch;
59 	e->reactToBlock = &changeRandomTarget;
60 
61 	e->creditsAction = &creditsMove;
62 
63 	e->type = ENEMY;
64 
65 	setEntityAnimation(e, "STAND");
66 
67 	return e;
68 }
69 
init()70 static void init()
71 {
72 	self->thinkTime = 0;
73 
74 	if (self->mental == 0)
75 	{
76 		playSoundToMap("sound/enemy/bug/buzz", -1, self->x, self->y, 0);
77 
78 		self->mental = 1;
79 	}
80 
81 	self->action = &entityWait;
82 
83 	self->action();
84 }
85 
entityWait()86 static void entityWait()
87 {
88 	self->thinkTime--;
89 
90 	if (self->thinkTime <= 0)
91 	{
92 		changeRandomTarget(NULL);
93 
94 		self->action = &moveToTarget;
95 	}
96 }
97 
changeRandomTarget(Entity * other)98 static void changeRandomTarget(Entity *other)
99 {
100 	self->dirX = 0;
101 }
102 
moveToTarget()103 static void moveToTarget()
104 {
105 	float x, y;
106 
107 	if (self->dirX == 0 || self->dirY == 0)
108 	{
109 		self->targetX = self->x + (prand() % 64) * (prand() % 2 == 0 ? -1 : 1);
110 		self->targetY = self->y + (prand() % 64) * (prand() % 2 == 0 ? -1 : 1);
111 
112 		calculatePath(self->x, self->y, self->targetX, self->targetY, &self->dirX, &self->dirY);
113 
114 		self->dirX *= self->speed;
115 		self->dirY *= self->speed;
116 	}
117 
118 	x = self->dirX;
119 	y = self->dirY;
120 
121 	checkToMap(self);
122 
123 	if (atTarget())
124 	{
125 		self->thinkTime = 120 + (prand() % self->maxThinkTime);
126 
127 		self->action = &entityWait;
128 	}
129 
130 	else if (self->dirX != x || self->dirY != y)
131 	{
132 		changeRandomTarget(NULL);
133 	}
134 }
135 
creditsMove()136 static void creditsMove()
137 {
138 	setEntityAnimation(self, "STAND");
139 
140 	self->dirX = self->speed;
141 
142 	checkToMap(self);
143 
144 	if (self->dirX == 0)
145 	{
146 		self->inUse = FALSE;
147 	}
148 }
149