Changeset 77 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 5, 2014, 7:13:38 PM (10 years ago)
Author:
chronos
Message:
  • Added: Auto save game to file option in settings.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r76 r77  
    2525  TUnitMoves = class;
    2626  TCells = class;
     27  TMap = class;
    2728
    2829  TFloatPoint = record
     
    3839  TCell = class
    3940  private
     41    FMap: TMap;
    4042    FPower: Integer;
    4143    procedure SetPower(AValue: Integer);
     
    4547    Polygon: TPointArray;
    4648    Terrain: TTerrainType;
     49    PlayerId: Integer;
    4750    Player: TPlayer;
    4851    MovesFrom: TUnitMoves;
    4952    MovesTo: TUnitMoves;
     53    NeighborsId: array of Integer;
    5054    Neighbors: TCells;
    5155    Mark: Boolean;
     56    procedure FixRefId;
    5257    procedure LoadFromNode(Node: TDOMNode);
    5358    procedure SaveToNode(Node: TDOMNode);
     
    6065    destructor Destroy; override;
    6166    property Power: Integer read FPower write SetPower;
     67    property Map: TMap read FMap write FMap;
    6268  end;
    6369
     
    6773
    6874  TCells = class(TObjectList)
     75    Map: TMap;
     76    procedure FixRefId;
    6977    function FindById(Id: Integer): TCell;
    7078    procedure LoadFromNode(Node: TDOMNode);
     
    235243    Game: TGame;
    236244    NewPlayerId: Integer;
     245    function FindById(Id: Integer): TPlayer;
    237246    procedure New(Name: string; Color: TColor; Mode: TPlayerMode);
    238247    function GetNewPlayerId: Integer;
     
    285294    FMapType: TMapType;
    286295    FOnMove: TMoveEvent;
     296    FOnNewTurn: TNotifyEvent;
    287297    FOnWin: TWinEvent;
    288298    FRunning: Boolean;
     
    334344    property OnMove: TMoveEvent read FOnMove write FOnMove;
    335345    property OnWin: TWinEvent read FOnWin write FOnWin;
     346    property OnNewTurn: TNotifyEvent read FOnNewTurn write FOnNewTurn;
    336347  end;
    337348
     
    427438{ TCells }
    428439
     440procedure TCells.FixRefId;
     441var
     442  I: Integer;
     443begin
     444  for I := 0 to Count - 1 do
     445  TCell(Items[I]).FixRefId;
     446end;
     447
    429448function TCells.FindById(Id: Integer): TCell;
    430449var
     
    432451begin
    433452  I := 0;
    434   while (I < 0) and (TCell(Items[I]).Id <> Id) do Inc(I);
     453  while (I < Count) and (TCell(Items[I]).Id <> Id) do Inc(I);
    435454  if I < Count then Result := TCell(Items[I])
    436455    else Result := nil;
     
    446465  while Assigned(Node2) and (Node2.NodeName = 'Cell') do begin
    447466    NewCell := TCell.Create;
     467    NewCell.Map := Map;
    448468    NewCell.LoadFromNode(Node2);
    449469    Add(NewCell);
     
    466486
    467487{ TPlayers }
     488
     489function TPlayers.FindById(Id: Integer): TPlayer;
     490var
     491  I: Integer;
     492begin
     493  I := 0;
     494  while (I < Count) and (TPlayer(Items[I]).Id <> Id) do Inc(I);
     495  if I < Count then Result := TPlayer(Items[I])
     496    else Result := nil;
     497end;
    468498
    469499procedure TPlayers.New(Name: string; Color: TColor; Mode: TPlayerMode);
     
    574604  for X := 0 to FSize.X - 1 do begin
    575605    NewCell := TCell.Create;
     606    NewCell.Map := Self;
    576607    NewCell.PosPx := Point(Trunc(Random * FSize.X * DefaultCellSize.X), Trunc(Random * FSize.Y * DefaultCellSize.Y));
    577608    SetLength(NewCell.Polygon, 1);
     
    618649  for X := 0 to FSize.X - 1 do begin
    619650    NewCell := TCell.Create;
     651    NewCell.Map := Self;
    620652    Reverse := Boolean(X mod 2) xor Boolean(Y mod 2);
    621653    if Reverse then Rev := -1
     
    658690  for X := 0 to FSize.X - 1 do begin
    659691    NewCell := TCell.Create;
     692    NewCell.Map := Self;
    660693    NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * SquareCellMulX),
    661694      Trunc(Y * DefaultCellSize.Y * SquareCellMulY));
     
    960993  for X := 0 to FSize.X - 1 do begin
    961994    NewCell := TCell.Create;
     995    NewCell.Map := Self;
    962996    NewCell.PosPx := Point(X, Y);
    963997    NewCell.Id := GetNewCellId;
     
    9711005  DefaultCellSize := Point(220, 220);
    9721006  Cells := TCells.create;
     1007  Cells.Map := Self;
    9731008  Size := Point(0, 0);
    9741009  Image := TImage.Create(nil);
     
    11451180end;
    11461181
     1182procedure TCell.FixRefId;
     1183var
     1184  I: Integer;
     1185begin
     1186  Player := Map.Game.Players.FindById(PlayerId);
     1187
     1188  Neighbors.Count := Length(NeighborsId);
     1189  for I := 0 to Length(NeighborsId) - 1 do begin
     1190     Neighbors[I] := Map.Cells.FindById(NeighborsId[I]);
     1191  end;
     1192end;
     1193
    11471194procedure TCell.LoadFromNode(Node: TDOMNode);
    11481195var
     
    11561203  PosPx.X := ReadInteger(Node, 'PosX', 0);
    11571204  PosPx.Y := ReadInteger(Node, 'PosY', 0);
    1158   //Player := Map.Game.Players.FindById(ReadInteger(Node, 'Player', 0));
     1205  PlayerId := ReadInteger(Node, 'Player', 0);
     1206
     1207  Node3 := Node.FindNode('Neighbours');
     1208  if Assigned(Node3) then begin
     1209    SetLength(NeighborsId, 0);
     1210    Node2 := Node3.FirstChild;
     1211    while Assigned(Node2) and (Node2.NodeName = 'Neighbour') do begin
     1212      SetLength(NeighborsId, Length(NeighborsId) + 1);
     1213      NeighborsId[High(NeighborsId)] := ReadInteger(Node2, 'Id', 0);
     1214      Node2 := Node2.NextSibling;
     1215    end;
     1216  end;
    11591217
    11601218  Node3 := Node.FindNode('Polygon');
     
    19772035      if Assigned(NewNode) then
    19782036        Moves.LoadFromNode(NewNode);
     2037
     2038      Map.Cells.FixRefId;
    19792039    end;
    19802040  finally
     
    20782138    CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
    20792139  until CurrentPlayer.TotalCells > 0;
    2080   if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then Inc(TurnCounter);
     2140  if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin
     2141    Inc(TurnCounter);
     2142    if Assigned(FOnNewTurn) then
     2143      FOnNewTurn(Self);
     2144  end;
    20812145  CheckWinObjective;
    20822146  // For computers take view from previous human
     
    23642428  for X := 0 to FSize.X - 1 do begin
    23652429    NewCell := TCell.Create;
     2430    NewCell.Map := Self;
    23662431    PX := X;
    23672432    PY := Y;
Note: See TracChangeset for help on using the changeset viewer.