Level wall generation bugfixes, moved level background drawing to LevelRenderer, more coordinate system bugfixes, better looking progress bars,

This commit is contained in:
Regalis
2015-12-14 18:59:59 +02:00
parent c74caadc42
commit 859be53d28
28 changed files with 318 additions and 232 deletions

View File

@@ -41,7 +41,6 @@ namespace Barotrauma
//List<Body> bodies;
private List<VoronoiCell> cells;
private VertexPositionTexture[] vertices;
//private VertexBuffer vertexBuffer;
private Vector2 startPosition, endPosition;
@@ -314,7 +313,9 @@ namespace Barotrauma
startPosition.Y = borders.Height;
endPosition.Y = borders.Height;
vertices = GeneratePolygons(cells, pathCells);
renderer.BodyVertices = GeneratePolygons(cells, pathCells);
renderer.WallVertices = GenerateWallShapes(cells);
wrappingWalls = new WrappingWall[2, 2];
@@ -325,7 +326,8 @@ namespace Barotrauma
wrappingWalls[side, i] = new WrappingWall(pathCells, cells, borders.Height * 0.5f,
(side == 0 ? -1 : 1) * (i == 0 ? 1 : 2));
wrappingWalls[side, i].Vertices = GeneratePolygons(wrappingWalls[side, i].Cells, new List<VoronoiCell>(), false);
wrappingWalls[side, i].BodyVertices = GeneratePolygons(wrappingWalls[side, i].Cells, new List<VoronoiCell>(), false);
wrappingWalls[side, i].WallVertices = GenerateWallShapes(wrappingWalls[side, i].Cells);
//wrappingWalls[side, i].Cells[0].edges[1].isSolid = false;
//wrappingWalls[side, i].Cells[0].edges[3].isSolid = false;
@@ -622,9 +624,9 @@ namespace Barotrauma
}
private VertexPositionTexture[] GeneratePolygons(List<VoronoiCell> cells, List<VoronoiCell> emptyCells, bool setSolid=true)
private VertexPositionColor[] GeneratePolygons(List<VoronoiCell> cells, List<VoronoiCell> emptyCells, bool setSolid=true)
{
List<VertexPositionTexture> verticeList = new List<VertexPositionTexture>();
List<VertexPositionColor> verticeList = new List<VertexPositionColor>();
//bodies = new List<Body>();
List<Vector2> tempVertices = new List<Vector2>();
@@ -657,14 +659,14 @@ namespace Barotrauma
continue;
}
//var triangles = MathUtils.TriangulateConvexHull(tempVertices, cell.Center);
//for (int i = 0; i < triangles.Count; i++)
//{
// foreach (Vector2 vertex in triangles[i])
// {
// verticeList.Add(new VertexPositionTexture(new Vector3(vertex, 0.0f), vertex/1000.0f));
// }
//}
var triangles = MathUtils.TriangulateConvexHull(tempVertices, cell.Center);
for (int i = 0; i < triangles.Count; i++)
{
foreach (Vector2 vertex in triangles[i])
{
verticeList.Add(new VertexPositionColor(new Vector3(vertex, 0.0f), Color.Black));
}
}
if (bodyPoints.Count < 2) continue;
@@ -684,7 +686,7 @@ namespace Barotrauma
bodyPoints[i] = ConvertUnits.ToSimUnits(bodyPoints[i]);
}
var triangles = MathUtils.TriangulateConvexHull(bodyPoints, cell.Center);
triangles = MathUtils.TriangulateConvexHull(bodyPoints, cell.Center);
Body edgeBody = new Body(GameMain.World);
@@ -706,15 +708,12 @@ namespace Barotrauma
bodies.Add(edgeBody);
}
verticeList = GenerateWallShapes(cells);
return verticeList.ToArray();
}
private List<VertexPositionTexture> GenerateWallShapes(List<VoronoiCell> cells)
private VertexPositionTexture[] GenerateWallShapes(List<VoronoiCell> cells)
{
float wallThickness = 500.0f;
float inwardThickness = 500.0f, outWardThickness = 30.0f;
List<VertexPositionTexture> verticeList = new List<VertexPositionTexture>();
@@ -726,13 +725,13 @@ namespace Barotrauma
if (edge.cell1 != null && edge.cell1.body == null) edge.cell1 = null;
if (edge.cell2 != null && edge.cell2.body == null) edge.cell2 = null;
//CompareCCW compare = new CompareCCW(cell.Center);
//if (compare.Compare(edge.point1, edge.point2) == -1)
//{
// var temp = edge.point1;
// edge.point1 = edge.point2;
// edge.point2 = temp;
//}
CompareCCW compare = new CompareCCW(cell.Center);
if (compare.Compare(edge.point1, edge.point2) == -1)
{
var temp = edge.point1;
edge.point1 = edge.point2;
edge.point2 = temp;
}
}
}
@@ -758,7 +757,7 @@ namespace Barotrauma
rightEdge = edge2;
}
}
Vector2 leftNormal = Vector2.Zero, rightNormal = Vector2.Zero;
if (leftEdge == null)
@@ -783,6 +782,10 @@ namespace Barotrauma
Vector2.Normalize(GetEdgeNormal(rightEdge) + GetEdgeNormal(edge, cell)) :
Vector2.Normalize(rightEdge.Center - edge.point2);
}
for (int i = 0; i < 2; i++)
{
Vector2[] verts = new Vector2[3];
@@ -791,9 +794,9 @@ namespace Barotrauma
if (i==0)
{
verts[0] = edge.point1;
verts[1] = edge.point2;
verts[2] = edge.point1 + leftNormal * wallThickness;
verts[0] = edge.point1 - leftNormal * outWardThickness;
verts[1] = edge.point2 - rightNormal * outWardThickness;
verts[2] = edge.point1 + leftNormal * inwardThickness;
vertPos[0] = new VertexPositionTexture(new Vector3(verts[0], 0.0f), Vector2.Zero);
vertPos[1] = new VertexPositionTexture(new Vector3(verts[1], 0.0f), Vector2.UnitX);
@@ -801,9 +804,9 @@ namespace Barotrauma
}
else
{
verts[0] = edge.point1 + leftNormal * wallThickness;
verts[1] = edge.point2;
verts[2] = edge.point2 + rightNormal * wallThickness;
verts[0] = edge.point1 + leftNormal * inwardThickness;
verts[1] = edge.point2 - rightNormal * outWardThickness;
verts[2] = edge.point2 + rightNormal * inwardThickness;
vertPos[0] = new VertexPositionTexture(new Vector3(verts[0], 0.0f), new Vector2(0.0f, 0.5f));
vertPos[1] = new VertexPositionTexture(new Vector3(verts[1], 0.0f), Vector2.UnitX);
@@ -821,7 +824,7 @@ namespace Barotrauma
}
}
return verticeList;
return verticeList.ToArray();
}
private Vector2 GetEdgeNormal(GraphEdge edge, VoronoiCell cell = null)
@@ -953,16 +956,21 @@ namespace Barotrauma
// }
//}
public void Draw(SpriteBatch spriteBatch)
public void Update (float deltaTime)
{
renderer.Update(deltaTime);
}
public void DrawFront(SpriteBatch spriteBatch)
{
if (renderer == null) return;
renderer.Draw(spriteBatch);
}
public void Render(GraphicsDevice graphicsDevice, Camera cam)
public void DrawBack(SpriteBatch spriteBatch, Camera cam, BackgroundSpriteManager backgroundSpriteManager = null)
{
if (renderer == null) return;
renderer.Render(graphicsDevice, cam, vertices);
renderer.DrawBackground(spriteBatch, cam, backgroundSpriteManager);
}
@@ -1094,9 +1102,7 @@ namespace Barotrauma
private void Unload()
{
renderer = null;
vertices = null;
cells = null;
bodies.Clear();

View File

@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
@@ -9,20 +10,32 @@ namespace Barotrauma
{
class LevelRenderer
{
private static BasicEffect basicEffect;
private static Sprite background, backgroundTop;
private static Texture2D dustParticles;
private static Texture2D shaftTexture;
private Level level;
Vector2 dustOffset;
private Level level;
public VertexPositionTexture[] WallVertices;
public VertexPositionColor[] BodyVertices;
public LevelRenderer(Level level)
{
if (shaftTexture == null) shaftTexture = TextureLoader.FromFile("Content/Map/shaft.png");
if (background==null)
{
background = new Sprite("Content/Map/background.png", Vector2.Zero);
backgroundTop = new Sprite("Content/Map/background2.png", Vector2.Zero);
dustParticles = Sprite.LoadTexture("Content/Map/dustparticles.png");
}
if (basicEffect == null)
{
basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice);
basicEffect.VertexColorEnabled = false;
@@ -33,6 +46,75 @@ namespace Barotrauma
this.level = level;
}
public void Update(float deltaTime)
{
dustOffset -= Vector2.UnitY * 10.0f * (float)deltaTime;
}
public void DrawBackground(SpriteBatch spriteBatch, Camera cam, BackgroundSpriteManager backgroundSpriteManager = null)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap);
Vector2 backgroundPos = cam.Position;
//if (Level.Loaded != null) backgroundPos -= Level.Loaded.Position;
backgroundPos.Y = -backgroundPos.Y;
backgroundPos /= 20.0f;
if (backgroundPos.Y < 1024)
{
if (backgroundPos.Y > -1024)
{
background.SourceRect = new Rectangle((int)backgroundPos.X, (int)Math.Max(backgroundPos.Y, 0), 1024, 1024);
background.DrawTiled(spriteBatch,
(backgroundPos.Y < 0) ? new Vector2(0.0f, -backgroundPos.Y) : Vector2.Zero,
new Vector2(GameMain.GraphicsWidth, 1024 - backgroundPos.Y),
Vector2.Zero, Color.White);
}
if (backgroundPos.Y < 0)
{
backgroundTop.SourceRect = new Rectangle((int)backgroundPos.X, (int)backgroundPos.Y, 1024, (int)Math.Min(-backgroundPos.Y, 1024));
backgroundTop.DrawTiled(spriteBatch, Vector2.Zero, new Vector2(GameMain.GraphicsWidth, Math.Min(-backgroundPos.Y, GameMain.GraphicsHeight)),
Vector2.Zero, Color.White);
}
}
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
SamplerState.LinearWrap, DepthStencilState.Default, null, null,
cam.Transform);
if (backgroundSpriteManager!=null) backgroundSpriteManager.Draw(spriteBatch);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend,
SamplerState.LinearWrap);
backgroundPos = new Vector2(cam.WorldView.X, cam.WorldView.Y) + dustOffset;
//if (Level.Loaded != null) backgroundPos -= Level.Loaded.Position;
Rectangle viewRect = cam.WorldView;
viewRect.Y = -viewRect.Y;
float multiplier = 0.8f;
for (int i = 1; i < 5; i++)
{
spriteBatch.Draw(dustParticles, new Rectangle(0,0,GameMain.GraphicsWidth,GameMain.GraphicsHeight),
new Rectangle((int)((backgroundPos.X * multiplier)), (int)((-backgroundPos.Y * multiplier)), cam.WorldView.Width*2, cam.WorldView.Height*2),
Color.White * multiplier, 0.0f, Vector2.Zero, SpriteEffects.None, 1.0f - multiplier);
multiplier -= 0.1f;
}
spriteBatch.End();
RenderWalls(GameMain.CurrGraphicsDevice, cam);
}
public void Draw(SpriteBatch spriteBatch)
{
Vector2 pos = new Vector2(0.0f, -level.StartPosition.Y);// level.EndPosition;
@@ -54,36 +136,50 @@ namespace Barotrauma
}
public void Render(GraphicsDevice graphicsDevice, Camera cam, VertexPositionTexture[] vertices)
public void RenderWalls(GraphicsDevice graphicsDevice, Camera cam)
{
if (vertices == null) return;
if (vertices.Length <= 0) return;
if (WallVertices == null || WallVertices.Length <= 0) return;
basicEffect.World = cam.ShaderTransform
* Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, vertices, 0, (int)Math.Floor(vertices.Length / 3.0f));
basicEffect.VertexColorEnabled = true;
basicEffect.TextureEnabled = false;
basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_VertexColor"];
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, BodyVertices, 0, (int)Math.Floor(BodyVertices.Length / 3.0f));
for (int side = 0; side < 2; side++)
{
for (int i = 0; i < 2; i++)
{
// basicEffect.World = Matrix.CreateTranslation(
// new Vector3(-Submarine.Loaded.Position + level.WrappingWalls[side, i].Offset, 0.0f)) *
// cam.ShaderTransform *
// Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f;
// basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList,
level.WrappingWalls[side, i].Vertices, 0,
(int)Math.Floor(level.WrappingWalls[side, i].Vertices.Length / 3.0f));
PrimitiveType.TriangleList, level.WrappingWalls[side, i].BodyVertices, 0,
(int)Math.Floor(level.WrappingWalls[side, i].BodyVertices.Length / 3.0f));
}
}
basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;
basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_Texture"];
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, WallVertices, 0, (int)Math.Floor(WallVertices.Length / 3.0f));
for (int side = 0; side < 2; side++)
{
for (int i = 0; i < 2; i++)
{
basicEffect.VertexColorEnabled = false;
basicEffect.TextureEnabled = true;
basicEffect.CurrentTechnique = basicEffect.Techniques["BasicEffect_Texture"];
basicEffect.CurrentTechnique.Passes[0].Apply();
graphicsDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, level.WrappingWalls[side, i].WallVertices, 0,
(int)Math.Floor(level.WrappingWalls[side, i].WallVertices.Length / 3.0f));
}
}

View File

@@ -15,7 +15,9 @@ namespace Barotrauma
public const float WallWidth = 20000.0f;
public VertexPositionTexture[] Vertices;
public VertexPositionTexture[] WallVertices;
public VertexPositionColor[] BodyVertices;
private Vector2 midPos;
private int slot;