From 2189ba267ba51aec8f5d8be61f9980b41aa3126c Mon Sep 17 00:00:00 2001 From: Jonathan Riedel Date: Sun, 7 May 2023 22:50:39 +0200 Subject: [PATCH] Added lighting --- CityGame/App.xaml | 10 +--------- CityGame/Car.cs | 24 +++++++++++++++++++++++- CityGame/Helicopter.cs | 27 ++++++++++++++++++++++----- CityGame/IntPoint.cs | 4 ++++ CityGame/MainWindow.xaml.cs | 3 +++ 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/CityGame/App.xaml b/CityGame/App.xaml index 51191dd..5f28270 100644 --- a/CityGame/App.xaml +++ b/CityGame/App.xaml @@ -1,9 +1 @@ - - - - - + \ No newline at end of file diff --git a/CityGame/Car.cs b/CityGame/Car.cs index 5fd9838..d35891a 100644 --- a/CityGame/Car.cs +++ b/CityGame/Car.cs @@ -29,7 +29,29 @@ namespace CityGame public float Speed { get; set; } = 128; public override OCanvas Render() { - return new SourcedImage("Car.png"); + OCanvas canvas = new OCanvas(); + Image car = new SourcedImage("Car.png"); + + canvas.Children.Add(car); + var light = new LightSource { Radius = 64, Intensity = 2, Color = Color.White, Type = LightSourceType.Spotlight, Rotation = -90, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + var light2 = new LightSource { Radius = 64, Intensity = 2, Color = Color.White, Type = LightSourceType.Spotlight, Rotation = -90, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + Canvas.SetLeft(light, 39); + Canvas.SetTop(light, 19); + Canvas.SetLeft(light2, 46); + Canvas.SetTop(light2, 19); + canvas.Children.Add(light); + canvas.Children.Add(light2); + + var blight = new LightSource { Radius = 24, Intensity = 1, Color = Color.Red, Type = LightSourceType.PointLight, Rotation = 90, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + var blight2 = new LightSource { Radius = 24, Intensity = 1, Color = Color.Red, Type = LightSourceType.PointLight, Rotation = 90, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + Canvas.SetLeft(blight, 39); + Canvas.SetTop(blight, 46); + Canvas.SetLeft(blight2, 46); + Canvas.SetTop(blight2, 46); + canvas.Children.Add(blight); + canvas.Children.Add(blight2); + + return canvas; } public Car() { diff --git a/CityGame/Helicopter.cs b/CityGame/Helicopter.cs index 7d1da92..dcc20cd 100644 --- a/CityGame/Helicopter.cs +++ b/CityGame/Helicopter.cs @@ -1,5 +1,5 @@ -using System; -using System.Numerics; +using Microsoft.Xna.Framework; +using System; using WPFGame; namespace CityGame @@ -15,6 +15,7 @@ namespace CityGame int RotorState = 0; public ISelectable Target; bool Move; + public LightSource Spotlight; public override OCanvas Render() { OCanvas canvas = new OCanvas(); @@ -31,6 +32,12 @@ namespace CityGame canvas.Children.Add(Blades1); canvas.Children.Add(Blades2); + Spotlight = new LightSource { Type = LightSourceType.Spotlight, Color = Color.White, Intensity = 3, Rotation = -90, Radius = MainWindow.TileSize * 3, RotationOrigin = new Point(MainWindow.TileSize / 2, MainWindow.TileSize / 2) }; + Canvas.SetTop(Spotlight, 7); + Canvas.SetLeft(Spotlight, 32); + + canvas.Children.Add(Spotlight); + return canvas; } @@ -50,14 +57,24 @@ namespace CityGame if (Target is not null) { IntPoint nextTarget = new IntPoint(Target.X(), Target.Y()); + if(Target is Car car) + { + var correctionvector = new IntPoint((int)Math.Cos(Microsoft.Xna.Framework.MathHelper.ToRadians(car.Rotation)), (int)Math.Sin(Microsoft.Xna.Framework.MathHelper.ToRadians(car.Rotation))); + correctionvector *= MainWindow.TileSize / 4; + nextTarget += correctionvector; + } Vector2 travel = new Vector2((float)nextTarget.X - X, (float)nextTarget.Y - Y); - if (travel.Length() < MainWindow.TileSize * 1) Move = false; - if (travel.Length() > MainWindow.TileSize * 3) Move = true; + float minDistance = MainWindow.TileSize * 1; + if (Target is Tile) minDistance = 0; + float minSpeedyDistance = MainWindow.TileSize * 3; + Spotlight.Radius = (int)Math.Min(minSpeedyDistance, travel.Length()); + if (travel.Length() < minDistance) Move = false; + if (travel.Length() > minSpeedyDistance) Move = true; Vector2 direction = Vector2.Normalize(travel); float degrees = (float)(Math.Atan2(direction.Y, direction.X) * (180 / Math.PI)) + 90; Rotation = degrees; float Speedmulti = 1; - if (travel.Length() < MainWindow.TileSize * 3) Speedmulti = (travel.Length() - MainWindow.TileSize) / (MainWindow.TileSize * 2); + if (travel.Length() < minSpeedyDistance) Speedmulti = (travel.Length() - minDistance) / (minSpeedyDistance - minDistance); var possibleDistance = Speed * Speedmulti * deltaTime / 1000; var finalDistance = Math.Min(possibleDistance, travel.Length()); Vector2 travelFinal = direction * finalDistance; diff --git a/CityGame/IntPoint.cs b/CityGame/IntPoint.cs index de83ee7..ac36957 100644 --- a/CityGame/IntPoint.cs +++ b/CityGame/IntPoint.cs @@ -13,6 +13,10 @@ { return new IntPoint(a.X + b.X, a.Y + b.Y); } + public static IntPoint operator *(IntPoint a, int b) + { + return new IntPoint(a.X * b, a.Y * b); + } public static bool operator !=(IntPoint a, IntPoint b) { return a.X != b.X || a.Y != b.Y; diff --git a/CityGame/MainWindow.xaml.cs b/CityGame/MainWindow.xaml.cs index bf913aa..7854907 100644 --- a/CityGame/MainWindow.xaml.cs +++ b/CityGame/MainWindow.xaml.cs @@ -73,6 +73,8 @@ namespace CityGame Canvas UICanvas = new OCanvas(); public MainWindow() { + AddPenumbra(); + #region | Texture Conversions | string[] patternCodes = new[] { "0", "1", "2", "2c", "3", "3a", "3ab", "3c", "4", "4m", "4c", "5", "7", "8" }; @@ -125,6 +127,7 @@ namespace CityGame int minBlockWidth = 3; int NPCCount = (int)Math.Ceiling(mapHeight * mapWidth / 100f); + //NPCCount = 1; random = new Random(seed);