Exemplo n.º 1
0
        /// <summary>
        /// Removes the specified GameDrawableInstance from this DrawableSet. This will only work if the
        /// GameDrawableInstance belongs to this DrawableSet - it will not work if it belongs to another.
        /// Returns a true boolean value if the specified instance was found within this set and removed.
        /// If nothing was removed, a false value is returned.
        /// </summary>
        public bool Remove(GameDrawableInstance gameDrawableInstance)
        {
            bool result = true;

            result &= _stateDictionary[gameDrawableInstance._associatedState].Remove(gameDrawableInstance);
            result &= _groupDictionary[gameDrawableInstance._associatedGroup].Remove(gameDrawableInstance);

            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Performs a Set union operation between this DrawableSet and the DrawableSet specified in the parameter.
 /// It is important to note that any IGameDrawables newly created in this DrawableSet are wrapped around a
 /// *new* GameDrawableInstance in order to allow properties to be set independently for each one.
 /// </summary>
 public void Union(DrawableSet drawableSet)
 {
     foreach (string state in drawableSet.GetStates())
     {
         foreach (GameDrawableInstance instance in drawableSet.GetByState(state))
         {
             GameDrawableInstance copiedInstance = Add(state, instance.Drawable, instance._associatedGroup);
             copiedInstance.Layer    = instance.Layer;
             copiedInstance.Offset   = instance.Offset;
             copiedInstance.Rotation = instance.Rotation;
             copiedInstance.Visible  = instance.Visible;
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Adds the specified IGameDrawable to this DrawableSet under the specified *state*. Optionally, the
        /// IGameDrawable being added can also be associated with a *group*. When an IGameDrawable is added to
        /// a DrawableSet, it is wrapped around a GameDrawableInstance class that allows properties about the
        /// drawable to be set such as its Color, Rotation, Visibility etc... This GameDrawableInstance that is
        /// created to wrap the IGameDrawable is returned by this method.
        /// </summary>
        public GameDrawableInstance Add(string state, IGameDrawable drawable, string group = "")
        {
            if (!_stateDictionary.ContainsKey(state))
            {
                _stateDictionary.Add(state, new HashSet <GameDrawableInstance>());
            }

            if (!_groupDictionary.ContainsKey(group))
            {
                _groupDictionary.Add(group, new HashSet <GameDrawableInstance>());
            }

            GameDrawableInstance instance = new GameDrawableInstance(drawable);

            instance._associatedGroup = group;
            instance._associatedState = state;

            _stateDictionary[state].Add(instance);
            _groupDictionary[group].Add(instance);

            return(instance);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Removes the specified GameDrawableInstance from this DrawableSet. This will only work if the
        /// GameDrawableInstance belongs to this DrawableSet - it will not work if it belongs to another.
        /// Returns a true boolean value if the specified instance was found within this set and removed.
        /// If nothing was removed, a false value is returned.
        /// </summary>
        public bool Remove(GameDrawableInstance gameDrawableInstance)
        {
            bool result = true;
            result &= _stateDictionary[gameDrawableInstance._associatedState].Remove(gameDrawableInstance);
            result &= _groupDictionary[gameDrawableInstance._associatedGroup].Remove(gameDrawableInstance);

            return result;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the specified IGameDrawable to this DrawableSet under the specified *state*. Optionally, the
        /// IGameDrawable being added can also be associated with a *group*. When an IGameDrawable is added to
        /// a DrawableSet, it is wrapped around a GameDrawableInstance class that allows properties about the 
        /// drawable to be set such as its Color, Rotation, Visibility etc... This GameDrawableInstance that is
        /// created to wrap the IGameDrawable is returned by this method.
        /// </summary>
        public GameDrawableInstance Add(string state, IGameDrawable drawable, string group="")
        {
            if (!_stateDictionary.ContainsKey(state))
                _stateDictionary.Add(state, new HashSet<GameDrawableInstance>());

            if (!_groupDictionary.ContainsKey(group))
                _groupDictionary.Add(group, new HashSet<GameDrawableInstance>());

            GameDrawableInstance instance = new GameDrawableInstance(drawable);

            instance._associatedGroup = group;
            instance._associatedState = state;

            _stateDictionary[state].Add(instance);
            _groupDictionary[group].Add(instance);

            return instance;
        }
Exemplo n.º 6
0
        /// <summary>
        /// Loads a DrawableSet file into a specified DrawableSet object.
        /// The method requires the string path to the xml file containing the drawable data and a reference to the
        /// ContentManager. An optional Layer value can be specified for the ordering of the drawables in the
        /// DrawableSet. Currently only supports loading of Animation objects.
        /// </summary>
        /// <param name="drawableSet">DrawableSet object to load the animations into.</param>
        /// <param name="path">String path to the XML formatted .anim file</param>
        /// <param name="content">Reference to the ContentManager instance being used in the application</param>
        public static void LoadDrawableSetXml(DrawableSet drawableSet, string path, ContentManager content, double startTimeMS = 0)
        {
            XmlDocument document = new XmlDocument();

            document.Load(path);

            foreach (XmlNode animNode in document.SelectNodes("Animations/Animation"))
            {
                int  frameDelay = XmlExtensions.GetAttributeValue <int>(animNode, "FrameDelay", 90);
                bool loop       = XmlExtensions.GetAttributeValue <bool>(animNode, "Loop", true);
                int  layer      = XmlExtensions.GetAttributeValue <int>(animNode, "Layer", 0);

                string   state       = XmlExtensions.GetAttributeValue(animNode, "State");
                string   group       = XmlExtensions.GetAttributeValue(animNode, "Group", "");
                string   spriteSheet = XmlExtensions.GetAttributeValue(animNode, "SpriteSheet");
                string[] offset      = XmlExtensions.GetAttributeValue(animNode, "Offset", "0, 0").Split(',');
                string[] origin      = XmlExtensions.GetAttributeValue(animNode, "Origin", "0.5, 1.0").Split(',');

                Vector2 offsetVector = new Vector2((float)Convert.ToDouble(offset[0]), (float)Convert.ToDouble(offset[1]));
                Vector2 originVector = new Vector2((float)Convert.ToDouble(origin[0]), (float)Convert.ToDouble(origin[1]));

                XmlNodeList frameNodes = animNode.SelectNodes("Frames/Frame");
                Rectangle[] frames     = new Rectangle[frameNodes.Count];

                for (int i = 0; i < frameNodes.Count; i++)
                {
                    string[] tokens = frameNodes[i].InnerText.Split(',');
                    if (tokens.Length != 4)
                    {
                        throw new FormatException("Expected 4 Values for Frame Definition: X, Y, Width, Height");
                    }

                    int x      = Convert.ToInt32(tokens[0]);
                    int y      = Convert.ToInt32(tokens[1]);
                    int width  = Convert.ToInt32(tokens[2]);
                    int height = Convert.ToInt32(tokens[3]);

                    frames[i] = new Rectangle(x, y, width, height);
                }

                Animation animation = new Animation(content.Load <Texture2D>(spriteSheet), frames, frameDelay, loop);
                animation.Origin = originVector;

                // TODO: Requires possible revision of code.
                // Allow support for specifying glob patterns in the case of state names.
                if (state.Contains("*"))
                {
                    // Use Glob patterns in favour of regular expressions.
                    state = Regex.Escape(state).Replace(@"\*", ".*").Replace(@"\?", ".");
                    Regex regexMatcher = new Regex(state);

                    foreach (string drawableSetState in drawableSet.GetStates())
                    {
                        if (regexMatcher.IsMatch(drawableSetState))
                        {
                            GameDrawableInstance instance = drawableSet.Add(drawableSetState, animation, group);
                            instance.StartTimeMS = startTimeMS;
                            instance.Layer       = layer;
                            instance.Offset      = offsetVector;
                        }
                    }
                }
                else
                {
                    GameDrawableInstance instance = drawableSet.Add(state, animation, group);
                    instance.StartTimeMS = startTimeMS;
                    instance.Layer       = layer;
                    instance.Offset      = offsetVector;
                }
            }
        }