Advertisement
tylerf001

Game1.cs Farseer Test

Apr 9th, 2012
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FarseerPhysics;
  5. using FarseerPhysics.Common;
  6. using FarseerPhysics.Common.Decomposition;
  7. using FarseerPhysics.Common.PolygonManipulation;
  8. using FarseerPhysics.DebugViews;
  9. using FarseerPhysics.Factories;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Audio;
  12. using Microsoft.Xna.Framework.Content;
  13. using Microsoft.Xna.Framework.GamerServices;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. using Microsoft.Xna.Framework.Media;
  17. using FarseerPhysics.Dynamics;
  18.  
  19. namespace FarseerSimple
  20. {
  21.     /// <summary>
  22.     /// This is the main type for your game
  23.     /// </summary>
  24.     public class Game1 : Microsoft.Xna.Framework.Game
  25.     {
  26.         GraphicsDeviceManager graphics;
  27.         SpriteBatch spriteBatch;
  28.         DebugViewXNA DebugView;
  29.         SpriteFont font;
  30.         public static bool IsCollision;
  31.  
  32.         public SceneItem Item1;
  33.         public SceneItem Item2;
  34.  
  35.         public World World;
  36.  
  37.         public Game1() {
  38.             graphics = new GraphicsDeviceManager(this);
  39.             graphics.PreferredBackBufferWidth = 1280;
  40.             graphics.PreferredBackBufferHeight = 720;
  41.             Content.RootDirectory = "Content";
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Allows the game to perform any initialization it needs to before starting to run.
  46.         /// This is where it can query for any required services and load any non-graphic
  47.         /// related content.  Calling base.Initialize will enumerate through any components
  48.         /// and initialize them as well.
  49.         /// </summary>
  50.         protected override void Initialize() {
  51.             // TODO: Add your initialization logic here
  52.  
  53.             base.Initialize();
  54.         }
  55.  
  56.         /// <summary>
  57.         /// LoadContent will be called once per game and is the place to load
  58.         /// all of your content.
  59.         /// </summary>
  60.         protected override void LoadContent() {
  61.             // Create a new SpriteBatch, which can be used to draw textures.
  62.             spriteBatch = new SpriteBatch(GraphicsDevice);
  63.             IsCollision = false;
  64.  
  65.             World = new World(Vector2.Zero);
  66.  
  67.             font = Content.Load<SpriteFont>(@"font");
  68.  
  69.             // TODO: use this.Content to load your game content here
  70.             Item1 = new SceneItem();
  71.             Item1.Position = new Vector2(200,200);
  72.             Item1.Texture = Content.Load<Texture2D>(@"object");
  73.             Item1.CreateFarseerObject(World);
  74.  
  75.             Item2 = new SceneItem();
  76.             Item2.Position = new Vector2(600, 600);
  77.             Item2.Texture = Content.Load<Texture2D>(@"object2");
  78.             Item2.CreateFarseerObject(World);
  79.  
  80.             if (DebugView == null) {
  81.                 DebugView = new DebugViewXNA(World);
  82.                 DebugView.AppendFlags(DebugViewFlags.Shape);
  83.                 DebugView.AppendFlags(DebugViewFlags.PolygonPoints);
  84.                 DebugView.AppendFlags(DebugViewFlags.PerformanceGraph);
  85.                 DebugView.AppendFlags(DebugViewFlags.AABB);
  86.                 DebugView.DefaultShapeColor = Color.White;
  87.                 DebugView.SleepingShapeColor = Color.LightGray;
  88.                 DebugView.LoadContent(graphics.GraphicsDevice, Content, font);
  89.             }
  90.         }
  91.  
  92.         /// <summary>
  93.         /// UnloadContent will be called once per game and is the place to unload
  94.         /// all content.
  95.         /// </summary>
  96.         protected override void UnloadContent() {
  97.             // TODO: Unload any non ContentManager content here
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Allows the game to run logic such as updating the world,
  102.         /// checking for collisions, gathering input, and playing audio.
  103.         /// </summary>
  104.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  105.         protected override void Update(GameTime gameTime) {
  106.             // Allows the game to exit
  107.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  108.                 this.Exit();
  109.  
  110.             KeyboardState keyState = Keyboard.GetState();
  111.             if (keyState.IsKeyDown(Keys.Escape))
  112.                 this.Exit();
  113.  
  114.             if (keyState.IsKeyDown(Keys.A))
  115.                 Item2.PositionX -= 5;
  116.  
  117.             if (keyState.IsKeyDown(Keys.D))
  118.                 Item2.PositionX += 5;
  119.  
  120.             if (keyState.IsKeyDown(Keys.S))
  121.                 Item2.PositionY += 5;
  122.  
  123.             if (keyState.IsKeyDown(Keys.W))
  124.                 Item2.PositionY -= 5;
  125.  
  126.             Item1.FarseerBody.Position = Item1.Position;
  127.             Item2.FarseerBody.Position = Item2.Position;
  128.  
  129.             IsCollision = false;
  130.             World.Step(1.0f / 30.0f);
  131.  
  132.             base.Update(gameTime);
  133.         }
  134.  
  135.         /// <summary>
  136.         /// This is called when the game should draw itself.
  137.         /// </summary>
  138.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  139.         protected override void Draw(GameTime gameTime) {
  140.             if (IsCollision)
  141.                 GraphicsDevice.Clear(Color.Red);
  142.             else
  143.                 GraphicsDevice.Clear(Color.CornflowerBlue);
  144.  
  145.             spriteBatch.Begin();
  146.  
  147.             // TODO: Add your drawing code here
  148.             spriteBatch.Draw(Item1.Texture, Item1.Position, Color.White);
  149.  
  150.             spriteBatch.Draw(Item2.Texture, Item2.Position, Color.White);
  151.  
  152.             spriteBatch.End();
  153.  
  154.  
  155.             //Matrix projection = Camera.SimProjection;
  156.             //Matrix view = Camera.SimView;
  157.  
  158.             Rectangle boundingRect = new Rectangle(0, 0, 1280, 720);
  159.             Matrix projection = Matrix.CreateOrthographicOffCenter(boundingRect.Left, boundingRect.Right,
  160.                 boundingRect.Bottom, boundingRect.Top, 0, 1);
  161.  
  162.             DebugView.RenderDebugData(ref projection);
  163.  
  164.             base.Draw(gameTime);
  165.         }
  166.  
  167.  
  168.         public static bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact) {
  169.             IsCollision = true;
  170.             return true;
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement