Example #1
0
        /// <summary>
        /// Updates the drawable. If the drawable changed in any
        /// visible manner, then this returns true.
        /// </summary>
        public bool Update(DrawableState state)
        {
            // Don't bother if we only have one frame
            if (tile.Count == 1)
            {
                return(false);
            }

            // Figure out the different in time
            TileFrame frame = tile.Frames[state.Frame];
            long      now   = DateTime.UtcNow.Ticks / 10000;
            long      diff  = now - state.LastUpdate;

            // If not enough time has passed, just move on
            if (diff < frame.Delay)
            {
                return(false);
            }

            // We need to move to the next one
            long fdiff = diff - frame.Delay;

            if (fdiff > frame.Delay)
            {
                fdiff %= frame.Delay;
            }

            state.Frame      = frame.NextFrame;
            state.LastUpdate = now + fdiff;
            return(true);
        }
Example #2
0
        /// <summary>
        /// Reads a single frame into memory.
        /// </summary>
        private void ReadFrame(
            Tile tile,
            XmlTextReader xtr)
        {
            // Grab the sequence
            int       seq   = Int32.Parse(xtr["sequence"]);
            TileFrame frame = null;

            try
            {
                frame = tile.Frames[seq];
            }
            catch (Exception)
            {
                return;
            }

            // Try to read in the rest of the optional elements
            if (!String.IsNullOrEmpty(xtr["delay-ms"]))
            {
                frame.Delay = Int32.Parse(xtr["delay-ms"]);
            }

            if (!String.IsNullOrEmpty(xtr["next-frame"]))
            {
                frame.NextFrame = Int32.Parse(xtr["next-frame"]);
            }

            if (!String.IsNullOrEmpty(xtr["random"]))
            {
                frame.Random = Boolean.Parse(xtr["random"]);
            }
        }
Example #3
0
        /// <summary>
        /// Randomizes the drawable state based on this specific
        /// drawable.
        /// </summary>
        public void Randomize(DrawableState state)
        {
            // Pick a random frame. We loop this since a frame may not
            // be randomizeable (the Random property is false).
            while (true)
            {
                // Pick one
                int       seq   = random.Next(0, tile.Count - 1);
                TileFrame frame = tile.Frames[seq];

                if (frame.Random)
                {
                    // We can use this as a random. We also randomize
                    // the last update to have not everything updating
                    // at the same time.
                    state.Frame      = seq;
                    state.LastUpdate = DateTime.UtcNow.Ticks / 10000 +
                                       random.Next(0, frame.Delay);
                    return;
                }
            }
        }