Changeset 193 for trunk/UMap.pas


Ignore:
Timestamp:
May 14, 2018, 5:02:00 PM (6 years ago)
Author:
chronos
Message:
  • Added: Isometric map type.
  • Modified: Removed unneeded explicit typecast for list items.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMap.pas

    r187 r193  
    1919  THexMap = class(TMap)
    2020  private
     21    const
     22      CellMulX = 1.12;
     23      CellMulY = 1.292;
    2124    function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean;
    2225    procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells);
     
    5760  end;
    5861
     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
    5976implementation
     77
     78{ TIsometricMap }
     79
     80function TIsometricMap.GetTilePolygon(Pos: TPoint; Size: TPoint): TPolygon;
     81begin
     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);
     87end;
     88
     89function TIsometricMap.IsValidIndex(Index: TPoint): Boolean;
     90begin
     91  Result := (Index.X >= 0) and (Index.X < Size.X) and
     92    (Index.Y >= 0) and (Index.Y < Size.Y);
     93end;
     94
     95procedure TIsometricMap.Generate;
     96var
     97  X, Y: Integer;
     98  NewCell: TCell;
     99  PX, PY: Double;
     100  P: TPoint;
     101begin
     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;
     138end;
    60139
    61140{ THexMap }
     
    136215var
    137216  X, Y: Integer;
     217  P: TPoint;
    138218begin
    139219  Neighbours.Count := 0;
    140220  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;
    145226  end;
    146227end;
     
    166247      //Y := Y + 0.5;
    167248    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));
    170251    NewCell.Polygon := GetHexagonPolygon(NewCell.PosPx, DefaultCellSize);
    171252    NewCell.Id := GetNewCellId;
     
    176257  for Y := 0 to Size.Y - 1 do
    177258  for X := 0 to Size.X - 1 do
    178   with TCell(Cells[Y * Size.X + X]) do begin
     259  with Cells[Y * Size.X + X] do begin
    179260    GetCellPosNeighbors(TPoint.Create(X, Y), Neighbors);
    180261  end;
     
    190271  X, Y: Integer;
    191272  NewCell: TCell;
     273  P: TPoint;
    192274begin
    193275  Clear;
     
    209291  for Y := 0 to Size.Y - 1 do
    210292  for X := 0 to Size.X - 1 do
    211   with TCell(Cells[Y * Size.X + X]) do begin
    212     if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then
    213       Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));
    214     if IsValidIndex(TPoint.Create(X + 0, Y + 1)) then
    215       Neighbors.Add(TCell(Cells[(Y + 1) * Size.X + (X + 0)]));
    216     if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then
    217       Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X - 1)]));
    218     if IsValidIndex(TPoint.Create(X + 0, Y - 1)) then
    219       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]);
    220302  end;
    221303
     
    332414  Reverse: Boolean;
    333415  NewCell: TCell;
     416  P: TPoint;
    334417begin
    335418  Clear;
     
    354437  for Y := 0 to Self.Size.Y - 1 do
    355438  for X := 0 to Size.X - 1 do
    356   with TCell(Cells[Y * Size.X + X]) do begin
     439  with Cells[Y * Size.X + X] do begin
    357440    if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1
    358441      else Rev := 1;
    359     if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then
    360       Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));
    361     if IsValidIndex(TPoint.Create(X + 0, Y - 1 * Rev)) then
    362       Neighbors.Add(TCell(Cells[(Y - 1 * Rev) * Size.X + (X + 0)]));
    363     if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then
    364       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]);
    365448  end;
    366449
Note: See TracChangeset for help on using the changeset viewer.