-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuUtils.pas
More file actions
198 lines (167 loc) · 6.9 KB
/
uUtils.pas
File metadata and controls
198 lines (167 loc) · 6.9 KB
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
unit uUtils;
interface
uses FMX.Graphics, system.Types, system.Math, system.UITypes, system.Classes, System.IOUtils,
system.math.Vectors;
type
// Structure pour les voitures adverses
TOpponentCar = record
Position: Single; // Position Z sur la route (comme FPosition)
X: Single; // Position latérale (-1 à 1, comme FPlayerX)
Speed: Single; // Vitesse de la voiture
Image: TBitmap; // Image de la voiture
BaseSpeed: Single; // Vitesse de base (pour revenir après collision)
TargetX: Single; // Position cible pour l'IA
LaneChangeTimer: Single; // Timer pour changements de voie
end;
TRoadSegment = record
Index: Integer;
Point: TPointF; // Position projetée à l'écran
World: TPointF; // Position dans le monde (X, Z)
Scale: Single; // Échelle de projection
Curve: Single; // Courbure
Y: Single; // Hauteur (collines)
Clip: Single; // Hauteur de clipping à l'écran
end;
TSpriteType = (stTree1, stTree2, stSign1, stSign2, stSign3, stStart, stLeft, stRight);
TSprite = record
SegmentIndex: Integer;
Offset: Single; // Décalage par rapport au centre de la route
SpriteType: TSpriteType;
end;
procedure loadImage(anImage : TBitmap; name: string);
function Accelerate(V, Accel, Dt: Single): Single;
function Interpolate(A, B, Percent: Single): Single;
function InterpolationInOutCubic(T: Single): Single;
procedure drawPolygon(aCanvas: TCanvas; aColor: Talphacolor; prevSegment, segment : TRoadSegment; aWidth, FWidth :single);
procedure drawSprite(Canvas: TCanvas; DestRect: TRectF; SpriteImage: TBitmap; ClipY, FWidth, FHeight: single);
procedure Project(var Segment: TRoadSegment; CamX, CamY, CamZ, FWidth, FHeight, FCameraDepth: Single);
procedure drawBackground(anImage: TBitmap; anOffset : integer; aCanvas: TCanvas; FWidth, FHeight : integer);
procedure drawScanline(aCanvas : TCanvas; FWidth, FHeight : integer);
procedure SortOpponentCars(var Cars: TArray<TOpponentCar>);
const
COLORS: array[0..8] of TAlphaColor = (
$FF6B6B6B, // Route foncée
$FF808080, // Route claire
$FFFF0000, // Bord rouge
$FFFFFFFF, // Bord blanc
$FFAA0000, // Bord rouge foncé
$FFBBBBBB, // Bord blanc foncé
$55BBEE88, // Fossé
$FF16AB3F, // Herbe claire
$FF009A00 // Herbe foncée
);
implementation
procedure loadImage(anImage : TBitmap; name: string);
begin
var stream := TResourceStream.Create(HInstance, System.IOUtils.TPath.GetFileNameWithoutExtension(name), RT_RCDATA);
anImage.LoadFromStream(stream);
stream.Free;
end;
function Accelerate(V, Accel, Dt: Single): Single;
begin
Result := V + (Accel * Dt);
end;
function Interpolate(A, B, Percent: Single): Single;
begin
Result := A + (B - A) * Percent;
end;
function InterpolationInOutCubic(T: Single): Single;
begin
result := if T < 0.5 then 4 * T * T * T
else 1 - Power(-2 * T + 2, 3) / 2;
end;
procedure drawPolygon(aCanvas: TCanvas; aColor: Talphacolor; prevSegment, segment : TRoadSegment; aWidth, FWidth :single);
begin
aCanvas.Fill.Color := aColor;
var X1 := prevSegment.Point.X - (prevSegment.Scale * aWidth * FWidth * 0.5);
var W1 := prevSegment.Scale * aWidth * FWidth;
var X2 := segment.Point.X - (segment.Scale * aWidth * FWidth * 0.5);
var W2 := segment.Scale * aWidth * FWidth;
var Polygon : TPolygon;
SetLength(Polygon, 4);
Polygon[0] := PointF(X1, round(prevSegment.Point.Y)+1); // + 1 pour être sur de couvrir les éventuel "trous" car point.Y est single
Polygon[1] := PointF(X1 + W1, round(prevSegment.Point.Y)+1);
Polygon[2] := PointF(X2 + W2, round(segment.Point.Y)+1);
Polygon[3] := PointF(X2, round(segment.Point.Y)+1);
aCanvas.Stroke.Kind := TBrushKind.None;
aCanvas.FillPolygon(Polygon, 1);
end;
procedure drawSprite(Canvas: TCanvas; DestRect: TRectF; SpriteImage: TBitmap; ClipY, FWidth, FHeight: single);
begin
// Le sprite ne doit être dessiné que si son BAS est AU-DESSUS de ClipY
if DestRect.Bottom > ClipY then begin
// Le sprite est partiellement ou totalement masqué par le terrain devant
if DestRect.Top > ClipY then
Exit; // Complètement masqué (tout le sprite est en dessous de l'horizon)
// Partiellement masqué : clipper la partie basse
var ClipRatio := (DestRect.Bottom - ClipY) / (DestRect.Bottom - DestRect.Top);
var SrcClipHeight := SpriteImage.Height * ClipRatio;
Canvas.DrawBitmap(SpriteImage,
RectF(0, 0, SpriteImage.Width, SpriteImage.Height - SrcClipHeight),
RectF(DestRect.Left, DestRect.Top, DestRect.Right, ClipY),
1, False);
end else begin
// Sprite entièrement visible (son bas est au-dessus de l'horizon)
if (DestRect.Bottom > 0) and (DestRect.Top < FHeight) and
(DestRect.Right > 0) and (DestRect.Left < FWidth) then begin
Canvas.DrawBitmap(SpriteImage,
RectF(0, 0, SpriteImage.Width, SpriteImage.Height),
DestRect, 1, False);
end;
end;
end;
procedure Project(var Segment: TRoadSegment; CamX, CamY, CamZ, FWidth, FHeight, FCameraDepth: Single);
begin
var PosX := Segment.World.X - CamX;
var PosY := Segment.Y - CamY;
var PosZ := Segment.World.Y - CamZ;
if PosZ <= FCameraDepth then begin
Segment.Scale := 0;
Exit;
end;
Segment.Scale := FCameraDepth / PosZ;
Segment.Point.X := FWidth * 0.5 + (Segment.Scale * PosX * FWidth * 0.5);
Segment.Point.Y := FHeight * 0.5 - (Segment.Scale * PosY * FHeight * 0.5);
Segment.Clip := Segment.Point.Y;
end;
procedure drawBackground(anImage: TBitmap; anOffset : integer; aCanvas: TCanvas; FWidth, FHeight : integer);
begin
var BgWidth := anImage.Width;
var BgHeight := anImage.Height;
// Normaliser l'offset
var NormalizedOffset := anOffset mod BgWidth;
if NormalizedOffset > 0 then
NormalizedOffset := NormalizedOffset - BgWidth;
// Calculer combien de copies on doit dessiner pour remplir l'écran
var StartX := NormalizedOffset;
var NumCopies := Ceil(FWidth / BgWidth) + 2; // +2 pour être sûr de tout couvrir
for var i := 0 to NumCopies - 1 do begin
var DestX := StartX + (i * BgWidth);
aCanvas.DrawBitmap(anImage,
RectF(0, 0, BgWidth, BgHeight),
RectF(DestX, 0, DestX + BgWidth, BgHeight),
1, False);
end;
end;
procedure drawScanline(aCanvas : TCanvas; FWidth, FHeight : integer);
begin
aCanvas.Fill.Color := TAlphacolorrec.Black;
for var i := 0 to FHeight-1 do begin
if i mod 4 = 0 then begin
aCanvas.FillRect(RectF(0, i, FWidth, i+1), 0, 0, [], 0.5);
end;
end;
end;
procedure SortOpponentCars(var Cars: TArray<TOpponentCar>);
begin
for var i := 1 to Length(Cars) -1 do begin
var Key := Cars[i];
var j := i - 1;
while (j >= 0) and (Cars[j].Position < Key.Position) do begin
Cars[j + 1] := Cars[j];
Dec(j);
end;
Cars[j + 1] := Key;
end;
end;
end.