public MainWindow() { ViewModel = new MainWindowViewModel(world); InitializeComponent(); keyboardHandler = new KeyboardHandler(tickInterval); keyboardHandler.HoldableKeys.Add(new HoldableKey(Key.Left, (duration) => World.Instance.ControlledCar.X -= 5, null)); keyboardHandler.HoldableKeys.Add(new HoldableKey(Key.Right, (duration) => World.Instance.ControlledCar.X += 5, null)); keyboardHandler.HoldableKeys.Add(new HoldableKey(Key.Up, (duration) => World.Instance.ControlledCar.Y -= 5, null)); keyboardHandler.HoldableKeys.Add(new HoldableKey(Key.Down, (duration) => World.Instance.ControlledCar.Y += 5, null)); hmiDebug = new HMIDebug(); keyboardHandler.PressableKeys.Add(new PressableKey(Key.D1, () => hmiDebug.OnDebugAction(1))); keyboardHandler.PressableKeys.Add(new PressableKey(Key.D2, () => hmiDebug.OnDebugAction(2))); keyboardHandler.PressableKeys.Add(new PressableKey(Key.D3, () => hmiDebug.OnDebugAction(3))); keyboardHandler.PressableKeys.Add(new PressableKey(Key.D4, () => hmiDebug.OnDebugAction(4))); timer.Interval = TimeSpan.FromMilliseconds(tickInterval); timer.Tick += logic; timer.Start(); // make my dockpanel focus of this game MainDockPanel.Focus(); world.Width = 2000; world.Height = 1000; var circle = new Circle(400, 200, "circle.png", 20); circle.Width = 40; circle.Height = 40; circle.ZIndex = 2; world.AddObject(circle); var controlledCar = new Models.AutomatedCar(50, 50, "car_1_white.png"); controlledCar.Width = 108; controlledCar.Height = 240; // read the world object polygons, get the one for the car in a primitive way // this is just a sample, the proecssing shold be much more general string json_text = System.IO.File.ReadAllText($"Assets/worldobject_polygons.json"); dynamic stuff = JObject.Parse(json_text); var geom = new Polyline(); // get the points from the json and add to the polyline foreach (var i in stuff["objects"][0]["polys"][0]["points"]) { geom.Points.Add(new Point(i[0].ToObject <int>(), i[1].ToObject <int>())); } // add polyline to the car controlledCar.Geometry = geom; world.AddObject(controlledCar); world.ControlledCar = controlledCar; controlledCar.Start(); }
public MainWindow() { ViewModel = new MainWindowViewModel(world); InitializeComponent(); timer.Interval = TimeSpan.FromMilliseconds(20); timer.Tick += logic; timer.Start(); // make my dockpanel focus of this game MainDockPanel.Focus(); world.Width = 2000; world.Height = 1000; var circle = new Circle(400, 200, "circle.png", 20); circle.Width = 40; circle.Height = 40; circle.ZIndex = 2; world.AddObject(circle); var controlledCar = new Models.AutomatedCar(50, 50, "car_1_white.png"); controlledCar.Width = 108; controlledCar.Height = 240; // read the world object polygons, get the one for the car in a primitive way // this is just a sample, the proecssing shold be much more general string json_text = System.IO.File.ReadAllText($"src/AutomatedCar/Assets/worldobject_polygons.json"); dynamic stuff = JObject.Parse(json_text); var geom = new Polyline(); // get the points from the json and add to the polyline foreach (var i in stuff["objects"][0]["polys"][0]["points"]) { geom.Points.Add(new Point(i[0].ToObject <int>(), i[1].ToObject <int>())); } // add polyline to the car controlledCar.Geometry = geom; world.AddObject(controlledCar); world.ControlledCar = controlledCar; controlledCar.Start(); }
public override void OnFrameworkInitializationCompleted() { if (this.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { JsonParser parser = new JsonParser(); parser.populateWorldObjects(World.Instance, $"AutomatedCar.Assets.test_world.json"); var world = World.Instance; StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly() .GetManifestResourceStream($"AutomatedCar.Assets.worldobject_polygons.json")); string json_text = reader.ReadToEnd(); dynamic stuff = JObject.Parse(json_text); List <Avalonia.Point> points = new List <Avalonia.Point>(); foreach (var i in stuff["objects"][0]["polys"][0]["points"]) { points.Add(new Avalonia.Point(i[0].ToObject <int>(), i[1].ToObject <int>())); } PolylineGeometry geom = new PolylineGeometry(points, false); int W = 108; int H = 240; List <List <Point> > polyList = new List <List <Point> >(); polyList.Add(points); AutomatedCar controlledCar = new Models.AutomatedCar(0, 0, "car_1_white", W, H, polyList); controlledCar.RadarBrush = new SolidColorBrush(Color.Parse("blue")); controlledCar.CameraBrush = new SolidColorBrush(Color.Parse("red")); List <Avalonia.Point> sensorPoints = new List <Avalonia.Point>(); sensorPoints.Add(new Avalonia.Point(51, 239)); sensorPoints.Add(new Avalonia.Point(200, 100)); sensorPoints.Add(new Avalonia.Point(100, 300)); List <Avalonia.Point> cameraSensorPoints = new List <Avalonia.Point>(); cameraSensorPoints.Add(new Avalonia.Point(100, 200)); cameraSensorPoints.Add(new Avalonia.Point(300, 200)); cameraSensorPoints.Add(new Avalonia.Point(150, 300)); controlledCar.RadarGeometry = new PolylineGeometry(sensorPoints, false); controlledCar.Geometry = geom; controlledCar.RadarVisible = true; controlledCar.UltraSoundVisible = true; controlledCar.CameraGeometry = new PolylineGeometry(cameraSensorPoints, false); controlledCar.CameraVisible = true; world.AddObject(controlledCar); world.ControlledCar = controlledCar; controlledCar.Start(); NpcCar npcCar = new NpcCar("car_1_red", W, H, polyList, $"AutomatedCar.Assets.npcCarRoute.json"); npcCar.SetStartPosition(); world.AddObject(npcCar); NpcPedestrian npcPedMan = new NpcPedestrian("man", W / 3, H / 3, polyList, $"AutomatedCar.Assets.npcPedRoute1.json"); npcPedMan.SetStartPosition(); world.AddObject(npcPedMan); NpcPedestrian npcPedWoman = new NpcPedestrian("woman", W / 3, H / 3, polyList, $"AutomatedCar.Assets.npcPedRoute2.json"); npcPedWoman.SetStartPosition(); world.AddObject(npcPedWoman); world.OnTick += npcCar.Move; world.OnTick += npcPedMan.Move; world.OnTick += npcPedWoman.Move; Game game = new Game(world); game.Start(); desktop.MainWindow = new MainWindow { DataContext = new MainWindowViewModel(world) }; } base.OnFrameworkInitializationCompleted(); }