Changeset 49
- Timestamp:
- Aug 17, 2014, 12:14:10 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r48 r49 89 89 MaxPower: Integer; 90 90 DefaultCellSize: TPoint; 91 Cells: TObjectList; // TList<TCell> 91 92 procedure DrawArrow(Canvas: TCanvas; View: TView; Pos: TPoint; Angle: Double; 92 93 Size: TPoint; Text: string); … … 118 119 function GetCellNeighbors2(Cell: TCell): TCellArray; 119 120 public 120 Cells: array of array of TCell;121 121 procedure Assign(Source: TMap); override; 122 122 procedure LoadFromFile(FileName: string); override; … … 141 141 function GetCellNeighbors2(Cell: TCell): TCellArray; 142 142 public 143 Cells: array of array of TCell;144 143 procedure Generate; override; 145 144 function IsValidIndex(Index: TPoint): Boolean; override; … … 337 336 inherited; 338 337 // Free previous 339 for Y := 0 to Length(Cells) - 1 do 340 for X := 0 to Length(Cells[Y]) - 1 do begin 341 TCell(Cells[Y, X]).Destroy; 342 end; 338 Cells.Count := 0; 343 339 // Allocate and init new 344 SetLength(Cells, FSize.Y, FSize.X);340 Cells.Count := FSize.Y * FSize.X; 345 341 for Y := 0 to FSize.Y - 1 do 346 342 for X := 0 to FSize.X - 1 do begin 347 343 NewCell := TCell.Create; 348 344 NewCell.Pos := Point(X, Y); 349 Cells[Y ,X] := NewCell;345 Cells[Y * FSize.X + X] := NewCell; 350 346 end; 351 347 // Generate neightbours 352 348 for Y := 0 to FSize.Y - 1 do 353 349 for X := 0 to FSize.X - 1 do begin 354 NeighCells := GetCellNeighbors2( Cells[Y, X]);350 NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X])); 355 351 for I := 0 to Length(NeighCells) - 1 do 356 Cells[Y, X].Neighbors.Add(NeighCells[I]);352 TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]); 357 353 end; 358 354 end; … … 396 392 Y := CY; 397 393 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then 398 if Cells[CY, CX].Terrain <> ttVoid then begin394 if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin 399 395 Frame := Rect(Trunc(X * CellSize.X - HexSize.X / 2), 400 396 Trunc(Y * CellSize.Y - HexSize.Y / 2), … … 402 398 Trunc(Y * CellSize.Y + HexSize.Y / 2)); 403 399 if PtInRect(Frame, Pos) then begin 404 Result := Cells[CY, CX];400 Result := TCell(Cells[CY * FSize.X + CX]); 405 401 Exit; 406 402 end; … … 434 430 for X := -1 to 1 do 435 431 if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and 436 IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin432 IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin 437 433 SetLength(Result, Length(Result) + 1); 438 Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];434 Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]); 439 435 end; 440 436 end; … … 449 445 for Y := 0 to Size.Y - 1 do 450 446 for X := 0 to Size.X - 1 do 451 Result[Y * Size.X + X] := Cells[Y, X];447 Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]); 452 448 end; 453 449 … … 476 472 begin 477 473 with Canvas do begin 478 if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY ,CX])) then begin474 if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY * FSize.X + CX])) then begin 479 475 Pen.Color := clYellow; 480 476 Pen.Style := psSolid; 481 477 Pen.Width := 1; 482 478 end else 483 if TCell(Cells[CY ,CX]).Terrain = ttCity then begin479 if TCell(Cells[CY * FSize.X + CX]).Terrain = ttCity then begin 484 480 // Cannot set clear border as it will display shifted on gtk2 485 481 //Pen.Style := psClear; … … 514 510 Y := CY; 515 511 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then begin 516 Cell := Cells[CY, CX];512 Cell := TCell(Cells[CY * FSize.X + CX]); 517 513 if Cell.Terrain <> ttVoid then begin 518 if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY ,CX])) then Brush.Color := clGreen519 else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY ,CX])) then Brush.Color := clPurple514 if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clGreen 515 else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clPurple 520 516 else Brush.Color := Cell.GetColor; 521 517 Pen.Color := clBlack; … … 686 682 687 683 procedure TMap.Generate; 688 begin 684 var 685 X, Y: Integer; 686 I: Integer; 687 NewCell: TCell; 688 NeighCells: TCellArray; 689 begin 690 inherited; 691 // Free previous 692 Cells.Count := 0; 693 // Allocate and init new 694 Cells.Count := FSize.Y * FSize.X; 695 for Y := 0 to FSize.Y - 1 do 696 for X := 0 to FSize.X - 1 do begin 697 NewCell := TCell.Create; 698 NewCell.Pos := Point(X, Y); 699 Cells[Y * FSize.X + X] := NewCell; 700 end; 689 701 end; 690 702 … … 693 705 MaxPower := 99; 694 706 DefaultCellSize := Point(62, 62); 707 Cells := TObjectList.create; 695 708 end; 696 709 … … 698 711 begin 699 712 Size := Point(0, 0); 713 FreeAndNil(Cells); 700 714 inherited Destroy; 701 715 end; … … 1519 1533 for X := -1 to 1 do 1520 1534 if IsValidIndex(Point(Cell.Pos.X + X, Cell.Pos.Y + Y)) and 1521 IsCellsNeighbor2(Cell, Cells[Cell.Pos.Y + Y, Cell.Pos.X + X]) then begin1535 IsCellsNeighbor2(Cell, TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)])) then begin 1522 1536 SetLength(Result, Length(Result) + 1); 1523 Result[Length(Result) - 1] := Cells[Cell.Pos.Y + Y, Cell.Pos.X + X];1537 Result[Length(Result) - 1] := TCell(Cells[(Cell.Pos.Y + Y) * FSize.X + (Cell.Pos.X + X)]); 1524 1538 end; 1525 1539 end; … … 1547 1561 end; 1548 1562 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then 1549 if Cells[CY, CX].Terrain <> ttVoid then begin1563 if TCell(Cells[CY * FSize.X + CX]).Terrain <> ttVoid then begin 1550 1564 Points := GetHexagonPolygon(Point(Trunc(X * CellSize.X), 1551 1565 Trunc(Y * CellSize.Y)), 1552 1566 Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 1553 1567 if PtInPoly(Points, Pos) then begin 1554 Result := Cells[CY, CX];1568 Result := TCell(Cells[CY * FSize.X + CX]); 1555 1569 Exit; 1556 1570 end; … … 1599 1613 begin 1600 1614 with Canvas do begin 1601 if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY ,CX])) then begin1615 if Assigned(View.FocusedCell) and (View.FocusedCell = TCell(Cells[CY * FSize.X + CX])) then begin 1602 1616 Pen.Color := clYellow; 1603 1617 Pen.Style := psSolid; 1604 1618 Pen.Width := 1; 1605 1619 end else 1606 if TCell(Cells[CY ,CX]).Terrain = ttCity then begin1620 if TCell(Cells[CY * FSize.X + CX]).Terrain = ttCity then begin 1607 1621 // Cannot set clear border as it will display shifted on gtk2 1608 1622 //Pen.Style := psClear; … … 1643 1657 end; 1644 1658 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then begin 1645 Cell := Cells[CY, CX];1659 Cell := TCell(Cells[CY * FSize.X + CX]); 1646 1660 if Cell.Terrain <> ttVoid then begin 1647 if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY ,CX])) then Brush.Color := clGreen1648 else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY ,CX])) then Brush.Color := clPurple1661 if Assigned(SelectedCell) and (SelectedCell = TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clGreen 1662 else if Assigned(SelectedCell) and IsCellsNeighbor(SelectedCell, TCell(Cells[CY * FSize.X + CX])) then Brush.Color := clPurple 1649 1663 else Brush.Color := Cell.GetColor; 1650 1664 Pen.Color := clBlack; … … 1684 1698 inherited; 1685 1699 // Free previous 1686 for Y := 0 to Length(Cells) - 1 do 1687 for X := 0 to Length(Cells[Y]) - 1 do begin 1688 TCell(Cells[Y, X]).Destroy; 1689 end; 1700 Cells.Count := 0; 1690 1701 // Allocate and init new 1691 SetLength(Cells, FSize.Y, FSize.X);1702 Cells.Count := FSize.Y * FSize.X; 1692 1703 for Y := 0 to FSize.Y - 1 do 1693 1704 for X := 0 to FSize.X - 1 do begin 1694 1705 NewCell := TCell.Create; 1695 1706 NewCell.Pos := Point(X, Y); 1696 Cells[Y ,X] := NewCell;1707 Cells[Y * FSize.X + X] := NewCell; 1697 1708 end; 1698 1709 // Generate neightbours 1699 1710 for Y := 0 to FSize.Y - 1 do 1700 1711 for X := 0 to FSize.X - 1 do begin 1701 NeighCells := GetCellNeighbors2( Cells[Y, X]);1712 NeighCells := GetCellNeighbors2(TCell(Cells[Y * FSize.X + X])); 1702 1713 for I := 0 to Length(NeighCells) - 1 do 1703 Cells[Y, X].Neighbors.Add(NeighCells[I]);1714 TCell(Cells[Y * FSize.X + X]).Neighbors.Add(NeighCells[I]); 1704 1715 end; 1705 1716 end; … … 1725 1736 for Y := 0 to Size.Y - 1 do 1726 1737 for X := 0 to Size.X - 1 do 1727 Result[Y * Size.X + X] := Cells[Y, X];1738 Result[Y * Size.X + X] := TCell(Cells[Y * FSize.X + X]); 1728 1739 end; 1729 1740
Note:
See TracChangeset
for help on using the changeset viewer.