1
0
mirror of https://github.com/Memorix101/UnityXNA/ synced 2024-12-30 15:25:35 +01:00
Barnaby Smith 6fe889760d First commit. Proof of concept implementation.
The XNA 4.0 PlatformerGame sample is successfully running inside Unity3D
3.5.
Implemented a basic game loop, game timing, content loading for
Texture2D, SoundEffect and Song. Emulated SpriteBatch drawing for
sprites and strings (note SpriteFont is not yet supported to all strings
are rendered using the default GUI label font). Songs can be played
using an AudioSource attached to the XNATest game object which acts as
an emulator for MediaPlayer. Playing a SoundEffect creates a game object
with an AudioSource attached which is automatically deleted when the
sound finishes. Implemented keyboard input with a limited set of XNA
Keys mapping to Unity3D KeyCodes.
2012-07-07 20:57:54 +01:00

119 lines
3.6 KiB
C#

#region File Description
//-----------------------------------------------------------------------------
// Gem.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
namespace Platformer
{
/// <summary>
/// A valuable item the player can collect.
/// </summary>
class Gem
{
private Texture2D texture;
private Vector2 origin;
private SoundEffect collectedSound;
public const int PointValue = 30;
public readonly Color Color = Color.Yellow;
// The gem is animated from a base position along the Y axis.
private Vector2 basePosition;
private float bounce;
public Level Level
{
get { return level; }
}
Level level;
/// <summary>
/// Gets the current position of this gem in world space.
/// </summary>
public Vector2 Position
{
get
{
return basePosition + new Vector2(0.0f, bounce);
}
}
/// <summary>
/// Gets a circle which bounds this gem in world space.
/// </summary>
public Circle BoundingCircle
{
get
{
return new Circle(Position, Tile.Width / 3.0f);
}
}
/// <summary>
/// Constructs a new gem.
/// </summary>
public Gem(Level level, Vector2 position)
{
this.level = level;
this.basePosition = position;
LoadContent();
}
/// <summary>
/// Loads the gem texture and collected sound.
/// </summary>
public void LoadContent()
{
texture = Level.Content.Load<Texture2D>("Sprites/Gem");
origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f);
collectedSound = Level.Content.Load<SoundEffect>("Sounds/GemCollected");
}
/// <summary>
/// Bounces up and down in the air to entice players to collect them.
/// </summary>
public void Update(GameTime gameTime)
{
// Bounce control constants
const float BounceHeight = 0.18f;
const float BounceRate = 3.0f;
const float BounceSync = -0.75f;
// Bounce along a sine curve over time.
// Include the X coordinate so that neighboring gems bounce in a nice wave pattern.
double t = gameTime.TotalGameTime.TotalSeconds * BounceRate + Position.X * BounceSync;
bounce = (float)Math.Sin(t) * BounceHeight * texture.Height;
}
/// <summary>
/// Called when this gem has been collected by a player and removed from the level.
/// </summary>
/// <param name="collectedBy">
/// The player who collected this gem. Although currently not used, this parameter would be
/// useful for creating special powerup gems. For example, a gem could make the player invincible.
/// </param>
public void OnCollected(Player collectedBy)
{
collectedSound.Play();
}
/// <summary>
/// Draws a gem in the appropriate color.
/// </summary>
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, Position, null, Color, 0.0f, origin, 1.0f, SpriteEffects.None, 0.0f);
}
}
}