private void SwitchState(GameTime gameTime) { if ((targetDistanceX >= -sprite.Width * 2 && targetDistanceX <= sprite.Width * 2) && (targetDistanceY >= -sprite.Height * 2 && targetDistanceY <= sprite.Height * 2)) { FollowTarget(); if (hasAttacked == false) { Attack(gameTime); attackSound.Play(); timer = new TimeSpan(0, 0, 0, 1, 0); } if (hasAttacked == true) { timer -= gameTime.ElapsedGameTime; if (timer <= TimeSpan.Zero) { hasAttacked = false; } } } if (targetDistanceX > sprite.Width * 2 || targetDistanceX < -sprite.Width * 2 || targetDistanceY > sprite.Height * 2 || targetDistanceY < -sprite.Height * 2) { velocity = new Vector2(0f, 0f); Patrol(); } if (health <= 0) { GameWorld.Destroy(this); } }
protected void HandleAttack(GameTime gameTime) { //Counts down the timer for the duration of the attack timer += gameTime.ElapsedGameTime; //Deletes the instance of the attack when the timer is zero or below zero. if (timer >= new TimeSpan(0, 0, 0, 0, 100)) { GameWorld.Destroy(this); } }
public override void Update(GameTime gameTime) { //Keeps the shadows position relative to its parrent object (crates for now) position.X = parrent.Position.X; position.Y = parrent.Position.Y + (sprite.Height * GameWorld.Scale); //Checks if the shadows parrent is colliding with a sunray and removes it if not if (Parrent.GiveShadow == false) { GameWorld.Destroy(this); } }
protected override void OnCollision(GameObject other) { //Checks if the player is colliding with a shadow and marks them as 'in a shadow' if (other is Shadow) { inShadow = true; inSun = false; } else { inShadow = false; } // Checks if the player is colliding with a sunray and marks them as 'in the sun' if (other is SunRay && inShadow == false) { inSun = true; } if (other is Key && keyState.IsKeyDown(Keys.V)) { playerKeys.Add(other); GameWorld.Destroy(other); } if (other is Door) { if (playerKeys.Contains(other.Parrent)) { other.Unlocked = true; playerKeys.Remove(other.Parrent); } } // Do something when we collide with another object if (other is Wall || other is Vase || other is Sun || other is Chest || (other is Door && other.Unlocked == false)) { intersection = Rectangle.Intersect(other.CollisionBox, CollisionBox); if (intersection.Width > intersection.Height) //Top and bottom. { if (other.Position.Y > position.Y) // When player bottom hits object top. { collidingTop = true; distance = CollisionBox.Bottom - other.CollisionBox.Top; position.Y -= distance; } if (other.Position.Y < position.Y) // When player top hits object bottom. { collidingBottom = true; distance = other.CollisionBox.Bottom - CollisionBox.Top; position.Y += distance; } } else // Left and right. { if (other.Position.X < position.X) // When player left hits object right. { collidingLeft = true; distance = other.CollisionBox.Right - CollisionBox.Left; position.X += distance; } if (other.Position.X > position.X) // When player right hits object left. { collidingRight = true; distance = CollisionBox.Right - other.CollisionBox.Left; position.X -= distance; } } } // Crates can't be walked through when they hit a solid object. if (other is Crate) { intersection = Rectangle.Intersect(other.CollisionBox, CollisionBox); if (intersection.Width > intersection.Height) // TOP OG BOTTOM { if (other.Position.Y > position.Y && (other as Crate).PushDown == false) // When Player bottom hits object top. Pushes the object downwards. { collidingTop = true; distance = CollisionBox.Bottom - other.CollisionBox.Top; position.Y -= distance; } if (other.Position.Y < position.Y && (other as Crate).PushUp == false) // When Player top hits object bottom. Pushes the object upwards. { collidingBottom = true; distance = other.CollisionBox.Bottom - CollisionBox.Top; position.Y += distance; } } else { if (other.Position.X < position.X && (other as Crate).PushRight == false) // When player left hits object right. Pushes the object to the left. { collidingLeft = true; distance = other.CollisionBox.Right - CollisionBox.Left; position.X += distance; } if (other.Position.X > position.X && (other as Crate).PushLeft == false) // When player right hits object left. Pushes the object to the right. { collidingRight = true; distance = CollisionBox.Right - other.CollisionBox.Left; position.X -= distance; } } } }
protected override void Break() { vaseBreakSound.Play(); GameWorld.Destroy(this); }