Changeset 30 for trunk/UGame.pas


Ignore:
Timestamp:
Mar 4, 2014, 12:18:22 AM (10 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r29 r30  
    5858    procedure SetZoom(AValue: Double);
    5959  public
     60    Game: TGame;
    6061    SourceRect: TRect;
     62    FocusedCell: TCell;
     63    SelectedCell: TCell;
     64    procedure Clear;
    6165    constructor Create;
    6266    destructor Destroy; override;
     67    procedure SelectCell(Pos: TPoint; Player: TPlayer);
    6368    function CanvasToCellPos(Pos: TPoint): TPoint;
    6469    function CellToCanvasPos(Pos: TPoint): TPoint;
     
    8691    function CellToPos(Cell: TCell; View: TView): TPoint;
    8792    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);
    8994    constructor Create;
    9095    destructor Destroy; override;
     
    102107
    103108  TPlayer = class
    104     Game: TGame;
     109  private
     110    FGame: TGame;
     111    procedure SetGame(AValue: TGame);
     112  public
    105113    Name: string;
    106114    Color: TColor;
    107115    View: TView;
    108     SelectedCell: TCell;
    109     FocusedCell: TCell;
    110116    Mode: TPlayerMode;
    111117    TotalUnits: Integer;
     
    113119    StartUnits: Integer;
    114120    procedure ComputerTurn;
    115     procedure SelectCell(Pos: TPoint);
    116121    procedure Paint(PaintBox: TPaintBox);
    117122    constructor Create;
    118123    destructor Destroy; override;
    119124    procedure Assign(Source: TPlayer);
     125    property Game: TGame read FGame write SetGame;
    120126  end;
    121127
     
    243249end;
    244250
     251procedure TView.Clear;
     252begin
     253  FocusedCell := nil;
     254  SelectedCell := nil;
     255end;
     256
    245257procedure TView.SetDestRect(AValue: TRect);
    246258begin
     
    254266constructor TView.Create;
    255267begin
    256   Zoom := 1;
     268  Zoom := 1.5;
     269  Clear;
    257270end;
    258271
     
    326339  DestRect := Source.DestRect;
    327340  Zoom := Source.Zoom;
     341  SelectedCell := Source.SelectedCell;
     342  FocusedCell := Source.FocusedCell;
     343end;
     344
     345procedure TPlayer.SetGame(AValue: TGame);
     346begin
     347  if FGame = AValue then Exit;
     348  FGame := AValue;
     349  View.Game := Game;
    328350end;
    329351
     
    336358  TotalAttackPower: Integer;
    337359  I: Integer;
     360  CanAttack: Integer;
    338361begin
    339362  for Y := 0 to Game.Map.Size.Y - 1 do
     
    341364  with TCell(Game.Map.Cells[Y, X]) do begin
    342365    if (Terrain <> ttVoid) and (Player <> Self) then begin
     366      // Attack to not owned cell yet
    343367      // Count own possible power
    344368      Cells := Game.Map.GetCellNeighbours(Game.Map.Cells[Y, X]);
     
    361385        end;
    362386      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;
     408end;
     409
     410procedure TView.SelectCell(Pos: TPoint; Player: TPlayer);
    368411var
    369412  NewSelectedCell: TCell;
     
    371414  BottomRight: TPoint;
    372415begin
    373   NewSelectedCell := Game.Map.PosToCell(View.CanvasToCellPos(Pos), View);
     416  NewSelectedCell := Game.Map.PosToCell(CanvasToCellPos(Pos), Self);
    374417  if Assigned(NewSelectedCell) then begin
    375418    if Assigned(SelectedCell) and IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
     
    377420      SelectedCell := nil;
    378421    end else
    379     if (NewSelectedCell <> SelectedCell) and (NewSelectedCell.Player = Self) then
     422    if (NewSelectedCell <> SelectedCell) and (NewSelectedCell.Player = Player) then
    380423      SelectedCell := NewSelectedCell
    381424    else
    382     if (NewSelectedCell = SelectedCell) and (NewSelectedCell.Player = Self) then
     425    if (NewSelectedCell = SelectedCell) and (NewSelectedCell.Player = Player) then
    383426      SelectedCell := nil;
    384427  end;
     
    387430procedure TPlayer.Paint(PaintBox: TPaintBox);
    388431begin
    389   Game.Map.Paint(PaintBox.Canvas, View, SelectedCell, FocusedCell);
     432  Game.Map.Paint(PaintBox.Canvas, View);
    390433end;
    391434
     
    393436begin
    394437  View := TView.Create;
    395   SelectedCell := nil;
    396   FocusedCell := nil;
    397438  StartUnits := DefaultPlayerStartUnits;
    398439end;
     
    413454  TotalUnits := Source.TotalUnits;
    414455  StartUnits := Source.StartUnits;
    415   SelectedCell := Source.SelectedCell;
    416   FocusedCell := Source.FocusedCell;
    417456  View.Assign(Source.View);
    418457end;
     
    432471      end else begin
    433472        // Fight
     473        //NewPower := CellTo.Power - Trunc(CountOnce / CellTo.Power);
    434474        if CellTo.Power > CountOnce then begin
    435475          // Defender wins
     
    519559    for I := 0 to Players.Count - 1 do
    520560    with TPlayer(Players[I]) do begin
    521       SelectedCell := nil;
    522       FocusedCell := nil;
     561      View.Clear;
    523562    end;
    524563  end;
     
    582621  end;
    583622  UpdateRepeatMoves(CurrentPlayer);
     623  // For computers take view from previous human
     624  if CurrentPlayer.Mode = pmComputer then CurrentPlayer.View.Assign(PrevPlayer.View);
    584625end;
    585626
     
    639680  for I := 0 to Players.Count - 1 do
    640681  with TPlayer(Players[I]) do begin
    641     SelectedCell := nil;
     682    View.Clear;
    642683    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
    643684      StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)];
     
    771812end;
    772813
    773 procedure THexMap.Paint(Canvas: TCanvas; View: TView; SelectedCell: TCell; FocusedCell: TCell);
     814procedure THexMap.Paint(Canvas: TCanvas; View: TView);
    774815var
    775816  CX, CY: Integer;
     
    788829begin
    789830  with Canvas do begin
    790     if Assigned(FocusedCell) and (FocusedCell = TCell(Cells[CY, CX])) then begin
     831    if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY, CX])) then begin
    791832      Pen.Color := clYellow;
    792833      Pen.Style := psSolid;
Note: See TracChangeset for help on using the changeset viewer.