Changeset 193 for trunk/UMap.pas
- Timestamp:
- May 14, 2018, 5:02:00 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UMap.pas
r187 r193 19 19 THexMap = class(TMap) 20 20 private 21 const 22 CellMulX = 1.12; 23 CellMulY = 1.292; 21 24 function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean; 22 25 procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells); … … 57 60 end; 58 61 62 { TIsometricMap } 63 64 TIsometricMap = class(TMap) 65 private 66 const 67 CellMulX = 0.95; 68 CellMulY = 3.35; 69 function GetTilePolygon(Pos: TPoint; Size: TPoint): TPolygon; 70 public 71 function IsValidIndex(Index: TPoint): Boolean; override; 72 procedure Generate; override; 73 end; 74 75 59 76 implementation 77 78 { TIsometricMap } 79 80 function TIsometricMap.GetTilePolygon(Pos: TPoint; Size: TPoint): TPolygon; 81 begin 82 SetLength(Result.Points, 4); 83 Result.Points[0] := TPoint.Create(Pos.X, Trunc(Pos.Y - Size.Y / 3.5)); 84 Result.Points[1] := TPoint.Create(Trunc(Pos.X + Size.X / 2), Pos.Y); 85 Result.Points[2] := TPoint.Create(Pos.X, Trunc(Pos.Y + Size.Y / 3.5)); 86 Result.Points[3] := TPoint.Create(Trunc(Pos.X - Size.X / 2), Pos.Y); 87 end; 88 89 function TIsometricMap.IsValidIndex(Index: TPoint): Boolean; 90 begin 91 Result := (Index.X >= 0) and (Index.X < Size.X) and 92 (Index.Y >= 0) and (Index.Y < Size.Y); 93 end; 94 95 procedure TIsometricMap.Generate; 96 var 97 X, Y: Integer; 98 NewCell: TCell; 99 PX, PY: Double; 100 P: TPoint; 101 begin 102 Clear; 103 104 // Allocate and init new 105 Cells.Count := Size.Y * Size.X; 106 for Y := 0 to Size.Y - 1 do 107 for X := 0 to Size.X - 1 do begin 108 NewCell := TCell.Create; 109 NewCell.Map := Self; 110 PX := X; 111 PY := Y; 112 if (Y and 1) = 1 then begin 113 PX := PX + 0.5; 114 //Y := Y + 0.5; 115 end; 116 NewCell.PosPx := TPoint.Create(Trunc(PX * DefaultCellSize.X / CellMulX), 117 Trunc(PY * DefaultCellSize.Y / CellMulY)); 118 NewCell.Polygon := GetTilePolygon(NewCell.PosPx, DefaultCellSize); 119 NewCell.Id := GetNewCellId; 120 Cells[Y * Size.X + X] := NewCell; 121 end; 122 123 // Generate neightbours 124 for Y := 0 to Size.Y - 1 do 125 for X := 0 to Size.X - 1 do 126 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]); 135 end; 136 137 FPixelRect := CalculatePixelRect; 138 end; 60 139 61 140 { THexMap } … … 136 215 var 137 216 X, Y: Integer; 217 P: TPoint; 138 218 begin 139 219 Neighbours.Count := 0; 140 220 for Y := -1 to 1 do 141 for X := -1 to 1 do 142 if IsValidIndex(TPoint.Create(CellPos.X + X, CellPos.Y + Y)) and 143 IsCellsPosNeighbor(CellPos, TPoint.Create((CellPos.X + X), (CellPos.Y + Y))) then begin 144 Neighbours.Add(TCell(Cells[(CellPos.Y + Y) * Size.X + (CellPos.X + X)])); 221 for X := -1 to 1 do begin 222 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]); 225 end; 145 226 end; 146 227 end; … … 166 247 //Y := Y + 0.5; 167 248 end; 168 NewCell.PosPx := TPoint.Create(Trunc(PX * DefaultCellSize.X / HexCellMulX),169 Trunc(PY * DefaultCellSize.Y / HexCellMulY));249 NewCell.PosPx := TPoint.Create(Trunc(PX * DefaultCellSize.X / CellMulX), 250 Trunc(PY * DefaultCellSize.Y / CellMulY)); 170 251 NewCell.Polygon := GetHexagonPolygon(NewCell.PosPx, DefaultCellSize); 171 252 NewCell.Id := GetNewCellId; … … 176 257 for Y := 0 to Size.Y - 1 do 177 258 for X := 0 to Size.X - 1 do 178 with TCell(Cells[Y * Size.X + X])do begin259 with Cells[Y * Size.X + X] do begin 179 260 GetCellPosNeighbors(TPoint.Create(X, Y), Neighbors); 180 261 end; … … 190 271 X, Y: Integer; 191 272 NewCell: TCell; 273 P: TPoint; 192 274 begin 193 275 Clear; … … 209 291 for Y := 0 to Size.Y - 1 do 210 292 for X := 0 to Size.X - 1 do 211 with TCell(Cells[Y * Size.X + X])do begin212 if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then213 Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));214 if IsValidIndex(TPoint.Create(X + 0, Y + 1)) then215 Neighbors.Add(TCell(Cells[(Y + 1) * Size.X + (X + 0)]));216 if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then217 Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X - 1)]));218 if IsValidIndex(TPoint.Create(X + 0, Y - 1)) then219 Neighbors.Add(TCell(Cells[(Y - 1) * Size.X + (X + 0)]));293 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]); 220 302 end; 221 303 … … 332 414 Reverse: Boolean; 333 415 NewCell: TCell; 416 P: TPoint; 334 417 begin 335 418 Clear; … … 354 437 for Y := 0 to Self.Size.Y - 1 do 355 438 for X := 0 to Size.X - 1 do 356 with TCell(Cells[Y * Size.X + X])do begin439 with Cells[Y * Size.X + X] do begin 357 440 if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1 358 441 else Rev := 1; 359 if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then360 Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));361 if IsValidIndex(TPoint.Create(X + 0, Y - 1 * Rev)) then362 Neighbors.Add(TCell(Cells[(Y - 1 * Rev) * Size.X + (X + 0)]));363 if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then364 Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X - 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]); 365 448 end; 366 449
Note:
See TracChangeset
for help on using the changeset viewer.