コード例 #1
0
        /// <summary>
        /// Defines a game object property.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="name">The name.</param>
        /// <param name="category">The category.</param>
        /// <param name="description">The description.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The property metadata.</returns>
        /// <remarks>
        /// Game object properties must be defined using this method before they can be used. Properties
        /// are identified by name and type. If a property with the given <paramref name="name"/>
        /// and type <typeparamref name="T"/> has already been defined, the metadata of the existing
        /// property is returned and the other parameters (<paramref name="category"/>,
        /// <paramref name="description"/> and <paramref name="defaultValue"/>) are ignored!
        /// </remarks>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="name"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="name"/> must not be an empty string.
        /// </exception>
        public static GamePropertyMetadata <T> CreateProperty <T>(string name, string category, string description, T defaultValue)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentException("Name must not be an empty string.", "name");
            }

            GamePropertyMetadata <T> metadata;

            if (GamePropertyMetadata <T> .Properties.TryGet(name, out metadata))
            {
                // Nothing to do - property was already defined.
                return(metadata);
            }

            Debug.Assert(NextPropertyId == PropertyMetadata.Count);

            // Create and add new metadata.
            metadata = new GamePropertyMetadata <T>(name, NextPropertyId)
            {
                Category     = category,
                Description  = description,
                DefaultValue = defaultValue,
            };

            PropertyMetadata.Add(metadata);
            NextPropertyId++;

            return(metadata);
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GameProperty{T}"/> struct.
        /// </summary>
        /// <param name="owner">The game object that owns this property.</param>
        /// <param name="metadata">The metadata.</param>
        internal GameProperty(GameObject owner, GamePropertyMetadata <T> metadata)
        {
            Debug.Assert(owner != null, "owner must not be null.");
            Debug.Assert(metadata != null, "metadata must not be null.");

            _owner    = owner;
            _metadata = metadata;
        }
コード例 #3
0
        public AnimatableGamePropertyData(GameObject owner, GamePropertyMetadata <T> metadata, GamePropertyData <T> data)
            : base(data)
        {
            Debug.Assert(owner != null, "owner must not be null.");
            Debug.Assert(metadata != null, "metadata must not be null.");

            _owner    = owner;
            _metadata = metadata;
        }
コード例 #4
0
        /// <summary>
        /// Gets the property for the given metadata.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="metadata">The metadata of the property.</param>
        /// <returns>The property</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="metadata"/> is <see langword="null"/>.
        /// </exception>
        public GameProperty <T> Get <T>(GamePropertyMetadata <T> metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            return(new GameProperty <T>(Owner, metadata));
        }
コード例 #5
0
        /// <summary>
        /// Adds a property for the given metadata.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="metadata">The metadata of the property.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="metadata"/> is <see langword="null"/>.
        /// </exception>
        public void Add <T>(GamePropertyMetadata <T> metadata)
        {
            if (metadata == null)
            {
                throw new ArgumentNullException("metadata");
            }

            Add <T>(metadata.Id);
        }
コード例 #6
0
        /// <summary>
        /// Gets the property with the given ID.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="id">The ID of the property.</param>
        /// <returns>The property.</returns>
        /// <exception cref="ArgumentException">
        /// The <paramref name="id"/> is invalid. Note: Properties must be defined with
        /// <see cref="GameObject.CreateProperty{T}"/> before they can be used.
        /// </exception>
        public GameProperty <T> Get <T>(int id)
        {
            if (id < 0 || id >= GameObject.PropertyMetadata.Count)
            {
                string message = string.Format(
                    CultureInfo.InvariantCulture,
                    "Unknown ID. No game object property with the ID '{0}' and type '{1}' was defined. "
                    + "Game object properties must be defined using GameObject.CreateProperty() before they can be used.",
                    id,
                    typeof(T).FullName);
                throw new ArgumentException(message, "id");
            }

            GamePropertyMetadata <T> metadata = (GamePropertyMetadata <T>)GameObject.PropertyMetadata[id];

            return(new GameProperty <T>(Owner, metadata));
        }
コード例 #7
0
        /// <summary>
        /// Set the value of the property for the given metadata.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="propertyMetadata">The metadata of the property.</param>
        /// <param name="value">The new value.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="propertyMetadata"/> is <see langword="null"/>.
        /// </exception>
        public void SetValue <T>(GamePropertyMetadata <T> propertyMetadata, T value)
        {
            var property = Properties.Get(propertyMetadata);

            property.Value = value;
        }
コード例 #8
0
 /// <summary>
 /// Gets the value of the property with the given metadata.
 /// </summary>
 /// <typeparam name="T">The type of the property value.</typeparam>
 /// <param name="propertyMetadata">The metadata of the property.</param>
 /// <returns>
 /// The value of the property.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="propertyMetadata"/> is <see langword="null"/>.
 /// </exception>
 public T GetValue <T>(GamePropertyMetadata <T> propertyMetadata)
 {
     return(Properties.Get(propertyMetadata).Value);
 }
コード例 #9
0
        public bool Remove <T>(string name)
        {
            GamePropertyMetadata <T> metadata = GetMetadataChecked <T>(name);

            return(Remove(metadata.Id));
        }
コード例 #10
0
        /// <overloads>
        /// <summary>
        /// Gets a game object property.
        /// </summary>
        /// </overloads>
        ///
        /// <summary>
        /// Gets the property with the given name.
        /// </summary>
        /// <typeparam name="T">The type of the property value.</typeparam>
        /// <param name="name">The name of the property.</param>
        /// <returns>The property.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="name"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="name"/> is an empty string.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// The property is not defined. Properties must be defined with
        /// <see cref="GameObject.CreateProperty{T}"/> before they can be used.
        /// </exception>
        public GameProperty <T> Get <T>(string name)
        {
            GamePropertyMetadata <T> metadata = GetMetadataChecked <T>(name);

            return(new GameProperty <T>(Owner, metadata));
        }
コード例 #11
0
        public void Add <T>(string name)
        {
            GamePropertyMetadata <T> metadata = GetMetadataChecked <T>(name);

            Add(metadata);
        }