Example #1
0
func (g *Game) makeTurn() {
	if len(g.World.Units) < MaximumUnits {
		g.World.addRandomEnemy()
	}

	unitsTree := kdtree.New(nil)
	unitsMap := make(map[int]*Unit)

	for _, unit := range g.World.Units {
		if unit.Health > 0 {
			unitsTree = insertUnitToKdTree(unitsTree, unit)
			unitsMap[unit.Id] = unit

			if unit.Fire {
				bullet := unit.unitBullet(BulletSpeed)
				unitsTree = insertUnitToKdTree(unitsTree, bullet)
				unitsMap[bullet.Id] = bullet
			}

			if unit.Type == Enemy {
				unit.moveToNearestPlayer(g.World.Players, EnemySpeed)
			}

		}

	}

	g.World.deleteToDelUnits(unitsMap)
	g.World.removeOutBoundUnits(unitsMap)
	g.enemyCollisionWithShell(unitsMap, unitsTree, BulletSpeed)

	newUnits := make([]*Unit, 0)
	for _, unit := range unitsMap {
		unit.move(g.Step)
		newUnits = append(newUnits, unit)
	}
	g.World.Units = newUnits

}
Example #2
0
func NewCollisionDetector(MaxTimeToHit float32) *CollisionDetector {
	return &CollisionDetector{kdtree.New(nil), MaxTimeToHit}
}