aboutsummaryrefslogtreecommitdiff
path: root/src/platform.c
blob: 4435aa7ec1ccb8aac7ae3a8d2db3a243544b8105 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "platform.h"
#include "game.h"
#include "PHL.h"
#include "hero.h"
#include <stdlib.h>

void createPlatform(int type, int xstart, int ystart, int xend, int yend, int spd, int secret)
{
	int i;
	for (i = 0; i < MAX_PLATFORMS; i++) {
		if (platforms[i] == NULL) {
			Platform* p = malloc(sizeof *p);
			p->id = i;
			p->type = type;
			
			p->x = p->xstart = xstart;
			p->y = p->ystart = ystart;
			
			p->xend = xend;
			p->yend = yend;
			
			p->timer = 0;
			
			p->secret = secret;
			
			if (roomSecret == 1) {
				p->secret = 0;
			}
			
			p->visible = 1;
			if (p->secret == 1) {
				p->visible = 0;
			}
			
			if (type == 1) {
				p->xend = p->x + 1;
				p->yend = p->y;				
			}
			
			p->state = 0;
			p->spd = spd;
			
			p->mask.circle = 0;
			p->mask.unused = 0;
			if (p->secret == 1) {
				p->mask.unused = 1;
			}
			p->mask.x = xstart;
			p->mask.y = ystart;
			p->mask.w = p->mask.h = 40;
			
			platforms[i] = p;
			i = MAX_PLATFORMS;
		}
	}
}

void platformStep(Platform* p)
{
	char isSolid = p->visible;
	
	//Megaman blocks
	if (p->type == 2) {
		int myStep = p->xend,
			maxSteps = p->yend;
			
		p->timer += 1;
		if (p->timer >= maxSteps * 60 + 60) {
			p->timer = 0;
		}
		
		//Play sound
		if (p->timer == myStep * 60) {
			PHL_PlaySound(sounds[sndPi03], CHN_SOUND);
		}
		
		if (p->timer > myStep * 60 && p->timer <= (myStep * 60) + 60) {
			isSolid = 1;
			p->visible = 1;
			
			//Blink effect
			if (p->timer > myStep * 60 + 30) {
				p->visible = p->timer % 2;
			}
		}else{
			isSolid = 0;
			p->mask.unused = 1;
			p->visible = 0;
			
			//Fall off platform
			if (getHeroMask().x > p->mask.x + p->mask.w || getHeroMask().x + getHeroMask().w < p->mask.x) {
			}else if (heroy == p->y - 40 && getHeroVsp() >= 0) {
				setHeroOnground(0);
			}
		}		
	}
	
	if (isSolid == 1) {
		p->mask.unused = 0;
		
		int targx = p->xend,
			targy = p->yend;
			
		if (p->state == 1) {
			targx = p->xstart;
			targy = p->ystart;
		}
		
		//Check if the player is standing on top
		int isontop = 0;
		int isabove = 0;
		if (getHeroMask().x > p->mask.x + p->mask.w || getHeroMask().x + getHeroMask().w < p->mask.x) {
		}else if (heroy == p->y - 40 && getHeroVsp() >= 0) {
			isontop = 1;
		}else if (heroy < p->y - 40) {
			isabove = 1;
		}
		
		//Move platform
		if (p->y != targy) {
			if (p->y < targy) {
				p->y += p->spd;
				if (isontop == 1) {
					heroy += p->spd;
				}
			}else{
				p->y -= p->spd;
				if (isontop == 1) {
					heroy -= p->spd;
				}else{
					p->mask.y = p->y;
					if (checkCollision(p->mask, getHeroMask()) && isabove == 1) {
						heroy = p->y - 40;
					}
				}
			}
		}
			
		if (p->x != targx) {
			if (p->x < targx) {
				p->x += p->spd;
				if (isontop == 1) {
					herox += p->spd;
				}
			}else{
				p->x -= p->spd;
				if (isontop == 1) {
					herox -= p->spd;
				}
			}
		}
			
		if (p->x == targx && p->y == targy) {
			if (p->state == 0) {
				p->state = 1;
			}else{
				p->state = 0;
			}
		}
		
		if (p->type == 0) //Moving platform
		{		
			
		}
		else if (p->type == 1) { //Loose block
			if (p->spd != 0) {
				p->timer -= 1;
				if (p->timer <= 0) {
					createRockSmash(p->x + 20, p->y + 20);
					if (isontop == 1) {
						setHeroOnground(0);
					}
					platformDestroy(p->id);					
					/*createEffectExtra(4, p->x, p->y, -1, 0, 0);
					createEffectExtra(4, p->x, p->y, -1, 0, 1);
					createEffectExtra(4, p->x, p->y, 1, 0, 0);
					createEffectExtra(4, p->x, p->y, 1, 0, 1);
					*/
				}
			}
		
			if (p->spd == 0 && isontop == 1) {
				p->spd = 2;
				p->timer = 30;
			}
		}
		
		//Update Mask
		p->mask.x = p->x;
		p->mask.y = p->y;
	}else{
		//p->mask.unused = 1;
		if (p->secret == 1) {
			if (roomSecret == 1) {
				p->mask.unused = 0;
				p->visible = 1;
				p->secret = 0;
				playSecret();
			}
		}
	}
}

void platformDraw(Platform* p)
{	
	if (p->visible == 1) {
		int cropX = p->type * 40;
		
		if (p->type == 3) {
			cropX = 9 * 40;
		}
		
		PHL_DrawSurfacePart(p->x, p->y, cropX, 400, 40, 40, images[imgMisc20]);
	}
}

void platformDestroy(int id)
{
	if (platforms[id] != NULL) {
		free(platforms[id]);
	}
	platforms[id] = NULL;
}