public void Update(GameTime gameTime) { KeyboardState currentKeyboardState = Keyboard.GetState(); HandleInput(currentKeyboardState); if (!projectileTimerActive && currentKeyboardState.IsKeyDown(Keys.Space)) { Vector2 projectilePosition = new Vector2(position.X + SpriteWidth, position.Y + SpriteHeight / 2); Vector2 projectileVelocity = new Vector2(10.0f, 0.0f); root.FireProjectile(projectilePosition, projectileVelocity); projectileTimerActive = true; projectileTimer = 0.0f; } if (projectileTimerActive) { projectileTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (projectileTimer >= projectileCoolDownTime) { projectileTimerActive = false; } } }
// Called each frame public void Update(GameTime gameTime, Vector2 playerPosition) { // Handle movement this.playerPosition = playerPosition; velocity.X = (playerPosition.X - position.X); velocity.Y = (playerPosition.Y - position.Y); velocity.Normalize(); // Bounce on top and bottom if (position.Y <= 0 || position.Y > (root.ScreenHeight * 10 - SpriteHeight)) { velocity.Y *= -1; } if (position.X <= 0 || position.X > (root.ScreenWidth * 10 - SpriteWidth)) { velocity.X *= -1; } position += (velocity * speed); projectileCooldown.Update(gameTime); if (!projectileCooldown.Active) { projectileCooldown.StartTimer(); Vector2 projectilePosition = new Vector2(); projectilePosition.X = position.X; projectilePosition.Y = position.Y + (SpriteHeight / 2); Vector2 projectileVelocity = new Vector2(); projectileVelocity.X = (playerPosition.X - position.X); projectileVelocity.Y = (playerPosition.Y - position.Y); projectileVelocity.Normalize(); projectileVelocity *= 5; if (EnemyType == EnemyType.Wizard) { root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.EnemyFire); } else if (EnemyType == EnemyType.Boss) { root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.BossFire); } } }
private void HandleInput(KeyboardState currentKeyboardState) { bool upKeyPressed = currentKeyboardState.IsKeyDown(Keys.Up); bool downKeyPressed = currentKeyboardState.IsKeyDown(Keys.Down); bool leftKeyPressed = currentKeyboardState.IsKeyDown(Keys.Left); bool rightKeyPressed = currentKeyboardState.IsKeyDown(Keys.Right); bool spaceKeyPressed = currentKeyboardState.IsKeyDown(Keys.Space); if (currentKeyboardState.IsKeyDown(Keys.M)) { movementSpeed = movementSpeed = 40; projectileCoolDown = projectileCoolDown = new Timer(0.05f); SpriteWidth = SpriteWidth = 20; SpriteImage = SpriteImage = root.Content.Load <Texture2D>("Mandalorian"); root.life++; } if (currentKeyboardState.IsKeyDown(Keys.N)) { root.life++; } if (upKeyPressed) { position.Y -= movementSpeed; } if (downKeyPressed) { position.Y += movementSpeed; } if (leftKeyPressed) { position.X -= movementSpeed; } if (rightKeyPressed) { position.X += movementSpeed; } if (spaceKeyPressed && !projectileCoolDown.Active) { Vector2 projectilePosition; Vector2 projectileVelocity; projectilePosition = new Vector2(position.X + (SpriteWidth / 2), position.Y + (SpriteHeight / 2)); projectileVelocity = new Vector2(10.0f, 0.0f); root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Player); projectileCoolDown.StartTimer(); } }
public void Update(GameTime gameTime) { position += velocity; if (position.Y < 0 || position.Y > (root.ScreenHeight - SpriteHeight)) { velocity.Y *= -1; } projectileCoolDown.Update(gameTime); if (!projectileCoolDown.Active) { projectileCoolDown.StartTimer(); Vector2 projectilePosition = new Vector2(position.X, position.Y + SpriteHeight / 2); Vector2 projectileVelocity = new Vector2(-5.0f, 0.0f); root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Enemy); } }
public void Update(GameTime gameTime) { position += velocity; if (position.Y < 0 || position.Y > (root.ScreenHeight - SpriteHeight)) { velocity.Y *= -1; } Vector2 projectilePosition = new Vector2(); projectilePosition.X = position.X; projectilePosition.Y = position.Y + (SpriteHeight / 2); Vector2 projectileVelocity = new Vector2(); projectileVelocity.X = -5.0f; projectileVelocity.Y = 0f; root.FireProjectile(projectilePosition, projectileVelocity); }
public void HandleInput(KeyboardState currentKeyboardState, MouseState currentMouseState, FieldBounds fieldBounds, Vector2 cameraPosition) { Point currentMousePosition = currentMouseState.Position; bool wKeyPressed = currentKeyboardState.IsKeyDown(Keys.W); if (wKeyPressed && position.Y >= 0 + movementSpeed) { position.Y -= movementSpeed; //position.Y = position.Y - movementSpeed } bool sKeyPressed = currentKeyboardState.IsKeyDown(Keys.S); if (sKeyPressed && position.Y < root.ScreenHeight * 10) { position.Y += movementSpeed; } bool aKeyPressed = currentKeyboardState.IsKeyDown(Keys.A); if (aKeyPressed && position.X >= 0 + movementSpeed) { position.X -= movementSpeed; } bool dKeyPressed = currentKeyboardState.IsKeyDown(Keys.D); if (dKeyPressed && position.X < root.ScreenWidth * 10) { position.X += movementSpeed; } bool spaceKeyPressed = currentKeyboardState.IsKeyDown(Keys.Space); if (spaceKeyPressed && !firingProjectile) { Vector2 projectilePosition; Vector2 projectileVelocity; projectilePosition = new Vector2(position.X + (SpriteWidth / 2), position.Y + (SpriteHeight / 2)); //projectileVelocity = new Vector2(10.0f, 0.0f); //original //These two lines do the same thing as the following, except with a lot of math instead of code. //projectileVelocity.X = ( 10*(currentMousePosition.X-position.X)/(Convert.ToInt32( Math.Sqrt( Math.Pow((currentMousePosition.X-position.X),2) + Math.Pow(((currentMousePosition.Y-position.Y)),2) ) ) ) ); //works //projectileVelocity.Y = ( 10*(currentMousePosition.Y-position.Y)/(Convert.ToInt32( Math.Sqrt( Math.Pow((currentMousePosition.X-position.X),2) + Math.Pow(((currentMousePosition.Y-position.Y)),2) ) ) ) ); //works //the good stuff: // projectileVelocity.X = (currentMousePosition.X-position.X); // projectileVelocity.Y = (currentMousePosition.Y-position.Y); projectileVelocity.X = ((currentMousePosition.X + fieldBounds.ScreenBoundaries.X) - position.X); projectileVelocity.Y = ((currentMousePosition.Y + fieldBounds.ScreenBoundaries.Y) - position.Y); projectileVelocity.Normalize(); projectileVelocity *= 7; root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Player); firingProjectile = true; } /* How to fire consistently * bool eKeyPressed = currentKeyboardState.IsKeyDown(Keys.E); * if (eKeyPressed && !cooldownTimer.Active) * { * Vector2 projectilePosition; * Vector2 projectileVelocity; * * projectilePosition = new Vector2(position.X + (SpriteWidth / 2), position.Y + (SpriteHeight / 2)); * projectileVelocity = new Vector2(10.0f, 0.0f); * root.FireProjectile(projectilePosition, projectileVelocity, ProjectileType.Player); * cooldownTimer.StartTimer(); * } */ // Shop Management: bool tKeyPressed = currentKeyboardState.IsKeyDown(Keys.T); if (tKeyPressed && (root.points >= 5)) { position.X = currentMousePosition.X; position.Y = currentMousePosition.Y; //position.X = fieldBounds.OriginalScreenMiddle.X+200; //position.Y = fieldBounds.OriginalScreenMiddle.Y+200; root.points -= 5; } bool qKeyPressed = currentKeyboardState.IsKeyDown(Keys.Q); if (qKeyPressed && (root.points >= 10)) { root.life++; root.points -= 10; } bool eKeyPressed = currentKeyboardState.IsKeyDown(Keys.E); if (eKeyPressed && (root.points >= 30)) { //Nothing yet } }