/// <summary> /// 小怪的状态信息 /// </summary> /// <param name="state"></param> /// <param name="id"></param> /// <param name="listNum"></param> public void SendEnemyStateMsg(int state, int id, int listNum) { byte[] by = EnemyStateMessage.GetBytes( new EnemyStateMessage(state, id, listNum)); NetworkMessage message = new NetworkMessage(61, NetworkTools.GetLocalIP(), by); byte[] bytes = NetworkMessage.GetBytes(message); SendMsg(RoomSingle.roomIP, NetworkConstent.UDPServerPort, bytes); }
public static byte[] GetBytes(EnemyStateMessage t) { byte[] bx = BitConverter.GetBytes(t.state); byte[] btype = BitConverter.GetBytes(t.enemyType); byte[] bnum = BitConverter.GetBytes(t.num); byte[] bytes = new byte[bx.Length + btype.Length + bnum.Length]; bx.CopyTo(bytes, 0); btype.CopyTo(bytes, 4); bnum.CopyTo(bytes, 8); return(bytes); }
protected override void NetworkCallback(NetworkMessage message) { if (message.type == 60) { EnemyInfoMessage info = EnemyInfoMessage.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (!enemyDic.ContainsKey(key)) { GameObject prefab = Resources.Load <GameObject>("Prefab/Enemy/Enemy_AI_" + info.enemyType); GameObject obj = Instantiate <GameObject>(prefab, new Vector3(info.x, info.y, info.z), Quaternion.Euler(info.angleX, info.angleY, info.angleZ), transform); NetEnemyBase net = obj.GetComponent <NetEnemyBase>(); net.SetIDandListNum(info.enemyType, info.num); net.SetTarget(info); enemyDic.Add(key, net); } } else if (message.type == 61) { //Debug.Log("同步小怪状态"); EnemyStateMessage info = EnemyStateMessage.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (enemyDic.ContainsKey(key)) { enemyDic[key].SwitchEnemyState((EnemyState)info.state); if (info.state == (int)(EnemyState.Death)) { enemyDic.Remove(key); } } } else if (message.type == 62 && !NetworkTools.GetLocalIP().Equals(message.ip)) { EnemyHurtMessge info = EnemyHurtMessge.GetMessage(message.message); string key = info.enemyType.ToString() + info.num.ToString(); if (enemyDic.ContainsKey(key)) { enemyDic[key].ReduceHP(info.hurt); } } }