public override void Update(GameTime time, Entity entity)
        {
            var animation = (Animated_Sprite)entity.Get(Types.Animation);
            var body      = (Body)entity.Get(Types.Body);

            if (!animation.Playing)
            {
                return;
            }

            animation.Timer += (float)time.ElapsedGameTime.TotalSeconds;

            if (animation.Animations.ContainsKey(animation.Current_Animation_ID) == false)
            {
                throw new Exception("Animation does not contain the key: " + animation.Current_Animation_ID); //Error Handling
            }
            Animation current_animation = animation.Animations[animation.Current_Animation_ID];

            if (animation.Current_Frame > current_animation.Frames.Count - 1)
            {
                animation.Current_Frame = 0;
            }

            Animation_Frame current_frame = current_animation.Frames[animation.Current_Frame];

            if (animation.Timer >= current_frame.Frame_Time)
            {
                animation.Timer = 0;
                animation.Current_Frame++;
                if (animation.Current_Frame > current_animation.Frames.Count - 1)
                {
                    animation.Current_Frame = 0;
                }
            }

            if (animation.Current_Frame == current_animation.Frames.Count - 1)
            {
                animation.Force_Animation = false;
                animation.Animation_End   = true;
            }
            else
            {
                animation.Animation_End = false;
            }

            //// flashes red when the timer is high
            //if (animation.Flash_Timer > 0 )
            //{
            //    if ( animation.Color == Color.White )
            //    {
            //        animation.Color = Color.Red;
            //    }else
            //    {
            //        animation.Color = Color.White;
            //    }

            //    animation.Flash_Timer -= (float) time.ElapsedGameTime.TotalSeconds;
            //}
        }
Esempio n. 2
0
        public void Load_Animations_From_Lua(string file)
        {
            LuaTable data = lua.DoFile(file)[0] as LuaTable;

            if (data["generate"] is LuaTable generate)
            {
                foreach (String key in generate.Keys)
                {
                    LuaTable gen_data = generate[key] as LuaTable;
                    int      sx       = (int)(gen_data[1] as double?);
                    int      sy       = (int)(gen_data[2] as double?);
                    int      fw       = (int)(gen_data[3] as double?);
                    int      fh       = (int)(gen_data[4] as double?);
                    int      nm       = (int)(gen_data[5] as double?);

                    Animation animation = Generate_Animation(key, new Vector2(sx, sy), new Vector2(fw, fh), nm);

                    if (gen_data["offset_x"] != null)
                    {
                        animation.Offset_X = (float)(gen_data["offset_x"] as double?);
                    }

                    if (gen_data["offset_y"] != null)
                    {
                        animation.Offset_Y = (float)(gen_data["offset_y"] as double?);
                    }

                    if (gen_data["left_offset_x"] != null)
                    {
                        animation.Left_Face_Offset += new Vector2((float)(gen_data["left_offset_x"] as double?), 0);
                    }

                    if (gen_data["right_offset_x"] != null)
                    {
                        animation.Right_Face_Offset += new Vector2((float)(gen_data["right_offset_x"] as double?), 0);
                    }

                    if (gen_data["right_offset_y"] != null)
                    {
                        animation.Right_Face_Offset += new Vector2(0, (float)(gen_data["right_offset_y"] as double?));
                    }

                    if (gen_data["left_offset_y"] != null)
                    {
                        animation.Left_Face_Offset += new Vector2(0, (float)(gen_data["left_offset_y"] as double?));
                    }

                    if (gen_data["speed"] != null)
                    {
                        float speed = (float)(gen_data["speed"] as double?);
                        foreach (Animation_Frame frame in animation.Frames)
                        {
                            frame.Frame_Time = speed;
                        }
                    }
                }
            }
            if (data["frames"] is LuaTable frames)
            {
                foreach (var key in frames.Keys)
                {
                    LuaTable animation_frames      = frames[key] as LuaTable;
                    List <Animation_Frame> aframes = new List <Animation_Frame>();

                    foreach (LuaTable frame in animation_frames.Values)
                    {
                        int x = (int)(frame[1] as double?);
                        int y = (int)(frame[2] as double?);
                        int w = (int)(frame[3] as double?);
                        int h = (int)(frame[4] as double?);

                        var _aframe = new Animation_Frame(
                            new Vector2(x, y),
                            new Vector2(w, h));
                        aframes.Add(_aframe);
                    }

                    Animation animation = new Animation(aframes, key as string);
                    animations.Add(key as string, animation);
                }
            }
        }