// 搜索视野重的坦克 public void NoTarget() { float minHp = float.MaxValue; GameObject[] targets = GameObject.FindGameObjectsWithTag("Tank"); for (int i = 0; i < targets.Length; i++) { TankBeheavior tank = targets [i].GetComponent <TankBeheavior> (); if (tank == null || targets[i] == gameObject) { continue; } // 判断是否同一阵营 if (Battle.instance.IsSameCamp(gameObject, targets[i])) { continue; } // 判断距离 Vector3 pos = transform.position; Vector3 targetPos = tank.transform.position; if (Vector3.Distance(pos, targetPos) > sightDistance) { continue; } // 判断生命值 if (minHp > tank.GetHP()) { minHp = tank.GetHP(); target = tank.gameObject; } } }
public void IncraeseKillNum(TankBeheavior tank) { for (int i = 0; i < battleTanks.Length; i++) { if (tank == battleTanks [i].tank) { battleTanks [i].numKill += 1; } } }
public void SetDead(TankBeheavior tank, bool isPlayer = false) { for (int i = 0; i < battleTanks.Length; i++) { if (tank == battleTanks [i].tank) { battleTanks [i].dead = true; if (isPlayer) { WatchBattle wb = gameObject.GetComponent <WatchBattle> (); wb.SetWatchMode(isPlayer, battleTanks [i].camp); } Debug.Log(tank.name + " Dead!"); } } }
public void StartTwoCampBattle(int n1, int n2) { n [0] = n1; n [1] = n2; // 获取出生点 Transform sp = GameObject.Find("SwopPoints").transform; Transform spCamp1 = sp.GetChild(0); Transform spCamp2 = sp.GetChild(1); // 清理战场 Clear(); battleTanks = new BattleTank[n1 + n2]; for (int i = 0; i < n1; i++) { GenerateTank(1, i, spCamp1, i); } for (int i = 0; i < n2; i++) { GenerateTank(2, i, spCamp2, n1 + i); } int index = Random.Range(0, n1 + n2); TankBeheavior tank = battleTanks [index].tank; battleTanks [index].isPlayer = true; tank.ctrlType = TankBeheavior.CtrlType.Player; tank.maxSteering = 50; CameraFollow cf = Camera.main.gameObject.GetComponent <CameraFollow> (); GameObject cameraTarget = tank.transform.GetChild(1).GetChild(1).gameObject; cf.SetTarget(cameraTarget); // 播放背景音乐 AudioClip bgm = bgMusics[Random.Range(0, bgMusics.Length)]; audio.clip = bgm; audio.loop = true; audio.Play(); // 锁定鼠标 Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; // 开始游戏 Time.timeScale = 1; battleStart = true; currentTime = countdown; }
/** * camp : 坦克所在阵营 * num : 阵营中的编号 * sp : 出生点容器 * index : 战场中的编号 **/ public void GenerateTank(int camp, int num, Transform sp, int index) { Transform tr = sp.GetChild(num); GameObject tankObj = (GameObject)Instantiate(tankPrefabs[camp - 1], tr.position, tr.rotation); TankBeheavior tank = tankObj.GetComponent <TankBeheavior> (); tank.ctrlType = TankBeheavior.CtrlType.NPC; battleTanks [index] = new BattleTank(); battleTanks [index].tank = tank; battleTanks [index].camp = camp; if (index < names.Length) { tankObj.name = names [index] + "-" + camp; battleTanks [index].name = names [index]; TextMesh tm = tank.transform.GetChild(5).GetChild(0).GetComponent <TextMesh> (); tm.text = names [index]; } }
public void BeAttacked(float damageAmount, GameObject attackTank) { if (Battle.instance.IsSameCamp(gameObject, attackTank)) { return; } if (hp > 0) { hp -= damageAmount; } if (hp <= 0) { Instantiate(explodeEffect, transform.position, transform.rotation); Battle.instance.SetDead(this.gameObject.GetComponent <TankBeheavior>(), ctrlType == CtrlType.Player); Destroy(gameObject); if (attackTank != null) { // 显示击杀提示 TankBeheavior tankCmp = attackTank.GetComponent <TankBeheavior> (); if (tankCmp != null && tankCmp.ctrlType == CtrlType.Player) { tankCmp.StartDrawKill(); } Battle.instance.DrawKillTip(attackTank.name, Battle.instance.GetCamp(attackTank), gameObject.name); Battle.instance.IncraeseKillNum(tankCmp); // 战场结算 Battle.instance.IsWin(attackTank); } } else if (hp <= maxHP / 2) { GameObject smoke = Instantiate(smokeEffect, transform.position, transform.rotation); smoke.transform.parent = gameObject.transform; } if (ai != null) { ai.OnAttacked(attackTank); } }
void OnCollisionEnter(Collision collisionInfo) { if (collisionInfo.gameObject == attackTank) { // 打到自身 return; } if (collisionInfo.gameObject.name == "Terrain") { Instantiate(dustExplode, transform.position, transform.rotation); } else { Instantiate(explode, transform.position, transform.rotation); } Destroy(gameObject); // 击中坦克 TankBeheavior tank = collisionInfo.gameObject.GetComponent <TankBeheavior>(); if (tank != null) { tank.BeAttacked(GetDemage(), attackTank); } }