summary refs log tree commit diff
path: root/src/player.c
blob: 0f0025e45d5b418a52665dcb12611fc2cd7d7b7f (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "main.h"
#include "entity.h"
#include "loader.h"
#include "input.h"
#include "tilemap.h"
#include <stdbool.h>

#define SIZE 4
#define ACCELERATION 1
#define FRICTION 2
#define MAX_SPEED 12
#define GRAVITY 1
#define JUMP 30

enum {
	PLAYER_NONE,
	PLAYER_IDLE,
	PLAYER_WALK,
	PLAYER_JUMP,
	PLAYER_FALL,
};

enum {
	PLAYER_A_IDLE,
	PLAYER_A_IDLE2,
	PLAYER_A_WALK,
	PLAYER_A_WALK2,
	PLAYER_A_WALK3,
	PLAYER_A_WALK4,
	PLAYER_A_JUMP,
	PLAYER_A_FALL,
	PLAYER_A_FALL2,
};

struct anim player_anims[] = {
	{PLAYER_A_IDLE2, {0, 0, 16, 16}, 300},
	{PLAYER_A_IDLE, {0, 32, 16, 16}, 2},
	{PLAYER_A_WALK2, {0, 0, 16, 16}, 6},
	{PLAYER_A_WALK3, {16, 0, 16, 16}, 6},
	{PLAYER_A_WALK4, {32, 0, 16, 16}, 6},
	{PLAYER_A_WALK, {48, 0, 16, 16}, 6},
	{PLAYER_A_JUMP, {16, 0, 16, 16}, 300},
	{PLAYER_A_FALL2, {32, 0, 16, 16}, 15},
	{PLAYER_A_FALL2, {48, 0, 16, 16}, 15},
};

static collision_T collide(struct entity *self) {
	return tilemap_area(tilemap, from_fixed(self->x) - SIZE / 2, from_fixed(self->y) - SIZE, from_fixed(self->x) + SIZE / 2, from_fixed(self->y));
}

static int move(struct entity *self) {
	int on_ground = false;
	int dx = (input_right(input_now) - input_left(input_now)) * ACCELERATION;
	self->velocity.x += dx;
	// deaccel
	if (dx == 0) {
		if (self->velocity.x < -FRICTION) {
			self->velocity.x += FRICTION;
		} else if (self->velocity.x > FRICTION) {
			self->velocity.x -= FRICTION;
		} else {
			self->velocity.x = 0;
		}
	}
	// speed cap
	if (self->velocity.x > MAX_SPEED) {
		self->velocity.x = MAX_SPEED;
	} else if (self->velocity.x < -MAX_SPEED) {
		self->velocity.x = -MAX_SPEED;
	}
	// x collision
	self->x += self->velocity.x;
	collision_T const cx = collide(self);
	if (collision_solid(cx)) {
		if (self->velocity.x < 0) {
			self->x += to_fixed(8) - ((self->x - to_fixed(SIZE / 2)) % to_fixed(8)); // left
		} else if (self->velocity.x == 0) {
			//fputs("what?\n", stderr);
		} else {
			self->x -= ((self->x + to_fixed(SIZE / 2)) % to_fixed(8)); // right
		}
		self->velocity.x = 0;
	}
	if (self->velocity.x < 0) {
		self->facing = -1;
	} else if (self->velocity.x > 0) {
		self->facing = +1;
	}
	self->velocity.y += GRAVITY;
	// y collision
	self->y += self->velocity.y;
	collision_T const cy = collide(self);
	if (collision_solid(cy)) {
		if (self->velocity.y < 0) {
			self->y += to_fixed(8) - ((self->y - to_fixed(SIZE)) % to_fixed(8)); // up
			self->velocity.y = 0;
		} else if (self->velocity.y == 0) {
			//fputs("what?\n", stderr);
		} else {
			self->y -= ((self->y) % to_fixed(8)); // down
			self->velocity.y = to_fixed(1); // crazy but it works
			on_ground = true;
		}
	}
	
	if (collision_hazard(cx | cy)) {
		self->hurt(self, 1);
	}
	
	self->hitbox.left = from_fixed(self->x) - SIZE / 2;
	self->hitbox.right = from_fixed(self->x) + SIZE / 2;
	self->hitbox.top = from_fixed(self->y) - SIZE;
	self->hitbox.bottom = from_fixed(self->y);
	return on_ground;
}

static void anim(struct entity *self, unsigned anim) {
	self->anim = player_anims[anim];
}

static int player_update(struct entity *self) {
	if (self->hp <= 0) {
		self->state = 0;
		return 1;
	}
	switch (self->state) {
		case PLAYER_IDLE:
			if (move(self) == false) {
				self->velocity.y = 0;
				anim(self, PLAYER_A_FALL);
				self->state = PLAYER_FALL;
			}
			if (input_right(input_now) - input_left(input_now)) {
				anim(self, PLAYER_A_WALK);
				self->state = PLAYER_WALK;
			}
			if (input_a(input_now)) {
				self->velocity.y = -JUMP;
				anim(self, PLAYER_A_JUMP);
				self->state = PLAYER_JUMP;
			}
			break;

		case PLAYER_WALK:
			if (move(self) == false) {
				self->velocity.y = 0;
				anim(self, PLAYER_A_FALL);
				self->state = PLAYER_FALL;
			}
			if (!(input_right(input_now) - input_left(input_now))) {
				anim(self, PLAYER_A_IDLE);
				self->state = PLAYER_IDLE;
			}
			if (input_a(input_now)) {
				self->velocity.y = -JUMP;
				anim(self, PLAYER_A_JUMP);
				self->state = PLAYER_JUMP;
			}
			break;
		
		case PLAYER_JUMP:
			if (!input_a(input_now)) {
				self->velocity.y /= 2;
				anim(self, PLAYER_A_FALL);
				self->state = PLAYER_FALL;
			}
			if (self->velocity.y > 0) {
				anim(self, PLAYER_A_FALL);
				self->state = PLAYER_FALL;
			}
			if (move(self) == true) {
				anim(self, PLAYER_A_IDLE);
				self->state = PLAYER_IDLE;
			}
			break;
		
		case PLAYER_FALL:
			if (move(self) == true) {
				anim(self, PLAYER_A_IDLE);
				self->state = PLAYER_IDLE;
			}
			break;
	}
	if (self->iframes > 0) {
		self->iframes--;
	}
	self->anim.length--;
	if (self->anim.length == 0) {
		anim(self, self->anim.frame);
	}
	return 0;
}

static int player_hurt(struct entity *self, int damage) {
	if (self->iframes == 0) {
		self->hp -= damage;
		self->iframes = 60;
	}
	return 0;
}

static int player_draw(struct entity *self, int camX, int camY) {
	if (!(self->iframes & 0x4)) {
		SDL_Rect rect = self->anim.rect;
		if (self->facing == -1) {
			rect.y += 16;
		}
		SDL_RenderCopy(renderer, self->texture, &rect, &(SDL_Rect) {from_fixed(self->x) - camX - 8, from_fixed(self->y) - camY - 12, 16, 16});
	}
	return 0;
}

struct entity *player_new(struct entities *entities) {
	entities->player[0] = (struct entity) {
		.update = player_update,
		.hurt = player_hurt,
		.draw = player_draw,
		.x = 0, .y = 0,
		.velocity = {.x = 0, .y = 0},
		.hitbox = {
			0, 0, 0, 0,
		},
		.state = PLAYER_IDLE,
		.hp = 3,
		.timer = 0,
		.facing = 1,
		.iframes = 0,
		.texture = NULL,
		.ext = NULL,
	};
	anim(entities->player, PLAYER_A_IDLE);
	entities->player[0].texture = res_get_texture("fywi").data;
	return entities->player + 0;
}

int player_property(struct entity *const restrict self, char const *const restrict property, char const *const restrict value) {
	if (strcmp(property, "x") == 0) {
		self->x = to_fixed(atoi(value));
	} else if (strcmp(property, "y") == 0) {
		self->y = to_fixed(atoi(value));
	} else {
		return 1;
	}
	return 0;
}