Example #1
0
        void EventReceiveData.IEv.Callback(EventReceiveData.Arguments args)
        {
            if (args.Reader.Type != MessageType.Pattern)
            {
                return;
            }

            var conPatternMgr = args.PeerInstance.GetPatternManager();
            var conEntityMgr  = args.PeerInstance.Get <ConnectionEntityManager>();
            var msgPattern    = conPatternMgr.GetPattern(args.Reader);

            if (msgPattern == SendDoJumpMsgId)
            {
                var entity = args.Reader.GetEntity();

                if (!EntityManager.Exists(entity))
                {
                    //TODO: Disconnect player
                    Debug.Log($"Invalid entity ({entity.Index}, {entity.Version}) ! TODO: Disconnect Player");
                    return;
                }

                EntityJump(entity, true);
            }
        }
        // -------------------------------------------------------- //
        // Callbacks
        // -------------------------------------------------------- //
        public void Callback(EventReceiveData.Arguments args)
        {
            if (args.Reader.Type != MessageType.Pattern || args.Caller.SelfHost)
            {
                return;
            }

            var caller       = args.Caller;
            var peerInstance = args.PeerInstance;
            var reader       = args.Reader;

            var conPatternMgr = peerInstance.GetPatternManager();
            var conEntityMgr  = peerInstance.Get <ConnectionEntityManager>();

            var msgId = conPatternMgr.GetPattern(reader);

            if (msgId == MsgUpdateVelocity)
            {
                var packet = new VelocityPacket(reader.Data);
                SetFromPacket(packet, conEntityMgr);
            }
            else if (msgId == MsgMassUpdateVelocity)
            {
                var length = reader.Data.GetInt();
                for (int i = 0; i != length; i++)
                {
                    var packet = new VelocityPacket(reader.Data);
                    SetFromPacket(packet, conEntityMgr);
                }
            }
        }
        void EventReceiveData.IEv.Callback(EventReceiveData.Arguments args)
        {
            if (args.Reader.Type != MessageType.Pattern)
            {
                return;
            }

            var conPatternMgr = args.PeerInstance.GetPatternManager();
            var conEntityMgr  = args.PeerInstance.Get <ConnectionEntityManager>();
            var msgPattern    = conPatternMgr.GetPattern(args.Reader);

            if (msgPattern == InputMsgToServerId)
            {
                var inputPacket = new InputPacket(args.Reader.Data);
                var entity      = inputPacket.Entity;

                if (!EntityManager.Exists(entity))
                {
                    //TODO: Disconnect player
                    Debug.Log($"Invalid entity ({entity.Index}, {entity.Version}) ! TODO: Disconnect Player");
                    return;
                }

                EntityUpdateInput(entity, new DefStRunInput(inputPacket.Timestamp, inputPacket.Direction), true);
            }
            else if (msgPattern == InputMsgToClientsId)
            {
                var inputPacket = new InputPacket(args.Reader.Data);
                var entity      = inputPacket.Entity;

                if (!conEntityMgr.HasEntity(entity))
                {
                    //TODO: Disconnect from server
                    Debug.Log($"Invalid entity ({entity.Index}, {entity.Version}) ! TODO: Disconnect from server.");
                    return;
                }

                entity = conEntityMgr.GetEntity(entity);

                EntityUpdateInput(entity, new DefStRunInput(inputPacket.Timestamp, inputPacket.Direction), false);
            }
        }
Example #4
0
        void EventReceiveData.IEv.Callback(EventReceiveData.Arguments args)
        {
            if (args.Reader.Type != MessageType.Pattern)
            {
                return;
            }

            var conMsgMgr    = args.PeerInstance.GetPatternManager();
            var conEntityMgr = args.PeerInstance.Get <ConnectionEntityManager>();

            var msg = conMsgMgr.GetPattern(args.Reader);

            if (msg == DefaultNetTransformConstants.MsgUpdatePosition)
            {
                var timestamp = args.Reader.Data.GetFloat();
                var length    = args.Reader.Data.GetInt();

                for (int i = 0; i != length; i++)
                {
                    ReadEntityPosition(i, timestamp, conEntityMgr, args.Reader, args.PeerInstance);
                }
            }
            else if (msg == DefaultNetTransformConstants.MsgUpdateRotation)
            {
                var entity = conEntityMgr.GetEntity(args.Reader.GetEntity());
                var x      = args.Reader.Data.GetFloat();
                var y      = args.Reader.Data.GetFloat();
                var z      = args.Reader.Data.GetFloat();
                var w      = args.Reader.Data.GetFloat();

                entity.SetOrAddComponentData(new NetRotation()
                {
                    Value = math.quaternion(x, y, z, w)
                });
            }
        }
        void EventReceiveData.IEv.Callback(EventReceiveData.Arguments args)
        {
            var caller       = args.Caller;
            var peerInstance = args.PeerInstance;
            var reader       = args.Reader;

            if (caller != NetInstance || reader.Type != MessageType.Pattern)
            {
                return;
            }

            var peerPatternMgr = peerInstance.GetPatternManager();
            var msg            = peerPatternMgr.GetPattern(reader);

            var peerNetId     = peerInstance.Global.Id;
            var conPlayerBank = peerInstance.Get <ConnectionPlayerBank>();
            var plBank        = MainWorld.GetOrCreateManager <GamePlayerBank>();
            var em            = MainWorld.GetOrCreateManager <EntityManager>();

            if (msg == m_MsgUpdatePlayer)
            {
                var playerId  = reader.Data.GetEntity();
                var userIndex = reader.Data.GetULong();
                var masterId  = reader.Data.GetIdent128();
                var owned     = reader.Data.GetBool();

                Debug.Log($"Update player ({playerId.Index}) {userIndex} {masterId}.");

                var player = plBank.GetPlayerFromIdent(masterId);
                if (!player.IsCreated)
                {
                    player = new GamePlayer(em.CreateEntity());
                    var masterServerPlayerId = new MasterServerPlayerId()
                    {
                        Id = masterId
                    };

                    player.WorldPointer.SetOrAddSharedComponentData(masterServerPlayerId, MainWorld);
                    plBank.AddPlayer(masterServerPlayerId, player);
                }
                else
                {
                    var masterServerPlayerId = new MasterServerPlayerId()
                    {
                        Id = masterId
                    };
                    player.WorldPointer.SetOrAddSharedComponentData(masterServerPlayerId, MainWorld);
                }

                conPlayerBank.RegisterPlayer(StMath.DoubleIntToLong(playerId.Index, playerId.Version), player);

                player.WorldPointer.SetOrAddSharedComponentData
                (
                    new PlayerPeerLink(caller.PeerInstance, peerInstance.Peer),
                    MainWorld
                );
                player.WorldPointer.SetOrAddSharedComponentData
                (
                    new PlayerUserLink(peerInstance.Id, peerNetId, userIndex),
                    MainWorld
                );
                player.WorldPointer.SetOrAddComponentData(new ConnectedPlayerEntity(), MainWorld);
            }
            else if (msg == m_MsgRemovePlayer)
            {
                var playerId = reader.Data.GetInt();
                var player   = m_ConnectionPlayerBank.Get(playerId);
                if (player.IsCreated)
                {
                    em.RemoveComponent <PlayerPeerLink>(player.WorldPointer);
                    em.RemoveComponent <PlayerUserLink>(player.WorldPointer);
                    em.RemoveComponent <ConnectedPlayerEntity>(player.WorldPointer);

                    conPlayerBank.UnregisterPlayer(playerId);

                    Debug.Log("player disconnected!");
                }
                else
                {
                    Debug.LogError("Data incoherence (player.IsCreated == false)");
                }
            }
        }
Example #6
0
        public void Callback(EventReceiveData.Arguments args)
        {
            if (!args.Reader.Type.IsPattern())
            {
                return;
            }

            var conMsgMgr = args.PeerInstance.GetPatternManager();

            var msg = conMsgMgr.GetPattern(args.Reader);
            var ecb = new EntityCommandBuffer(Allocator.Temp);

            if (msg == MsgTestCreateProj)
            {
                var position = args.Reader.Data.GetVec3();
                var aim      = args.Reader.Data.GetVec3();
                var proj     = CreateProjectile(ecb);

                EntityManager.SetComponentData(proj, new Position {
                    Value = position
                });
                EntityManager.SetComponentData(proj, new StVelocity(aim * 42f));

                Debug.Log($"Creating a new projectile. P: {position}; A: {aim}");

                SendProjectile(proj);
            }
            else if (msg == MsgStartProjectile)
            {
                var serverEntity = args.Reader.GetEntity();
                var owner        = default(Entity);
                var position     = args.Reader.Data.GetVec3();
                var velocity     = args.Reader.Data.GetVec3();

                var clientEntity = EntityManager.CreateEntity(m_EntityArchetype);

                ServerEntityMgr.ConvertAsNetworkable(ecb, clientEntity, serverEntity);

                EntityManager.SetOrAddComponentData(clientEntity, new ProjectileTag());
                EntityManager.SetOrAddComponentData(clientEntity, new ProjectileOwner(owner));
                EntityManager.SetOrAddComponentData(clientEntity, new Position {
                    Value = position
                });
                EntityManager.SetOrAddComponentData(clientEntity, new StVelocity(velocity));

                Debug.Log("We got a new projectile from server!");
            }
            else if (msg == MsgEndProjectile)
            {
                var serverEntity = args.Reader.GetEntity();
                var clientEntity = GetEntity(serverEntity);

                Debug.Log($"Destroying projectile... c: {clientEntity}, s: {serverEntity}");

                var projGO = ClientProjectileRocketExplosion[0];
                projGO.transform.position = clientEntity.GetComponentData <Position>().Value;
                projGO.GetComponentInChildren <ParticleSystem>().Play(true);

                ServerEntityMgr.DestroyEntity(serverEntity);
                ecb.DestroyEntity(clientEntity);
            }

            ecb.Playback(EntityManager);
            ecb.Dispose();
        }