summaryrefslogtreecommitdiffhomepage
path: root/src/wall.c
blob: d3c9d3fb665ec1cf2f5dc7bdb1aac5f3b38c2568 (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
#include "wall.h"
#include "rodeo.h"

static rodeo_collision_2d_world_t collision_wall_world;
rodeo_texture_2d_t wall_texture;
rodeo_texture_2d_t floor_texture;

void
init_wall(void)
{
	wall_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/walls.png"));
	floor_texture = rodeo_texture_2d_create_from_path(cstr_lit("assets/floor.png"));
	uint16_t window_width = 1600;
	uint16_t window_height = 900;
	collision_wall_world = rodeo_collision_2d_world_create();
	float walls[][4] = {
		{0, -10, window_width, 10},
		{0, window_height, window_width, 10},
		{-10, 0, 10, window_height},
		{window_width, 0, 10, window_height},
		{169,105,72,263},
		{241,105,191,73},
		{1169,105,262,72},
		{1359,177,72,190},
		{1169,723,262,72},
		{1359,533,72,190},
		{169,533,72,262},
		{241,723,191,72},
		{764,200,72,500},
		{550,414,501,72}
	};
	int numwalls = sizeof(walls)/sizeof(walls[0]);
	new_wall(0, -10, window_width, 10);
	new_wall(0, window_height, window_width, 10);
	new_wall(-10, 0, 10, window_height);
	new_wall(window_width, 0, 10, window_height);
	for (int i = 0; i < numwalls; ++i) {
		new_wall(walls[i][0], walls[i][1], walls[i][2], walls[i][3]);
	}
}

void
deinit_wall(void)
{
	rodeo_collision_2d_world_destroy(&collision_wall_world);
}

rodeo_collision_2d_world_t *
get_wall_world(void)
{
	return &collision_wall_world;
}

rodeo_collision_2d_world_item_t *
new_wall(
    float x,
    float y,
    float width,
    float height
)
{
	return rodeo_collision_2d_world_item_create(
			&collision_wall_world,
			(rodeo_collision_2d_world_item_t)
			{
				.x = x,
				.y = y,
				.width = width,
				.height = height
			});
}

bool
coords_inside_wall(
    float x,
    float y
)
{
	c_foreach(i, cvec_collision_2d_world_item, collision_wall_world) {
		if (x >= i.ref->x && x <= i.ref->x + i.ref->width &&
				y >= i.ref->y && y <= i.ref->y + i.ref->height) {
			return true;
		}
	}
	return false;
}

void
moving_wall_resolver(
	rodeo_collision_2d_world_item_t *obj_collision,
	rodeo_collision_2d_world_item_t *wall_collision
)
{
	rodeo_collision_2d_world_item_t *p = obj_collision;
	rodeo_collision_2d_world_item_t *w = wall_collision;
	rodeo_rectangle_t step = (rodeo_rectangle_t){
		.x = p->x + p->dx * rodeo_frame_time_get(),
		.y = p->y + p->dy * rodeo_frame_time_get(),
		.width = p->width,
		.height = p->height
	};
	rodeo_rectangle_t intersection = rodeo_collision_2d_get_collision_rect(p, w);
	if (intersection.width < intersection.height) {
		if (intersection.x == step.x) {
			p->x = w->x + w->width;
			if (p->dx < 0) {
				p->dx = 0;
			}
		} else {
			p->x = w->x - p->width;
			if (p->dx > 0) {
				p->dx = 0;
			}
		}
	}
	else if (intersection.height < intersection.width) {
		if (intersection.y == step.y) {
			p->y = w->y + w->height;
			if (p->dy < 0) {
				p->dy = 0;
			}
		} else {
			p->y = w->y - p->height;
			if (p->dy > 0) {
				p->dy = 0;
			}
		}
	}
	else if (p->width == w->width && p->height == w->height) {
		p->dx = 0;
		p->dy = 0;
	}
}

void
draw_level(void)
{
	rodeo_rectangle_t rect = (rodeo_rectangle_t){0,0,1600,900};
	rodeo_texture_2d_draw(&rect, &rect, NULL, &wall_texture);
	rodeo_texture_2d_draw(&rect, &rect, NULL, &floor_texture);

}