Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Audio;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Media;
  9. using FaceGame.Utility;
  10.  
  11. namespace FaceGame {
  12.     public class Game {
  13.  
  14.         int playerCount;
  15.  
  16.         // Tile dimensions & Map
  17.         float TileWidth, TileHeight; // individual tile width/height
  18.         Effect MapEffect;
  19.         List<Entity> Map;
  20.         Texture2D MapTexture;
  21.         VertexBuffer MapVertexBuffer;
  22.         IndexBuffer MapIndexBuffer;
  23.  
  24.         // Camera
  25.         Camera camera;
  26.         Pair<int, int> mousePosition;
  27.         Vector3 cameraRotation;
  28.  
  29.         public Game(int playerCount) {
  30.  
  31.             this.playerCount = playerCount;
  32.  
  33.             // Init Camera
  34.             camera = new Camera(Global.Graphics.Viewport.AspectRatio, MathHelper.ToRadians(45.0f), 1.0f, 1000.0f);
  35.             camera.setPosition(0.0f, 10.0f, 0.0f);
  36.             camera.setLookAt(0.0f, 0.0f, 1.0f);
  37.             mousePosition = new Pair<int, int>(0, 0);
  38.             cameraRotation = Vector3.Zero;
  39.  
  40.             CreateMap(150, 150);
  41.  
  42.         }
  43.  
  44.         private void CreateMap(ushort WidthInTiles, ushort HeightInTiles) {
  45.  
  46.             // Init the map
  47.             Map = new List<Entity>();
  48.  
  49.             // set the mapWidth/height here
  50.             ushort MapWidth = (ushort)(WidthInTiles + 1);
  51.             ushort MapHeight = (ushort)(HeightInTiles + 1);
  52.  
  53.             TileWidth = 10.0f;
  54.             TileHeight = 10.0f;
  55.  
  56.             Vector2 texCoord = Vector2.Zero;
  57.  
  58.             // create each vertex
  59.             VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[MapHeight * MapWidth];
  60.             for (ushort i = 0; i < MapHeight; i++) {
  61.                 for (ushort j = 0; j < MapWidth; j++) {
  62.  
  63.                     texCoord.X = (float)j * 1.0f;
  64.                     texCoord.Y = (float)i * 1.0f;
  65.  
  66.                     vertices[j + (i * MapHeight)] = new VertexPositionNormalTexture(
  67.                         new Vector3(j * TileWidth, (float)Math.Sin(i) * 5.0f, i * TileHeight),
  68.                         Vector3.Up,
  69.                         texCoord);
  70.  
  71.                 }
  72.             }
  73.  
  74.  
  75.             List<ushort> indexList = new List<ushort>();
  76.  
  77.             for (ushort y = 0; y < MapHeight - 1; y++) {
  78.                 for (ushort x = 0; x < MapWidth - 1; x++) {
  79.  
  80.                     ushort topleft = (ushort)(x + y * MapHeight);
  81.                     ushort topright = (ushort)( (x + 1) + y * MapHeight);
  82.                     ushort botleft = (ushort)(x + (y + 1) * MapHeight);
  83.                     ushort botright = (ushort)( (x + 1) + (y + 1) * MapHeight);
  84.                     indexList.Add(topleft);
  85.                     indexList.Add(topright);
  86.                     indexList.Add(botleft);
  87.  
  88.                     indexList.Add(topright);
  89.                     indexList.Add(botright);
  90.                     indexList.Add(botleft);
  91.  
  92.                    
  93.  
  94.                 }
  95.  
  96.             }
  97.  
  98.             ushort[] indexes = indexList.ToArray();
  99.  
  100.             MapVertexBuffer = new VertexBuffer(Global.Graphics, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.None);
  101.             MapVertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
  102.  
  103.             MapIndexBuffer = new IndexBuffer(Global.Graphics, IndexElementSize.SixteenBits, indexes.Length, BufferUsage.None);
  104.             MapIndexBuffer.SetData<ushort>(indexes);
  105.  
  106.             MapEffect = Global.Content.Load<Effect>("Effects/TileEffect");
  107.             MapTexture = Global.Content.Load<Texture2D>("Textures/Grass");
  108.  
  109.         }
  110.  
  111.         public void Update(GameTime gameTime) {
  112.             //update stuff
  113.             if (Global.KeyState.IsKeyDown(Keys.Escape)) Global.main.Exit();
  114.  
  115.             UpdateCamera();
  116.  
  117.         }
  118.  
  119.         private void UpdateCamera() {
  120.  
  121.             // finish moving the camera
  122.             if (Global.MouseState.RightButton == ButtonState.Released &&
  123.                 Global.PrevMouseState.RightButton == ButtonState.Pressed) {
  124.  
  125.                 // right just released
  126.                 Global.main.IsMouseVisible = true;
  127.                 Mouse.SetPosition(mousePosition.First, mousePosition.Second);
  128.  
  129.             }
  130.  
  131.             // start to move the camera
  132.             if (Global.MouseState.RightButton == ButtonState.Pressed &&
  133.                 Global.PrevMouseState.RightButton == ButtonState.Released) {
  134.  
  135.                 // right just pressed
  136.                 Global.main.IsMouseVisible = false;
  137.                 mousePosition.First = Global.MouseState.X;
  138.                 mousePosition.Second = Global.MouseState.Y;
  139.  
  140.             }
  141.  
  142.             // rotate the camera
  143.             if (Global.MouseState.RightButton == ButtonState.Pressed) {
  144.  
  145.                 Vector3 cameraPosition = camera.position;
  146.  
  147.                 float mouseDiffX = (Global.MouseState.X - Global.PrevMouseState.X);
  148.                 float mouseDiffY = (Global.MouseState.Y - Global.PrevMouseState.Y);
  149.  
  150.                 cameraRotation.X -= mouseDiffX * 0.01f;
  151.                 cameraRotation.Y += mouseDiffY * 0.01f;
  152.  
  153.                 if (cameraRotation.Y > 1.5f) cameraRotation.Y = 1.5f;
  154.                 if (cameraRotation.Y < -1.5f) cameraRotation.Y = -1.5f;
  155.  
  156.                 // rotX = up/down
  157.                 // rotY = left/right
  158.                 Matrix cameraMatrix = Matrix.CreateRotationX(cameraRotation.Y) * Matrix.CreateRotationY(cameraRotation.X);
  159.  
  160.                 if (Global.KeyState.IsKeyDown(Keys.W)) {
  161.  
  162.                     // move forward
  163.                     cameraPosition.X += (float)Math.Sin(cameraRotation.X);
  164.                     cameraPosition.Y -= (float)Math.Sin(cameraRotation.Y);
  165.                     cameraPosition.Z += (float)Math.Cos(cameraRotation.X);
  166.  
  167.                 } else if (Global.KeyState.IsKeyDown(Keys.S)) {
  168.  
  169.                     // move backwards
  170.                     cameraPosition.X -= (float)Math.Sin(cameraRotation.X);
  171.                     cameraPosition.Y += (float)Math.Sin(cameraRotation.Y);
  172.                     cameraPosition.Z -= (float)Math.Cos(cameraRotation.X);
  173.  
  174.                 }
  175.  
  176.                 if (Global.KeyState.IsKeyDown(Keys.A)) {
  177.  
  178.                     // move left
  179.                     cameraPosition.X += (float)Math.Sin(cameraRotation.X + MathHelper.PiOver2);
  180.                     cameraPosition.Z += (float)Math.Cos(cameraRotation.X + MathHelper.PiOver2);
  181.  
  182.                 } else if (Global.KeyState.IsKeyDown(Keys.D)) {
  183.  
  184.                     // move right
  185.                     cameraPosition.X += (float)Math.Sin(cameraRotation.X - MathHelper.PiOver2);
  186.                     cameraPosition.Z += (float)Math.Cos(cameraRotation.X - MathHelper.PiOver2);
  187.  
  188.                 }
  189.  
  190.                 camera.setPosition(cameraPosition);
  191.                 camera.setLookAt(camera.position + Vector3.Transform(new Vector3(0.0f, 0.0f, 1.0f), cameraMatrix));
  192.  
  193.             }
  194.  
  195.         }
  196.  
  197.         public void Draw(GameTime gameTime) {
  198.  
  199.             RasterizerState r = new RasterizerState();
  200.             r.CullMode = CullMode.CullCounterClockwiseFace;
  201.             //r.FillMode = FillMode.WireFrame;
  202.  
  203.             Global.Graphics.DepthStencilState = DepthStencilState.Default;
  204.             Global.Graphics.BlendState = BlendState.Opaque;
  205.             Global.Graphics.RasterizerState = r;
  206.             Global.Graphics.VertexSamplerStates[0] = SamplerState.AnisotropicClamp;
  207.  
  208.             DrawMap();
  209.  
  210.         }
  211.  
  212.         private void DrawMap() {
  213.  
  214.             MapEffect.CurrentTechnique = MapEffect.Techniques["GeneralTechnique"];
  215.             MapEffect.Parameters["View"].SetValue(camera.view);
  216.             MapEffect.Parameters["Projection"].SetValue(camera.projection);
  217.             MapEffect.Parameters["TileTexture"].SetValue(MapTexture); // the tile texture can be set individually
  218.  
  219.             Global.Graphics.SetVertexBuffer(MapVertexBuffer);
  220.             Global.Graphics.Indices = MapIndexBuffer;
  221.  
  222.             MapEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(0.0f, 0.0f, 0.0f));
  223.             MapEffect.CurrentTechnique.Passes[0].Apply();
  224.  
  225.             Global.Graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList,
  226.                 0, 0, MapVertexBuffer.VertexCount, 0, MapIndexBuffer.IndexCount);
  227.  
  228.         }
  229.     }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement