Projekt přejmenován. Neko nastaven na výchozí pozici

This commit is contained in:
Perry 2026-03-22 18:31:05 +01:00
parent 1a27dd6fab
commit ceac37920b
104 changed files with 873 additions and 208 deletions

View file

@ -0,0 +1,33 @@
using System.Linq;
using Microsoft.Xna.Framework;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class EnemyUIElement : UIElement {
private int unlitTexturesId;
private bool currentlyLit = true;
public EnemyUIElement(TextureRegion litTexture, TextureRegion unlitTexture, Point position, int drawPriority = 0) : base([litTexture, unlitTexture], position, drawPriority) {
unlitTexturesId = 1;
}
public EnemyUIElement(TextureRegion[] litTextures, TextureRegion[] unlitTextures, Point position, int drawPriority = 0) : base(litTextures.Concat(unlitTextures).ToArray(), position, drawPriority) {
unlitTexturesId = litTextures.Length;
}
public void SetTexture(bool lit, int id) {
currentlyLit = lit;
base.SetTexture(lit ? id : id + unlitTexturesId);
}
public override void SetTexture(int id) {
base.SetTexture(currentlyLit ? id : id + unlitTexturesId);
}
public void SetTexture(bool lit) {
if(lit == currentlyLit) return;
currentlyLit = lit;
SetTexture(lit ? currentTextureId - unlitTexturesId : currentTextureId);
}
}

View file

@ -0,0 +1,59 @@
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class JumpscareUIElement : UIElement {
private int twitchHorizontal;
private int twitchVertical;
private Point positionDefault;
private Random random;
private float defaultScaleMultiplier;
private float twitchScale;
private bool playing = false;
private Stopwatch stopwatch = new();
private int duration;
public JumpscareUIElement(TextureRegion texture, Point positionDefault, int twitchHorizontal, int twitchVertical, float defaultScaleMultiplier, float twitchScale, int durationMs = 2000, Action afterStop = null) : base(texture, positionDefault) {
this.twitchHorizontal = twitchHorizontal;
this.twitchVertical = twitchVertical;
this.positionDefault = positionDefault;
random = new Random();
this.defaultScaleMultiplier = defaultScaleMultiplier;
ScaleMultiplier = defaultScaleMultiplier;
this.twitchScale = twitchScale;
duration = durationMs;
Active = false;
Visible = false;
AfterStop = afterStop;
}
// public JumpscareUIElement(UIElement element) : base(element.GetTextures(), element.Bounds.Item1) {}
public void Play() {
playing = true;
Active = true;
Visible = true;
stopwatch.Start();
}
public override void Update() {
if (!playing) return;
SetPosition(new(positionDefault.X + random.Next(-twitchHorizontal, twitchHorizontal), positionDefault.Y + random.Next(-twitchVertical, twitchVertical)));
if (stopwatch.ElapsedMilliseconds >= duration){
playing = false;
Active = false;
Visible = false;
if (AfterStop != null){
AfterStop();
}
}
// ScaleMultiplier = defaultScaleMultiplier + (float)(random.NextDouble() * twitchScale * new[]{-1, 1}[random.Next(2)]);
}
private Action AfterStop;
}

View file

@ -0,0 +1,37 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ONDClient.GUI;
public class LoadingUIElement : TextUIElement {
private string expectedEndpoint;
private Client.ConnectionState lastState = Client.ConnectionState.IDLE;
public LoadingUIElement(Point corner1, SpriteFont font, string expectedEndpoint, Alignment alignment = Alignment.CENTER, bool autoBounds = true) : base(corner1, font, alignment, autoBounds) {
this.expectedEndpoint = expectedEndpoint;
Active = true;
// Color = Color.LightGray;
}
public override void Update() {
if(lastState == Client.State) return;
lastState = Client.State;
switch (Client.State){
case Client.ConnectionState.CONNECTING:
Text = "Connecting to " + expectedEndpoint;
break;
case Client.ConnectionState.CONNECTED:
Text = "Connected to " + expectedEndpoint;
break;
case Client.ConnectionState.ACCEPTED:
Text = "Waiting for opponent...";
break;
case Client.ConnectionState.GAME_STARTING:
Text = "Opponent: " + Client.Opponent.username;
Color = Color.White;
// ScaleMultiplier = 1.5f;
break;
}
}
}

View file

@ -0,0 +1,48 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class MenuInputField : UIElement {
private TextUIElement labelElement;
private TextBoxUIElement textBoxElement;
public MenuInputField(SpriteFont font, Point position, string label, string defaultValue = "") : base(position, position) {
labelElement =
new TextUIElement(position, font){Text = label, Color = Color.Gray};
textBoxElement =
new TextBoxUIElement(font,
new(labelElement.Bounds.Item1.X + (int)labelElement.Measure().X, labelElement.Bounds.Item1.Y),
new(640, labelElement.Bounds.Item2.Y + (int)labelElement.Measure().Y));
textBoxElement.OnFocused = () => {
textBoxElement.Color = Color.LightGreen;
labelElement.Color = Color.DarkGreen;
};
textBoxElement.OnUnfocused = () => {
textBoxElement.Color = Color.White;
labelElement.Color = Color.Gray;
};
Bounds = (labelElement.Bounds.Item1, textBoxElement.Bounds.Item2);
Pressable = true;
OnMousePress = textBoxElement.OnMousePress;
textBoxElement.Text = defaultValue;
}
public string Text{
get => textBoxElement.Text;
set => textBoxElement.Text = value;
}
public override void Draw(SpriteBatch spriteBatch) {
labelElement.Draw(spriteBatch);
textBoxElement.Draw(spriteBatch);
}
public override void Update() {
base.Update();
labelElement.Update();
textBoxElement.Update();
}
}

View file

@ -0,0 +1,9 @@
using Microsoft.Xna.Framework;
namespace ONDClient.GUI;
public static class PointExtensions {
public static Point MultiplyByScalar(this Point point, int scalar) => new(point.X * scalar, point.Y * scalar);
public static Point MultiplyByScalar(this Point point, float scalar) => new((int)(point.X * scalar), (int)(point.Y * scalar));
}

View file

@ -0,0 +1,26 @@
using GlobalClassLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ONDClient.GUI;
public class PowerIndicator : TextUIElement {
private string label;
private ClientPlayer player;
private int lastPowerValue;
public PowerIndicator(Point corner1, SpriteFont font, ClientPlayer player, string label, Alignment alignment = Alignment.LEFT) : base(corner1, font, alignment, autoBounds:true) {
this.player = player;
this.label = label;
lastPowerValue = player.state.power;
Text = GetText();
}
public override void Update() {
if (player.state.power == lastPowerValue) return;
lastPowerValue = player.state.power;
Text = GetText();
}
private string GetText() => $"{label}{(int)(((float)player.state.power / Power.MAX_POWER_VALUE) * 100)}";
}

155
ONDClient/GUI/Screen.cs Normal file
View file

@ -0,0 +1,155 @@
using System.Collections.Generic;
using Microsoft.VisualBasic.CompilerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;
public class Screen {
public static Dictionary<string, Screen> Screens = new();
public static Screen CurrentScreen{ get; private set; } = Empty;
public static Screen OverlayScreen{ get; private set; } = Empty;
public static void AddScreens(Screen[] screens) {
foreach (var screen in screens){
Screens.Add(screen.Label, screen);
}
}
public static void AddScreen(string id, Screen screen, bool activate = false) {
Screens.Add(id, screen);
if (activate) SetScreen(id);
}
public static void SetScreen(string id) {
if (CurrentScreen.temporary){
Screens.Remove(CurrentScreen.Label);
}
CurrentScreen.Active = false;
CurrentScreen = Screens[id];
CurrentScreen.Active = true;
}
public static void RemoveScreen(string id) {
Screens.Remove(id);
}
public static void UpdateAll() {
foreach (var screen in Screens.Values){
if (!screen.Active) continue;
screen.Update();
}
}
public static Screen Empty => new(""){temporary = true};
public static void SetOverlayScreen(string id) {
if (OverlayScreen.temporary){
Screens.Remove(OverlayScreen.Label);
}
OverlayScreen.Active = false;
OverlayScreen = Screens[id];
OverlayScreen.Active = true;
}
public static void DisableOverlay() {
OverlayScreen = Empty;
}
public static void DrawCurrentAndOverlay(SpriteBatch spriteBatch) {
CurrentScreen.Draw(spriteBatch);
OverlayScreen.Draw(spriteBatch);
}
public string Label{ get; }
private Dictionary<string, UIElement> elements = new();
private List<UIElement> elementsInDrawOrder = new();
public bool Active { get; private set; } = false;
private InputListenerHook mouseInputHook = new(true);
private bool temporary = false;
private List<string> temporaryElements = new();
public Screen(string label) {
Label = label;
InputManager.AddListener(InputManager.MouseButton.LEFT, () => ProcessMouseInput(InputManager.MouseState), InputTiming.PRESS, mouseInputHook);
}
public Screen(string label, Dictionary<string, UIElement> elements) {
this.elements = elements;
Label = label;
}
public UIElement this[string id] => elements[id];
public UIElement TryGetElement(string id) => elements.TryGetValue(id, out var val) ? val : null;
public UIElement AddElement(string id, UIElement element, bool temporary = false) {
elements.Add(id, element);
int insertIndex = elementsInDrawOrder.FindLastIndex(e => e.DrawPriority == element.DrawPriority);
if (insertIndex == -1){
elementsInDrawOrder.Add(element);
}
else{
elementsInDrawOrder.Insert(insertIndex + 1, element);
}
if (temporary){
temporaryElements.Add(id);
}
return element;
}
public void RemoveElement(string id) {
if (!elements.ContainsKey(id)) return;
elements.Remove(id, out var element);
elementsInDrawOrder.RemoveAll(e => e == element);
}
public void RemoveTemporary() {
temporaryElements.ForEach(RemoveElement);
temporaryElements.Clear();
}
public void SetActive(bool active) {
Active = active;
if (Active == active) return;
foreach (var keyValuePair in elements){
keyValuePair.Value.Active = Active;
}
}
private void ProcessMouseInput(MouseState mouseState) {
if (!Active){
return;
}
foreach (var element in elements.Values){
if (!element.Pressable) continue;
if (element.IsWithinBounds(mouseState.Position)){
element.OnMousePress(); // TODO: differentiate between press, hold and release events
}
}
}
public void Update() {
foreach (var element in elements.Values){
if (!element.Active) continue;
element.Update();
}
}
public void Draw(SpriteBatch spriteBatch) {
foreach (var val in elementsInDrawOrder){
val.Draw(spriteBatch);
}
}
}

View file

@ -0,0 +1,42 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;
public class TextBoxUIElement : TextUIElement {
public bool Focused{ get; set; } = false;
public Action OnFocused{ get; set; } = () => { };
public Action OnUnfocused{ get; set; } = () => { };
public TextBoxUIElement(SpriteFont font, Point corner1, Point corner2) : base(corner1, corner2, font) {
Core.Instance.Window.TextInput += TextInputHandler;
InputManager.AddListener(InputManager.MouseButton.LEFT, () => {
if (!IsWithinBounds(InputManager.MouseState.Position)){
Focused = false;
OnUnfocused();
}
},
InputTiming.PRESS, new InputListenerHook(true));
Pressable = true;
OnMousePress = () => {
Focused = true;
OnFocused();
};
}
public void TextInputHandler(object sender, TextInputEventArgs e) {
if (!Focused) return;
if (e.Character == '\b') {
if (Text.Length > 0) Text = Text[..^1];
return;
}
if(Font.Characters.Contains(e.Character))
Text += e.Character;
}
}

View file

@ -0,0 +1,81 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class TextUIElement : UIElement {
public SpriteFont Font { get; set; }
public Alignment CurrentAlignment { get; set; }
public string Text{
get;
set{
field = value;
if(AutoBounds)
Bounds = (Bounds.Item1, Bounds.Item1 + new Point((int)Measure().X, (int)Measure().Y));
}
} = "";
public Color Color{ get; set; } = Color.White;
public bool AutoBounds{ get; protected set; } = false;
private Vector2 origin;
private const float UNIVERSAL_TEXT_SCALE_MULTIPLIER = 0.3f;
public TextUIElement(Point corner1, SpriteFont font, Alignment alignment = Alignment.LEFT, bool autoBounds = true) : base(corner1, corner1) {
Font = font;
AutoBounds = autoBounds;
Align(alignment);
}
public TextUIElement(Point corner1, Point corner2, SpriteFont font, Alignment alignment = Alignment.LEFT) : base(corner1, corner2) {
Font = font;
Align(alignment);
}
public override void Draw(SpriteBatch spriteBatch) {
base.Draw(spriteBatch);
align();
spriteBatch.DrawString(Font, Text, screenSpaceBounds.Item1.ToVector2(), Color, 0, origin, pixelScaleMultiplier * UNIVERSAL_TEXT_SCALE_MULTIPLIER, SpriteEffects.None, 0);
}
public void Align(Alignment alignment) {
CurrentAlignment = alignment;
switch (alignment){
case Alignment.LEFT:
AlignLeft();
break;
case Alignment.RIGHT:
AlignRight();
break;
case Alignment.CENTER:
AlignCenter();
break;
}
}
protected override void UpdateBounds() {
Point inSpaceOrigin = Bounds.Item1 + origin.ToPoint();
_bounds = ((Bounds.Item1 - inSpaceOrigin).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin);
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
Align(CurrentAlignment);
}
public Vector2 Measure() => Font.MeasureString(Text) * UNIVERSAL_TEXT_SCALE_MULTIPLIER * ScaleMultiplier;
private void AlignLeft() {
align = () => origin = Vector2.Zero;
}
private void AlignRight() {
align = () => origin = Font.MeasureString(Text) * ScaleMultiplier;
}
private void AlignCenter() {
align = () => origin = new(Font.MeasureString(Text).X * ScaleMultiplier / 2, 0);
}
private Action align;
public enum Alignment {
LEFT, RIGHT, CENTER
}
}

View file

@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ONDClient.GUI;
public class TimerUIElement : TextUIElement{
private Stopwatch stopwatch = new();
public TimerUIElement(Point corner1, SpriteFont font) : base(corner1, font) {
Text = "00:00.000";
Bounds = (corner1, corner1 + new Point((int)Measure().X, (int)Measure().Y));
}
public override void Update() {
if (stopwatch.IsRunning){
Text = stopwatch.Elapsed.ToString("mm\\:ss\\.fff");
// Text = stopwatch.ElapsedMilliseconds.ToString();
}
}
public void Start() {
stopwatch.Restart();
}
public void Stop() {
stopwatch.Stop();
}
}

108
ONDClient/GUI/UIElement.cs Normal file
View file

@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;
public class UIElement {
public bool Active { get; set; } = true;
public bool Pressable { get; set; } = false;
public bool Visible { get; set; } = true;
public int DrawPriority { get; } = 0;
protected (Point, Point) _bounds;
public (Point, Point) Bounds{
get => _bounds;
protected set { _bounds = value; UpdateBounds(); }
}
protected (Point, Point) screenSpaceBounds;
public List<TextureRegion> Textures = new();
protected int currentTextureId = 0;
private float _scaleMultiplier = 1;
public float ScaleMultiplier{
get{
return _scaleMultiplier;
}
set{
_scaleMultiplier = value;
LoadPixelScaleMultiplier();
}
}
protected int pixelScaleMultiplier = 1;
private void LoadPixelScaleMultiplier() {
pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense
UpdateBounds();
}
protected virtual void UpdateBounds() {
_bounds = (Bounds.Item1, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + Bounds.Item1);
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
}
public UIElement(TextureRegion texture, Point position, int drawPriority = 0) {
Textures.Add(texture);
Bounds = (position, position + new Point(texture.Width, texture.Height));
DrawPriority = drawPriority;
LoadPixelScaleMultiplier();
}
public UIElement(TextureRegion[] textures, Point position, int drawPriority = 0) {
this.Textures.AddRange(textures);
Bounds = (position, position + new Point(textures[0].Width, textures[0].Height));
DrawPriority = drawPriority;
LoadPixelScaleMultiplier();
}
public UIElement(Point corner1, Point corner2) {
Bounds = (corner1, corner2);
Visible = false;
LoadPixelScaleMultiplier();
}
public virtual void SetTexture(int textureId) {
if (textureId >= Textures.Count){
Console.WriteLine($"WARNING: TEXTURE {textureId} OUT OF BOUNDS");
return;
}
currentTextureId = textureId;
}
public virtual void Update() { }
public bool IsWithinBounds(Point pos) {
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; }
// public virtual void OnMousePress() { }
public virtual void OnMouseRelease() { }
public virtual void OnMouseHold() { }
public virtual void Draw(SpriteBatch spriteBatch) {
if (!Visible || !Active){
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);
}
public void SetPosition(Point position) {
Bounds = (position, position + new Point(Textures[0].Width, Textures[0].Height));
}
public UIElement Clone() {
return new UIElement(Textures.ToArray(), Bounds.Item1);
}
public TextureRegion[] GetTextures() => Textures.ToArray();
}

348
ONDClient/GUI/UIManager.cs Normal file
View file

@ -0,0 +1,348 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GlobalClassLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
using ONDClient.Map;
namespace ONDClient.GUI;
public class UIManager {
public static class ScreenTypes {
public const string OFFICE = "office";
public const string CAMERAS = "monitor";
public const string OVERLAY = "overlay";
public const string WIN = "win";
public const string LOSE = "lose";
public const string MENU = "menu";
public const string LOADING = "loading";
}
private static Screen officeScreen = new(ScreenTypes.OFFICE);
private static Screen monitorScreen = new(ScreenTypes.CAMERAS);
private static Screen overlayScreen = new(ScreenTypes.OVERLAY);
private static Screen winScreen = new(ScreenTypes.WIN);
private static Screen loseScreen = new(ScreenTypes.LOSE);
private static Screen menuScreen = new(ScreenTypes.MENU);
private static Screen loadingScreen = new(ScreenTypes.LOADING);
private static TextureAtlas testAtlas;
public static TextureAtlas OfficeAtlas{ get; private set; }
public static TextureAtlas MonitorAtlas{ get; private set; }
public static TextureAtlas EnemyAtlas{ get; private set; }
public static TextureAtlas RoomAtlas{ get; private set; }
public static SpriteFont PixelMonoFont{ get; private set; }
public static int GlobalPixelMultiplier{ get; private set; }
// private Dictionary<(int, int), UIElement> doorElements = new();
private static Dictionary<int, UIElement> enemyElements = new();
private static TimerUIElement timerElement;
private static UIElement cameraView;
private static Dictionary<int, UIElement> lightIndicators = new();
private static InputListenerHook monitorSwitchHook;
private static bool fullBright = false; // Debug
public static void LoadAssets() {
testAtlas = TextureAtlas.FromFile(Core.content, "images/testBlocks-definition.xml");
OfficeAtlas = TextureAtlas.FromFile(Core.content, "images/office-definition.xml");
MonitorAtlas = TextureAtlas.FromFile(Core.content, "images/monitor-definition.xml");
EnemyAtlas = TextureAtlas.FromFile(Core.content, "images/enemies-definition.xml");
RoomAtlas = TextureAtlas.FromFile(Core.content, "images/rooms-definition.xml");
PixelMonoFont = Core.content.Load<SpriteFont>("ponderosa");
}
public static void InitUI() {
GlobalPixelMultiplier = Core.graphicsDevice.Viewport.Height / 360;
Screen.AddScreens([officeScreen, monitorScreen, overlayScreen, winScreen, loseScreen, menuScreen, loadingScreen]);
// Screen.SetScreen(ScreenTypes.OFFICE);
// Screen.SetOverlayScreen(ScreenTypes.OVERLAY);
officeScreen.AddElement("office_left", new UIElement([OfficeAtlas["left-open"], OfficeAtlas["left-closed"]], Point.Zero));
officeScreen.AddElement("office_centre", new UIElement([OfficeAtlas["centre-open"], OfficeAtlas["centre-closed"]], new Point(200, 0)));
officeScreen.AddElement("office_right", new UIElement([OfficeAtlas["right-open"], OfficeAtlas["right-closed"]], new Point(440, 0)));
// officeScreen.AddElement("test",
// new UIElement(testAtlas[0], Point.Zero)
// {Pressable = true, OnMousePress = () => Console.WriteLine("Pressed!")}
// );
monitorScreen.AddElement("screen", new UIElement(MonitorAtlas["screen"], Point.Zero));
monitorScreen.AddElement("view-frame", new UIElement(MonitorAtlas["view-frame"], new Point(62, 55)));
monitorScreen.AddElement("map-frame", new UIElement(MonitorAtlas["map-frame"], new Point(334, 135)));
monitorScreen.AddElement("map", new UIElement(MonitorAtlas["map"], new Point(334, 135)));
List<TextureRegion> rooms = new();
for (int i = 0; i < ClientMapManager.MAP_SIDE_LENGTH * ClientMapManager.MAP_SIDE_LENGTH; i++){
rooms.Add(RoomAtlas["room" + i]);
}
cameraView = new UIElement(rooms.ToArray(), new(64, 64));
monitorScreen.AddElement("camera-view", cameraView);
monitorScreen.AddElement("p1-office-door-left", new UIElement([MonitorAtlas["door-office-p1-left-open"], MonitorAtlas["door-office-p1-left-closed"]], new Point(400, 272)));
monitorScreen.AddElement("p1-office-door-centre", new UIElement([MonitorAtlas["door-office-p1-centre-open"], MonitorAtlas["door-office-p1-centre-closed"]], new Point(400, 272)));
monitorScreen.AddElement("p1-office-door-right", new UIElement([MonitorAtlas["door-office-p1-right-open"], MonitorAtlas["door-office-p1-right-closed"]], new Point(400, 272)));
monitorScreen.AddElement("p2-office-door-right", new UIElement([MonitorAtlas["door-office-p2-right-open"], MonitorAtlas["door-office-p2-right-closed"]], new Point(400, 144)));
monitorScreen.AddElement("p2-office-door-centre", new UIElement([MonitorAtlas["door-office-p2-centre-open"], MonitorAtlas["door-office-p2-centre-closed"]], new Point(400, 144)));
monitorScreen.AddElement("p2-office-door-left", new UIElement([MonitorAtlas["door-office-p2-left-open"], MonitorAtlas["door-office-p2-left-closed"]], new Point(400, 144)));
// main menu
timerElement = new(new(0, 0), PixelMonoFont);
overlayScreen.AddElement("timer", timerElement);
officeScreen.AddElement("power-p1-office", new PowerIndicator(new(timerElement.Bounds.Item1.X, timerElement.Bounds.Item2.Y + 5), PixelMonoFont, Client.Player, "POWER: "));
TextUIElement powerLabel = (TextUIElement)
monitorScreen.AddElement("power-label", new TextUIElement(new(510, 150), PixelMonoFont){Text = "POWER:"});
TextUIElement powerP1 = (TextUIElement)
monitorScreen.AddElement("power-p2", new PowerIndicator(new(powerLabel.Bounds.Item1.X + 10, powerLabel.Bounds.Item2.Y + 10), PixelMonoFont, Client.Opponent, ""){Color = new Color(220, 10, 10, 255)});
monitorScreen.AddElement("power-p1", new PowerIndicator( new (powerP1.Bounds.Item1.X, powerP1.Bounds.Item2.Y + 5), PixelMonoFont, Client.Player, ""){Color = new Color(15, 190, 247, 255)});
winScreen.AddElement("win-text", new TextUIElement(new(320, 180), PixelMonoFont, TextUIElement.Alignment.CENTER){Text = "YOU WIN", Color = Color.Green});
loseScreen.AddElement("lose-text", new TextUIElement(new(320, 180), PixelMonoFont, TextUIElement.Alignment.CENTER){Text = "YOU LOSE", Color = Color.Red});
MenuInputField usernameField = (MenuInputField)menuScreen.AddElement("username-field", new MenuInputField(PixelMonoFont, new(20, 20), "USERNAME: "));
MenuInputField field = (MenuInputField)menuScreen.AddElement("server-ip-field", new MenuInputField(PixelMonoFont, new(usernameField.Bounds.Item1.X, usernameField.Bounds.Item2.Y + 20), "SERVER IP: ", "127.0.0.1"));
UIElement connectButton = menuScreen.AddElement("server-ip-submit", new TextUIElement(new Point(field.Bounds.Item1.X, field.Bounds.Item2.Y), PixelMonoFont)
{
Text = "CONNECT",
Pressable = true,
OnMousePress = () => {
// string[] input = serverIpTextBox.Text.Split(":");
// if(input.Length != 2 || !int.TryParse(input[0], out var port)) return;
// Client.Connect(input[0], port);
Client.Player.username = usernameField.Text;
Client.Connect(field.Text, 9012);
Screen.SetScreen(ScreenTypes.LOADING);
}
});
menuScreen.AddElement("host-button",
new TextUIElement(new(connectButton.Bounds.Item1.X, connectButton.Bounds.Item2.Y + 30), PixelMonoFont) {Text = "HOST", Pressable = true,
OnMousePress = () => {
Client.StartServer();
Client.Player.username = usernameField.Text;
Client.Connect("127.0.0.1", 9012);
Screen.SetScreen(ScreenTypes.LOADING);
}});
loadingScreen.AddElement("loading-text", new LoadingUIElement(new(320, 180), PixelMonoFont, field.Text));
}
public static void DisplayMainMenu() {
ResetUI();
Screen.SetScreen(ScreenTypes.MENU);
CommandManager.AllowGameControls(false);
// if(Client.Player.username != null)
// ((MenuInputField)menuScreen["username-field"]).Text = Client.Player.username;
}
public static void SpawnMapElements(TileConnectorProjection[] doors) {
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++){
int id = ClientMapManager.CoordsToId(i, 4 - j);
if (Client.Player.state.officeTileId == id || Client.Opponent.state.officeTileId == id) continue; // TODO: remove the other check for office
Point point1 = new Point(336 + (32 * i), 144 + (32 * j));
Point point2 = new Point(367 + (32 * i), 175 + (32 * j));
monitorScreen.AddElement(
$"room{id}", new UIElement(point1, point2)
{Pressable = true, OnMousePress = (() => CommandManager.SendChangeCamera(id))},
true);
lightIndicators.Add(id,
monitorScreen.AddElement(
$"light{id}", new UIElement(MonitorAtlas["map-light-indicator"], point1)
{Visible = false},
true));
//
// if (doorPositions.ContainsKey((i, j))){
// monitorScreen.AddElement("door"+doorPositions[(i, j)], new UIElement([monitorAtlas[5], monitorAtlas[6]], point1));
// }
}
}
monitorScreen.AddElement("eye-player", new UIElement(MonitorAtlas["eye-small-player"], monitorScreen["room"+Client.Player.state.camera].Bounds.Item1), true);
monitorScreen.AddElement("eye-opponent", new UIElement([MonitorAtlas["eye-small-opponent-closed"], MonitorAtlas["eye-small-opponent-open"]], monitorScreen["room"+Client.Opponent.state.camera].Bounds.Item1), true);
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"+Math.Max(door.Tiles.tile1.Id, door.Tiles.tile2.Id)+"-"+Math.Min(door.Tiles.tile1.Id, door.Tiles.tile2.Id), new UIElement([MonitorAtlas["door-remote-open"], MonitorAtlas["door-remote-closed"]], tile.Bounds.Item1), true);
}
}
}
public static void DisplayGameUI() {
Screen.SetScreen(ScreenTypes.OFFICE);
Screen.SetOverlayScreen(ScreenTypes.OVERLAY);
CommandManager.AllowGameControls(true);
UpdateCameras([Client.Player.state.camera]); // in case there is an enemy on the default camera
cameraView.SetTexture(Client.Player.state.camera);
}
public static void StartTimer() {
timerElement.Start();
}
public static void AddEnemySprite(int id, UIElement sprite, UIElement jumpscareSprite = null) {
monitorScreen.AddElement($"enemy{id}", sprite, true);
if (jumpscareSprite != null){
officeScreen.AddElement($"enemy{id}-jumpscare", jumpscareSprite, true);
jumpscareSprite.Active = false;
jumpscareSprite.Visible = false;
}
enemyElements.Add(id, sprite);
sprite.Visible = false;
}
public static void ChangeDoorState(Direction dir, bool state) {
int stateInt = state ? 1 : 0;
switch ((int)dir){
case 0:
officeScreen["office_left"].SetTexture(stateInt);
monitorScreen["p1-office-door-left"].SetTexture(stateInt);
break;
case 1:
officeScreen["office_centre"].SetTexture(stateInt);
monitorScreen["p1-office-door-centre"].SetTexture(stateInt);
break;
case 2:
officeScreen["office_right"].SetTexture(stateInt);
monitorScreen["p1-office-door-right"].SetTexture(stateInt);
break;
}
}
public static void ChangeDoorStateOpponent(Direction dir, bool state) { // TODO: overload to avoid excessive casting
int stateInt = state ? 1 : 0;
switch ((int)dir){
case 0:
monitorScreen["p2-office-door-left"].SetTexture(stateInt);
break;
case 1:
monitorScreen["p2-office-door-centre"].SetTexture(stateInt);
break;
case 2:
monitorScreen["p2-office-door-right"].SetTexture(stateInt);
break;
}
}
public static void ChangeRemoteDoorState((int, int) id, bool state) {
monitorScreen["door"+Math.Max(id.Item1, id.Item2)+"-"+Math.Min(id.Item1, id.Item2)].SetTexture(state ? 1 : 0);
}
public static void ChangeMonitorState(bool state) {
Screen.SetScreen(state ? ScreenTypes.CAMERAS : ScreenTypes.OFFICE);
UpdateCameras([Client.Player.state.camera]);
}
public static void ChangeMonitorStateOpponent(bool state) {
monitorScreen["eye-opponent"].SetTexture(state ? 1 : 0);
}
public static void ChangeCamera(int id) {
monitorScreen["eye-player"].SetPosition(monitorScreen["room"+id].Bounds.Item1);
cameraView.SetTexture(id);
UpdateCameras([id]);
}
public static void UpdateCameras(int[] camIds) {
foreach (var id in camIds){
MapTileProjection tile = ClientMapManager.Get(id);
if(tile.Owner == null || tile.Id == Client.Player.state.officeTileId || tile.Id == Client.Opponent.state.officeTileId) continue;
lightIndicators[id].Visible = tile.Lit;
}
if (camIds.Contains(Client.Player.state.camera)){
bool lit = ClientMapManager.Get(Client.Player.state.camera).Lit || fullBright;
cameraView.Visible = lit;
enemyElements.Values.Where(e => e.Visible).ToList().ForEach(e => e.Visible = false);
ClientEnemy[] enemies = ClientEnemyManager.GetByLocation(ClientMapManager.Get(Client.Player.state.camera));
foreach (var enemy in enemies){
enemyElements.TryGetValue(enemy.Id, out var element);
if (element == null) continue;
EnemyUIElement enemyElement = (EnemyUIElement)element;
enemyElement.Visible = true;
enemyElement.SetTexture(lit);
}
if (!lit && Client.Player.state.monitorUp && enemies.Any(e => e.TypeId == (int)EnemyType.NEKO)){
SoundManager.StartNekoPurr();
}
else{
SoundManager.StopNekoPurr();
}
}
}
public static void ChangeCameraOpponent(int id) {
monitorScreen["eye-opponent"].SetPosition(monitorScreen["room"+id].Bounds.Item1);
}
public static void Jumpscare(ClientEnemy enemy) {
Screen.SetScreen(ScreenTypes.OFFICE);
enemy.JumpscareSprite.Play();
timerElement.Stop();
CommandManager.AllowGameControls(false);
// UIElement jumpscareElement = enemy.Sprite.Clone();
// jumpscareElement.ScaleMultiplier = 2;
// jumpscareElement.SetPosition(new Point(0, 0));
// officeScreen.AddElement("jumpscare", jumpscareElement);
}
public static void ShowVictoryScreen() {
Screen.SetScreen(ScreenTypes.WIN);
Screen.DisableOverlay();
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
InputManager.AddListener(Keys.Space, DisplayMainMenu, InputTiming.PRESS, new InputListenerHook(true, true));
}
public static void ShowDeathScreen() {
Screen.SetScreen(ScreenTypes.LOSE);
Screen.DisableOverlay();
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
InputManager.AddListener(Keys.Space, DisplayMainMenu, InputTiming.PRESS, new InputListenerHook(true, true));
}
public static void ResetUI() {
foreach (Screen screen in Screen.Screens.Values){
screen.RemoveTemporary();
}
lightIndicators.Clear();
enemyElements.Clear();
timerElement.Stop();
}
// private static Point GetRoomUIPos((int x, int y) pos) {
// return new Point(336 + (32 * pos.x), 144 + (32 * pos.y));
// }
}