Example #1
0
        public Actor CreateActor(bool addToWorld, string name, TypeDictionary initDict)
        {
            var a = new Actor(this, name, initDict);

            a.Initialize(addToWorld);
            return(a);
        }
Example #2
0
        public Player(World world, Session.Client client, PlayerReference pr, MersenneTwister playerRandom)
        {
            World           = world;
            InternalName    = pr.Name;
            PlayerReference = pr;

            inMissionMap = world.Map.Visibility.HasFlag(MapVisibility.MissionSelector);

            // Real player or host-created bot
            if (client != null)
            {
                ClientIndex = client.Index;
                Color       = client.Color;
                PlayerName  = ResolvePlayerName(client, world.LobbyInfo.Clients, world.Map.Rules.Actors["player"].TraitInfos <IBotInfo>());

                BotType        = client.Bot;
                Faction        = ResolveFaction(world, client.Faction, playerRandom, !pr.LockFaction);
                DisplayFaction = ResolveDisplayFaction(world, client.Faction);

                var assignSpawnPoints = world.WorldActor.TraitOrDefault <IAssignSpawnPoints>();
                HomeLocation      = assignSpawnPoints?.AssignHomeLocation(world, client, playerRandom) ?? pr.HomeLocation;
                SpawnPoint        = assignSpawnPoints?.SpawnPointForPlayer(this) ?? client.SpawnPoint;
                DisplaySpawnPoint = client.SpawnPoint;

                Handicap = client.Handicap;
            }
            else
            {
                // Map player
                ClientIndex    = 0;              // Owned by the host (TODO: fix this)
                Color          = pr.Color;
                PlayerName     = pr.Name;
                NonCombatant   = pr.NonCombatant;
                Playable       = pr.Playable;
                spectating     = pr.Spectating;
                BotType        = pr.Bot;
                Faction        = ResolveFaction(world, pr.Faction, playerRandom, false);
                DisplayFaction = ResolveDisplayFaction(world, pr.Faction);
                HomeLocation   = pr.HomeLocation;
                SpawnPoint     = DisplaySpawnPoint = 0;
                Handicap       = pr.Handicap;
            }

            if (!spectating)
            {
                PlayerMask = new LongBitSet <PlayerBitMask>(InternalName);
            }

            // Set this property before running any Created callbacks on the player actor
            IsBot = BotType != null;

            // Special case handling is required for the Player actor:
            // Since Actor.Created would be called before PlayerActor is assigned here
            // querying player traits in INotifyCreated.Created would crash.
            // Therefore assign the uninitialized actor and run the Created callbacks
            // by calling Initialize ourselves.
            var playerActorType = world.Type == WorldType.Editor ? EditorPlayerActorType : PlayerActorType;

            PlayerActor = new Actor(world, playerActorType, new TypeDictionary {
                new OwnerInit(this)
            });
            PlayerActor.Initialize(true);

            Shroud           = PlayerActor.Trait <Shroud>();
            FrozenActorLayer = PlayerActor.TraitOrDefault <FrozenActorLayer>();

            // Enable the bot logic on the host
            if (IsBot && Game.IsHost)
            {
                var logic = PlayerActor.TraitsImplementing <IBot>().FirstOrDefault(b => b.Info.Type == BotType);
                if (logic == null)
                {
                    Log.Write("debug", "Invalid bot type: {0}", BotType);
                }
                else
                {
                    logic.Activate(this);
                }
            }

            stanceColors.Self     = ChromeMetrics.Get <Color>("PlayerStanceColorSelf");
            stanceColors.Allies   = ChromeMetrics.Get <Color>("PlayerStanceColorAllies");
            stanceColors.Enemies  = ChromeMetrics.Get <Color>("PlayerStanceColorEnemies");
            stanceColors.Neutrals = ChromeMetrics.Get <Color>("PlayerStanceColorNeutrals");

            unlockRenderPlayer = PlayerActor.TraitsImplementing <IUnlocksRenderPlayer>().ToArray();
        }