Beispiel #1
0
        private void setupEvents()
        {
            On("open", (E) =>
            {
                Debug.Log("Connection made to the server");
            });

            On("register", (E) =>
            {
                menu     = GameObject.FindGameObjectWithTag("MainMenu");
                ClientID = E.data["id"].ToString().RemoveQuotes();
                Debug.LogFormat("Our Client's ID ({0})", ClientID);
                // local storage 에 유저 기록이 있는지 확인 후 없다면
                menu.GetComponent <MenuManager>().setInputPanel("Enter your name");
            });

            On("setBlockChain", (E) =>
            {
                Debug.Log("setBlockChain");
                SetGenesis(E);
                Debug.Log("done setBlockChain");
            });

            On("initializeGameSetting", (E) =>
            {
                //initialize game field
                LevelCanvas = GameObject.FindGameObjectWithTag("LevelCanvas");
                UserInfo    = LevelCanvas.transform.Find("UserInfo").gameObject;
                RollButton  = LevelCanvas.transform.Find("RollButton").gameObject;
                gameManager = GameObject.FindGameObjectWithTag("GameManager");
                landManager = GameObject.FindGameObjectWithTag("LandManager");
                landManager.GetComponent <LandManager>().initializeLandSettings();
            });

            On("spawn", (E) =>
            {
                //Handling all spawning all players
                //Passed Data
                string id                  = E.data["id"].ToString().RemoveQuotes();
                int order                  = Int32.Parse(E.data["order"].ToString().RemoveQuotes());
                string username            = E.data["username"].ToString().RemoveQuotes();
                GameObject go              = Instantiate(playerPrefab, networkContainer);
                go.name                    = string.Format("Player ({0})", username);
                GameObject playerCharacter = go.transform.GetChild(order).gameObject;
                playerCharacter.SetActive(true);

                NetworkIdentity ni = go.GetComponent <NetworkIdentity>();
                ni.GetPlayerManager().setAnimator(playerCharacter.GetComponent <Animator>());
                Debug.Log("succesfully set animation");
                ni.GetPlayerManager().setColor(playerCharacter.transform.Find("Color").gameObject.GetComponent <Renderer>().material.color);
                ni.GetPlayerManager().setUserInfo(UserInfo.transform.GetChild(order).gameObject, username);

                ni.GetPlayerManager().setAssets(Int32.Parse(E.data["assets"].ToString().RemoveQuotes()));
                ni.GetPlayerManager().setBalance(Int32.Parse(E.data["balance"].ToString().RemoveQuotes()));

                ni.SetControllerID(id);
                ni.SetSocketReference(this);

                serverObjects.Add(id, ni);
                Debug.Log("spawn complete");
            });

            On("disconnected", (E) =>
            {
                string id = E.data["id"].ToString().RemoveQuotes();

                GameObject go = serverObjects[id].gameObject;
                Destroy(go);              //Remove from game
                serverObjects.Remove(id); //Remove from memory
                Debug.Log("distroy gameobject");
            });

            On("updatePosition", (E) =>
            {
                Data D     = SetNextBlock(E);
                string id  = D.eventData["id"].ToString().RemoveQuotes();
                float DIR  = D.eventData["DIR"].f;  // 방향주사위 눈 {0 :북 1 :동 2 : 남 3 :서 }
                float DIST = D.eventData["DIST"].f; // 거리주사위 눈
                float x    = D.eventData["x"].f;    // 당장 가야할 dx (항상 양수)
                float y    = D.eventData["y"].f;    // 당장 가야할 dy (항상 양수)
                float dist = D.eventData["dist"].f; // 남은 이동 거리 횟수
                Debug.Log("updatePosition");

                NetworkIdentity ni = serverObjects[id];
                gameManager.GetComponent <GameManager>().UpdatePosition(ni, DIR, x, y, dist, DIST);
            });
            On("updateBalance", (E) =>
            {
                Debug.Log("updateBalanece");
                Data D          = SetNextBlock(E);
                string sender   = D.eventData["senderID"].ToString().RemoveQuotes();
                string receiver = D.eventData["receiverID"].ToString().RemoveQuotes();
                if (receiver != "")
                {
                    NetworkIdentity receiver_ni = serverObjects[receiver];
                    receiver_ni.GetPlayerManager().Deposit(Int32.Parse(D.eventData["cost"].ToString().RemoveQuotes()));
                    receiver_ni.GetPlayerManager().setAssets(Int32.Parse(D.eventData["receiverAssets"].ToString().RemoveQuotes()));
                }
                if (sender != "")
                {
                    NetworkIdentity sender_ni = serverObjects[sender];
                    sender_ni.GetPlayerManager().Withdraw(Int32.Parse(D.eventData["cost"].ToString().RemoveQuotes()));
                    sender_ni.GetPlayerManager().setAssets(Int32.Parse(D.eventData["senderAssets"].ToString().RemoveQuotes()));
                }
            });
            On("updateLandData", (E) =>
            {
                Debug.Log("call updateLandData");
                Data D = SetNextBlock(E);
                Debug.Log("after SetNextBlock");
                Debug.Log(D);
                landManager.GetComponent <LandManager>().updateLandData(D);
            });
            // 1 Turn Start
            On("updateTurn", (E) =>
            {
                Data D       = SetNextBlock(E);
                string id    = D.eventData["id"].ToString().RemoveQuotes();
                int lapsToGo = Int32.Parse(D.eventData["lapsToGo"].ToString().RemoveQuotes());
                gameManager.GetComponent <GameManager>().UpdateTurn(lapsToGo);
                NetworkIdentity ni = serverObjects[id];
                ni.checkIsMyTurn(id);
                Debug.Log("updateTurn");

                if (ni.IsMyTurn())
                {
                    Debug.Log("ISmYTURN");
                    RollButton.GetComponent <RollDices>().StartRoll();
                }
            });
            On("gameOver", (E) =>
            {
                Debug.Log("On GAME OVER");
                Data D = SetNextBlock(E);
                gameManager.GetComponent <GameManager>().UpdateTurn(0);
                gameManager.GetComponent <GameManager>().GameOver(D);
            });
            On("GoBankrupt", (E) =>
            {
                Data D             = SetNextBlock(E);
                string id          = D.eventData["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                if (id == ClientID)
                {
                    CallTurnOver(ni);
                }
                ni.GetPlayerManager().setAssets(0);
                ni.GetPlayerManager().setBalance(0);
                landManager.GetComponent <LandManager>().resetLandSettings(id);
                ni.GetPlayerManager().SetUserState("BANKRUPT");
            });
            On("loadGame", (E) =>
            {
                Debug.Log("Switching to game");
                StartCoroutine(SceneManagementManager.Instance.LoadLevel(SceneList.LEVEL, (levelName) => {
                    SceneManagementManager.Instance.UnLoadLevel(SceneList.MAIN_MENU);
                }, this));
            });
            On("OnEnterGameLobby", (E) => {
                int index   = Int32.Parse(E.data["index"].ToString().RemoveQuotes());
                string id   = E.data["id"].ToString().RemoveQuotes();
                string name = E.data["name"].ToString().RemoveQuotes();
                gameLobby.GetComponent <GameLobby>().OnEnterPlayer(index, id, name);
            });
            On("lobbyUpdate", (E) => {
                OnGameStateChange.Invoke(E);
            });
            On("changedReadyState", (E) =>
            {
                int index = Int32.Parse(E.data["index"].ToString().RemoveQuotes());
                //string id = E.data["id"].ToString().RemoveQuotes();
                bool state = Boolean.Parse(E.data["state"].ToString().RemoveQuotes());
                gameLobby.GetComponent <GameLobby>().setPlayerState(index, state);
            });
            On("signupResult", (E) =>
            {
                string result = E.data["result"].ToString().RemoveQuotes();
                switch (result)
                {
                case "success":
                    menu.GetComponent <MenuManager>().setLobbyPanel();
                    break;

                case "exist":
                    menu.GetComponent <MenuManager>().setInputPanel("exist name!");
                    break;
                }
            });
        }
Beispiel #2
0
        private void setupEvents()
        {
            On("open", (E) => {
                Debug.Log("Connection made to the server");
            });

            On("register", (E) => {
                ClientID = E.data["id"].ToString().RemoveQuotes();
                Debug.LogFormat("Our Client's ID ({0})", ClientID);
            });

            On("spawn", (E) => {
                //Handling all spawning all players
                //Passed Data
                string id = E.data["id"].ToString().RemoveQuotes();

                GameObject go      = Instantiate(playerPrefab, networkContainer);
                go.name            = string.Format("Player ({0})", id);
                NetworkIdentity ni = go.GetComponent <NetworkIdentity>();
                ni.SetControllerID(id);
                ni.SetSocketReference(this);
                serverObjects.Add(id, ni);
            });

            On("disconnected", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();

                GameObject go = serverObjects[id].gameObject;
                Destroy(go);              //Remove from game
                serverObjects.Remove(id); //Remove from memory
            });

            On("updatePosition", (E) => {
                string id = E.data["id"].ToString().RemoveQuotes();
                float x   = E.data["position"]["x"].f;
                float y   = E.data["position"]["y"].f;

                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
            });

            On("updateRotation", (E) => {
                string id            = E.data["id"].ToString().RemoveQuotes();
                float tankRotation   = E.data["tankRotation"].f;
                float barrelRotation = E.data["barrelRotation"].f;

                NetworkIdentity ni            = serverObjects[id];
                ni.transform.localEulerAngles = new Vector3(0, 0, tankRotation);
                ni.GetComponent <PlayerManager>().SetRotation(barrelRotation);
            });

            On("updateAI", (E) => {
                string id            = E.data["id"].ToString().RemoveQuotes();
                float x              = E.data["position"]["x"].f;
                float y              = E.data["position"]["y"].f;
                float tankRotation   = E.data["tankRotation"].f;
                float barrelRotation = E.data["barrelRotation"].f;

                NetworkIdentity ni = serverObjects[id];
                if (ni.gameObject.activeInHierarchy)
                {
                    StartCoroutine(AIPositionSmoothing(ni.transform, new Vector3(x, y, 0)));
                    ni.GetComponent <AIManager>().SetTankRotation(tankRotation);
                    ni.GetComponent <AIManager>().SetBarrelRotation(barrelRotation);
                }
            });

            On("serverSpawn", (E) => {
                string name = E.data["name"].str;
                string id   = E.data["id"].ToString().RemoveQuotes();
                float x     = E.data["position"]["x"].f;
                float y     = E.data["position"]["y"].f;
                Debug.LogFormat("Server wants us to spawn a '{0}'", name);

                if (!serverObjects.ContainsKey(id))
                {
                    ServerObjectData sod             = serverSpawnables.GetObjectByName(name);
                    var spawnedObject                = Instantiate(sod.Prefab, networkContainer);
                    spawnedObject.transform.position = new Vector3(x, y, 0);
                    var ni = spawnedObject.GetComponent <NetworkIdentity>();
                    ni.SetControllerID(id);
                    ni.SetSocketReference(this);

                    //If bullet apply direction as well
                    if (name == "Bullet")
                    {
                        float directionX = E.data["direction"]["x"].f;
                        float directionY = E.data["direction"]["y"].f;
                        string activator = E.data["activator"].ToString().RemoveQuotes();
                        float speed      = E.data["speed"].f;

                        float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg;
                        Vector3 currentRotation          = new Vector3(0, 0, rot - 90);
                        spawnedObject.transform.rotation = Quaternion.Euler(currentRotation);

                        WhoActivatedMe whoActivatedMe = spawnedObject.GetComponent <WhoActivatedMe>();
                        whoActivatedMe.SetActivator(activator);

                        Projectile projectile = spawnedObject.GetComponent <Projectile>();
                        projectile.Direction  = new Vector2(directionX, directionY);
                        projectile.Speed      = speed;
                    }

                    serverObjects.Add(id, ni);
                }
            });

            On("serverUnspawn", (E) => {
                string id          = E.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                serverObjects.Remove(id);
                DestroyImmediate(ni.gameObject);
            });

            On("playerDied", (E) => {
                string id          = E.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                if (ni.GetComponent <AIManager>())
                {
                    ni.GetComponent <AIManager>().StopCoroutines();
                }

                ni.gameObject.SetActive(false);
            });

            On("playerRespawn", (E) => {
                string id             = E.data["id"].ToString().RemoveQuotes();
                float x               = E.data["position"]["x"].f;
                float y               = E.data["position"]["y"].f;
                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
                ni.gameObject.SetActive(true);
            });

            On("loadGame", (E) => {
                Debug.Log("Switching to game");
                SceneManagementManager.Instance.LoadLevel(SceneList.LEVEL, (levelName) => {
                    SceneManagementManager.Instance.UnLoadLevel(SceneList.MAIN_MENU);
                });
            });

            On("lobbyUpdate", (E) => {
                OnGameStateChange.Invoke(E);
            });
        }
Beispiel #3
0
 public static void CallTurnOver(NetworkIdentity ni)
 {
     ni.SetIsMyTurn(false);
     ni.GetSocket().Emit("turnOver");
 }
Beispiel #4
0
        private void SetUpEvents()
        {
            //open prints two times, but this is bug with the socket.io asset
            //the connection does not get duplicated so it does not matter much.
            On("open", (e) =>
            {
                Debug.Log("Connection Made with Server");
            });

            On("register", (e) =>
            {
                ClientID = e.data["id"].ToString().RemoveQuotes();//.RemoveQuotes();

                Debug.LogFormat("Our Client's ID is ({0})", ClientID);
            });

            On("spawn", (e) =>
            {
                string id = e.data["id"].ToString().RemoveQuotes();

                GameObject go = Instantiate(playerPrefab, networkContainer);
                go.name       = string.Format("Player ({0})", id);
                //Network Identity: NI : ni
                NetworkIdentity ni = go.GetComponent <NetworkIdentity>();
                ni.SetControllerID(id);
                ni.SetSocketReference(this);
                serverObjects.Add(id, ni);
            });

            On("disconnected", (e) =>
            {
                string id = e.data["id"].ToString().RemoveQuotes();

                GameObject go = serverObjects[id].gameObject;
                Destroy(go);
                serverObjects.Remove(id);
            });

            On("updatePosition", (e) =>
            {
                string id = e.data["id"].ToString().RemoveQuotes();
                float x   = e.data["position"]["x"].f;
                float y   = e.data["position"]["y"].f;

                NetworkIdentity ni    = serverObjects[id];
                ni.transform.position = new Vector3(x, y, 0);
            });

            On("updateRotation", (e) =>
            {
                string id = e.data["id"].ToString().RemoveQuotes();

                float tankRotation   = e.data["tankRotation"].f;
                float barrelRotation = e.data["barrelRotation"].f;

                NetworkIdentity ni            = serverObjects[id];
                ni.transform.localEulerAngles = new Vector3(0, 0, tankRotation);
                ni.GetComponent <PlayerManager>().SetRotation(barrelRotation);
            });

            On("serverSpawn", (e) =>
            {
                string name = e.data["name"].str;
                string id   = e.data["id"].ToString().RemoveQuotes();
                float x     = e.data["position"]["x"].f;
                float y     = e.data["position"]["y"].f;
                Debug.LogFormat("Server wants us to spawn a '{0}'", name);

                if (!serverObjects.ContainsKey(id))
                {
                    ServerObjectData sod             = serverSpawnables.GetObjectByName(name);
                    var spawnedObject                = Instantiate(sod.Prefab, networkContainer);
                    spawnedObject.transform.position = new Vector3(x, y, 0);
                    var ni = spawnedObject.GetComponent <NetworkIdentity>();
                    ni.SetControllerID(id);
                    ni.SetSocketReference(this);

                    //If bullet, apply direction as well
                    if (name == "Bullet")
                    {
                        float directionX = e.data["direction"]["x"].f;
                        float directionY = e.data["direction"]["y"].f;

                        float rot = Mathf.Atan2(directionY, directionX) * Mathf.Rad2Deg;
                        Vector3 currentRotation          = new Vector3(0, 0, rot - 90);
                        spawnedObject.transform.rotation = Quaternion.Euler(currentRotation);
                    }

                    serverObjects.Add(id, ni);
                }
            });

            On("serverUnspawn", (e) =>
            {
                string id          = e.data["id"].ToString().RemoveQuotes();
                NetworkIdentity ni = serverObjects[id];
                serverObjects.Remove(id);
                DestroyImmediate(ni.gameObject);
            });
        }