Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace Games_Game_1
  13. {
  14.     /// <summary>
  15.     /// This is the main type for your game
  16.     /// </summary>
  17.     public class Game1 : Microsoft.Xna.Framework.Game
  18.     {
  19.         GraphicsDeviceManager graphics;
  20.         SpriteBatch spriteBatch;
  21.         Skybox sky;
  22.         PlayerIndex player;
  23.        
  24.    
  25.  
  26.         public Game1()
  27.         {
  28.             graphics = new GraphicsDeviceManager(this);
  29.             Content.RootDirectory = "Content";
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Allows the game to perform any initialization it needs to before starting to run.
  34.         /// This is where it can query for any required services and load any non-graphic
  35.         /// related content.  Calling base.Initialize will enumerate through any components
  36.         /// and initialize them as well.
  37.         /// </summary>
  38.         protected override void Initialize()
  39.         {
  40.             // TODO: Add your initialization logic here
  41.  
  42.        
  43.  
  44.             base.Initialize();
  45.         }
  46.  
  47.         /// <summary>
  48.         /// LoadContent will be called once per game and is the place to load
  49.         /// all of your content.
  50.         /// </summary>
  51.         ///
  52.  
  53.             Model myModel;
  54.  
  55.             float aspectRatio;
  56.  
  57.  
  58.         protected override void LoadContent()
  59.         {
  60.  
  61.             // Create a new SpriteBatch, which can be used to draw textures.
  62.            
  63.             spriteBatch = new SpriteBatch(GraphicsDevice);
  64.            
  65.             myModel = Content.Load<Model>("Model\\BlenderDone");
  66.             aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
  67.             Model skybox = Content.Load<Model>(@"skybox");
  68.             sky = new Skybox(graphics.GraphicsDevice, skybox);
  69.             player.Draw();
  70.  
  71.            
  72.            
  73.  
  74.            
  75.            
  76.             // TODO: use this.Content to load your game content here
  77.         }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.         /// <summary>
  84.         /// UnloadContent will be called once per game and is the place to unload
  85.         /// all content.
  86.         /// </summary>
  87.         protected override void UnloadContent()
  88.         {
  89.             // TODO: Unload any non ContentManager content here
  90.         }
  91.  
  92.         /// <summary>
  93.         /// Allows the game to run logic such as updating the world,
  94.         /// checking for collisions, gathering input, and playing audio.
  95.         /// </summary>
  96.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  97.         ///
  98.  
  99.         Vector3 modelVelocity = Vector3.Zero;
  100.  
  101.         protected override void Update(GameTime gameTime)
  102.         {
  103.             // Allows the game to exit
  104.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  105.                 this.Exit();
  106.  
  107.             UpdateInput();
  108.  
  109.             modelPosition += modelVelocity;
  110.  
  111.             modelVelocity *= 0.95f;
  112.             // TODO: Add your update logic here
  113.  
  114.            
  115.      
  116.  
  117.             base.Update(gameTime);
  118.         }
  119.  
  120.         protected void UpdateInput()
  121.         {
  122.            
  123.             GamePadState currentState = GamePad.GetState(PlayerIndex.One);
  124.             KeyboardState currentKeyState = Keyboard.GetState();
  125.  
  126.  
  127.             if (currentKeyState.IsKeyDown(Keys.Left))
  128.                 modelRotation += 0.10f;
  129.             else if (currentKeyState.IsKeyDown(Keys.Right))
  130.                 modelRotation -= 0.10f;
  131.          
  132.             else
  133.  
  134.            
  135.  
  136.                 modelRotation -= currentState.ThumbSticks.Left.X * 0.10f;
  137.  
  138.            
  139.                 Vector3 modelVelocityAdd = Vector3.Zero;
  140.  
  141.              
  142.                 modelVelocityAdd.X = -(float)Math.Sin(modelRotation);
  143.                 modelVelocityAdd.Z = -(float)Math.Cos(modelRotation);
  144.  
  145.                 if (currentKeyState.IsKeyDown(Keys.W))
  146.                     modelVelocityAdd *= 1;
  147.                 else if (currentKeyState.IsKeyDown(Keys.S))
  148.                     modelVelocityAdd *= -1;
  149.                 else if (currentKeyState.IsKeyDown(Keys.D))
  150.                     modelVelocityAdd.X = 1;
  151.                 else if (currentKeyState.IsKeyDown(Keys.A))
  152.                     modelVelocityAdd.X = -1;
  153.                 else
  154.                 modelVelocityAdd *= currentState.Triggers.Right;
  155.  
  156.              
  157.                 modelVelocity += modelVelocityAdd;
  158.  
  159.                 GamePad.SetVibration(PlayerIndex.One,
  160.                     currentState.Triggers.Right,
  161.                     currentState.Triggers.Right);
  162.  
  163.  
  164.                 // In case you get lost, press A to warp back to the center.
  165.                 if (currentState.Buttons.A == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.Enter))
  166.                 {
  167.                     modelPosition = Vector3.Zero;
  168.                     modelVelocity = Vector3.Zero;
  169.                     modelRotation = 0.0f;
  170.                
  171.             }
  172.         }
  173.  
  174.         /// <summary>
  175.         /// This is called when the game should draw itself.
  176.         /// </summary>
  177.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  178.         ///
  179.  
  180.         Vector3 modelPosition = Vector3.Zero;
  181.         float modelRotation = 0.0f;
  182.  
  183.         Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);
  184.        
  185.  
  186.         protected override void Draw(GameTime gameTime)
  187.         {
  188.             GraphicsDevice.Clear(Color.CornflowerBlue);
  189.  
  190.             // TODO: Add your drawing code here
  191.  
  192.  
  193.  
  194.  
  195.             Matrix[] transforms = new Matrix[myModel.Bones.Count];
  196.             myModel.CopyAbsoluteBoneTransformsTo(transforms);
  197.          
  198.  
  199.             foreach (ModelMesh mesh in myModel.Meshes)
  200.             {
  201.                 foreach (BasicEffect effect in mesh.Effects)
  202.                 {
  203.                     effect.EnableDefaultLighting();
  204.                     effect.World = transforms[mesh.ParentBone.Index] *
  205.                         Matrix.CreateRotationY(modelRotation)
  206.                         * Matrix.CreateTranslation(modelPosition);
  207.                     effect.View = Matrix.CreateLookAt(cameraPosition,
  208.                         Vector3.Zero, Vector3.Up);
  209.                     effect.Projection = Matrix.CreatePerspectiveFieldOfView(
  210.                         MathHelper.ToRadians(45.0f), aspectRatio,
  211.                         1.0f, 10000.0f);
  212.                 }
  213.                 mesh.Draw();
  214.             }
  215.  
  216.    
  217.            
  218.  
  219.             base.Draw(gameTime);
  220.         }
  221.  
  222.         public static void DrawModel(Model model, Matrix modelTransform,
  223.          Matrix[] absoluteBoneTransforms)
  224.         {
  225.             //Draw the model, a model can have multiple meshes, so loop
  226.             foreach (ModelMesh mesh in model.Meshes)
  227.             {
  228.                 //This is where the mesh orientation is set
  229.                 foreach (BasicEffect effect in mesh.Effects)
  230.                 {
  231.                     effect.World =
  232.                         absoluteBoneTransforms[mesh.ParentBone.Index] *
  233.                         modelTransform;
  234.                 }
  235.                 //Draw the mesh, will use the effects set above.
  236.                 mesh.Draw();
  237.             }
  238.         }
  239.  
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement