Changeset 30 for trunk/UGame.pas
- Timestamp:
- Mar 4, 2014, 12:18:22 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r29 r30 58 58 procedure SetZoom(AValue: Double); 59 59 public 60 Game: TGame; 60 61 SourceRect: TRect; 62 FocusedCell: TCell; 63 SelectedCell: TCell; 64 procedure Clear; 61 65 constructor Create; 62 66 destructor Destroy; override; 67 procedure SelectCell(Pos: TPoint; Player: TPlayer); 63 68 function CanvasToCellPos(Pos: TPoint): TPoint; 64 69 function CellToCanvasPos(Pos: TPoint): TPoint; … … 86 91 function CellToPos(Cell: TCell; View: TView): TPoint; 87 92 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 88 procedure Paint(Canvas: TCanvas; View: TView ; SelectedCell: TCell; FocusedCell: TCell);93 procedure Paint(Canvas: TCanvas; View: TView); 89 94 constructor Create; 90 95 destructor Destroy; override; … … 102 107 103 108 TPlayer = class 104 Game: TGame; 109 private 110 FGame: TGame; 111 procedure SetGame(AValue: TGame); 112 public 105 113 Name: string; 106 114 Color: TColor; 107 115 View: TView; 108 SelectedCell: TCell;109 FocusedCell: TCell;110 116 Mode: TPlayerMode; 111 117 TotalUnits: Integer; … … 113 119 StartUnits: Integer; 114 120 procedure ComputerTurn; 115 procedure SelectCell(Pos: TPoint);116 121 procedure Paint(PaintBox: TPaintBox); 117 122 constructor Create; 118 123 destructor Destroy; override; 119 124 procedure Assign(Source: TPlayer); 125 property Game: TGame read FGame write SetGame; 120 126 end; 121 127 … … 243 249 end; 244 250 251 procedure TView.Clear; 252 begin 253 FocusedCell := nil; 254 SelectedCell := nil; 255 end; 256 245 257 procedure TView.SetDestRect(AValue: TRect); 246 258 begin … … 254 266 constructor TView.Create; 255 267 begin 256 Zoom := 1; 268 Zoom := 1.5; 269 Clear; 257 270 end; 258 271 … … 326 339 DestRect := Source.DestRect; 327 340 Zoom := Source.Zoom; 341 SelectedCell := Source.SelectedCell; 342 FocusedCell := Source.FocusedCell; 343 end; 344 345 procedure TPlayer.SetGame(AValue: TGame); 346 begin 347 if FGame = AValue then Exit; 348 FGame := AValue; 349 View.Game := Game; 328 350 end; 329 351 … … 336 358 TotalAttackPower: Integer; 337 359 I: Integer; 360 CanAttack: Integer; 338 361 begin 339 362 for Y := 0 to Game.Map.Size.Y - 1 do … … 341 364 with TCell(Game.Map.Cells[Y, X]) do begin 342 365 if (Terrain <> ttVoid) and (Player <> Self) then begin 366 // Attack to not owned cell yet 343 367 // Count own possible power 344 368 Cells := Game.Map.GetCellNeighbours(Game.Map.Cells[Y, X]); … … 361 385 end; 362 386 end; 363 end; 364 end; 365 end; 366 367 procedure TPlayer.SelectCell(Pos: TPoint); 387 end else 388 if (Terrain <> ttVoid) and (Player = Self) then begin 389 // Inner moves 390 // We need to move available power to borders to be available for attacks 391 // or defense 392 Cells := Game.Map.GetCellNeighbours(Game.Map.Cells[Y, X]); 393 CanAttack := 0; 394 for I := 0 to Length(Cells) - 1 do 395 if (Cells[I].Player <> Self) then begin 396 Inc(CanAttack); 397 end; 398 if CanAttack = 0 then begin 399 // We cannot attack and should do move 400 // For simplicty just try to balance inner area cells power 401 for I := 0 to Length(Cells) - 1 do 402 if (Cells[I].Player = Self) and (Cells[I].Power < Game.Map.Cells[Y, X].GetAvialPower) then begin 403 Game.SetMove(Game.Map.Cells[Y, X], Cells[I], (Game.Map.Cells[Y, X].GetAvialPower - Cells[I].Power) div 2); 404 end; 405 end; 406 end; 407 end; 408 end; 409 410 procedure TView.SelectCell(Pos: TPoint; Player: TPlayer); 368 411 var 369 412 NewSelectedCell: TCell; … … 371 414 BottomRight: TPoint; 372 415 begin 373 NewSelectedCell := Game.Map.PosToCell( View.CanvasToCellPos(Pos), View);416 NewSelectedCell := Game.Map.PosToCell(CanvasToCellPos(Pos), Self); 374 417 if Assigned(NewSelectedCell) then begin 375 418 if Assigned(SelectedCell) and IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin … … 377 420 SelectedCell := nil; 378 421 end else 379 if (NewSelectedCell <> SelectedCell) and (NewSelectedCell.Player = Self) then422 if (NewSelectedCell <> SelectedCell) and (NewSelectedCell.Player = Player) then 380 423 SelectedCell := NewSelectedCell 381 424 else 382 if (NewSelectedCell = SelectedCell) and (NewSelectedCell.Player = Self) then425 if (NewSelectedCell = SelectedCell) and (NewSelectedCell.Player = Player) then 383 426 SelectedCell := nil; 384 427 end; … … 387 430 procedure TPlayer.Paint(PaintBox: TPaintBox); 388 431 begin 389 Game.Map.Paint(PaintBox.Canvas, View , SelectedCell, FocusedCell);432 Game.Map.Paint(PaintBox.Canvas, View); 390 433 end; 391 434 … … 393 436 begin 394 437 View := TView.Create; 395 SelectedCell := nil;396 FocusedCell := nil;397 438 StartUnits := DefaultPlayerStartUnits; 398 439 end; … … 413 454 TotalUnits := Source.TotalUnits; 414 455 StartUnits := Source.StartUnits; 415 SelectedCell := Source.SelectedCell;416 FocusedCell := Source.FocusedCell;417 456 View.Assign(Source.View); 418 457 end; … … 432 471 end else begin 433 472 // Fight 473 //NewPower := CellTo.Power - Trunc(CountOnce / CellTo.Power); 434 474 if CellTo.Power > CountOnce then begin 435 475 // Defender wins … … 519 559 for I := 0 to Players.Count - 1 do 520 560 with TPlayer(Players[I]) do begin 521 SelectedCell := nil; 522 FocusedCell := nil; 561 View.Clear; 523 562 end; 524 563 end; … … 582 621 end; 583 622 UpdateRepeatMoves(CurrentPlayer); 623 // For computers take view from previous human 624 if CurrentPlayer.Mode = pmComputer then CurrentPlayer.View.Assign(PrevPlayer.View); 584 625 end; 585 626 … … 639 680 for I := 0 to Players.Count - 1 do 640 681 with TPlayer(Players[I]) do begin 641 SelectedCell := nil;682 View.Clear; 642 683 if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin 643 684 StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)]; … … 771 812 end; 772 813 773 procedure THexMap.Paint(Canvas: TCanvas; View: TView ; SelectedCell: TCell; FocusedCell: TCell);814 procedure THexMap.Paint(Canvas: TCanvas; View: TView); 774 815 var 775 816 CX, CY: Integer; … … 788 829 begin 789 830 with Canvas do begin 790 if Assigned( FocusedCell) and (FocusedCell = TCell(Cells[CY, CX])) then begin831 if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY, CX])) then begin 791 832 Pen.Color := clYellow; 792 833 Pen.Style := psSolid;
Note:
See TracChangeset
for help on using the changeset viewer.