Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using FarseerPhysics;
- using FarseerPhysics.Common;
- using FarseerPhysics.Common.Decomposition;
- using FarseerPhysics.Common.PolygonManipulation;
- using FarseerPhysics.DebugViews;
- using FarseerPhysics.Factories;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using FarseerPhysics.Dynamics;
- namespace FarseerSimple
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- DebugViewXNA DebugView;
- SpriteFont font;
- public static bool IsCollision;
- public SceneItem Item1;
- public SceneItem Item2;
- public World World;
- public Game1() {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = 1280;
- graphics.PreferredBackBufferHeight = 720;
- Content.RootDirectory = "Content";
- }
- /// <summary>
- /// Allows the game to perform any initialization it needs to before starting to run.
- /// This is where it can query for any required services and load any non-graphic
- /// related content. Calling base.Initialize will enumerate through any components
- /// and initialize them as well.
- /// </summary>
- protected override void Initialize() {
- // TODO: Add your initialization logic here
- base.Initialize();
- }
- /// <summary>
- /// LoadContent will be called once per game and is the place to load
- /// all of your content.
- /// </summary>
- protected override void LoadContent() {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- IsCollision = false;
- World = new World(Vector2.Zero);
- font = Content.Load<SpriteFont>(@"font");
- // TODO: use this.Content to load your game content here
- Item1 = new SceneItem();
- Item1.Position = new Vector2(200,200);
- Item1.Texture = Content.Load<Texture2D>(@"object");
- Item1.CreateFarseerObject(World);
- Item2 = new SceneItem();
- Item2.Position = new Vector2(600, 600);
- Item2.Texture = Content.Load<Texture2D>(@"object2");
- Item2.CreateFarseerObject(World);
- if (DebugView == null) {
- DebugView = new DebugViewXNA(World);
- DebugView.AppendFlags(DebugViewFlags.Shape);
- DebugView.AppendFlags(DebugViewFlags.PolygonPoints);
- DebugView.AppendFlags(DebugViewFlags.PerformanceGraph);
- DebugView.AppendFlags(DebugViewFlags.AABB);
- DebugView.DefaultShapeColor = Color.White;
- DebugView.SleepingShapeColor = Color.LightGray;
- DebugView.LoadContent(graphics.GraphicsDevice, Content, font);
- }
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// all content.
- /// </summary>
- protected override void UnloadContent() {
- // TODO: Unload any non ContentManager content here
- }
- /// <summary>
- /// Allows the game to run logic such as updating the world,
- /// checking for collisions, gathering input, and playing audio.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Update(GameTime gameTime) {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- KeyboardState keyState = Keyboard.GetState();
- if (keyState.IsKeyDown(Keys.Escape))
- this.Exit();
- if (keyState.IsKeyDown(Keys.A))
- Item2.PositionX -= 5;
- if (keyState.IsKeyDown(Keys.D))
- Item2.PositionX += 5;
- if (keyState.IsKeyDown(Keys.S))
- Item2.PositionY += 5;
- if (keyState.IsKeyDown(Keys.W))
- Item2.PositionY -= 5;
- Item1.FarseerBody.Position = Item1.Position;
- Item2.FarseerBody.Position = Item2.Position;
- IsCollision = false;
- World.Step(1.0f / 30.0f);
- base.Update(gameTime);
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime) {
- if (IsCollision)
- GraphicsDevice.Clear(Color.Red);
- else
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- // TODO: Add your drawing code here
- spriteBatch.Draw(Item1.Texture, Item1.Position, Color.White);
- spriteBatch.Draw(Item2.Texture, Item2.Position, Color.White);
- spriteBatch.End();
- //Matrix projection = Camera.SimProjection;
- //Matrix view = Camera.SimView;
- Rectangle boundingRect = new Rectangle(0, 0, 1280, 720);
- Matrix projection = Matrix.CreateOrthographicOffCenter(boundingRect.Left, boundingRect.Right,
- boundingRect.Bottom, boundingRect.Top, 0, 1);
- DebugView.RenderDebugData(ref projection);
- base.Draw(gameTime);
- }
- public static bool Body_OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact) {
- IsCollision = true;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement