Skip to content

Commit

Permalink
[game] resolve collisions when following perpendicularly
Browse files Browse the repository at this point in the history
  • Loading branch information
ghthor committed Feb 21, 2015
1 parent 40a807e commit 3ad73e2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
11 changes: 11 additions & 0 deletions game/sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ func (phase narrowPhase) resolveActorActorCollision(a, b *actor) {
pathCollision := coord.NewPathCollision(*a.pathAction, *b.pathAction)

switch pathCollision.Type() {
case coord.CT_A_INTO_B_FROM_SIDE:
if *a.pathAction == pathCollision.B {
a, b = b, a
}

if a.pathAction.End() >= b.pathAction.End() {
return
}

fallthrough

case coord.CT_A_INTO_B:
if *a.pathAction == pathCollision.A {
a.undoLastMoveAction()
Expand Down
47 changes: 47 additions & 0 deletions game/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,55 @@ func DescribeCollision(c gospec.Context) {
})
}

c.Specify("in perpendicular directions", func() {
testCases := []spec_2moving{{
spec: "allows the leader to move",
paths: []coord.PathAction{
pa(0, 10, cell(0, 0), cell(1, 0)),
pa(0, 9, cell(0, -1), cell(0, 0)),
},
expectations: func(testCase spec_2moving, index actorIndex, c gospec.Context) {
c.Assume(testCase.paths[0].Direction(), Equals, coord.East)
c.Assume(testCase.paths[1].Direction(), Equals, coord.North)
c.Expect(*index[0].pathAction, Equals, testCase.paths[0])
c.Expect(index[1].pathAction, IsNil)
},
}, {
spec: "allows both to move",
paths: []coord.PathAction{
pa(0, 10, cell(0, 0), cell(1, 0)),
pa(0, 10, cell(0, -1), cell(0, 0)),
},
expectations: func(testCase spec_2moving, index actorIndex, c gospec.Context) {
c.Assume(testCase.paths[0].Direction(), Equals, coord.East)
c.Assume(testCase.paths[1].Direction(), Equals, coord.North)
c.Expect(*index[0].pathAction, Equals, testCase.paths[0])
c.Expect(*index[1].pathAction, Equals, testCase.paths[1])
},
}, {
spec: "allows both to move",
paths: []coord.PathAction{
pa(0, 10, cell(0, 0), cell(1, 0)),
pa(0, 11, cell(0, -1), cell(0, 0)),
},
expectations: func(testCase spec_2moving, index actorIndex, c gospec.Context) {
c.Assume(testCase.paths[0].Direction(), Equals, coord.East)
c.Assume(testCase.paths[1].Direction(), Equals, coord.North)
c.Expect(*index[0].pathAction, Equals, testCase.paths[0])
c.Expect(*index[1].pathAction, Equals, testCase.paths[1])
},
}}

for _, testCase := range testCases {
c.Specify(testCase.spec, func() {
testCase.runSpec(c)
})
}
})

c.Specify("and contesting the same location", func() {
c.Specify("from the side", func() {

testCases := []spec_2moving{{
spec: "moving east loses to moving north",
paths: []coord.PathAction{
Expand Down

0 comments on commit 3ad73e2

Please sign in to comment.