From e8baeaf22f226b6c9bc947d714eb7a62962305ab Mon Sep 17 00:00:00 2001 From: Daniel Brunner <0xFEEDC0DE64@gmail.com> Date: Fri, 4 Nov 2016 20:23:37 +0100 Subject: [PATCH] Terrain now only consists of blocks --- .../Entities/PlayerEntity.cs | 17 ++++ .../Entities/TerrainEntity.cs | 95 +++++++++++++++---- .../FluckyGame.Client/Game1.cs | 9 +- 3 files changed, 99 insertions(+), 22 deletions(-) diff --git a/src/FluckyGame.Client/FluckyGame.Client/Entities/PlayerEntity.cs b/src/FluckyGame.Client/FluckyGame.Client/Entities/PlayerEntity.cs index 49cdabb..137ce09 100644 --- a/src/FluckyGame.Client/FluckyGame.Client/Entities/PlayerEntity.cs +++ b/src/FluckyGame.Client/FluckyGame.Client/Entities/PlayerEntity.cs @@ -28,6 +28,9 @@ namespace FluckyGame.Client.Entities AnimationLooping = true; cameraDistance = 200; + + UpdateY(); + UpdateWorld(); } public override void Update(GameTime gameTime, KeyboardState keyboardState, MouseState mouseState) @@ -98,6 +101,7 @@ namespace FluckyGame.Client.Entities AnimationPlaying = false; } + UpdateY(); UpdateWorld(); var test = new Vector3(0, 50, 0); @@ -113,5 +117,18 @@ namespace FluckyGame.Client.Entities } Game1.currentInstance.terrain.View = view; } + + public void UpdateY() + { + var x = (int)Math.Round(Position.X / 10); + var z = (int)Math.Round(Position.Z / 10); + + if (x < 0 || z < 0 || x >= Game1.currentInstance.world.GetLength(0) || z >= Game1.currentInstance.world.GetLength(1)) + return; + + var y = Game1.currentInstance.world[x, z]; + + Position.Y = y - 25; + } } } diff --git a/src/FluckyGame.Client/FluckyGame.Client/Entities/TerrainEntity.cs b/src/FluckyGame.Client/FluckyGame.Client/Entities/TerrainEntity.cs index eb1114f..fca0e73 100644 --- a/src/FluckyGame.Client/FluckyGame.Client/Entities/TerrainEntity.cs +++ b/src/FluckyGame.Client/FluckyGame.Client/Entities/TerrainEntity.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -13,7 +14,7 @@ namespace FluckyGame.Client.Entities public Matrix View { set { effect.View = value; } } - public TerrainEntity(int width, int height, GraphicsDevice graphicsDevice, Matrix projection) : + public TerrainEntity(int[,] world, GraphicsDevice graphicsDevice, Matrix projection) : base() { this.graphicsDevice = graphicsDevice; @@ -21,30 +22,82 @@ namespace FluckyGame.Client.Entities effect.Projection = projection; effect.VertexColorEnabled = true; - vertices = new VertexPositionColor[width * height]; - indices = new short[(width - 1) * (height - 1) * 6]; - int currentIndex = 0; - for (int x = 0; x < width; x++) - for(int z = 0; z < height; z++) + const int size = 10; + + var verticesList = new List(); + var indicesList = new List(); + + for (int z = 0; z < 100; z++) + for (int x = 0; x < 100; x++) { - vertices[x + z * width].Position = new Vector3(-8000 + (16000 / width * x), (float)Game1.random.NextDouble() * 50 - 50, -8000 + (16000 / height * z)); - vertices[x + z * width].Color = new Color(Game1.random.Next(0, 255), Game1.random.Next(0, 255), Game1.random.Next(0, 255)); + float y = world[x, z] - 25; - if(x < width - 1 && z < height - 1) - { - short downLeft = (short)(x + z * width); - short downRight = (short)((x + 1) + z * width); - short upLeft = (short)(x + (z + 1) * width); - short upRight = (short)((x + 1) + (z + 1) * width); + short index00 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3(x * size, y, z * size), Color.Red)); + short index10 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3((x + 1) * size, y, z * size), Color.Green)); + short index01 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3(x * size, y, (z + 1) * size), Color.Blue)); + short index11 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3((x + 1) * size, y, (z + 1) * size), Color.Red)); - indices[currentIndex++] = downRight; - indices[currentIndex++] = upLeft; - indices[currentIndex++] = downLeft; - indices[currentIndex++] = upRight; - indices[currentIndex++] = upLeft; - indices[currentIndex++] = downRight; - } + short indexBottom00 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3(x * size, -100, z * size), Color.Red)); + short indexBottom10 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3((x + 1) * size, -100, z * size), Color.Green)); + short indexBottom01 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3(x * size, -100, (z + 1) * size), Color.Blue)); + short indexBottom11 = (short)verticesList.Count; + verticesList.Add(new VertexPositionColor(new Vector3((x + 1) * size, -100, (z + 1) * size), Color.Red)); + + //Top + indicesList.Add(index11); + indicesList.Add(index01); + indicesList.Add(index00); + + indicesList.Add(index10); + indicesList.Add(index11); + indicesList.Add(index00); + + //North + indicesList.Add(index00); + indicesList.Add(index01); + indicesList.Add(indexBottom01); + + indicesList.Add(index00); + indicesList.Add(indexBottom01); + indicesList.Add(indexBottom00); + + //West + indicesList.Add(index10); + indicesList.Add(index00); + indicesList.Add(indexBottom00); + + indicesList.Add(index10); + indicesList.Add(indexBottom00); + indicesList.Add(indexBottom10); + + //South + indicesList.Add(index11); + indicesList.Add(index10); + indicesList.Add(indexBottom10); + + indicesList.Add(index11); + indicesList.Add(indexBottom10); + indicesList.Add(indexBottom11); + + //East + indicesList.Add(index01); + indicesList.Add(index11); + indicesList.Add(indexBottom11); + + indicesList.Add(index01); + indicesList.Add(indexBottom11); + indicesList.Add(indexBottom01); } + + vertices = verticesList.ToArray(); + indices = indicesList.ToArray(); } public override void Draw(GameTime gameTime) diff --git a/src/FluckyGame.Client/FluckyGame.Client/Game1.cs b/src/FluckyGame.Client/FluckyGame.Client/Game1.cs index c0a05e7..075e19c 100644 --- a/src/FluckyGame.Client/FluckyGame.Client/Game1.cs +++ b/src/FluckyGame.Client/FluckyGame.Client/Game1.cs @@ -42,6 +42,8 @@ namespace FluckyGame.Client private Queue queue; private bool exiting; + public int[,] world; + static Game1() { random = new Random(); @@ -120,10 +122,15 @@ namespace FluckyGame.Client effect.TextureEnabled = true; } + world = new int[200, 200]; + for (int y = 0; y < world.GetLength(1); y++) + for (int x = 0; x < world.GetLength(0); x++) + world[x, y] = 25 + (int)((float)Math.Sin((double)x / 10) * (float)Math.Sin((double)y / 10) * 25f); + entities = new List() { (player = new PlayerEntity()), - (terrain = new TerrainEntity(200, 200, GraphicsDevice, projection)) + (terrain = new TerrainEntity(world, GraphicsDevice, projection)) }; entitiesById = new Dictionary();