public void Update(GameTime gameTime) { KeyboardState kState = Keyboard.GetState(); float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; if (healthTimer > 0) { healthTimer -= dt; } anim = animations[(int)direction]; if (isMoving) { anim.Update(gameTime); } else { anim.setFrame(1); } isMoving = false; if (kState.IsKeyDown(Keys.Right)) { direction = Dir.Right; isMoving = true; } if (kState.IsKeyDown(Keys.Left)) { direction = Dir.Left; isMoving = true; } if (kState.IsKeyDown(Keys.Up)) { direction = Dir.Up; isMoving = true; } if (kState.IsKeyDown(Keys.Down)) { direction = Dir.Down; isMoving = true; } if (isMoving) { Vector2 tempPos = position; switch (direction) { case Dir.Right: tempPos.X += speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.X < mapW) { position.X += speed * dt; } break; case Dir.Left: tempPos.X -= speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.X > 0) { position.X -= speed * dt; } break; case Dir.Down: tempPos.Y += speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y < mapH) { position.Y += speed * dt; } break; case Dir.Up: tempPos.Y -= speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y > 0) { position.Y -= speed * dt; } break; default: break; } } if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space)) { Projectile.projectiles.Add(new Projectile(position, direction)); } kStateOld = kState; }
// Methods // player movement public void Update(GameTime gameTime, int mapW, int mapH) // int mapW, int mapH is for the right and bottom camera edges { KeyboardState kstate = Keyboard.GetState(); float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; // so players health doesnt decline too fast when hit if (healthTimer > 0) { healthTimer -= dt; } // set the animation based on direction player is facing anim = animations[(int)direction]; // this cycles through the player animations if (isMoving) { anim.Update(gameTime); } else { anim.setFrame(1); // return animation to standing still frame } // this means every frame the player stops moving isMoving = false; // however if the keys are held, player will move if (kstate.IsKeyDown(Keys.Right)) { direction = Dir.Right; isMoving = true; } if (kstate.IsKeyDown(Keys.Left)) { direction = Dir.Left; isMoving = true; } if (kstate.IsKeyDown(Keys.Up)) { direction = Dir.Up; isMoving = true; } if (kstate.IsKeyDown(Keys.Down)) { direction = Dir.Down; isMoving = true; } // movement speed if (isMoving) { Vector2 tempPos = position; // for obstable collision switch (direction) { case Dir.Down: tempPos.Y += speed * dt; // put next move into variable and pass to Obstacle.didCollide if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y < mapH) // if collision is NOT made and not at edge of screen { position.Y += speed * dt; // do the actual movement } break; case Dir.Up: tempPos.Y -= speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y > 0) { position.Y -= speed * dt; } break; case Dir.Left: tempPos.X -= speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.X > 0) { position.X -= speed * dt; } break; case Dir.Right: tempPos.X += speed * dt; if (!Obstacle.didCollide(tempPos, radius) && tempPos.X < mapW) { position.X += speed * dt; } break; default: break; } } // when spacebar is pressed, create ONE projectile if (kstate.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space)) { Projectile.projectiles.Add(new Projectile(position, direction)); MySounds.projectileSound.Play(1f, 0.5f, 0f); } kStateOld = kstate; }