Beispiel #1
0
            public override object NodeToType(Type type, YamlNode node, YamlObjectSerializer serializer)
            {
                var mapping  = (YamlMappingNode)node;
                var nodeType = mapping.GetNode("type");

                switch (nodeType.AsString())
                {
                case SpriteLayerToggle.NAME:
                    var    keyString = mapping.GetNode("key").AsString();
                    object key;
                    if (_reflectionManager.TryParseEnumReference(keyString, out var @enum))
                    {
                        key = @enum;
                    }
                    else
                    {
                        key = keyString;
                    }
                    var layer = mapping.GetNode("layer").AsInt();
                    return(new SpriteLayerToggle(key, layer));

                default:
                    var visType = _reflectionManager.LooseGetType(nodeType.AsString());
                    if (!typeof(AppearanceVisualizer).IsAssignableFrom(visType))
                    {
                        throw new InvalidOperationException();
                    }
                    var vis = (AppearanceVisualizer)Activator.CreateInstance(visType);
                    vis.LoadData(mapping);
                    return(vis);
                }
            }
        private void AddBodyPart(BodyPart part, string slotName)
        {
            DebugTools.AssertNotNull(part);
            DebugTools.AssertNotNull(slotName);

            _parts.Add(slotName, part);

            part.Body = this;

            var argsAdded = new BodyPartAddedEventArgs(part, slotName);

            foreach (var component in Owner.GetAllComponents <IBodyPartAdded>().ToArray())
            {
                component.BodyPartAdded(argsAdded);
            }

            if (!Template.Layers.TryGetValue(slotName, out var partMap) ||
                !_reflectionManager.TryParseEnumReference(partMap, out var partEnum))
            {
                Logger.Warning($"Template {Template.Name} has an invalid RSI map key {partMap} for body part {part.Name}.");
                return;
            }

            part.RSIMap = partEnum;

            var partMessage = new BodyPartAddedMessage(part.RSIPath, part.RSIState, partEnum);

            SendNetworkMessage(partMessage);

            foreach (var mechanism in part.Mechanisms)
            {
                if (!Template.MechanismLayers.TryGetValue(mechanism.Id, out var mechanismMap))
                {
                    continue;
                }

                if (!_reflectionManager.TryParseEnumReference(mechanismMap, out var mechanismEnum))
                {
                    Logger.Warning($"Template {Template.Name} has an invalid RSI map key {mechanismMap} for mechanism {mechanism.Id}.");
                    continue;
                }

                var mechanismMessage = new MechanismSpriteAddedMessage(mechanismEnum);

                SendNetworkMessage(mechanismMessage);
            }
        }
        public async Task PerformAction(IEntity entity, IEntity?user)
        {
            if (string.IsNullOrEmpty(Key))
            {
                return;
            }

            if (entity.TryGetComponent(out AppearanceComponent? appearance))
            {
                if (_reflectionManager.TryParseEnumReference(Key, out var @enum))
                {
                    appearance.SetData(@enum, Data);
                }
                else
                {
                    appearance.SetData(Key, Data);
                }
            }
        }