Changeset 248 for trunk/UMapType.pas


Ignore:
Timestamp:
Sep 22, 2018, 2:35:46 PM (6 years ago)
Author:
chronos
Message:
  • Fixed: Drawing Voronoi type map in cyclic mode.
  • Fixed: Clear player cells before map cells regeneration.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMapType.pas

    r247 r248  
    421421  MP: TPoint;
    422422  LinkLine: TLine;
     423  P: TPolygon;
     424  AreaSize: TPoint;
    423425const
    424426  CellGapWidth = 4;
     
    441443  end;
    442444
    443   // Compute polygon by catting out map area by all other cells
    444   for Cell in Cells do begin
    445     Cell.Polygon := TPolygon.Create(TRect.Create(TPoint.Create(0, 0),
    446       TPoint.Create(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y)));
    447     for Cell2 in Cells do
    448     if Cell2 <> Cell then begin
    449       LinkLine := TLine.Create(Cell.PosPx, Cell2.PosPx);
    450       LinkLine.Distance := LinkLine.Distance - CellGapWidth;
    451       MP := LinkLine.GetMiddle;
    452       // Create half plane vector
    453       L1 := TLine.Create(MP, TPoint.Create(MP.X + LinkLine.GetSize.X,
    454         MP.Y + LinkLine.GetSize.Y));
    455 
    456       Cell.Polygon.CutLine(L1, Cell.PosPx);
     445  AreaSize := TPoint.Create(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y);
     446
     447  // Compute polygon by cutting out map area by all other cells
     448  if Cyclic then begin
     449    for Cell in Cells do begin
     450      Cell.Polygon := TPolygon.Create(TRect.Create(
     451        TPoint.Create(-AreaSize.X, -AreaSize.Y),
     452        TPoint.Create(2 * AreaSize.X, 2 * AreaSize.Y)));
     453      for Y := -1 to 1 do
     454      for X := -1 to 1 do
     455      for Cell2 in Cells do
     456      if Cell2 <> Cell then begin
     457        LinkLine := TLine.Create(Cell.PosPx, Cell2.PosPx +
     458          TPoint.Create(X * AreaSize.X, Y * AreaSize.Y));
     459        LinkLine.Distance := LinkLine.Distance - CellGapWidth;
     460        MP := LinkLine.GetMiddle;
     461        // Create half plane vector
     462        L1 := TLine.Create(MP, TPoint.Create(MP.X + LinkLine.GetSize.X,
     463          MP.Y + LinkLine.GetSize.Y));
     464
     465        Cell.Polygon.CutLine(L1, Cell.PosPx);
     466      end;
     467    end;
     468  end else begin
     469    for Cell in Cells do begin
     470      Cell.Polygon := TPolygon.Create(TRect.Create(TPoint.Create(0, 0),
     471        AreaSize));
     472      for Cell2 in Cells do
     473      if Cell2 <> Cell then begin
     474        LinkLine := TLine.Create(Cell.PosPx, Cell2.PosPx);
     475        LinkLine.Distance := LinkLine.Distance - CellGapWidth;
     476        MP := LinkLine.GetMiddle;
     477        // Create half plane vector
     478        L1 := TLine.Create(MP, TPoint.Create(MP.X + LinkLine.GetSize.X,
     479          MP.Y + LinkLine.GetSize.Y));
     480
     481        Cell.Polygon.CutLine(L1, Cell.PosPx);
     482      end;
    457483    end;
    458484  end;
    459485
    460486  // Link all cells with neighboring polygon edges
    461   for I := 0 to Cells.Count - 1 do begin
    462     for J := I + 1 to Cells.Count - 1 do begin
    463       if Cells[I].Polygon.EdgeDistance(Cells[J].Polygon) < 2 * CellGapWidth then
    464         Cells[I].ConnectTo(Cells[J]);
     487  if Cyclic then begin
     488    for I := 0 to Cells.Count - 1 do begin
     489      for Y := -1 to 1 do
     490      for X := -1 to 1 do
     491      for J := 0 to Cells.Count - 1 do begin
     492        P := Cells[J].Polygon;
     493        P.Move(TPoint.Create(X * AreaSize.X, Y * AreaSize.Y));
     494        if Cells[I].Polygon.EdgeDistance(P) < 2 * CellGapWidth then
     495          Cells[I].ConnectTo(Cells[J]);
     496      end;
     497    end;
     498  end else begin
     499    for I := 0 to Cells.Count - 1 do begin
     500      for J := I + 1 to Cells.Count - 1 do begin
     501        if Cells[I].Polygon.EdgeDistance(Cells[J].Polygon) < 2 * CellGapWidth then
     502          Cells[I].ConnectTo(Cells[J]);
     503      end;
    465504    end;
    466505  end;
     
    476515function TVoronoiMap.CalculatePixelRect: TRect;
    477516begin
    478   Result := inherited CalculatePixelRect;
    479   Result.P2 := Result.P2 + TPoint.Create(
    480     Trunc(0.02 * DefaultCellSize.X),
    481     Trunc(0.02 * DefaultCellSize.Y)
    482   );
     517  Result.P1 := TPoint.Create(0, 0);
     518  Result.P2 := TPoint.Create(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y);
    483519end;
    484520
Note: See TracChangeset for help on using the changeset viewer.