AddActor() private method

private AddActor ( Actor, _actor ) : void
_actor Actor,
return void
Esempio n. 1
0
    public void RunService(int serverPort)
    {
        if (m_actorManager != null)
        {
            return;
        }

        m_actorManager = new ActorManager();

        m_watchDogActor = new WatchDogActor(this);
        //Id为1的Actor, 不与Agent绑定,也没有U3DId标识
        m_actorManager.AddActor(m_watchDogActor, true);

        m_worldActor = new WorldActor(this);
        m_watchDogActor.WorldActor = m_worldActor;
        //Id为2的Actor, 不与Agent绑定,也没有U3DId标识
        m_actorManager.AddActor(m_worldActor, true);

        m_serverActor = new ServerActor(this);
        m_watchDogActor.ServerActor = m_serverActor;
        m_serverActor.WatchDogActor = m_watchDogActor;
        //Id为3的Actor, 不与Agent绑定,也没有U3DId标识
        m_actorManager.AddActor(m_serverActor, true);

        //启动服务器
        m_serverActor.Run(serverPort);
    }
Esempio n. 2
0
 public IActionResult Create(Actor actor)
 {
     if (ModelState.IsValid)
     {
         actorManager.AddActor(actor);
         TempData["message"] = $"Se ha agregado el actor '{actor.Name}' correctamente";
         return(RedirectToAction("Index"));
     }
     return(View());
 }
        private void ProcessCreatedActors(CreateActor originalMessage, IList <Actor> createdActors, Action onCompleteCallback)
        {
            var guids     = new DeterministicGuids(originalMessage.Actor?.Id);
            var rootActor = createdActors.FirstOrDefault();

            if (rootActor.transform.parent == null)
            {
                // Delete entire hierarchy as we no longer have a valid parent actor for the root of this hierarchy.  It was likely
                // destroyed in the process of the async operation before this callback was called.
                foreach (var actor in createdActors)
                {
                    actor.Destroy();
                }

                createdActors.Clear();

                SendCreateActorResponse(
                    originalMessage,
                    failureMessage: "Parent for the actor being created no longer exists.  Cannot create new actor.");
                return;
            }

            ProcessActors(rootActor.transform, rootActor.transform.parent.GetComponent <Actor>());

            rootActor?.ApplyPatch(originalMessage.Actor);
            Actor.ApplyVisibilityUpdate(rootActor);

            _actorManager.UponStable(
                () => SendCreateActorResponse(originalMessage, actors: createdActors, onCompleteCallback: onCompleteCallback));

            void ProcessActors(Transform xfrm, Actor parent)
            {
                // Generate actors for all GameObjects, even if the loader didn't. Only loader-generated
                // actors are returned to the app though. We do this so library objects get enabled/disabled
                // correctly, even if they're not tracked by the app.
                var actor = xfrm.gameObject.GetComponent <Actor>() ?? xfrm.gameObject.AddComponent <Actor>();

                _actorManager.AddActor(guids.Next(), actor);
                _ownedGameObjects.Add(actor.gameObject);

                actor.ParentId = parent?.Id ?? actor.ParentId;
                if (actor.Renderer != null)
                {
                    actor.MaterialId = MREAPI.AppsAPI.AssetCache.GetId(actor.Renderer.sharedMaterial) ?? Guid.Empty;
                    actor.MeshId     = MREAPI.AppsAPI.AssetCache.GetId(actor.UnityMesh) ?? Guid.Empty;
                }

                foreach (Transform child in xfrm)
                {
                    ProcessActors(child, actor);
                }
            }
        }
        private void ProcessCreatedActors(CreateActor originalMessage, IList <Actor> createdActors, Action onCompleteCallback)
        {
            var guids     = new DeterministicGuids(originalMessage.Actor?.Id);
            var rootActor = createdActors.FirstOrDefault();

            ProcessActors(rootActor.transform, rootActor.transform.parent.GetComponent <Actor>());

            rootActor?.ApplyPatch(originalMessage.Actor);
            Actor.ApplyVisibilityUpdate(rootActor);

            foreach (var actor in createdActors)
            {
                actor.AddSubscriptions(originalMessage.Subscriptions);
            }

            SendCreateActorResponse(originalMessage, actors: createdActors, onCompleteCallback: onCompleteCallback);

            void ProcessActors(Transform xfrm, Actor parent)
            {
                // Generate actors for all GameObjects, even if the loader didn't. Only loader-generated
                // actors are returned to the app though. We do this so library objects get enabled/disabled
                // correctly, even if they're not tracked by the app.
                var actor = xfrm.gameObject.GetComponent <Actor>() ?? xfrm.gameObject.AddComponent <Actor>();

                _actorManager.AddActor(guids.Next(), actor);
                _ownedGameObjects.Add(actor.gameObject);

                actor.ParentId = parent?.Id ?? actor.ParentId;
                if (actor.Renderer != null)
                {
                    actor.MaterialId = MREAPI.AppsAPI.AssetCache.GetId(actor.Renderer.sharedMaterial) ?? Guid.Empty;
                }

                foreach (Transform child in xfrm)
                {
                    ProcessActors(child, actor);
                }
            }
        }
Esempio n. 5
0
        public void AddActorToDB()
        {
            // Arrange
            Actor aniston = new Actor()
            {
                Id = 1, Name = "Jennifer Aniston"
            };

            var context = new AppContext();

            if (context.Database.GetPendingMigrations().Any())
            {
                context.Database.Migrate();
            }
            ActorRepository repo    = new ActorRepository(context);
            ActorManager    manager = new ActorManager(repo);

            // Act
            manager.AddActor(aniston);

            // Assert
            Assert.Single(manager.GetAllActors());
        }
        private void ProcessCreatedActors(CreateActor originalMessage, IList <Actor> createdActors, Action onCompleteCallback, string guidSeed = null)
        {
            Guid guidGenSeed;

            if (originalMessage != null)
            {
                guidGenSeed = originalMessage.Actor.Id;
            }
            else
            {
                guidGenSeed = UtilMethods.StringToGuid(guidSeed);
            }
            var guids = new DeterministicGuids(guidGenSeed);

            // find the actors with no actor parents
            var rootActors = GetDistinctTreeRoots(
                createdActors.Select(a => a.gameObject).ToArray()
                ).Select(go => go.GetComponent <Actor>()).ToArray();

            var rootActor    = createdActors.FirstOrDefault();
            var createdAnims = new List <Animation.BaseAnimation>(5);

            if (rootActors.Length == 1 && rootActor.transform.parent == null)
            {
                // Delete entire hierarchy as we no longer have a valid parent actor for the root of this hierarchy.  It was likely
                // destroyed in the process of the async operation before this callback was called.
                foreach (var actor in createdActors)
                {
                    actor.Destroy();
                }

                createdActors.Clear();

                SendCreateActorResponse(
                    originalMessage,
                    failureMessage: "Parent for the actor being created no longer exists.  Cannot create new actor.");
                return;
            }

            var secondPassXfrms = new List <Transform>(2);

            foreach (var root in rootActors)
            {
                ProcessActors(root.transform, root.transform.parent != null ? root.transform.parent.GetComponent <Actor>() : null);
            }
            // some things require the whole hierarchy to have actors on it. run those here
            foreach (var pass2 in secondPassXfrms)
            {
                ProcessActors2(pass2);
            }

            if (originalMessage != null && rootActors.Length == 1)
            {
                rootActor?.ApplyPatch(originalMessage.Actor);
            }
            Actor.ApplyVisibilityUpdate(rootActor);

            _actorManager.UponStable(
                () => SendCreateActorResponse(originalMessage, actors: createdActors, anims: createdAnims, onCompleteCallback: onCompleteCallback));

            void ProcessActors(Transform xfrm, Actor parent)
            {
                // Generate actors for all GameObjects, even if the loader didn't. Only loader-generated
                // actors are returned to the app though. We do this so library objects get enabled/disabled
                // correctly, even if they're not tracked by the app.
                var actor = xfrm.gameObject.GetComponent <Actor>() ?? xfrm.gameObject.AddComponent <Actor>();

                _actorManager.AddActor(guids.Next(), actor);
                _ownedGameObjects.Add(actor.gameObject);

                actor.ParentId = parent?.Id ?? actor.ParentId;
                if (actor.Renderer != null)
                {
                    actor.MaterialId = AssetCache.GetId(actor.Renderer.sharedMaterial) ?? Guid.Empty;
                    actor.MeshId     = AssetCache.GetId(actor.UnityMesh) ?? Guid.Empty;
                }

                // native animation construction requires the whole actor hierarchy to already exist. defer to second pass
                var nativeAnim = xfrm.gameObject.GetComponent <UnityEngine.Animation>();

                if (nativeAnim != null && createdActors.Contains(actor))
                {
                    secondPassXfrms.Add(xfrm);
                }

                foreach (Transform child in xfrm)
                {
                    ProcessActors(child, actor);
                }
            }

            void ProcessActors2(Transform xfrm)
            {
                var actor      = xfrm.gameObject.GetComponent <Actor>();
                var nativeAnim = xfrm.gameObject.GetComponent <UnityEngine.Animation>();

                if (nativeAnim != null && createdActors.Contains(actor))
                {
                    var animTargets = xfrm.gameObject.GetComponent <PrefabAnimationTargets>();
                    int stateIndex  = 0;
                    foreach (AnimationState state in nativeAnim)
                    {
                        var anim = new NativeAnimation(AnimationManager, guids.Next(), nativeAnim, state);
                        anim.TargetIds = animTargets != null
                                                        ? animTargets.GetTargets(xfrm, stateIndex ++, addRootToTargets : true).Select(a => a.Id).ToList()
                                                        : new List <Guid>()
                        {
                            actor.Id
                        };

                        AnimationManager.RegisterAnimation(anim);
                        createdAnims.Add(anim);
                    }
                }
            }
        }
        private void ProcessCreatedActors(CreateActor originalMessage, IList <Actor> createdActors, Action onCompleteCallback, string guidSeed = null)
        {
            Guid guidGenSeed;

            if (originalMessage != null)
            {
                guidGenSeed = originalMessage.Actor.Id;
            }
            else
            {
                guidGenSeed = UtilMethods.StringToGuid(guidSeed);
            }
            var guids = new DeterministicGuids(guidGenSeed);

            // find the actors with no actor parents
            var rootActors = GetDistinctTreeRoots(
                createdActors.ToArray()
                ).Select(go => go as Actor).ToArray();
            var rootActor    = createdActors.FirstOrDefault();
            var createdAnims = new List <Animation.BaseAnimation>(5);

            if (rootActors.Length == 1 && rootActor.GetParent() == null)
            {
                // Delete entire hierarchy as we no longer have a valid parent actor for the root of this hierarchy.  It was likely
                // destroyed in the process of the async operation before this callback was called.
                foreach (var actor in createdActors)
                {
                    actor.Destroy();
                }

                createdActors.Clear();

                SendCreateActorResponse(
                    originalMessage,
                    failureMessage: "Parent for the actor being created no longer exists.  Cannot create new actor.");
                return;
            }

            var secondPassXfrms = new List <Spatial>(2);

            foreach (var root in rootActors)
            {
                ProcessActors(root.Node3D, root.GetParent() as Actor);
            }
            // some things require the whole hierarchy to have actors on it. run those here
            foreach (var pass2 in secondPassXfrms)
            {
                ProcessActors2(pass2);
            }

            if (originalMessage != null && rootActors.Length == 1)
            {
                rootActor?.ApplyPatch(originalMessage.Actor);
            }
            Actor.ApplyVisibilityUpdate(rootActor);

            _actorManager.UponStable(
                () => SendCreateActorResponse(originalMessage, actors: createdActors, anims: createdAnims, onCompleteCallback: onCompleteCallback));

            void ProcessActors(Spatial node3D, Actor parent)
            {
                // Generate actors for all node3D, even if the loader didn't. Only loader-generated
                // actors are returned to the app though. We do this so library objects get enabled/disabled
                // correctly, even if they're not tracked by the app.
                Actor actor = (node3D as Actor) ?? Actor.Instantiate(node3D);

                _actorManager.AddActor(guids.Next(), actor);
                _ownedNodes.Add(actor);

                actor.ParentId = parent?.Id ?? actor.ParentId;

                if (actor.MeshInstance != null)
                {
                    // only overwrite material if there's something in the cache, i.e. not a random library material
                    if (actor.MeshInstance.MaterialOverride != null)
                    {
                        var matId = AssetManager.GetByObject(actor.MeshInstance.MaterialOverride)?.Id;
                        if (matId.HasValue)
                        {
                            actor.MaterialId = matId.Value;
                        }
                    }

                    actor.MeshId = AssetManager.GetByObject(actor.GodotMesh)?.Id ?? Guid.Empty;
                }

                // native animation construction requires the whole actor hierarchy to already exist. defer to second pass
                var nativeAnim = node3D.GetChild <Godot.AnimationPlayer>();

                if (nativeAnim != null && createdActors.Contains(actor))
                {
                    secondPassXfrms.Add(node3D);
                }

                foreach (object node in actor.GetChildren())
                {
                    if (node is Spatial)
                    {
                        ProcessActors((Spatial)node, actor);
                    }
                }
            }

            void ProcessActors2(Spatial node3D)
            {
                var actor           = node3D as Actor;
                var animationPlayer = node3D.GetChild <Godot.AnimationPlayer>();

                if (animationPlayer != null && createdActors.Contains(actor))
                {
                    var animTargets = node3D.GetChild <PrefabAnimationTargets>();
                    int animIndex   = 0;
                    foreach (string animationString in animationPlayer.GetAnimationList())
                    {
                        var anim = new NativeAnimation(AnimationManager, guids.Next(), animationPlayer, animationPlayer.GetAnimation(animationString));
                        anim.TargetIds = animTargets != null
                                                        ? animTargets.GetTargets(node3D, animIndex ++, addRootToTargets : true).Select(a => a.Id).ToList()
                                                        : new List <Guid>()
                        {
                            actor.Id
                        };

                        AnimationManager.RegisterAnimation(anim);
                        createdAnims.Add(anim);
                    }
                }
            }
        }
Esempio n. 8
0
    private void OnActorAdd(SocketAsyncEventArgs args, byte[] bytes)
    {
        ActorAdd input = ActorAdd.Parser.ParseFrom(bytes);

        // 这条消息是在房间内部接收的,所以要判断这条消息是否属于这个房间。如果放在外部判断,可以通过外部的函数找到该房间,然后直接调用该房间的函数
        // 放在哪里都可以,目前放在这里是因为可以测试一下,同一条消息,多个函数注册是否会有BUG
        if (input.RoomId != _roomId)
        {
            return;
        }

        var piir = GetPlayerInRoom(input.OwnerId);

        if (piir == null)
        {
            ActorAddReply output = new ActorAddReply()
            {
                Ret = false,
            };
            ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.ActorAddReply, output.ToByteArray());
            ServerRoomManager.Instance.Log($"MSG: ActorAdd - Player not found in server! Id:{input.OwnerId}"); // 当前玩家不在本本战场!
        }
        else
        {
            if (input.CellIndex == 0)
            {
                Debug.LogError("OnActorAdd F**k!!! Actor position is lost!!!");
            }
            ActorBehaviour ab = new ActorBehaviour()
            {
                RoomId      = input.RoomId,
                OwnerId     = input.OwnerId,
                ActorId     = input.ActorId,
                PosX        = input.PosX,
                PosZ        = input.PosZ,
                CellIndex   = input.CellIndex,
                Orientation = input.Orientation,
                Species     = input.Species,
                ActorInfoId = input.ActorInfoId,
            };
            ab.LoadFromTable();
            ActorManager.AddActor(ab, this);

            // 转发给房间内的所有玩家
            ActorAddReply output = new ActorAddReply()
            {
                RoomId      = ab.RoomId,
                OwnerId     = ab.OwnerId,
                ActorId     = ab.ActorId,
                Orientation = ab.Orientation,
                PosX        = ab.PosX,
                PosZ        = ab.PosZ,
                CellIndex   = ab.CellIndex,
                Species     = ab.Species,
                ActorInfoId = ab.ActorInfoId,

                Name          = ab.Name,
                Hp            = ab.Hp,
                HpMax         = ab.HpMax,
                AttackPower   = ab.AttackPower,
                DefencePower  = ab.DefencePower,
                Speed         = ab.Speed,
                FieldOfVision = ab.FieldOfVision,
                ShootingRange = ab.ShootingRange,

                AttackDuration = ab.AttackDuration,
                AttackInterval = ab.AttackInterval,
                AmmoBase       = ab.AmmoBase,
                AmmoBaseMax    = ab.AmmoBaseMax,

                Ret = true,
            };
            BroadcastMsg(ROOM_REPLY.ActorAddReply, output.ToByteArray());
        }
    }