Terrain now only consists of blocks

This commit is contained in:
Daniel Brunner
2016-11-04 20:23:37 +01:00
parent c541b0ee03
commit e8baeaf22f
3 changed files with 99 additions and 22 deletions

View File

@ -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;
}
}
}

View File

@ -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<VertexPositionColor>();
var indicesList = new List<short>();
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)

View File

@ -42,6 +42,8 @@ namespace FluckyGame.Client
private Queue<Packet> 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<Entity>()
{
(player = new PlayerEntity()),
(terrain = new TerrainEntity(200, 200, GraphicsDevice, projection))
(terrain = new TerrainEntity(world, GraphicsDevice, projection))
};
entitiesById = new Dictionary<string, ModelEntity>();