Opravena chyba se špatně převedenými hitboxy UI elementů. Opravena chyba s měněním screenu, kdy stará zůstala aktivní. Přidány elementy pro překlikávání kamer.

This commit is contained in:
Perry 2026-01-29 19:37:40 +01:00
parent 2cd215cc33
commit 8a3267cc4b
8 changed files with 56 additions and 19 deletions

View file

@ -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;

View file

@ -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)]);
}
}

View file

@ -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;

View file

@ -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<TextureRegion> 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);
}
}

View file

@ -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);
}
}