- Timestamp:
- Aug 17, 2014, 1:12:18 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r49 r50 37 37 public 38 38 Pos: TPoint; 39 Polygon: TPointArray; 39 40 Terrain: TTerrainType; 40 41 Player: TPlayer; … … 118 119 function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 119 120 function GetCellNeighbors2(Cell: TCell): TCellArray; 121 function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray; 120 122 public 121 123 procedure Assign(Source: TMap); override; … … 125 127 function PosToCell(Pos: TPoint; View: TView): TCell; override; 126 128 function CellToPos(Cell: TCell): TPoint; override; 127 function Get HexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;129 function GetPixelRect: TRect; override; 128 130 procedure Paint(Canvas: TCanvas; View: TView); override; 129 131 procedure Generate; override; 130 132 constructor Create; override; 131 133 destructor Destroy; override; 132 function GetAllCells: TCellArray; override;133 function GetPixelRect: TRect; override;134 134 end; 135 135 … … 140 140 function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 141 141 function GetCellNeighbors2(Cell: TCell): TCellArray; 142 function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPointArray; 142 143 public 143 procedure Generate; override;144 144 function IsValidIndex(Index: TPoint): Boolean; override; 145 145 function PosToCell(Pos: TPoint; View: TView): TCell; override; 146 146 function CellToPos(Cell: TCell): TPoint; override; 147 function GetAllCells: TCellArray; override;148 147 function GetPixelRect: TRect; override; 149 148 procedure Paint(Canvas: TCanvas; View: TView); override; 149 procedure Generate; override; 150 150 constructor Create; override; 151 151 destructor Destroy; override; … … 347 347 // Generate neightbours 348 348 for Y := 0 to FSize.Y - 1 do 349 for X := 0 to FSize.X - 1 do begin 349 for X := 0 to FSize.X - 1 do 350 with TCell(Cells[Y * FSize.X + X]) do begin 350 351 NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X])); 351 352 for I := 0 to Length(NeighCells) - 1 do 352 TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]); 353 Neighbors.Add(NeighCells[I]); 354 Polygon := GetSquarePolygon(Point(Trunc(X * DefaultCellSize.X * SquareCellMulX), 355 Trunc(Y * DefaultCellSize.Y * SquareCellMulY)), DefaultCellSize); 353 356 end; 354 357 end; … … 436 439 end; 437 440 438 function TSquareMap.GetAllCells: TCellArray; 439 var 440 X: Integer; 441 Y: Integer; 442 I: Integer; 443 begin 444 SetLength(Result, Size.Y * Size.X); 445 for Y := 0 to Size.Y - 1 do 446 for X := 0 to Size.X - 1 do 447 Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]); 441 function TSquareMap.GetSquarePolygon(Pos: TPoint; Size: TPoint): TPointArray; 442 begin 443 SetLength(Result, 4); 444 Result[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - Size.Y / 2)); 445 Result[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - Size.Y / 2)); 446 Result[2] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y + Size.Y / 2)); 447 Result[3] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y + Size.Y / 2)); 448 448 end; 449 449 … … 470 470 471 471 procedure PaintHexagon(Pos: TPoint; Text: string); 472 var 473 TextPos: TPoint; 474 I: Integer; 472 475 begin 473 476 with Canvas do begin … … 490 493 Pen.Width := 0; 491 494 end; 492 Rectangle(Trunc(Pos.X - HexSize.X / 2), Trunc(Pos.Y - HexSize.Y / 2), Trunc(Pos.X + HexSize.X / 2), Trunc(Pos.Y + HexSize.Y / 2)); 495 // Transform view 496 SetLength(Points, Length(TCell(Cells[CY * FSize.X + CX]).Polygon)); 497 for I := 0 to Length(Points) - 1 do 498 Points[I] := View.CellToCanvasPos(TCell(Cells[CY * FSize.X + CX]).Polygon[I]); 499 500 Polygon(Points, False, 0, Length(Points)); 493 501 //Rectangle(Trunc(Pos.X), Trunc(Pos.Y), Trunc(Pos.X + HexSize.X), Trunc(Pos.Y + HexSize.Y)); 494 502 Pen.Style := psSolid; 495 503 Font.Color := clWhite; 496 504 Font.Size := Trunc(12 * View.Zoom); 497 TextOut(Round(Pos.X) - TextWidth(Text) div 2, Round(Pos.Y) - TextHeight(Text) div 2, Text); 505 TextPos := View.CellToCanvasPos(Pos); 506 TextOut(Round(TextPos.X) - TextWidth(Text) div 2, Round(TextPos.Y) - TextHeight(Text) div 2, Text); 498 507 end; 499 508 end; … … 516 525 else Brush.Color := Cell.GetColor; 517 526 Pen.Color := clBlack; 518 PaintHexagon( View.CellToCanvasPos(Point(Trunc(X * CellSize.X),519 Trunc(Y * CellSize.Y)) ),527 PaintHexagon(Point(Trunc(X * CellSize.X), 528 Trunc(Y * CellSize.Y)), 520 529 IntToStr(Cell.GetAvialPower)); 521 530 // Draw arrows … … 750 759 end; 751 760 752 753 761 function TMap.GetAllCells: TCellArray; 754 begin 755 SetLength(Result, 0); 762 var 763 I: Integer; 764 begin 765 SetLength(Result, Cells.Count); 766 for I := 0 to Cells.Count - 1do 767 Result[I] := TCell(Cells[I]); 756 768 end; 757 769 … … 1450 1462 { THexMap } 1451 1463 1452 function THexMap.GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;1453 var 1454 HexShift: TFloatPoint;1455 begin 1456 HexShift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi));1464 function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray; 1465 var 1466 Shift: TFloatPoint; 1467 begin 1468 Shift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi)); 1457 1469 SetLength(Result, 6); 1458 Result[0] := Point(Round(Pos.X + 0 * HexSize.X), Round(Pos.Y - 0.5 * HexSize.Y));1459 Result[1] := Point(Round(Pos.X + HexShift.X * HexSize.X), Round(Pos.Y - HexShift.Y * HexSize.Y));1460 Result[2] := Point(Round(Pos.X + HexShift.X * HexSize.X), Round(Pos.Y + HexShift.Y * HexSize.Y));1461 Result[3] := Point(Round(Pos.X + 0 * HexSize.X), Round(Pos.Y + 0.5 * HexSize.Y));1462 Result[4] := Point(Round(Pos.X - HexShift.X * HexSize.X), Round(Pos.Y + HexShift.Y * HexSize.Y));1463 Result[5] := Point(Round(Pos.X - HexShift.X * HexSize.X), Round(Pos.Y - HexShift.Y * HexSize.Y));1470 Result[0] := Point(Round(Pos.X + 0 * Size.X), Round(Pos.Y - 0.5 * Size.Y)); 1471 Result[1] := Point(Round(Pos.X + Shift.X * Size.X), Round(Pos.Y - Shift.Y * Size.Y)); 1472 Result[2] := Point(Round(Pos.X + Shift.X * Size.X), Round(Pos.Y + Shift.Y * Size.Y)); 1473 Result[3] := Point(Round(Pos.X + 0 * Size.X), Round(Pos.Y + 0.5 * Size.Y)); 1474 Result[4] := Point(Round(Pos.X - Shift.X * Size.X), Round(Pos.Y + Shift.Y * Size.Y)); 1475 Result[5] := Point(Round(Pos.X - Shift.X * Size.X), Round(Pos.Y - Shift.Y * Size.Y)); 1464 1476 end; 1465 1477 … … 1545 1557 HexSize: TFloatPoint; 1546 1558 CellSize: TFloatPoint; 1547 Points: array of TPoint;1548 1559 begin 1549 1560 // TODO: This is implemented as simple sequence lookup. Needs some faster algorithm … … 1562 1573 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then 1563 1574 if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin 1564 Points := GetHexagonPolygon(Point(Trunc(X * CellSize.X), 1565 Trunc(Y * CellSize.Y)), 1566 Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 1567 if PtInPoly(Points, Pos) then begin 1575 if PtInPoly(TCell(Cells[CY * FSize.X + CX]).Polygon, Pos) then begin 1568 1576 Result := TCell(Cells[CY * FSize.X + CX]); 1569 1577 Exit; … … 1608 1616 ArrowCenter: TPoint; 1609 1617 1610 procedure PaintHexagon(Pos: TPoint; Text: string); 1611 var 1612 Points: array of TPoint; 1618 procedure PaintHexagon(Pos: TPoint; Text: string; View: TView); 1619 var 1620 I: Integer; 1621 TextPos: TPoint; 1613 1622 begin 1614 1623 with Canvas do begin … … 1631 1640 Pen.Width := 0; 1632 1641 end; 1633 Points := GetHexagonPolygon(Point(Trunc(Pos.X), Trunc(Pos.Y)), Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 1642 // Transform view 1643 SetLength(Points, Length(TCell(Cells[CY * FSize.X + CX]).Polygon)); 1644 for I := 0 to Length(Points) - 1 do 1645 Points[I] := View.CellToCanvasPos(TCell(Cells[CY * FSize.X + CX]).Polygon[I]); 1646 1634 1647 Polygon(Points, False, 0, Length(Points)); 1635 1648 //Rectangle(Trunc(Pos.X), Trunc(Pos.Y), Trunc(Pos.X + HexSize.X), Trunc(Pos.Y + HexSize.Y)); … … 1637 1650 Font.Color := clWhite; 1638 1651 Font.Size := Trunc(12 * View.Zoom); 1639 TextOut(Round(Pos.X) - TextWidth(Text) div 2, Round(Pos.Y) - TextHeight(Text) div 2, Text); 1652 TextPos := View.CellToCanvasPos(Pos); 1653 TextOut(Round(TextPos.X) - TextWidth(Text) div 2, Round(TextPos.Y) - TextHeight(Text) div 2, Text); 1640 1654 end; 1641 1655 end; … … 1663 1677 else Brush.Color := Cell.GetColor; 1664 1678 Pen.Color := clBlack; 1665 PaintHexagon( View.CellToCanvasPos(Point(Trunc(X * CellSize.X),1666 Trunc(Y * CellSize.Y)) ),1667 IntToStr(Cell.GetAvialPower) );1679 PaintHexagon(Point(Trunc(X * CellSize.X), 1680 Trunc(Y * CellSize.Y)), 1681 IntToStr(Cell.GetAvialPower), View); 1668 1682 // Draw arrows 1669 1683 Pen.Color := clCream; … … 1695 1709 NewCell: TCell; 1696 1710 NeighCells: TCellArray; 1711 PX, PY: Double; 1697 1712 begin 1698 1713 inherited; … … 1709 1724 // Generate neightbours 1710 1725 for Y := 0 to FSize.Y - 1 do 1711 for X := 0 to FSize.X - 1 do begin 1726 for X := 0 to FSize.X - 1 do 1727 with TCell(Cells[Y * FSize.X + X]) do begin 1712 1728 NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X])); 1729 Neighbors.Count := Length(NeighCells); 1713 1730 for I := 0 to Length(NeighCells) - 1 do 1714 TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]); 1731 Neighbors[I] := NeighCells[I]; 1732 PX := X; 1733 PY := Y; 1734 if (Y and 1) = 1 then begin 1735 PX := PX + 0.5; 1736 //Y := Y + 0.5; 1737 end; 1738 Polygon := GetHexagonPolygon(Point(Trunc(PX * DefaultCellSize.X / HexCellMulX), 1739 Trunc(PY * DefaultCellSize.Y / HexCellMulY)), DefaultCellSize); 1715 1740 end; 1716 1741 end; … … 1725 1750 begin 1726 1751 inherited Destroy; 1727 end;1728 1729 function THexMap.GetAllCells: TCellArray;1730 var1731 X: Integer;1732 Y: Integer;1733 I: Integer;1734 begin1735 SetLength(Result, Size.Y * Size.X);1736 for Y := 0 to Size.Y - 1 do1737 for X := 0 to Size.X - 1 do1738 Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]);1739 1752 end; 1740 1753
Note:
See TracChangeset
for help on using the changeset viewer.