summary refs log tree commit diff
path: root/src/warp.c
blob: 3556ef3023883b55b09a57c1dd209cf42fbf24e0 (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
#include "main.h"
#include "entity.h"
#include "loader.h"
#include "tilemap.h"

static int warp_update(struct warp *self) {
	if (
		self->hitbox.left < entities.player[0].hitbox.right &&
		self->hitbox.right > entities.player[0].hitbox.left &&
		self->hitbox.top < entities.player[0].hitbox.bottom &&
		self->hitbox.bottom > entities.player[0].hitbox.top
	) {
		if (self->ext == NULL) {
			return 0;
		}
		game_next_level = self->ext;
		memcpy(next_entities.player, entities.player, sizeof (entities.player));
		next_entities.player[0].x = to_fixed(self->tox);
		next_entities.player[0].y = to_fixed(self->toy);
		game_state = STATE_FADE_IN;
		self->ext = NULL;
		return 0;
	}
	return 0;
}

struct warp *warp_new(struct entities *entities) {
	entities->warp[entities->warps] = (struct warp) {
		.update = warp_update,
		.x = 0, .y = 0,
		.w = 0, .h = 0,
		.tox = 0, .toy = 0,
		.hitbox = {
			0, 0, 0, 0,
		},
		.timer = 0,
		.ext = NULL,
	};
	return entities->warp + entities->warps++;
}

int warp_property(struct warp *const restrict self, char const *const restrict property, char const *const restrict value) {
	if (strcmp(property, "x") == 0) {
		self->x = atoi(value);
	} else if (strcmp(property, "y") == 0) {
		self->y = atoi(value);
	} else if (strcmp(property, "width") == 0) {
		self->w = atoi(value);
	} else if (strcmp(property, "height") == 0) {
		self->h = atoi(value);
	} else if (strcmp(property, "tox") == 0) {
		self->tox = atoi(value);
	} else if (strcmp(property, "toy") == 0) {
		self->toy = atoi(value);
	} else if (strcmp(property, "map") == 0) {
		self->ext = value;
	} else {
		return 1;
	}
	self->hitbox = (struct hitbox) {.left = self->x, .top = self->y, .right = self->x + self->w, .bottom = self->y + self->h};
	return 0;
}