//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------
namespace DarkWynter.Stream
{
#region Using Statements
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.ComponentModel;
using DarkWynter.Stream.UIInterfacing;
#endregion
///
/// Camera class
///
public class Camera
{
///
/// Viewport associated with this Camera
///
public Viewport _viewport;
[EditorAttribute(typeof(ObjectPropertyEditor), typeof(System.Drawing.Design.UITypeEditor))]
public Viewport viewport { get { return _viewport; } set { _viewport = value; } }
///
/// Allows user to move camera around relative the Mass's currentPosition
///
private Vector3 _offset = new Vector3(0);
public Vector3 offset { get { return _offset; } set { _cameraPosition = value; } }
private Vector3 _firstPersonOffset = new Vector3(0, 0, 0);
public Vector3 firstPersonOffset { get { return _firstPersonOffset; } set { _firstPersonOffset = value; } }
private Vector3 _thirdPersonOffset = new Vector3(400, 900, 400);
public Vector3 thirdPersonOffset { get { return _thirdPersonOffset; } set { _thirdPersonOffset = value; } }
private Vector3 _fixedPosition = Vector3.One;
public Vector3 fixedPosition { get { return _fixedPosition; } set { _fixedPosition = value; } }
///
/// Camera Modes:
/// Free - No Collision; mainly for development.
/// FirstPerson - See through the eyes of the character.
/// ThirdPerson - Follow character from a fixed distance.
/// Fixed - Camera position remains constant.
/// Spline - Camera moves from one point to another over time.
///
public enum CameraMode { FirstPerson, ThirdPerson, FixedPosition}
private CameraMode _mode = CameraMode.FirstPerson;
public CameraMode mode { get { return _mode; } set { _mode = value; } }
private Vector3 _cameraPosition;
public Vector3 cameraPosition { get { return _cameraPosition; } set { _cameraPosition = value; } }
public Vector3 _lookAtPosition;
public Vector3 lookAtPosition { get { return _lookAtPosition; } set { _lookAtPosition = value; } }
public Vector3 _upVec;
public Vector3 upVec { get { return _upVec; } set { _upVec = value; } }
///
/// Constructor
///
public Camera()
{
viewport = new Viewport();
// G2L override
offset = thirdPersonOffset;
mode = CameraMode.ThirdPerson;
}
private void GetOffset(bool fpvEnabled)
{
// First person view
if (fpvEnabled)
{
if (offset.Length() > firstPersonOffset.Length())
{
offset -= thirdPersonOffset / 100;
}
}
// Third person view
else
{
if (offset.Length() < thirdPersonOffset.Length())
{
offset += thirdPersonOffset / 100;
}
}
}
///
/// Generates the View Matrix based on the mass's position and the camera offset
///
/// Player's current position
/// Player's normal Vector
/// Player's up vector
/// First Person View enabled/disabled
/// View Matrix
public Matrix GetViewMatrix(Vector3 currentPosition, Vector3 normalVector, Vector3 upVector, bool fpvEnabled)
{
GetOffset(fpvEnabled);
// Calculate new camera position relative to current position
cameraPosition = currentPosition - (offset * normalVector);
// Calculate new lookAtPosition in the opposite direction of cameraPosition relative to current position
lookAtPosition = currentPosition + normalVector + (offset * normalVector);
// Use just the height value or camera goes wobbly sideways
upVec = upVector + new Vector3(0, offset.Y, 0);
// Make sure camera doesn't go below player position (clipping)
if(cameraPosition.Y < currentPosition.Y)
_cameraPosition.Y = currentPosition.Y;
// return final view matrix
return Matrix.CreateLookAt(
cameraPosition,
lookAtPosition,
upVec
);
}
///
/// Generates the projection matrix
///
/// Projection matrix
public Matrix GetProjectionMatrix()
{
return
Matrix.CreatePerspectiveFieldOfView(
(float)Math.PI / 4,
viewport.Width / viewport.Height,
0.3f, 100000f
);
}
}
}