Esempio n. 1
0
        public SpriteAnimation(String anim_name, int num_frames, String anim_pattern, AnimationStyle anim_style)
        {
            int cur_pos, next_pos, len, cur_val;
            bool expecting_frame;

            // temporary queues for storing data while reading through the movestring
            Queue<int> frame_queue = new Queue<int>();
            Queue<int> wait_queue = new Queue<int>();

            name = anim_name;
            style = anim_style;
            _pattern = SpriteAnimation.clean_pattern(anim_pattern);
            len = _pattern.Length;
            transition_to = null;

            if (_pattern.Substring(0, 1) != SpriteAnimation.FRAME) throw new MalformedAnimationPatternException(_pattern, "Patterns must begin with \"F\"");
            cur_pos = 1;
            expecting_frame = false;
            while (cur_pos < len) {
                if (expecting_frame) next_pos = _pattern.IndexOf(SpriteAnimation.FRAME, cur_pos);
                else next_pos = _pattern.IndexOf(SpriteAnimation.WAIT, cur_pos);
                if (next_pos == -1) next_pos = len;
                try {
                    cur_val = Int32.Parse(_pattern.Substring(cur_pos, next_pos - cur_pos));
                }
                catch (Exception) {
                    throw new MalformedAnimationPatternException(_pattern, "Patterns must alternate between F# and W# terms, where each # is a positive integer.");
                }
                if (expecting_frame) {
                    //Console.WriteLine("Wait {0}", cur_val);
                    if (cur_val < 0) throw new MalformedAnimationPatternException(_pattern, "Negative wait specified.");
                    wait_queue.Enqueue(cur_val);
                }
                else {
                    //Console.WriteLine("Frame {0}", cur_val);
                    if (cur_val < 0) throw new MalformedAnimationPatternException(_pattern, "Negative frame specified.");
                    if (cur_val >= num_frames) throw new MalformedAnimationPatternException(_pattern, "Frame " + cur_val + " specified, but the sprite only has " + num_frames + " frames. Note that frames begin at 0.");
                    frame_queue.Enqueue(cur_val);
                }
                expecting_frame = !expecting_frame;
                cur_pos = next_pos + 1;
            }

            // It's legal not to specify the last frame's wait time. If it's left blank, give it a wait time of 1.
            if (wait_queue.Count < frame_queue.Count) wait_queue.Enqueue(1);

            length = frame_queue.Count;
            frame = new int[length];
            delay = new int[length];
            for (int i = 0; i < length; i++) {
                frame[i] = frame_queue.Dequeue();
                delay[i] = wait_queue.Dequeue();
            }
        }
Esempio n. 2
0
        public SpriteAnimation( String anim_name, int num_frames, System.Collections.Generic.List<System.Object> anim_pattern, AnimationStyle anim_style )
        {
            name = anim_name;
            style = anim_style;
            length = anim_pattern.Count;
            transition_to = null;
            frame = new int[length];
            delay = new int[length];
            for( int i = 0; i < length; i++ ) {

                System.Collections.Generic.List<System.Object> item = (System.Collections.Generic.List<System.Object>)anim_pattern[i];

                frame[i] = (int)(System.Int64)item[0];
                delay[i] = (int)(System.Int64)item[1];
            }
        }
Esempio n. 3
0
 public virtual void set_animation(SpriteAnimation anim)
 {
     fiddle_flag = true;
     cur_step = 0;
     cur_animation = anim;
     time_to_next = anim.delay[0];
     going_backwards = false;
     _animation_paused = false;
 }
Esempio n. 4
0
 // Switches the entity to the specified animation, setting "animating" to true if it was false.
 // Can also be used to reset the animation currently in progress.
 public virtual void set_animation(String name)
 {
     fiddle_flag = true;
     if (String.IsNullOrEmpty(name)) {
         cur_animation = null;
         fixed_frame = -1;
         cur_step = 0;
         time_to_next = 0;
     }
     else {
         set_animation(basis.animations[name]);
         _animation_paused = false;
     }
 }
Esempio n. 5
0
        public virtual void advance_frame(bool ignore_delegates)
        {
            bool spillover = false;
            if (angular_momentum != 0f) {
                angle += angular_momentum*(VERGEGame.game.tick - last_draw_tick)/100f;
            }

            if (!visible || !animating) {
                last_draw_tick = VERGEGame.game.tick;
                return;
            }
            if (rate == 1.0f) time_to_next -= 100*(VERGEGame.game.tick - last_draw_tick);
            else time_to_next -= (int)(rate*100*(VERGEGame.game.tick - last_draw_tick));
            while (time_to_next <= 0) {
                if (going_backwards) {
                    cur_step--;
                    if (cur_step < 0) spillover = true;
                }
                else {
                    cur_step++;
                    if (cur_step >= cur_animation.length) spillover = true;
                }
                if (spillover) {
                    switch (cur_animation.style) {
                        case AnimationStyle.Looping:
                            cur_step = 0;
                            break;
                        case AnimationStyle.BackAndForth:
                            going_backwards = !going_backwards;
                            if (going_backwards) {
                                if (cur_animation.length > 1) cur_step -= 2;
                                else cur_step--;
                            }
                            else {
                                if (cur_animation.length > 1) cur_step += 2;
                                else cur_step++;
                            }
                            break;
                        case AnimationStyle.Once:
                            cur_step--; // stay at the final step
                            if (!_handle_callback()) {
                                _animation_paused = true;
                                time_to_next = 0;
                                last_draw_tick = VERGEGame.game.tick;
                                return;
                            }
                            break;
                        case AnimationStyle.Transition:
                            cur_step--;
                            if (!_handle_callback()) {
                                cur_animation = cur_animation.transition_to;
                                cur_step = 0;
                            }
                            break;
                        default: throw new InvalidOperationException("Unknown animation style (integer value " + (int)cur_animation.style + ").");
                    }
                }
                time_to_next += 100*cur_animation.delay[cur_step];
            }
            last_draw_tick = VERGEGame.game.tick;
        }
Esempio n. 6
0
 // Switches the entity to the specified animation, setting "animating" to true if it was false.
 // Can also be used to reset the animation currently in progress.
 public virtual void set_animation(String name)
 {
     //Console.WriteLine(name);
     if (String.IsNullOrEmpty(name)) {
         cur_animation = null;
         fixed_frame = -1;
         cur_step = 0;
         time_to_next = 0;
     }
     else {
         set_animation(basis.animations[name]);
         _animation_paused = false;
     }
 }
Esempio n. 7
0
 public virtual void advance_frame(bool ignore_delegates)
 {
     // TODO: delegates
     if (!visible || !animating) {
         last_draw_tick = VERGEGame.game.tick;
         return;
     }
     if (rate == 1.0f) time_to_next -= 100*(VERGEGame.game.tick - last_draw_tick);
     else time_to_next -= (int)(rate*100*(VERGEGame.game.tick - last_draw_tick));
     while (time_to_next <= 0) {
         cur_step++;
         if (cur_step >= cur_animation.length) {
             if (cur_animation.style == AnimationStyle.Looping) cur_step = 0;
             else if (cur_animation.style == AnimationStyle.Once) {
                 _animation_paused = true;
                 cur_step--; // stay at the final step
                 time_to_next = 0;
                 last_draw_tick = VERGEGame.game.tick;
                 return;
             }
             else if (cur_animation.style == AnimationStyle.BackAndForth) {
                 going_backwards = !going_backwards;
                 if (going_backwards) {
                     if (cur_animation.length > 1) cur_step -= 2;
                     else cur_step--;
                 }
                 else {
                     if (cur_animation.length > 1) cur_step += 2;
                     else cur_step++;
                 }
             }
             else if (cur_animation.style == AnimationStyle.Transition) {
                 cur_animation = cur_animation.transition_to;
                 cur_step = 0;
             }
         }
         time_to_next += 100*cur_animation.delay[cur_step];
     }
     last_draw_tick = VERGEGame.game.tick;
 }
Esempio n. 8
0
 // If the frame and delay array have already been generated (as is the case with sprites saved to file), this is the constructor
 // to use. It makes no effort to check that the pattern actually matches the animation arrays given.
 public SpriteAnimation(String anim_name, String anim_pattern, int[] frame_arr, int[] delay_arr, AnimationStyle anim_style)
 {
     name = anim_name;
     _pattern = anim_pattern;
     frame = frame_arr;
     delay = delay_arr;
     length = frame_arr.Length;
     style = anim_style;
     transition_to = null;
 }