-
Notifications
You must be signed in to change notification settings - Fork 3
/
unitsensor.pas
228 lines (169 loc) · 5.15 KB
/
unitsensor.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
unit unitSensor;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, unitCollidebleImage, unitTerrain, raylib, raymath, Math;
type
{ Sensor }
Sensor = class
private
position: TVector2;
angle: single;
terrain: unitTerrain.Terrain;
sensorMain: CollidebleImage;
sensorGround: CollidebleImage;
sensorLeft: CollidebleImage;
sensorRight: CollidebleImage;
sensorTop: CollidebleImage;
sensorBottom: CollidebleImage;
sensorSlopeLeft: CollidebleImage;
sensorSlopeRight: CollidebleImage;
public
constructor Create(_terrain: unitTerrain.Terrain;
_position: TVector2; _imgMaskMain: TImage; _imgMaskSmall: TImage);
procedure SetPosition(_position: TVector2);
function GetPosition(): TVector2;
function GetAngle(): single;
procedure SetAngle(_angle: single);
function IsCollisionMain(): boolean;
function IsCollidingGround(): boolean;
function IsCollidingLeft(): boolean;
function IsCollidingRight(): boolean;
function IsCollidingTop(): boolean;
function IsCollidingBottom(): boolean;
function IsCollidingSlopeLeft(): boolean;
function IsCollidingSlopeRight(): boolean;
function CalculateAngle(): single;
procedure Draw();
end;
implementation
constructor Sensor.Create(_terrain: unitTerrain.Terrain; _position: TVector2;
_imgMaskMain: TImage; _imgMaskSmall: TImage);
begin
terrain := _terrain;
position := _position;
angle := 0;
sensorMain := CollidebleImage.Create(position, _imgMaskMain);
sensorLeft := CollidebleImage.Create(position, _imgMaskSmall);
sensorRight := CollidebleImage.Create(position, _imgMaskSmall);
sensorTop := CollidebleImage.Create(position, _imgMaskSmall);
sensorBottom := CollidebleImage.Create(position, _imgMaskSmall);
sensorGround := CollidebleImage.Create(position, _imgMaskSmall);
sensorSlopeLeft := CollidebleImage.Create(position, _imgMaskSmall);
sensorSlopeRight := CollidebleImage.Create(position, _imgMaskSmall);
SetPosition(_position);
end;
procedure Sensor.SetPosition(_position: TVector2);
begin
position := _position;
sensorMain.SetPosition(position);
{ Y }
sensorBottom.SetPosition(Vector2Create(position.x - 15 *
Sin(angle), position.y + 15 * Cos(angle)));
sensorTop.SetPosition(Vector2Create(position.x + 15 *
Sin(angle), position.y - 15 * Cos(angle)));
{ X }
sensorLeft.SetPosition(Vector2Create(position.x - 15 *
Cos(angle), position.y - 15 * Sin(angle)));
sensorRight.SetPosition(Vector2Create(position.x + 15 *
Cos(angle), position.y + 15 * Sin(angle)));
{ Slopes }
sensorSlopeRight.SetPosition(Vector2Create(position.x -
24 * Sin(angle) + 8 * Cos(angle), position.y + 24 * Cos(angle) +
8 * Sin(angle)));
sensorSlopeLeft.SetPosition(Vector2Create(position.x -
24 * Sin(angle) - 8 * Cos(angle), position.y + 24 * Cos(angle) -
8 * Sin(angle)));
{ Ground }
sensorGround.SetPosition(Vector2Create(position.x - 24 *
Sin(angle), position.y + 24 * Cos(angle)));
end;
function Sensor.GetPosition(): TVector2;
begin
Exit(position);
end;
function Sensor.GetAngle(): single;
begin
Exit(angle);
end;
procedure Sensor.SetAngle(_angle: single);
begin
angle := _angle;
SetPosition(position);
end;
function Sensor.IsCollidingGround(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorGround));
end;
function Sensor.IsCollisionMain(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorMain));
end;
function Sensor.IsCollidingLeft(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorLeft));
end;
function Sensor.IsCollidingRight(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorRight));
end;
function Sensor.IsCollidingTop(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorTop));
end;
function Sensor.IsCollidingBottom(): boolean;
begin
Exit(terrain.IsCollidingWith(sensorBottom));
end;
function Sensor.IsCollidingSlopeLeft: boolean;
begin
Exit(terrain.IsCollidingWith(sensorSlopeLeft));
end;
function Sensor.IsCollidingSlopeRight: boolean;
begin
Exit(terrain.IsCollidingWith(sensorSlopeRight));
end;
function Sensor.CalculateAngle(): single;
var
i: integer;
pointLeft, pointRight: TVector2;
foundLeft, foundRight: boolean;
begin
foundLeft := False;
foundRight := False;
for i := 0 to 30 do
begin
if (not foundRight) then
begin
pointRight := Vector2Create(position.x - (10 + i) *
Sin(angle) + 8 * Cos(angle), position.y + (10 + i) *
Cos(angle) + 8 * Sin(angle));
if (terrain.IsCollidingWithPoint(pointRight)) then foundRight := True;
end;
if (not foundLeft) then
begin
pointLeft := Vector2Create(position.x - (10 + i) *
Sin(angle) - 8 * Cos(angle), position.y + (10 + i) *
Cos(angle) - 8 * Sin(angle));
if (terrain.IsCollidingWithPoint(pointLeft)) then foundLeft := True;
end;
end;
if (foundLeft and foundRight) then
begin
Exit(ArcTan2(pointRight.y - pointLeft.y, pointRight.x -
pointLeft.x));
end;
Exit(angle);
end;
procedure Sensor.Draw();
begin
sensorMain.Draw();
sensorGround.Draw();
sensorLeft.Draw();
sensorRight.Draw();
sensorTop.Draw();
sensorBottom.Draw();
sensorSlopeLeft.Draw();
sensorSlopeRight.Draw();
end;
end.