Changeset 11


Ignore:
Timestamp:
Feb 18, 2014, 8:15:50 PM (11 years ago)
Author:
chronos
Message:
  • Added: List of moves between cells. Moves are processed after turn end.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UFormMain.pas

    r10 r11  
    185185  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    186186begin
    187   with TPlayer(Game.Players[0]) do
     187  with Game.CurrentPlayer do
    188188    ViewZoom := ViewZoom / ZoomFactor;
    189189  Redraw;
     
    193193  MousePos: TPoint; var Handled: Boolean);
    194194begin
    195   with TPlayer(Game.Players[0]) do
     195  with Game.CurrentPlayer do
    196196    ViewZoom := ViewZoom * ZoomFactor;
    197197  Redraw;
  • trunk/UGame.pas

    r10 r11  
    6565  end;
    6666
     67  TMove = class
     68    CellFrom: TCell;
     69    CellTo: TCell;
     70    CountOnce: Integer;
     71    CountRepeat: Integer;
     72  end;
     73
    6774  { TGame }
    6875
    6976  TGame = class
    70     Players: TObjectList;
     77  private
     78    procedure MoveAll(Player: TPlayer);
     79    procedure SetMove(CellFrom, CellTo: TCell; Count: Integer);
     80  public
     81    Players: TObjectList; // TList<TPlayer>
    7182    Map: TMap;
    7283    VoidEnabled: Boolean;
    7384    CurrentPlayer: TPlayer;
     85    Moves: TObjectList; // TList<TMove>
    7486    procedure NextTurn;
    7587    constructor Create;
     
    149161    (NewSelectedCell.Y >= 0) and (NewSelectedCell.Y < Length(Game.Map.Cells)) then begin
    150162    if IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
    151       // Attack
    152       TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player := Self;
    153       TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Power :=
    154         TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Power +
    155         TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]).Power;
    156       TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]).Power := 0;
     163      Game.SetMove(TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]),
     164        TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]),
     165        TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]).Power);
    157166      SelectedCell := Point(-1, -1);
    158167    end else
     
    180189{ TGame }
    181190
     191procedure TGame.MoveAll(Player: TPlayer);
     192var
     193  I: Integer;
     194begin
     195  for I := 0 to Moves.Count - 1 do
     196  with TMove(Moves[I]) do begin
     197    if CellFrom.Player = Player then begin
     198      if CellTo.Player = Player then begin
     199        // Inner move
     200        CellTo.Power := CellTo.Power + CountOnce;
     201      end else begin
     202        // Fight
     203        if CellTo.Power > CountOnce then begin
     204          // Defender wins
     205          CellTo.Power := CellTo.Power - CountOnce;
     206        end else begin
     207          // Attacker wins
     208          CellTo.Power := CountOnce - CellTo.Power;
     209          CellTo.Player := Player;
     210        end;
     211      end;
     212      CellFrom.Power := CellFrom.Power - CountOnce;
     213      CountOnce := 0;
     214    end;
     215  end;
     216  // Remove empty moves
     217  for I := Moves.Count - 1 downto 0 do
     218  if (TMove(Moves[I]).CellFrom.Player = Player) and
     219    (TMove(Moves[I]).CountOnce = 0) and (TMove(Moves[I]).CountRepeat = 0) then
     220    Moves.Delete(I);
     221end;
     222
     223procedure TGame.SetMove(CellFrom, CellTo: TCell; Count: Integer);
     224var
     225  NewMove: TMove;
     226  I: Integer;
     227begin
     228  I := 0;
     229  while (I < Moves.Count) and (TMove(Moves[I]).CellFrom <> CellFrom) and
     230    (TMove(Moves[I]).CellTo <> CellTo) do Inc(I);
     231  if I < Moves.Count then begin
     232    // Already have such move
     233    if Count = 0 then Moves.Delete(I)
     234      else TMove(Moves[I]).CountOnce := Count;
     235  end else begin
     236    // Add new move
     237    if Count > 0 then begin
     238      NewMove := TMove(Moves[Moves.Add(TMove.Create)]);
     239      NewMove.CellFrom := CellFrom;
     240      NewMove.CellTo := CellTo;
     241      NewMove.CountOnce := Count;
     242    end;
     243  end;
     244end;
     245
    182246procedure TGame.NextTurn;
    183247begin
     248  MoveAll(CurrentPlayer);
    184249  Map.Grow(CurrentPlayer);
    185250  CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
     
    205270
    206271  Map := TMap.Create;
     272  Moves := TObjectList.Create;
    207273end;
    208274
    209275destructor TGame.Destroy;
    210276begin
     277  FreeAndNil(Moves);
    211278  FreeAndNil(Players);
    212279  FreeAndNil(Map);
Note: See TracChangeset for help on using the changeset viewer.