func TestFindVehicle(t *testing.T) { physics := processor.DefaultPhysics() Convey("Find Vehicle", t, func() { a := physics.NewGameState() So(a.Vehicles, ShouldBeEmpty) a.Vehicles = make([]*state.Vehicle, 4) a.Vehicles[0] = &(state.Vehicle{ Point: state.NewPoint(1, 0), Owner: "me"}) a.Vehicles[1] = &(state.Vehicle{ Point: state.NewPoint(2, 0), Owner: "you"}) a.Vehicles[2] = &(state.Vehicle{ Point: state.NewPoint(3, 0), Owner: "austin"}) a.Vehicles[3] = &(state.Vehicle{ Point: state.NewPoint(4, 0), Owner: "abc"}) So(a.GetVehicle("me").X, ShouldEqual, 1) So(a.GetVehicle("you").X, ShouldEqual, 2) So(a.GetVehicle("austin").X, ShouldEqual, 3) So(a.GetVehicle("abc").X, ShouldEqual, 4) So(a.GetVehicle("gz"), ShouldEqual, nil) }) }
func (t *FireCommandProcessor) Run(g *state.GameState, c cmd.GameCommand) { if t.lastFired == nil { t.lastFired = make(map[string]time.Time) } command := c.(*cmd.FireCommand) vehicle := g.GetVehicle(command.UserId) if vehicle == nil || !vehicle.IsAlive { return } last := t.lastFired[vehicle.Owner] diff := time.Now().Sub(last) if diff < t.Physics.BulletDelay { return } t.lastFired[vehicle.Owner] = time.Now() b := state.Bullet{ Point: state.NewPoint(vehicle.X, vehicle.Y), Sized: state.NewSized(t.Physics.BulletWidth, t.Physics.BulletWidth), Velocity: t.Physics.BulletVelocity, Angle: vehicle.Angle, OwnerId: vehicle.Owner, } g.Bullets = append(g.Bullets, &b) }
func (t *PowerupCommandProcessor) placeWell(v *state.Vehicle, g *state.GameState) { v.StoredPowerup = NO_POWERUP r := state.GravityWell{ Point: state.NewPoint(v.X, v.Y), Sized: state.NewSized(10, 10), Owner: v.Owner, Expires: time.Now().Add(5 * time.Second), } g.GravityWells = append(g.GravityWells, &r) }
func TestStateCopy(t *testing.T) { physics := processor.DefaultPhysics() Convey("Copy State", t, func() { a := physics.NewGameState() a.Rockets = append(a.Rockets, &state.Rocket{}) a.Vehicles = make([]*state.Vehicle, 4) a.Vehicles[0] = &(state.Vehicle{ Point: state.NewPoint(1, 0), Owner: "me", IsAlive: true}) a.Vehicles[1] = &(state.Vehicle{ Point: state.NewPoint(1, 0), Owner: "you", IsAlive: true}) a.Vehicles[2] = &(state.Vehicle{ Point: state.NewPoint(1, 0), Owner: "austin", IsAlive: true}) a.Vehicles[3] = &(state.Vehicle{ Point: state.NewPoint(1, 0), Owner: "abc", IsAlive: true}) a.Bullets = make([]*state.Bullet, 4) a.Bullets[0] = &(state.Bullet{}) a.Bullets[1] = &(state.Bullet{}) a.Bullets[2] = &(state.Bullet{}) a.Bullets[3] = &(state.Bullet{}) theCopy := a.Copy() So(len(theCopy.Vehicles), ShouldEqual, len(a.Vehicles)) So(len(theCopy.Bases), ShouldEqual, len(a.Bases)) So(len(theCopy.ShieldGenerators), ShouldEqual, len(a.ShieldGenerators)) So(len(theCopy.Rockets), ShouldEqual, len(a.Rockets)) So(theCopy.GameOver, ShouldEqual, a.GameOver) }) }
func (t *PowerupCommandProcessor) fireRocket(v *state.Vehicle, g *state.GameState) { v.StoredPowerup = NO_POWERUP targetedVehicle := targetVehicle(v, g) r := state.Rocket{ Point: state.NewPoint(v.X, v.Y), Sized: state.NewSized(t.Physics.BulletWidth*3, t.Physics.BulletWidth*1.25), Target: targetedVehicle, Velocity: t.Physics.BulletVelocity * .75, } g.Rockets = append(g.Rockets, &r) }
func TestSlowVehicle(t *testing.T) { physics := DefaultPhysics() Convey("Slow to stop Vehicle", t, func() { before_x := 10.0 before_y := 10.0 before_angle := 270.0 before_speed := 1.0 vehicle := state.Vehicle{ Point: state.NewPoint(before_x, before_y), Angle: before_angle, Velocity: before_speed} physics.VehicleFrictionSlow(&vehicle, time.Second) So(vehicle.Velocity, ShouldAlmostEqual, 0, .001) }) Convey("Slow to stop Vehicle, reverse", t, func() { before_x := 10.0 before_y := 10.0 before_angle := 270.0 before_speed := -1.0 vehicle := state.Vehicle{ Point: state.NewPoint(before_x, before_y), Angle: before_angle, Velocity: before_speed} physics.VehicleFrictionSlow(&vehicle, time.Second) So(vehicle.Velocity, ShouldAlmostEqual, 0, .001) }) }
func TestVehicleBounding(t *testing.T) { physics := DefaultPhysics() Convey("Should Be in Bounds", t, func() { v := state.Vehicle{ Point: state.NewPoint(20, 30), } physics.VehicleBounding(&v) So(v.X, ShouldAlmostEqual, 20, .001) So(v.Y, ShouldAlmostEqual, 30, .001) }) Convey("Should Be reBound from negative", t, func() { v := state.Vehicle{ Point: state.NewPoint(-20, -30), } physics.VehicleBounding(&v) So(v.X, ShouldBeGreaterThan, 0) So(v.Y, ShouldBeGreaterThan, 0) }) Convey("Should Be reBound from max", t, func() { v := state.Vehicle{ Point: state.NewPoint(math.MaxFloat64, math.MaxFloat64), } physics.VehicleBounding(&v) So(v.X, ShouldBeLessThan, physics.WorldWidth) So(v.Y, ShouldBeLessThan, physics.WorldHeight) }) }
func TestBox2dVehicle(t *testing.T) { vehicle := state.Vehicle{ Point: state.NewPoint(10, 20), Sized: state.NewSized(15, 40), Angle: 3} Convey("Proper Box Vehicle", t, func() { So(vehicle.AngleDegrees(), ShouldAlmostEqual, vehicle.Angle, .001) x, y := vehicle.Position() w, h := vehicle.Size() So(x, ShouldAlmostEqual, vehicle.X, .001) So(y, ShouldAlmostEqual, vehicle.Y, .001) So(w, ShouldAlmostEqual, vehicle.Height, .001) So(h, ShouldAlmostEqual, vehicle.Width, .001) }) }
func TestMoveVehicle(t *testing.T) { physics := DefaultPhysics() Convey("Move Right", t, func() { before_x := 10.0 before_y := 10.0 before_angle := 270.0 before_speed := 1.0 vehicle := state.Vehicle{ Point: state.NewPoint(before_x, before_y), Angle: before_angle, Velocity: before_speed} physics.MoveVehicle(&vehicle, time.Second) So(vehicle.Y, ShouldAlmostEqual, before_y-1, .001) So(vehicle.X, ShouldAlmostEqual, before_x, .001) }) }
func TestCollisions(t *testing.T) { Convey("Basic No Collision", t, func() { v1 := state.Vehicle{ Point: state.NewPoint(10, 10), Sized: state.NewSized(10, 10), Angle: 0} v2 := state.Vehicle{ Point: state.NewPoint(30, 30), Sized: state.NewSized(10, 10), Angle: 0} check := collision.Collides(v1, v2) So(check, ShouldEqual, false) }) Convey("Basic Should Collide", t, func() { v1 := state.Vehicle{ Point: state.NewPoint(10, 10), Sized: state.NewSized(10, 10), Angle: 0} v2 := state.Vehicle{ Point: state.NewPoint(5, 5), Sized: state.NewSized(10, 10), Angle: 0} check := collision.Collides(v1, v2) So(check, ShouldEqual, true) }) Convey("Basic Should Collide", t, func() { v1 := state.Vehicle{ Point: state.NewPoint(10, 10), Sized: state.NewSized(10, 10), Angle: 0} v2 := state.Vehicle{ Point: state.NewPoint(7, 7), Sized: state.NewSized(10, 10), Angle: 0} check := collision.Collides(v1, v2) So(check, ShouldEqual, true) }) Convey("Basic Should Collide with Rotation", t, func() { v1 := state.Vehicle{ Point: state.NewPoint(10, 10), Sized: state.NewSized(10, 10), Angle: 90} v2 := state.Vehicle{ Point: state.NewPoint(7, 7), Sized: state.NewSized(10, 10), Angle: 0} check := collision.Collides(v1, v2) So(check, ShouldEqual, true) }) Convey("Basic Should Collide with Rotation", t, func() { v1 := state.Vehicle{ Point: state.NewPoint(10, 10), Sized: state.NewSized(10, 10), Angle: 90} v2 := state.Vehicle{ Point: state.NewPoint(7, 7), Sized: state.NewSized(10, 10), Angle: 90} check := collision.Collides(v1, v2) So(check, ShouldEqual, true) }) }
func (p *Physics) NewGameState() state.GameState { bases := []*state.Base{} b1 := state.Base{ Point: state.NewPoint(p.BaseOffset, p.WorldHeight-p.BaseOffset), Sized: state.NewSized(p.BaseWidth, p.BaseWidth), CurrentHealth: p.BaseHealth, MaxHealth: p.BaseHealth, TeamId: 0} b2 := state.Base{ Point: state.NewPoint(p.WorldWidth-p.BaseOffset, p.BaseOffset), Sized: state.NewSized(p.BaseWidth, p.BaseWidth), CurrentHealth: p.BaseHealth, MaxHealth: p.BaseHealth, TeamId: 1} bases = append(bases, &b1, &b2) shields := []*state.Shield{} s1 := state.Shield{ Point: state.NewPoint(p.WorldWidth-p.BaseOffset, p.BaseOffset), Sized: state.NewSized(p.BaseWidth*1.5, p.BaseWidth*1.5), IsEnabled: true, TeamId: 1} s2 := state.Shield{ Point: state.NewPoint(p.BaseOffset, p.WorldHeight-p.BaseOffset), Sized: state.NewSized(p.BaseWidth*1.5, p.BaseWidth*1.5), IsEnabled: true, TeamId: 0} shields = append(shields, &s1, &s2) generators := []*state.ShieldGenerator{} g1 := state.ShieldGenerator{ Point: state.NewPoint(p.WorldWidth-p.ShieldOffset, p.WorldHeight-p.BaseOffset), Sized: state.NewSized(p.ShieldWidth, p.ShieldWidth), CurrentHealth: p.ShieldGeneratorHealth, MaxHealth: p.ShieldGeneratorHealth, TeamId: 0, RespawnTime: time.Now()} g1.Shield = &s2 g2 := state.ShieldGenerator{ Point: state.NewPoint(p.ShieldOffset, p.ShieldOffset), Sized: state.NewSized(p.ShieldWidth, p.ShieldWidth), CurrentHealth: p.ShieldGeneratorHealth, MaxHealth: p.ShieldGeneratorHealth, TeamId: 1, RespawnTime: time.Now()} g2.Shield = &s1 generators = append(generators, &g1, &g2) state := state.GameState{ Val: "", Vehicles: []*state.Vehicle{}, Bases: bases, ShieldGenerators: generators, GameOver: -1, Bullets: []*state.Bullet{}, Shields: shields, Rockets: []*state.Rocket{}, PowerUps: []*state.Powerup{}, SecToRestart: -1, } return state }
func distance(p1, p2 state.Point) state.Point { return state.NewPoint(p1.X-p2.X, p1.Y-p2.Y) }