Changeset 21 for trunk/UGame.pas
- Timestamp:
- Mar 1, 2014, 5:39:22 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r20 r21 55 55 DefaultCellSize: TPoint; 56 56 Cells: array of array of TCell; 57 function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double 58 ): TCell; 59 function CellToPos(Cell: TCell; Rect: TRect; Zoom: Double 60 ): TPoint; 57 function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TCell; 58 function CellToPos(Cell: TCell; Rect: TRect; Zoom: Double): TPoint; 61 59 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 62 60 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell); … … 77 75 Game: TGame; 78 76 Name: string; 79 View: TRect; 77 CellPos: TPoint; 78 ViewSize: TPoint; 80 79 Color: TColor; 81 80 ViewZoom: Double; … … 85 84 TotalCells: Integer; 86 85 StartUnits: Integer; 86 function CanvasToCellPos(Pos: TPoint): TPoint; 87 function CellToCanvasPos(Pos: TPoint): TPoint; 88 function CanvasToCellRect(Pos: TRect): TRect; 89 function CellToCanvasRect(Pos: TRect): TRect; 87 90 procedure ComputerTurn; 88 91 procedure SelectCell(Pos: TPoint); … … 222 225 { TPlayer } 223 226 227 function TPlayer.CanvasToCellPos(Pos: TPoint): TPoint; 228 begin 229 Result := Point(Trunc(Pos.X / ViewZoom + CellPos.X), 230 Trunc(Pos.Y / ViewZoom + CellPos.Y)); 231 end; 232 233 function TPlayer.CellToCanvasPos(Pos: TPoint): TPoint; 234 begin 235 Result := Point(Trunc((Pos.X - CellPos.X) * ViewZoom), 236 Trunc((Pos.Y - CellPos.Y) * ViewZoom)); 237 end; 238 239 function TPlayer.CanvasToCellRect(Pos: TRect): TRect; 240 begin 241 Result.TopLeft := CanvasToCellPos(Pos.TopLeft); 242 Result.BottomRight := CanvasToCellPos(Pos.BottomRight); 243 end; 244 245 function TPlayer.CellToCanvasRect(Pos: TRect): TRect; 246 begin 247 Result.TopLeft := CellToCanvasPos(Pos.TopLeft); 248 Result.BottomRight := CellToCanvasPos(Pos.BottomRight); 249 end; 250 224 251 procedure TPlayer.ComputerTurn; 225 252 begin … … 230 257 var 231 258 NewSelectedCell: TCell; 232 begin 233 NewSelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom); 259 TopLeft: TPoint; 260 BottomRight: TPoint; 261 begin 262 NewSelectedCell := Game.Map.PosToCell(CanvasToCellPos(Pos), CanvasToCellRect(Bounds(0, 0, ViewSize.X, ViewSize.Y)), ViewZoom); 234 263 if Assigned(NewSelectedCell) then begin 235 264 if Assigned(SelectedCell) and IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin … … 247 276 procedure TPlayer.Paint(PaintBox: TPaintBox); 248 277 begin 249 Game.Map.Paint(PaintBox.Canvas, View, ViewZoom, SelectedCell);278 Game.Map.Paint(PaintBox.Canvas, CanvasToCellRect(Bounds(0, 0, ViewSize.X, ViewSize.Y)), ViewZoom, SelectedCell); 250 279 end; 251 280 … … 460 489 ViewZoom := 1; 461 490 // Center board 462 View := Bounds(-(View.Right - View.Left) div 2 + Map.GetPixelSize.X div 2, 463 -(View.Bottom - View.Top) div 2 + Map.GetPixelSize.Y div 2, 464 View.Right - View.Left, 465 View.Bottom - View.Top); 491 CellPos := Point(Trunc(Map.GetPixelSize.X div 2 - ViewSize.X div 2 / ViewZoom), 492 Trunc(Map.GetPixelSize.Y div 2 - ViewSize.Y div 2 / ViewZoom)); 466 493 end; 467 494 CurrentPlayer := TPlayer(Players[0]); … … 516 543 Points: array of TPoint; 517 544 begin 545 // TODO: This is implemented as simple sequence lookup. It needs some faster algorithm 518 546 Result := nil; 519 547 CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom);
Note:
See TracChangeset
for help on using the changeset viewer.