Changeset 14
- Timestamp:
- Feb 19, 2014, 10:51:42 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r13 r14 52 52 Cells: array of array of TCell; 53 53 function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double 54 ): T Point;54 ): TCell; 55 55 function CellToPos(Cell: TCell; Rect: TRect; Zoom: Double 56 56 ): TPoint; 57 57 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 58 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: T Point);58 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell); 59 59 constructor Create; 60 60 destructor Destroy; override; … … 74 74 Color: TColor; 75 75 ViewZoom: Double; 76 SelectedCell: T Point;76 SelectedCell: TCell; 77 77 Mode: TPlayerMode; 78 78 procedure ComputerTurn; … … 141 141 end; 142 142 143 function IsCellsNeighbor(Cell1, Cell2: T Point): Boolean;143 function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; 144 144 var 145 145 DX: Integer; … … 147 147 MinY: Integer; 148 148 begin 149 if Cell1. Y < Cell2.Y then MinY:= Cell1.Y150 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; 153 153 Result := (Abs(DX) <= 1) and (Abs(DY) <= 1) and 154 154 ((((MinY mod 2) = 1) and … … 192 192 procedure TPlayer.SelectCell(Pos: TPoint); 193 193 var 194 NewSelectedCell: T Point;194 NewSelectedCell: TCell; 195 195 begin 196 196 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; 203 201 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 206 203 SelectedCell := NewSelectedCell 207 204 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; 211 207 end; 212 208 end; … … 220 216 begin 221 217 ViewZoom := 1; 222 SelectedCell := Point(-1, -1);218 SelectedCell := nil; 223 219 end; 224 220 … … 349 345 for I := 0 to Players.Count - 1 do 350 346 with TPlayer(Players[I]) do begin 347 SelectedCell := nil; 351 348 StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)]; 352 349 StartCell.Terrain := ttNormal; … … 378 375 C: Integer; 379 376 begin 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; 393 end; 394 395 function THexMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TCell; 397 396 var 398 397 CX, CY: Integer; … … 402 401 Points: array of TPoint; 403 402 begin 403 Result := nil; 404 404 CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom); 405 405 HexSize := FloatPoint(DefaultCellSize.X * Zoom, DefaultCellSize.Y * Zoom); … … 417 417 Trunc(Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y)), Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 418 418 if PtInPoly(Points, Pos) then begin 419 Result := Point(CX, CY);419 Result := Cells[CY, CX]; 420 420 Exit; 421 421 end; … … 446 446 447 447 448 procedure THexMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: T Point);448 procedure THexMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TCell); 449 449 var 450 450 CX, CY: Integer; … … 494 494 Cell := Cells[CY, CX]; 495 495 if Cell.Terrain = ttNormal then begin 496 if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen497 else if IsCellsNeighbor(SelectedCell, Point(CX, CY)) then Brush.Color := clPurple496 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 498 498 else Brush.Color := Cell.GetColor; 499 499 Pen.Color := clBlack;
Note:
See TracChangeset
for help on using the changeset viewer.