Changeset 49 for trunk


Ignore:
Timestamp:
Aug 17, 2014, 12:14:10 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Cells moved to base Map class as it will be no longer 2D array. Not it is simple list to support diferent mesh type and different map shape.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r48 r49  
    8989    MaxPower: Integer;
    9090    DefaultCellSize: TPoint;
     91    Cells: TObjectList; // TList<TCell>
    9192    procedure DrawArrow(Canvas: TCanvas; View: TView; Pos: TPoint; Angle: Double;
    9293      Size: TPoint; Text: string);
     
    118119    function GetCellNeighbors2(Cell: TCell): TCellArray;
    119120  public
    120     Cells: array of array of TCell;
    121121    procedure Assign(Source: TMap); override;
    122122    procedure LoadFromFile(FileName: string); override;
     
    141141    function GetCellNeighbors2(Cell: TCell): TCellArray;
    142142  public
    143     Cells: array of array of TCell;
    144143    procedure Generate; override;
    145144    function IsValidIndex(Index: TPoint): Boolean; override;
     
    337336  inherited;
    338337  // Free previous
    339   for Y := 0 to Length(Cells) - 1 do
    340   for X := 0 to Length(Cells[Y]) - 1 do begin
    341     TCell(Cells[Y, X]).Destroy;
    342   end;
     338  Cells.Count := 0;
    343339  // Allocate and init new
    344   SetLength(Cells, FSize.Y, FSize.X);
     340  Cells.Count := FSize.Y * FSize.X;
    345341  for Y := 0 to FSize.Y - 1 do
    346342  for X := 0 to FSize.X - 1 do begin
    347343    NewCell := TCell.Create;
    348344    NewCell.Pos := Point(X, Y);
    349     Cells[Y, X] := NewCell;
     345    Cells[Y * FSize.X + X] := NewCell;
    350346  end;
    351347  // Generate neightbours
    352348  for Y := 0 to FSize.Y - 1 do
    353349  for X := 0 to FSize.X - 1 do begin
    354     NeighCells := GetCellNeighbors2(Cells[Y, X]);
     350    NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X]));
    355351    for I := 0 to Length(NeighCells) - 1 do
    356       Cells[Y, X].Neighbors.Add(NeighCells[I]);
     352      TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]);
    357353  end;
    358354end;
     
    396392      Y := CY;
    397393      if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then
    398       if Cells[CY, CX].Terrain <> ttVoid then begin
     394      if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin
    399395        Frame := Rect(Trunc(X * CellSize.X - HexSize.X / 2),
    400396          Trunc(Y * CellSize.Y - HexSize.Y / 2),
     
    402398          Trunc(Y * CellSize.Y + HexSize.Y / 2));
    403399        if PtInRect(Frame, Pos) then begin
    404           Result := Cells[CY, CX];
     400          Result := TCell(Cells[CY * FSize.X + CX]);
    405401          Exit;
    406402        end;
     
    434430  for X := -1 to 1 do
    435431  if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    436   IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
     432  IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin
    437433    SetLength(Result, Length(Result) + 1);
    438     Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];
     434    Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]);
    439435  end;
    440436end;
     
    449445  for Y := 0 to Size.Y - 1 do
    450446  for X := 0 to Size.X - 1 do
    451     Result[Y * Size.X + X] := Cells[Y, X];
     447    Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]);
    452448end;
    453449
     
    476472begin
    477473  with Canvas do begin
    478     if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY, CX])) then begin
     474    if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY * FSize.X + CX])) then begin
    479475      Pen.Color := clYellow;
    480476      Pen.Style := psSolid;
    481477      Pen.Width := 1;
    482478    end else
    483     if TCell(Cells[CY, CX]).Terrain = ttCity then begin
     479    if TCell(Cells[CY * FSize.X + CX]).Terrain = ttCity then begin
    484480      // Cannot set clear border as it will display shifted on gtk2
    485481      //Pen.Style := psClear;
     
    514510      Y := CY;
    515511      if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then begin
    516         Cell := Cells[CY, CX];
     512        Cell := TCell(Cells[CY * FSize.X + CX]);
    517513        if Cell.Terrain <> ttVoid then begin
    518           if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY, CX])) then Brush.Color := clGreen
    519             else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY, CX])) then Brush.Color := clPurple
     514          if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clGreen
     515            else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clPurple
    520516            else Brush.Color := Cell.GetColor;
    521517          Pen.Color := clBlack;
     
    686682
    687683procedure TMap.Generate;
    688 begin
     684var
     685  X, Y: Integer;
     686  I: Integer;
     687  NewCell: TCell;
     688  NeighCells: TCellArray;
     689begin
     690  inherited;
     691  // Free previous
     692  Cells.Count := 0;
     693  // Allocate and init new
     694  Cells.Count := FSize.Y * FSize.X;
     695  for Y := 0 to FSize.Y - 1 do
     696  for X := 0 to FSize.X - 1 do begin
     697    NewCell := TCell.Create;
     698    NewCell.Pos := Point(X, Y);
     699    Cells[Y * FSize.X + X] := NewCell;
     700  end;
    689701end;
    690702
     
    693705  MaxPower := 99;
    694706  DefaultCellSize := Point(62, 62);
     707  Cells := TObjectList.create;
    695708end;
    696709
     
    698711begin
    699712  Size := Point(0, 0);
     713  FreeAndNil(Cells);
    700714  inherited Destroy;
    701715end;
     
    15191533  for X := -1 to 1 do
    15201534  if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    1521   IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin
     1535  IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin
    15221536    SetLength(Result, Length(Result) + 1);
    1523     Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];
     1537    Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]);
    15241538  end;
    15251539end;
     
    15471561      end;
    15481562      if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then
    1549       if Cells[CY, CX].Terrain <> ttVoid then begin
     1563      if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin
    15501564        Points := GetHexagonPolygon(Point(Trunc(X * CellSize.X),
    15511565          Trunc(Y * CellSize.Y)),
    15521566          Point(Trunc(HexSize.X), Trunc(HexSize.Y)));
    15531567        if PtInPoly(Points, Pos) then begin
    1554           Result := Cells[CY, CX];
     1568          Result := TCell(Cells[CY * FSize.X + CX]);
    15551569          Exit;
    15561570        end;
     
    15991613begin
    16001614  with Canvas do begin
    1601     if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY, CX])) then begin
     1615    if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY * FSize.X + CX])) then begin
    16021616      Pen.Color := clYellow;
    16031617      Pen.Style := psSolid;
    16041618      Pen.Width := 1;
    16051619    end else
    1606     if TCell(Cells[CY, CX]).Terrain = ttCity then begin
     1620    if TCell(Cells[CY * FSize.X + CX]).Terrain = ttCity then begin
    16071621      // Cannot set clear border as it will display shifted on gtk2
    16081622      //Pen.Style := psClear;
     
    16431657      end;
    16441658      if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then begin
    1645         Cell := Cells[CY, CX];
     1659        Cell := TCell(Cells[CY * FSize.X + CX]);
    16461660        if Cell.Terrain <> ttVoid then begin
    1647           if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY, CX])) then Brush.Color := clGreen
    1648             else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY, CX])) then Brush.Color := clPurple
     1661          if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clGreen
     1662            else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clPurple
    16491663            else Brush.Color := Cell.GetColor;
    16501664          Pen.Color := clBlack;
     
    16841698  inherited;
    16851699  // Free previous
    1686   for Y := 0 to Length(Cells) - 1 do
    1687   for X := 0 to Length(Cells[Y]) - 1 do begin
    1688     TCell(Cells[Y, X]).Destroy;
    1689   end;
     1700  Cells.Count := 0;
    16901701  // Allocate and init new
    1691   SetLength(Cells, FSize.Y, FSize.X);
     1702  Cells.Count := FSize.Y * FSize.X;
    16921703  for Y := 0 to FSize.Y - 1 do
    16931704  for X := 0 to FSize.X - 1 do begin
    16941705    NewCell := TCell.Create;
    16951706    NewCell.Pos := Point(X, Y);
    1696     Cells[Y, X] := NewCell;
     1707    Cells[Y * FSize.X + X] := NewCell;
    16971708  end;
    16981709  // Generate neightbours
    16991710  for Y := 0 to FSize.Y - 1 do
    17001711  for X := 0 to FSize.X - 1 do begin
    1701     NeighCells := GetCellNeighbors2(Cells[Y, X]);
     1712    NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X]));
    17021713    for I := 0 to Length(NeighCells) - 1 do
    1703       Cells[Y, X].Neighbors.Add(NeighCells[I]);
     1714      TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]);
    17041715  end;
    17051716end;
     
    17251736  for Y := 0 to Size.Y - 1 do
    17261737  for X := 0 to Size.X - 1 do
    1727     Result[Y * Size.X + X] := Cells[Y, X];
     1738    Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]);
    17281739end;
    17291740
Note: See TracChangeset for help on using the changeset viewer.