Пример #1
0
        private Animation LoadAnimation(string animname, List <KeyValuePair <string, Rectangle> > frames)
        {
            Animation anim       = new Animation(animname, true, 14, animname);
            var       anim_frame = (from f in frames
                                    where f.Key.StartsWith(animname)
                                    select f.Value).ToList();

            //foreach (var f in anim_frame) {
            for (int i = 1; i < anim_frame.Count; i++)
            {
                anim.AddKeyFrame(anim_frame[i]);
            }
            return(anim);
        }
Пример #2
0
        private void InitCanvas()
        {
            canvas = new Canvas();

            Label label_name = new Label("Untitled", new Point(400, 0), new Vector2(200, 20), CONTENT_MANAGER.Fonts["default"])
            {
                Origin          = new Vector2(-2, -2),
                BackgroundColor = Color.LightGray
            };

            label_snippet = new Label("", new Point(400, 30), new Vector2(20, 20), CONTENT_MANAGER.Fonts["default"])
            {
                Origin = new Vector2(-2, -2)
            };

            CONTENT_MANAGER.gameInstance.Window.TextInput += (obj, e) =>
            {
                //if (label_name.IsFocused)
                {
                    if (char.IsLetterOrDigit(e.Character))
                    {
                        label_name.Text += e.Character;
                    }
                    else if (e.Key == Keys.Back)
                    {
                        int cc = label_name.Text.Length - 1;
                        if (cc >= 0)
                        {
                            label_name.Text = label_name.Text.Remove(cc);
                        }
                    }
                }
            };

            pictureBox_spritesheet = new PictureBox(CONTENT_MANAGER.Sprites[texture], new Point(50, 0), null, null);

            pictureBox_snippet = new PictureBox(CONTENT_MANAGER.Sprites[texture], new Point(400, 70), Rectangle.Empty, null);

            pictureBox_spritesheet.MouseDrag += (obj, e) =>
            {
                if (e.Size != Point.Zero)
                {
                    //DrawingHelper.RemoveShape(selectedFrame);
                    //selectedFrame = DrawingHelper.GetRectangle(e, Color.Red, false);
                    //DrawingHelper.DrawShape(selectedFrame);
                    selectedFrame      = new Rectangle((e.Location.ToVector2() * pictureBox_spritesheet.Scale).ToPoint(), (e.Size.ToVector2() * pictureBox_spritesheet.Scale).ToPoint());
                    label_snippet.Text = selectedFrame.ToString();
                    e.Location         = new Point(e.Location.X - pictureBox_spritesheet.Position.X, e.Location.Y - pictureBox_spritesheet.Position.Y);
                    pictureBox_snippet.SourceRectangle = e;
                }
            };

            pictureBox_spritesheet.KeyPress += (obj, e) =>
            {
                if (HelperFunction.IsKeyPress(Keys.OemPlus))
                {
                    pictureBox_spritesheet.Scale += 0.1f;
                    scaleChanged = true;
                }

                if (HelperFunction.IsKeyPress(Keys.OemMinus))
                {
                    if (pictureBox_spritesheet.Scale > 1)
                    {
                        pictureBox_spritesheet.Scale -= 0.1f;
                        scaleChanged = true;
                    }
                }
            };
            Button button_new = new Button("New", new Point(650, 30), new Vector2(60, 30), CONTENT_MANAGER.Fonts["default"])
            {
                ButtonColorReleased = Color.White
            };

            button_new.MouseClick += (o, e) =>
            {
                animationPlayer.StopAnimation();
                label_name.Text = "Untitled";
            };

            Button button_add_sprite = new Button("Add F", new Point(720, 30), new Vector2(60, 30), CONTENT_MANAGER.Fonts["default"])
            {
                ButtonColorReleased = Color.White
            };

            button_add_sprite.MouseClick += (obj, e) =>
            {
                if (string.IsNullOrWhiteSpace(label_name.Text))
                {
                    label_name.Text = "Please enter the name of this animation";
                }
                else if (!animation_list.ContainsKey(label_name.Text))
                {
                    animation_list.Add(label_name.Text, new List <Rectangle>()
                    {
                        pictureBox_snippet.SourceRectangle
                    });
                }
                else
                {
                    animation_list[label_name.Text].Add(pictureBox_snippet.SourceRectangle);
                }
            };

            animationPlayer = new AnimationPlayer(new AnimatedEntity(), new Point(700, 70));

            Button button_test_animation = new Button("Test", new Point(790, 30), new Vector2(60, 30), CONTENT_MANAGER.Fonts["default"])
            {
                ButtonColorReleased = Color.White
            };

            button_test_animation.MouseClick += (o, e) =>
            {
                string    animationName = label_name.Text;
                Animation animation     = new Animation(animationName, true, 1, null);
                foreach (var frame in animation_list[animationName])
                {
                    animation.AddKeyFrame(frame);
                }
                if (animationPlayer.ContainAnimation(animation.Name))
                {
                    animationPlayer.RemoveAnimation(animation.Name);
                }
                animationPlayer.AddAnimation(animation);
                animationPlayer.LoadContent(pictureBox_snippet.Texture2D);
                animationPlayer.PlayAnimation(animation.Name);
            };

            Button button_export = new Button("Export", new Point(860, 30), new Vector2(60, 30), CONTENT_MANAGER.Fonts["default"])
            {
                ButtonColorReleased = Color.White
            };

            button_export.MouseClick += (o, e) =>
            {
                var animdata = JsonConvert.SerializeObject(animationPlayer.AnimatedEntity, Formatting.Indented);
                File.WriteAllText($"{label_name.Text}.txt", animdata);
            };

            canvas.AddElement("label_test", label_name);
            canvas.AddElement("label_snippet", label_snippet);
            canvas.AddElement("pictureBox_test", pictureBox_spritesheet);
            canvas.AddElement("pictureBox_snippet", pictureBox_snippet);
            canvas.AddElement("button_add_sprite", button_add_sprite);
            canvas.AddElement("animationPlayer", animationPlayer);
            canvas.AddElement("button_test_animation", button_test_animation);
            canvas.AddElement("button_export", button_export);
            canvas.AddElement("button_new", button_new);
        }
Пример #3
0
        public static Unit Create(UnitType unittype, Owner owner, int hp = 10, AnimationName startingAnimation = AnimationName.idle, Guid guid = default(Guid))
        {
            AnimatedEntity animEntity = new AnimatedEntity(Vector2.Zero, Vector2.Zero, Color.White, LayerDepth.Unit);

            animEntity.LoadContent(CONTENT_MANAGER.animationSheets[unittype.GetSpriteSheetUnit(owner)]);

            #region declare animation frame
            Animation idle;
            Animation right;
            Animation up;
            Animation down;
            Animation done;
            switch (unittype)
            {
            case UnitType.TransportCopter:
            case UnitType.BattleCopter:
                animEntity.Origin = new Vector2(8, 16);
                idle = new Animation("idle", true, 3, string.Empty);
                for (int i = 0; i < 3; i++)
                {
                    idle.AddKeyFrame(i * 64, 0, 64, 64);
                }

                right = new Animation("right", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    right.AddKeyFrame(i * 64, 64, 64, 64);
                }

                up = new Animation("up", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    up.AddKeyFrame(i * 64, 128, 64, 64);
                }

                down = new Animation("down", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    down.AddKeyFrame(i * 64, 192, 64, 64);
                }

                done = new Animation("done", true, 1, string.Empty);
                done.AddKeyFrame(0, 256, 64, 64);
                break;

            case UnitType.Fighter:
            case UnitType.Bomber:
                animEntity.Origin = new Vector2(8, 16);

                idle = new Animation("idle", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    idle.AddKeyFrame(i * 64, 0, 64, 64);
                }

                right = new Animation("right", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    right.AddKeyFrame(i * 64, 64, 64, 64);
                }

                up = new Animation("up", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    up.AddKeyFrame(i * 64, 128, 64, 64);
                }

                down = new Animation("down", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    down.AddKeyFrame(i * 64, 192, 64, 64);
                }

                done = new Animation("done", true, 1, string.Empty);
                done.AddKeyFrame(0, 256, 64, 64);
                break;

            default:
                idle = new Animation("idle", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    idle.AddKeyFrame(i * 48, 0, 48, 48);
                }

                right = new Animation("right", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    right.AddKeyFrame(i * 48, 48, 48, 48);
                }

                up = new Animation("up", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    up.AddKeyFrame(i * 48, 96, 48, 48);
                }

                down = new Animation("down", true, 4, string.Empty);
                for (int i = 0; i < 4; i++)
                {
                    down.AddKeyFrame(i * 48, 144, 48, 48);
                }

                done = new Animation("done", true, 1, string.Empty);
                done.AddKeyFrame(0, 192, 48, 48);
                break;
            }
            #endregion

            animEntity.AddAnimation(idle, right, up, down, done);

            Unit result;
            if (guid == default(Guid))
            {
                result = new Unit(unittype, animEntity, owner, hp);
            }
            else
            {
                result = new Unit(unittype, animEntity, owner, guid, hp);
            }

            result.Animation.PlayAnimation(startingAnimation.ToString());
            return(result);
        }
Пример #4
0
        private static void LoadAnimationContent()
        {
            //string delimit = "Yellow";
            CONTENT_MANAGER.animationEntities = new Dictionary <SpriteSheetUnit, AnimatedEntity>();
            CONTENT_MANAGER.animationSheets   = new Dictionary <SpriteSheetUnit, Texture2D>();
            CONTENT_MANAGER.animationTypes    = new List <Animation>();

            //list of unit type
            var UnitTypes = new List <SpriteSheetUnit>((IEnumerable <SpriteSheetUnit>)Enum.GetValues(typeof(SpriteSheetUnit)));

            //Artillery
            //load animation sprite sheet for each unit type
            foreach (SpriteSheetUnit unittype in UnitTypes)
            {
                var paths = unittype.ToString().Split('_');
                //if (paths[0].CompareTo(delimit) == 0)
                {
                    //  break;
                }
                CONTENT_MANAGER.animationSheets.Add(unittype, CONTENT_MANAGER.Content.Load <Texture2D>("sprite//Animation//" + paths[0] + "//" + paths[1]));
            }

            //declare animation frame

            //animation frame for "normal" unit
            Animation idle = new Animation("idle", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                idle.AddKeyFrame(i * 48, 0, 48, 48);
            }

            Animation right = new Animation("right", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                right.AddKeyFrame(i * 48, 48, 48, 48);
            }

            Animation up = new Animation("up", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                up.AddKeyFrame(i * 48, 96, 48, 48);
            }

            Animation down = new Animation("down", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                down.AddKeyFrame(i * 48, 144, 48, 48);
            }

            Animation done = new Animation("done", true, 1, string.Empty);

            done.AddKeyFrame(0, 192, 48, 48);

            //animation frame for "HIGH" unit
            Animation idleAir = new Animation("idle", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                idleAir.AddKeyFrame(i * 64, 0, 64, 64);
            }

            Animation rightAir = new Animation("right", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                rightAir.AddKeyFrame(i * 64, 64, 64, 64);
            }

            Animation upAir = new Animation("up", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                upAir.AddKeyFrame(i * 64, 128, 64, 64);
            }

            Animation downAir = new Animation("down", true, 4, string.Empty);

            for (int i = 0; i < 4; i++)
            {
                downAir.AddKeyFrame(i * 64, 192, 64, 64);
            }

            Animation doneAir = new Animation("done", true, 1, string.Empty);

            doneAir.AddKeyFrame(0, 256, 64, 64);

            //animation frame for copter unit
            Animation idleCopter = new Animation("idle", true, 3, string.Empty);

            for (int i = 0; i < 3; i++)
            {
                idleCopter.AddKeyFrame(i * 64, 0, 64, 64);
            }

            CONTENT_MANAGER.animationTypes.Add(idle);
            CONTENT_MANAGER.animationTypes.Add(right);
            CONTENT_MANAGER.animationTypes.Add(up);
            CONTENT_MANAGER.animationTypes.Add(down);
            CONTENT_MANAGER.animationTypes.Add(done);

            foreach (SpriteSheetUnit unittype in UnitTypes)
            {
                string         unittypestring = unittype.ToString();
                AnimatedEntity temp           = new AnimatedEntity(Vector2.Zero, Vector2.Zero, Color.White, LayerDepth.Unit);
                //if (unittypestring.Contains(delimit))
                {
                    //break;
                }

                temp.LoadContent(CONTENT_MANAGER.animationSheets[unittype]);

                if (unittypestring.Contains("TransportCopter") ||
                    unittypestring.Contains("BattleCopter") ||
                    unittypestring.Contains("Fighter") ||
                    unittypestring.Contains("Bomber"))
                {
                    //we enter "HIGH" mode
                    //first we set the origin to "HIGH"
                    //because we are drawing from topleft and the sprite size is 64x64
                    temp.Origin = new Vector2(8, 16);

                    //then we load the "HIGH" animation in
                    if (unittypestring.Contains("Copter"))
                    {
                        temp.AddAnimation(idleCopter, rightAir, upAir, downAir, doneAir);
                    }
                    else
                    {
                        temp.AddAnimation(idleAir, rightAir, upAir, downAir, doneAir);
                    }
                }
                else
                {
                    //we enter "normal" mode
                    temp.AddAnimation(idle, right, up, down, done);
                }

                temp.PlayAnimation("idle");
                CONTENT_MANAGER.animationEntities.Add(unittype, temp);
            }
        }