private void PlayerJoined(int id, bool isBot = false) // Server { // init new player var cubeGO = Instantiate(serverCubePrefab, Utils.RandomStartPos(), Quaternion.identity); // Attach label on top of his head var canvas = Instantiate(playerUICanvas, new Vector3(), Quaternion.identity); canvas.transform.SetParent(cubeGO.transform); canvas.transform.localPosition = new Vector3(0f, 2f, 0f); cubeGO.transform.SetParent(GameObject.Find("Players(Server)").transform); cubeGO.name = $"player-{id}"; var newCube = new CubeEntity(cubeGO, id, isBot); cubeEntitiesServer[newCube.id] = newCube; newCube.SetUIManager(canvas.GetComponent <UIManager>(), $"{id}", 1f); if (isBot) { bots[newCube.id] = newCube; // store bots in another list to test applying movement to them } else { packetNumbers[newCube.id] = 0; maxInputs[newCube.id] = 0; } print($"{cubeGO.name} joined"); foreach (var cube in cubeEntitiesServer.Values) { if (cube.isBot) { continue; } // send player joined packet to everyone (including to the new player as a confirmation) Packet playerJoinedPacket = Packet.Obtain(); var buffer = playerJoinedPacket.buffer; buffer.PutInt((int)Utils.Ports.PLAYER_JOINED); buffer.PutInt(id); buffer.PutInt(cubeEntitiesServer.Count - 1); foreach (var sendCube in cubeEntitiesServer.Values) { if (sendCube.id == cube.id) { continue; // dont send a player to himself } buffer.PutInt(sendCube.id); sendCube.Serialize(buffer); } buffer.Flush(); Utils.Send(playerJoinedPacket, channel, cube.port); } }
private void AddUIToCube(CubeEntity cube, int cubeId) { var canvas = Instantiate(playerUICanvas, new Vector3(), Quaternion.identity); canvas.transform.SetParent(cube.GO.transform); canvas.transform.localPosition = new Vector3(0f, 2f, 0f); cube.SetUIManager(canvas.GetComponent <UIManager>(), $"{cubeId}", 1f); // var healthBar = canvas.GetComponent<HealthBar>(); // if (healthBar != null) // { // var healthBarComp = healthBar.GetComponent<HealthBar>(); // if(healthBarComp != null) cube.SetHealthBar(healthBarComp); // else // { // print("mini not"); // cube.SetHealthBar(healthBar); // } // } // else // { // print("not"); // } }