Changeset 35 for trunk/UGame.pas
- Timestamp:
- Mar 7, 2014, 11:03:30 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r33 r35 66 66 destructor Destroy; override; 67 67 procedure SelectCell(Pos: TPoint; Player: TPlayer); 68 procedure CenterMap; 68 69 function CanvasToCellPos(Pos: TPoint): TPoint; 69 70 function CellToCanvasPos(Pos: TPoint): TPoint; … … 96 97 procedure Grow(APlayer: TPlayer); 97 98 procedure ComputePlayerStats; 98 function GetPixel Size: TPoint;99 function GetPixelRect: TRect; 99 100 property Size: TPoint read FSize write SetSize; 100 101 end; … … 137 138 FCellFrom: TCell; 138 139 FCellTo: TCell; 140 FDestroying: Boolean; 139 141 procedure SetCellFrom(AValue: TCell); 140 142 procedure SetCellTo(AValue: TCell); … … 160 162 FOnWin: TWinEvent; 161 163 FRunning: Boolean; 164 procedure Attack(var AttackPower, DefendPower: Integer); 162 165 procedure MoveAll(Player: TPlayer); 163 166 procedure ClearMovesFromCell(Cell: TCell); … … 195 198 196 199 procedure InitStrings; 200 function FloatPoint(AX, AY: Double): TFloatPoint; 197 201 198 202 … … 295 299 296 300 destructor TMove.Destroy; 301 var 302 LastState: Boolean; 297 303 begin 298 304 CellFrom := nil; 299 305 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; 302 316 inherited Destroy; 303 317 end; … … 306 320 307 321 procedure TView.SetZoom(AValue: Double); 322 var 323 OldSourceRect: TRect; 308 324 begin 309 325 if FZoom = AValue then Exit; 310 326 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), 312 329 Trunc((DestRect.Right - DestRect.Left) / Zoom), 313 330 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); … … 321 338 322 339 procedure TView.SetDestRect(AValue: TRect); 340 var 341 Diff: TPoint; 323 342 begin 324 343 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); 325 346 FDestRect := AValue; 326 SourceRect := Bounds(SourceRect.Left , SourceRect.Top,347 SourceRect := Bounds(SourceRect.Left + Diff.X, SourceRect.Top + Diff.Y, 327 348 Trunc((DestRect.Right - DestRect.Left) / Zoom), 328 349 Trunc((DestRect.Bottom - DestRect.Top) / Zoom)); … … 360 381 constructor TCell.Create; 361 382 begin 383 Player := nil; 362 384 MovesFrom := TObjectList.Create; 363 385 MovesFrom.OwnsObjects := False; … … 499 521 end; 500 522 523 procedure TView.CenterMap; 524 var 525 MapRect: TRect; 526 begin 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); 532 end; 533 501 534 procedure TPlayer.Paint(PaintBox: TPaintBox); 502 535 begin … … 530 563 { TGame } 531 564 565 procedure TGame.Attack(var AttackPower, DefendPower: Integer); 566 var 567 AttackerRoll: Integer; 568 DefenderRoll: Integer; 569 begin 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; 577 end; 578 532 579 procedure TGame.MoveAll(Player: TPlayer); 533 580 var 534 581 I: Integer; 582 Remain: Integer; 583 AttackerPower: Integer; 584 DefenderPower: Integer; 535 585 begin 536 586 I := 0; … … 542 592 CellTo.Power := CellTo.Power + CountOnce; 543 593 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); 552 600 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'); 555 608 end; 556 609 CellFrom.Power := CellFrom.Power - CountOnce; … … 764 817 end; 765 818 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; 769 820 end; 770 821 CurrentPlayer := TPlayer(Players[0]); … … 873 924 Points: array of TPoint; 874 925 begin 875 with View do begin876 926 CellSize := FloatPoint(DefaultCellSize.X / CellMulX, DefaultCellSize.Y / CellMulY); 877 927 HexSize := FloatPoint(DefaultCellSize.X, DefaultCellSize.Y); … … 885 935 Result.X := Trunc(X * CellSize.X); 886 936 Result.Y := Trunc(Y * CellSize.Y); 887 end;888 937 end; 889 938 … … 1008 1057 Player.TotalCells := Player.TotalCells + 1; 1009 1058 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; 1061 end; 1062 1063 function THexMap.GetPixelRect: TRect; 1064 begin 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)); 1018 1069 end; 1019 1070
Note:
See TracChangeset
for help on using the changeset viewer.