source: trunk/World.pas

Last change on this file was 73, checked in by chronos, 8 months ago
  • Modified: Removed U prefix from unit names.
File size: 4.9 KB
Line 
1unit World;
2
3interface
4
5uses
6 Classes, SysUtils, Graphics, GraphType, SpecializedMatrix, Matter;
7
8type
9 { TWorld }
10
11 TWorld = class
12 private
13 function GetSize: TMatrixByteIndex;
14 procedure SetSize(const AValue: TMatrixByteIndex);
15 procedure InitMatter;
16 public
17 Surface: TMatrixByte;
18 Matters: TMatters;
19 procedure Generate;
20 constructor Create;
21 destructor Destroy; override;
22 procedure DrawToBitmap(Bitmap: TBitmap);
23 property Size: TMatrixByteIndex read GetSize write SetSize;
24 end;
25
26
27implementation
28
29uses
30 FastPixel;
31
32{ TWorld }
33
34function TWorld.GetSize: TMatrixByteIndex;
35begin
36 Result := Surface.Count;
37end;
38
39procedure TWorld.SetSize(const AValue: TMatrixByteIndex);
40begin
41 Surface.Count := AValue;
42 Generate;
43end;
44
45procedure TWorld.InitMatter;
46var
47 I: Integer;
48begin
49 // Space
50 with Matters.AddNew do begin
51 Kind := mkSpace;
52 Color := clBlack;
53 Player := -1;
54 end;
55 // Dirt1
56 with Matters.AddNew do begin
57 Kind := mkDirt;
58 Color := $0756b0;
59 Player := -1;
60 Diggable := True;
61 end;
62 // Dirt2
63 with Matters.AddNew do begin
64 Kind := mkDirt;
65 Color := $2170c3;
66 Player := -1;
67 Diggable := True;
68 end;
69 // Rock
70 with Matters.AddNew do begin
71 Kind := mkRock;
72 Color := TColor($9a9a9a);
73 Player := -1;
74 Blocking := True;
75 end;
76 // Bullet1
77 with Matters.AddNew do begin
78 Kind := mkBullet;
79 Color := clRed;
80 Player := -1;
81 end;
82 // Bullet2
83 with Matters.AddNew do begin
84 Kind := mkBullet;
85 Color := clRed;
86 Player := -1;
87 end;
88 // Border
89 with Matters.AddNew do begin
90 Kind := mkBorder;
91 Color := clBlack;
92 Player := -1;
93 end;
94
95 for I := 0 to 7 do begin
96 // Player cannon
97 with Matters.AddNew do begin
98 Kind := mkTankBody;
99 Player := I;
100 Blocking := True;
101 end;
102 // Player home
103 with Matters.AddNew do begin
104 Kind := mkHome;
105 Player := I;
106 Blocking := True;
107 end;
108 // Player body1
109 with Matters.AddNew do begin
110 Kind := mkTankBody;
111 Player := I;
112 Blocking := True;
113 end;
114 // Player body2
115 with Matters.AddNew do begin
116 Kind := mkTankBody;
117 Player := I;
118 Blocking := True;
119 end;
120 end;
121end;
122
123procedure TWorld.Generate;
124var
125 X, Y: Integer;
126 Distance: Double;
127 Delta: Double;
128begin
129 for Y := 0 to Surface.Count.Y - 1 do
130 for X := 0 to Surface.Count.X - 1 do begin
131 if Random < 0.5 then
132 Surface.ItemsXY[X, Y] := Byte(miDirt1) else
133 Surface.ItemsXY[X, Y] := Byte(miDirt2);
134 end;
135
136 Distance := 0.1 * Surface.Count.X;
137 Delta := 0;
138 for Y := 0 to Surface.Count.Y - 1 do begin
139 for X := 0 to Round(Distance) - 1 do begin
140 Surface.ItemsXY[X, Y] := Byte(miRock);
141 end;
142 Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.X) * 2 - 1);
143 Distance := Distance + Delta;
144 end;
145
146 Distance := 0.1 * Surface.Count.X;
147 Delta := 0;
148 for Y := 0 to Surface.Count.Y - 1 do begin
149 for X := 0 to Round(Distance) - 1 do begin
150 Surface.ItemsXY[Surface.Count.X - 1 - X, Y] := Byte(miRock);
151 end;
152 Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.X) * 2 - 1);
153 Distance := Distance + Delta;
154 end;
155
156 Distance := 0.1 * Surface.Count.Y;
157 Delta := 0;
158 for X := 0 to Surface.Count.X - 1 do begin
159 for Y := 0 to Round(Distance) - 1 do begin
160 Surface.ItemsXY[X, Y] := Byte(miRock);
161 end;
162 Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.Y) * 2 - 1);
163 Distance := Distance + Delta;
164 end;
165
166 Distance := 0.1 * Surface.Count.Y;
167 Delta := 0;
168 for X := 0 to Surface.Count.X - 1 do begin
169 for Y := 0 to Round(Distance) - 1 do begin
170 Surface.ItemsXY[X, Surface.Count.Y - 1 - Y] := Byte(miRock);
171 end;
172 Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.Y) * 2 - 1);
173 Distance := Distance + Delta;
174 end;
175end;
176
177constructor TWorld.Create;
178var
179 NewSize: TMatrixByteIndex;
180begin
181 Matters := TMatters.Create;
182 InitMatter;
183 Surface := TMatrixByte.Create;
184 NewSize.X := 800;
185 NewSize.Y := 300;
186 Size := NewSize;
187end;
188
189destructor TWorld.Destroy;
190begin
191 FreeAndNil(Surface);
192 FreeAndNil(Matters);
193 inherited;
194end;
195
196procedure TWorld.DrawToBitmap(Bitmap: TBitmap);
197var
198 X, Y: Integer;
199 PixelPtr: PInteger;
200 PixelRowPtr: PInteger;
201 RawImage: TRawImage;
202 BytePerPixel: Integer;
203 P: Integer;
204begin
205 Bitmap.BeginUpdate;
206 try
207 RawImage := Bitmap.RawImage;
208 PixelRowPtr := PInteger(RawImage.Data);
209 BytePerPixel := RawImage.Description.BitsPerPixel div 8;
210 for Y := 0 to Bitmap.Height - 1 do begin
211 PixelPtr := PixelRowPtr;
212 for X := 0 to Bitmap.Width - 1 do begin
213 P := Matters[Surface.ItemsXY[Trunc(X / Bitmap.Width * Surface.Count.X),
214 Trunc(Y / Bitmap.Height * Surface.Count.Y)]].Color;
215 PixelPtr^ := SwapBRComponent(P);
216 Inc(PByte(PixelPtr), BytePerPixel);
217 end;
218 Inc(PByte(PixelRowPtr), RawImage.Description.BytesPerLine);
219 end;
220 finally
221 Bitmap.EndUpdate;
222 end;
223end;
224
225
226end.
227
Note: See TracBrowser for help on using the repository browser.