Skip to content

Commit

Permalink
[game] rely on less state during a recursive solve
Browse files Browse the repository at this point in the history
  • Loading branch information
ghthor committed Feb 23, 2015
1 parent 02dfae0 commit 25599fd
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions game/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ func (phase *narrowPhase) resolveActorEntity(a *actor, with entity.Entity, colli
case actorEntity:
b := phase.actorIndex[e.Id()]

return phase.solveActorActor(&solverActorActor{
a: a, b: b,
collision: collision,
})
return phase.solveActorActor(&solverActorActor{}, a, b, collision)
}

return nil
Expand Down Expand Up @@ -284,9 +281,7 @@ func otherEntityIn(a *actor, collision quad.Collision) entity.Entity {
}

type solverActorActor struct {
visited []*actor
a, b *actor
collision quad.Collision
visited []*actor
}

func (s solverActorActor) hasVisited(actor *actor) bool {
Expand All @@ -299,9 +294,7 @@ func (s solverActorActor) hasVisited(actor *actor) bool {
return false
}

func (phase *narrowPhase) solveActorActor(solver *solverActorActor) []entity.Entity {
a, b := solver.a, solver.b
collision := solver.collision
func (phase *narrowPhase) solveActorActor(solver *solverActorActor, a, b *actor, collision quad.Collision) []entity.Entity {

// When this functions returns the
// collision will have been solved
Expand All @@ -327,7 +320,7 @@ attemptSolve:
// have a collision that when solved will
// render them motionless, thus we would become
// motionless as well.
e, err := phase.solveDependencies(solver)
e, err := phase.solveDependencies(solver, a, b, collision)

switch err {
case errCycleDetected:
Expand Down Expand Up @@ -368,7 +361,7 @@ attemptSolve:
// have a collision that when solved will
// render them motionless, thus we would become
// motionless as well.
e, err := phase.solveDependencies(solver)
e, err := phase.solveDependencies(solver, a, b, collision)

switch err {
case nil:
Expand Down Expand Up @@ -448,10 +441,7 @@ resolved:
var errNoDependencies = errors.New("no dependencies")
var errCycleDetected = errors.New("cycle detected")

func (phase *narrowPhase) solveDependencies(solver *solverActorActor) ([]entity.Entity, error) {
a, b := solver.a, solver.b
collision := solver.collision

func (phase *narrowPhase) solveDependencies(solver *solverActorActor, a, b *actor, collision quad.Collision) ([]entity.Entity, error) {
node := followGraph(a, b, collision)

// Mark what actors have been visited
Expand Down Expand Up @@ -493,11 +483,7 @@ func (phase *narrowPhase) solveDependencies(solver *solverActorActor) ([]entity.
return nil, errCycleDetected
}

solver.a = node.actor
solver.b = actor
solver.collision = c

return phase.solveActorActor(solver), nil
return phase.solveActorActor(solver, node.actor, actor, c), nil
}

}
Expand Down

0 comments on commit 25599fd

Please sign in to comment.