OneNightDuel/ONDClient/GUI/UIManager.cs

325 lines
No EOL
16 KiB
C#

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.Enemies;
using ONDClient.Map;
using ONDClient.Net;
using ONDClient.Sound;
namespace ONDClient.GUI;
public static 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 readonly Screen officeScreen = new(ScreenTypes.OFFICE);
private static readonly Screen monitorScreen = new(ScreenTypes.CAMERAS);
private static readonly Screen overlayScreen = new(ScreenTypes.OVERLAY);
private static readonly Screen winScreen = new(ScreenTypes.WIN);
private static readonly Screen loseScreen = new(ScreenTypes.LOSE);
private static readonly Screen menuScreen = new(ScreenTypes.MENU);
private static readonly 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; }
// 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 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() {
UIElement.GlobalPixelMultiplier = Core.graphicsDevice.Viewport.Height / 360;
Screen.AddScreens([officeScreen, monitorScreen, overlayScreen, winScreen, loseScreen, menuScreen, loadingScreen]);
// office
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)));
// monitor
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)));
// timer and power
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)});
// loading
ReturnToMenuElement returnToMenuElement = new ReturnToMenuElement(new(320, 290), PixelMonoFont);
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});
winScreen.AddElement("press-space", returnToMenuElement);
loseScreen.AddElement("press-space", returnToMenuElement);
loadingScreen.AddElement("press-space", returnToMenuElement);
// main menu
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 = () => {
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);
}
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;
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));
}
}
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) {
int stateInt = state ? 1 : 0;
switch (dir){
case Direction.EAST:
monitorScreen["p2-office-door-left"].SetTexture(stateInt);
break;
case Direction.NORTH:
monitorScreen["p2-office-door-centre"].SetTexture(stateInt);
break;
case Direction.WEST:
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.Type == 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);
}
public static void ShowVictoryScreen() {
Screen.SetScreen(ScreenTypes.WIN);
Screen.DisableOverlay();
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
SoundManager.StopNekoPurr();
}
public static void ShowDeathScreen() {
Screen.SetScreen(ScreenTypes.LOSE);
Screen.DisableOverlay();
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
SoundManager.StopNekoPurr();
}
public static void ResetUI() {
foreach (Screen screen in Screen.GetAllScreens()){
screen.RemoveTemporary();
}
lightIndicators.Clear();
enemyElements.Clear();
timerElement.Stop();
}
}