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
|
/*
* collision.c -- Comntiene funciones de detección de colisiones.
* Copyright (C) 2018 Juan Marín Noguera
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "block.h"
#include "block_list.h"
#include "collision.h"
#include "collision_type.h"
#include "item.h"
#include "item_list.h"
#include "player.h"
CollisionType collision_data(double x1, double y1, double w1, double h1,
double x2, double y2, double w2, double h2)
{
double dw = (w1+w2)/2, dh = (h1+h2)/2;
double dx = x1 - x2 + dw - w2, dy = y1 - y2 + dh - h2;
double dxp, dyp;
CollisionType c = 0;
if (dx < dw && -dx < dw && dy < dh && -dy < dh) {
dxp = dx * 0.75 - dy;
dyp = dx * 0.75 + dy;
if (dxp > 0) {
if (dyp > 0)
c |= COLLISION_RIGHT;
else
c |= COLLISION_BOTTOM;
} else {
if (dyp > 0)
c |= COLLISION_TOP;
else
c |= COLLISION_LEFT;
}
}
return c;
}
void collision_hit_if_top_and_advance(Player p, BlockNode *bn,
CollisionType c, ItemList il)
{
Item i = NULL;
if (c & COLLISION_TOP) {
if (block_hit(p, block_list_current(*bn), &i))
block_list_delete(*bn);
else
*bn = block_list_next(*bn);
if (i != NULL)
item_list_append(il, i);
} else {
*bn = block_list_next(*bn);
}
}
void collision_player_with_blocks(Player p, BlockList bl, ItemList il)
{
BlockNode bn = block_list_begin(bl);
Block b;
double px = player_x(p), py = player_y(p), ph = player_height(p);
int pxb = (int)player_x(p) / BLOCK_SIZE;
CollisionType total_collisions = 0, c;
while (!block_list_isend(bn) &&
block_x(block_list_current(bn)) < pxb)
bn = block_list_next(bn);
while (!block_list_isend(bn) &&
block_x(b = block_list_current(bn)) < pxb + 2)
if (!block_can_pass_through(b) &&
(c = collision_data(
block_x(b) * BLOCK_SIZE,
block_y(b) * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE,
px,
py,
PLAYER_WIDTH,
ph))
!= 0) {
collision_hit_if_top_and_advance(p, &bn, c, il);
total_collisions |= c;
} else {
bn = block_list_next(bn);
}
player_correct_on_collision(p, total_collisions);
}
void collision_player_with_items(Player p, ItemList il)
{
ItemNode in = item_list_begin(il);
Item i;
double px = player_x(p), py = player_y(p), ph = player_height(p);
CollisionType c;
while (!item_list_isend(in)) {
i = item_list_current(in);
c = collision_data(
item_x(i), item_y(i), item_width(i), item_height(i),
px, py, PLAYER_WIDTH, ph);
if (c != 0 && item_player_collide(i, p, c))
item_list_delete(in);
else
item_list_advance(in);
}
item_list_free_node(in);
}
void collision_blocks_with_items(BlockList bl, ItemList il)
{
ItemNode in;
Item i;
BlockNode bn;
Block b;
CollisionType c;
double ix, iy, iw, ih;
for (in = item_list_begin(il); !item_list_isend(in);
item_list_advance(in)) {
i = item_list_current(in);
ix = item_x(i);
iy = item_y(i);
iw = item_width(i);
ih = item_height(i);
c = 0;
for (bn = block_list_begin(bl); !block_list_isend(bn);
bn = block_list_next(bn)) {
b = block_list_current(bn);
if (!block_can_pass_through(b))
c |= collision_data(
block_x(b) * BLOCK_SIZE,
block_y(b) * BLOCK_SIZE,
BLOCK_SIZE,
BLOCK_SIZE,
ix,
iy,
iw,
ih);
}
if (c != 0)
item_correct_on_block_collision(i, c);
}
item_list_free_node(in);
}
|