Changeset 47 for trunk/UGame.pas
- Timestamp:
- Aug 17, 2014, 2:02:43 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r42 r47 41 41 MovesFrom: TObjectList; // TList<TMove> 42 42 MovesTo: TObjectList; // TList<TMove> 43 Neighbors: TObjectList; // TList<TCell> 43 44 function GetColor: TColor; 44 45 function GetAvialPower: Integer; … … 98 99 procedure Grow(APlayer: TPlayer); virtual; 99 100 procedure ComputePlayerStats; virtual; 101 procedure Generate; virtual; 100 102 constructor Create; virtual; 101 103 destructor Destroy; override; 102 function GetCellNeighbo urs(Cell: TCell): TCellArray; virtual;104 function GetCellNeighbors(Cell: TCell): TCellArray; virtual; 103 105 procedure Paint(Canvas: TCanvas; View: TView); virtual; 104 106 function GetPixelRect: TRect; virtual; … … 115 117 function GetSize: TPoint; override; 116 118 procedure SetSize(AValue: TPoint); override; 119 function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 120 function GetCellNeighbors2(Cell: TCell): TCellArray; 117 121 public 118 122 Cells: array of array of TCell; 119 function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; override; 120 procedure Assign(Source: TMap); virtual; 123 procedure Assign(Source: TMap); override; 121 124 procedure LoadFromFile(FileName: string); override; 122 125 procedure SaveToFile(FileName: string); override; 123 126 function IsValidIndex(Index: TPoint): Boolean; override; 124 function GetCellNeighbours(Cell: TCell): TCellArray; override;125 127 function PosToCell(Pos: TPoint; View: TView): TCell; override; 126 128 function CellToPos(Cell: TCell): TPoint; override; 127 129 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 128 130 procedure Paint(Canvas: TCanvas; View: TView); override; 131 procedure Generate; override; 129 132 constructor Create; override; 130 133 destructor Destroy; override; … … 140 143 function GetSize: TPoint; override; 141 144 procedure SetSize(AValue: TPoint); override; 145 function IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 146 function GetCellNeighbours2(Cell: TCell): TCellArray; 142 147 public 143 148 Cells: array of array of TCell; 144 function IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; override;145 149 function IsValidIndex(Index: TPoint): Boolean; override; 146 150 function PosToCell(Pos: TPoint; View: TView): TCell; override; 147 151 function CellToPos(Cell: TCell): TPoint; override; 148 function GetCellNeighbours(Cell: TCell): TCellArray; override;149 152 function GetAllCells: TCellArray; override; 150 153 function GetPixelRect: TRect; override; … … 359 362 end; 360 363 361 function TSquareMap.IsCellsNeighbor (Cell1, Cell2: TCell): Boolean;364 function TSquareMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 362 365 var 363 366 DX: Integer; … … 427 430 end; 428 431 429 function TSquareMap.GetCellNeighbours (Cell: TCell): TCellArray;432 function TSquareMap.GetCellNeighbours2(Cell: TCell): TCellArray; 430 433 var 431 434 X, Y: Integer; … … 435 438 for X := -1 to 1 do 436 439 if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and 437 IsCellsNeighbor (Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin440 IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin 438 441 SetLength(Result, Length(Result) + 1); 439 442 Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]; … … 608 611 function TMap.IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; 609 612 begin 610 Result := False;613 Result := Cell1.Neighbors.IndexOf(Cell2) <> -1; 611 614 end; 612 615 … … 683 686 end; 684 687 688 procedure TMap.Generate; 689 begin 690 end; 691 685 692 constructor TMap.Create; 686 693 begin … … 695 702 end; 696 703 697 function TMap.GetCellNeighbours(Cell: TCell): TCellArray; 698 begin 699 704 function TMap.GetCellNeighbors(Cell: TCell): TCellArray; 705 var 706 I: Integer; 707 begin 708 SetLength(Result, Cell.Neighbors.Count); 709 for I := 0 to Length(Result) - 1 do 710 Result[I] := TCell(Cell.Neighbors[I]); 700 711 end; 701 712 … … 729 740 function TMap.GetAllCells: TCellArray; 730 741 begin 731 742 SetLength(Result, 0); 732 743 end; 733 744 … … 861 872 begin 862 873 Player := nil; 874 Neighbors := TObjectList.Create; 875 Neighbors.OwnsObjects := False; 863 876 MovesFrom := TObjectList.Create; 864 877 MovesFrom.OwnsObjects := False; … … 877 890 TUnitMove(MovesTo[I]).Free; 878 891 FreeAndNil(MovesTo); 892 FreeAndNil(Neighbors); 879 893 inherited Destroy; 880 894 end; … … 940 954 // Attack to not owned cell yet 941 955 // Count own possible power 942 Cells := Game.Map.GetCellNeighbo urs(AllCells[C]);956 Cells := Game.Map.GetCellNeighbors(AllCells[C]); 943 957 TotalPower := 0; 944 958 for I := 0 to Length(Cells) - 1 do … … 964 978 // We need to move available power to borders to be available for attacks 965 979 // or defense 966 Cells := Game.Map.GetCellNeighbo urs(AllCells[C]);980 Cells := Game.Map.GetCellNeighbors(AllCells[C]); 967 981 CanAttack := 0; 968 982 for I := 0 to Length(Cells) - 1 do … … 1125 1139 mtHexagon: Map := THexMap.Create; 1126 1140 mtSquare: Map := TSquareMap.Create; 1141 else Map := TMap.Create; 1127 1142 end; 1128 1143 Map.Assign(OldMap); … … 1141 1156 begin 1142 1157 I := 0; 1158 Confirm := True; 1143 1159 while (I < Moves.Count) and ((TUnitMove(Moves[I]).CellFrom <> CellFrom) or 1144 1160 (TUnitMove(Moves[I]).CellTo <> CellTo)) do Inc(I); … … 1225 1241 1226 1242 procedure TGame.LoadConfig(Config: TXmlConfig; Path: string); 1243 var 1244 P: TPoint; 1227 1245 begin 1228 1246 with Config do begin … … 1439 1457 1440 1458 procedure THexMap.SetSize(AValue: TPoint); 1441 var1442 X, Y: Integer;1443 NewCell: TCell;1444 C: Integer;1445 1459 begin 1446 1460 if (FSize.X <> AValue.X) or (FSize.Y <> AValue.Y) then begin 1447 // Free previous1448 for Y := 0 to FSize.Y - 1 do1449 for X := 0 to FSize.X - 1 do begin1450 TCell(Cells[Y, X]).Destroy;1451 end;1452 1461 FSize := AValue; 1453 // Allocate and init new 1454 SetLength(Cells, FSize.Y, FSize.X); 1455 for Y := 0 to FSize.Y - 1 do 1456 for X := 0 to FSize.X - 1 do begin 1457 NewCell := TCell.Create; 1458 NewCell.Pos := Point(X, Y); 1459 Cells[Y, X] := NewCell; 1460 end; 1461 end; 1462 end; 1463 1464 function THexMap.IsCellsNeighbor(Cell1, Cell2: TCell): Boolean; 1462 Generate; 1463 end; 1464 end; 1465 1466 function THexMap.IsCellsNeighbor2(Cell1, Cell2: TCell): Boolean; 1465 1467 var 1466 1468 DX: Integer; … … 1484 1486 procedure THexMap.Assign(Source: TMap); 1485 1487 begin 1488 inherited; 1486 1489 end; 1487 1490 … … 1522 1525 end; 1523 1526 1524 function THexMap.GetCellNeighbo urs(Cell: TCell): TCellArray;1527 function THexMap.GetCellNeighbors2(Cell: TCell): TCellArray; 1525 1528 var 1526 1529 X, Y: Integer; … … 1530 1533 for X := -1 to 1 do 1531 1534 if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and 1532 IsCellsNeighbor (Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin1535 IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin 1533 1536 SetLength(Result, Length(Result) + 1); 1534 1537 Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]; … … 1686 1689 end; 1687 1690 1691 procedure THexMap.Generate; 1692 var 1693 X, Y: Integer; 1694 I: Integer; 1695 NewCell: TCell; 1696 NeighCells: TCellArray; 1697 begin 1698 // Free previous 1699 for Y := 0 to Length(Cells) - 1 do 1700 for X := 0 to Length(Cells[Y]) - 1 do begin 1701 TCell(Cells[Y, X]).Destroy; 1702 end; 1703 // Allocate and init new 1704 SetLength(Cells, FSize.Y, FSize.X); 1705 for Y := 0 to FSize.Y - 1 do 1706 for X := 0 to FSize.X - 1 do begin 1707 NewCell := TCell.Create; 1708 NewCell.Pos := Point(X, Y); 1709 Cells[Y, X] := NewCell; 1710 end; 1711 // Generate neightbours 1712 for Y := 0 to FSize.Y - 1 do 1713 for X := 0 to FSize.X - 1 do begin 1714 NeighCells := GetCellNeighbors2(Cells[Y, X]); 1715 for I := 0 to Length(NeighCells) - 1 do 1716 Cells[Y, X].Neighbors.Add(NeighCells[I]); 1717 end; 1718 end; 1719 1688 1720 constructor THexMap.Create; 1689 1721 begin
Note:
See TracChangeset
for help on using the changeset viewer.