Changeset 21 for trunk


Ignore:
Timestamp:
Mar 1, 2014, 5:39:22 PM (11 years ago)
Author:
chronos
Message:
  • Modified: Player View rectangle separated to CellPos and ViewSize which are values in different coordinate system.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Languages/xtactics.po

    r20 r21  
    3434msgstr ""
    3535
     36#: uformmain.splayerwins
     37msgid "Player %s wins"
     38msgstr ""
     39
  • trunk/UFormMain.pas

    r20 r21  
    7676  UFormNew, UFormMove;
    7777
     78resourcestring
     79  SPlayerWins = 'Player %s wins';
     80
    7881{$R *.lfm}
    7982
     
    8487  if Game.Running then
    8588  with Game.CurrentPlayer do begin
    86     View := Bounds(View.Left, View.Top, PaintBox1.Width,
    87       PaintBox1.Height);
     89    ViewSize := Point(PaintBox1.Width, PaintBox1.Height);
    8890    Paint(PaintBox1);
    8991  end;
     
    98100  if Assigned(Game.CurrentPlayer) then
    99101  with Game.CurrentPlayer do
    100     View := Bounds(View.Left + (View.Right - View.Left) div 2 - PaintBox1.Width div 2,
    101       View.Top + (View.Bottom - View.Top) div 2 - PaintBox1.Height div 2,
    102       PaintBox1.Width, PaintBox1.Height);
     102    ViewSize := Point(PaintBox1.Width, PaintBox1.Height);
    103103  Redraw;
    104104end;
    105105
    106106procedure TFormMain.Timer1Timer(Sender: TObject);
     107var
     108  NewCaption: string;
    107109begin
    108110  if RedrawPending then begin
    109111    RedrawPending := False;
    110112    PaintBox1.Repaint;
    111     Caption := Game.CurrentPlayer.Name + ' - turn ' + IntToStr(Game.TurnCounter) + ' - xTactics';
     113
     114    NewCaption := 'xTactics';
     115    if Assigned(Game.CurrentPlayer) then
     116      NewCaption := Game.CurrentPlayer.Name + ' - turn ' + IntToStr(Game.TurnCounter) + ' - ' + NewCaption;
     117    Caption := NewCaption;
    112118  end;
    113119end;
     
    128134procedure TFormMain.DoOnWin(Player: TPlayer);
    129135begin
    130   ShowMessage('Player ' + Player.Name + ' wins');
     136  ShowMessage(Format(SPlayerWins, [Player.Name]));
    131137end;
    132138
     
    188194
    189195procedure TFormMain.FormShow(Sender: TObject);
    190 begin
     196var
     197  I: Integer;
     198begin
     199  for I := 0 to Game.Players.Count - 1 do
     200    TPlayer(Game.Players[I]).ViewSize := Point(PaintBox1.Canvas.Width, PaintBox1.Canvas.Height);
    191201  Game.New;
     202  Game.Running := True;
    192203  Redraw;
    193204end;
     
    199210    if Game.CurrentPlayer.Mode = pmHuman then begin
    200211      StartMousePoint := Point(X, Y);
    201       StartViewPoint := Point(Game.CurrentPlayer.View.Left, Game.CurrentPlayer.View.Top);
     212      StartViewPoint := Game.CurrentPlayer.CellPos;
    202213      MoveActive := True;
    203214    end;
     
    215226  if MoveActive then begin
    216227    if Game.CurrentPlayer.Mode = pmHuman then begin
    217       Game.CurrentPlayer.View := Bounds(StartViewPoint.X + StartMousePoint.X - X,
    218         StartViewPoint.Y + StartMousePoint.Y - Y,
    219         Game.CurrentPlayer.View.Right - Game.CurrentPlayer.View.Left,
    220         Game.CurrentPlayer.View.Bottom - Game.CurrentPlayer.View.Top);
     228      Game.CurrentPlayer.CellPos := Point(Trunc(StartViewPoint.X + (StartMousePoint.X - X) / Game.CurrentPlayer.ViewZoom),
     229        Trunc(StartViewPoint.Y + (StartMousePoint.Y - Y) / Game.CurrentPlayer.ViewZoom));
    221230      Redraw;
    222231    end;
     
    242251begin
    243252  with Game.CurrentPlayer do begin
    244     D := Point(Trunc(MousePos.X - Game.CurrentPlayer.View.Left / ViewZoom),
    245       Trunc(MousePos.Y - Game.CurrentPlayer.View.Top / ViewZoom));
     253    //D := Point(Trunc(MousePos.X - View.Left / ViewZoom),
     254    //  Trunc(MousePos.Y - View.Top / ViewZoom));
    246255    ViewZoom := ViewZoom / ZoomFactor;
    247256    //View := Bounds(Trunc((D.X - MousePos.X) * ViewZoom),
  • trunk/UGame.pas

    r20 r21  
    5555    DefaultCellSize: TPoint;
    5656    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;
    6159    function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;
    6260    procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell);
     
    7775    Game: TGame;
    7876    Name: string;
    79     View: TRect;
     77    CellPos: TPoint;
     78    ViewSize: TPoint;
    8079    Color: TColor;
    8180    ViewZoom: Double;
     
    8584    TotalCells: Integer;
    8685    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;
    8790    procedure ComputerTurn;
    8891    procedure SelectCell(Pos: TPoint);
     
    222225{ TPlayer }
    223226
     227function TPlayer.CanvasToCellPos(Pos: TPoint): TPoint;
     228begin
     229  Result := Point(Trunc(Pos.X / ViewZoom + CellPos.X),
     230    Trunc(Pos.Y / ViewZoom + CellPos.Y));
     231end;
     232
     233function TPlayer.CellToCanvasPos(Pos: TPoint): TPoint;
     234begin
     235  Result := Point(Trunc((Pos.X - CellPos.X) * ViewZoom),
     236    Trunc((Pos.Y - CellPos.Y) * ViewZoom));
     237end;
     238
     239function TPlayer.CanvasToCellRect(Pos: TRect): TRect;
     240begin
     241  Result.TopLeft := CanvasToCellPos(Pos.TopLeft);
     242  Result.BottomRight := CanvasToCellPos(Pos.BottomRight);
     243end;
     244
     245function TPlayer.CellToCanvasRect(Pos: TRect): TRect;
     246begin
     247  Result.TopLeft := CellToCanvasPos(Pos.TopLeft);
     248  Result.BottomRight := CellToCanvasPos(Pos.BottomRight);
     249end;
     250
    224251procedure TPlayer.ComputerTurn;
    225252begin
     
    230257var
    231258  NewSelectedCell: TCell;
    232 begin
    233   NewSelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom);
     259  TopLeft: TPoint;
     260  BottomRight: TPoint;
     261begin
     262  NewSelectedCell := Game.Map.PosToCell(CanvasToCellPos(Pos), CanvasToCellRect(Bounds(0, 0, ViewSize.X, ViewSize.Y)), ViewZoom);
    234263  if Assigned(NewSelectedCell) then begin
    235264    if Assigned(SelectedCell) and IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
     
    247276procedure TPlayer.Paint(PaintBox: TPaintBox);
    248277begin
    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);
    250279end;
    251280
     
    460489    ViewZoom := 1;
    461490    // 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));
    466493  end;
    467494  CurrentPlayer := TPlayer(Players[0]);
     
    516543  Points: array of TPoint;
    517544begin
     545  // TODO: This is implemented as simple sequence lookup. It needs some faster algorithm
    518546  Result := nil;
    519547  CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom);
Note: See TracChangeset for help on using the changeset viewer.