aboutsummaryrefslogtreecommitdiff
path: root/src/enemies/slug.c
blob: c8fcc2c4a6b44d338394ade2aec1b43b133af906 (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
#include "slug.h"
#include "../enemy.h"
#include "../game.h"
#include "../PHL.h"
#include "../hero.h"
#include <stdlib.h>

void slugStep(Slug* s);
void slugDraw(Slug* s);

void createSlug(int x, int y, int dir)
{
	int i;
	for (i = 0; i < MAX_ENEMIES; i++) {
		if (enemies[i] == NULL) {
			Enemy* e = malloc(sizeof *e);
			Slug* s = malloc(sizeof *s);
			s->id = i;
			
			s->x = x;
			s->y = y;
			
			s->imageIndex = 0;			
			s->vsp = 0;
			
			s->dir = 1;
			if (dir == 1) {
				s->dir = -1;
			}			

			e->data = s;
			e->enemyStep = slugStep;
			e->enemyDraw = slugDraw;
			e->type = 2;
			
			enemies[i] = e;
			i = MAX_ENEMIES;
		}
	}
}

void slugStep(Slug* s)
{
	//Create Mask
	Mask mask;
	{
		mask.circle = 0;
		mask.unused = 0;
		mask.w = 32;
		mask.h = 24;
		mask.x = s->x + ((40 - mask.w) / 2);
		mask.y = s->y + (40 - mask.h);
	}
	
	//Animate
	{
		s->imageIndex += 0.1;
		if (s->imageIndex >= 4) {
			s->imageIndex -= 4;
		}
	}
	
	//Check if on ground
	int onground = 1;
	{
		mask.y += 1;
		if (checkTileCollision(1, mask) == 0 && checkTileCollision(3, mask) == 0) {
			onground = 0;
		}
		mask.y -= 1;
	}
	
	if (onground == 0) {
		double grav = 0.2;
		
		//Fall
		{
			s->y += s->vsp;
			s->vsp += grav;
		}
		
		//Land on ground
		{
			mask.y = mask.y + (40 - mask.h);
			PHL_Rect collide = getTileCollision(1, mask);
			if (collide.x == -1) {
				collide = getTileCollision(3, mask);
			}
			if (collide.x != -1) {
				s->y = collide.y - 40;
				s->vsp = 0;
			}
		}
	}else{
		//Check if on ledge
		{
			mask.x += mask.w * s->dir;
			mask.y += 1;
			
			PHL_Rect collide = getTileCollision(1, mask);
			if (collide.x == -1) {
				collide = getTileCollision(3, mask);
			}
			if (collide.x == -1) {
				s->dir *= -1;
			}
		}		
	}
	
	//Horizontal movement
	double hsp = 0.5;
	{
		s->x += s->dir * hsp;
	}
	
	//Check if hit a wall
	{
		mask.x = s->x + ((40 - mask.w) / 2);
		mask.y = s->y + (40 - mask.h);
		
		PHL_Rect collide = getTileCollision(1, mask);
		if (collide.x != -1) {
			s->dir *= -1;
		}
	}
	
	//Hit Player
	{
		if (checkCollision(mask, getHeroMask()) == 1) {
			heroHit(15, mask.x + (mask.w / 2));
		}
	}
	
	//Weapon collision
	{
		int i;
		for (i = 0; i < MAX_WEAPONS; i++) {
			if (weapons[i] != NULL) {
				if (checkCollision(mask, weapons[i]->weaponMask) == 1) {				
					weaponHit(weapons[i]);
					
					createEffect(2, s->x - 12, s->y - 6);
					spawnCollectable(s->x + 20, s->y);
					enemyDestroy(s->id);
					
					i = MAX_WEAPONS;
				}
			}	
		}
	}
	
}

void slugDraw(Slug* s)
{	
	int anim[4] = { 1, 0, 2, 0 };
	
	int cropx = anim[(int)s->imageIndex] * 40;
	if (s->dir == -1) {
		cropx += 120;
	}
	
	PHL_DrawSurfacePart(s->x, s->y + 10, cropx, 40, 40, 40, images[imgEnemies]);
}