Changeset 344 for trunk/Player.pas


Ignore:
Timestamp:
Dec 23, 2024, 8:16:05 AM (10 hours ago)
Author:
chronos
Message:
  • Modified: Improved implementation of TItemList class to be generic class to avoid explicit typecasting.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Player.pas

    r342 r344  
    100100    procedure GetValue(Index: Integer; out Value); override;
    101101    procedure SetValue(Index: Integer; var Value); override;
    102     function GetReferenceList(Index: Integer): TItemList; override;
     102    function GetReferenceList(Index: Integer): IList<TItem>; override;
    103103    class function GetClassSysName: string; override;
    104104    class function GetClassName: string; override;
     
    134134  { TPlayers }
    135135
    136   TPlayers = class(TItemList)
     136  TPlayers = class(TItemList<TPlayer>)
    137137  public
    138138    Game: TObject; //TGame;
    139     class function GetItemClass: TItemClass; override;
    140139    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
    141     function CreateItem(Name: string = ''): TItem; override;
     140    function CreateItem(Name: string = ''): TPlayer; override;
    142141    function GetFirstHuman: TPlayer;
    143142    procedure LoadConfig(Config: TXmlConfig; Path: string);
     
    488487  Result := 0;
    489488  for I := 0 to Count - 1 do
    490     if TPlayer(Items[I]).IsAlive then Inc(Result);
     489    if Items[I].IsAlive then Inc(Result);
    491490end;
    492491
     
    497496  Players.Clear;
    498497  for I := 0 to Count - 1 do
    499     if TPlayer(Items[I]).IsAlive then Players.Add(TPlayer(Items[I]));
    500 end;
    501 
    502 class function TPlayers.GetItemClass: TItemClass;
    503 begin
    504   Result := TPlayer;
     498    if Items[I].IsAlive then Players.Add(Items[I]);
    505499end;
    506500
     
    508502begin
    509503  AddItem(Name);
    510   TPlayer(Last).Color := Color;
    511   TPlayer(Last).Mode := Mode;
     504  Last.Color := Color;
     505  Last.Mode := Mode;
    512506  if Mode = pmComputer then
    513     TPlayer(Last).Agressivity := caMedium;
    514 end;
    515 
    516 function TPlayers.CreateItem(Name: string): TItem;
     507    Last.Agressivity := caMedium;
     508end;
     509
     510function TPlayers.CreateItem(Name: string): TPlayer;
    517511begin
    518512  Result := inherited;
    519   TPlayer(Result).Game := Game;
     513  Result.Game := Game;
    520514end;
    521515
     
    525519begin
    526520  I := 0;
    527   while (I < Count) and (TPlayer(Items[I]).Mode <> pmHuman) do Inc(I);
    528   if I < Count then Result := TPlayer(Items[I])
     521  while (I < Count) and (Items[I].Mode <> pmHuman) do Inc(I);
     522  if I < Count then Result := Items[I]
    529523    else Result := nil;
    530524end;
     
    544538        Items[I] := CreateItem;
    545539        Items[I].Id := GetNewId;
    546         TPlayer(Items[I]).LoadConfig(Config, Path + '/Player' + IntToStr(I));
     540        Items[I].LoadConfig(Config, Path + '/Player' + IntToStr(I));
    547541      end;
    548542    end;
     
    555549begin
    556550  for I := 0 to Count - 1 do
    557     TPlayer(Items[I]).SaveConfig(Config, Path + '/Player' + IntToStr(I));
     551    Items[I].SaveConfig(Config, Path + '/Player' + IntToStr(I));
    558552  with Config do begin
    559553    SetValue(DOMString(Path + '/Count'), Count);
     
    568562  SetLength(Result, 0);
    569563  for I := 0 to Count - 1 do
    570     if TPlayer(Items[I]).IsAlive then begin
     564    if Items[I].IsAlive then begin
    571565      SetLength(Result, Length(Result) + 1);
    572       Result[Length(Result) - 1] := TPlayer(Items[I]);
     566      Result[Length(Result) - 1] := Items[I];
    573567    end;
    574568end;
     
    581575  SetLength(Result, 0);
    582576  for I := 0 to Count - 1 do
    583     if TPlayer(Items[I]).TotalCities > 0 then begin
     577    if Items[I].TotalCities > 0 then begin
    584578      SetLength(Result, Length(Result) + 1);
    585       Result[Length(Result) - 1] := TPlayer(Items[I]);
     579      Result[Length(Result) - 1] := Items[I];
    586580    end;
    587581end;
     
    828822    Defensive := GetValue(DOMString(Path + '/Defensive'), False);
    829823    Agressivity := TComputerAgressivity(GetValue(DOMString(Path + '/Agressivity'), 0));
    830     Nation := TNation(TGame(Game).GameSystem.Nations.FindById(GetValue(DOMString(Path + '/Nation'), 0)));
     824    Nation := TGame(Game).GameSystem.Nations.FindById(GetValue(DOMString(Path + '/Nation'), 0));
    831825  end;
    832826end;
     
    12281222end;
    12291223
    1230 function TPlayer.GetReferenceList(Index: Integer): TItemList;
     1224function TPlayer.GetReferenceList(Index: Integer): IList<TItem>;
    12311225begin
    12321226  if Index = 2 then Result := TGame(Game).GameSystem.Nations
     
    12721266    ((TGame(Game).GrowCells = gcPlayerCities) and (Assigned(Building) and (Building.Kind.SpecialType = stCity)))) then begin
    12731267      if not Assigned(OneUnit) then begin
    1274         NewUnit := TGame(Game).Units.AddNew(TUnitKind(TGame(Game).GameSystem.UnitKinds.First), 0);
     1268        NewUnit := TGame(Game).Units.AddNew(TGame(Game).GameSystem.UnitKinds.First, 0);
    12751269        NewUnit.Player := Self;
    12761270        NewUnit.MapCell := Cells[I];
Note: See TracChangeset for help on using the changeset viewer.