Changeset 14


Ignore:
Timestamp:
Feb 19, 2014, 10:51:42 PM (11 years ago)
Author:
chronos
Message:
  • Modified: Refer to cell by object reference instead of TPoint position.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r13 r14  
    5252    Cells: array of array of TCell;
    5353    function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double
    54       ): TPoint;
     54      ): TCell;
    5555    function CellToPos(Cell: TCell; Rect: TRect; Zoom: Double
    5656      ): TPoint;
    5757    function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;
    58     procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint);
     58    procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell);
    5959    constructor Create;
    6060    destructor Destroy; override;
     
    7474    Color: TColor;
    7575    ViewZoom: Double;
    76     SelectedCell: TPoint;
     76    SelectedCell: TCell;
    7777    Mode: TPlayerMode;
    7878    procedure ComputerTurn;
     
    141141end;
    142142
    143 function IsCellsNeighbor(Cell1, Cell2: TPoint): Boolean;
     143function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean;
    144144var
    145145  DX: Integer;
     
    147147  MinY: Integer;
    148148begin
    149   if Cell1.Y < Cell2.Y then MinY:= Cell1.Y
    150     else MinY := Cell2.Y;
    151   DX := Cell2.X - Cell1.X;
    152   DY := Cell2.Y - Cell1.Y;
     149  if Cell1.Pos.Y < Cell2.Pos.Y then MinY:= Cell1.Pos.Y
     150    else MinY := Cell2.Pos.Y;
     151  DX := Cell2.Pos.X - Cell1.Pos.X;
     152  DY := Cell2.Pos.Y - Cell1.Pos.Y;
    153153  Result := (Abs(DX) <= 1) and (Abs(DY) <= 1) and
    154154  ((((MinY mod 2) = 1) and
     
    192192procedure TPlayer.SelectCell(Pos: TPoint);
    193193var
    194   NewSelectedCell: TPoint;
     194  NewSelectedCell: TCell;
    195195begin
    196196  NewSelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom);
    197   if (NewSelectedCell.X >= 0) and (NewSelectedCell.X < Game.Map.Size.X) and
    198     (NewSelectedCell.Y >= 0) and (NewSelectedCell.Y < Game.Map.Size.Y) then begin
    199     if IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
    200       Game.SetMove(TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]),
    201         TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]));
    202       SelectedCell := Point(-1, -1);
     197  if Assigned(NewSelectedCell) then begin
     198    if Assigned(SelectedCell) and IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin
     199      Game.SetMove(SelectedCell, NewSelectedCell);
     200      SelectedCell := nil;
    203201    end else
    204     if (NewSelectedCell.X <> SelectedCell.X) and (NewSelectedCell.Y <> SelectedCell.Y) and
    205       (TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player = Self) then
     202    if (NewSelectedCell <> SelectedCell) and (NewSelectedCell.Player = Self) then
    206203      SelectedCell := NewSelectedCell
    207204    else
    208     if (NewSelectedCell.X = SelectedCell.X) and (NewSelectedCell.Y = SelectedCell.Y) and
    209       (TCell(Game.Map.Cells[NewSelectedCell.Y, NewSelectedCell.X]).Player = Self) then
    210         SelectedCell := Point(-1, -1);
     205    if (NewSelectedCell = SelectedCell) and (NewSelectedCell.Player = Self) then
     206      SelectedCell := nil;
    211207  end;
    212208end;
     
    220216begin
    221217  ViewZoom := 1;
    222   SelectedCell := Point(-1, -1);
     218  SelectedCell := nil;
    223219end;
    224220
     
    349345  for I := 0 to Players.Count - 1 do
    350346  with TPlayer(Players[I]) do begin
     347    SelectedCell := nil;
    351348    StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)];
    352349    StartCell.Terrain := ttNormal;
     
    378375  C: Integer;
    379376begin
    380   // Free previous
    381   for Y := 0 to FSize.Y - 1 do
    382   for X := 0 to FSize.X - 1 do begin
    383     Cells[Y, X].Destroy;
    384   end;
    385   FSize := AValue;
    386   // Allocate and init new
    387   SetLength(Cells, FSize.X, FSize.Y);
    388   for Y := 0 to FSize.Y - 1 do
    389   for X := 0 to FSize.X - 1 do begin
    390     NewCell := TCell.Create;
    391     NewCell.Pos := Point(X, Y);
    392     Cells[Y, X] := NewCell;
    393   end;
    394 end;
    395 
    396 function THexMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TPoint;
     377  if (FSize.X <> AValue.X) or (FSize.Y <> AValue.Y) then begin
     378    // Free previous
     379    for Y := 0 to FSize.Y - 1 do
     380    for X := 0 to FSize.X - 1 do begin
     381      Cells[Y, X].Destroy;
     382    end;
     383    FSize := AValue;
     384    // Allocate and init new
     385    SetLength(Cells, FSize.X, FSize.Y);
     386    for Y := 0 to FSize.Y - 1 do
     387    for X := 0 to FSize.X - 1 do begin
     388      NewCell := TCell.Create;
     389      NewCell.Pos := Point(X, Y);
     390      Cells[Y, X] := NewCell;
     391    end;
     392  end;
     393end;
     394
     395function THexMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TCell;
    397396var
    398397  CX, CY: Integer;
     
    402401  Points: array of TPoint;
    403402begin
     403  Result := nil;
    404404  CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom);
    405405  HexSize := FloatPoint(DefaultCellSize.X * Zoom, DefaultCellSize.Y * Zoom);
     
    417417          Trunc(Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y)), Point(Trunc(HexSize.X), Trunc(HexSize.Y)));
    418418        if PtInPoly(Points, Pos) then begin
    419           Result := Point(CX, CY);
     419          Result := Cells[CY, CX];
    420420          Exit;
    421421        end;
     
    446446
    447447
    448 procedure THexMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint);
     448procedure THexMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell);
    449449var
    450450  CX, CY: Integer;
     
    494494        Cell := Cells[CY, CX];
    495495        if Cell.Terrain = ttNormal then begin
    496           if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen
    497             else if IsCellsNeighbor(SelectedCell, Point(CX, CY)) then Brush.Color := clPurple
     496          if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY, CX])) then Brush.Color := clGreen
     497            else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY, CX])) then Brush.Color := clPurple
    498498            else Brush.Color := Cell.GetColor;
    499499          Pen.Color := clBlack;
Note: See TracChangeset for help on using the changeset viewer.