// Initialize the player public void Initialize(String animationDir, Vector2 position) { string[] filePaths = Directory.GetFiles("Content/" + animationDir); int i = 0; foreach (string file in filePaths) { Texture2D animTexture = parentGame.Content.Load<Texture2D>(animationDir + "/" + Path.GetFileNameWithoutExtension(file)); Animation anim = new Animation(); if (file.ToLower().Contains("back")) { Animations.Add("walkBack", anim); CurrentAnimation = anim; anim.Initialize(animTexture, Vector2.Zero, 6, 30, Color.White, 2f, true); } if (file.ToLower().Contains("front")) { Animations.Add("walkFront", anim); anim.Initialize(animTexture, Vector2.Zero, 6, 30, Color.White, 2f, true); } if (file.ToLower().Contains("side")) { Animations.Add("walkLeft", anim); anim.Initialize(animTexture, Vector2.Zero, 6, 30, Color.White, 2f, true); anim = new Animation(); Animations.Add("walkRight", anim); anim.Initialize(animTexture, Vector2.Zero, 6, 30, Color.White, 2f, true); anim.Flip = true; } if (file.ToLower().Contains("stand")) { Animations.Add("stand", anim); anim.Initialize(animTexture, Vector2.Zero, 3, 30, Color.White, 2f, false); } if (file.ToLower().Contains("attack")) { Animations.Add("attack", anim); anim.Initialize(animTexture, Vector2.Zero, 15, 30, Color.White, 2f, true); } i++; } // Set the starting position of the player around the middle of the screen and to the back Position = position; // Set the player to be active Active = true; // Set the player health Health = 100; worldPosition = position; }
protected override void Initialize() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch( GraphicsDevice ); IsMouseVisible = true; // Makes the mouse cursor visible prevState = Keyboard.GetState(); // Create the character theDude = new TheDude( this ); theDude2 = new PlayerSprite( this ); Animation playerAnimation = new Animation(); Texture2D playerTexture = Content.Load<Texture2D>("Windlasher/walkside"); playerAnimation.Initialize(playerTexture, Vector2.Zero, 6, 30, Color.White, 2f, true); theDude2.Initialize("WindLasher", new Vector2(298,212)); Components.Add( theDude2 ); // Create the debug helper as a service dbg = new DbgAssistor( this ); dbg.SetSpritebatch( spriteBatch ); Services.AddService( typeof(IDbgAssistor), dbg ); Components.Add( dbg ); // Add some debugging info SetupDebugTexts(); base.Initialize(); }
// Update the player animation public override void Update(GameTime gameTime) { Vector2 motion = Vector2.Zero; CurrentAnimation = Animations["stand"]; KeyboardState keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Keys.I) || keyState.IsKeyDown(Keys.W)) { motion.Y--; CurrentAnimation = Animations["walkBack"]; } if (keyState.IsKeyDown(Keys.K) || keyState.IsKeyDown(Keys.S)) { motion.Y++; CurrentAnimation = Animations["walkFront"]; } if (keyState.IsKeyDown(Keys.L) || keyState.IsKeyDown(Keys.D)) { motion.X++; CurrentAnimation = Animations["walkRight"]; } if (keyState.IsKeyDown(Keys.J) || keyState.IsKeyDown(Keys.A)) { motion.X--; CurrentAnimation = Animations["walkLeft"]; } if (keyState.IsKeyDown(Keys.J) || keyState.IsKeyDown(Keys.F)) { CurrentAnimation = Animations["attack"]; } if (motion != Vector2.Zero) { motion.Normalize(); } Vector2 nextPosition = Position + (motion * Speed); Position = nextPosition; worldPosition = Position; int height = parentGame.tileMap.GetHeightAtWorldPosition(worldPosition); // Adjust the draw depth float drawDepth = parentGame.tileMap.GetDrawDepth(worldPosition, height); CurrentAnimation.drawDepth = drawDepth; //Update the animation CurrentAnimation.Update(gameTime); //Console.WriteLine(Position); parentGame.camera.CenterOn(Position); base.Update(gameTime); }