Esempio n. 1
0
        public DebugPortableItem(ITypeResolver resolver, Vector2 pos)
            : base(resolver, pos)
        {
            portable = new PortableProperty(this, 10, 10);

            AddProperty(portable);
        }
Esempio n. 2
0
        public DebugPortableItem(ITypeResolver resolver, Vector2 pos)
            : base(resolver, pos)
        {
            portable = new PortableProperty(this, 10, 10);

            AddProperty(portable);
        }
Esempio n. 3
0
        public PhysicsUnit(Item item, Dictionary <int, PhysicsUnit> items)
        {
            this.item  = item;
            this.items = items;
            moving     = item.GetProperty <WalkingProperty>();
            collidable = item.GetProperty <CollidableProperty>();
            carrier    = item.GetProperty <CarrierProperty>();
            portable   = item.GetProperty <PortableProperty>();

            map = item.Engine.Map;

            mapSize = map.GetSize();

            // Attach Item Stuff
            item.CellChanged += item_CellChanged;

            // Attach Moving Stuff
            if (moving != null)
            {
                moving.OnMaximumMoveSpeedChanged += moving_OnMaximumMoveSpeedChanged;
                moving.OnMoveDirectionChanged    += moving_OnMoveDirectionChanged;
                moving.OnMoveSpeedChanged        += moving_OnMoveSpeedChanged;
                moving.MoveMalus = 1;
            }

            // Attach Collision Stuff
            if (collidable != null)
            {
                collidable.OnCollisionMassChanged  += collidable_OnCollisionMassChanged;
                collidable.OnCollisionFixedChanged += collidable_OnCollisionFixedChanged;
            }

            // Attach Carrier Stuff
            if (carrier != null)
            {
                carrier.OnCarrierLoadChanged     += carrier_OnCarrierLoadChanged;
                carrier.OnCarrierStrengthChanged += carrier_OnCarrierStrengthChanged;
            }

            // Attach Portable Stuff
            if (portable != null)
            {
                portable.OnPortableWeightChanged += portable_OnPortableMassChanged;
                portable.OnNewCarrierItem        += portable_OnNewCarrierItem;
                portable.OnLostCarrierItem       += portable_OnLostCarrierItem;
                clusterUnits = new HashSet <PhysicsUnit>();
            }

            Recalc();
        }
Esempio n. 4
0
        public bool Carry(Item item)
        {
            if (item == null)
            {
                return(false);
            }

            if (!item.ContainsProperty <PortableProperty>())
            {
                return(false);
            }

            PortableProperty portable = item.GetProperty <PortableProperty>();

            return(carrier.Carry(portable));
        }
Esempio n. 5
0
        /// <summary>
        ///     Das tragende Teil wurde geändert - Abhängigkeiten neu prüfen
        /// </summary>
        /// <param name="item"></param>
        /// <param name="newValue"></param>
        private void carrier_OnCarrierLoadChanged(Item item, PortableProperty newValue)
        {
            if (newValue != null)
            {
                int id = newValue.Item.Id;
                if (!items.ContainsKey(id))
                {
                    throw new IndexOutOfRangeException("Item does not exist");
                }

                load = items[id];
            }
            else
            {
                load = null;
            }

            Recalc();
        }
Esempio n. 6
0
        /// <summary>
        /// Registers Apples
        /// </summary>
        /// <param name="typeMapper">Type Mapper</param>
        /// <param name="settings">Settings</param>
        private void RegisterApple(ITypeMapper typeMapper, KeyValueStore settings)
        {
            // Apple
            typeMapper.RegisterItem <AppleItem, AppleState, AppleInfo>(this, "Apple");

            // Collidable
            settings.Set <AppleItem>("Mass", 200f, "Mass of an Apple");
            typeMapper.AttachItemProperty <AppleItem, CollidableProperty>(this, "Apple Collidable", (i) =>
            {
                CollidableProperty property = new CollidableProperty(i);

                // Define Radius
                property.CollisionRadius = AppleItem.AppleInnerRadius;

                // Define Mass
                property.CollisionFixed = false;
                property.CollisionMass  = i.Settings.GetFloat <AppleItem>("Mass").Value;

                return(property);
            });

            // Visibility
            typeMapper.AttachItemProperty <AppleItem, VisibleProperty>(this, "Apple Visible", (i) =>
            {
                VisibleProperty property = new VisibleProperty(i);

                // Bind Visibility Radius to the Item Radius
                property.VisibilityRadius = i.Radius;
                i.RadiusChanged          += (item, v) => { property.VisibilityRadius = v; };

                return(property);
            });

            // Portable
            settings.Set <AppleItem>("Weight", 200f, "Weight of an Apple");
            typeMapper.AttachItemProperty <AppleItem, PortableProperty>(this, "Apple Portable", (i) =>
            {
                PortableProperty property = new PortableProperty(i);

                // Set Weight
                property.PortableWeight = settings.GetFloat <AppleItem>("Weight").Value;

                // Bind Portable Radius to the Item Radius
                property.PortableRadius = i.Radius;
                i.RadiusChanged        += (item, v) => { property.PortableRadius = v; };

                return(property);
            });

            settings.Set <AppleItem>("Collectable", false, "Will an Apple be collectable");
            settings.Set <AppleItem>("Amount", 250, "Amount of Apple Units");
            typeMapper.AttachItemProperty <AppleItem, AppleCollectableProperty>(this, "Apple Collectable", (i) =>
            {
                if (!i.Settings.GetBool <AppleItem>("Collectable").Value)
                {
                    return(null);
                }

                AppleCollectableProperty property = new AppleCollectableProperty(i);
                property.Capacity = i.Settings.GetInt <AppleItem>("Amount").Value;
                property.Amount   = i.Settings.GetInt <AppleItem>("Amount").Value;
                return(property);
            });
        }
        /// <summary>
        /// Visit the given builder <c>T</c> and optionally mutate it using
        /// information contained within the given Type.
        /// </summary>
        /// <param name="builder">
        /// The builder being visited.
        /// </param>
        /// <param name="type">
        /// The Type used to enrich the builder.
        /// </param>
        public virtual void Visit(T builder, Type type)
        {
            Portable portable = Attribute.GetCustomAttribute(type, typeof(Portable)) as Portable;

            // fast escape switch
            if (portable == null)
            {
                return;
            }

            // get header level information
            bool autoIndex = m_autoIndex;

            builder.SetType(type);

            // BindingFlags for member access
            const BindingFlags bindings = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;

            // get property level information
            PropertyInfo[] props          = type.GetProperties(bindings);
            IList <string> fieldsExcluded = new List <string>(props.Length);

            for (int i = 0; i < props.Length; ++i)
            {
                PropertyInfo     prop      = props[i];
                PortableProperty attribute = Attribute.GetCustomAttribute(prop, typeof(PortableProperty)) as PortableProperty;
                if (attribute == null)
                {
                    continue;
                }

                string propName = prop.Name;
                string mangled  = NameManglers.PROPERTY_MANGLER.Mangle(propName);
                ICodec codec    = Codecs.GetCodec(attribute.Codec);

                if (!autoIndex && attribute.Index < 0)
                {
                    throw new ArgumentException("A POF Index must be specified for the property "
                                                + type.Name + "#" + propName + " by specifying "
                                                + "within the annotation or enabling auto indexing");
                }

                builder.AddAttribute(
                    builder.NewAttribute()
                    .SetName(mangled)
                    .SetCodec(codec)
                    .SetInvocationStrategy(new IS.PropertyInvcationStrategy <TB>(prop))
                    .SetIndex(attribute.Index).Build());

                // field level annotations take precedence over accessor annotations
                fieldsExcluded.Add(mangled);
            }

            // get field level information
            FieldInfo[] fields = type.GetFields(bindings);

            for (int i = 0; i < fields.Length; ++i)
            {
                FieldInfo        field     = fields[i];
                PortableProperty attribute = Attribute.GetCustomAttribute(field, typeof(PortableProperty)) as PortableProperty;
                if (attribute == null)
                {
                    continue;
                }

                string fieldName = field.Name;
                string mangled   = NameManglers.FIELD_MANGLER.Mangle(fieldName);
                ICodec codec     = Codecs.GetCodec(attribute.Codec);

                if (!autoIndex && attribute.Index < 0)
                {
                    throw new ArgumentException("A POF Index must be specified for the property "
                                                + type.Name + "#" + fieldName + " by specifying "
                                                + "within the annotation or enabling auto indexing");
                }

                builder.AddAttribute(
                    builder.NewAttribute()
                    .SetName(mangled)
                    .SetCodec(codec)
                    .SetInvocationStrategy(new IS.FieldInvcationStrategy <TB>(field))
                    .SetIndex(attribute.Index).Build());

                // field level annotations take precedence over accessor annotations
                fieldsExcluded.Add(mangled);
            }

            // get method level information
            MethodInfo[] methods = type.GetMethods(bindings);
            for (int i = 0; i < methods.Length; ++i)
            {
                MethodInfo       method    = methods[i];
                PortableProperty attribute = Attribute.GetCustomAttribute(method, typeof(PortableProperty)) as PortableProperty;
                if (attribute == null)
                {
                    continue;
                }

                string methodName = method.Name;
                if (methodName.StartsWith("Get") || methodName.StartsWith("Set") ||
                    methodName.StartsWith("Is"))
                {
                    string mangled = NameManglers.METHOD_MANGLER.Mangle(methodName);
                    if (fieldsExcluded.Contains(mangled))
                    {
                        continue;
                    }

                    ICodec codec = Codecs.GetCodec(attribute.Codec);
                    if (!autoIndex && attribute.Index < 0)
                    {
                        throw new ArgumentException("A POF Index must be specified for the method "
                                                    + type.Name + "#" + methodName + " by specifying "
                                                    + "within the annotation or enabling auto indexing");
                    }

                    builder.AddAttribute(
                        builder.NewAttribute()
                        .SetName(mangled)
                        .SetCodec(codec)
                        .SetInvocationStrategy(new IS.MethodInvocationStrategy <TB>(method))
                        .SetIndex(attribute.Index).Build());

                    // in the case where both accessors (getters and setters) are
                    // annotated we only use the values in the first annotation we
                    // come stumble across
                    fieldsExcluded.Add(mangled);
                }
            }
        }
Esempio n. 8
0
 public string Serialize(PortableProperty property, string prefix, SerializeOption option) =>
 Base(property, prefix, option);