From 8a3267cc4b178c5fc2e70f4ff7af7c0f9c7319f4 Mon Sep 17 00:00:00 2001 From: Perry Date: Thu, 29 Jan 2026 19:37:40 +0100 Subject: [PATCH] =?UTF-8?q?Opravena=20chyba=20se=20=C5=A1patn=C4=9B=20p?= =?UTF-8?q?=C5=99eveden=C3=BDmi=20hitboxy=20UI=20element=C5=AF.=20Opravena?= =?UTF-8?q?=20chyba=20s=20m=C4=9Bn=C4=9Bn=C3=ADm=20screenu,=20kdy=20star?= =?UTF-8?q?=C3=A1=20z=C5=AFstala=20aktivn=C3=AD.=20P=C5=99id=C3=A1ny=20ele?= =?UTF-8?q?menty=20pro=20p=C5=99eklik=C3=A1v=C3=A1n=C3=AD=20kamer.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FNAF_Clone/Client.cs | 4 ++-- FNAF_Clone/CommandManager.cs | 6 +++++- FNAF_Clone/GUI/Screen.cs | 6 ++++++ FNAF_Clone/GUI/UIElement.cs | 22 +++++++++++++++++----- FNAF_Clone/GUI/UIManager.cs | 29 ++++++++++++++++++++++------- FNAF_Server/CommandProcessor.cs | 4 ++-- PacketLib/GameEvent.cs | 2 +- PacketLib/PlayerCommand.cs | 2 +- 8 files changed, 56 insertions(+), 19 deletions(-) diff --git a/FNAF_Clone/Client.cs b/FNAF_Clone/Client.cs index 2620ec0..ac6ae8c 100644 --- a/FNAF_Clone/Client.cs +++ b/FNAF_Clone/Client.cs @@ -72,7 +72,7 @@ public class Client { client.PollEvents(); } - public static void OnPlayerUpdate(UpdatePlayerPacket packet) { + public static void OnPlayerUpdate(UpdatePlayerPacket packet) { // TODO: move this to a separate class //Player.state = Player.state.pid == 0 ? packet.stateP1 : packet.stateP2; foreach (var e in packet.events){ @@ -87,7 +87,7 @@ public class Client { if (Player.state.pid == e.Args[0]){ Player.state.camera = e.Args[1]; } - Console.WriteLine($"E: player {e.Args[0]} switched to camera {e.Args[1]}"); + Console.WriteLine($"E: player {e.Args[0]} switched to camera {e.Args[1]}-{e.Args[2]}"); break; case 3: // toggle cam Player.state.monitorUp = e.Args[1] == 1; diff --git a/FNAF_Clone/CommandManager.cs b/FNAF_Clone/CommandManager.cs index 2f77446..f9ca51e 100644 --- a/FNAF_Clone/CommandManager.cs +++ b/FNAF_Clone/CommandManager.cs @@ -25,7 +25,7 @@ public class CommandManager { private static void SendToggleCamera() { Client.SendCommands([PlayerCommand.TOGGLE_MONITOR()]); } - + private static void ToggleDoorLeft() => SendToggleDoor(0); private static void ToggleDoorCentre() => SendToggleDoor(1); private static void ToggleDoorRight() => SendToggleDoor(2); @@ -37,4 +37,8 @@ public class CommandManager { } Client.SendCommands([PlayerCommand.TOGGLE_DOOR_OFFICE(id)]); } + + public static void SendChangeCamera(int idx, int idy) { + Client.SendCommands([PlayerCommand.SWITCH_CAM(idx, idy)]); + } } \ No newline at end of file diff --git a/FNAF_Clone/GUI/Screen.cs b/FNAF_Clone/GUI/Screen.cs index 09172d7..69d1b12 100644 --- a/FNAF_Clone/GUI/Screen.cs +++ b/FNAF_Clone/GUI/Screen.cs @@ -22,6 +22,9 @@ public class Screen { } public static void SetScreen(string id) { + if (CurrentScreen != null){ + CurrentScreen.Active = false; + } CurrentScreen = Screens[id]; CurrentScreen.Active = true; } @@ -62,6 +65,9 @@ public class Screen { } private void ProcessMouseInput(MouseState mouseState) { + if (!Active){ + return; + } foreach (var element in elements.Values){ if (!element.Pressable) continue; diff --git a/FNAF_Clone/GUI/UIElement.cs b/FNAF_Clone/GUI/UIElement.cs index 1160e38..23e4181 100644 --- a/FNAF_Clone/GUI/UIElement.cs +++ b/FNAF_Clone/GUI/UIElement.cs @@ -12,8 +12,10 @@ public class UIElement { public bool Active { get; set; } = false; public bool Pressable { get; set; } = false; + public bool Visible { get; set; } = true; private (Point, Point) bounds; // TODO: Change this to support non-rectangular hitboxes + private (Point, Point) screenSpaceBounds; private List textures = new(); private int currentTextureId = 0; @@ -27,9 +29,10 @@ public class UIElement { LoadPixelScaleMultiplier(); } } - private float pixelScaleMultiplier = 1; + private int pixelScaleMultiplier = 1; private void LoadPixelScaleMultiplier() { - pixelScaleMultiplier = UIManager.GlobalPixelMultiplier * _scaleMultiplier; + pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense + screenSpaceBounds = (bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), bounds.Item2.MultiplyByScalar(pixelScaleMultiplier)); } public UIElement(TextureRegion texture, Point position) { @@ -42,6 +45,12 @@ public class UIElement { bounds = (position, position + new Point(textures[0].Width, textures[0].Height)); LoadPixelScaleMultiplier(); } + + public UIElement(Point corner1, Point corner2) { + bounds = (corner1, corner2); + Visible = false; + LoadPixelScaleMultiplier(); + } public void SetTexture(int textureId) { @@ -57,8 +66,8 @@ public class UIElement { } public bool IsWithinBounds(Point pos) { - return pos.X >= Math.Min(bounds.Item1.X, bounds.Item2.X) && pos.X <= Math.Max(bounds.Item1.X, bounds.Item2.X) && - pos.Y >= Math.Min(bounds.Item1.Y, bounds.Item2.Y) && pos.Y <= Math.Max(bounds.Item1.Y, bounds.Item2.Y); + return pos.X >= Math.Min(screenSpaceBounds.Item1.X, screenSpaceBounds.Item2.X) && pos.X <= Math.Max(screenSpaceBounds.Item1.X, screenSpaceBounds.Item2.X) && + pos.Y >= Math.Min(screenSpaceBounds.Item1.Y, screenSpaceBounds.Item2.Y) && pos.Y <= Math.Max(screenSpaceBounds.Item1.Y, screenSpaceBounds.Item2.Y); } public Action OnMousePress{ get; set; } @@ -70,7 +79,10 @@ public class UIElement { public virtual void OnMouseHold() { } public void Draw(SpriteBatch spriteBatch) { - textures[currentTextureId].Draw(spriteBatch, bounds.Item1.ToVector2(), Color.White, 0, Vector2.Zero, pixelScaleMultiplier, SpriteEffects.None, 0); + if (!Visible){ + return; + } + textures[currentTextureId].Draw(spriteBatch, screenSpaceBounds.Item1.ToVector2(), Color.White, 0, Vector2.Zero, pixelScaleMultiplier, SpriteEffects.None, 0); // texture.Draw(spriteBatch, bounds.Item1.ToVector2(), Color.White); } } \ No newline at end of file diff --git a/FNAF_Clone/GUI/UIManager.cs b/FNAF_Clone/GUI/UIManager.cs index eeede6a..e895135 100644 --- a/FNAF_Clone/GUI/UIManager.cs +++ b/FNAF_Clone/GUI/UIManager.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using MonoGameLibrary; using MonoGameLibrary.Graphics; @@ -29,19 +30,31 @@ public class UIManager { Screen.AddScreens([officeScreen, monitorScreen]); Screen.SetScreen(ScreenTypes.OFFICE); + + officeScreen.AddElement("office_left", new UIElement([officeAtlas[3], officeAtlas[0]], Point.Zero)); + officeScreen.AddElement("office_centre", new UIElement([officeAtlas[4], officeAtlas[1]], new Point(200, 0))); + officeScreen.AddElement("office_right", new UIElement([officeAtlas[5], officeAtlas[2]], new Point(440, 0))); + // officeScreen.AddElement("test", // new UIElement(testAtlas[0], Point.Zero) // {Pressable = true, OnMousePress = () => Console.WriteLine("Pressed!")} // ); - officeScreen.AddElement("office_left", new UIElement([officeAtlas[3], officeAtlas[0]], Point.Zero)); - officeScreen.AddElement("office_centre", new UIElement([officeAtlas[4], officeAtlas[1]], new Point(200, 0).MultiplyByScalar(GlobalPixelMultiplier))); - officeScreen.AddElement("office_right", new UIElement([officeAtlas[5], officeAtlas[2]], new Point(440, 0).MultiplyByScalar(GlobalPixelMultiplier))); - monitorScreen.AddElement("screen", new UIElement(monitorAtlas[0], Point.Zero)); - monitorScreen.AddElement("view-frame", new UIElement(monitorAtlas[1], new Point(62, 55).MultiplyByScalar(GlobalPixelMultiplier))); - monitorScreen.AddElement("map-frame", new UIElement(monitorAtlas[2], new Point(334, 135).MultiplyByScalar(GlobalPixelMultiplier))); - monitorScreen.AddElement("map", new UIElement(monitorAtlas[3], new Point(334, 135).MultiplyByScalar(GlobalPixelMultiplier))); + monitorScreen.AddElement("view-frame", new UIElement(monitorAtlas[1], new Point(62, 55))); + monitorScreen.AddElement("map-frame", new UIElement(monitorAtlas[2], new Point(334, 135))); + monitorScreen.AddElement("map", new UIElement(monitorAtlas[3], new Point(334, 135))); + + for (int i = 0; i < 5; i++){ + for (int j = 0; j < 5; j++){ + int i1 = i; + int j1 = j; + monitorScreen.AddElement($"room{i}-{j}", new UIElement(new Point(336 + (32 * i), 144 + (32 * j)), new Point(367 + (32 * i), 175 + (32 * j))) + {Pressable = true, OnMousePress = (() => CommandManager.SendChangeCamera(i1, j1))}); + } + } + + } public static void ChangeDoorState(int id, bool state) { @@ -61,4 +74,6 @@ public class UIManager { public static void ChangeMonitorState(bool state) { Screen.SetScreen(state ? ScreenTypes.CAMERAS : ScreenTypes.OFFICE); } + + } \ No newline at end of file diff --git a/FNAF_Server/CommandProcessor.cs b/FNAF_Server/CommandProcessor.cs index e59ca0e..1a0d48c 100644 --- a/FNAF_Server/CommandProcessor.cs +++ b/FNAF_Server/CommandProcessor.cs @@ -9,8 +9,8 @@ public class CommandProcessor { foreach (var playerCommand in commands){ switch (playerCommand.ID){ case 0: - Console.WriteLine($"C: Player {pid} switched to camera {playerCommand.Args[0]}"); - Server.SendUpdateToAll([GameEvent.SWITCH_CAM(pid, playerCommand.Args[0])]); + Console.WriteLine($"C: Player {pid} switched to camera {playerCommand.Args[0]}-{playerCommand.Args[1]}"); + Server.SendUpdateToAll([GameEvent.SWITCH_CAM(pid, playerCommand.Args[0], playerCommand.Args[1])]); break; case 1: bool monitorState = !currentPlayer.state.monitorUp; diff --git a/PacketLib/GameEvent.cs b/PacketLib/GameEvent.cs index 55340c9..10c8dbf 100644 --- a/PacketLib/GameEvent.cs +++ b/PacketLib/GameEvent.cs @@ -7,7 +7,7 @@ namespace PacketLib; public struct GameEvent : INetSerializable{ public static GameEvent PLAYER_JOIN(int pid) => new(){ID = 0, Args = [pid] }; public static GameEvent PLAYER_LEAVE(int pid) => new(){ID = 1, Args = [pid] }; - public static GameEvent SWITCH_CAM(int pid, int camId) => new(){ID = 2, Args = [pid, camId] }; + public static GameEvent SWITCH_CAM(int pid, int idx, int idy) => new(){ID = 2, Args = [pid, idx, idy] }; public static GameEvent TOGGLE_MONITOR(int pid, bool state) => new(){ID = 3, Args = [pid, state ? 1 : 0]}; public static GameEvent TOGGLE_DOOR_OFFICE(int pid, int doorId, bool state) => new(){ID = 4, Args = [pid, doorId, state ? 1 : 0]}; diff --git a/PacketLib/PlayerCommand.cs b/PacketLib/PlayerCommand.cs index 835c051..3e7742b 100644 --- a/PacketLib/PlayerCommand.cs +++ b/PacketLib/PlayerCommand.cs @@ -3,7 +3,7 @@ using LiteNetLib.Utils; namespace PacketLib; public struct PlayerCommand : INetSerializable { - public static PlayerCommand SWITCH_CAM(int camId) => new(){ID = 0, Args = [camId] }; + public static PlayerCommand SWITCH_CAM(int idx, int idy) => new(){ID = 0, Args = [idx, idy] }; public static PlayerCommand TOGGLE_MONITOR() => new(){ID = 1, Args = []}; public static PlayerCommand TOGGLE_DOOR_OFFICE(int doorId) => new(){ID = 2, Args = [doorId]}; public int ID{ get; set; }