From 70b5debb2279682457ac9bf71690e694da8836ec Mon Sep 17 00:00:00 2001 From: Perry Date: Mon, 16 Feb 2026 21:48:59 +0100 Subject: [PATCH] =?UTF-8?q?Remote=20dve=C5=99e=20se=20renderuj=C3=AD=20na?= =?UTF-8?q?=20map=C4=9B.=20Opravena=20chyba=20v=20GlobalMapTile.CoordsToId?= =?UTF-8?q?,=20kter=C3=A1=20zp=C5=AFsobovala=20desynchronizaci=20id=20m?= =?UTF-8?q?=C3=ADstnost=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FNAF_Clone.sln.DotSettings.user | 1 + FNAF_Clone/Client.cs | 10 ++++++--- FNAF_Clone/GUI/UIElement.cs | 10 ++++----- FNAF_Clone/GUI/UIManager.cs | 36 +++++++++++++++++++++--------- FNAF_Clone/GameMain.cs | 2 +- FNAF_Clone/Map/ClientMapManager.cs | 15 +++++++++---- GlobalClassLib/GlobalMapTile.cs | 11 ++++++--- 7 files changed, 58 insertions(+), 27 deletions(-) diff --git a/FNAF_Clone.sln.DotSettings.user b/FNAF_Clone.sln.DotSettings.user index 7cf1b1f..6fd933d 100644 --- a/FNAF_Clone.sln.DotSettings.user +++ b/FNAF_Clone.sln.DotSettings.user @@ -2,6 +2,7 @@ True ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded \ No newline at end of file diff --git a/FNAF_Clone/Client.cs b/FNAF_Clone/Client.cs index f7e3999..30957df 100644 --- a/FNAF_Clone/Client.cs +++ b/FNAF_Clone/Client.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Net; using System.Net.Sockets; using FNAF_Clone.GUI; @@ -71,11 +72,14 @@ public class Client { } private static void OnMapInit(MapInitPacket packet) { - (int id1, int id2, ConnectorType type)[] connectors = new (int id1, int id2, ConnectorType type)[packet.Connectors.Length / 3]; + (int id1, int id2, ConnectorType type)[] connectorsData = new (int id1, int id2, ConnectorType type)[packet.Connectors.Length / 3]; for (int i = 0; i < packet.Connectors.Length / 3; i++){ - connectors[i] = (packet.Connectors[i * 3], packet.Connectors[i * 3 + 1], (ConnectorType)packet.Connectors[i * 3 + 2]); + connectorsData[i] = (packet.Connectors[i * 3], packet.Connectors[i * 3 + 1], (ConnectorType)packet.Connectors[i * 3 + 2]); } - ClientMapManager.InitMap(connectors); + ClientMapManager.InitMap(); + TileConnectorProjection[] connectors = connectorsData.Select(c => new TileConnectorProjection(ClientMapManager.Get(c.id1), ClientMapManager.Get(c.id2), c.type)).ToArray(); + ClientMapManager.InitConnectors(connectors); + UIManager.SpawnDoors(connectors); } public static void Update() { diff --git a/FNAF_Clone/GUI/UIElement.cs b/FNAF_Clone/GUI/UIElement.cs index 23e4181..d4d9d63 100644 --- a/FNAF_Clone/GUI/UIElement.cs +++ b/FNAF_Clone/GUI/UIElement.cs @@ -14,7 +14,7 @@ public class UIElement { public bool Pressable { get; set; } = false; public bool Visible { get; set; } = true; - private (Point, Point) bounds; // TODO: Change this to support non-rectangular hitboxes + public (Point, Point) Bounds{ get; private set; } // TODO: Change this to support non-rectangular hitboxes private (Point, Point) screenSpaceBounds; private List textures = new(); private int currentTextureId = 0; @@ -32,22 +32,22 @@ public class UIElement { private int pixelScaleMultiplier = 1; private void LoadPixelScaleMultiplier() { pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense - screenSpaceBounds = (bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), bounds.Item2.MultiplyByScalar(pixelScaleMultiplier)); + screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier)); } public UIElement(TextureRegion texture, Point position) { textures.Add(texture); - bounds = (position, position + new Point(texture.Width, texture.Height)); + Bounds = (position, position + new Point(texture.Width, texture.Height)); LoadPixelScaleMultiplier(); } public UIElement(TextureRegion[] textures, Point position) { this.textures.AddRange(textures); - bounds = (position, position + new Point(textures[0].Width, textures[0].Height)); + Bounds = (position, position + new Point(textures[0].Width, textures[0].Height)); LoadPixelScaleMultiplier(); } public UIElement(Point corner1, Point corner2) { - bounds = (corner1, corner2); + Bounds = (corner1, corner2); Visible = false; LoadPixelScaleMultiplier(); } diff --git a/FNAF_Clone/GUI/UIManager.cs b/FNAF_Clone/GUI/UIManager.cs index c094c4b..60f486d 100644 --- a/FNAF_Clone/GUI/UIManager.cs +++ b/FNAF_Clone/GUI/UIManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using FNAF_Clone.Map; using GlobalClassLib; using Microsoft.Xna.Framework; using MonoGameLibrary; @@ -22,6 +23,7 @@ public class UIManager { private static TextureAtlas monitorAtlas; public static int GlobalPixelMultiplier{ get; private set; } + private Dictionary<(int, int), UIElement> doorElements = new(); public static void InitUI() { GlobalPixelMultiplier = Core.graphicsDevice.Viewport.Height / 360; @@ -47,10 +49,10 @@ public class UIManager { monitorScreen.AddElement("map-frame", new UIElement(monitorAtlas[2], new Point(334, 135))); monitorScreen.AddElement("map", new UIElement(monitorAtlas[3], new Point(334, 135))); - Dictionary<(int,int),string> doorPositions = new(){ - {(0, 0),"2-5"},{ (1, 0), "2-4" }, { (1, 1), "2-3" }, { (3, 0), "2-2" }, { (3, 1), "2-1" }, { (4, 0), "2-0" }, // TODO: generate this dynamically from server map info - {(0, 3),"1-0"},{ (1, 3), "1-1" }, { (1, 2), "1-2" }, { (3, 3), "1-3" }, { (3, 2), "1-4" }, { (4, 3), "1-5" } - }; + // Dictionary<(int,int),string> doorPositions = new(){ + // {(0, 0),"2-5"},{ (1, 0), "2-4" }, { (1, 1), "2-3" }, { (3, 0), "2-2" }, { (3, 1), "2-1" }, { (4, 0), "2-0" }, // TODO: generate this dynamically from server map info + // {(0, 3),"1-0"},{ (1, 3), "1-1" }, { (1, 2), "1-2" }, { (3, 3), "1-3" }, { (3, 2), "1-4" }, { (4, 3), "1-5" } + // }; for (int i = 0; i < 5; i++){ // NOTE: this loop does y in reverse, y labels are inverted to match server for (int j = 0; j < 5; j++){ @@ -58,16 +60,28 @@ public class UIManager { int j1 = j; Point point1 = new Point(336 + (32 * i), 144 + (32 * j)); Point point2 = new Point(367 + (32 * i), 175 + (32 * j)); - monitorScreen.AddElement($"room{i}-{4 - j}", new UIElement(point1, point2) + monitorScreen.AddElement($"room{MapTileProjection.CoordsToId(i, 4 - j)}", new UIElement(point1, point2) {Pressable = true, OnMousePress = (() => CommandManager.SendChangeCamera(i1, 4 - j1))}); - if (doorPositions.ContainsKey((i, j))){ - monitorScreen.AddElement("door"+doorPositions[(i, j)], new UIElement([monitorAtlas[5], monitorAtlas[6]], point1)); - } + // + // if (doorPositions.ContainsKey((i, j))){ + // monitorScreen.AddElement("door"+doorPositions[(i, j)], new UIElement([monitorAtlas[5], monitorAtlas[6]], point1)); + // } + } + } + } + + public static void SpawnDoors(TileConnectorProjection[] doors) { + foreach (var door in doors){ + if(door.Type != ConnectorType.DOOR_REMOTE) continue; + + (int x, int y) dpos = (Math.Abs(door.Tiles.tile1.GridPosition.x - door.Tiles.tile2.GridPosition.x), Math.Abs(door.Tiles.tile1.GridPosition.y - door.Tiles.tile2.GridPosition.y)); + + if (dpos.y == 1){ + int targetId = door.Tiles.tile1.GridPosition.y > door.Tiles.tile2.GridPosition.y ? door.Tiles.tile1.Id : door.Tiles.tile2.Id; + UIElement tile = monitorScreen["room"+targetId]; + monitorScreen.AddElement("door"+targetId+"-"+(targetId == door.Tiles.tile1.Id ? door.Tiles.tile2.Id : door.Tiles.tile1.Id), new UIElement([monitorAtlas[5], monitorAtlas[6]], tile.Bounds.Item1)); } } - - - } public static void ChangeDoorState(Direction dir, bool state) { diff --git a/FNAF_Clone/GameMain.cs b/FNAF_Clone/GameMain.cs index e643bb8..bb1d240 100644 --- a/FNAF_Clone/GameMain.cs +++ b/FNAF_Clone/GameMain.cs @@ -10,7 +10,7 @@ using MonoGameLibrary.Input; namespace FNAF_Clone; -public class GameMain() : Core("fnafkooo", 1920, 1080, false) { +public class GameMain() : Core("fnafkooo", 1280, 720, false) { // private GraphicsDeviceManager _graphics; // private SpriteBatch _spriteBatch; diff --git a/FNAF_Clone/Map/ClientMapManager.cs b/FNAF_Clone/Map/ClientMapManager.cs index 124b103..eb4c962 100644 --- a/FNAF_Clone/Map/ClientMapManager.cs +++ b/FNAF_Clone/Map/ClientMapManager.cs @@ -5,7 +5,8 @@ namespace FNAF_Clone.Map; public class ClientMapManager { private static MapTileProjection[,] map = new MapTileProjection[5, 5]; private static MapTileProjection Get((int x, int y) coords) => map[coords.x, coords.y]; - public static void InitMap((int id1, int id2, ConnectorType type)[] connectors) { + public static MapTileProjection Get(int tileId) => Get(MapTileProjection.IdToCoords(tileId)); + public static void InitMap() { for (int i = 0; i < 5; i++){ for (int j = 0; j < 2; j++){ map[i, j] = new MapTileProjection(MapTileProjection.CoordsToId(i, j)); // TODO: implement ownership @@ -17,12 +18,18 @@ public class ClientMapManager { } } + + } + + public static void InitConnectors(TileConnectorProjection[] connectors) { foreach (var con in connectors){ - (int x, int y) coords1 = MapTileProjection.IdToCoords(con.id1); - (int x, int y) coords2 = MapTileProjection.IdToCoords(con.id2); - map[coords1.x, coords1.y].AddConnector(new TileConnectorProjection(map[coords2.x, coords2.y], con.type)); + // (int x, int y) coords1 = MapTileProjection.IdToCoords(con.Tiles.tile1.Id); + // (int x, int y) coords2 = MapTileProjection.IdToCoords(con.Tiles.tile2.Id); + map[con.Tiles.tile1.GridPosition.x, con.Tiles.tile1.GridPosition.y].AddConnector(con); } } + + public static TileConnectorProjection[] GetConnectors(int tileId) => Get(MapTileProjection.IdToCoords(tileId)).GetAllConnectors(); } \ No newline at end of file diff --git a/GlobalClassLib/GlobalMapTile.cs b/GlobalClassLib/GlobalMapTile.cs index 0c57f90..a3911dc 100644 --- a/GlobalClassLib/GlobalMapTile.cs +++ b/GlobalClassLib/GlobalMapTile.cs @@ -3,13 +3,16 @@ namespace GlobalClassLib; public abstract class GlobalMapTile where TCon : GlobalTileConnector where TTile : GlobalMapTile { // TTile should be the inheriting class public int Id { get; private set; } public bool Lit { get; set; } = false; + public (int x, int y) GridPosition { get; private set; } private List connectors = new(); public GlobalMapTile(int id) { Id = id; + GridPosition = IdToCoords(id); } public void AddConnector(TCon connector) { // tile1 is ignored when provided + connector = connector.Clone(); connector.Tiles.tile1 = (TTile)this; connectors.Add(connector); connector.Tiles.tile2._AddConnectorNoRepeat(connector.Clone()); @@ -36,8 +39,10 @@ public abstract class GlobalMapTile where TCon : GlobalTileConnecto return null; } - public TCon[] GetAllConnectors() => connectors.ToArray(); + public TCon[] GetAllConnectors() => connectors.ToArray(); - public static int CoordsToId(int x, int y) => x * 5 + y; - public static (int, int) IdToCoords(int id) => (id % 5, id / 5); + + public const int ID_X_OFFSET = 5; // map grid height + public static int CoordsToId(int x, int y) => x * ID_X_OFFSET + y; + public static (int, int) IdToCoords(int id) => (id / ID_X_OFFSET, id % ID_X_OFFSET); } \ No newline at end of file