mirror of
https://github.com/Memorix101/UnityXNA/
synced 2024-12-30 15:25:35 +01:00
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.
67 lines
2.7 KiB
C#
67 lines
2.7 KiB
C#
#region File Description
|
|
//-----------------------------------------------------------------------------
|
|
// RectangleExtensions.cs
|
|
//
|
|
// Microsoft XNA Community Game Platform
|
|
// Copyright (C) Microsoft Corporation. All rights reserved.
|
|
//-----------------------------------------------------------------------------
|
|
#endregion
|
|
|
|
using System;
|
|
using Microsoft.Xna.Framework;
|
|
|
|
namespace Platformer
|
|
{
|
|
/// <summary>
|
|
/// A set of helpful methods for working with rectangles.
|
|
/// </summary>
|
|
public static class RectangleExtensions
|
|
{
|
|
/// <summary>
|
|
/// Calculates the signed depth of intersection between two rectangles.
|
|
/// </summary>
|
|
/// <returns>
|
|
/// The amount of overlap between two intersecting rectangles. These
|
|
/// depth values can be negative depending on which wides the rectangles
|
|
/// intersect. This allows callers to determine the correct direction
|
|
/// to push objects in order to resolve collisions.
|
|
/// If the rectangles are not intersecting, Vector2.Zero is returned.
|
|
/// </returns>
|
|
public static Vector2 GetIntersectionDepth(this Rectangle rectA, Rectangle rectB)
|
|
{
|
|
// Calculate half sizes.
|
|
float halfWidthA = rectA.Width / 2.0f;
|
|
float halfHeightA = rectA.Height / 2.0f;
|
|
float halfWidthB = rectB.Width / 2.0f;
|
|
float halfHeightB = rectB.Height / 2.0f;
|
|
|
|
// Calculate centers.
|
|
Vector2 centerA = new Vector2(rectA.Left + halfWidthA, rectA.Top + halfHeightA);
|
|
Vector2 centerB = new Vector2(rectB.Left + halfWidthB, rectB.Top + halfHeightB);
|
|
|
|
// Calculate current and minimum-non-intersecting distances between centers.
|
|
float distanceX = centerA.X - centerB.X;
|
|
float distanceY = centerA.Y - centerB.Y;
|
|
float minDistanceX = halfWidthA + halfWidthB;
|
|
float minDistanceY = halfHeightA + halfHeightB;
|
|
|
|
// If we are not intersecting at all, return (0, 0).
|
|
if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
|
|
return Vector2.Zero;
|
|
|
|
// Calculate and return intersection depths.
|
|
float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
|
|
float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
|
|
return new Vector2(depthX, depthY);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the position of the center of the bottom edge of the rectangle.
|
|
/// </summary>
|
|
public static Vector2 GetBottomCenter(this Rectangle rect)
|
|
{
|
|
return new Vector2(rect.X + rect.Width / 2.0f, rect.Bottom);
|
|
}
|
|
}
|
|
}
|