Changeset 229 for trunk/UMap.pas
- Timestamp:
- Sep 18, 2018, 6:11:18 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UMap.pas
r193 r229 23 23 CellMulY = 1.292; 24 24 function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean; 25 procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells);25 procedure GetCellPosNeighbors(CellPos: TPoint; Cell: TCell); 26 26 function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon; 27 27 public 28 28 procedure LoadFromFile(FileName: string); override; 29 29 procedure SaveToFile(FileName: string); override; 30 function IsValidIndex(Index: TPoint): Boolean; override;31 30 procedure Generate; override; 32 31 end; … … 38 37 function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon; 39 38 public 40 function IsValidIndex(Index: TPoint): Boolean; override;41 39 procedure Generate; override; 42 40 end; … … 48 46 function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPolygon; 49 47 public 50 function IsValidIndex(Index: TPoint): Boolean; override;51 48 procedure Generate; override; 52 49 end; … … 69 66 function GetTilePolygon(Pos: TPoint; Size: TPoint): TPolygon; 70 67 public 71 function IsValidIndex(Index: TPoint): Boolean; override;72 68 procedure Generate; override; 73 69 end; … … 85 81 Result.Points[2] := TPoint.Create(Pos.X, Trunc(Pos.Y + Size.Y / 3.5)); 86 82 Result.Points[3] := TPoint.Create(Trunc(Pos.X - Size.X / 2), Pos.Y); 87 end;88 89 function TIsometricMap.IsValidIndex(Index: TPoint): Boolean;90 begin91 Result := (Index.X >= 0) and (Index.X < Size.X) and92 (Index.Y >= 0) and (Index.Y < Size.Y);93 83 end; 94 84 … … 125 115 for X := 0 to Size.X - 1 do 126 116 with Cells[Y * Size.X + X] do begin 127 P := TPoint.Create(X + 0 + (Y mod 2), Y + 1); 128 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 129 P := TPoint.Create(X - 1 + (Y mod 2), Y + 1); 130 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 131 P := TPoint.Create(X + 0 + (Y mod 2), Y - 1); 132 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 133 P := TPoint.Create(X - 1 + (Y mod 2), Y - 1); 134 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 117 if Cyclic then begin 118 P := TPoint.Create(X + 0 + (Y mod 2), Y + 1); 119 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 120 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 121 P := TPoint.Create(X - 1 + (Y mod 2), Y + 1); 122 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 123 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 124 P := TPoint.Create(X + 0 + (Y mod 2), Y - 1); 125 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 126 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 127 P := TPoint.Create(X - 1 + (Y mod 2), Y - 1); 128 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 129 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 130 end else begin 131 P := TPoint.Create(X + 0 + (Y mod 2), Y + 1); 132 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 133 P := TPoint.Create(X - 1 + (Y mod 2), Y + 1); 134 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 135 P := TPoint.Create(X + 0 + (Y mod 2), Y - 1); 136 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 137 P := TPoint.Create(X - 1 + (Y mod 2), Y - 1); 138 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 139 end; 135 140 end; 136 141 … … 206 211 end; 207 212 208 function THexMap.IsValidIndex(Index: TPoint): Boolean; 209 begin 210 Result := (Index.X >= 0) and (Index.X < Size.X) and 211 (Index.Y >= 0) and (Index.Y < Size.Y); 212 end; 213 214 procedure THexMap.GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells); 213 procedure THexMap.GetCellPosNeighbors(CellPos: TPoint; Cell: TCell); 215 214 var 216 215 X, Y: Integer; 217 216 P: TPoint; 218 begin 219 Neighbours.Count := 0; 217 PMod: TPoint; 218 begin 220 219 for Y := -1 to 1 do 221 220 for X := -1 to 1 do begin 222 221 P := TPoint.Create(CellPos.X + X, CellPos.Y + Y); 223 if IsValidIndex(P) and IsCellsPosNeighbor(CellPos, P) then begin 224 Neighbours.Add(Cells[P.Y * Size.X + P.X]); 222 PMod := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 223 if Cyclic then begin 224 if IsValidIndex(PMod) and IsCellsPosNeighbor(CellPos, P) then begin 225 Cell.ConnectTo(Cells[PMod.Y * Size.X + PMod.X]); 226 end; 227 end else begin 228 if IsValidIndex(P) and IsCellsPosNeighbor(CellPos, P) then begin 229 Cell.ConnectTo(Cells[P.Y * Size.X + P.X]); 230 end; 225 231 end; 226 232 end; … … 258 264 for X := 0 to Size.X - 1 do 259 265 with Cells[Y * Size.X + X] do begin 260 GetCellPosNeighbors(TPoint.Create(X, Y), Neighbors);266 GetCellPosNeighbors(TPoint.Create(X, Y), Cells[Y * Size.X + X]); 261 267 end; 262 268 … … 292 298 for X := 0 to Size.X - 1 do 293 299 with Cells[Y * Size.X + X] do begin 294 P := TPoint.Create(X + 1, Y + 0); 295 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 296 P := TPoint.Create(X + 0, Y + 1); 297 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 298 P := TPoint.Create(X - 1, Y + 0); 299 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 300 P := TPoint.Create(X + 0, Y - 1); 301 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 300 if Cyclic then begin 301 P := TPoint.Create(X + 1, Y + 0); 302 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 303 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 304 P := TPoint.Create(X + 0, Y + 1); 305 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 306 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 307 P := TPoint.Create(X - 1, Y + 0); 308 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 309 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 310 P := TPoint.Create(X + 0, Y - 1); 311 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 312 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 313 end else begin 314 P := TPoint.Create(X + 1, Y + 0); 315 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 316 P := TPoint.Create(X + 0, Y + 1); 317 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 318 P := TPoint.Create(X - 1, Y + 0); 319 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 320 P := TPoint.Create(X + 0, Y - 1); 321 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 322 end; 302 323 end; 303 324 304 325 FPixelRect := CalculatePixelRect; 305 end;306 307 function TSquareMap.IsValidIndex(Index: TPoint): Boolean;308 begin309 Result := (Index.X >= 0) and (Index.X < Size.X) and310 (Index.Y >= 0) and (Index.Y < Size.Y);311 326 end; 312 327 … … 402 417 end; 403 418 404 function TTriangleMap.IsValidIndex(Index: TPoint): Boolean;405 begin406 Result := (Index.X >= 0) and (Index.X < Size.X) and407 (Index.Y >= 0) and (Index.Y < Size.Y);408 end;409 410 419 procedure TTriangleMap.Generate; 411 420 var … … 440 449 if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1 441 450 else Rev := 1; 442 P := TPoint.Create(X + 1, Y + 0); 443 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 444 P := TPoint.Create(X + 0, Y - 1 * Rev); 445 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 446 P := TPoint.Create(X - 1, Y + 0); 447 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 451 if Cyclic then begin 452 P := TPoint.Create(X + 1, Y + 0); 453 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 454 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 455 P := TPoint.Create(X + 0, Y - 1 * Rev); 456 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 457 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 458 P := TPoint.Create(X - 1, Y + 0); 459 P := TPoint.Create((P.X + Size.X) mod Size.X, (P.Y + Size.Y) mod Size.Y); 460 Neighbors.Add(Cells[P.Y * Size.X + P.X]); 461 end else begin 462 P := TPoint.Create(X + 1, Y + 0); 463 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 464 P := TPoint.Create(X + 0, Y - 1 * Rev); 465 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 466 P := TPoint.Create(X - 1, Y + 0); 467 if IsValidIndex(P) then Neighbors.Add(Cells[P.Y * Size.X + P.X]); 468 end; 448 469 end; 449 470
Note:
See TracChangeset
for help on using the changeset viewer.