Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- 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;
- namespace Games_Game_1
- {
- /// <summary>
- /// This is the main type for your game
- /// </summary>
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Skybox sky;
- PlayerIndex player;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- 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>
- ///
- Model myModel;
- float aspectRatio;
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- myModel = Content.Load<Model>("Model\\BlenderDone");
- aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
- Model skybox = Content.Load<Model>(@"skybox");
- sky = new Skybox(graphics.GraphicsDevice, skybox);
- player.Draw();
- // TODO: use this.Content to load your game content here
- }
- /// <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>
- ///
- Vector3 modelVelocity = Vector3.Zero;
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- UpdateInput();
- modelPosition += modelVelocity;
- modelVelocity *= 0.95f;
- // TODO: Add your update logic here
- base.Update(gameTime);
- }
- protected void UpdateInput()
- {
- GamePadState currentState = GamePad.GetState(PlayerIndex.One);
- KeyboardState currentKeyState = Keyboard.GetState();
- if (currentKeyState.IsKeyDown(Keys.Left))
- modelRotation += 0.10f;
- else if (currentKeyState.IsKeyDown(Keys.Right))
- modelRotation -= 0.10f;
- else
- modelRotation -= currentState.ThumbSticks.Left.X * 0.10f;
- Vector3 modelVelocityAdd = Vector3.Zero;
- modelVelocityAdd.X = -(float)Math.Sin(modelRotation);
- modelVelocityAdd.Z = -(float)Math.Cos(modelRotation);
- if (currentKeyState.IsKeyDown(Keys.W))
- modelVelocityAdd *= 1;
- else if (currentKeyState.IsKeyDown(Keys.S))
- modelVelocityAdd *= -1;
- else if (currentKeyState.IsKeyDown(Keys.D))
- modelVelocityAdd.X = 1;
- else if (currentKeyState.IsKeyDown(Keys.A))
- modelVelocityAdd.X = -1;
- else
- modelVelocityAdd *= currentState.Triggers.Right;
- modelVelocity += modelVelocityAdd;
- GamePad.SetVibration(PlayerIndex.One,
- currentState.Triggers.Right,
- currentState.Triggers.Right);
- // In case you get lost, press A to warp back to the center.
- if (currentState.Buttons.A == ButtonState.Pressed || currentKeyState.IsKeyDown(Keys.Enter))
- {
- modelPosition = Vector3.Zero;
- modelVelocity = Vector3.Zero;
- modelRotation = 0.0f;
- }
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- ///
- Vector3 modelPosition = Vector3.Zero;
- float modelRotation = 0.0f;
- Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- // TODO: Add your drawing code here
- Matrix[] transforms = new Matrix[myModel.Bones.Count];
- myModel.CopyAbsoluteBoneTransformsTo(transforms);
- foreach (ModelMesh mesh in myModel.Meshes)
- {
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.EnableDefaultLighting();
- effect.World = transforms[mesh.ParentBone.Index] *
- Matrix.CreateRotationY(modelRotation)
- * Matrix.CreateTranslation(modelPosition);
- effect.View = Matrix.CreateLookAt(cameraPosition,
- Vector3.Zero, Vector3.Up);
- effect.Projection = Matrix.CreatePerspectiveFieldOfView(
- MathHelper.ToRadians(45.0f), aspectRatio,
- 1.0f, 10000.0f);
- }
- mesh.Draw();
- }
- base.Draw(gameTime);
- }
- public static void DrawModel(Model model, Matrix modelTransform,
- Matrix[] absoluteBoneTransforms)
- {
- //Draw the model, a model can have multiple meshes, so loop
- foreach (ModelMesh mesh in model.Meshes)
- {
- //This is where the mesh orientation is set
- foreach (BasicEffect effect in mesh.Effects)
- {
- effect.World =
- absoluteBoneTransforms[mesh.ParentBone.Index] *
- modelTransform;
- }
- //Draw the mesh, will use the effects set above.
- mesh.Draw();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement