Changeset 229 for trunk/UMap.pas


Ignore:
Timestamp:
Sep 18, 2018, 6:11:18 PM (6 years ago)
Author:
chronos
Message:
  • Added: Support for cyclic map. Movement across map borders will take player units to opposite map border.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMap.pas

    r193 r229  
    2323      CellMulY = 1.292;
    2424    function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean;
    25     procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells);
     25    procedure GetCellPosNeighbors(CellPos: TPoint; Cell: TCell);
    2626    function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon;
    2727  public
    2828    procedure LoadFromFile(FileName: string); override;
    2929    procedure SaveToFile(FileName: string); override;
    30     function IsValidIndex(Index: TPoint): Boolean; override;
    3130    procedure Generate; override;
    3231  end;
     
    3837    function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon;
    3938  public
    40     function IsValidIndex(Index: TPoint): Boolean; override;
    4139    procedure Generate; override;
    4240  end;
     
    4846    function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPolygon;
    4947  public
    50     function IsValidIndex(Index: TPoint): Boolean; override;
    5148    procedure Generate; override;
    5249  end;
     
    6966    function GetTilePolygon(Pos: TPoint; Size: TPoint): TPolygon;
    7067  public
    71     function IsValidIndex(Index: TPoint): Boolean; override;
    7268    procedure Generate; override;
    7369  end;
     
    8581  Result.Points[2] := TPoint.Create(Pos.X, Trunc(Pos.Y + Size.Y / 3.5));
    8682  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);
    9383end;
    9484
     
    125115  for X := 0 to Size.X - 1 do
    126116  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;
    135140  end;
    136141
     
    206211end;
    207212
    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);
     213procedure THexMap.GetCellPosNeighbors(CellPos: TPoint; Cell: TCell);
    215214var
    216215  X, Y: Integer;
    217216  P: TPoint;
    218 begin
    219   Neighbours.Count := 0;
     217  PMod: TPoint;
     218begin
    220219  for Y := -1 to 1 do
    221220  for X := -1 to 1 do begin
    222221    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;
    225231    end;
    226232  end;
     
    258264  for X := 0 to Size.X - 1 do
    259265  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]);
    261267  end;
    262268
     
    292298  for X := 0 to Size.X - 1 do
    293299  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;
    302323  end;
    303324
    304325  FPixelRect := CalculatePixelRect;
    305 end;
    306 
    307 function TSquareMap.IsValidIndex(Index: TPoint): Boolean;
    308 begin
    309   Result := (Index.X >= 0) and (Index.X < Size.X) and
    310     (Index.Y >= 0) and (Index.Y < Size.Y);
    311326end;
    312327
     
    402417end;
    403418
    404 function TTriangleMap.IsValidIndex(Index: TPoint): Boolean;
    405 begin
    406   Result := (Index.X >= 0) and (Index.X < Size.X) and
    407     (Index.Y >= 0) and (Index.Y < Size.Y);
    408 end;
    409 
    410419procedure TTriangleMap.Generate;
    411420var
     
    440449    if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1
    441450      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;
    448469  end;
    449470
Note: See TracChangeset for help on using the changeset viewer.