Changeset 35 for trunk/UGame.pas


Ignore:
Timestamp:
Mar 7, 2014, 11:03:30 PM (11 years ago)
Author:
chronos
Message:
  • Added: Zoom all action to show entire map.
  • Added: Actions Zoom in and Zoom out which keep view center.
  • Fixed: Form resize now keep view center.
  • Modified: Attacking is now using mutiple dice rolls system.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r33 r35  
    6666    destructor Destroy; override;
    6767    procedure SelectCell(Pos: TPoint; Player: TPlayer);
     68    procedure CenterMap;
    6869    function CanvasToCellPos(Pos: TPoint): TPoint;
    6970    function CellToCanvasPos(Pos: TPoint): TPoint;
     
    9697    procedure Grow(APlayer: TPlayer);
    9798    procedure ComputePlayerStats;
    98     function GetPixelSize: TPoint;
     99    function GetPixelRect: TRect;
    99100    property Size: TPoint read FSize write SetSize;
    100101  end;
     
    137138    FCellFrom: TCell;
    138139    FCellTo: TCell;
     140    FDestroying: Boolean;
    139141    procedure SetCellFrom(AValue: TCell);
    140142    procedure SetCellTo(AValue: TCell);
     
    160162    FOnWin: TWinEvent;
    161163    FRunning: Boolean;
     164    procedure Attack(var AttackPower, DefendPower: Integer);
    162165    procedure MoveAll(Player: TPlayer);
    163166    procedure ClearMovesFromCell(Cell: TCell);
     
    195198
    196199procedure InitStrings;
     200function FloatPoint(AX, AY: Double): TFloatPoint;
    197201
    198202
     
    295299
    296300destructor TMove.Destroy;
     301var
     302  LastState: Boolean;
    297303begin
    298304  CellFrom := nil;
    299305  CellTo := nil;
    300   if Assigned(List) then
    301     List.Remove(Self);
     306  if Assigned(List) then begin
     307    // To remove itself from list we need disable owning to not be called twice
     308    try
     309      LastState := List.OwnsObjects;
     310      List.OwnsObjects := False;
     311      List.Remove(Self);
     312    finally
     313      List.OwnsObjects := LastState;
     314    end;
     315  end;
    302316  inherited Destroy;
    303317end;
     
    306320
    307321procedure TView.SetZoom(AValue: Double);
     322var
     323  OldSourceRect: TRect;
    308324begin
    309325  if FZoom = AValue then Exit;
    310326  FZoom := AValue;
    311   SourceRect := Bounds(SourceRect.Left, SourceRect.Top,
     327  SourceRect := Bounds(Trunc(SourceRect.Left + (SourceRect.Right - SourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),
     328    Trunc(SourceRect.Top +  (SourceRect.Bottom - SourceRect.Top) div 2 - (DestRect.Bottom - DestRect.Top) / Zoom / 2),
    312329    Trunc((DestRect.Right - DestRect.Left) / Zoom),
    313330    Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
     
    321338
    322339procedure TView.SetDestRect(AValue: TRect);
     340var
     341  Diff: TPoint;
    323342begin
    324343  if RectEquals(FDestRect, AValue) then Exit;
     344  Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2,
     345    Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2);
    325346  FDestRect := AValue;
    326   SourceRect := Bounds(SourceRect.Left, SourceRect.Top,
     347  SourceRect := Bounds(SourceRect.Left + Diff.X, SourceRect.Top + Diff.Y,
    327348    Trunc((DestRect.Right - DestRect.Left) / Zoom),
    328349    Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
     
    360381constructor TCell.Create;
    361382begin
     383  Player := nil;
    362384  MovesFrom := TObjectList.Create;
    363385  MovesFrom.OwnsObjects := False;
     
    499521end;
    500522
     523procedure TView.CenterMap;
     524var
     525  MapRect: TRect;
     526begin
     527  MapRect := Game.Map.GetPixelRect;
     528  SourceRect := Bounds(MapRect.Left + (MapRect.Right - MapRect.Left) div 2 - (SourceRect.Right - SourceRect.Left) div 2,
     529    MapRect.Top + (MapRect.Bottom - MapRect.Top) div 2 - (SourceRect.Bottom - SourceRect.Top) div 2,
     530    SourceRect.Right - SourceRect.Left,
     531    SourceRect.Bottom - SourceRect.Top);
     532end;
     533
    501534procedure TPlayer.Paint(PaintBox: TPaintBox);
    502535begin
     
    530563{ TGame }
    531564
     565procedure TGame.Attack(var AttackPower, DefendPower: Integer);
     566var
     567  AttackerRoll: Integer;
     568  DefenderRoll: Integer;
     569begin
     570  while (AttackPower > 0) and (DefendPower > 0) do begin
     571    // Earch side do dice roll and compare result. Defender wins tie
     572    AttackerRoll := Random(6);
     573    DefenderRoll := Random(6);
     574    if AttackerRoll > DefenderRoll then Dec(DefendPower)
     575      else Dec(AttackPower);
     576  end;
     577end;
     578
    532579procedure TGame.MoveAll(Player: TPlayer);
    533580var
    534581  I: Integer;
     582  Remain: Integer;
     583  AttackerPower: Integer;
     584  DefenderPower: Integer;
    535585begin
    536586  I := 0;
     
    542592        CellTo.Power := CellTo.Power + CountOnce;
    543593      end else begin
    544         // Fight
    545         //NewPower := CellTo.Power - Trunc(CountOnce / CellTo.Power);
    546         if CellTo.Power > CountOnce then begin
    547           // Defender wins
    548           CellTo.Power := CellTo.Power - CountOnce;
    549         end else begin
    550           // Attacker wins
    551           CellTo.Power := CountOnce - CellTo.Power;
     594        AttackerPower := CountOnce;
     595        DefenderPower := CellTo.Power;
     596        Attack(AttackerPower, DefenderPower);
     597        if DefenderPower = 0 then begin
     598          // Attacker wins with possible loses
     599          ClearMovesFromCell(CellTo);
    552600          CellTo.Player := Player;
    553           ClearMovesFromCell(CellTo);
    554         end;
     601          CellTo.Power := AttackerPower;
     602        end else
     603        if AttackerPower = 0 then begin
     604          // Defender wins with possible loses
     605          CellTo.Power := DefenderPower;
     606        end else
     607          raise Exception.Create('Unfinished battle');
    555608      end;
    556609      CellFrom.Power := CellFrom.Power - CountOnce;
     
    764817    end;
    765818    View.Zoom := 1;
    766     // Center board
    767     View.SourceRect.TopLeft := Point(Trunc(Map.GetPixelSize.X div 2 - (View.SourceRect.Right - View.SourceRect.Left) div 2 / View.Zoom),
    768       Trunc(Map.GetPixelSize.Y div 2 - (View.SourceRect.Bottom - View.SourceRect.Top) div 2 / View.Zoom));
     819    View.CenterMap;
    769820  end;
    770821  CurrentPlayer := TPlayer(Players[0]);
     
    873924  Points: array of TPoint;
    874925begin
    875   with View do begin
    876926  CellSize := FloatPoint(DefaultCellSize.X / CellMulX, DefaultCellSize.Y / CellMulY);
    877927  HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y);
     
    885935  Result.X := Trunc(X * CellSize.X);
    886936  Result.Y := Trunc(Y * CellSize.Y);
    887   end;
    888937end;
    889938
     
    10081057      Player.TotalCells := Player.TotalCells + 1;
    10091058      Player.TotalUnits := Player.TotalUnits + Power;
    1010 
    1011     end;
    1012   end;
    1013 end;
    1014 
    1015 function THexMap.GetPixelSize: TPoint;
    1016 begin
    1017   Result := Point(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y);
     1059    end;
     1060  end;
     1061end;
     1062
     1063function THexMap.GetPixelRect: TRect;
     1064begin
     1065  Result := Bounds(Trunc(-0.5 * DefaultCellSize.X),
     1066    Trunc(-0.5 * DefaultCellSize.Y),
     1067    Trunc((Size.X + 0.5) * DefaultCellSize.X),
     1068    Trunc(((Size.Y + 0.5) * 0.78) * DefaultCellSize.Y));
    10181069end;
    10191070
Note: See TracChangeset for help on using the changeset viewer.