-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhitbox2.pas
140 lines (92 loc) · 1.94 KB
/
hitbox2.pas
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
(* HIT BOX 2 *)
uses crt, joystick, fastgraph;
type
TBox = record
x,y, w, h: byte;
end;
TColision = (left=1, right=2, top=4, bottom=8, nohit=0);
var
b1, b2: TBox;
v, f: byte;
{
x,y
*---------------------------*
| |
| Cx,Cy | h-eight
| |
*---------------------------*
w-idth
Cx = x + w/2
Cy = y + h/2
}
function hitBox(A, B: TBox): TColision;
var w,h, _w, _h: byte;
dx,dy,wy,hx: smallint;
begin
Result:=nohit;
w := byte(A.w + B.w) shr 1;
h := byte(A.h + B.h) shr 1;
dx := (A.x + A.w shr 1) - (B.x + B.w shr 1); // A.Cx - B.Cx
if dx >= 0 then
_w := dx
else
_w := -dx;
dy := (A.y + A.h shr 1) - (B.y + B.h shr 1); // A.Cy - B.Cy
if dy >= 0 then
_h := dy
else
_h := -dy;
if (_w <= w) and (_h <= h) then begin
(* collision! *)
wy := w * dy;
hx := h * dx;
if (wy > hx) then begin
if (wy > smallint(-hx)) then
(* collision at the bottom *)
Result := bottom
else
(* on the left *)
Result := left;
end else
if (wy < smallint(-hx)) then
(* on the top *)
Result := top
else
(* at the right *)
Result := right;
end;
end;
procedure drawBox(B: TBox; c: byte);
begin
SetColor(c);
Rectangle(B.x, B.y, B.x + B.w, B.y + B.h)
end;
begin
InitGraph(15);
b1.x:=85; // box1 -> fire ON + joy
b1.y:=75;
b1.w:=11;
b1.h:=16;
b2.x:=50; // box2 -> fire OFF + joy
b2.y:=54;
b2.w:=27;
b2.h:=16;
drawBox(b1, 1);
drawBox(b2, 2);
repeat
pause;
v:=joy_1; // read joystick #1
f:=trig0; // read fire #1
if v <> joy_none then begin
{$i hitbox.inc}
case hitBox(b1,b2) of
left: writeln('left');
right: writeln('right');
top: writeln('top');
bottom: writeln('bottom');
else
writeln;
end;
end;
until false;
end.