public InGameView(string level, IGameView v) { mainMenu = v; if (File.Exists("settings.json")) settings = JsonConvert.DeserializeObject<GameSettings>(File.ReadAllText("settings.json")); else { settings = new GameSettings(); settings.EnableClouds = true; } bgFore = new Sprite(new Texture("Content/bgFore.png") { Smooth = true }); bgFore.Origin = new Vector2f(0, 256); bgFore.Scale = new Vector2f(2, 2); bgBack = new Sprite(new Texture("Content/bgBack.png") { Smooth = true }); bgBack.Origin = new Vector2f(0, 256); bgBack.Scale = new Vector2f(2, 2); world = new PhysicsWorld(); SceneDeserializer d = new SceneDeserializer(SceneLoader.Load(level)); player = new Player(d.Scene.StartCoords, world); d.AddObjects(world); player.FinishHigh = d.Scene.FinishHigh; player.FinishLow = d.Scene.FinishLow; portal = new Sprite(new Texture("Content/portal.png") { Smooth = true }); portal.Scale = new Vector2f(2, 2); portal.Position = new Vector2f(player.FinishLow.X + (player.FinishHigh.X - player.FinishLow.X) * 0.5f, player.FinishLow.Y + (player.FinishHigh.Y - player.FinishLow.Y) * 0.5f) * 10; scene = new Scene(ScrollInputs.None); listener = new GameEventListener(world, player); listener.OnWin += listener_OnWin; scene.AddComponent(listener); cloudSprite = new Sprite(); clouds = new Texture(256, 64); clouds.Smooth = true; if (settings.EnableClouds) { GenerateClouds(); } bgGradient = new Texture("Content/bg.png"); bg = new Sprite(bgGradient); }
public GameEventListener(PhysicsWorld world, Player player) { this.world = world; this.player = player; player.OnWin += player_OnWin; }
public void Run() { window = new RenderWindow(new VideoMode(1280, 720), "DOX30 Editor", Styles.Titlebar | Styles.Close); window.Closed += window_Closed; window.Resized += window_Resized; window.MouseWheelMoved += window_MouseWheelMoved; window.MouseButtonPressed += window_MouseButtonPressed; window.MouseMoved += window_MouseMoved; window.MouseButtonReleased += window_MouseButtonReleased; window.KeyPressed += window_KeyPressed; window.KeyReleased += window_KeyReleased; world = new PhysicsWorld(true); ui = new UISceneManager(); ui.Init(window); Scene scene = new Scene(ScrollInputs.None); RectControl bg = new RectControl() { Position = new Vector2f(0, 0), Size = new Vector2f(1280, 50), Anchor = AnchorPoints.Left | AnchorPoints.Top | AnchorPoints.Right, BackgroundColor = Colors.WhiteSmoke }; FastButton runButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/playButton.png", "Content/playButton.png", "Content/playButton.png") { Position = new Vector2f(0, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; runButton.OnClick += (s, e) => { enabled = true; }; FastButton pauseButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/pauseButton.png", "Content/pauseButton.png", "Content/pauseButton.png") { Position = new Vector2f(50, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; pauseButton.OnClick += (s, e) => { enabled = false; }; FastButton saveButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/saveButton.png", "Content/saveButton.png", "Content/saveButton.png") { Position = new Vector2f(125, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; saveButton.OnClick += (s, e) => { Export(); }; FastButton loadButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/loadButton.png", "Content/loadButton.png", "Content/loadButton.png") { Position = new Vector2f(175, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; loadButton.OnClick += (s, e) => { var ofd = new System.Windows.Forms.OpenFileDialog(); ofd.Filter = "Level Files (*.json)|*.json"; ofd.InitialDirectory = Directory.GetCurrentDirectory(); var r = ofd.ShowDialog(); if (r == System.Windows.Forms.DialogResult.OK) { Import(ofd.FileName); } }; FastButton addBoxButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/boxButton.png", "Content/boxButton.png", "Content/boxButton.png") { Position = new Vector2f(250, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; addBoxButton.OnClick += (s, e) => { world.CreateBox(new Vector2f(10, 10), 4, BodyType.Static).Position = new Vector2(offset.X, offset.Y) * -0.1f; world.Step(0); }; FastButton addCircleButton = new FastButton(new Font("Content/font.ttf"), 22, "Content/circleButton.png", "Content/circleButton.png", "Content/circleButton.png") { Position = new Vector2f(300, 0), Size = new Vector2f(50, 48), Text = "", Anchor = AnchorPoints.Left | AnchorPoints.Top }; addCircleButton.OnClick += (s, e) => { world.CreateCircle(5, 5, BodyType.Static).Position = new Vector2(offset.X, offset.Y) * -0.1f; world.Step(0); }; scene.AddComponent(bg); scene.AddComponent(runButton); scene.AddComponent(pauseButton); scene.AddComponent(addBoxButton); scene.AddComponent(addCircleButton); scene.AddComponent(saveButton); scene.AddComponent(loadButton); // scene.AddComponent(new WorldHierachyRenderer(world) { Size = new Vector2f(300, 670), Position = new Vector2f(0, 50), BackgroundColor = Colors.Snow }); // Does not update on change dynamic/static ui.CurrentScene = scene; grid = new Sprite(new Texture("Content/grid.png")); grid.Origin = new Vector2f(512, 512); contextMenu = new ContextMenu(); contextMenu.Add(() => { selected.BodyType = BodyType.Dynamic; }, "Set Dynamic"); contextMenu.Add(() => { selected.BodyType = BodyType.Static; }, "Set Static"); contextMenu.Add(() => { world.FindBody(selected).GameDimension = Dimension.None; }, "Set No Dimension"); contextMenu.Add(() => { world.FindBody(selected).GameDimension = world.FindBody(selected).GameDimension == Dimension.OneO ? Dimension.TwoX : Dimension.OneO; }, "Switch Dimension"); contextMenu.Add(() => { selected.Rotation += 0.0872664626f; }, "+5 Rotation"); contextMenu.Add(() => { selected.Rotation += 0.785398163f; }, "+45 Rotation"); contextMenu.Add(() => { selected.Rotation -= 0.0872664626f; }, "-5 Rotation"); contextMenu.Add(() => { selected.Rotation -= 0.785398163f; }, "-45 Rotation"); contextMenu.Add(() => { world.Copy(selected, new Vector2(offset.X, offset.Y) * -0.1f); }, "Duplicate"); contextMenu.Add(() => { world.Remove(selected); }, "Remove"); messageScene = new Scene(ScrollInputs.None); Stopwatch sw = new Stopwatch(); TimeSpan elapsed = TimeSpan.Zero; TimeSpan secondCounter = TimeSpan.Zero; int frames = 0; world.Step(0); View v; Console.WriteLine(window.GetView().Center); while (window.IsOpen()) { sw.Start(); window.DispatchEvents(); window.Clear(); if (enabled) world.Step((float)elapsed.TotalSeconds); v = window.GetView(); v.Zoom(zoom); v.Center = (world.CamLock == null ? -offset : new Vector2f(world.CamLock.Position.X * 10, world.CamLock.Position.Y * 10)); if (world.CamLock != null) v.Rotation = world.CamLock.Rotation * 57.2957795f; else v.Rotation = 0; window.SetView(v); window.Draw(grid); world.Render(window); v = window.GetView(); v.Size = new Vector2f(1280, 720); v.Rotation = 0; v.Center = new Vector2f(640, 360); window.SetView(v); ui.Render(window); ui.CurrentScene = messageScene; ui.Render(window); ui.CurrentScene = scene; contextMenu.Render(window); window.Display(); sw.Stop(); elapsed = sw.Elapsed; secondCounter += elapsed; frames++; if (secondCounter >= TimeSpan.FromSeconds(1)) { Console.WriteLine(frames / secondCounter.TotalSeconds); secondCounter -= TimeSpan.FromSeconds(1); frames = 0; } sw.Reset(); } }