public void ChangeObserveTarget() { for (int i = 0; i < Battle.instance.battleTanks.Length; i++) { BattleTank bt = Battle.instance.battleTanks [i]; if (bt.camp == observedCamp && !bt.dead && i >= index) { bool updated = false; for (int j = i + 1; j < Battle.instance.battleTanks.Length; j++) { BattleTank bt2 = Battle.instance.battleTanks [j]; if (bt2.camp == observedCamp && !bt2.dead) { index = j; updated = true; break; } } if (!updated) { index = 0; } Debug.Log("Update index: " + index); CameraFollow cf = Camera.main.gameObject.GetComponent <CameraFollow> (); GameObject cameraTarget = bt.tank.transform.GetChild(1).GetChild(1).gameObject; cf.SetTarget(cameraTarget); return; } } }
public void RecvResult(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); int winTeam = proto.GetInt(start, ref start); //弹出胜负面板 string id = GameMgr.instance.id; BattleTank bt = list[id]; if (bt.camp == winTeam) { PanelMgr.instance.OpenPanel <WinPanel>("", 1); } else { PanelMgr.instance.OpenPanel <WinPanel>("", 0); } //取消监听 NetMgr.Instance.srvConn.msgDist.DelListener("UpdateUnitInfo", RecvUpdateUnitInfo); NetMgr.Instance.srvConn.msgDist.DelListener("Shooting", RecvShooting); NetMgr.Instance.srvConn.msgDist.DelListener("Hit", RecvHit); NetMgr.Instance.srvConn.msgDist.DelListener("Result", RecvResult); }
private void RecvHit(ProtocolBase protocol) { int start = 0; Debug.LogError("RecvHit"); ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string attId = proto.GetString(start, ref start); string defId = proto.GetString(start, ref start); float hurt = proto.GetFloat(start, ref start); if (!list.ContainsKey(attId)) { Debug.Log("RecvHit attBt==null+ " + attId); return; } BattleTank attBt = list[attId]; if (!list.ContainsKey(defId)) { Debug.Log("Recvh defBt ==null" + defId); return; } BattleTank defBt = list[defId]; defBt.tank.NetBeAttacked(hurt, attBt.tank.gameObject); }
public void OnRecvHit(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_hit_reply reply = PackCodec.Deserialize <s2c_hit_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 attId = reply.id; UInt32 defId = reply.enemy_id; float hurt = (float)(reply.damage * 1.0 / 1000); //取得BattleTank if (!list.ContainsKey(attId)) { Debug.Log("OnRecvHit attBt == null " + attId); return; } BattleTank attBt = list[attId]; if (!list.ContainsKey(defId)) { Debug.Log("OnRecvHit defBt == null " + defId); return; } BattleTank defBt = list[defId]; //被击中的坦克 defBt.tank.NetBeAttacked(hurt, attBt.tank.gameObject); }
public void RecvUpdateUnitInfo(ProtocolBase protocol) { int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string id = proto.GetString(start, ref start); Vector3 nPos; Vector3 nRot; nPos.x = proto.GetFloat(start, ref start); nPos.y = proto.GetFloat(start, ref start); nPos.z = proto.GetFloat(start, ref start); nRot.x = proto.GetFloat(start, ref start); nRot.y = proto.GetFloat(start, ref start); nRot.z = proto.GetFloat(start, ref start); float turretY = proto.GetFloat(start, ref start); float gunX = proto.GetFloat(start, ref start); Debug.Log("RecvUpdateUnitInfo" + id); if (!list.ContainsKey(id)) { Debug.Log("RecvUpdateUnitInfo bt == null"); return; } BattleTank bt = list [id]; if (id == GameMgr.instance.id) { return; } bt.tank.NetForecastInfo(nPos, nRot); //bt.tank.NetTurretTarget(turretY,gunX); }
public void OnRecvResult(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_result_reply reply = PackCodec.Deserialize <s2c_result_reply>(buffer.ReadBytes(size - sizeof(Int32))); Int32 team = reply.camp; //弹出胜负面板 UInt32 id = GameMgr.instance.id; BattleTank bt = list[id]; if (bt.camp == team) { PanelMgr.instance.OpenPanel <WinPanel>("", 1); } else { PanelMgr.instance.OpenPanel <WinPanel>("", 0); } //取消监听 NetMgr.servConn.msgDist.DelListener("s2c_update_unit_reply", OnRecvUpdateUnitInfo); NetMgr.servConn.msgDist.DelListener("s2c_shooting_reply", OnRecvShooting); NetMgr.servConn.msgDist.DelListener("s2c_hit_reply", OnRecvHit); NetMgr.servConn.msgDist.DelListener("s2c_result_reply", OnRecvResult); }
/// <summary> /// 处理接收到的开火同步信息 /// </summary> /// <param name="protocol"></param> public void RecvShooting(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string id = proto.GetString(start, ref start); Vector3 pos; Vector3 rot; pos.x = proto.GetFloat(start, ref start); pos.y = proto.GetFloat(start, ref start); pos.z = proto.GetFloat(start, ref start); rot.x = proto.GetFloat(start, ref start); rot.y = proto.GetFloat(start, ref start); rot.z = proto.GetFloat(start, ref start); //处理 if (!list.ContainsKey(id)) { Debug.Log("RecvShooting bt == null "); return; } BattleTank bt = list[id]; if (id == GameMgr.instance.id) { return; } bt.tank.NetShoot(pos, rot); }
public void RecvHit(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string attId = proto.GetString(start, ref start); string defId = proto.GetString(start, ref start); float hurt = proto.GetFloat(start, ref start); //获取BattleTank if (!list.ContainsKey(attId)) { Debug.Log("RecvHit attBt == null " + attId); return; } BattleTank attBt = list[attId]; if (!list.ContainsKey(defId)) { Debug.Log("RecvHit defBt == null " + defId); return; } BattleTank defBt = list[defId]; //被击中的坦克 defBt.tank.NetBeAttacked(hurt, attBt.tank.gameObject); }
public void RecvShooting(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string name = proto.GetString(start, ref start); Vector3 Pos; Vector3 Rot; Pos.x = proto.GetFloat(start, ref start); Pos.y = proto.GetFloat(start, ref start); Pos.z = proto.GetFloat(start, ref start); Rot.x = proto.GetFloat(start, ref start); Rot.y = proto.GetFloat(start, ref start); Rot.z = proto.GetFloat(start, ref start); Debug.Log("RecvShooting " + name); if (!list.ContainsKey(name)) { Debug.Log("RecvShooting bt == null "); return; } BattleTank bt = list[name]; if (name == Root.this_player.Name) //跳过自己的更新 { return; } bt.tank.NetShoot(Pos, Rot); }
public void OnRecvShooting(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_shooting_reply reply = PackCodec.Deserialize <s2c_shooting_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 id = reply.id; Vector3 pos; Vector3 rot; pos.x = (float)(reply.pos_x * 1.0 / 1000); pos.y = (float)(reply.pos_y * 1.0 / 1000); pos.z = (float)(reply.pos_z * 1.0 / 1000); rot.x = (float)(reply.rot_x * 1.0 / 1000); rot.y = (float)(reply.rot_y * 1.0 / 1000); rot.z = (float)(reply.rot_z * 1.0 / 1000); //处理 if (!list.ContainsKey(id)) { Debug.Log("OnRecvShooting bt == null"); return; } BattleTank bt = list[id]; //跳过自己的同步信息 if (id == GameMgr.instance.id) { return; } bt.tank.NetShoot(pos, rot); }
/// <summary> /// 生成坦克 /// </summary> /// <param name="id"></param> /// <param name="team"></param> /// <param name="swopID"></param> public void GenerateTank(string id, int team, int swopID) { // 获取预设的出生点 Transform sp = GameObject.Find("SwopPoints").transform; Transform swopTrans; if (team == 1) { Transform teamSwop = sp.GetChild(0); swopTrans = teamSwop.GetChild(swopID - 1); } else { Transform teamSwop = sp.GetChild(1); swopTrans = teamSwop.GetChild(swopID - 1); } if (swopTrans == null) { Debug.LogError("GenerateTank出生点错误!"); return; } // 预设 if (tankPrefabs.Length < 2) { Debug.LogError("坦克预设数量不够"); return; } // 产生坦克 GameObject tankObj = (GameObject)Instantiate(tankPrefabs[team - 1]); tankObj.name = id; tankObj.transform.position = swopTrans.position; tankObj.transform.rotation = swopTrans.rotation; // 列表处理 BattleTank bt = new BattleTank(); bt.tank = tankObj.GetComponent <Tank>(); bt.camp = team; list.Add(id, bt); // 玩家处理 if (id == GameMgr.instance.id) { bt.tank.ctrlType = Tank.CtrlType.player; CameraFollow cf = Camera.main.gameObject.GetComponent <CameraFollow>(); GameObject target = bt.tank.gameObject; cf.SetTarget(target); } else { bt.tank.ctrlType = Tank.CtrlType.net; bt.tank.InitNetCtrl(); //初始化网络同步 } }
private void SetText(BattleTank bt, int camp = 0, int index = 0) { string obName = "Camp"; string name = "", killNum = ""; Color color; if (bt != null) { obName += bt.camp; if (bt.camp == 1) { camp1Index++; obName += camp1Index; color = (bt.dead) ? camp1Dead : camp1Alive; } else { camp2Index++; obName += camp2Index; color = (bt.dead) ? camp2Dead : camp2Alive; } name = bt.name; killNum += bt.numKill; if (bt.isPlayer) { color = player; } } else { obName = obName + camp + index; color = new Color(0, 0, 0); } // 显示名字 text = list.Find(obName).GetComponent <Text>(); text.text = name; // 由于颜色改不了而做的妥协 if (bt != null) { // 死了显示白色 if (bt.dead) { text.color = color; } // 粗斜体表示玩家 if (bt.isPlayer) { text.fontStyle = FontStyle.BoldAndItalic; } } // 显示击杀数 text = list.Find(obName + "-Kill").GetComponent <Text>(); text.text = killNum; }
//生成场景 private void generateTank(int camp, int num, Transform spCamp, int index) { //throw new NotImplementedException(); Transform trans = spCamp.GetChild(num); Vector3 pos = trans.position; Quaternion rot = trans.rotation; GameObject perfab = tankPerfabs[camp - 1]; GameObject tankObj = (GameObject)Instantiate(perfab, pos, rot); tankcontrol tankCmp = tankObj.GetComponent <tankcontrol>(); battleTanks[index] = new BattleTank(); battleTanks[index].tank = tankCmp; battleTanks[index].camp = camp; }
public void GenerateTank(int camp, int num, Transform spCamp, int index) { Transform trans = spCamp.GetChild(num);//这个num用的很奇怪 Vector3 pos = trans.position; Quaternion rot = trans.rotation; GameObject prefab = tankPrefabs[camp - 1]; GameObject tankObj = GameObject.Instantiate(prefab, pos, rot); Tank tankCmp = tankObj.GetComponent <Tank>(); tankCmp.ctrlType = CtrlType.computer; battleTanks[index] = new BattleTank(); battleTanks[index].tank = tankCmp; battleTanks[index].camp = camp; }
public int GetCamp(GameObject tankObj) { for (int i = 0; i < battleTanks.Length; i++) { BattleTank bt = battleTanks [i]; if (bt == null) { return(0); } if (bt.tank != null && bt.tank.gameObject == tankObj) { return(bt.camp); } } return(0); }
//生成一辆坦克 public void GenerateTank(int camp, int num, Transform spCamp, int index) { //获取出生点和预设 Transform trans = spCamp.GetChild(num); Vector3 pos = trans.position; Quaternion rot = trans.rotation; GameObject prefab = tankPrefabs[camp - 1]; //产生坦克 GameObject tankObj = (GameObject)Instantiate(prefab, pos, rot); //设置属性 Tank tankCmp = tankObj.GetComponent <Tank>(); tankCmp.ctrlType = Tank.CtrlType.computer; //battleTanks battleTanks[index] = new BattleTank(); battleTanks[index].tank = tankCmp; battleTanks[index].camp = camp; }
//是否获胜 public bool IsWin(int camp) { for (int i = 0; i < battleTanks.Length; i++) { BattleTank Tank = battleTanks[i]; Debug.Log(Tank + " " + Tank.camp + " " + Tank.tank.Hp); if (Tank.camp != camp) { if (Tank.tank.Hp > 0) { return(false); } } } Debug.Log("阵营" + camp + "获胜"); PanelMgr.instance.OpenPanel <WinPanel>("", camp); return(true); }
/// <summary> /// 处理接收到的单位同步信息 /// </summary> /// <param name="protocol"></param> public void RecvUpdateUnitInfo(ProtocolBase protocol) { // 解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string id = proto.GetString(start, ref start); Vector3 nPos; Vector3 nRot; // 位置向量 nPos.x = proto.GetFloat(start, ref start); nPos.y = proto.GetFloat(start, ref start); nPos.z = proto.GetFloat(start, ref start); // 转向向量 nRot.x = proto.GetFloat(start, ref start); nRot.y = proto.GetFloat(start, ref start); nRot.z = proto.GetFloat(start, ref start); float turretY = proto.GetFloat(start, ref start); float gunX = proto.GetFloat(start, ref start); // 处理 Debug.Log("RecvUpdateUnitInfo " + id); if (!list.ContainsKey(id)) { Debug.Log("RecvUpdateUnitInfo bt == null "); return; } BattleTank bt = list[id]; // 如果该位置同步消息包是自己发出的,则忽略,不进行位置和转向同步,避免被拉回到之前的位置上 if (id == GameMgr.instance.id) { return; } // 设置坦克的位置和转向 bt.tank.NetForecastInfo(nPos, nRot); bt.tank.NetTurretTarget(turretY, gunX); // 稍后实现 }
public void OnRecvUpdateUnitInfo(byte[] Byte) { ByteBuffer buffer = new ByteBuffer(Byte); int size = Byte.Length; int typeId = buffer.ReadInt(); s2c_update_unit_reply reply = PackCodec.Deserialize <s2c_update_unit_reply>(buffer.ReadBytes(size - sizeof(Int32))); UInt32 id = reply.id; Vector3 nPos; Vector3 nRot; nPos.x = (float)(reply.pos_x * 1.0 / 1000); nPos.y = (float)(reply.pos_y * 1.0 / 1000); nPos.z = (float)(reply.pos_z * 1.0 / 1000); nRot.x = (float)(reply.rot_x * 1.0 / 1000); nRot.y = (float)(reply.rot_y * 1.0 / 1000); nRot.z = (float)(reply.rot_z * 1.0 / 1000); float turretY = (float)(reply.gun_rot * 1.0 / 1000); float gunX = (float)(reply.gun_roll * 1.0 / 1000); //处理 Debug.Log("OnRecvUpdateUnitInfo " + id); if (!list.ContainsKey(id)) { Debug.Log("OnRecvUpdateUnitInfo bt == null"); return; } BattleTank bt = list[id]; //跳过自己的同步信息 if (id == GameMgr.instance.id) { return; } if (bt == null) { Debug.Log("bt == null "); return; } //更新预测位置和旋转 bt.tank.NetForecastInfo(nPos, nRot); //更新炮塔和炮管的旋转角度 bt.tank.NetTurretTarget(turretY, gunX); }
/** * 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]; } }
void OnGUI() { if (enable) { for (int i = 0; i < Battle.instance.battleTanks.Length; i++) { BattleTank bt = Battle.instance.battleTanks [i]; if (bt.camp == camp && !bt.dead) { if (bt.isPlayer) { DrawPosition(bt.tank.transform.position, minePosTexture); } else { DrawPosition(bt.tank.transform.position, othersPosTexture); } } } } }
//产生坦克 public void GenerateTank(string name, int swopID) { Transform sp = GameObject.Find("SwopPoints").transform; Transform swopTrans; swopTrans = sp.GetChild(swopID); //给这辆坦克分配一个出生点 if (swopTrans == null) { Debug.LogError("GenerateTank 出生点错误"); return; } GameObject tankObj = (GameObject)Instantiate(tankPrefabs); tankObj.name = name; tankObj.transform.position = swopTrans.position; tankObj.transform.rotation = swopTrans.rotation; //列表处理 BattleTank bt = new BattleTank(); bt.tank = tankObj.GetComponent <ID_Control_CS>(); list.Add(name, bt); if (name == Root.this_player.Name) //找通过名字 筛选本人 { bt.tank.isPlayer = true; // CarmeraFollow 书上的是没有相机的,所以需要加上相机,而我的project每个tank自带相机,所以删除其他的相机 } else { //删除它们的相机 GameObject.Destroy(bt.tank.transform.Find("MainBody").Find("Camera_Pivot").gameObject); bt.tank.isPlayer = false; //并且不控制 //bt.tank.ctrlType = Tank.CtrlType.net; //初始化位置预测数据,因为 Vector3的默认值为 (0,0,0) bt.tank.InitNetCtrl(); } }
public void RecvUpdateUnitInfo(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string name = proto.GetString(start, ref start); Vector3 nPos; Vector3 nRot; nPos.x = proto.GetFloat(start, ref start); nPos.y = proto.GetFloat(start, ref start); nPos.z = proto.GetFloat(start, ref start); nRot.x = proto.GetFloat(start, ref start); nRot.y = proto.GetFloat(start, ref start); nRot.z = proto.GetFloat(start, ref start); float blood = proto.GetFloat(start, ref start); float tur_currentAng_in = proto.GetFloat(start, ref start); float targetAng_in = proto.GetFloat(start, ref start); float canno_current_in = proto.GetFloat(start, ref start); Debug.Log("RecvUpdateUnitInfo " + name); if (!list.ContainsKey(name)) { Debug.Log("RecvUpdateUnitInfo bt == null "); return; } BattleTank bt = list[name]; if (name == Root.this_player.Name) //跳过自己的更新 { return; } bt.tank.NetForecastInfo(nPos, nRot); //更新预测位置和旋转 bt.tank.NetTurretTarget(tur_currentAng_in, targetAng_in, canno_current_in); //更新炮塔和炮管的旋转角 bt.tank.NetBlood(blood); }
public void RecvBlood(ProtocolBase protocol) { //解析协议 int start = 0; ProtocolBytes proto = (ProtocolBytes)protocol; string protoName = proto.GetString(start, ref start); string name = proto.GetString(start, ref start); float blood = proto.GetFloat(start, ref start); Debug.Log("RecvBlood " + name); if (!list.ContainsKey(name)) { Debug.Log("RecvBlood bt == null "); return; } BattleTank bt = list[name]; if (name == Root.this_player.Name) //跳过自己的更新 { return; } bt.tank.NetBlood(blood); }
private void SetText(BattleTank bt, int camp = 0, int index = 0) { bool mvp = false; string obName = "Camp"; string name = "", killNum = ""; if (bt != null) { obName += bt.camp; if (bt.camp == 1) { camp1Index++; obName += camp1Index; } else { camp2Index++; obName += camp2Index; } name = bt.name; killNum += bt.numKill; if (bt.numKill > maxKill) { maxKill = bt.numKill; mvp = true; } } else { obName = obName + camp + index; } // 显示名字 text = list.Find(obName).GetComponent <Text>(); text.text = name; if (bt != null) { // 粗斜体表示玩家 if (bt.isPlayer) { text.fontStyle = FontStyle.BoldAndItalic; } } // 显示击杀数 text = list.Find(obName + "-Kill").GetComponent <Text>(); text.text = killNum; // 显示MVP标识 if (mvp) { Vector3 mvpPos; if (bt.camp == 1) { Vector3 targetPos = list.Find(obName).position; mvpPos = new Vector3(targetPos.x - 100, targetPos.y + 15, targetPos.z); } else { Vector3 targetPos = list.Find(obName + "-Kill").position; mvpPos = new Vector3(targetPos.x + 60, targetPos.y + 15, targetPos.z); } mvpImage.position = mvpPos; } }