Example #1
0
    public void OnEvent(EventData eventData)
    {
        //server raised an event
        switch ((StarCollectorEventTypes)eventData.Code)
        {
        // store player ID
        case StarCollectorEventTypes.ReceivePlayerID:
            long playerId = (long)eventData.Parameters[0];
            PlayerID = playerId;
            Debug.Log("Received player ID, awaiting game state...");
            break;

        // spawn actor
        case StarCollectorEventTypes.CreateActor:
            byte       actorType = (byte)eventData.Parameters[0];
            long       actorID   = (long)eventData.Parameters[1];
            float      posX      = (float)eventData.Parameters[2];
            float      posY      = (float)eventData.Parameters[3];
            GameObject actor     = null;
            switch (actorType)
            {
            // Star
            case 0:
                actor = (GameObject)Instantiate(StarPrefab, new Vector3(posX, 0f, posY), Quaternion.identity);
                break;

            // Player
            case 1:
                long ownerID = (long)eventData.Parameters[4];
                actor = (GameObject)Instantiate(PlayerPrefab, new Vector3(posX, 0f, posY), Quaternion.identity);
                actor.SendMessage("SetOwnerID", ownerID);
                break;
            }
            actor.SendMessage("SetActorID", actorID);
            break;

        // destroy actor
        case StarCollectorEventTypes.DestroyActor:
            GameActor destroyActor = GameActor.Actors[(long)eventData.Parameters[0]];
            if (destroyActor != null)
            {
                destroyActor.Destruct();
            }
            break;

        // update actor
        case StarCollectorEventTypes.UpdateActor:
            GameActor updateActor = GameActor.Actors[(long)eventData.Parameters[0]];
            float     newPosX     = (float)eventData.Parameters[1];
            float     newPosY     = (float)eventData.Parameters[2];
            updateActor.SendMessage("UpdatePosition", new Vector3(newPosX, 0f, newPosY), SendMessageOptions.DontRequireReceiver);
            break;

        // log chat messages
        case StarCollectorEventTypes.ChatMessage:
            Debug.Log((string)eventData.Parameters[0]);
            break;
        }
    }
    public void OnEvent(EventData eventData)
    {
        Debug.Log((AckEventType)eventData.Code);
        //server raised an event
        switch ((AckEventType)eventData.Code)
        {
        // store player ID
        case AckEventType.AssignPlayerID:
            IActor    player        = GeneralFunc.Deserialize <IActor>(eventData.Parameters[0] as byte[]);
            GameActor playerCreated = Instantiate
                                      (
                playerPref,
                new Vector3(player.posX, 0.5f, player.posY),
                playerPref.transform.rotation
                                      ) as GameActor;
            playerCreated.SetActor(player);
            playerCreated.GetComponent <OwnerInfo>().SetOwnerID(player.peerID);
            StarCollectorClient.playerID = player.peerID;
            playerCreated.GetComponent <CheckRegion>().Load();
            break;

        // create actor
        case AckEventType.CreateActor:
            if (eventData.Parameters.ContainsKey((byte)Parameter.Creep))
            {
                List <IActor> listAllCreep = GeneralFunc.Deserialize <List <IActor> >(eventData.Parameters[(byte)Parameter.Creep] as byte[]);
                Debug.Log("count all creep :" + listAllCreep.Count);
                List <IActor> listSameCreep = GetListSameActor(listAllCreep, GameActor.listCreep);

                List <IActor> listNewCreep = listAllCreep.Except(listSameCreep).ToList();

                List <GameActor> listDeleteCreep = GameActor.listCreep.Where(a => !listAllCreep.Any(b => b.actorID == a.actor.actorID)).ToList();

                foreach (var creepDelete in listDeleteCreep)
                {
                    creepDelete.Destruct();
                }

                foreach (var creep in listNewCreep)
                {
                    GameActor creepCreated = Instantiate(creepPref, new Vector3(creep.posX, 0.5f, creep.posY), playerPref.transform.rotation) as GameActor;
                    creepCreated.SetActor(creep);
                    creepCreated.GetComponent <CheckRegion>().Load();
                }
            }
            if (eventData.Parameters.ContainsKey((byte)Parameter.Regions))
            {
                InterestRegions dataInterestRegion = GeneralFunc.Deserialize <InterestRegions>(eventData.Parameters[(byte)Parameter.Regions] as byte[]);

                if (dataInterestRegion.playerPosition != null)
                {
                    Debug.Log(string.Format("player position -------------- x: {0} - y: {1}", dataInterestRegion.playerPosition.x, dataInterestRegion.playerPosition.y));
                }

                //foreach (var data in dataInterestRegion.listRegion)
                //{
                //    Debug.Log(string.Format("x: {0} - y: {1}",data.x,data.y));
                //}

                AreaController.Instance.ShowInterestRegion(dataInterestRegion.listRegion);
            }
            break;

        case AckEventType.DestroyActor:
            if (eventData.Parameters.ContainsKey((byte)Parameter.Data))
            {
                IActor    playerDestroy    = GeneralFunc.Deserialize <IActor>(eventData.Parameters[(byte)Parameter.Data] as byte[]);
                GameActor gameActorDestroy = GameActor.listPlayer.Where(a => a.actor.actorID == playerDestroy.actorID).FirstOrDefault();
                if (gameActorDestroy != null)
                {
                    gameActorDestroy.Destruct();
                }
            }
            break;

        // log chat messages
        case AckEventType.ChatMessage:
            break;
        }
    }