From 3c1c7f6898ea4b634c451e6023e1e5f787c35017 Mon Sep 17 00:00:00 2001 From: riedel Date: Tue, 9 May 2023 14:54:36 +0200 Subject: [PATCH] b --- CityGame.sln | 6 ++ CityGame/CityGame.csproj | 19 ++++- CityGame/Classes/Entities/Car.cs | 52 ++++++++++-- CityGame/Classes/Entities/Helicopter.cs | 3 +- CityGame/Classes/Entities/PoliceCar.cs | 26 +++--- CityGame/Classes/Misc/Extensions.cs | 1 - .../Classes/Rendering/ColorConversionMaps.cs | 6 +- CityGame/Classes/Rendering/CustomShader.cs | 4 +- CityGame/Classes/Rendering/SourcedImage.cs | 8 +- CityGame/Licenses/ASTARLITE-LICENSE.txt | 21 +++++ CityGame/Licenses/LAME-LICENSE.txt | 20 +++++ CityGame/Licenses/NAUDIO-LICENSE.txt | 7 ++ CityGame/Licenses/RE-UI-LICENSE.txt | 21 +++++ CityGame/Licenses/SIMPLEXNOISE-LICENSE.htm | 80 +++++++++++++++++ CityGame/MainWindow.xaml.cs | 85 +++++++++++++++---- CityGame/Program.cs | 28 ++++++ 16 files changed, 328 insertions(+), 59 deletions(-) create mode 100644 CityGame/Licenses/ASTARLITE-LICENSE.txt create mode 100644 CityGame/Licenses/LAME-LICENSE.txt create mode 100644 CityGame/Licenses/NAUDIO-LICENSE.txt create mode 100644 CityGame/Licenses/RE-UI-LICENSE.txt create mode 100644 CityGame/Licenses/SIMPLEXNOISE-LICENSE.htm create mode 100644 CityGame/Program.cs diff --git a/CityGame.sln b/CityGame.sln index 5f86bc4..9168462 100644 --- a/CityGame.sln +++ b/CityGame.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CityGame", "CityGame\CityGa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WPFGame", "..\WPFGame\WPFGame\WPFGame.csproj", "{4F2DC7EE-24BA-4383-9AB6-EBA46EB191DF}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MGUI", "..\..\RE-UI-a-Monogame-GUI\MGUI\MGUI.csproj", "{BEEAE3E9-28E8-4052-90FC-1A84E40526F6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {4F2DC7EE-24BA-4383-9AB6-EBA46EB191DF}.Debug|Any CPU.Build.0 = Debug|Any CPU {4F2DC7EE-24BA-4383-9AB6-EBA46EB191DF}.Release|Any CPU.ActiveCfg = Release|Any CPU {4F2DC7EE-24BA-4383-9AB6-EBA46EB191DF}.Release|Any CPU.Build.0 = Release|Any CPU + {BEEAE3E9-28E8-4052-90FC-1A84E40526F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BEEAE3E9-28E8-4052-90FC-1A84E40526F6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BEEAE3E9-28E8-4052-90FC-1A84E40526F6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BEEAE3E9-28E8-4052-90FC-1A84E40526F6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CityGame/CityGame.csproj b/CityGame/CityGame.csproj index 333cf85..e26751f 100644 --- a/CityGame/CityGame.csproj +++ b/CityGame/CityGame.csproj @@ -9,15 +9,13 @@ - - - + @@ -37,6 +35,21 @@ + + Always + + + Always + + + Always + + + Always + + + Always + PreserveNewest diff --git a/CityGame/Classes/Entities/Car.cs b/CityGame/Classes/Entities/Car.cs index 7a783f0..2056284 100644 --- a/CityGame/Classes/Entities/Car.cs +++ b/CityGame/Classes/Entities/Car.cs @@ -3,17 +3,17 @@ using CityGame.Classes.World; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; -using System.Diagnostics.Eventing.Reader; +using System.Diagnostics; using System.Linq; -using System.Runtime.ConstrainedExecution; -using System.Windows; using WPFGame; namespace CityGame.Classes.Entities { public class Car : Entity { - public bool desperate = false; + public int grid = 1; + public long TimeUntilReroute = 5000; + protected long RerouteTimePassed = 0; public static List Cars = new List(); public delegate void CarEvent(Car car); public event CarEvent JourneyFinished; @@ -44,6 +44,7 @@ namespace CityGame.Classes.Entities protected ColoredRectangle debugRect; protected Tile lastTile; protected List lights = new List(); + protected LightSource PointLight; public override OCanvas Render() { OCanvas canvas = new OCanvas(); @@ -73,6 +74,12 @@ namespace CityGame.Classes.Entities lights.Add(blight); lights.Add(blight2); + PointLight = new LightSource { Type = LightSourceType.PointLight, Color = Color.White, Radius = 24, Angle = 24, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + Canvas.SetLeft(PointLight, 43); + Canvas.SetTop(PointLight, 32); + + canvas.Children.Add(PointLight); + return canvas; } public Car() @@ -80,7 +87,9 @@ namespace CityGame.Classes.Entities Cars.Add(this); JourneyFinished += c => { }; JourneyImpossible += c => { }; + Move += c => { }; } + public event CarEvent Move; public override void Tick(long deltaTime) { @@ -92,11 +101,12 @@ namespace CityGame.Classes.Entities new Tuple(TileType.Road, "1"), new Tuple(TileType.Garage, null) }; + if (this == MainWindow.Selected) Debug.WriteLine("Selected."); Tile myTile = MainWindow.Grid[Point.X, Point.Y]; bool fullBlock = fullBlockTiles.Any(x => (x.Item1 == myTile.Type || (x.Item1 == TileType.Road && (myTile.Type == TileType.Path || myTile.Type == TileType.Highway || myTile.Type == TileType.Bridge || myTile.Type == TileType.HighwayBridge))) && (x.Item2 == myTile.Pattern.PatternCode || x.Item2 is null)); if (myTile.Type == TileType.Garage) { - Rotation = ((Canvas)myTile.Element).Children[1].Rotation-90; + Rotation = ((Canvas)myTile.Element).Children[1].Rotation - 90; lights.ForEach(x => x.Visible = false); } else lights.ForEach(x => x.Visible = true); @@ -106,10 +116,11 @@ namespace CityGame.Classes.Entities if (Path is null) { var pf = MainWindow.pathfinder; - if (desperate) pf = MainWindow.pathfinderDesperate; + if (grid == 2) pf = MainWindow.pathfinderDesperate; Path = pf.FindPath(Point.Convert(), new Point((int)(Target.X() / MainWindow.TileSize), (int)(Target.Y() / MainWindow.TileSize)).Convert()).Select(x => x.Convert()).ToArray(); NextTarget = 0; } + if(new Point(Target.X() / 64, Target.Y() / 64) != Point) Move(this); if (Path.Length == 0) { JourneyImpossible(this); @@ -150,18 +161,40 @@ namespace CityGame.Classes.Entities float degrees = (float)(Math.Atan2(direction.Y, direction.X) * (180 / Math.PI)) + 90; Rotation = degrees; Tile targetTile = MainWindow.Grid[nextTarget.X, nextTarget.Y]; + Car blockingCar = null; if (Math.Round(Rotation) == 0 || Math.Round(Rotation) == 90) { if (!OccupiedTilesFill.ContainsKey(myTile)) OccupiedTilesFill.Add(myTile, this); if (!OccupiedTilesFill2.ContainsKey(myTile) && fullBlock) OccupiedTilesFill2.Add(myTile, this); - if (OccupiedTiles.ContainsKey(targetTile) && OccupiedTiles[targetTile] != this) SpeedMulti = 0; + if (OccupiedTiles.ContainsKey(targetTile) && OccupiedTiles[targetTile] != this) + { + SpeedMulti = 0; + blockingCar = OccupiedTiles[targetTile]; + } } if (Math.Round(Rotation) == 180 || Math.Round(Rotation) == 270) { if (!OccupiedTilesFill2.ContainsKey(myTile)) OccupiedTilesFill2.Add(myTile, this); - if (OccupiedTiles2.ContainsKey(targetTile) && OccupiedTiles2[targetTile] != this) SpeedMulti = 0; if (!OccupiedTilesFill.ContainsKey(myTile) && fullBlock) OccupiedTilesFill.Add(myTile, this); + if (OccupiedTiles2.ContainsKey(targetTile) && OccupiedTiles2[targetTile] != this) + { + SpeedMulti = 0; + blockingCar = OccupiedTiles2[targetTile]; + } + } + + if (SpeedMulti == 0 && blockingCar is not null) + { + RerouteTimePassed += deltaTime; + if (RerouteTimePassed > TimeUntilReroute) + { + var resetFunc = MainWindow.UpdatePathfinding(targetTile, 0, 3); + blockingCar.Move += resetFunc; + } + } else + { + RerouteTimePassed = 0; } var possibleDistance = Speed * deltaTime / 1000 * SpeedMulti; @@ -169,7 +202,8 @@ namespace CityGame.Classes.Entities Vector2 travelFinal = direction * finalDistance; X += travelFinal.X; Y += travelFinal.Y; - } else + } + else { if (Math.Round(Rotation) == 0 || Math.Round(Rotation) == 90) { diff --git a/CityGame/Classes/Entities/Helicopter.cs b/CityGame/Classes/Entities/Helicopter.cs index f64e62d..b89217d 100644 --- a/CityGame/Classes/Entities/Helicopter.cs +++ b/CityGame/Classes/Entities/Helicopter.cs @@ -9,7 +9,7 @@ namespace CityGame.Classes.Entities { public class Helicopter : Entity { - public float Speed { get; set; } = 256; + public float Speed { get; set; } = 512; public float RotSpeed { get; set; } = 1; public bool Landed = false; Image Heli1; @@ -93,6 +93,7 @@ namespace CityGame.Classes.Entities if (Target is not null) { Vector2 nextTarget = new Vector2(Target.X(), Target.Y()); + if (Target is Tile) nextTarget += new Vector2(1, 1); if (Target is Car car) { var correctionvector = new Vector2((float)Math.Cos(MathHelper.ToRadians(car.Rotation)), (float)Math.Sin(MathHelper.ToRadians(car.Rotation))); diff --git a/CityGame/Classes/Entities/PoliceCar.cs b/CityGame/Classes/Entities/PoliceCar.cs index 106ce23..bebc257 100644 --- a/CityGame/Classes/Entities/PoliceCar.cs +++ b/CityGame/Classes/Entities/PoliceCar.cs @@ -1,18 +1,17 @@ using CityGame.Classes.Rendering; +using CityGame.Classes.World; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; -using WPFGame; namespace CityGame.Classes.Entities { public class PoliceCar : Car { public static List PCars = new List(); - protected LightSource sirenLight; public PoliceCar() : base() { - desperate = true; + grid = 2; PCars.Add(this); Speed = 192; PNGFile = "PoliceCar.png"; @@ -21,25 +20,26 @@ namespace CityGame.Classes.Entities { OCanvas canvas = base.Render(); - sirenLight = new LightSource { Radius = 24, Angle = 24, Intensity = 4f, Color = Color.Red, Type = LightSourceType.PointLight, RotationOrigin = new Point(MainWindow.TileSize / 2) }; - canvas.Children.Add(sirenLight); - lights.Add(sirenLight); - Canvas.SetLeft(sirenLight, 43); - Canvas.SetTop(sirenLight, 32); - return canvas; } public override void Tick(long deltaTime) { - if (sirenLight is null) return; + if (PointLight is null) return; long ms = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; - if (ms / 250 % 2 == 0) + Tile myTile = MainWindow.Grid[Point.X, Point.Y]; if (myTile.Type == TileType.Garage) { - sirenLight.Color = Color.Red; + PointLight.Color = Color.White; } else { - sirenLight.Color = Color.Blue; + if (ms / 250 % 2 == 0) + { + PointLight.Color = Color.Red; + } + else + { + PointLight.Color = Color.Blue; + } } base.Tick(deltaTime); diff --git a/CityGame/Classes/Misc/Extensions.cs b/CityGame/Classes/Misc/Extensions.cs index 296dfbb..f3172e6 100644 --- a/CityGame/Classes/Misc/Extensions.cs +++ b/CityGame/Classes/Misc/Extensions.cs @@ -1,6 +1,5 @@ using Microsoft.Xna.Framework; using System; -using System.Windows; namespace CityGame { diff --git a/CityGame/Classes/Rendering/ColorConversionMaps.cs b/CityGame/Classes/Rendering/ColorConversionMaps.cs index 4d7212d..4e90312 100644 --- a/CityGame/Classes/Rendering/ColorConversionMaps.cs +++ b/CityGame/Classes/Rendering/ColorConversionMaps.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; namespace CityGame.Classes.Rendering { diff --git a/CityGame/Classes/Rendering/CustomShader.cs b/CityGame/Classes/Rendering/CustomShader.cs index dc7ac84..28bd6be 100644 --- a/CityGame/Classes/Rendering/CustomShader.cs +++ b/CityGame/Classes/Rendering/CustomShader.cs @@ -1,6 +1,4 @@ -using System; -using System.IO; -/* +/* namespace CityGame { public class CustomShader : ShaderEffect diff --git a/CityGame/Classes/Rendering/SourcedImage.cs b/CityGame/Classes/Rendering/SourcedImage.cs index f6102c0..54812c9 100644 --- a/CityGame/Classes/Rendering/SourcedImage.cs +++ b/CityGame/Classes/Rendering/SourcedImage.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using WPFGame; -/* +/* namespace CityGame.OldSourcedImage { diff --git a/CityGame/Licenses/ASTARLITE-LICENSE.txt b/CityGame/Licenses/ASTARLITE-LICENSE.txt new file mode 100644 index 0000000..58c4a84 --- /dev/null +++ b/CityGame/Licenses/ASTARLITE-LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Val Antonini + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/CityGame/Licenses/LAME-LICENSE.txt b/CityGame/Licenses/LAME-LICENSE.txt new file mode 100644 index 0000000..73462c7 --- /dev/null +++ b/CityGame/Licenses/LAME-LICENSE.txt @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013-2019 Corey Murtagh + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/CityGame/Licenses/NAUDIO-LICENSE.txt b/CityGame/Licenses/NAUDIO-LICENSE.txt new file mode 100644 index 0000000..3569456 --- /dev/null +++ b/CityGame/Licenses/NAUDIO-LICENSE.txt @@ -0,0 +1,7 @@ +Copyright 2020 Mark Heath + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/CityGame/Licenses/RE-UI-LICENSE.txt b/CityGame/Licenses/RE-UI-LICENSE.txt new file mode 100644 index 0000000..04a2289 --- /dev/null +++ b/CityGame/Licenses/RE-UI-LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 Evan Reeves + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/CityGame/Licenses/SIMPLEXNOISE-LICENSE.htm b/CityGame/Licenses/SIMPLEXNOISE-LICENSE.htm new file mode 100644 index 0000000..f25a726 --- /dev/null +++ b/CityGame/Licenses/SIMPLEXNOISE-LICENSE.htm @@ -0,0 +1,80 @@ + + + + + 'BSD-3-Clause' reference + + +
+ + + +

BSD 3-Clause "New" or "Revised" License

+ +

SPDX identifier

+
BSD-3-Clause
+ +

License text

+ +
+

Copyright (c) <year> <owner>.

+ +
+ +

Redistribution and use in source and binary forms, with or without modification, are permitted provided + that the following conditions are met:

+ +
    + +
  • + 1. + Redistributions of source code must retain the above copyright notice, this list of conditions + and the following disclaimer. +
  • + +
  • + 2. + Redistributions in binary form must reproduce the above copyright notice, this list of conditions + and the following disclaimer in the documentation and/or other materials provided with the + distribution. +
  • + +
  • + 3. + + Neither the name of the copyright holder nor the names of its contributors may + be used to endorse or promote products derived from this + software without specific prior written permission. +
  • + +
+

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ + + + + +

Notes

+

Note that the Eclipse Distribution License - v 1.0 (EDL 1.0) is a match to BSD-3-Clause, even though it uses a different name.

+ +

SPDX web page

+ + +

Notice

+

This license content is provided by the SPDX project. For more information about licenses.nuget.org, see our documentation. + +

Data pulled from spdx/license-list-data on February 9, 2023.

+
+ + + \ No newline at end of file diff --git a/CityGame/MainWindow.xaml.cs b/CityGame/MainWindow.xaml.cs index bf69f21..6434b13 100644 --- a/CityGame/MainWindow.xaml.cs +++ b/CityGame/MainWindow.xaml.cs @@ -10,16 +10,12 @@ using SimplexNoise; using System; using System.Collections.Generic; using System.Diagnostics; -using System.Globalization; using System.Linq; -using System.Speech.Synthesis; -using System.Speech.Synthesis.TtsEngine; using WPFGame; +using static CityGame.Classes.Entities.Car; namespace CityGame { - - public class Program { public static void Main() { new MainWindow(); } } /// /// Interaction logic for MainWindow.xaml /// @@ -64,10 +60,10 @@ namespace CityGame public static Vector2 MousePos = new Vector2(0, 0); public static PathFinder pathfinder; public static PathFinder pathfinderDesperate; - public static short[,] pathfindingGrid; - public static short[,] pathfindingGridDesperate; + public static Dictionary Grids = new Dictionary(); + public static Dictionary WorldGrids = new Dictionary(); public static List npcWalkable = new List(); - public static List Entities { get; set; } = new List(); + public static List Entities { get; set; } public const int TileSize = 64; public static Tile[,] Grid; public static ISelectable Selected; @@ -79,9 +75,63 @@ namespace CityGame Canvas CameraCanvas = new OCanvas(); Canvas UICanvas = new OCanvas(); + /// + /// Updates the pathfinding grids of all grid numbers includes in the "grids" parameter + /// + /// The tile to update + /// The value to update the tile to + /// The grids to perform the update for + /// A function that should be called once the reason for the update has resolved + public static CarEvent UpdatePathfinding(Tile tile, short value, int grids = 3, params Car[] exclude) + { + Dictionary resetValues = new Dictionary(); + foreach (var kvp in Grids) + { + if ((grids & kvp.Key) == kvp.Key) + { + resetValues[kvp.Key] = kvp.Value[tile.Y, tile.X]; + kvp.Value[tile.Y, tile.X] = value; + WorldGrids[kvp.Key][tile.Y, tile.X] = value; + } + } + + bool hasExecuted = false; + object lockObject = new object(); + CarEvent resetFunc = (Car exclude) => + { + lock (lockObject) + { + if (!hasExecuted) + { + hasExecuted = true; + + foreach (var kvp in Grids) + { + if ((grids & kvp.Key) == kvp.Key) + { + UpdatePathfinding(tile, resetValues[kvp.Key], kvp.Key, exclude); + } + } + } + } + }; + + InstantiatePathfinders(); + Car.Cars.ForEach(car => car.Path = !exclude.Contains(car) && (grids & car.grid) == car.grid ? null : car.Path); + + return resetFunc; + } + public static void InstantiatePathfinders() + { + pathfinder = new PathFinder(WorldGrids[1], new PathFinderOptions { PunishChangeDirection = true, UseDiagonals = false, SearchLimit = int.MaxValue, HeuristicFormula = AStar.Heuristics.HeuristicFormula.Euclidean }); + pathfinderDesperate = new PathFinder(WorldGrids[2], new PathFinderOptions { PunishChangeDirection = true, UseDiagonals = false, SearchLimit = int.MaxValue, HeuristicFormula = AStar.Heuristics.HeuristicFormula.Euclidean }); + } + public static AudioListener SoundEffectListener; public MainWindow() { + _graphics.PreferredBackBufferWidth = 1920; + _graphics.PreferredBackBufferHeight = 1080; AddPenumbra(); #region | Texture Conversions | @@ -116,7 +166,7 @@ namespace CityGame Noise.Seed = seed; int mapHeight = 100; - int mapWidth = 100; + int mapWidth = 200; float[,] lakeMap = Noise.Calc2D(mapWidth, mapHeight, 0.05f); @@ -139,6 +189,7 @@ namespace CityGame int NPCCount = (int)Math.Ceiling(mapHeight * mapWidth / 100f); //NPCCount = 1; + //NPCCount = 0; random = new Random(seed); @@ -223,8 +274,8 @@ namespace CityGame } } Dictionary decidedBridges = new Dictionary(); - pathfindingGrid = new short[doubleWidth, doubleHeight]; - pathfindingGridDesperate = new short[doubleWidth, doubleHeight]; + Grids.Add(1, new short[doubleHeight, doubleWidth]); + Grids.Add(2, new short[doubleHeight, doubleWidth]); List bridgeTiles = new List(); List roadTiles = new List(); for (int y = 0; y < doubleHeight; y++) @@ -347,19 +398,19 @@ namespace CityGame { var type = IntermediateGrid[x, y].Type; bool walkable = ((int)type) / 100 == 4; - pathfindingGridDesperate[y, x] = (short)(walkable ? 1 : 0); + Grids[2][y, x] = (short)(walkable ? 1 : 0); if (type == TileType.Path) walkable = false; - pathfindingGrid[y, x] = (short)(walkable ? 1 : 0); + Grids[1][y, x] = (short)(walkable ? 1 : 0); if (type == TileType.Bridge) bridgeTiles.Add(IntermediateGrid[x, y]); if (type == TileType.Road) roadTiles.Add(IntermediateGrid[x, y]); if (type == TileType.Road || type == TileType.Bridge) npcWalkable.Add(IntermediateGrid[x, y]); } } - var worldGrid = new WorldGrid(pathfindingGrid); - pathfinder = new PathFinder(worldGrid, new PathFinderOptions { PunishChangeDirection = true, UseDiagonals = false, SearchLimit = int.MaxValue, HeuristicFormula = AStar.Heuristics.HeuristicFormula.Euclidean }); - var worldGridDesperate = new WorldGrid(pathfindingGridDesperate); - pathfinderDesperate = new PathFinder(worldGridDesperate, new PathFinderOptions { PunishChangeDirection = true, UseDiagonals = false, SearchLimit = int.MaxValue, HeuristicFormula = AStar.Heuristics.HeuristicFormula.Euclidean }); + foreach (var kvp in Grids) WorldGrids.Add(kvp.Key, new WorldGrid(kvp.Value)); + InstantiatePathfinders(); + + Entities = new List(); foreach (Image image in SourcedImage.GetObjectsBySourceFile("Helipad.png")) { diff --git a/CityGame/Program.cs b/CityGame/Program.cs new file mode 100644 index 0000000..fab28b1 --- /dev/null +++ b/CityGame/Program.cs @@ -0,0 +1,28 @@ +using Microsoft.Xna.Framework.Graphics; +using WPFGame; + +namespace CityGame +{ + public class Program { + public static void Main() { + var menu = new MenuWindow(); + } + } + public class MenuWindow : Window + { + private GraphicsDevice graphics; + public MenuWindow() + { + + + Show(); + } + } + public class GenerationSettings + { + public GenerationSettings() + { + + } + } +} \ No newline at end of file