Added curvature to cars taking turns (90° only)

This commit is contained in:
riedel 2023-05-10 12:52:53 +02:00
parent 003903b60c
commit 6a4ed4b3a8
5 changed files with 102 additions and 10 deletions

View file

@ -15,7 +15,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\RE-UI-a-Monogame-GUI\MGUI\MGUI.csproj" />
<ProjectReference Include="..\..\WPFGame\WPFGame\WPFGame.csproj" /> <ProjectReference Include="..\..\WPFGame\WPFGame\WPFGame.csproj" />
</ItemGroup> </ItemGroup>
@ -151,4 +150,14 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Assemblies\" />
</ItemGroup>
<ItemGroup>
<Reference Include="MGUI">
<HintPath>..\..\..\RE-UI-a-Monogame-GUI\MGUI\bin\Debug\net6.0\MGUI.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>

View file

@ -90,9 +90,13 @@ namespace CityGame.Classes.Entities
Move += c => { }; Move += c => { };
} }
public event CarEvent Move; public event CarEvent Move;
private int curveMode;
private float curveModePixelDuration;
private int curveModeStartedAt;
public override void Tick(long deltaTime) public override void Tick(long deltaTime)
{ {
//deltaTime /= 10;
//deltaTime *= 500; //deltaTime *= 500;
Tuple<TileType, string>[] fullBlockTiles = new Tuple<TileType, string>[] Tuple<TileType, string>[] fullBlockTiles = new Tuple<TileType, string>[]
{ {
@ -101,7 +105,7 @@ namespace CityGame.Classes.Entities
new Tuple<TileType, string>(TileType.Road, "1"), new Tuple<TileType, string>(TileType.Road, "1"),
new Tuple<TileType, string>(TileType.Garage, null) new Tuple<TileType, string>(TileType.Garage, null)
}; };
if (this == MainWindow.Selected) Debug.WriteLine("Selected."); //if (this == MainWindow.Selected) Debug.WriteLine("Selected.");
Tile myTile = MainWindow.Grid[Point.X, Point.Y]; 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)); 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) if (myTile.Type == TileType.Garage)
@ -192,14 +196,85 @@ namespace CityGame.Classes.Entities
var resetFunc = MainWindow.UpdatePathfinding(targetTile, 0, 3); var resetFunc = MainWindow.UpdatePathfinding(targetTile, 0, 3);
blockingCar.Move += resetFunc; blockingCar.Move += resetFunc;
} }
} else }
else
{ {
RerouteTimePassed = 0; RerouteTimePassed = 0;
} }
if (Path is not null && Path.Length > NextTarget + 1 && curveMode == 0)
{
var actPoint = Path[NextTarget + 1];
var afterCurveTile = MainWindow.Grid[actPoint.X, actPoint.Y];
// North
if (Rotation == 0 && afterCurveTile.X == nextTarget.X - 1 && afterCurveTile.Y == nextTarget.Y) curveMode = -1; // Left
else if (Rotation == 0 && afterCurveTile.X == nextTarget.X + 1 && afterCurveTile.Y == nextTarget.Y) curveMode = 1; // Right
// East
else if (Rotation == 90 && afterCurveTile.X == nextTarget.X && afterCurveTile.Y == nextTarget.Y - 1) curveMode = -1; // Left
else if (Rotation == 90 && afterCurveTile.X == nextTarget.X && afterCurveTile.Y == nextTarget.Y + 1) curveMode = 1; // Right
// South
else if (Rotation == 180 && afterCurveTile.X == nextTarget.X + 1 && afterCurveTile.Y == nextTarget.Y) curveMode = -1; // Left
else if (Rotation == 180 && afterCurveTile.X == nextTarget.X - 1 && afterCurveTile.Y == nextTarget.Y) curveMode = 1; // Right
// West
else if (Rotation == 270 && afterCurveTile.X == nextTarget.X && afterCurveTile.Y == nextTarget.Y + 1) curveMode = -1; // Left
else if (Rotation == 270 && afterCurveTile.X == nextTarget.X && afterCurveTile.Y == nextTarget.Y - 1) curveMode = 1; // Right
if (curveMode != 0)
{
curveModePixelDuration = MainWindow.TileSize * 2;
curveModeStartedAt = NextTarget;
}
}
var possibleDistance = Speed * deltaTime / 1000 * SpeedMulti; var possibleDistance = Speed * deltaTime / 1000 * SpeedMulti;
var finalDistance = Math.Min(possibleDistance, travel.Length()); var finalDistance = Math.Min(possibleDistance, travel.Length());
Vector2 travelFinal = direction * finalDistance; Vector2 travelFinal = direction * finalDistance;
UseVisualPosition = false;
if (curveMode != 0)
{
float rotationStart = MainWindow.TileSize * 1.25f;
float rotationEnd = MainWindow.TileSize * 0.75f;
curveModePixelDuration -= travelFinal.Length();
if (curveModePixelDuration <= rotationEnd) curveMode = 0;
else
{
UseVisualPosition = true;
if (curveModePixelDuration < rotationStart && curveModePixelDuration > rotationEnd)
{
float percentage = (curveModePixelDuration - rotationEnd) / (rotationStart - rotationEnd);
if (this == MainWindow.Selected) Debug.WriteLine(percentage);
float vRotDeg = 0;
if (NextTarget == curveModeStartedAt)
{
float percentage2 = percentage - 0.5f;
percentage2 *= 2;
vRotDeg = (45 - 45 * percentage2) * curveMode;
}
else
{
float percentage2 = percentage * 2;
vRotDeg = (-45 * percentage2) * curveMode;
}
if (this == MainWindow.Selected) Debug.WriteLine(vRotDeg);
visualX = X;
visualY = Y;
visualRotation = Rotation + vRotDeg;
}
else
{
visualX = X;
visualY = Y;
visualRotation = Rotation;
}
}
}
X += travelFinal.X; X += travelFinal.X;
Y += travelFinal.Y; Y += travelFinal.Y;
} }

View file

@ -7,6 +7,13 @@ namespace CityGame.Classes.Entities
{ {
public float X { get; set; } public float X { get; set; }
public float Y { get; set; } public float Y { get; set; }
protected float visualX;
protected float visualY;
protected float visualRotation;
public float VisualX { get => UseVisualPosition ? visualX : X; set => X = visualX = value; }
public float VisualY { get => UseVisualPosition ? visualY : Y; set => Y = visualY = value; }
public float VisualRotation { get => UseVisualPosition ? visualRotation : Rotation; set => Rotation = visualRotation = value; }
public bool UseVisualPosition { get; set; }
public float Rotation { get; set; } public float Rotation { get; set; }
public long Time { get; set; } public long Time { get; set; }
public OCanvas Object { get; set; } public OCanvas Object { get; set; }

View file

@ -37,7 +37,7 @@ namespace CityGame
if (x < 0 || y < 0) return null; if (x < 0 || y < 0) return null;
if (x > Grid.GetLength(0) - 1 || y > Grid.GetLength(1) - 1) return null; if (x > Grid.GetLength(0) - 1 || y > Grid.GetLength(1) - 1) return null;
Debug.WriteLine(Grid[x, y]); //Debug.WriteLine(Grid[x, y]);
return Grid[x, y]; return Grid[x, y];
} }
public static Entity GetEntityFromImage(Image image) public static Entity GetEntityFromImage(Image image)
@ -478,7 +478,7 @@ namespace CityGame
swv = state.ScrollWheelValue; swv = state.ScrollWheelValue;
if (delta != 0) if (delta != 0)
{ {
Debug.WriteLine(delta); //Debug.WriteLine(delta);
} }
float multi = 1.05f; float multi = 1.05f;
if (delta < 0) multi = 1 / multi; if (delta < 0) multi = 1 / multi;
@ -513,6 +513,7 @@ namespace CityGame
foreach (Entity entity in Entities) foreach (Entity entity in Entities)
{ {
long deltaTime = milliseconds - entity.Time; long deltaTime = milliseconds - entity.Time;
deltaTime = (long)time.ElapsedGameTime.TotalMilliseconds;
entity.Time = milliseconds; entity.Time = milliseconds;
entity.Tick(deltaTime); entity.Tick(deltaTime);
@ -521,9 +522,9 @@ namespace CityGame
entity.Object = entity.Render(); entity.Object = entity.Render();
GameCanvas.Children.Add(entity.Object); GameCanvas.Children.Add(entity.Object);
} }
entity.Object.Rotation = (int)entity.Rotation; entity.Object.Rotation = (int)entity.VisualRotation;
Canvas.SetLeft(entity.Object, entity.X); Canvas.SetLeft(entity.Object, entity.VisualX);
Canvas.SetTop(entity.Object, entity.Y); Canvas.SetTop(entity.Object, entity.VisualY);
} }
Car.OccupiedTiles = Car.OccupiedTilesFill; Car.OccupiedTiles = Car.OccupiedTilesFill;
Car.OccupiedTilesFill = new Dictionary<Tile, Car>(); Car.OccupiedTilesFill = new Dictionary<Tile, Car>();

View file

@ -5,7 +5,7 @@ namespace CityGame
{ {
public class Program { public class Program {
public static void Main() { public static void Main() {
var menu = new MenuWindow(); var menu = new MainWindow();
} }
} }
public class MenuWindow : Window public class MenuWindow : Window