tasting on MonoGame
[tastes.git] / Platformer2D / Platformer2D.Core / Game / Gem.cs
blob1aa5114195856267f65025edfdfa7cf67c4ee595
1 #region File Description
2 //-----------------------------------------------------------------------------
3 // Gem.cs
4 //
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
8 #endregion
10 using System;
11 using Microsoft.Xna.Framework;
12 using Microsoft.Xna.Framework.Graphics;
13 using Microsoft.Xna.Framework.Audio;
15 namespace Platformer2D
17 /// <summary>
18 /// A valuable item the player can collect.
19 /// </summary>
20 class Gem
22 private Texture2D texture;
23 private Vector2 origin;
24 private SoundEffect collectedSound;
26 public readonly int PointValue = 30;
27 public readonly Color Color = Color.Yellow;
29 // The gem is animated from a base position along the Y axis.
30 private Vector2 basePosition;
31 private float bounce;
33 public Level Level
35 get { return level; }
37 Level level;
39 /// <summary>
40 /// Gets the current position of this gem in world space.
41 /// </summary>
42 public Vector2 Position
44 get
46 return basePosition + new Vector2(0.0f, bounce);
50 /// <summary>
51 /// Gets a circle which bounds this gem in world space.
52 /// </summary>
53 public Circle BoundingCircle
55 get
57 return new Circle(Position, Tile.Width / 3.0f);
61 /// <summary>
62 /// Constructs a new gem.
63 /// </summary>
64 public Gem(Level level, Vector2 position)
66 this.level = level;
67 this.basePosition = position;
69 LoadContent();
72 /// <summary>
73 /// Loads the gem texture and collected sound.
74 /// </summary>
75 public void LoadContent()
77 texture = Level.Content.Load<Texture2D>("Sprites/Gem");
78 origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
79 collectedSound = Level.Content.Load<SoundEffect>("Sounds/GemCollected");
82 /// <summary>
83 /// Bounces up and down in the air to entice players to collect them.
84 /// </summary>
85 public void Update(GameTime gameTime)
87 // Bounce control constants
88 const float BounceHeight = 0.18f;
89 const float BounceRate = 3.0f;
90 const float BounceSync = -0.75f;
92 // Bounce along a sine curve over time.
93 // Include the X coordinate so that neighboring gems bounce in a nice wave pattern.
94 double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
95 bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
98 /// <summary>
99 /// Called when this gem has been collected by a player and removed from the level.
100 /// </summary>
101 /// <param name="collectedBy">
102 /// The player who collected this gem. Although currently not used, this parameter would be
103 /// useful for creating special power-up gems. For example, a gem could make the player invincible.
104 /// </param>
105 public void OnCollected(Player collectedBy)
107 collectedSound.Play();
110 /// <summary>
111 /// Draws a gem in the appropriate color.
112 /// </summary>
113 public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
115 spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);