Exemple #1
0
 public void Initialize(Animation animation, Vector2 position)
 {
     EnemyAnimation = animation;
     Position = position;
     Active = true;
     Damage = 5;
     enemyMoveSpeed = 6f;
     Health = 10;
     Value = 100;
 }
        public MCharacter(GraphicsDevice graphicsDevice)
        {
            if (CharacterSheetTexture == null)
            {
                using (var stream = TitleContainer.OpenStream("Content/charactersheet.png"))
                {
                    CharacterSheetTexture = Texture2D.FromStream(graphicsDevice, stream);
                }
            }
            X = 100;
            Y = 100;
            collider = new Rectangle(0, 0, 16, 16);

            walkDown = new Animation();
            walkDown.AddFrame(new Rectangle(0, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkDown.AddFrame(new Rectangle(16, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkDown.AddFrame(new Rectangle(0, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkDown.AddFrame(new Rectangle(32, 0, 16, 16), TimeSpan.FromSeconds(.25));

            walkUp = new Animation();
            walkUp.AddFrame(new Rectangle(144, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkUp.AddFrame(new Rectangle(160, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkUp.AddFrame(new Rectangle(144, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkUp.AddFrame(new Rectangle(176, 0, 16, 16), TimeSpan.FromSeconds(.25));

            walkLeft = new Animation();
            walkLeft.AddFrame(new Rectangle(48, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkLeft.AddFrame(new Rectangle(64, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkLeft.AddFrame(new Rectangle(48, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkLeft.AddFrame(new Rectangle(80, 0, 16, 16), TimeSpan.FromSeconds(.25));

            walkRight = new Animation();
            walkRight.AddFrame(new Rectangle(96, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkRight.AddFrame(new Rectangle(112, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkRight.AddFrame(new Rectangle(96, 0, 16, 16), TimeSpan.FromSeconds(.25));
            walkRight.AddFrame(new Rectangle(128, 0, 16, 16), TimeSpan.FromSeconds(.25));

            // Standing animations only have a single frame of animation:
            standDown = new Animation();
            standDown.AddFrame(new Rectangle(0, 0, 16, 16), TimeSpan.FromSeconds(.25));

            standUp = new Animation();
            standUp.AddFrame(new Rectangle(144, 0, 16, 16), TimeSpan.FromSeconds(.25));

            standLeft = new Animation();
            standLeft.AddFrame(new Rectangle(48, 0, 16, 16), TimeSpan.FromSeconds(.25));

            standRight = new Animation();
            standRight.AddFrame(new Rectangle(96, 0, 16, 16), TimeSpan.FromSeconds(.25));
        }
 public void Update(GameTime gameTime)
 {
     var velocity = GetDesiredVelocityFromInput();
     this.X += velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
     this.Y += velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
     this.collider.Location = new Point((int)this.X, (int)this.Y);
     if (velocity != Vector2.Zero)
     {
         bool movingHorizontally = Math.Abs(velocity.X) > Math.Abs(velocity.Y);
         if (movingHorizontally)
         {
             if (velocity.X > 0)
             {
                 currentAnimation = walkRight;
             }
             else
             {
                 currentAnimation = walkLeft;
             }
         }
         else
         {
             if (velocity.Y > 0)
             {
                 currentAnimation = walkDown;
             }
             else
             {
                 currentAnimation = walkUp;
             }
         }
     }
     else
     {
         if (currentAnimation == walkRight)
         {
             currentAnimation = standRight;
         }
         else if (currentAnimation == walkLeft)
         {
             currentAnimation = standLeft;
         }
         else if (currentAnimation == walkUp)
         {
             currentAnimation = standUp;
         }
         else if (currentAnimation == walkDown)
         {
             currentAnimation = standDown;
         }
         else if (currentAnimation == null)
         {
             currentAnimation = standDown;
         }
     }
     currentAnimation.Update(gameTime);
 }