Ejemplo n.º 1
0
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)

}
Ejemplo n.º 2
0
func (t *ConnectCommandProcessor) Run(g *state.GameState, c cmd.GameCommand) {
	command := c.(*cmd.ConnectCommand)

	// if the user already has a vehicle, ignore
	vehicle := g.GetVehicle(command.UserId)
	if vehicle != nil {
		return
	}

	// For now, randomly join team 0 or 1
	teamNum := len(g.Vehicles) % 2

	size := state.NewSized(t.Physics.VehicleWidth, t.Physics.VehicleHeight)
	pt := t.Physics.findSpace(size, *g)

	newVehicle := state.Vehicle{
		Point:             pt,
		Sized:             size,
		Velocity:          0.0,
		Angle:             0,
		TeamId:            teamNum,
		MaxHealth:         t.Physics.VehicleHealth,
		CurrentHealth:     t.Physics.VehicleHealth,
		Owner:             command.UserId,
		Mass:              10,
		IsAlive:           true,
		ActivePowerup:     NO_POWERUP,
		StoredPowerup:     NO_POWERUP,
		OverRideSpeedTill: time.Now().Add(-5 * time.Second)}
	g.Vehicles = append(g.Vehicles, &newVehicle)

}
Ejemplo n.º 3
0
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)
}
Ejemplo n.º 4
0
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)
}
Ejemplo n.º 5
0
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)

	})
}
Ejemplo n.º 6
0
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)
	})
}
Ejemplo n.º 7
0
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
}