-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunitCollidebleImage.pas
85 lines (66 loc) · 1.93 KB
/
unitCollidebleImage.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
unit unitCollidebleImage;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, raylib;
type CollidebleImage = class
private
image: TImage;
texture: TTexture2D;
position: TVector2;
left, right, top, bottom: Single;
public
constructor Create(_position: TVector2; _image: TImage);
function IsPointColliding(point: TVector2): boolean;
function IsCollidingWithOther(other: CollidebleImage): boolean;
procedure SetPosition(_position: TVector2);
function GetPosition(): TVector2;
procedure Draw();
end;
implementation
constructor CollidebleImage.Create(_position: TVector2; _image: TImage);
begin
image := _image;
texture := LoadTextureFromImage(image);
SetPosition(_position);
end;
function CollidebleImage.IsPointColliding(point: TVector2): Boolean;
var x, y: Integer;
begin
if ((point.x <= left) or (point.x >= right) or
(point.y <= top) or (point.y >= bottom))
then
Exit(false);
x := round(point.x - left);
y := round(point.y - top);
Exit(GetImageColor(image, x, y).a <> 0);
end;
function CollidebleImage.IsCollidingWithOther(other: CollidebleImage): boolean;
var i, j: integer;
begin
for i := 0 to image.height-1 do begin
for j := 0 to image.width-1 do begin;
if (GetImageColor(image, j, i).a = 0) then continue;
if (other.IsPointColliding(Vector2Create(left + j, top + i))) then
Exit(true);
end;
end;
Exit(false);
end;
procedure CollidebleImage.SetPosition(_position: TVector2);
begin
position := _position;
left := position.x - image.width / 2;
right := position.x + image.width / 2 - 1;
top := position.y - image.height / 2;
bottom := position.y + image.height / 2 - 1;
end;
function CollidebleImage.GetPosition(): TVector2;
begin
Exit(position);
end;
procedure CollidebleImage.Draw();
begin
DrawTextureV(texture, Vector2Create(left, top), WHITE);
end;
end.