Example #1
0
func (a *avatar) control(ctrl <-chan Msg, input <-chan *protocol.Message) {
	for {
		select {
		case msg := <-ctrl:
			switch m := msg.(type) {
			// TODO: Handle MsgTick?
			case MsgQuit:
				a.player.Chan <- MsgSetState{Remove{true}}
				a.svc.Game <- MsgEntityRemoved{&a.player}
				return
			}
		case msg := <-input:
			switch *msg.Type {
			case protocol.Message_Type(protocol.Message_MOVE):
				dir := msg.Move.Direction
				vec := s3dm.NewV3(*dir.X, *dir.Y, *dir.Z)
				a.player.Chan <- MsgRunAction{Move{vec}, false}
			default:
				log.Println("Client sent unhandled message, ignoring:",
					protocol.Message_Type_name[int32(*msg.Type)])

			}
		}
	}
}
Example #2
0
func (w *World) spawnSpiders(pos *s3dm.V3) {
	// Represents the gradually increasing difficulty as the player gets
	// farther from the center of Spider Forest. Every time the player
	// passes this threshold, another spider will definitely spawn upon
	// player movement. So if the player is 173 units from the center,
	// 173/50=3 spiders will spawn with a 23/50 chance to make it 4.
	// The lower this number, the more difficult the game will be.
	const LEVEL_DIST = 100.
	// Minimum and maximum distance from the player to spawn spiders
	const MIN_DIST = 40.
	const MAX_DIST = 60.

	// Find how many spiders will definitely spawn
	dist := pos.Length()
	count := 0
	for dist >= LEVEL_DIST {
		dist -= LEVEL_DIST
		count++
	}
	// Random chance for a spider to spawn
	if rand.Float64() <= dist/LEVEL_DIST {
		count++
	}

	// Spawn spiders at a random point in a ring around the player
	// between MIN_DIST and MAX_DIST.
	reply := make(chan Msg)
	for i := 0; i < count; i++ {
		radius := rand.Float64()*(MAX_DIST-MIN_DIST) + MIN_DIST
		angle := rand.Float64() * 2. * math.Pi
		x := pos.X + radius*math.Cos(angle)
		y := pos.Y + radius*math.Sin(angle)
		// Create the spider entity and position it
		Send(w, w.svc.Game, game.MsgSpawnEntity{InitSpider, reply})
		spider := Recv(w, reply).(*EntityDesc)
		Send(w, spider.Chan, MsgSetState{Position{s3dm.NewV3(x, y, 0.)}})
	}
}