public bool Hook(Controller controller) { if (controller is WindowsMouseController) { WindowsMouseController mwc = controller as WindowsMouseController; return(HookMouse(mwc.Frame_KeyInfo, mwc.Frame_KeyDown, mwc.Frame_KeyUp)); } else if (controller is WindowsKeyController) { WindowsKeyController wkc = controller as WindowsKeyController; return(HookKeyboard(wkc.Frame_KeyDown, wkc.Frame_KeyUp)); } else { throw new ArgumentOutOfRangeException($"{this.GetType().Name} does not use {controller.GetType().Name}"); } }
public static void Main(string[] args) { WavPlayer.Init(WavProvider); WavPlayer.Play(); Keyboard = new WindowsKeyController(keyMap.ToDictionary(kvp => (int)kvp.Key, kvp => (int)kvp.Value)); Mouse = new WindowsMouseController(mouseMap.ToDictionary(kvp => (int)kvp.Key, kvp => (int)kvp.Value)); Builder = new GameBuilder(); Builder.GameEngine(new FixedTickEngine(TPS)) .GameView(new GameView2D(ScreenWidth, ScreenHeight, Scale, Scale, Color.DarkSlateGray)) .GameFrame(new GameFrame(new AvaloniaWindowBuilder(), 0, 0, ScreenWidth, ScreenHeight, Scale, Scale)) .Controller(Keyboard) .Controller(Mouse) .Build(); Engine = Builder.Engine; GameFrame frame = Builder.Frame; AvaloniaWindow window = frame.Window as AvaloniaWindow; Referee = new Referee(); Engine.TickEnd += Referee.Tick; Sprites(); Rules(); SetupLevels(); SetupTitleScreen(); Engine.TickEnd += (s, gs) => { if (Iframe > 0) { Iframe--; } if (Program.Keyboard[(int)Actions.ESCAPE].IsPress()) { Program.WavPlayer.Stop(); Program.WavProvider.RemoveAllMixerInputs(); StopMovingWindow(); SetupTitleScreen(); } if (Program.Keyboard[(int)Actions.DIAGS].IsPress()) { ShowDiags = !ShowDiags; } if (Program.Keyboard[(int)Actions.RESTART].IsPress()) { if ((Program.Referee.OutofControl || Program.Level == 7) && !Engine.Location.GetEntities <Banner>().Any()) { return; } if (Engine.Location.GetEntities <Banner>().FirstOrDefault()?.Text == "you win") { if (Level == -1) { for (int i = 0; i < 10; i++) { Referee.AddRule(Rule.GetNameRandomRule()); } } else { Level++; } } StopMovingWindow(); Program.WavPlayer.Stop(); Program.WavProvider.RemoveAllMixerInputs(); // Levels can call the reset with something different Referee.ResetTimer(); switch (Level) { case -1: SetupCrazyMode(); break; case 0: break; case 8: SetupCredits(); Level = 9; break; case 9: if (CreditsFinished) { SetupThanksForPlaying(); Level = 10; } break; case 10: SetupTitleScreen(); break; default: if (CreditsFinished) { StartingLevel = Level; Levels[Level - 1].SetupLevel(); } break; } if (Program.Diff == Difficulty.NORMAL && Level == 7) { Lives = 3; } else if (Program.Diff == Difficulty.EASY && Level == 7) { Lives = 4; } else { Lives = 1; } } }; Engine.Start(); while (true) { } }
public void TickAction(GameState state, Entity entity) { WindowsKeyController controller = state.Controllers[controllerIndex] as WindowsKeyController; DescriptionPlayer descr = entity.Description as DescriptionPlayer; if (descr == null) { return; } List <WallDescription> walls = state.Location.GetEntities <WallDescription>().ToList(); for (int i = descr.controllerWalkChecks.Count - 1; i >= 0; i--) { int key = descr.controllerWalkChecks[i]; if (descr.walkDuration <= 0 && controller[key].IsDown() && controller[key].Duration > 5) { descr.walkDirection = key; descr.walkDuration = DescriptionPlayer.maxWalkDuration; descr.run = controller[(int)Program.KEYS.B].IsDown(); descr.controllerWalkChecks.RemoveAt(i); descr.controllerWalkChecks.Insert(0, key); } } if (descr.walkDuration > 0) { int dist = descr.run ? 2 : 1; descr.ImageIndex = descr.walkDirection * 4 + descr.walkDuration / 8 % 4; if (descr.walkDuration % 2 == 1) { } else if (descr.walkDirection == (int)Program.KEYS.UP) { descr.ChangeCoordsDelta(0, -1); if (IsCollision(walls, descr)) { descr.walkDuration = 0; descr.ChangeCoordsDelta(0, 1); } } else if (descr.walkDirection == (int)Program.KEYS.LEFT) { descr.ChangeCoordsDelta(-1, 0); if (IsCollision(walls, descr)) { descr.walkDuration = 0; descr.ChangeCoordsDelta(1, 0); } } else if (descr.walkDirection == (int)Program.KEYS.DOWN) { descr.ChangeCoordsDelta(0, 1); if (IsCollision(walls, descr)) { descr.walkDuration = 0; descr.ChangeCoordsDelta(0, -1); } } else if (descr.walkDirection == (int)Program.KEYS.RIGHT) { descr.ChangeCoordsDelta(1, 0); if (IsCollision(walls, descr)) { descr.walkDuration = 0; descr.ChangeCoordsDelta(-1, 0); } } descr.walkDuration -= dist; } if (controller[(int)Program.KEYS.X].State == HoldState.PRESS) { oldState = descr.Serialize(); } if (controller[(int)Program.KEYS.Y].State == HoldState.PRESS) { descr.Deserialize(oldState); } if (controller[(int)Program.KEYS.RESET].State == HoldState.PRESS) { Program.Engine.Deserialize(Program.states.Peek()); } }
public static async Task Main(string[] args) { Engine = new FixedTickEngine(144); #if WinForm GameView2D view = new GameView2D(new Drawer2DSystemDrawing(), 240, 160, 4, 4, Color.FromArgb(0, Color.Magenta), 2); #endif #if Avalonia var drawer = new Drawer2DAvalonia(); GameView2D view = new GameView2D(drawer, 240, 160, 4, 4, Color.FromArgb(0, Color.Transparent)); #endif view.ScrollTop = view.Height / 2; view.ScrollBottom = view.Height / 2 - 16; view.ScrollLeft = view.Width / 2; view.ScrollRight = view.Width / 2 - 16; Engine.TickEnd(0) += view.Tick; Engine.SetView(0, view); Engine.SetLocation(0, Location.Load("GridWalkRPG.Maps.map.dat")); #if WinForm Frame = new GameFrame(new WinFormWindowBuilder(), 0, 0, 240, 160, 4, 4); #endif #if Avalonia Frame = new GameFrame( new AvaloniaWindowBuilder() //.TopMost(true) //.Decorations(Avalonia.Controls.SystemDecorations.None) .Transparency(Avalonia.Controls.WindowTransparencyLevel.Transparent) .StartupLocation(Avalonia.Controls.WindowStartupLocation.CenterScreen) .CanResize(false) .ShowInTaskBar(false), 0, 0, 240, 160, 4, 4); #endif Engine.DrawEnd(0) += Frame.DrawHandle; Frame.Start(); await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() => { Frame.SetBounds(0, 0, 2560, 1440); view.Resize(2560, 1440); }); var frame2 = new GameFrame( new AvaloniaWindowBuilder() .TopMost(true) .StartupLocation(Avalonia.Controls.WindowStartupLocation.CenterScreen), 0, 0, 240, 160, 4, 4); Engine.DrawEnd(0) += (s, v) => frame2.DrawHandle(s, v); frame2.Start(); WindowsKeyController controller = new WindowsKeyController(keymap); Engine.AddController(0, controller); Frame.Window.Hook(controller); DescriptionPlayer dp = new DescriptionPlayer(new Sprite("circle", "Sprites.circle.png", 16, 16), 48, 48); Entity player = new Entity(dp); Guid playerId = player.Id; PlayerActions pActions = new PlayerActions(Engine.GetControllerIndex(0, controller)); Engine.TickEnd(0) += (s, e) => Entity.Entities[playerId].TickAction = pActions.TickAction; player.TickAction = pActions.TickAction; Engine.AddEntity(0, player); #if Avalonia //AvaloniaWindowBuilder.MakeTransparent(Frame, true); //dp.AddMovementListener(d => AvaloniaWindowBuilder.SetWindowRegion(Frame, d.X - (Engine.View as GameView2D).ViewBounds.X, d.Y - (Engine.View as GameView2D).ViewBounds.Y, d.Width, d.Height)); var points = new AvaloniaWindowBuilder.Point[] { new AvaloniaWindowBuilder.Point { }, new AvaloniaWindowBuilder.Point { }, new AvaloniaWindowBuilder.Point { }, new AvaloniaWindowBuilder.Point { }, }; //dp.AddMovementListener(d => { // points[0].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X; // points[0].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y; // points[1].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X; // points[1].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y - 10; // points[2].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 10; // points[2].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y - 10; // points[3].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 20; // points[3].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 20; // for (int i = 0; i < points.Length; i++) // { // points[i].x *= 4; // points[i].y *= 4; // } // AvaloniaWindowBuilder.SetWindowRegion(Frame, ref points); //}); var pointarray = new AvaloniaWindowBuilder.Point[][] { new AvaloniaWindowBuilder.Point[] { new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), }, new AvaloniaWindowBuilder.Point[] { new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), new AvaloniaWindowBuilder.Point(), }, }; //dp.AddMovementListener(d => //{ // pointarray[0][0].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X; // pointarray[0][0].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y; // pointarray[0][1].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X; // pointarray[0][1].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y - 10; // pointarray[0][2].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 10; // pointarray[0][2].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y - 10; // pointarray[0][3].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 20; // pointarray[0][3].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 20; // pointarray[1][0].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 20; // pointarray[1][0].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 20; // pointarray[1][1].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 30; // pointarray[1][1].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 40; // pointarray[1][2].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 30; // pointarray[1][2].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 30; // pointarray[1][3].x = (int)d.X - (Engine.View as GameView2D).ViewBounds.X + 20; // pointarray[1][3].y = (int)d.Y - (Engine.View as GameView2D).ViewBounds.Y + 30; // for (int p = 0; p < pointarray.Length; p++) // { // for (int i = 0; i < pointarray[p].Length; i++) // { // pointarray[p][i].x *= 4; // pointarray[p][i].y *= 4; // } // } // AvaloniaWindowBuilder.SetWindowRegion(Frame, ref pointarray); //}); short prevState = AvaloniaWindowBuilder.GetKeyState(0xA1); Engine.TickEnd(0) += (s, e) => { short state = AvaloniaWindowBuilder.GetKeyState(0xA1); if (prevState != state) { Console.WriteLine(state); if (state != 0 && state != 1) { AvaloniaWindowBuilder.MakeTransparent(Frame, false); } else { AvaloniaWindowBuilder.MakeTransparent(Frame, true); } } prevState = state; }; #endif view.Follow(player.Description as Description2D); Engine.TickEnd(0) += (s, e) => view.Follow(Entity.Entities[playerId].Description as Description2D); MML mml = new MML(new string[] { ////// Good // https://www.reddit.com/r/archebards/comments/26rjdt/ocarina_of_time/ ////"r1l8<faaafaaafaaafaaaegggeggcegggeggcfaaafaaafaaafaaaegggeggcegggeggc1", ////"r1l8>fab4fab4fab>ed4c-c<bge2&edege2.fab4fab4fab>ed4c-ce<bg2&gbgde1", ////"r1l2<ffffccccffffcccc" // Very good // https://www.gaiaonline.com/guilds/viewtopic.php?page=1&t=23690909#354075091 "l16o3f8o4crcrcro3f8o4crcrcro3f8o4crcrcro3f8o4crcro3cre8o4crcrcro3e8o4crcrcro3e8o4crcrcro3e8o4crcro3c8f8o4crcrcro3f8o4crcrcro3f8o4crcrcro3f8o4crcro3cro3e8o4crcrcro3e8o4crcrcro3e8o4crcrcro3e8o4crcrc8o3drardraro2gro3gro2gro3grcro4cro3cro4cro2aro3aro2aro3aro3drardraro2gro3gro2gro3grcro4cro3cro4cro2aro3aro2aro3aro3drardraro2gro3gro2gro3grcro4cro3cro4cro2aro3aro2aro3aro3drararrrdrararrrcrbrbrrrcrbrbrrrerarrrarerarrrarerg#rg#rg#rg#rrre&er", "l16o5frarb4frarb4frarbr>erd4<b8>cr<brgre2&e8drergre2&e4frarb4frarb4frarbr>erd4<b8>crer<brg2&g8brgrdre2&e4r1r1frgra4br>crd4e8frg2&g4r1r1<f8era8grb8ar>c8<br>d8cre8drf8er<b>cr<ab1&b2r4e&e&er", "l16r1r1r1r1r1r1r1r1o4drerf4grarb4>c8<bre2&e4drerf4grarb4>c8dre2&e4<drerf4grarb4>c8<bre2&e4d8crf8erg8fra8grb8ar>c8<br>d8crefrde1&e2r4" }); //Frame.PlayTrack(new AvaloniaTrack(mml)); TileMap map = Engine.Location(0).Description as TileMap; if (map != null) { // This is a hack to make the walls spawn where tree tiles are. for (int x = 0; x < map.Width; x += 16) { for (int y = 0; y < map.Height; y += 16) { switch (map[x / map.Sprite.Width, y / map.Sprite.Height]) { case 3: case 4: case 19: case 20: Engine.Location(0).AddEntity(new Entity(new WallDescription(x, y, 16, 16))); break; } } } } watchSecond = new Stopwatch(); watchSecond.Start(); watchTickTime = new Stopwatch(); Engine.TickEnd(0) += TickInfo; Engine.TickStart(0) += TickTimer; Engine.TickEnd(0) += TickTimer; Engine.TickEnd(0) += (s, e) => { states.Enqueue(Engine.Serialize()); if (states.Count > 60) { states.Dequeue(); } }; Engine.Start(); while (true) { await Task.Delay(TimeSpan.FromSeconds(1)); } }