public void HandleBuildAction(int index) { BuildSite site = null; foreach (Unit b in GameObject.Find("CellGrid").GetComponent <CellGrid>().Units) { if (b is BuildSite && (b as BuildSite).selected) { site = b as BuildSite; break; } } if (site == null) { return; } //handle cost if ((DarkRift.DarkRiftAPI.isConnected && !site.CanAffordToBuild(NetManager.buildCosts[index - 1]))) { return; } site.grid.Players[site.PlayerNumber].Money -= NetManager.buildCosts[index - 1]; PlayerController.currTurnMoney = site.grid.Players[site.PlayerNumber].Money; SetAvailableFundsText(GameObject.Find("Canvas").GetComponentsInChildren <Text>(), site); //GameObject.Find("Canvas").GetComponentsInChildren<Text>()[site.PlayerNumber].text = PlayerController.currTurnMoney.ToString() + "G" + "(+" + site.grid.Players[site.PlayerNumber].income.ToString() + "G)"; site.buildingType = (MatIndex)index; site.GetComponent <ChangeMaterial>().SetNewMaterial((MatIndex)index); UnitUpdate update = new UnitUpdate(); update.command = UnitUpdateCommand.BUILDSITE; update.moving = site.unitIndex; update.buildingType = (MatIndex)index; NetManager.SendData(TagIndex.Controller, TagIndex.PlayerUpdate, update); site.OnUnitDeselected(); }
void ReceiveData(ushort senderID, byte tag, ushort subject, object data) { bool ignoreMessage = false; if (ignoreMessage) { return; } if (tag == TagIndex.Controller) { if (subject == TagIndex.ControllerSubjects.JoinMessage) { networkConnected = true; DarkRiftAPI.SendMessageToID(senderID, TagIndex.Controller, TagIndex.ControllerSubjects.SpawnPlayer, null); SendData(TagIndex.Controller, TagIndex.PlayerUpdate, factions[DarkRiftAPI.id - 1]); if (senderID == 1) { playerNumber = 2; } if (senderID > playerCount) { playerCount = senderID; } //GameObject.Find("Name1").GetComponent<UnityEngine.UI.InputField>().readOnly = true; //GameObject.Find("Name2").GetComponent<UnityEngine.UI.InputField>().readOnly = false; //Handle Connect message } if (subject == TagIndex.ControllerSubjects.SpawnPlayer) { networkConnected = true; //Handle spawning the player into our game } if (subject == TagIndex.PlayerUpdate) { if (data is string && (data as string) == "ERROR") { possibleDesyncOccurred = true; RetrySendData(); } if (data is string[]) { string[] name = data as string[]; if (name[0] == "name") { GameObject go = GameObject.Find("Name" + (senderID).ToString()); go.GetComponent <UnityEngine.UI.InputField>().text = name[1]; } else if (name[0] == "StartGame") { DontDestroyOnLoad(transform.gameObject); DontDestroyOnLoad(GameObject.Find("GameInitiator")); //if (NetManager.playerCount >= 2) //{ // GameInit.mapName = GameInit.mapNames[NetManager.playerCount - 2]; //} //string mapDropDownText = GameObject.Find("mapDropDown").GetComponent<Dropdown>().itemText.text; //GameInit.mapName = mapDropDownText.Split(' ')[1]; GameInit.mapName = name[1]; SceneManager.LoadScene(GameInit.mapName); //SceneManager.LoadScene("Main"); } else if (name[0] == "charUpdate") { GameObject.Find(name[1]).GetComponent <UnityEngine.UI.Dropdown>().value = int.Parse(name[2]); } } if (data is UnitUpdate /*&& messageIndex + 1 == (data as UnitUpdate).index*/) { UnitUpdate update = data as UnitUpdate; GameObject unitsParent = GameObject.Find("Units Parent"); Unit[] units = unitsParent.GetComponentsInChildren <Unit>(); CellGrid grid = GameObject.Find("CellGrid").GetComponent <CellGrid>(); messageIndex = update.index; if (IsEndTurnCommand(update)) { grid.EndTurn(); //end turn } else if (IsMoveUnitCommand(update)) { foreach (Unit u in units) { if (u.unitIndex == update.moving) { //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>(); foreach (Cell c in grid.Cells) { if (c.transform.position.x == update.newLocationX && c.transform.position.y == update.newLocationY) { u.AdjustCellMovementCosts(); var path = u.FindPath(grid.Cells, c); u.ResetMovementPoints(); u.Move(c, path); u.ResetMovementPoints(); break; } } break; } } } else if (IsAttackCommand(update)) { Unit attacker = null; Unit defender = null; foreach (Unit u in units) { if (u.unitIndex == update.moving) { attacker = u; //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>(); } if (u.unitIndex == update.target) { defender = u; } if (attacker != null && defender != null) { attacker.DealDamage(defender, true); //attack break; } } } else if (IsSpawnUnitCommand(update)) { foreach (Unit c in grid.Units) { if (c.transform.position.x == update.newLocationX && c.transform.position.y == update.newLocationY) { if (c is BarracksUnit) { (c as BarracksUnit).InstantiateUnit(update.type); } else if (c is BuildSite) { if (update.type >= UnitType.INFANTRY && update.type <= UnitType.ANTIAIR) { //TODO 1 //guiCamRef.GetComponent<NewBarracks>().SpawnAStarFromNetwork((int)update.type, (int)update.moving); guiCamRef.GetComponent <NewBarracks>().SpawnUnitFromNetwork((int)update.type, (int)update.moving, 1); //GameObject.Find("GUICamera").GetComponent<NewBarracks>().SpawnAStarFromNetwork((int)update.type, (int)update.index); } else if (update.type >= UnitType.ANDROID && update.type <= UnitType.TURRET) { guiCamRef.GetComponent <NewBarracks>().SpawniFactionFromNetwork((int)update.type, (int)update.moving); } else if (update.type >= UnitType.ADVERSARY && update.type <= UnitType.STASISGUN) { guiCamRef.GetComponent <NewBarracks>().SpawnusbFromNetwork((int)update.type, (int)update.moving); } //(c as BuildSite).InstantiateUnit(update.type); } break; } } foreach (Unit c in grid.Units) { if (c.transform.position.x == update.newLocationX && c.transform.position.y == update.newLocationY) { if (c is Airport) { (c as Airport).InstantiateUnit(update.type); } else if (c is BuildSite) { if (update.type >= UnitType.FIGHTER && update.type <= UnitType.COPTER) { guiCamRef.GetComponent <NewAirport>().SpawnAStarFromNetwork((int)update.type, (int)update.moving); } else if (update.type >= UnitType.CAPCOPTER && update.type <= UnitType.BOMB) { guiCamRef.GetComponent <NewAirport>().SpawniFactionFromNetwork((int)update.type, (int)update.moving); } else if (update.type >= UnitType.ORBITALSTRIKER && update.type <= UnitType.NULLSTAR) { guiCamRef.GetComponent <NewAirport>().SpawniFactionFromNetwork((int)update.type, (int)update.moving); } //(c as BuildSite).InstantiateUnit(update.type); } break; } } //CellGrid grid = GameObject.Find("CellGrid").GetComponent<CellGrid>() } else if (IsCaptureCommand(update)) { } else if (IsIncreaseMoneGenCommand(update)) { foreach (Unit c in grid.Units) { if (c.unitIndex == update.moving) { if (c is City) { (c as City).SelfIncreaseMoneyGenIfAble(); } else if (c is BuildSite) { (c as BuildSite).SelfIncreaseMoneyGenIfAble(); } } } } else if (IsUpgradeBuildSiteCommand(update)) { BuildSite site = null; foreach (Unit b in GameObject.Find("CellGrid").GetComponent <CellGrid>().Units) { if (b is BuildSite && b.unitIndex == update.moving) { site = b as BuildSite; break; } } if (site == null) { return; } //handle cost Debug.Log("Site == null: " + (site == null).ToString()); Debug.Log("update == null: " + (update == null).ToString()); Debug.Log(NetManager.buildCosts[(int)update.buildingType - 1]); if (!site.CanAffordToBuild(NetManager.buildCosts[(int)update.buildingType - 1])) { return; } site.grid.Players[site.PlayerNumber].Money -= NetManager.buildCosts[(int)update.buildingType - 1]; PlayerController.currTurnMoney = site.grid.Players[site.PlayerNumber].Money; GameObject.Find("Canvas").GetComponentsInChildren <Text>()[site.PlayerNumber].text = PlayerController.currTurnMoney.ToString() + "G" + "(+" + site.grid.Players[site.PlayerNumber].income.ToString() + "G)"; site.buildingType = update.buildingType; site.GetComponent <ChangeMaterial>().SetNewMaterial(update.buildingType); } else if (data is UnitUpdate && messageIndex + 1 != (data as UnitUpdate).index) { possibleDesyncOccurred = true; SendData(TagIndex.Controller, TagIndex.PlayerUpdate, "ERROR"); //send error message } } else if (data is Unit.Faction) { factions[senderID - 1] = (Unit.Faction)data; } } } }