File size: 4,014 Bytes
05c9ac2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Unity.MLAgents;
using UnityEngine.Serialization;
public class GridArea : MonoBehaviour
{
[HideInInspector]
public List<GameObject> actorObjs;
[HideInInspector]
public int[] players;
public GameObject trueAgent;
Camera m_AgentCam;
[FormerlySerializedAs("PlusPref")] public GameObject GreenPlusPrefab;
[FormerlySerializedAs("ExPref")] public GameObject RedExPrefab;
GameObject[] m_Objects;
public int numberOfPlus = 1;
public int numberOfEx = 1;
GameObject m_Plane;
GameObject m_Sn;
GameObject m_Ss;
GameObject m_Se;
GameObject m_Sw;
Vector3 m_InitialPosition;
EnvironmentParameters m_ResetParams;
public void Start()
{
m_ResetParams = Academy.Instance.EnvironmentParameters;
m_Objects = new[] { GreenPlusPrefab, RedExPrefab };
m_AgentCam = transform.Find("agentCam").GetComponent<Camera>();
actorObjs = new List<GameObject>();
var sceneTransform = transform.Find("scene");
m_Plane = sceneTransform.Find("Plane").gameObject;
m_Sn = sceneTransform.Find("sN").gameObject;
m_Ss = sceneTransform.Find("sS").gameObject;
m_Sw = sceneTransform.Find("sW").gameObject;
m_Se = sceneTransform.Find("sE").gameObject;
m_InitialPosition = transform.position;
}
void SetEnvironment()
{
transform.position = m_InitialPosition * (m_ResetParams.GetWithDefault("gridSize", 5f) + 1);
var playersList = new List<int>();
for (var i = 0; i < (int)m_ResetParams.GetWithDefault("numPlusGoals", numberOfPlus); i++)
{
playersList.Add(0);
}
for (var i = 0; i < (int)m_ResetParams.GetWithDefault("numExGoals", numberOfEx); i++)
{
playersList.Add(1);
}
players = playersList.ToArray();
var gridSize = (int)m_ResetParams.GetWithDefault("gridSize", 5f);
m_Plane.transform.localScale = new Vector3(gridSize / 10.0f, 1f, gridSize / 10.0f);
m_Plane.transform.localPosition = new Vector3((gridSize - 1) / 2f, -0.5f, (gridSize - 1) / 2f);
m_Sn.transform.localScale = new Vector3(1, 1, gridSize + 2);
m_Ss.transform.localScale = new Vector3(1, 1, gridSize + 2);
m_Sn.transform.localPosition = new Vector3((gridSize - 1) / 2f, 0.0f, gridSize);
m_Ss.transform.localPosition = new Vector3((gridSize - 1) / 2f, 0.0f, -1);
m_Se.transform.localScale = new Vector3(1, 1, gridSize + 2);
m_Sw.transform.localScale = new Vector3(1, 1, gridSize + 2);
m_Se.transform.localPosition = new Vector3(gridSize, 0.0f, (gridSize - 1) / 2f);
m_Sw.transform.localPosition = new Vector3(-1, 0.0f, (gridSize - 1) / 2f);
m_AgentCam.orthographicSize = (gridSize) / 2f;
m_AgentCam.transform.localPosition = new Vector3((gridSize - 1) / 2f, gridSize + 1f, (gridSize - 1) / 2f);
}
public void AreaReset()
{
var gridSize = (int)m_ResetParams.GetWithDefault("gridSize", 5f);
foreach (var actor in actorObjs)
{
DestroyImmediate(actor);
}
SetEnvironment();
actorObjs.Clear();
var numbers = new HashSet<int>();
while (numbers.Count < players.Length + 1)
{
numbers.Add(Random.Range(0, gridSize * gridSize));
}
var numbersA = numbers.ToArray();
for (var i = 0; i < players.Length; i++)
{
var x = (numbersA[i]) / gridSize;
var y = (numbersA[i]) % gridSize;
var actorObj = Instantiate(m_Objects[players[i]], transform);
actorObj.transform.localPosition = new Vector3(x, -0.25f, y);
actorObjs.Add(actorObj);
}
var xA = (numbersA[players.Length]) / gridSize;
var yA = (numbersA[players.Length]) % gridSize;
trueAgent.transform.localPosition = new Vector3(xA, -0.25f, yA);
}
}
|