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 "../entity.h"
24 #include "../graphics/animation.h"
25 #include "../system/error.h"
26 #include "../system/properties.h"
27 
28 extern Entity *self;
29 
30 static void swing(void);
31 static int ringDraw(void);
32 static void init(void);
33 
addPendulum(int x,int y,char * name)34 Entity *addPendulum(int x, int y, char *name)
35 {
36 	Entity *e = getFreeEntity();
37 
38 	if (e == NULL)
39 	{
40 		showErrorAndExit("No free slots to add a Pendulum");
41 	}
42 
43 	loadProperties(name, e);
44 
45 	e->x = x;
46 	e->y = y;
47 
48 	e->action = &init;
49 	e->draw = &drawLoopingAnimationToMap;
50 	e->touch = &entityTouch;
51 
52 	e->type = ENEMY;
53 
54 	setEntityAnimation(e, "STAND");
55 
56 	return e;
57 }
58 
init()59 static void init()
60 {
61 	Entity *e = getFreeEntity();
62 
63 	if (e == NULL)
64 	{
65 		showErrorAndExit("No free slots to add a Pendulum Ring");
66 	}
67 
68 	loadProperties("enemy/pendulum_ring", e);
69 
70 	setEntityAnimation(e, "STAND");
71 
72 	e->x = self->startX + self->w / 2 - e->w / 2;
73 	e->y = self->startY;
74 
75 	e->startX = e->x;
76 	e->startY = e->y;
77 
78 	e->action = &doNothing;
79 	e->draw = &ringDraw;
80 
81 	e->type = ENEMY;
82 
83 	e->head = self;
84 
85 	self->action = &swing;
86 }
87 
swing()88 static void swing()
89 {
90 	float x, y, radians, origX;
91 
92 	x = 0;
93 	y = self->endY - self->startY;
94 
95 	self->thinkTime += 2;
96 
97 	radians = DEG_TO_RAD(self->thinkTime);
98 
99 	self->dirX = (self->speed * cos(radians) - 0 * sin(radians));
100 
101 	self->endX += self->dirX;
102 
103 	radians = DEG_TO_RAD(self->endX);
104 
105 	origX = self->x;
106 
107 	self->x = (x * cos(radians) - y * sin(radians));
108 	self->y = (x * sin(radians) + y * cos(radians));
109 
110 	self->x += self->startX;
111 	self->y += self->startY;
112 
113 	if ((origX < self->startX && self->x >= self->startX) || (origX > self->startX && self->x <= self->startX))
114 	{
115 		playSoundToMap("sound/enemy/pendulum/swing", -1, self->x, self->y, 0);
116 	}
117 }
118 
ringDraw()119 static int ringDraw()
120 {
121 	int i, endX, endY;
122 	float diffX, diffY;
123 
124 	endX = self->head->x;
125 	endY = self->head->y + self->head->h / 2 - self->h / 2;
126 
127 	diffX = (endX - self->head->startX) / self->head->mental;
128 	diffY = (endY - self->head->startY) / self->head->mental;
129 
130 	drawLoopingAnimationToMap();
131 
132 	for (i=0;i<self->head->mental;i++)
133 	{
134 		drawSpriteToMap();
135 
136 		self->x += diffX;
137 		self->y += diffY;
138 	}
139 
140 	self->x = self->startX;
141 	self->y = self->startY;
142 
143 	return 1;
144 }
145