Changeset 52 for trunk


Ignore:
Timestamp:
Aug 17, 2014, 2:10:09 PM (10 years ago)
Author:
chronos
Message:
  • Modified: Cell Pos which was index in 2D array was changed to PosPx which is 2D pixel position in map.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r46 r52  
    299299    if Assigned(Cell) then begin
    300300      Core.Game.CurrentPlayer.View.FocusedCell := Cell;
    301       StatusBar1.Panels[0].Text := '[' + IntToStr(Cell.Pos.X) + ', ' + IntToStr(Cell.Pos.Y) +
     301      StatusBar1.Panels[0].Text := '[' + IntToStr(Cell.PosPx.X) + ', ' + IntToStr(Cell.PosPx.Y) +
    302302        '] (' + IntToStr(Cell.MovesFrom.Count) + ', ' + IntToStr(Cell.MovesTo.Count) + ')';
    303303    end else begin
  • trunk/UGame.pas

    r51 r52  
    3636    procedure SetPower(AValue: Integer);
    3737  public
    38     Pos: TPoint;
     38    PosPx: TPoint;
    3939    Polygon: TPointArray;
    4040    Terrain: TTerrainType;
     
    4343    MovesTo: TObjectList; // TList<TMove>
    4444    Neighbors: TObjectList; // TList<TCell>
     45    procedure Assign(Source: TCell);
    4546    function GetColor: TColor;
    4647    function GetAvialPower: Integer;
     
    119120  THexMap = class(TMap)
    120121  private
    121     function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
    122     function GetCellNeighbors2(Cell: TCell): TCellArray;
     122    function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean;
     123    function GetCellPosNeighbors(CellPos: TPoint): TCellArray;
    123124    function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray;
    124125  public
     
    127128    procedure SaveToFile(FileName: string); override;
    128129    function IsValidIndex(Index: TPoint): Boolean; override;
    129     function PosToCell(Pos: TPoint; View: TView): TCell; override;
    130     function CellToPos(Cell: TCell): TPoint; override;
    131130    function GetPixelRect: TRect; override;
    132131    procedure Paint(Canvas: TCanvas; View: TView); override;
     
    140139  TSquareMap = class(TMap)
    141140  private
    142     function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
    143     function GetCellNeighbors2(Cell: TCell): TCellArray;
    144141    function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPointArray;
    145142  public
    146143    function IsValidIndex(Index: TPoint): Boolean; override;
    147     function PosToCell(Pos: TPoint; View: TView): TCell; override;
    148     function CellToPos(Cell: TCell): TPoint; override;
    149144    function GetPixelRect: TRect; override;
    150145    procedure Paint(Canvas: TCanvas; View: TView); override;
     
    344339  for X := 0 to FSize.X - 1 do begin
    345340    NewCell := TCell.Create;
    346     NewCell.Pos := Point(X, Y);
     341    NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * SquareCellMulX),
     342      Trunc(Y * DefaultCellSize.Y * SquareCellMulY));
     343    NewCell.Polygon := GetSquarePolygon(NewCell.PosPx, DefaultCellSize);
    347344    Cells[Y * FSize.X + X] := NewCell;
    348345  end;
    349   // Generate neightbours
     346  // Generate neighbours
    350347  for Y := 0 to FSize.Y - 1 do
    351348  for X := 0 to FSize.X - 1 do
    352349  with TCell(Cells[Y * FSize.X + X]) do begin
    353     NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X]));
    354     for I := 0 to Length(NeighCells) - 1 do
    355       Neighbors.Add(NeighCells[I]);
    356     Polygon := GetSquarePolygon(Point(Trunc(X * DefaultCellSize.X * SquareCellMulX),
    357       Trunc(Y * DefaultCellSize.Y * SquareCellMulY)), DefaultCellSize);
    358   end;
    359 end;
    360 
    361 function TSquareMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
    362 var
    363   DX: Integer;
    364   DY: Integer;
    365   MinY: Integer;
    366 begin
    367   if Cell1.Pos.Y < Cell2.Pos.Y then MinY:= Cell1.Pos.Y
    368     else MinY := Cell2.Pos.Y;
    369   DX := Cell2.Pos.X - Cell1.Pos.X;
    370   DY := Cell2.Pos.Y - Cell1.Pos.Y;
    371   Result := (Abs(DX) <= 1) and (Abs(DY) <= 1);
    372   Result := Result and not (Cell1 = Cell2);
     350    if IsValidIndex(Point(Y + 0, X + 1)) then
     351      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X + 1)]));
     352    if IsValidIndex(Point(Y + 1, X + 0)) then
     353      Neighbors.Add(TCell(Cells[(Y + 1) * FSize.X + (X + 0)]));
     354    if IsValidIndex(Point(Y + 0, X - 1)) then
     355      Neighbors.Add(TCell(Cells[(Y + 0) * FSize.X + (X - 1)]));
     356    if IsValidIndex(Point(Y - 1, X + 0)) then
     357      Neighbors.Add(TCell(Cells[(Y - 1) * FSize.X + (X + 0)]));
     358  end;
    373359end;
    374360
     
    377363  Result := (Index.X >= 0) and (Index.X < Size.X) and
    378364    (Index.Y >= 0) and (Index.Y < Size.Y);
    379 end;
    380 
    381 function TSquareMap.PosToCell(Pos: TPoint; View: TView): TCell;
    382 var
    383   CX, CY: Integer;
    384   X, Y: Double;
    385   HexSize: TFloatPoint;
    386   CellSize: TFloatPoint;
    387   Frame: TRect;
    388 begin
    389   // TODO: This is implemented as simple sequence lookup. Needs some faster algorithm
    390   Result := nil;
    391   CellSize := FloatPoint(DefaultCellSize.X * SquareCellMulX, DefaultCellSize.Y * SquareCellMulX);
    392   HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y);
    393   with View do
    394     for CY := Trunc(SourceRect.Top / CellSize.Y) to Trunc(SourceRect.Bottom / CellSize.Y) + 1 do
    395     for CX := Trunc(SourceRect.Left / CellSize.X) to Trunc(SourceRect.Right / CellSize.X) + 1 do begin
    396       X := CX;
    397       Y := CY;
    398       if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then
    399       if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin
    400         Frame := Rect(Trunc(X * CellSize.X - HexSize.X / 2),
    401           Trunc(Y * CellSize.Y - HexSize.Y / 2),
    402           Trunc(X * CellSize.X + HexSize.X / 2),
    403           Trunc(Y * CellSize.Y + HexSize.Y / 2));
    404         if PtInRect(Frame, Pos) then begin
    405           Result := TCell(Cells[CY * FSize.X + CX]);
    406           Exit;
    407         end;
    408       end;
    409     end;
    410 end;
    411 
    412 function TSquareMap.CellToPos(Cell: TCell): TPoint;
    413 var
    414   CX, CY: Integer;
    415   X, Y: Double;
    416   HexSize: TFloatPoint;
    417   CellSize: TFloatPoint;
    418   Points: array of TPoint;
    419 begin
    420   CellSize := FloatPoint(DefaultCellSize.X * SquareCellMulX, DefaultCellSize.Y * SquareCellMulX);
    421   HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y);
    422   X := Cell.Pos.X;
    423   Y := Cell.Pos.Y;
    424 
    425   Result.X := Trunc(X * CellSize.X);
    426   Result.Y := Trunc(Y * CellSize.Y);
    427 end;
    428 
    429 function TSquareMap.GetCellNeighbors2(Cell: TCell): TCellArray;
    430 var
    431   X, Y: Integer;
    432 begin
    433   SetLength(Result, 0);
    434   for Y := -1 to 1 do
    435   for X := -1 to 1 do
    436   if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    437   IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin
    438     SetLength(Result, Length(Result) + 1);
    439     Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]);
    440   end;
    441365end;
    442366
     
    586510
    587511procedure TMap.Assign(Source: TMap);
     512var
     513  I: Integer;
    588514begin
    589515  MaxPower := Source.MaxPower;
     
    591517  Size := Source.Size;
    592518  DefaultCellSize := Source.DefaultCellSize;
     519  FSize := Source.Size;
     520
     521  // Copy all cells
     522  Cells.Count := 0;
     523  Cells.Count := Source.Cells.Count;
     524  for I := 0 to Cells.Count - 1 do begin
     525    Cells[I] := TCell.Create;
     526    TCell(Cells[I]).Assign(TCell(Source.Cells[I]));
     527  end;
    593528end;
    594529
     
    604539
    605540function TMap.PosToCell(Pos: TPoint; View: TView): TCell;
     541var
     542  I: Integer;
    606543begin
    607544  Result := nil;
     545  for I := 0 to Cells.Count - 1 do
     546  if TCell(Cells[I]).Terrain <> ttVoid then begin
     547    if PtInPoly(TCell(Cells[I]).Polygon, Pos) then begin
     548      Result := TCell(Cells[I]);
     549      Exit;
     550    end;
     551  end;
    608552end;
    609553
    610554function TMap.CellToPos(Cell: TCell): TPoint;
    611555begin
    612   Result := Point(0, 0);
     556  Result := Cell.PosPx;
    613557end;
    614558
     
    707651  for X := 0 to FSize.X - 1 do begin
    708652    NewCell := TCell.Create;
    709     NewCell.Pos := Point(X, Y);
     653    NewCell.PosPx := Point(X, Y);
    710654    Cells[Y * FSize.X + X] := NewCell;
    711655  end;
     
    878822    raise Exception.Create('Not allowed to substract power under zero do negative value');
    879823  FPower := AValue;
     824end;
     825
     826procedure TCell.Assign(Source: TCell);
     827begin
     828  PosPx := Source.PosPx;
     829  Terrain := Source.Terrain;
     830  Polygon := Source.Polygon;
     831  // TODO: How to copy neighbours and moves list
    880832end;
    881833
     
    14781430end;
    14791431
    1480 function THexMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean;
     1432function THexMap.IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean;
    14811433var
    14821434  DX: Integer;
     
    14841436  MinY: Integer;
    14851437begin
    1486   if Cell1.Pos.Y < Cell2.Pos.Y then MinY:= Cell1.Pos.Y
    1487     else MinY := Cell2.Pos.Y;
    1488   DX := Cell2.Pos.X - Cell1.Pos.X;
    1489   DY := Cell2.Pos.Y - Cell1.Pos.Y;
     1438  if CellPos1.Y < CellPos2.Y then MinY:= CellPos1.Y
     1439    else MinY := CellPos2.Y;
     1440  DX := CellPos2.X - CellPos1.X;
     1441  DY := CellPos2.Y - CellPos1.Y;
    14901442  Result := (Abs(DX) <= 1) and (Abs(DY) <= 1) and
    14911443  ((((MinY mod 2) = 1) and
     
    14951447    not ((DX = -1) and (DY = -1)) and
    14961448    not ((DX = 1) and (DY = 1))));
    1497   Result := Result and not (Cell1 = Cell2);
     1449  Result := Result and not ((CellPos1.X = CellPos2.X) and (CellPos1.Y = CellPos2.Y));
    14981450end;
    14991451
     
    15391491end;
    15401492
    1541 function THexMap.GetCellNeighbors2(Cell: TCell): TCellArray;
     1493function THexMap.GetCellPosNeighbors(CellPos: TPoint): TCellArray;
    15421494var
    15431495  X, Y: Integer;
     
    15461498  for Y := -1 to 1 do
    15471499  for X := -1 to 1 do
    1548   if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and
    1549   IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin
     1500  if IsValidIndex(Point(CellPos.X + X, CellPos.Y + Y)) and
     1501  IsCellsPosNeighbor(CellPos, Point((CellPos.X + X), (CellPos.Y + Y))) then begin
    15501502    SetLength(Result, Length(Result) + 1);
    1551     Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]);
    1552   end;
    1553 end;
    1554 
    1555 function THexMap.PosToCell(Pos: TPoint; View: TView): TCell;
    1556 var
    1557   CX, CY: Integer;
    1558   X, Y: Double;
    1559   HexSize: TFloatPoint;
    1560   CellSize: TFloatPoint;
    1561 begin
    1562   // TODO: This is implemented as simple sequence lookup. Needs some faster algorithm
    1563   Result := nil;
    1564   CellSize := FloatPoint(DefaultCellSize.X / HexCellMulX, DefaultCellSize.Y / HexCellMulY);
    1565   HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y);
    1566   with View do
    1567     for CY := Trunc(SourceRect.Top / CellSize.Y) to Trunc(SourceRect.Bottom / CellSize.Y) + 1 do
    1568     for CX := Trunc(SourceRect.Left / CellSize.X) to Trunc(SourceRect.Right / CellSize.X) + 1 do begin
    1569       X := CX;
    1570       Y := CY;
    1571       if (CY and 1) = 1 then begin
    1572         X := X + 0.5;
    1573         //Y := Y + 0.5;
    1574       end;
    1575       if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then
    1576       if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin
    1577         if PtInPoly(TCell(Cells[CY * FSize.X + CX]).Polygon, Pos) then begin
    1578           Result := TCell(Cells[CY * FSize.X + CX]);
    1579           Exit;
    1580         end;
    1581       end;
    1582     end;
    1583 end;
    1584 
    1585 function THexMap.CellToPos(Cell: TCell): TPoint;
    1586 var
    1587   CX, CY: Integer;
    1588   X, Y: Double;
    1589   HexSize: TFloatPoint;
    1590   CellSize: TFloatPoint;
    1591   Points: array of TPoint;
    1592 begin
    1593   CellSize := FloatPoint(DefaultCellSize.X / HexCellMulX, DefaultCellSize.Y / HexCellMulY);
    1594   HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y);
    1595   X := Cell.Pos.X;
    1596   Y := Cell.Pos.Y;
    1597   if (Cell.Pos.Y and 1) = 1 then begin
    1598     X := X + 0.5;
    1599     //Y := Y + 0.5;
    1600   end;
    1601 
    1602   Result.X := Trunc(X * CellSize.X);
    1603   Result.Y := Trunc(Y * CellSize.Y);
     1503    Result[Length(Result) - 1] := TCell(Cells[(CellPos.Y + Y) * FSize.X + (CellPos.X + X)]);
     1504  end;
    16041505end;
    16051506
     
    16811582  for X := 0 to FSize.X - 1 do begin
    16821583    NewCell := TCell.Create;
    1683     NewCell.Pos := Point(X, Y);
    1684     Cells[Y * FSize.X + X] := NewCell;
    1685   end;
    1686   // Generate neightbours
    1687   for Y := 0 to FSize.Y - 1 do
    1688   for X := 0 to FSize.X - 1 do
    1689   with TCell(Cells[Y * FSize.X + X]) do begin
    1690     NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X]));
    1691     Neighbors.Count := Length(NeighCells);
    1692     for I := 0 to Length(NeighCells) - 1 do
    1693       Neighbors[I] := NeighCells[I];
    16941584    PX := X;
    16951585    PY := Y;
     
    16981588      //Y := Y + 0.5;
    16991589    end;
    1700     Polygon := GetHexagonPolygon(Point(Trunc(PX * DefaultCellSize.X / HexCellMulX),
    1701       Trunc(PY * DefaultCellSize.Y / HexCellMulY)), DefaultCellSize);
     1590    NewCell.PosPx := Point(Trunc(PX * DefaultCellSize.X / HexCellMulX),
     1591      Trunc(PY * DefaultCellSize.Y / HexCellMulY));
     1592    NewCell.Polygon := GetHexagonPolygon(NewCell.PosPx, DefaultCellSize);
     1593    Cells[Y * FSize.X + X] := NewCell;
     1594  end;
     1595
     1596  // Generate neightbours
     1597  for Y := 0 to FSize.Y - 1 do
     1598  for X := 0 to FSize.X - 1 do
     1599  with TCell(Cells[Y * FSize.X + X]) do begin
     1600    NeighCells := GetCellPosNeighbors(Point(X, Y));
     1601    Neighbors.Count := Length(NeighCells);
     1602    for I := 0 to Length(NeighCells) - 1 do
     1603      Neighbors[I] := NeighCells[I];
    17021604  end;
    17031605end;
Note: See TracChangeset for help on using the changeset viewer.