public Animation(AnimatedSprite sprite, Vector2 loc) { Sprite = sprite; Location = loc; }
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)) { position.X += speed * dt; } break; case Dir.Left: tempPos.X -= speed + dt; if (!Obstacle.didCollide(tempPos, radius)) { position.X -= speed * dt; } break; case Dir.Up: tempPos.Y -= speed + dt; if (!Obstacle.didCollide(tempPos, radius)) { position.Y -= speed * dt; } break; case Dir.Down: tempPos.Y += speed + dt; if (!Obstacle.didCollide(tempPos, radius)) { 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; }
public void Update(GameTime gameTime) { KeyboardState kState = Keyboard.GetState(); float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; if (healthTimer > 0) { healthTimer -= dt; } /// <summary> /// this one liner is a better version of switch statement below. /// Because "directions" is enum, so basically /// it resolves to [0, 1, 2, 3] /// </summary> animSprite = animations[(int)direction]; //switch (direction) //{ // case Dir.Down: // animSprite = animations[0]; // break; // case Dir.Up: // animSprite = animations[1]; // break; // case Dir.Left: // animSprite = animations[2]; // break; // case Dir.Right: // animSprite = animations[3]; // break; // default: // break; //} // for stopping character to always moving if (isMoving) { animSprite.Update(gameTime); } else { animSprite.SetFrame(1); } #region Changing direction // every frame will be false and only if keys are down, it will be true 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; } #endregion if (isMoving) { // holding current player position at this particular moment Vector2 tempPos = position; switch (direction) { case Dir.Right: tempPos.X += speed * dt; if (!Obstacle.DidCollide(tempPos, radius)) { position.X += speed * dt; } break; case Dir.Left: tempPos.X -= speed * dt; if (!Obstacle.DidCollide(tempPos, radius)) { position.X -= speed * dt; } break; case Dir.Up: tempPos.Y -= speed * dt; if (!Obstacle.DidCollide(tempPos, radius)) { position.Y -= speed * dt; } break; case Dir.Down: tempPos.Y += speed * dt; if (!Obstacle.DidCollide(tempPos, radius)) { position.Y += speed * dt; } break; default: break; } } // this makes projectile shoot once only when Space key is pressed down if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space)) { Projectile.projectiles.Add(new Projectile(position, direction)); } kStateOld = kState; }
public void Update() { //Set correct Sprite based on direction to Draw currentAnim = animations[(int)direction]; if (isMoving) { currentAnim.Update(); } else { currentAnim.setCurrentFrame(1); } KeyboardState kState = Keyboard.GetState(); float dt = (float)GameServices.dt; //Set Correct Position and Direction isMoving = false; if (kState.IsKeyDown(Keys.Down)) { direction = Dir.Down; isMoving = true; } if (kState.IsKeyDown(Keys.Up)) { direction = Dir.Up; isMoving = true; } if (kState.IsKeyDown(Keys.Left)) { direction = Dir.Left; isMoving = true; } if (kState.IsKeyDown(Keys.Right)) { direction = Dir.Right; isMoving = true; } if (isMoving) { switch (direction) { case Dir.Down: position.Y += speed * dt; break; case Dir.Up: position.Y -= speed * dt; break; case Dir.Left: position.X -= speed * dt; break; case Dir.Right: position.X += speed * dt; break; default: break; } } }