Changeset 125 for trunk/UGame.pas


Ignore:
Timestamp:
Jun 17, 2017, 2:24:51 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Introduced TClient class for real user accessing particular player or spectator. TPlayer is now just player in the game.
  • Fixed: Exception if no human player was selected.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r115 r125  
    2828  TCellLinks = class;
    2929  TMapArea = class;
     30  TClient = class;
    3031
    3132  TFloatPoint = record
     
    248249  TPlayer = class
    249250  private
     251    FClient: TClient;
    250252    FGame: TGame;
     253    procedure SetClient(AValue: TClient);
    251254    procedure SetGame(AValue: TGame);
    252255  public
     
    254257    Name: string;
    255258    Color: TColor;
    256     View: TView;
    257259    Mode: TPlayerMode;
    258260    TotalUnits: Integer;
     
    266268    procedure LoadFromNode(Node: TDOMNode);
    267269    procedure SaveToNode(Node: TDOMNode);
    268     procedure Paint(Canvas: TCanvas);
     270    procedure Paint(Canvas: TCanvas; View: TView);
    269271    constructor Create;
    270272    destructor Destroy; override;
    271273    procedure Assign(Source: TPlayer);
    272274    property Game: TGame read FGame write SetGame;
     275    property Client: TClient read FClient write SetClient;
    273276  end;
    274277
     
    333336  end;
    334337
     338  { TClient }
     339
     340  TClient = class
     341  private
     342    FGame: TGame;
     343    FControlPlayer: TPlayer;
     344    procedure SetControlPlayer(AValue: TPlayer);
     345    procedure SetGame(AValue: TGame);
     346  public
     347    Name: string;
     348    View: TView;
     349    constructor Create;
     350    destructor Destroy; override;
     351    property ControlPlayer: TPlayer read FControlPlayer write SetControlPlayer;
     352    property Game: TGame read FGame write SetGame;
     353  end;
     354
     355  { TClients }
     356
     357  TClients = class(TObjectList)
     358    Game: TGame;
     359    procedure New(Name: string);
     360  end;
     361
    335362  { TGame }
    336363
     
    349376    FOnMove: TMoveEvent;
    350377    FOnNewTurn: TNotifyEvent;
     378    FOnPlayerChange: TNotifyEvent;
    351379    FOnWin: TWinEvent;
    352380    FRunning: Boolean;
     
    367395  public
    368396    Players: TPlayers;
     397    Clients: TClients;
    369398    Map: TMap;
    370399    MapImageFileName: string;
     
    406435    property OnWin: TWinEvent read FOnWin write FOnWin;
    407436    property OnNewTurn: TNotifyEvent read FOnNewTurn write FOnNewTurn;
     437    property OnPlayerChange: TNotifyEvent read FOnPlayerChange write FOnPlayerChange;
    408438  end;
    409439
     
    509539    ((((Color shr 16) and $ff) shr 1) shl 16) or
    510540    ((((Color shr 24) and $ff) shr 0) shl 24);
     541end;
     542
     543{ TClients }
     544
     545procedure TClients.New(Name: string);
     546var
     547  NewClient: TClient;
     548begin
     549  NewClient := TClient.Create;
     550  NewClient.Game := Game;
     551  NewClient.Name := Name;
     552  Add(NewClient);
     553end;
     554
     555{ TClient }
     556
     557procedure TClient.SetGame(AValue: TGame);
     558begin
     559  if FGame = AValue then Exit;
     560  FGame := AValue;
     561  View.Game := AValue;
     562end;
     563
     564procedure TClient.SetControlPlayer(AValue: TPlayer);
     565begin
     566  if FControlPlayer = AValue then Exit;
     567  if Assigned(FControlPlayer) then
     568    FControlPlayer.FClient := nil;
     569  FControlPlayer := AValue;
     570  if Assigned(FControlPlayer) then
     571     FControlPlayer.FClient := Self;
     572end;
     573
     574constructor TClient.Create;
     575begin
     576  View := TView.Create;
     577end;
     578
     579destructor TClient.Destroy;
     580begin
     581  ControlPlayer := nil;
     582  FreeAndNil(View);
     583  inherited Destroy;
    511584end;
    512585
     
    17401813  if FGame = AValue then Exit;
    17411814  FGame := AValue;
    1742   View.Game := Game;
     1815end;
     1816
     1817procedure TPlayer.SetClient(AValue: TClient);
     1818begin
     1819  if FClient=AValue then Exit;
     1820  if Assigned(FClient) then FClient.FControlPlayer := nil;
     1821  FClient := AValue;
     1822  if Assigned(FClient) then FClient.FControlPlayer := Self;
    17431823end;
    17441824
     
    21462226end;
    21472227
    2148 procedure TPlayer.Paint(Canvas: TCanvas);
     2228procedure TPlayer.Paint(Canvas: TCanvas; View: TView);
    21492229begin
    21502230  PlayerMap.Paint(Canvas, View);
     
    21532233constructor TPlayer.Create;
    21542234begin
    2155   View := TView.Create;
    21562235  StartUnits := DefaultPlayerStartUnits;
    21572236  StartCell := nil;
     
    21632242begin
    21642243  FreeAndNil(PlayerMap);
    2165   FreeAndNil(View);
    21662244  inherited Destroy;
    21672245end;
     
    21802258  Agressivity := Source.Agressivity;
    21812259  Defensive := Source.Defensive;
    2182   View.Assign(Source.View);
    21832260end;
    21842261
     
    24322509  end else begin
    24332510    FRunning := AValue;
    2434     for I := 0 to Players.Count - 1 do
    2435     with TPlayer(Players[I]) do begin
     2511    for I := 0 to Clients.Count - 1 do
     2512    with TClient(Clients[I]) do begin
    24362513      View.Clear;
    24372514    end;
     
    28112888  PrevPlayer: TPlayer;
    28122889begin
    2813   CurrentPlayer.View.SelectedCell := nil;
     2890  //TODO CurrentPlayer.View.SelectedCell := nil;
    28142891  MoveAll(CurrentPlayer);
    28152892  Map.Grow(CurrentPlayer);
     
    28202897  repeat
    28212898    CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
     2899    if Assigned(FOnPlayerChange) then
     2900      FOnPlayerChange(Self);
    28222901  until CurrentPlayer.TotalCells > 0;
    28232902  if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin
     
    28702949  Players := TPlayers.Create;
    28712950  Players.Game := Self;
     2951  Clients := TClients.Create;
     2952  Clients.Game := Self;
    28722953
    28732954  MapImageFileName := 'Images/Maps/WorldMap.png';
     
    28872968destructor TGame.Destroy;
    28882969begin
     2970  FreeAndNil(Clients);
    28892971  FreeAndNil(Moves);
    28902972  FreeAndNil(Players);
     
    29183000  end;
    29193001
    2920 
    29213002  if SymetricMap then begin
    29223003    for C := 0 to (Map.Cells.Count div 2) - 1 do begin
     
    29313012  with TPlayer(Players[I]) do begin
    29323013    PlayerMap.Update;
    2933     View.Clear;
    29343014    if (Map.Size.X > 0) and (Map.Size.Y > 0) then begin
    29353015      // Try to obtain start cell for each player
     
    29503030      StartCell.Power := TPlayer(Players[I]).StartUnits;
    29513031    end;
     3032    PlayerMap.CheckVisibility;
     3033  end;
     3034  if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
     3035    else CurrentPlayer := nil;
     3036
     3037  Clients.Clear;
     3038  Clients.New('Spectator');
     3039  for I := 0 to Players.Count - 1 do
     3040  with TPlayer(Players[I]) do
     3041  if Mode = pmHuman then begin
     3042    Clients.New(TPlayer(Players[I]).Name);
     3043    TPlayer(Players[I]).Client := TClient(Clients.Last);
     3044  end;
     3045
     3046  for I := 0 to Clients.Count - 1 do
     3047  with TClient(Clients[I]) do begin
     3048    View.Clear;
    29523049    View.Zoom := 1;
    29533050    View.CenterMap;
    2954     PlayerMap.CheckVisibility;
    2955   end;
    2956   if Players.Count > 0 then CurrentPlayer := TPlayer(Players[0])
    2957     else CurrentPlayer := nil;
     3051  end;
    29583052end;
    29593053
     
    29853079    with TCellLink(CellLinks[C]) do begin
    29863080      if Length(Points) >= 2 then begin
    2987         MoveTo(Points[0]);
     3081        MoveTo(View.CellToCanvasPos(Points[0]));
    29883082        for I := 1 to Length(Points) - 1 do
    2989           LineTo(Points[I]);
     3083          LineTo(View.CellToCanvasPos(Points[I]));
    29903084      end;
    29913085    end;
Note: See TracChangeset for help on using the changeset viewer.