Changeset 9 for trunk/UGame.pas


Ignore:
Timestamp:
Feb 12, 2014, 10:58:00 PM (11 years ago)
Author:
chronos
Message:
  • Added: Neighbors of selected cell can be selected as attack target.
  • Added: End of turn cause player switch.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r8 r9  
    3434  private
    3535  public
     36    MaxPower: Integer;
    3637    DefaultCellSize: TPoint;
    3738    Cells: array of array of TCell;
     
    4344    destructor Destroy; override;
    4445    procedure Init(Size: TPoint);
     46    procedure Grow(APlayer: TPlayer);
    4547  end;
    4648
     
    6567    Map: TMap;
    6668    VoidEnabled: Boolean;
     69    CurrentPlayer: TPlayer;
     70    procedure NextTurn;
    6771    constructor Create;
    6872    destructor Destroy; override;
     
    99103end;
    100104
     105function IsCellsNeighbor(Cell1, Cell2: TPoint): Boolean;
     106var
     107  DX: Integer;
     108  DY: Integer;
     109  MinY: Integer;
     110begin
     111  if Cell1.Y < Cell2.Y then MinY:= Cell1.Y
     112    else MinY := Cell2.Y;
     113  DX := Cell2.X - Cell1.X;
     114  DY := Cell2.Y - Cell1.Y;
     115  Result := (Abs(DX) <= 1) and (Abs(DY) <= 1) and
     116  ((((MinY mod 2) = 1) and
     117    not ((DX = 1) and (DY = -1)) and
     118    not ((DX = -1) and (DY = 1))) or
     119    (((MinY mod 2) = 0) and
     120    not ((DX = -1) and (DY = -1)) and
     121    not ((DX = 1) and (DY = 1))));
     122end;
     123
    101124{ TCell }
    102125
     
    114137begin
    115138  NewSelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom);
    116   if (NewSelectedCell.X = SelectedCell.X) and (NewSelectedCell.Y = SelectedCell.Y) then
    117     SelectedCell := Point(-1, -1)
    118     else SelectedCell := NewSelectedCell;
     139  if (NewSelectedCell.X >= 0) and (NewSelectedCell.X < Length(Game.Map.Cells[0])) and
     140    (NewSelectedCell.Y >= 0) and (NewSelectedCell.Y < Length(Game.Map.Cells)) then begin
     141    if IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
     142      // Attack
     143      TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player := Self;
     144      TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Power :=
     145        TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Power +
     146        TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]).Power;
     147      TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]).Power := 0;
     148      SelectedCell := Point(-1, -1);
     149    end else
     150    if (NewSelectedCell.X <> SelectedCell.X) and (NewSelectedCell.Y <> SelectedCell.Y) and
     151      (TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player = Self) then
     152      SelectedCell := NewSelectedCell
     153    else
     154    if (NewSelectedCell.X = SelectedCell.X) and (NewSelectedCell.Y = SelectedCell.Y) and
     155      (TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player = Self) then
     156        SelectedCell := Point(-1, -1);
     157  end;
    119158end;
    120159
     
    127166begin
    128167  ViewZoom := 1;
     168  SelectedCell := Point(-1, -1);
    129169end;
    130170
    131171{ TGame }
     172
     173procedure TGame.NextTurn;
     174begin
     175  Map.Grow(CurrentPlayer);
     176  CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]);
     177end;
    132178
    133179constructor TGame.Create;
     
    137183  Players := TObjectList.Create;
    138184  Player := TPlayer.Create;
     185  Player.Name := 'Player 1';
    139186  Player.Game := Self;
    140187  Player.Color := clBlue;
    141188  Players.Add(Player);
    142189  Player := TPlayer.Create;
     190  Player.Name := 'Player 2';
    143191  Player.Game := Self;
    144192  Player.Color := clRed;
    145193  Players.Add(Player);
     194
     195  //VoidEnabled := True;
    146196
    147197  Map := TMap.Create;
     
    176226    StartCell.Player := TPlayer(Players[I]);
    177227  end;
     228  CurrentPlayer := TPlayer(Players[0]);
    178229end;
    179230
     
    261312      if Cells[CY, CX].Terrain = ttNormal then begin
    262313        if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen
     314          else if IsCellsNeighbor(SelectedCell, Point(CX, CY)) then Brush.Color := clPurple
    263315          else Brush.Color := Cells[CY, CX].GetColor;
    264316        PaintHexagon(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X,
     
    274326begin
    275327  DefaultCellSize := Point(62, 62);
     328  MaxPower := 99;
    276329end;
    277330
     
    302355end;
    303356
     357procedure TMap.Grow(APlayer: TPlayer);
     358var
     359  X, Y: Integer;
     360begin
     361  for Y := 0 to Length(Cells) - 1 do
     362  for X := 0 to Length(Cells[0]) - 1 do
     363  with TCell(Cells[Y, X]) do begin
     364    if Player = APlayer then begin
     365      Power := Power + 1;
     366      if Power > MaxPower then Power := MaxPower;
     367    end;
     368  end;
     369end;
     370
    304371end.
    305372
Note: See TracChangeset for help on using the changeset viewer.