Ejemplo n.º 1
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.)}})
	}
}
Ejemplo n.º 2
0
// Puts the passed entity in an empty position as close to pos as possible.
// TODO: Current implementation doesn't try very hard at closeness ;)
func (w *World) putInEmptyPos(ent *EntityDesc, pos *s3dm.V3) {
	old_pos := pos.Copy()
	for {
		if _, ok := w.ents[hashV3(pos)]; !ok {
			break // Empty, proceed
		}
		inc := &s3dm.V3{1, 1, 0}
		pos = pos.Add(inc)
	}
	w.setPos(ent, pos, nil)
	if !pos.Equals(old_pos) {
		// Update with new pos
		Send(w, ent.Chan, MsgSetState{Position{pos}})
	}
}