public Map Generate(Texture2D image, int width, int height, MLAcademy academy) { if (width < image.width) { Debug.LogError("The map is wider than the level"); } if (height < image.height) { Debug.LogError("The map is higher then the level"); } Map map = new Map(width, height); //Spawn Objects floor.localScale = new Vector3(width, 1, height); floor.position = new Vector3(width * 0.5f - 0.5f, 0, height * 0.5f - 0.5f);; cameraMount.position = new Vector3(width * 0.5f - 0.5f, 0, height * 0.5f - 0.5f);; Color32[] pixels = image.GetPixels32(); SpawnLevel(pixels, image.width, image.height, map, (width - image.width) / 2, (height - image.height) / 2); //Calculate Navigation map.CalculateNavMesh(); //Spawn Units SpawnUnits(pixels, image.width, image.height, academy, (width - image.width) / 2, (height - image.height) / 2); return(map); }
public void SetTeam(Brain brain, Material color, MLAcademy academy, Camera camera) { GiveBrain(brain); for (int i = 0; i < teamColors.Length; i++) { var mats = teamColors[i].renderer.materials; mats[teamColors[i].index] = color; teamColors[i].renderer.materials = mats; } this.academy = academy; observations = new List <Camera>(new Camera[] { camera }); team = academy.RegisterUnit(this); AgentReset(); }
void SpawnUnits(Color32[] map, int width, int height, MLAcademy academy, int offsetX = 0, int offsetY = 0) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { var item = map [x + y * width]; if (item.r == 0 && item.g == 0 && item.b != 0) { var position = new Vector3(x + offsetX, 0, y + offsetY); GameObject go = ObjectPool.Spawn(soldier, position, Quaternion.LookRotation(floor.position - position, Vector3.up)); Soldier s = go.GetComponent <Soldier>(); s.SetTeam(academy.teams[0].brain, playerOneColor, academy, aiCamera); } else if (item.r != 0 && item.g == 0 && item.b == 0) { var position = new Vector3(x + offsetX, 0, y + offsetY); GameObject go = ObjectPool.Spawn(soldier, position, Quaternion.LookRotation(floor.position - position, Vector3.up)); Soldier s = go.GetComponent <Soldier>(); s.SetTeam(academy.teams[1].brain, playerTwoColor, academy, aiCamera); } } } }