private void OnTriggerEnter(Collider other) { SoldierCtrl soldierCtrl = other.gameObject.GetComponent <SoldierCtrl>(); DataMgr.Instance.SoldierDic.Remove(soldierCtrl.id); Destroy(other.gameObject); if (soldierCtrl.playerId == LocalUser.Instance.PlayerId) { string key = soldierCtrl.SoldierName; var message = new mmopb.soldierBreak_req(); message.keyName = key; ClientNet.Instance.Send(ProtoBuf.ProtoHelper.EncodeWithName(message)); } }
//出兵 private void StartSoldier() { string soldierName = SoldierName + "_" + this.Level.ToString(); //当前按建筑生产的士兵的名称,用于获取信息 Vector3 newSoldierPos = transform.position; newSoldierPos.z -= (float)1; //创建士兵(导入模型和材质贴图的预制体) GameObject newSoldier = (GameObject)Resources.Load("Prefabs/" + SoldierName); SoldierCtrl newSoldierCtrl = Instantiate(newSoldier, newSoldierPos, StartRotate).AddComponent <SoldierCtrl>(); Material material = Resources.Load("Materials/level" + Level) as Material; //Debug.Log(newSoldier.name + "****************"); GameObject child = newSoldierCtrl.gameObject.transform.Find(DataMgr.Instance.ObjChildObjDic[newSoldier.name]).gameObject; child.GetComponent <SkinnedMeshRenderer>().material = material; lock (lockObject) { DataMgr.Instance.SoldierDic.Add(DataMgr.SoldierId, newSoldierCtrl); //将士兵加入士兵字典 newSoldierCtrl.Init(DataMgr.SoldierId++, BuildID, PlayerID, camp, Level, soldierName); } }
/// <summary> /// 每1/45秒向服务器发送己方小兵位置 /// </summary> private void SendSoldierPosition() { var handle = new mmopb.sendSoldierPos_req(); var dic = new Dictionary <int, mmopb.p_SoldierPosInfo>(); foreach (var pair in DataMgr.Instance.SoldierDic) { if (pair.Value.playerId == LocalUser.Instance.PlayerId) { SoldierCtrl soldier = pair.Value; var info = new mmopb.p_SoldierPosInfo(); info.x = soldier.transform.position.x; info.y = soldier.transform.position.y; info.z = soldier.transform.position.z; info.xQuternion = soldier.transform.rotation.x; info.yQuternion = soldier.transform.rotation.y; info.zQuternion = soldier.transform.rotation.z; info.wQuternion = soldier.transform.rotation.w; dic.Add(soldier.id, info); } } handle.soldierPosInfoList = dic; ClientNet.Instance.Send(ProtoBuf.ProtoHelper.EncodeWithName(handle)); }
//行军状态 private void RunState() { //Debug.Log("+++++" + DataMgr.Instance.dicInfo[SoldierName].Speed + " " + influenceSpeed + "+++++++++"); transform.Translate(Vector3.forward * Time.deltaTime * (DataMgr.Instance.dicInfo[SoldierName].Speed - influenceSpeed)); //如果凉了切话到死亡状态 if (currentHP <= 0) { state = State.Dead; ani.SetInteger("HP", currentHP); } //没有凉的话就探测敌人,最近 else { var nearestEnemy = new SoldierCtrl(); float nearestDis = (float)Double.PositiveInfinity; float tempDis; foreach (var e in soldierData.SoldierDic) { if (soldierData.SoldierDic.Contains(e) && e.Value.camp != this.camp) { tempDis = Vector3.Distance(e.Value.transform.position, transform.position); if (tempDis < nearestDis) { nearestEnemy = e.Value; nearestDis = tempDis; } } } //当有敌人进入探测范围时朝它前进 if (nearestDis <= Consts.DetectionRange) { Vector3 rotateVector = nearestEnemy.transform.position - transform.position; Quaternion newRotate = Quaternion.LookRotation(rotateVector); this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, newRotate, Consts.RotateSpeed * Time.deltaTime); //敌人进入攻击距离,攻击 if (nearestDis <= DataMgr.Instance.dicInfo[SoldierName].AttackRange) { state = State.Fight; FightId = nearestEnemy.id; } } //默认前进方向,根据建筑物选择 else { if (camp == Camp.Dark) { if (DataMgr.Instance.DarkBuildDic.ContainsKey(buildId)) { this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, DataMgr.Instance.DarkBuildDic[buildId].StartRotate, Consts.RotateSpeed * Time.deltaTime); } } else { if (DataMgr.Instance.BrightBuildDic.ContainsKey(buildId)) { this.transform.rotation = Quaternion.RotateTowards(this.transform.rotation, DataMgr.Instance.BrightBuildDic[buildId].StartRotate, Consts.RotateSpeed * Time.deltaTime); } } } } }