void newEnnemies(int nb, Transform player) { ennemies = new Transform[nb]; int spawn = 1; for (int i = 0; i < nb; i++) { ennemies[i] = Instantiate(tankPrefab, spawnPositions[spawn].position, spawnPositions[spawn].rotation) as Transform; TankAI ai = ennemies[i].gameObject.AddComponent <TankAI>(); ai.setPlayer(player, ennemies); setupTank(ennemies[i], 0.9f, i + 3); spawn++; if (spawn >= spawnPositions.Length) { spawn = 1; } } }
private void OnDisable() { if (m_CurrentTarget == null) { return; } switch (m_CurrentType) { case eMapPointType.FISH: FishAI fish = m_CurrentTarget.GetComponent <FishAI>(); fish.OnSpawn -= ShowPoint; fish.OnDeath -= HidePoint; break; case eMapPointType.PATROL: PatrolAI patrol = m_CurrentTarget.GetComponent <PatrolAI>(); patrol.OnSpawn -= ShowPoint; patrol.OnDeath -= HidePoint; break; case eMapPointType.FISHVARIANT: FishVariantAI fishVariant = m_CurrentTarget.GetComponent <FishVariantAI>(); fishVariant.OnSpawn -= ShowPoint; fishVariant.OnDeath -= HidePoint; break; case eMapPointType.TANK: TankAI tank = m_CurrentTarget.GetComponent <TankAI>(); tank.OnSpawn -= ShowPoint; tank.OnDeath -= HidePoint; break; //case eMapPointType.PLAYER: // m_CurrentPlayer.OnSpawnBegin -= ShowPoint; // m_CurrentPlayer.OnDeathBegin -= HidePoint; // break; } UIPanelRadar.Instance.UpdatePoint -= UpdatePosition; }
public void Setup() { m_Movement = m_Instance.GetComponent <TankMovement>(); m_Shooting = m_Instance.GetComponent <TankShooting>(); m_Health = m_Instance.GetComponent <TankHealth>(); m_Radar = m_Instance.GetComponent <TankRadar>(); m_TankAI = m_Instance.GetComponent <TankAI>(); m_CanvasGameObject = m_Instance.GetComponentInChildren <Canvas>().gameObject; m_Movement.m_PlayerNumber = m_PlayerNumber; m_Shooting.m_PlayerNumber = m_PlayerNumber; m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>"; MeshRenderer[] renderers = m_Instance.GetComponentsInChildren <MeshRenderer>(); for (int i = 0; i < renderers.Length; i++) { renderers[i].material.color = m_PlayerColor; } }
public void Init(GameObject target, eMapPointType type) { m_CurrentTarget = target; m_CurrentType = type; m_Image = this.GetComponent <Image>(); m_Color = m_Image.color; FindRadarCenter(); CalculatePosition(); switch (m_CurrentType) { case eMapPointType.FISH: FishAI fish = target.GetComponent <FishAI>(); fish.OnSpawn += ShowPoint; fish.OnDeath += HidePoint; break; case eMapPointType.PATROL: PatrolAI patrol = target.GetComponent <PatrolAI>(); patrol.OnSpawn += ShowPoint; patrol.OnDeath += HidePoint; break; case eMapPointType.FISHVARIANT: FishVariantAI fishVariant = target.GetComponent <FishVariantAI>(); fishVariant.OnSpawn += ShowPoint; fishVariant.OnDeath += HidePoint; break; case eMapPointType.TANK: TankAI tank = target.GetComponent <TankAI>(); tank.OnSpawn += ShowPoint; tank.OnDeath += HidePoint; break; } UIPanelRadar.Instance.UpdatePoint += UpdatePosition; }
void createLevel(float difficulty) { paramsDiff currentDiff; currentDiff.nbEnnemies = Mathf.RoundToInt(Mathf.Lerp((float)easyDiff.nbEnnemies, (float)hardDiff.nbEnnemies, difficulty)); currentDiff.speedTurn = Mathf.Lerp(easyDiff.speedTurn, hardDiff.speedTurn, difficulty); currentDiff.speedMove = Mathf.Lerp(easyDiff.speedMove, hardDiff.speedMove, difficulty); currentDiff.timeBetweenShot = Mathf.Lerp(easyDiff.timeBetweenShot, hardDiff.timeBetweenShot, difficulty); currentDiff.precision = Mathf.Lerp(easyDiff.precision, hardDiff.precision, difficulty); spawnEverybody(currentDiff.nbEnnemies); for (int i = 0; i < ennemies.Length; i++) { TankAI ai = ennemies[i].GetComponent <TankAI>(); ai.timeBetweenShot = currentDiff.timeBetweenShot; ai.speedTurn = currentDiff.speedTurn; ai.speedMove = currentDiff.speedMove; ai.precision = currentDiff.precision; } waitingForNewLevel = false; updateScoreLabel(); }
private void Awake() { m_TankAI = GetComponent <TankAI>(); m_LastShootAction = Time.realtimeSinceStartup; }
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { TankAI tankAI = animator.gameObject.GetComponent <TankAI>(); tankAI.SetNextPoint(); }
public void SetAI(TankAI ai) { tankAI = ai; }
// Start is called before the first frame update void Start() { tankai = GetComponent<TankAI>(); }
void Start() { tankAI = gameObject.GetComponentInParent<TankAI>(); rb = gameObject.GetComponentInParent<Rigidbody2D> (); }
// Use this for initialization void Start() { tankAI = gameObject.GetComponentInParent<TankAI>(); }
private void Awake() { m_Tank = GetComponent <TankAI>(); }
void Init() { if (Generation == 0) { for (int tki = 0; tki < TankAmount; tki++) { GameObject tk = Instantiate(TankPrefab, new Vector3(Random.Range(-field, field), 0, Random.Range(-field, field)), Quaternion.Euler(new Vector3(0, Random.Range(-180, 180), 0))); TankAI tkai = tk.GetComponent <TankAI>(); for (int i = 0; i < MyMath.InNode; i++) { for (int j = 0; j < MyMath.HideNode; j++) { tkai.InMatrix.matrix[i, j] = Random.Range(-1f, 1f); } } for (int i = 0; i < MyMath.HideNode; i++) { tkai.InMatrix.Bias[i] = Random.Range(-1f, 1f); } for (int i = 0; i < MyMath.HideNode; i++) { for (int j = 0; j < MyMath.OutNode; j++) { tkai.OutMatrix.matrix[i, j] = Random.Range(-1f, 1f); } } for (int i = 0; i < MyMath.OutNode; i++) { tkai.Bias[i] = Random.Range(-1f, 1f); } } AIdatas = new List <AIData>(); Generation++; } else { GameObject[] lst = GameObject.FindGameObjectsWithTag("Tank"); foreach (GameObject obj in lst) { obj.GetComponent <TankAI>().Die(); } AIdatas.Sort(new AIData.AiDataComparer()); /*防止死循环*/ //int min = 10000; for (int i = 0; i < AIdatas.Count; i++) { if (AIdatas[i].score <= 0) { AIdatas[i].score = 1; } } /*if(min<0) * { * for (int i = 0; i < AIdatas.Count; i++) * { * AIdatas[i].score += -min; * } * }*/ List <AIData> datalist = new List <AIData>(); int tki; if (TankAmount > 30) { datalist.Add(AIdatas[0]); datalist.Add(AIdatas[0]); datalist.Add(AIdatas[0]); datalist.Add(AIdatas[1]); datalist.Add(AIdatas[1]); datalist.Add(AIdatas[1]); datalist.Add(AIdatas[2]); datalist.Add(AIdatas[2]); datalist.Add(AIdatas[3]); datalist.Add(AIdatas[3]); for (tki = 4; tki < TankAmount / 4; tki++) { datalist.Add(AIdatas[tki]); } } else if (TankAmount > 20) { datalist.Add(AIdatas[0]); datalist.Add(AIdatas[0]); datalist.Add(AIdatas[0]); datalist.Add(AIdatas[1]); datalist.Add(AIdatas[1]); for (tki = 2; tki < TankAmount / 4; tki++) { datalist.Add(AIdatas[tki]); } } else { for (tki = 0; tki < TankAmount / 4; tki++) { datalist.Add(AIdatas[tki]); } } for (; tki < TankAmount; tki++) { int rb = Random.Range(0, MyMath.OutNode); int ran = rb * MyMath.HideNode + Random.Range(0, MyMath.HideNode); int hb = Random.Range(0, MyMath.HideNode); int t1 = Random.Range(0, 200); int t2 = Random.Range(0, 200); AIData AI1, AI2; MyMath.MatrixInToHide InMatrix = new MyMath.MatrixInToHide(); MyMath.MatrixHideToOut OutMatrix = new MyMath.MatrixHideToOut(); float[] Bias = new float[MyMath.OutNode]; int wheel = 0; for (int i = 0; ; i %= TankAmount) { wheel += AIdatas[i].score; if (wheel >= t1 && i <= TankAmount) { AI1 = AIdatas[i]; break; } i++; } for (int i = 0; ; i %= TankAmount) { wheel += AIdatas[i].score; if (wheel >= t2 && i <= TankAmount) { AI2 = AIdatas[i]; break; } i++; } /* 杂交 */ /*InMatrix*/ for (int i = 0; i < MyMath.InNode; i++) { for (int j = 0; j < MyMath.HideNode; j++) { if ((i * MyMath.HideNode + j) < ran) { InMatrix.matrix[i, j] = AI1.InMatrix.matrix[i, j]; } else { InMatrix.matrix[i, j] = AI2.InMatrix.matrix[i, j]; } //突变 if (Random.Range(0, 1f) < mutationRate / 4) { InMatrix.matrix[i, j] += Random.Range(-mutationField * 16 < 1 ? -mutationField * 16 : 1, mutationField * 16 < 1 ? -mutationField * 16 : 1); } if (Random.Range(0, 1f) < mutationRate / 2) { InMatrix.matrix[i, j] += Random.Range(-mutationField * 4, mutationField * 4); } if (Random.Range(0, 1f) < mutationRate) { InMatrix.matrix[i, j] += Random.Range(-mutationField, mutationField); } if (Random.Range(0, 1f) < mutationRate * 2) { InMatrix.matrix[i, j] += Random.Range(-mutationField / 4, mutationField / 4); } } } /*OutMatrix*/ for (int i = 0; i < MyMath.OutNode; i++) { for (int j = 0; j < MyMath.HideNode; j++) { if ((i * MyMath.HideNode + j) < ran) { OutMatrix.matrix[j, i] = AI1.OutMatrix.matrix[j, i]; } else { OutMatrix.matrix[j, i] = AI2.OutMatrix.matrix[j, i]; } //突变 if (Random.Range(0, 1f) < mutationRate / 4) { OutMatrix.matrix[j, i] += Random.Range(-mutationField * 16 < 1 ? -mutationField * 16 : 1, mutationField * 16 < 1 ? -mutationField * 16 : 1); } if (Random.Range(0, 1f) < mutationRate / 2) { OutMatrix.matrix[j, i] += Random.Range(-mutationField * 4, mutationField * 4); } if (Random.Range(0, 1f) < mutationRate) { OutMatrix.matrix[j, i] += Random.Range(-mutationField, mutationField); } if (Random.Range(0, 1f) < mutationRate * 2) { OutMatrix.matrix[j, i] += Random.Range(-mutationField / 4, mutationField / 4); } } } /*Hide Bias*/ for (int i = 0; i < MyMath.HideNode; i++) { /*if (i < hb) InMatrix.Bias[i] = AI1.InMatrix.Bias[i]; * else InMatrix.Bias[i] = AI2.InMatrix.Bias[i];*/ if (i == hb) { InMatrix.Bias[i] = AI2.InMatrix.Bias[i]; } else { InMatrix.Bias[i] = AI1.InMatrix.Bias[i]; } if (Random.Range(0, 1f) < mutationRate / 2) { InMatrix.Bias[i] += Random.Range(-mutationField * 2, mutationField * 2); } if (Random.Range(0, 1f) < mutationRate) { InMatrix.Bias[i] += Random.Range(-mutationField, mutationField); } if (Random.Range(0, 1f) < mutationRate * 2) { InMatrix.Bias[i] += Random.Range(-mutationField / 4, mutationField / 4); } } /*Out Bias*/ for (int i = 0; i < MyMath.OutNode; i++) { /*if (i < rb) Bias[i] = AI1.Bias[i]; * else Bias[i] = AI2.Bias[i];*/ if (i == rb) { Bias[i] = AI2.Bias[i]; } else { Bias[i] = AI1.Bias[i]; } if (Random.Range(0, 1f) < mutationRate / 2) { Bias[i] += Random.Range(-mutationField * 2, mutationField * 2); } if (Random.Range(0, 1f) < mutationRate) { Bias[i] += Random.Range(-mutationField, mutationField); } if (Random.Range(0, 1f) < mutationRate * 2) { Bias[i] += Random.Range(-mutationField / 4, mutationField / 4); } } /* 只突变 交换1位 */ /*for (int i = 0; i < 3; i++) * { * for (int j = 0; j < 9; j++) * { * if (i == rb) * { * m49.matrix[i, j] = AI2.InMatrix.matrix[i, j]; * m93.matrix[j, i] = AI2.OutMatrix.matrix[j, i]; * * } * else * { * m49.matrix[i, j] = AI1.InMatrix.matrix[i, j]; * m93.matrix[j, i] = AI1.OutMatrix.matrix[j, i]; * } * if (Random.Range(0, 1f) < mutationRate) * { * m49.matrix[i, j] += Random.Range(-mutationField, mutationField); * } * if (Random.Range(0, 1f) < mutationRate) * { * m93.matrix[j, i] += Random.Range(-mutationField, mutationField); * } * } * if (i == rb) Bias[i] = AI2.Bias[i]; * else Bias[i] = AI1.Bias[i]; * if (Random.Range(0, 1f) < mutationRate) * { * Bias[i] += Random.Range(-mutationField, mutationField); * } * }*/ datalist.Add(new AIData(0, InMatrix, OutMatrix, Bias)); } for (tki = 0; tki < TankAmount; tki++) { GameObject tk = Instantiate(TankPrefab, new Vector3(Random.Range(-field, field), 0, Random.Range(-field, field)), Quaternion.Euler(new Vector3(0, Random.Range(-180, 180), 0))); TankAI tkai = tk.GetComponent <TankAI>(); { tkai.InMatrix = datalist[tki].InMatrix; tkai.OutMatrix = datalist[tki].OutMatrix; tkai.Bias = datalist[tki].Bias; } } AIdatas.Clear(); Generation++; } }