diff --git a/.vs/Subsurface_Solution/v14/.suo b/.vs/Subsurface_Solution/v14/.suo index 25aff9562..da95ab367 100644 Binary files a/.vs/Subsurface_Solution/v14/.suo and b/.vs/Subsurface_Solution/v14/.suo differ diff --git a/Subsurface/Barotrauma.csproj b/Subsurface/Barotrauma.csproj index d268371f8..1eed57d21 100644 --- a/Subsurface/Barotrauma.csproj +++ b/Subsurface/Barotrauma.csproj @@ -63,6 +63,7 @@ + @@ -101,6 +102,7 @@ + diff --git a/Subsurface/Source/Characters/AI/AIController.cs b/Subsurface/Source/Characters/AI/AIController.cs index 77fff925b..5ecd56a8b 100644 --- a/Subsurface/Source/Characters/AI/AIController.cs +++ b/Subsurface/Source/Characters/AI/AIController.cs @@ -10,7 +10,7 @@ namespace Barotrauma public enum AiState { None, Attack, GoTo, Escape } public enum SteeringState { Wander, Seek, Escape } - public Character Character; + public readonly Character Character; protected AiState state; @@ -45,10 +45,7 @@ namespace Barotrauma steeringManager = new SteeringManager(this); } - public virtual void DebugDraw(SpriteBatch spriteBatch) - { - - } + public virtual void DebugDraw(SpriteBatch spriteBatch) { } public virtual void OnAttacked(IDamageable attacker, float amount) { } diff --git a/Subsurface/Source/Characters/AI/Objectives/AIObjective.cs b/Subsurface/Source/Characters/AI/Objectives/AIObjective.cs index f073fb06a..e00457bb8 100644 --- a/Subsurface/Source/Characters/AI/Objectives/AIObjective.cs +++ b/Subsurface/Source/Characters/AI/Objectives/AIObjective.cs @@ -11,7 +11,7 @@ namespace Barotrauma public virtual bool IsCompleted() { - return true; + return false; } public AIObjective() diff --git a/Subsurface/Source/Characters/AI/PathSteeringManager.cs b/Subsurface/Source/Characters/AI/PathSteeringManager.cs new file mode 100644 index 000000000..333fcbc81 --- /dev/null +++ b/Subsurface/Source/Characters/AI/PathSteeringManager.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using FarseerPhysics; + +namespace Barotrauma +{ + class PathSteeringManager : SteeringManager + { + private PathFinder pathFinder; + private SteeringPath currentPath; + + private Vector2 currentTarget; + + private float findPathTimer; + + public PathSteeringManager(ISteerable host) + : base(host) + {} + + public override void Update(float speed = 1) + { + base.Update(speed); + + findPathTimer -= 1.0f / 60.0f; + } + + + protected override Vector2 DoSteeringSeek(Vector2 target, float speed = 1) + { + //find a new path if one hasn't been found yet or the target is different from the current target + if (currentPath == null || Vector2.DistanceSquared(target, currentTarget)>10.0f) + { + if (findPathTimer > 0.0f) return Vector2.Zero; + + currentTarget = target; + currentPath = pathFinder.FindPath(ConvertUnits.ToDisplayUnits(host.SimPosition), target); + + findPathTimer = 1.0f; + + return DiffToCurrentNode(); + } + + Vector2 diff = DiffToCurrentNode(); + + return (diff == Vector2.Zero) ? Vector2.Zero : Vector2.Normalize(diff)*speed; + } + + private Vector2 DiffToCurrentNode() + { + if (currentPath == null) return Vector2.Zero; + + currentPath.CheckProgress(host.SimPosition, 0.1f); + + if (currentPath.CurrentNode == null) return Vector2.Zero; + + return currentPath.CurrentNode.SimPosition - host.SimPosition; + } + } +} diff --git a/Subsurface/Source/Characters/AI/SteeringManager.cs b/Subsurface/Source/Characters/AI/SteeringManager.cs index 9352195fa..30a447b00 100644 --- a/Subsurface/Source/Characters/AI/SteeringManager.cs +++ b/Subsurface/Source/Characters/AI/SteeringManager.cs @@ -6,12 +6,12 @@ namespace Barotrauma { class SteeringManager { - private const float CircleDistance = 2.5f; - private const float CircleRadius = 0.3f; + protected const float CircleDistance = 2.5f; + protected const float CircleRadius = 0.3f; - private const float RayCastInterval = 0.5f; + protected const float RayCastInterval = 0.5f; - private ISteerable host; + protected ISteerable host; private Vector2 steering; @@ -50,7 +50,7 @@ namespace Barotrauma steering += DoSteeringAvoid(deltaTime, speed); } - public void Update(float speed = 1.0f) + public virtual void Update(float speed = 1.0f) { float steeringSpeed = steering.Length(); if (steeringSpeed>speed) @@ -61,7 +61,7 @@ namespace Barotrauma host.Steering = steering; } - private Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f) + protected virtual Vector2 DoSteeringSeek(Vector2 target, float speed = 1.0f) { Vector2 targetVel = target - host.SimPosition; @@ -81,7 +81,7 @@ namespace Barotrauma return newSteering; } - private Vector2 DoSteeringWander(float speed = 1.0f) + protected virtual Vector2 DoSteeringWander(float speed = 1.0f) { Vector2 circleCenter = (host.Velocity == Vector2.Zero) ? new Vector2(speed, 0.0f) : host.Velocity; circleCenter = Vector2.Normalize(circleCenter) * CircleDistance; @@ -105,7 +105,7 @@ namespace Barotrauma return newSteering; } - private Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f) + protected virtual Vector2 DoSteeringAvoid(float deltaTime, float speed = 1.0f) { if (steering == Vector2.Zero || host.Steering == Vector2.Zero) return Vector2.Zero; diff --git a/Subsurface/Source/Characters/AI/SteeringPath.cs b/Subsurface/Source/Characters/AI/SteeringPath.cs index bfe4b0b35..e52f748a7 100644 --- a/Subsurface/Source/Characters/AI/SteeringPath.cs +++ b/Subsurface/Source/Characters/AI/SteeringPath.cs @@ -48,10 +48,10 @@ namespace Barotrauma currentIndex++; } - public WayPoint CheckProgress(Vector2 pos, float minSimDistance = 0.1f) + public WayPoint CheckProgress(Vector2 simPosition, float minSimDistance = 0.1f) { if (nodes.Count == 0 || currentIndex>nodes.Count-1) return null; - if (Vector2.Distance(pos, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++; + if (Vector2.Distance(simPosition, nodes[currentIndex].SimPosition) < minSimDistance) currentIndex++; return CurrentNode; } diff --git a/Subsurface/Source/Map/Levels/Level.cs b/Subsurface/Source/Map/Levels/Level.cs index 95f3aa857..98455b9cc 100644 --- a/Subsurface/Source/Map/Levels/Level.cs +++ b/Subsurface/Source/Map/Levels/Level.cs @@ -21,7 +21,7 @@ namespace Barotrauma static Level loaded; - private static Texture2D shaftTexture; + private LevelRenderer renderer; //how close the sub has to be to start/endposition to exit const float ExitDistance = 6000.0f; @@ -40,13 +40,10 @@ namespace Barotrauma //List bodies; private List cells; - private static BasicEffect basicEffect; - private VertexPositionTexture[] vertices; //private VertexBuffer vertexBuffer; - private Vector2 startPosition; - private Vector2 endPosition; + private Vector2 startPosition, endPosition; private Rectangle borders; @@ -94,6 +91,11 @@ namespace Barotrauma get { return positionsOfInterest; } } + public WrappingWall[,] WrappingWalls + { + get { return wrappingWalls; } + } + public string Seed { get { return seed; } @@ -107,18 +109,6 @@ namespace Barotrauma public Level(string seed, float difficulty, int width, int height, int siteInterval) { - if (shaftTexture == null) shaftTexture = TextureLoader.FromFile("Content/Map/shaft.png"); - - if (basicEffect==null) - { - - basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice); - basicEffect.VertexColorEnabled = false; - - basicEffect.TextureEnabled = true; - basicEffect.Texture = TextureLoader.FromFile("Content/Map/iceSurface.png"); - } - this.seed = seed; this.siteInterval = siteInterval; @@ -150,13 +140,12 @@ namespace Barotrauma Stopwatch sw = new Stopwatch(); sw.Start(); - if (loaded != null) - { - loaded.Unload(); - } - + if (loaded != null) loaded.Unload(); + loaded = this; + renderer = new LevelRenderer(this); + Voronoi voronoi = new Voronoi(1.0); List sites = new List(); @@ -391,7 +380,10 @@ namespace Barotrauma endPosition = temp; } + Debug.WriteLine("**********************************************************************************"); Debug.WriteLine("Generated a map with " + sites.Count + " sites in " + sw.ElapsedMilliseconds + " ms"); + Debug.WriteLine("Seed: "+seed); + Debug.WriteLine("**********************************************************************************"); } private List GeneratePath(List points, List cells, Microsoft.Xna.Framework.Rectangle limits, float minWidth, float wanderAmount = 0.3f, bool mirror=false, bool placeWaypoints=false) @@ -821,6 +813,15 @@ namespace Barotrauma } } + public void Draw(SpriteBatch spriteBatch) + { + renderer.Draw(spriteBatch); + } + + public void Render(GraphicsDevice graphicsDevice, Camera cam) + { + renderer.Render(graphicsDevice, cam, vertices); + } public void DebugCheckPos() @@ -841,26 +842,6 @@ namespace Barotrauma System.Diagnostics.Debug.WriteLine("pos: " + Position); } - public void Draw(SpriteBatch spriteBatch) - { - Vector2 pos = endPosition; - pos.Y = -pos.Y - Position.Y; - - if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y-512) return; - - pos.X = GameMain.GameScreen.Cam.WorldView.X-512.0f; - //pos.X += Position.X % 512; - - int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width/512.0f + 2.0f)*512.0f); - - spriteBatch.Draw(shaftTexture, - new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) + Position.X % 512) , (int)pos.Y, width, 512), - new Rectangle(0, 0, width, 256), - Color.White, 0.0f, - Vector2.Zero, - SpriteEffects.None, 0.0f); - } - public List GetCells(Vector2 pos, int searchDepth = 2) { int gridPosX = (int)Math.Floor(pos.X / GridCellWidth); @@ -907,7 +888,6 @@ namespace Barotrauma public List GetCellEdges(Vector2 refPos, int searchDepth = 2, bool onlySolid = true) { - int gridPosX = (int)Math.Floor(refPos.X / GridCellWidth); int gridPosY = (int)Math.Floor(refPos.Y / GridCellWidth); @@ -928,6 +908,7 @@ namespace Barotrauma for (int i = 0; i < cell.edges.Count; i++) { if (onlySolid && !cell.edges[i].isSolid) continue; + Vector2 start = cell.edges[i].point1 + Position; start.Y = -start.Y; @@ -949,13 +930,14 @@ namespace Barotrauma foreach (VoronoiCell cell in wrappingWalls[side, n].Cells) { + Vector2 offset = wrappingWalls[side, n].Offset + Position; for (int i = 0; i < cell.edges.Count; i++) { if (onlySolid && !cell.edges[i].isSolid) continue; - Vector2 start = cell.edges[i].point1 + Position; + Vector2 start = cell.edges[i].point1 + offset; start.Y = -start.Y; - Vector2 end = cell.edges[i].point2 + Position; + Vector2 end = cell.edges[i].point2 + offset; end.Y = -end.Y; edges.Add(new Vector2[] { start, end }); @@ -967,51 +949,9 @@ namespace Barotrauma return edges; } - public void Render(GraphicsDevice graphicsDevice, Camera cam) - { - if (vertices == null) return; - if (vertices.Length <= 0) return; - - basicEffect.World = Matrix.CreateTranslation(new Vector3(Position, 0.0f)) * cam.ShaderTransform - * Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f; - - basicEffect.CurrentTechnique.Passes[0].Apply(); - - graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; - - graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, (int)Math.Floor(vertices.Length / 3.0f)); - - - for (int side = 0; side < 2; side++) - { - for (int i = 0; i < 2; i++) - { - basicEffect.World = Matrix.CreateTranslation(new Vector3(Position + 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, - wrappingWalls[side, i].Vertices, 0, (int)Math.Floor(wrappingWalls[side, i].Vertices.Length / 3.0f)); - - } - } - } - private void Unload() { - //position = Vector2.Zero; - - //foreach (VoronoiCell cell in cells) - //{ - // //foreach (Body b in cell.bodies) - // //{ - // Game1.world.RemoveBody(cell.body); - // //} - //} - - - //bodies = null; + renderer = null; vertices = null; diff --git a/Subsurface/Source/Map/Levels/LevelRenderer.cs b/Subsurface/Source/Map/Levels/LevelRenderer.cs new file mode 100644 index 000000000..c51393cae --- /dev/null +++ b/Subsurface/Source/Map/Levels/LevelRenderer.cs @@ -0,0 +1,93 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Barotrauma +{ + class LevelRenderer + { + + private static BasicEffect basicEffect; + + private static Texture2D shaftTexture; + + private Level level; + + public LevelRenderer(Level level) + { + if (shaftTexture == null) shaftTexture = TextureLoader.FromFile("Content/Map/shaft.png"); + + if (basicEffect == null) + { + + basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice); + basicEffect.VertexColorEnabled = false; + + basicEffect.TextureEnabled = true; + basicEffect.Texture = TextureLoader.FromFile("Content/Map/iceSurface.png"); + } + + this.level = level; + } + + public void Draw(SpriteBatch spriteBatch) + { + Vector2 pos = level.EndPosition; + pos.Y = -pos.Y - level.Position.Y; + + if (GameMain.GameScreen.Cam.WorldView.Y < -pos.Y - 512) return; + + pos.X = GameMain.GameScreen.Cam.WorldView.X - 512.0f; + //pos.X += Position.X % 512; + + int width = (int)(Math.Ceiling(GameMain.GameScreen.Cam.WorldView.Width / 512.0f + 2.0f) * 512.0f); + + spriteBatch.Draw(shaftTexture, + new Rectangle((int)(MathUtils.Round(pos.X, 512.0f) + level.Position.X % 512), (int)pos.Y, width, 512), + new Rectangle(0, 0, width, 256), + Color.White, 0.0f, + Vector2.Zero, + SpriteEffects.None, 0.0f); + } + + + public void Render(GraphicsDevice graphicsDevice, Camera cam, VertexPositionTexture[] vertices) + { + if (vertices == null) return; + if (vertices.Length <= 0) return; + + basicEffect.World = Matrix.CreateTranslation(new Vector3(level.Position, 0.0f)) * cam.ShaderTransform + * Matrix.CreateOrthographic(GameMain.GraphicsWidth, GameMain.GraphicsHeight, -1, 1) * 0.5f; + + basicEffect.CurrentTechnique.Passes[0].Apply(); + + graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; + + graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, (int)Math.Floor(vertices.Length / 3.0f)); + + + for (int side = 0; side < 2; side++) + { + for (int i = 0; i < 2; i++) + { + basicEffect.World = Matrix.CreateTranslation( + new Vector3(level.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)); + + } + } + } + + } +} diff --git a/Subsurface/Source/Map/Levels/WrappingWall.cs b/Subsurface/Source/Map/Levels/WrappingWall.cs index 80f0fef83..65314d417 100644 --- a/Subsurface/Source/Map/Levels/WrappingWall.cs +++ b/Subsurface/Source/Map/Levels/WrappingWall.cs @@ -149,8 +149,8 @@ namespace Barotrauma slot += amount; Vector2 moveAmount = Vector2.UnitX * WallWidth * amount; - Vector2 simMoveAmount = ConvertUnits.ToSimUnits(moveAmount); + foreach (VoronoiCell cell in cells) { cell.body.SetTransform(cell.body.Position + simMoveAmount, 0.0f); @@ -158,7 +158,6 @@ namespace Barotrauma midPos += moveAmount; offset += moveAmount; - } } } diff --git a/Subsurface/Source/Map/Map.cs b/Subsurface/Source/Map/Map.cs index 1871beac9..2ec229a53 100644 --- a/Subsurface/Source/Map/Map.cs +++ b/Subsurface/Source/Map/Map.cs @@ -3,7 +3,6 @@ using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using Voronoi2; namespace Barotrauma