2012-03-19 23:57:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using FarseerPhysics;
|
|
|
|
|
using FarseerPhysics.DebugViews;
|
|
|
|
|
using FarseerPhysics.Dynamics;
|
|
|
|
|
using FarseerPhysics.Dynamics.Joints;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Content;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2012-04-13 03:09:49 +00:00
|
|
|
|
using FarseerPhysics.SamplesFramework;
|
2012-03-21 02:44:43 +00:00
|
|
|
|
using Axios.Engine;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
namespace GameStateManagement
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
public class PhysicsGameScreen : GameScreen
|
|
|
|
|
{
|
|
|
|
|
public Camera2D Camera;
|
|
|
|
|
protected DebugViewXNA DebugView;
|
|
|
|
|
public World World;
|
|
|
|
|
|
|
|
|
|
private float _agentForce;
|
|
|
|
|
private float _agentTorque;
|
|
|
|
|
private FixedMouseJoint _fixedMouseJoint;
|
|
|
|
|
private Body _userAgent;
|
|
|
|
|
|
|
|
|
|
protected PhysicsGameScreen()
|
|
|
|
|
{
|
|
|
|
|
TransitionOnTime = TimeSpan.FromSeconds(0.75);
|
|
|
|
|
TransitionOffTime = TimeSpan.FromSeconds(0.75);
|
2012-03-21 02:44:43 +00:00
|
|
|
|
#if DEBUG
|
2012-03-19 23:57:59 +00:00
|
|
|
|
EnableCameraControl = true;
|
2012-03-21 02:44:43 +00:00
|
|
|
|
HasCursor = true;
|
|
|
|
|
#else
|
|
|
|
|
EnableCameraControl = false;
|
|
|
|
|
HasCursor = false;
|
|
|
|
|
#endif
|
2012-03-19 23:57:59 +00:00
|
|
|
|
_userAgent = null;
|
|
|
|
|
World = null;
|
|
|
|
|
Camera = null;
|
|
|
|
|
DebugView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool EnableCameraControl { get; set; }
|
|
|
|
|
|
|
|
|
|
protected void SetUserAgent(Body agent, float force, float torque)
|
|
|
|
|
{
|
|
|
|
|
_userAgent = agent;
|
|
|
|
|
_agentForce = force;
|
|
|
|
|
_agentTorque = torque;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
|
|
public override void Activate(bool instancePreserved)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (!instancePreserved)
|
|
|
|
|
{
|
|
|
|
|
base.Activate(instancePreserved);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
//We enable diagnostics to show get values for our performance counters.
|
|
|
|
|
Settings.EnableDiagnostics = true;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (World == null)
|
|
|
|
|
{
|
|
|
|
|
World = new World(Vector2.Zero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
World.Clear();
|
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (DebugView == null)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (!Axios.Settings.ScreenSaver)
|
|
|
|
|
{
|
2012-04-18 03:21:33 +00:00
|
|
|
|
ContentManager man = new ContentManager(this.ScreenManager.Game.Services, "Content");
|
2012-04-13 03:09:49 +00:00
|
|
|
|
DebugView = new DebugViewXNA(World);
|
|
|
|
|
DebugView.RemoveFlags(DebugViewFlags.Shape);
|
|
|
|
|
DebugView.RemoveFlags(DebugViewFlags.Joint);
|
|
|
|
|
DebugView.DefaultShapeColor = Color.White;
|
|
|
|
|
DebugView.SleepingShapeColor = Color.LightGray;
|
2012-04-18 03:21:33 +00:00
|
|
|
|
DebugView.LoadContent(ScreenManager.GraphicsDevice, man);
|
2012-04-13 03:09:49 +00:00
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (Camera == null)
|
|
|
|
|
{
|
|
|
|
|
Camera = new Camera2D(ScreenManager.GraphicsDevice);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Camera.ResetCamera();
|
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
// Loading may take a while... so prevent the game from "catching up" once we finished loading
|
|
|
|
|
ScreenManager.Game.ResetElapsedTime();
|
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
|
|
|
|
|
{
|
|
|
|
|
if (!coveredByOtherScreen && !otherScreenHasFocus)
|
|
|
|
|
{
|
|
|
|
|
// variable time step but never less then 30 Hz
|
|
|
|
|
World.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalSeconds, (1f / 30f)));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
World.Step(0f);
|
|
|
|
|
}
|
|
|
|
|
Camera.Update(gameTime);
|
|
|
|
|
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void CleanUp()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
public override void HandleInput(GameTime gameTime, InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
// Control debug view
|
2012-04-13 03:09:49 +00:00
|
|
|
|
PlayerIndex player;
|
|
|
|
|
if (input.IsNewButtonPress(Buttons.Start, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Shape);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.DebugPanel);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.PerformanceGraph);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Joint);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.ContactPoints);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.ContactNormals);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Controllers);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F1, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Shape);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F2, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.DebugPanel);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.PerformanceGraph);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F3, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Joint);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F4, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.ContactPoints);
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.ContactNormals);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F5, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.PolygonPoints);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F6, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.Controllers);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F7, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.CenterOfMass);
|
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
if (input.IsNewKeyPress(Keys.F8, ControllingPlayer.Value, out player))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
EnableOrDisableFlag(DebugViewFlags.AABB);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-21 02:44:43 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (_userAgent != null)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-03-21 02:44:43 +00:00
|
|
|
|
HandleUserAgent(input);
|
|
|
|
|
}
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-03-21 02:44:43 +00:00
|
|
|
|
if (EnableCameraControl)
|
|
|
|
|
{
|
|
|
|
|
HandleCamera(input, gameTime);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
|
if (HasCursor)
|
|
|
|
|
{
|
|
|
|
|
HandleCursor(input);
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
PlayerIndex i;
|
|
|
|
|
if (input.IsNewButtonPress(Buttons.Back, PlayerIndex.One, out i) || input.IsNewKeyPress(Keys.Escape, PlayerIndex.One, out i))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-05-06 04:18:31 +00:00
|
|
|
|
//if (this.ScreenState == GameStateManagement.ScreenState.Active && this.TransitionPosition == 0 && this.TransitionAlpha == 1)
|
|
|
|
|
//{ //Give the screens a chance to transition
|
2012-03-21 02:44:43 +00:00
|
|
|
|
CleanUp();
|
|
|
|
|
ExitScreen();
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
2012-05-06 04:18:31 +00:00
|
|
|
|
//}
|
2012-03-21 02:44:43 +00:00
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
base.HandleInput(gameTime, input);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
|
|
public virtual void HandleCursor(InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-04-13 03:09:49 +00:00
|
|
|
|
PlayerIndex player;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
Vector2 position = Camera.ConvertScreenToWorld(input.Cursor);
|
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if ((input.IsNewButtonPress(Buttons.A, PlayerIndex.One, out player) ||
|
2012-03-19 23:57:59 +00:00
|
|
|
|
input.IsNewMouseButtonPress(MouseButtons.LeftButton)) &&
|
|
|
|
|
_fixedMouseJoint == null)
|
|
|
|
|
{
|
|
|
|
|
Fixture savedFixture = World.TestPoint(position);
|
2012-03-21 02:44:43 +00:00
|
|
|
|
if (savedFixture != null && savedFixture.UserData is SimpleAxiosGameObject && ((SimpleAxiosGameObject)(savedFixture.UserData)).AllowAutomaticMouseJoint)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
Body body = savedFixture.Body;
|
|
|
|
|
_fixedMouseJoint = new FixedMouseJoint(body, position);
|
|
|
|
|
_fixedMouseJoint.MaxForce = 1000.0f * body.Mass;
|
|
|
|
|
World.AddJoint(_fixedMouseJoint);
|
|
|
|
|
body.Awake = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-13 03:09:49 +00:00
|
|
|
|
|
|
|
|
|
if ((input.IsNewButtonRelease(Buttons.A, ControllingPlayer.Value, out player) ||
|
2012-04-18 04:34:46 +00:00
|
|
|
|
input.IsNewMouseButtonRelease(MouseButtons.LeftButton)) &&
|
2012-03-19 23:57:59 +00:00
|
|
|
|
_fixedMouseJoint != null)
|
|
|
|
|
{
|
|
|
|
|
World.RemoveJoint(_fixedMouseJoint);
|
|
|
|
|
_fixedMouseJoint = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_fixedMouseJoint != null)
|
|
|
|
|
{
|
|
|
|
|
_fixedMouseJoint.WorldAnchorB = position;
|
|
|
|
|
}
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
private void HandleCamera(InputState input, GameTime gameTime)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
Vector2 camMove = Vector2.Zero;
|
|
|
|
|
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Up))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
camMove.Y -= 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Down))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
camMove.Y += 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Left))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
camMove.X -= 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Right))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
camMove.X += 10f * (float)gameTime.ElapsedGameTime.TotalSeconds;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.PageUp))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
Camera.Zoom += 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Camera.Zoom / 20f;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.PageDown))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
Camera.Zoom -= 5f * (float)gameTime.ElapsedGameTime.TotalSeconds * Camera.Zoom / 20f;
|
|
|
|
|
}
|
|
|
|
|
if (camMove != Vector2.Zero)
|
|
|
|
|
{
|
|
|
|
|
Camera.MoveCamera(camMove);
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
PlayerIndex i;
|
|
|
|
|
if (input.IsNewKeyPress(Keys.Home, PlayerIndex.One, out i))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
Camera.ResetCamera();
|
|
|
|
|
}
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
private void HandleUserAgent(InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
Vector2 force = _agentForce * new Vector2(input.CurrentGamePadStates[0].ThumbSticks.Right.X,
|
|
|
|
|
-input.CurrentGamePadStates[0].ThumbSticks.Right.Y);
|
|
|
|
|
float torque = _agentTorque * (input.CurrentGamePadStates[0].Triggers.Right - input.CurrentGamePadStates[0].Triggers.Left);
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
|
_userAgent.ApplyForce(force);
|
|
|
|
|
_userAgent.ApplyTorque(torque);
|
|
|
|
|
|
|
|
|
|
float forceAmount = _agentForce * 0.6f;
|
|
|
|
|
|
|
|
|
|
force = Vector2.Zero;
|
|
|
|
|
torque = 0;
|
|
|
|
|
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.A))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
force += new Vector2(-forceAmount, 0);
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.S))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
force += new Vector2(0, forceAmount);
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.D))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
force += new Vector2(forceAmount, 0);
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.W))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
force += new Vector2(0, -forceAmount);
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.Q))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
torque -= _agentTorque;
|
|
|
|
|
}
|
2012-04-18 04:34:46 +00:00
|
|
|
|
if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.E))
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
torque += _agentTorque;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_userAgent.ApplyForce(force);
|
|
|
|
|
_userAgent.ApplyTorque(torque);
|
2012-03-21 02:44:43 +00:00
|
|
|
|
|
2012-03-19 23:57:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnableOrDisableFlag(DebugViewFlags flag)
|
|
|
|
|
{
|
|
|
|
|
if ((DebugView.Flags & flag) == flag)
|
|
|
|
|
{
|
|
|
|
|
DebugView.RemoveFlags(flag);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DebugView.AppendFlags(flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(GameTime gameTime)
|
|
|
|
|
{
|
|
|
|
|
Matrix projection = Camera.SimProjection;
|
|
|
|
|
Matrix view = Camera.SimView;
|
|
|
|
|
|
|
|
|
|
if (!Axios.Settings.ScreenSaver)
|
|
|
|
|
DebugView.RenderDebugData(ref projection, ref view);
|
|
|
|
|
base.Draw(gameTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|