aboutsummaryrefslogtreecommitdiff
path: root/src/enemies/jellyfish.c
blob: def3198966651acbbde8f0a15041443089386386 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "jellyfish.h"
#include "../enemy.h"
#include "../game.h"
#include "../hero.h"
#include <stdlib.h>
#include <math.h>

void jellyfishStep(Jellyfish* j);
void jellyfishDraw(Jellyfish* j);

void createJellyfish(int x, int y)
{
	int i;
	for (i = 0; i < MAX_ENEMIES; i++) {
		if (enemies[i] == NULL) {
			Enemy* e = malloc(sizeof *e);
			Jellyfish* j = malloc(sizeof *j);
			j->id = i;
			
			j->x = x;
			j->y = j->ystart = y;
			j->ystart += 20;
			
			j->spd = 0;
			j->angle = 0;
			
			j->state = 0;
			j->imageIndex = 0;
			
			e->data = j;
			e->enemyStep = jellyfishStep;
			e->enemyDraw = jellyfishDraw;
			e->type = 20;
			
			enemies[i] = e;
			i = MAX_ENEMIES;
		}
	}
}

void jellyfishStep(Jellyfish* j)
{
	Mask mask;
	mask.unused = mask.circle = 0;
	mask.w = mask.h = 30;
	mask.x = j->x + 20 - (mask.w / 2);
	mask.y = j->y + 20 - (mask.h / 2);
	
	//Idle float
	if (j->state == 0)
	{
		//Animate
		j->imageIndex += 0.06;
		if (j->imageIndex >= 4) {
			j->imageIndex -= 4;
		}
		
		//Movement
		j->angle += 2.5;
		if (j->angle >= 360) { j->angle -= 360; }
		j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180));

		//Update mask
		mask.y = j->y + 20 - (mask.h / 2);
		
		//if player is close enough
		Mask area;
		area.unused = area.circle = 0;
		area.w = area.h = 160;
		area.x = j->x - 60;
		area.y = j->y - 60;
		
		if (checkCollision(area, getHeroMask()) == 1) {
			j->state = 1;
			j->spd = 0;
		}
	}
	//Attack
	if (j->state == 1)
	{
		//Setup
		if (j->spd == 0) {
			PHL_PlaySound(sounds[sndPi02], CHN_ENEMIES);
			j->spd = 3;
			
			//Move Right
			if (herox > j->x + 20) {
				//Move Up
				if (heroy < j->y) {
					j->angle = 135;
				}
				//Move Down
				else {
					j->angle = 45;
				}
			}
			//Move Left
			else{
				//Move Up
				if (heroy < j->y) {
					j->angle = 225;
				}
				//Move Down
				else {
					j->angle = 315;
				}
			}
		}
		
		//Movement
		j->x += (j->spd) * sin(j->angle * 3.14159 / 180);
		j->y += (j->spd) * cos(j->angle * 3.14159 / 180);
		
		//Slow down
		j->spd -= 0.075;
		if (j->spd <= 0) {
			j->spd = 0;
			j->state = 2;
		}
	}
	//Stablize
	if (j->state == 2)
	{
		//Setup
		if (j->spd == 0) {
			j->spd = 1;
			j->ystart = j->y - 20;
			j->angle = 80;
		}
		
		//Movement
		j->angle += 2.5;
		if (j->angle >= 360) { j->angle -= 360; }
		j->y = j->ystart + (20 * sin(j->angle * 3.14159 / 180));
		
		
		if (j->angle >= 180) {
			j->state = 0;
			j->ystart = j->y - 20;
			j->angle = 100;
		}
	}
	
	//Update Mask
	mask.x = j->x + 20 - (mask.w / 2);
	mask.y = j->y + 20 - (mask.h / 2);
	
	//Collide with hero
	if (checkCollision(mask, getHeroMask())) {
		heroHit(15, j->x + 20);
	}
	
	//Sword collision
	int i;
	for (i = 0; i < MAX_WEAPONS; i++) {
		if (weapons[i] != NULL) {
			if (checkCollision(mask, weapons[i]->weaponMask)) {
				spawnCollectable(j->x + 20, j->y);				
				weaponHit(weapons[i]);
				
				createEffect(2, j->x - 12, j->y - 12);
				enemyDestroy(j->id);
				
				i = MAX_WEAPONS;
			}
		}	
	}
}

void jellyfishDraw(Jellyfish* j)
{
	int frame = 0;
	
	//if (j->state == 0) {
		int animation[4] = { 0, 1, 0, 2};
		frame = animation[(int)j->imageIndex];
	//}
	
	if (j->state == 1) {
		if (j->angle == 135) {
			frame = 3;
		}
		else if (j->angle == 225) {
			frame = 4;
		}
		else if (j->angle == 315) {
			frame = 5;
		}
		else {
			frame = 6;
		}
	}
	
	PHL_DrawSurfacePart(j->x, j->y, frame * 40, 520, 40, 40, images[imgEnemies]);
}