Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using FaceGame.Utility;
- namespace FaceGame {
- public class Game {
- int playerCount;
- // Tile dimensions & Map
- float TileWidth, TileHeight; // individual tile width/height
- Effect MapEffect;
- List<Entity> Map;
- Texture2D MapTexture;
- VertexBuffer MapVertexBuffer;
- IndexBuffer MapIndexBuffer;
- // Camera
- Camera camera;
- Pair<int, int> mousePosition;
- Vector3 cameraRotation;
- public Game(int playerCount) {
- this.playerCount = playerCount;
- // Init Camera
- camera = new Camera(Global.Graphics.Viewport.AspectRatio, MathHelper.ToRadians(45.0f), 1.0f, 1000.0f);
- camera.setPosition(0.0f, 10.0f, 0.0f);
- camera.setLookAt(0.0f, 0.0f, 1.0f);
- mousePosition = new Pair<int, int>(0, 0);
- cameraRotation = Vector3.Zero;
- CreateMap(150, 150);
- }
- private void CreateMap(ushort WidthInTiles, ushort HeightInTiles) {
- // Init the map
- Map = new List<Entity>();
- // set the mapWidth/height here
- ushort MapWidth = (ushort)(WidthInTiles + 1);
- ushort MapHeight = (ushort)(HeightInTiles + 1);
- TileWidth = 10.0f;
- TileHeight = 10.0f;
- Vector2 texCoord = Vector2.Zero;
- // create each vertex
- VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[MapHeight * MapWidth];
- for (ushort i = 0; i < MapHeight; i++) {
- for (ushort j = 0; j < MapWidth; j++) {
- texCoord.X = (float)j * 1.0f;
- texCoord.Y = (float)i * 1.0f;
- vertices[j + (i * MapHeight)] = new VertexPositionNormalTexture(
- new Vector3(j * TileWidth, (float)Math.Sin(i) * 5.0f, i * TileHeight),
- Vector3.Up,
- texCoord);
- }
- }
- List<ushort> indexList = new List<ushort>();
- for (ushort y = 0; y < MapHeight - 1; y++) {
- for (ushort x = 0; x < MapWidth - 1; x++) {
- ushort topleft = (ushort)(x + y * MapHeight);
- ushort topright = (ushort)( (x + 1) + y * MapHeight);
- ushort botleft = (ushort)(x + (y + 1) * MapHeight);
- ushort botright = (ushort)( (x + 1) + (y + 1) * MapHeight);
- indexList.Add(topleft);
- indexList.Add(topright);
- indexList.Add(botleft);
- indexList.Add(topright);
- indexList.Add(botright);
- indexList.Add(botleft);
- }
- }
- ushort[] indexes = indexList.ToArray();
- MapVertexBuffer = new VertexBuffer(Global.Graphics, VertexPositionNormalTexture.VertexDeclaration, vertices.Length, BufferUsage.None);
- MapVertexBuffer.SetData<VertexPositionNormalTexture>(vertices);
- MapIndexBuffer = new IndexBuffer(Global.Graphics, IndexElementSize.SixteenBits, indexes.Length, BufferUsage.None);
- MapIndexBuffer.SetData<ushort>(indexes);
- MapEffect = Global.Content.Load<Effect>("Effects/TileEffect");
- MapTexture = Global.Content.Load<Texture2D>("Textures/Grass");
- }
- public void Update(GameTime gameTime) {
- //update stuff
- if (Global.KeyState.IsKeyDown(Keys.Escape)) Global.main.Exit();
- UpdateCamera();
- }
- private void UpdateCamera() {
- // finish moving the camera
- if (Global.MouseState.RightButton == ButtonState.Released &&
- Global.PrevMouseState.RightButton == ButtonState.Pressed) {
- // right just released
- Global.main.IsMouseVisible = true;
- Mouse.SetPosition(mousePosition.First, mousePosition.Second);
- }
- // start to move the camera
- if (Global.MouseState.RightButton == ButtonState.Pressed &&
- Global.PrevMouseState.RightButton == ButtonState.Released) {
- // right just pressed
- Global.main.IsMouseVisible = false;
- mousePosition.First = Global.MouseState.X;
- mousePosition.Second = Global.MouseState.Y;
- }
- // rotate the camera
- if (Global.MouseState.RightButton == ButtonState.Pressed) {
- Vector3 cameraPosition = camera.position;
- float mouseDiffX = (Global.MouseState.X - Global.PrevMouseState.X);
- float mouseDiffY = (Global.MouseState.Y - Global.PrevMouseState.Y);
- cameraRotation.X -= mouseDiffX * 0.01f;
- cameraRotation.Y += mouseDiffY * 0.01f;
- if (cameraRotation.Y > 1.5f) cameraRotation.Y = 1.5f;
- if (cameraRotation.Y < -1.5f) cameraRotation.Y = -1.5f;
- // rotX = up/down
- // rotY = left/right
- Matrix cameraMatrix = Matrix.CreateRotationX(cameraRotation.Y) * Matrix.CreateRotationY(cameraRotation.X);
- if (Global.KeyState.IsKeyDown(Keys.W)) {
- // move forward
- cameraPosition.X += (float)Math.Sin(cameraRotation.X);
- cameraPosition.Y -= (float)Math.Sin(cameraRotation.Y);
- cameraPosition.Z += (float)Math.Cos(cameraRotation.X);
- } else if (Global.KeyState.IsKeyDown(Keys.S)) {
- // move backwards
- cameraPosition.X -= (float)Math.Sin(cameraRotation.X);
- cameraPosition.Y += (float)Math.Sin(cameraRotation.Y);
- cameraPosition.Z -= (float)Math.Cos(cameraRotation.X);
- }
- if (Global.KeyState.IsKeyDown(Keys.A)) {
- // move left
- cameraPosition.X += (float)Math.Sin(cameraRotation.X + MathHelper.PiOver2);
- cameraPosition.Z += (float)Math.Cos(cameraRotation.X + MathHelper.PiOver2);
- } else if (Global.KeyState.IsKeyDown(Keys.D)) {
- // move right
- cameraPosition.X += (float)Math.Sin(cameraRotation.X - MathHelper.PiOver2);
- cameraPosition.Z += (float)Math.Cos(cameraRotation.X - MathHelper.PiOver2);
- }
- camera.setPosition(cameraPosition);
- camera.setLookAt(camera.position + Vector3.Transform(new Vector3(0.0f, 0.0f, 1.0f), cameraMatrix));
- }
- }
- public void Draw(GameTime gameTime) {
- RasterizerState r = new RasterizerState();
- r.CullMode = CullMode.CullCounterClockwiseFace;
- //r.FillMode = FillMode.WireFrame;
- Global.Graphics.DepthStencilState = DepthStencilState.Default;
- Global.Graphics.BlendState = BlendState.Opaque;
- Global.Graphics.RasterizerState = r;
- Global.Graphics.VertexSamplerStates[0] = SamplerState.AnisotropicClamp;
- DrawMap();
- }
- private void DrawMap() {
- MapEffect.CurrentTechnique = MapEffect.Techniques["GeneralTechnique"];
- MapEffect.Parameters["View"].SetValue(camera.view);
- MapEffect.Parameters["Projection"].SetValue(camera.projection);
- MapEffect.Parameters["TileTexture"].SetValue(MapTexture); // the tile texture can be set individually
- Global.Graphics.SetVertexBuffer(MapVertexBuffer);
- Global.Graphics.Indices = MapIndexBuffer;
- MapEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(0.0f, 0.0f, 0.0f));
- MapEffect.CurrentTechnique.Passes[0].Apply();
- Global.Graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList,
- 0, 0, MapVertexBuffer.VertexCount, 0, MapIndexBuffer.IndexCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement