Skip to content

Commit

Permalink
完成了基本的位置同步,待测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Caizc committed Sep 8, 2017
1 parent 72986bd commit 7c05264
Show file tree
Hide file tree
Showing 34 changed files with 678 additions and 227 deletions.
4 changes: 2 additions & 2 deletions Assets/Completed-Assets/Scripts/Done_DestroyByContact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public class Done_DestroyByContact : MonoBehaviour

void Start ()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("BattleController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <Done_GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
Debug.Log ("Cannot find 'BattleController' script");
}
}

Expand Down
9 changes: 9 additions & 0 deletions Assets/Prefabs/Characters.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Prefabs/Characters/ShipA.prefab
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Assets/Prefabs/Characters/ShipB.prefab
Binary file not shown.
9 changes: 9 additions & 0 deletions Assets/Prefabs/Characters/ShipB.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Prefabs/Enemy.prefab
Binary file not shown.
5 changes: 3 additions & 2 deletions Assets/Prefabs/Enemy.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
9 changes: 9 additions & 0 deletions Assets/Prefabs/LevelController.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Prefabs/Player.prefab
Binary file not shown.
4 changes: 2 additions & 2 deletions Assets/Prefabs/Player.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Assets/Prefabs/UICamera.prefab
Binary file not shown.
Binary file added Assets/Prefabs/UICanvas.prefab
Binary file not shown.
9 changes: 9 additions & 0 deletions Assets/Prefabs/UICanvas.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/Scripts/GameMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class GameMgr : MonoBehaviour
{
public static GameMgr instance;

public string id = "Tank";
// 当前玩家 id
public string id = "_UnknowPlayerID_";

void Awake()
{
Expand Down
130 changes: 90 additions & 40 deletions Assets/Scripts/MultiBattle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,111 @@

public class MultiBattle : MonoBehaviour
{
//单例
// 单例
public static MultiBattle instance;
//坦克预设

// 坦克预设
public GameObject[] tankPrefabs;
//战场中的所有坦克

// 战场中的所有坦克
public Dictionary<string, BattleTank> list = new Dictionary<string, BattleTank>();

// Use this for initialization
void Start()
{
//单例模式
// 单例模式
instance = this;
}

//获取阵营 0表示错误
// 获取阵营 0表示错误
public int GetCamp(GameObject tankObj)
{
foreach (BattleTank mt in list.Values)
{
if (mt.tank.gameObject == tankObj)
{
return mt.camp;
}
}
return 0;
}

//是否同一阵营
/// <summary>
/// 判断两个单位是否同一阵营
/// </summary>
/// <param name="tank1"></param>
/// <param name="tank2"></param>
/// <returns></returns>
public bool IsSameCamp(GameObject tank1, GameObject tank2)
{
return GetCamp(tank1) == GetCamp(tank2);
}

//清理场景
/// <summary>
/// 清理场景
/// </summary>
public void ClearBattle()
{
list.Clear();

// TODO: 这里使用了 Tank 标签来获取所有单位
GameObject[] tanks = GameObject.FindGameObjectsWithTag("Tank");

for (int i = 0; i < tanks.Length; i++)
{
Destroy(tanks[i]);
}
}

//开始战斗
/// <summary>
/// 开始战斗
/// </summary>
/// <param name="proto"></param>
public void StartBattle(ProtocolBytes proto)
{
//解析协议
// 解析协议
int start = 0;
string protoName = proto.GetString(start, ref start);
if (protoName != "Fight")
{
return;
//坦克总数
}

// 坦克总数
int count = proto.GetInt(start, ref start);
//清理场景

// 清理场景
ClearBattle();
//每一辆坦克

// 每一辆坦克
for (int i = 0; i < count; i++)
{
string id = proto.GetString(start, ref start);
int team = proto.GetInt(start, ref start);
int swopID = proto.GetInt(start, ref start);

// 生成坦克
GenerateTank(id, team, swopID);
}
NetMgr.srvConn.msgDist.AddListener ("UpdateUnitInfo", RecvUpdateUnitInfo);
NetMgr.srvConn.msgDist.AddListener ("Shooting", RecvShooting);
NetMgr.srvConn.msgDist.AddListener ("Hit", RecvHit);
NetMgr.srvConn.msgDist.AddListener ("Result", RecvResult);
}

// 向消息分发管理器注册相应事件的回调方法
NetMgr.srvConn.msgDist.AddListener("UpdateUnitInfo", RecvUpdateUnitInfo);
NetMgr.srvConn.msgDist.AddListener("Shooting", RecvShooting);
NetMgr.srvConn.msgDist.AddListener("Hit", RecvHit);
NetMgr.srvConn.msgDist.AddListener("Result", RecvResult);
}

//产生坦克
/// <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);
Expand All @@ -87,28 +119,33 @@ public void GenerateTank(string id, int team, int swopID)
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]);

// 产生坦克
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;
Expand All @@ -119,50 +156,66 @@ public void GenerateTank(string id, int team, int swopID)
else
{
bt.tank.ctrlType = Tank.CtrlType.net;
bt.tank.InitNetCtrl (); //初始化网络同步
bt.tank.InitNetCtrl(); //初始化网络同步
}
}


/// <summary>
/// 处理接收到的单位同步信息
/// </summary>
/// <param name="protocol"></param>
public void RecvUpdateUnitInfo(ProtocolBase protocol)
{
//解析协议
// 解析协议
int start = 0;
ProtocolBytes proto = (ProtocolBytes)protocol;
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); //稍后实现
bt.tank.NetTurretTarget(turretY, gunX); // 稍后实现
}



/// <summary>
/// 处理接收到的开火同步信息
/// </summary>
/// <param name="protocol"></param>
public void RecvShooting(ProtocolBase protocol)
{
//解析协议
int start = 0;
ProtocolBytes proto = (ProtocolBytes)protocol;
ProtocolBytes proto = (ProtocolBytes) protocol;
string protoName = proto.GetString(start, ref start);
string id = proto.GetString(start, ref start);
Vector3 pos;
Expand Down Expand Up @@ -192,7 +245,7 @@ public void RecvHit(ProtocolBase protocol)
{
//解析协议
int start = 0;
ProtocolBytes proto = (ProtocolBytes)protocol;
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);
Expand Down Expand Up @@ -220,7 +273,7 @@ public void RecvResult(ProtocolBase protocol)
{
//解析协议
int start = 0;
ProtocolBytes proto = (ProtocolBytes)protocol;
ProtocolBytes proto = (ProtocolBytes) protocol;
string protoName = proto.GetString(start, ref start);
int winTeam = proto.GetInt(start, ref start);
//弹出胜负面板
Expand All @@ -240,7 +293,4 @@ public void RecvResult(ProtocolBase protocol)
NetMgr.srvConn.msgDist.DelListener("Hit", RecvHit);
NetMgr.srvConn.msgDist.DelListener("Result", RecvResult);
}
}



}
Loading

0 comments on commit 7c05264

Please sign in to comment.