Changeset 9


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.
Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/UFormMain.lfm

    r7 r9  
    11object FormMain: TFormMain
    2   Left = 526
     2  Left = 503
    33  Height = 621
    4   Top = 270
     4  Top = 263
    55  Width = 775
    66  Caption = 'xTactics'
     
    1212  OnShow = FormShow
    1313  LCLVersion = '1.3'
     14  WindowState = wsMaximized
    1415  object PaintBox1: TPaintBox
    1516    Left = 0
     
    5253  end
    5354  object ActionList1: TActionList
    54     left = 223
     55    left = 224
    5556    top = 49
    5657    object AGameNew: TAction
    5758      Caption = 'New'
    5859      OnExecute = AGameNewExecute
     60      ShortCut = 16462
    5961    end
    6062    object AExit: TAction
    6163      Caption = 'Exit'
    6264      OnExecute = AExitExecute
     65      ShortCut = 32883
    6366    end
    6467    object AGameEnd: TAction
    6568      Caption = 'End'
     69      ShortCut = 16454
    6670    end
    6771    object AGameEndTurn: TAction
    6872      Caption = 'End turn'
     73      OnExecute = AGameEndTurnExecute
     74      ShortCut = 16468
    6975    end
    7076    object AGameRestart: TAction
    7177      Caption = 'Restart'
     78      ShortCut = 16466
    7279    end
    7380  end
  • trunk/UFormMain.pas

    r8 r9  
    3434    Timer1: TTimer;
    3535    procedure AExitExecute(Sender: TObject);
     36    procedure AGameEndTurnExecute(Sender: TObject);
    3637    procedure AGameNewExecute(Sender: TObject);
    3738    procedure FormCreate(Sender: TObject);
     
    9293    RedrawPending := False;
    9394    PaintBox1.Repaint;
     95    Caption := Game.CurrentPlayer.Name + ' - xTactics';
    9496  end;
    9597end;
     
    108110begin
    109111  Application.Terminate;
     112end;
     113
     114procedure TFormMain.AGameEndTurnExecute(Sender: TObject);
     115begin
     116  Game.NextTurn;
     117  Redraw;
    110118end;
    111119
     
    128136begin
    129137  Game.New;
     138  Redraw;
    130139end;
    131140
  • 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.