Skip to content

Commit

Permalink
refactor: connector render function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Dec 24, 2022
1 parent 9dc4360 commit efd211f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions internal/board/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ type Connector struct {
to *Block
}

func checkCollision(
matrix *graphics.Matrix,
cursor utils.Vector,
from *Block,
to *Block,
) (bool, error) {
return matrix.RayCastVertical(
cursor,
map[string]func(string) bool{
// if an arrow or a blockChar is already present, then that is a hit.
cellType: func(value string) bool {
return utils.InArray(value, []string{blockChar, arrow})
},
// if there is a line, belonging to another connector which claimed ownership, then hit.
noCrossOwnership: func(value string) bool {
return value != from.Id
},
},
to.Position.Y-from.Position.Y,
)
}

// Render This function is far less clear if factored out.
//
//nolint:gocyclo
func (c *Connector) Render(matrix *graphics.Matrix) error {
reverseX := c.to.Position.X <= c.from.Position.X
reverseY := c.to.Position.Y < c.from.Position.Y
Expand Down Expand Up @@ -46,29 +71,17 @@ func (c *Connector) Render(matrix *graphics.Matrix) error {

// 3. move horizontally until no vertical collision is expected.
for {
collides, err := matrix.RayCastVertical(
cur,
map[string]func(string) bool{
// if an arrow or a blockChar is already present, then that is a hit.
cellType: func(value string) bool {
return utils.InArray(value, []string{blockChar, arrow})
},
// if there is a line, belonging to another connector which claimed ownership, then hit.
noCrossOwnership: func(value string) bool {
return value != c.from.Id
},
},
c.to.Position.Y-c.from.Position.Y,
)
collides, err := checkCollision(matrix, cur, c.from, c.to)
if err != nil {
return err
} else if !collides {
break
}

cur = tracer.MoveHorizontal(!reverseX)
cell := matrix.Cell(cur)
cell = matrix.Cell(cur)
if cell == nil && cur.X >= matrix.W() {
// While moving horizontally, the end was reached, expanding...
matrix.ExpandRight(1)
cell = matrix.Cell(cur)
}
Expand All @@ -78,13 +91,13 @@ func (c *Connector) Render(matrix *graphics.Matrix) error {
cell.Tag(noCrossOwnership, c.from.Id)
}

// 3. displacing vertically until aligned...
// 4. displacing vertically until aligned...
for cur.Y != c.to.Position.Y && cur.Y >= 0 && cur.Y < matrix.H() {
cur = tracer.MoveVertical(cur.Y > c.to.Position.Y)
matrix.Cell(cur).Tag(noCrossOwnership, c.from.Id)
}

// 4. moving horizontally until meeting target node...
// 5. moving horizontally until meeting target node...
for cur.X != c.to.Position.X && cur.X >= 0 && cur.X < matrix.W() {
next := matrix.Cell(utils.Vec(cur.X+utils.Bool2Int(!reverseX), cur.Y))
if next != nil && (next.Is(cellType, blockChar) || next.Is(cellType, blockSpace)) {
Expand All @@ -97,7 +110,7 @@ func (c *Connector) Render(matrix *graphics.Matrix) error {
return err
}

// 5. placing arrow in target node...
// 6. placing arrow in target node...
cell = matrix.Cell(cur)
if cell != nil {
cell.PlaceArrow(reverseX)
Expand Down

0 comments on commit efd211f

Please sign in to comment.