1
#region File Description
2 //-----------------------------------------------------------------------------
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
11 using Microsoft
.Xna
.Framework
;
12 using Microsoft
.Xna
.Framework
.Graphics
;
13 using Microsoft
.Xna
.Framework
.Audio
;
15 namespace Platformer2D
18 /// A valuable item the player can collect.
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
;
40 /// Gets the current position of this gem in world space.
42 public Vector2 Position
46 return basePosition
+ new Vector2(0.0f
, bounce
);
51 /// Gets a circle which bounds this gem in world space.
53 public Circle BoundingCircle
57 return new Circle(Position
, Tile
.Width
/ 3.0f
);
62 /// Constructs a new gem.
64 public Gem(Level level
, Vector2 position
)
67 this.basePosition
= position
;
73 /// Loads the gem texture and collected sound.
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");
83 /// Bounces up and down in the air to entice players to collect them.
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
;
99 /// Called when this gem has been collected by a player and removed from the level.
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.
105 public void OnCollected(Player collectedBy
)
107 collectedSound
.Play();
111 /// Draws a gem in the appropriate color.
113 public void Draw(GameTime gameTime
, SpriteBatch spriteBatch
)
115 spriteBatch
.Draw(texture
, Position
, null, Color
, 0.0f
, origin
, 1.0f
, SpriteEffects
.None
, 0.0f
);