Oprava spawnování monster, optimalizace v CommandProcessor a EventProcessor. Přesunutí některých tříd do vlastních namespaců, pročištění kódu, úpravy formátování, odstranění nepoužívaných souborů a zakomentovaného kódu

This commit is contained in:
Perry 2026-03-28 09:59:31 +01:00
parent e5d746d597
commit 243f071a43
62 changed files with 873 additions and 1217 deletions

View file

@ -1,8 +0,0 @@
using PacketLib;
namespace ONDClient;
public class ClientPlayer {
public PlayerState state;
public string username;
}

View file

@ -1,24 +1,20 @@
using GlobalClassLib;
using MonoGameLibrary.Graphics;
using ONDClient.GUI;
using ONDClient.Map;
namespace ONDClient;
namespace ONDClient.Enemies;
public class ClientEnemy : GlobalEnemy<MapTileProjection, TileConnectorProjection> {
public ClientEnemy(int typeId, string name, int id, EnemyUIElement sprite, int difficulty, MapTileProjection location, JumpscareUIElement jumpscareSprite) : base(id) {
public UIElement Sprite { get; set; }
public override string Name{ get; }
public override EnemyType Type{ get; }
public JumpscareUIElement JumpscareSprite { get; set; }
public ClientEnemy(EnemyType type, string name, int id, EnemyUIElement sprite, MapTileProjection location, JumpscareUIElement jumpscareSprite) : base(id) {
Name = name;
TypeId = typeId;
Type = type;
Sprite = sprite;
Location = location;
JumpscareSprite = jumpscareSprite;
}
// public TextureRegion Sprite { get; set; }
public UIElement Sprite { get; set; }
public override string Name{ get; }
public override int TypeId{ get; }
public JumpscareUIElement JumpscareSprite { get; set; }
}
}

View file

@ -1,47 +1,43 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using GlobalClassLib;
using Microsoft.Xna.Framework;
using MonoGameLibrary.Graphics;
using ONDClient.GUI;
using ONDClient.Map;
namespace ONDClient;
namespace ONDClient.Enemies;
public static class ClientEnemyManager {
private static Dictionary<int, ClientEnemy> enemies = new();
private static Point cameraCorner = new(64, 64);
private static Action defaultAfterJumpscare = UIManager.ShowDeathScreen;
private static readonly Point cameraCorner = new(64, 64);
private static readonly Action defaultAfterJumpscare = UIManager.ShowDeathScreen;
public static void AddEnemy(ClientEnemy enemy) {
enemies.Add(enemy.Id, enemy);
UIManager.AddEnemySprite(enemy.Id, enemy.Sprite, enemy.JumpscareSprite);
}
public static void AddEnemyByTemplate(EnemyType type, int id, int difficulty, MapTileProjection location) {
public static void AddEnemyByTemplate(EnemyType type, int id, MapTileProjection location) {
switch (type){
case EnemyType.LURK:
AddEnemy(new ClientEnemy(
(int)type,
type,
"Lurk",
id,
new EnemyUIElement(UIManager.EnemyAtlas["lurk-lit"], UIManager.EnemyAtlas["lurk-unlit"], cameraCorner),
difficulty,
location,
new JumpscareUIElement(UIManager.EnemyAtlas["lurk-lit"], new(0, 0), 3, 2, 2, 0.1f, afterStop:defaultAfterJumpscare)
new JumpscareUIElement(UIManager.EnemyAtlas["lurk-lit"], new(0, 0), 3, 2, 2, afterStop:defaultAfterJumpscare)
));
break;
case EnemyType.NEKO:
AddEnemy(new ClientEnemy(
(int)type,
type,
"Neko",
id,
new EnemyUIElement(UIManager.EnemyAtlas["neko-lit"], UIManager.EnemyAtlas["neko-unlit"], cameraCorner, 1),
difficulty,
location,
new JumpscareUIElement(UIManager.EnemyAtlas["neko-lit"], new(0, -30), 3, 2, 2, 0.1f, afterStop:defaultAfterJumpscare)
new JumpscareUIElement(UIManager.EnemyAtlas["neko-lit"], new(0, -30), 3, 2, 2, afterStop:defaultAfterJumpscare)
));
break;
case EnemyType.SPOT:
@ -49,35 +45,32 @@ public static class ClientEnemyManager {
new EnemyUIElement([UIManager.EnemyAtlas["spot-awake-lit"], UIManager.EnemyAtlas["spot-asleep-lit"]],[UIManager.EnemyAtlas["spot-awake-unlit"], UIManager.EnemyAtlas["spot-asleep-unlit"]], cameraCorner, 2);
element.SetTexture(true, 1);
AddEnemy(new ClientEnemy(
(int)type,
type,
"Spot",
id,
element,
difficulty,
element,
location,
new JumpscareUIElement(UIManager.EnemyAtlas["spot-awake-lit"], new(0, 0), 3, 2, 2, 0.1f, afterStop:defaultAfterJumpscare)
new JumpscareUIElement(UIManager.EnemyAtlas["spot-awake-lit"], new(0, 0), 3, 2, 2, afterStop:defaultAfterJumpscare)
));
break;
case EnemyType.DASH:
AddEnemy(new ClientEnemy(
(int)type,
type,
"Dash",
id,
new EnemyUIElement(UIManager.EnemyAtlas["dash-lit"], UIManager.EnemyAtlas["dash-unlit"], cameraCorner, 3),
difficulty,
location,
new JumpscareUIElement(UIManager.EnemyAtlas["dash-lit"], new(0, 0), 3, 2, 2, 0.1f, afterStop:defaultAfterJumpscare)
new JumpscareUIElement(UIManager.EnemyAtlas["dash-lit"], new(0, 0), 3, 2, 2, afterStop:defaultAfterJumpscare)
));
break;
case EnemyType.MARE:
AddEnemy(new ClientEnemy(
(int)type,
type,
"Mare",
id,
new EnemyUIElement(UIManager.EnemyAtlas["mare-lit"], UIManager.EnemyAtlas["mare-unlit"], cameraCorner),
difficulty,
location,
new JumpscareUIElement(UIManager.EnemyAtlas["mare-lit"], new(0, 0), 3, 2, 2, 0.1f, afterStop:defaultAfterJumpscare)
new JumpscareUIElement(UIManager.EnemyAtlas["mare-lit"], new(0, 0), 3, 2, 2, afterStop:defaultAfterJumpscare)
));
break;
}

View file

@ -1,188 +0,0 @@
using System;
using System.Linq;
using GlobalClassLib;
using MonoGameLibrary.Input;
using ONDClient.GUI;
using ONDClient.Map;
using PacketLib;
namespace ONDClient;
public class EventProcessor {
public static void Evaluate(GameEvent[] events) {
foreach (var e in events){
switch (e.ID){
case 0: // join
Console.WriteLine("E: Player joined");
break;
case 1: // leave
Console.WriteLine("E: Player left");
break;
case 2: // switch cam
if (Client.Player.state.pid != e.Args[0]){
UIManager.ChangeCameraOpponent(e.Args[1]);
break;
}
if (Client.Player.state.camera != e.Args[1]) Console.WriteLine("!!! DESYNC: CAMERA STATE");
Console.WriteLine($"E: player {e.Args[0]} switched to camera {e.Args[1]}");
break;
case 3: // toggle cam
Console.WriteLine($"E: Player {e.Args[0]} toggled monitor {(e.Args[1] == 0 ? "off" : "on")}");
if (e.Args[0] != Client.Player.state.pid){
UIManager.ChangeMonitorStateOpponent(e.Args[1] == 1);
break;
}
if (Client.Player.state.monitorUp != (e.Args[1] == 1)) Console.WriteLine("!!! DESYNC: MONITOR STATE");
// UIManager.ChangeMonitorState(e.Args[1] == 1);
break;
case 4: // toggle door
Console.WriteLine($"E: Player {e.Args[0]} {(e.Args[2] == 1 ? "closed" : "opened")} door {e.Args[1]}");
if (e.Args[0] == Client.Player.state.pid){
if (Client.Player.state.doorStates[e.Args[1]] != (e.Args[2] == 1)) Console.WriteLine("!!! DESYNC: DOOR STATE");
break;
}
Client.Opponent.state.doorStates[e.Args[1]] = e.Args[2] == 1;
UIManager.ChangeDoorStateOpponent((Direction)e.Args[1], e.Args[2] == 1);
break;
case 5: // toggle remote door
Console.WriteLine($"E: Player {e.Args[0]} {(e.Args[3] == 1 ? "closed" : "opened")} door {e.Args[1]}-{e.Args[2]}");
if (e.Args[0] == Client.Player.state.pid){
if (ClientMapManager.GetConnector((e.Args[1], e.Args[2])).Blocked != (e.Args[3] == 1)) Console.WriteLine("!!! DESYNC: REMOTE DOOR STATE");
break;
}
ClientMapManager.GetConnector((e.Args[1], e.Args[2])).Blocked = e.Args[3] == 1;
UIManager.ChangeRemoteDoorState((e.Args[1], e.Args[2]), e.Args[3] == 1);
break;
case 6: // spawn
Console.WriteLine($"E: Spawned enemy {e.Args[0]} at {e.Args[3]}");
ClientEnemyManager.AddEnemyByTemplate((EnemyType)e.Args[0], e.Args[1], e.Args[2], ClientMapManager.Get(e.Args[3]));
UIManager.UpdateCameras([e.Args[3]]);
break;
case 7: // movement
Console.WriteLine($"E: Enemy {e.Args[0]} moved to {e.Args[1]}");
int oldPos = ClientEnemyManager.Get(e.Args[0]).Location!.Id;
ClientEnemyManager.Move(e.Args[0], ClientMapManager.Get(e.Args[1]));
UIManager.UpdateCameras([oldPos, e.Args[1]]);
SoundManager.PlayEnemyMove(ClientEnemyManager.Get(e.Args[0]));
break;
case 8: // attack
Console.WriteLine($"E: Enemy {e.Args[0]} attacked player {e.Args[1]}"); // TODO: add an arg to indicate lethality
if (e.Args[1] == Client.Player.state.pid) {
UIManager.Jumpscare(ClientEnemyManager.Get(e.Args[0]));
SoundManager.PlayJumpscare();
}
break;
case 9: // reset
Console.WriteLine($"E: Enemy {e.Args[0]} reset to {e.Args[1]}");
int preResetPos = ClientEnemyManager.Get(e.Args[0]).Location!.Id;
ClientEnemyManager.Move(e.Args[0], ClientMapManager.Get(e.Args[1]));
UIManager.UpdateCameras([preResetPos, e.Args[1]]);
SoundManager.PlayEnemyReset(ClientEnemyManager.Get(e.Args[0]));
break;
case 10:
Console.WriteLine($"E: Spot:{e.Args[0]} turned {(e.Args[1] == 1 ? "on" : " off")}");
ClientEnemyManager.Get(e.Args[0]).Sprite.SetTexture(e.Args[1] == 1 ? 0 : 1);
SoundManager.PlaySpotActivate();
break;
case 11:
Console.WriteLine($"E: Player {e.Args[0]} won");
if(Client.Player.state.pid == e.Args[0]) UIManager.ShowVictoryScreen();
Client.Disconnect();
ClientEnemyManager.ClearEnemies();
break;
case 12: // game start
Console.WriteLine("E: Game started");
UIManager.DisplayGameUI();
UIManager.StartTimer();
SoundManager.StartAmbience();
Client.State = Client.ConnectionState.GAME_IN_PROGRESS;
break;
case 13:
// Console.WriteLine($"E: power tick {e.Args[0]}: {e.Args[1]}");
if (e.Args[0] == Client.Player.state.pid){
Client.Player.state.power = e.Args[1];
}
else{
Client.Opponent.state.power = e.Args[1];
}
break;
case 14: // powerout
Console.WriteLine($"E: Player {e.Args[0]} powered out");
ClientMapManager.GetAllConnectors().Where(c =>
(c.Type == ConnectorType.DOOR_REMOTE || c.Type == ConnectorType.DOOR_OFFICE) &&
c.Owner == Client.GetPlayer(e.Args[0])).ToList().ForEach(c =>
{
c.Blocked = false;
if(c.Type == ConnectorType.DOOR_REMOTE)
UIManager.ChangeRemoteDoorState(c.Id, false);
});
foreach (var tile in ClientMapManager.GetAllTiles()){
tile.Lit = false;
}
if (e.Args[0] == Client.Player.state.pid){
UIManager.ChangeDoorState(Direction.EAST, false);
UIManager.ChangeDoorState(Direction.NORTH, false);
UIManager.ChangeDoorState(Direction.WEST, false);
CommandManager.AllowGameControls(false);
UIManager.ChangeMonitorState(false);
SoundManager.PlayPowerOut();
}
else{
UIManager.ChangeDoorStateOpponent(Direction.EAST, false);
UIManager.ChangeDoorStateOpponent(Direction.NORTH, false);
UIManager.ChangeDoorStateOpponent(Direction.WEST, false);
UIManager.ChangeMonitorStateOpponent(false);
}
break;
case 15: // light
bool lightState = e.Args[2] == 1;
Console.WriteLine($"E: Player {e.Args[0]} {(lightState ? "lit": "unlit")} tile {e.Args[1]}");
if (e.Args[0] == Client.Player.state.pid){
if (ClientMapManager.Get(e.Args[1]).Lit != lightState) Console.WriteLine("!!! DESYNC: LIGHT STATE");
break;
}
ClientMapManager.Get(e.Args[1]).Lit = lightState;
UIManager.UpdateCameras([e.Args[1]]);
break;
case 16: // neko anger
SoundManager.PlayNekoAnger();
break;
case 17:
SoundManager.PlayVentWalk();
break;
case 18:
SoundManager.PlayBell();
break;
}
}
}
}

View file

@ -5,7 +5,7 @@ using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class EnemyUIElement : UIElement {
private int unlitTexturesId;
private readonly int unlitTexturesId;
private bool currentlyLit = true;
public EnemyUIElement(TextureRegion litTexture, TextureRegion unlitTexture, Point position, int drawPriority = 0) : base([litTexture, unlitTexture], position, drawPriority) {
@ -28,6 +28,6 @@ public class EnemyUIElement : UIElement {
public void SetTexture(bool lit) {
if(lit == currentlyLit) return;
currentlyLit = lit;
SetTexture(lit ? currentTextureId - unlitTexturesId : currentTextureId);
SetTexture(lit ? CurrentTextureId - unlitTexturesId : CurrentTextureId);
}
}

View file

@ -10,29 +10,23 @@ public class JumpscareUIElement : UIElement {
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) {
public JumpscareUIElement(TextureRegion texture, Point positionDefault, int twitchHorizontal, int twitchVertical, float defaultScaleMultiplier, 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;
@ -51,8 +45,6 @@ public class JumpscareUIElement : UIElement {
AfterStop();
}
}
// ScaleMultiplier = defaultScaleMultiplier + (float)(random.NextDouble() * twitchScale * new[]{-1, 1}[random.Next(2)]);
}
private Action AfterStop;

View file

@ -1,17 +1,17 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ONDClient.Net;
namespace ONDClient.GUI;
public class LoadingUIElement : TextUIElement {
private Func<string> expectedEndpointGetter;
private readonly Func<string> expectedEndpointGetter;
private Client.ConnectionState lastState = Client.ConnectionState.IDLE;
public LoadingUIElement(Point corner1, SpriteFont font, Func<string> expectedEndpointGetter, Alignment alignment = Alignment.CENTER, bool autoBounds = true) : base(corner1, font, alignment, autoBounds) {
this.expectedEndpointGetter = expectedEndpointGetter;
Active = true;
// Color = Color.LightGray;
}
public override void Update() {
@ -29,9 +29,8 @@ public class LoadingUIElement : TextUIElement {
Text = "Waiting for opponent...";
break;
case Client.ConnectionState.GAME_STARTING:
Text = "Opponent: " + Client.Opponent.username;
Text = "Opponent: " + Client.Opponent.Username;
Color = Color.White;
// ScaleMultiplier = 1.5f;
break;
case Client.ConnectionState.IDLE:
Text = "Idle";

View file

@ -1,12 +1,11 @@
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;
private readonly TextUIElement labelElement;
private readonly TextBoxUIElement textBoxElement;
public MenuInputField(SpriteFont font, Point position, string label, string defaultValue = "") : base(position, position) {
labelElement =

View file

@ -1,26 +1,27 @@
using GlobalClassLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ONDClient.Net;
namespace ONDClient.GUI;
public class PowerIndicator : TextUIElement {
private string label;
private ClientPlayer player;
private readonly string label;
private readonly 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;
lastPowerValue = player.State.Power;
Text = GetText();
}
public override void Update() {
if (player.state.power == lastPowerValue) return;
lastPowerValue = player.state.power;
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)}";
private string GetText() => $"{label}{(int)(((float)player.State.Power / Power.MAX_POWER_VALUE) * 100)}";
}

View file

@ -2,6 +2,7 @@ using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Input;
using ONDClient.Net;
namespace ONDClient.GUI;

View file

@ -1,5 +1,5 @@
using System.Collections.Generic;
using Microsoft.VisualBasic.CompilerServices;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Input;
@ -9,36 +9,36 @@ namespace ONDClient.GUI;
public class Screen {
public static Dictionary<string, Screen> Screens = new();
private 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 AddScreens(Screen[] screensToAdd) {
foreach (var screen in screensToAdd){
Screen.screens.Add(screen.Label, screen);
}
}
public static void AddScreen(string id, Screen screen, bool activate = false) {
Screens.Add(id, screen);
screens.Add(id, screen);
if (activate) SetScreen(id);
}
public static void SetScreen(string id) {
if (CurrentScreen.temporary){
Screens.Remove(CurrentScreen.Label);
screens.Remove(CurrentScreen.Label);
}
CurrentScreen.Active = false;
CurrentScreen = Screens[id];
CurrentScreen = screens[id];
CurrentScreen.Active = true;
}
public static void RemoveScreen(string id) {
Screens.Remove(id);
screens.Remove(id);
}
public static void UpdateAll() {
foreach (var screen in Screens.Values){
foreach (var screen in screens.Values){
if (!screen.Active) continue;
screen.Update();
}
@ -47,11 +47,11 @@ public class Screen {
public static void SetOverlayScreen(string id) {
if (OverlayScreen.temporary){
Screens.Remove(OverlayScreen.Label);
screens.Remove(OverlayScreen.Label);
}
OverlayScreen.Active = false;
OverlayScreen = Screens[id];
OverlayScreen = screens[id];
OverlayScreen.Active = true;
}
@ -63,6 +63,8 @@ public class Screen {
CurrentScreen.Draw(spriteBatch);
OverlayScreen.Draw(spriteBatch);
}
public static Screen[] GetAllScreens() => screens.Values.ToArray();
@ -117,15 +119,6 @@ public class Screen {
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){
@ -135,19 +128,19 @@ public class Screen {
if (!element.Pressable) continue;
if (element.IsWithinBounds(mouseState.Position)){
element.OnMousePress(); // TODO: differentiate between press, hold and release events
element.OnMousePress();
}
}
}
public void Update() {
private void Update() {
foreach (var element in elements.Values){
if (!element.Active) continue;
element.Update();
}
}
public void Draw(SpriteBatch spriteBatch) {
private void Draw(SpriteBatch spriteBatch) {
foreach (var val in elementsInDrawOrder){
val.Draw(spriteBatch);
}

View file

@ -2,7 +2,6 @@ using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;

View file

@ -1,7 +1,6 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
@ -20,6 +19,7 @@ public class TextUIElement : UIElement {
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;
@ -39,7 +39,7 @@ public class TextUIElement : UIElement {
if(!Visible || !Active) return;
base.Draw(spriteBatch);
align();
spriteBatch.DrawString(Font, Text, screenSpaceBounds.Item1.ToVector2(), Color, 0, origin, pixelScaleMultiplier * UNIVERSAL_TEXT_SCALE_MULTIPLIER, SpriteEffects.None, 0);
spriteBatch.DrawString(Font, Text, ScreenSpaceBounds.Item1.ToVector2(), Color, 0, origin, pixelScaleMultiplier * UNIVERSAL_TEXT_SCALE_MULTIPLIER, SpriteEffects.None, 0);
}
public void Align(Alignment alignment) {
@ -60,7 +60,7 @@ public class TextUIElement : UIElement {
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));
ScreenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
Align(CurrentAlignment);
}

View file

@ -1,4 +1,3 @@
using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
@ -8,7 +7,6 @@ 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));
@ -17,7 +15,6 @@ public class TimerUIElement : TextUIElement{
public override void Update() {
if (stopwatch.IsRunning){
Text = stopwatch.Elapsed.ToString("mm\\:ss\\.fff");
// Text = stopwatch.ElapsedMilliseconds.ToString();
}
}

View file

@ -1,31 +1,28 @@
using System;
using System.Collections.Generic;
using System.Net.Mime;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
namespace ONDClient.GUI;
public class UIElement {
public static int GlobalPixelMultiplier{ get; set; }
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;
protected (Point, Point) _bounds;
protected (Point, Point) ScreenSpaceBounds;
protected int CurrentTextureId = 0;
private List<TextureRegion> textures = new();
private float _scaleMultiplier = 1;
public float ScaleMultiplier{
get{
@ -38,23 +35,23 @@ public class UIElement {
}
protected int pixelScaleMultiplier = 1;
private void LoadPixelScaleMultiplier() {
pixelScaleMultiplier = (int)(UIManager.GlobalPixelMultiplier * _scaleMultiplier); // TODO: move GlobalPixelMultiplier somewhere where it would make sense
pixelScaleMultiplier = (int)(GlobalPixelMultiplier * _scaleMultiplier);
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));
ScreenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
}
public UIElement(TextureRegion texture, Point position, int drawPriority = 0) {
Textures.Add(texture);
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);
this.textures.AddRange(textures);
Bounds = (position, position + new Point(textures[0].Width, textures[0].Height));
DrawPriority = drawPriority;
LoadPixelScaleMultiplier();
@ -67,44 +64,41 @@ public class UIElement {
}
public virtual void SetTexture(int textureId) {
if (textureId >= Textures.Count){
if (textureId >= textures.Count){
Console.WriteLine($"WARNING: TEXTURE {textureId} OUT OF BOUNDS");
return;
}
currentTextureId = textureId;
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);
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 OnMouseRelease() { }
// public virtual void OnMouseHold() { }
public virtual void Draw(SpriteBatch spriteBatch) {
if (!Visible || !Active || Textures.Count == 0){
if (!Visible || !Active || textures.Count == 0){
return;
}
Textures[currentTextureId].Draw(spriteBatch, screenSpaceBounds.Item1.ToVector2(), Color.White, 0, Vector2.Zero, pixelScaleMultiplier, SpriteEffects.None, 0);
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));
Bounds = (position, position + new Point(textures[0].Width, textures[0].Height));
}
public UIElement Clone() {
return new UIElement(Textures.ToArray(), Bounds.Item1);
return new UIElement(textures.ToArray(), Bounds.Item1);
}
public TextureRegion[] GetTextures() => Textures.ToArray();
public TextureRegion[] GetTextures() => textures.ToArray();
}

View file

@ -8,11 +8,14 @@ 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 class UIManager {
public static class UIManager {
public static class ScreenTypes {
public const string OFFICE = "office";
@ -24,35 +27,31 @@ public class UIManager {
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 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;
// 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");
// 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");
@ -61,21 +60,16 @@ public class UIManager {
}
public static void InitUI() {
GlobalPixelMultiplier = Core.graphicsDevice.Viewport.Height / 360;
UIElement.GlobalPixelMultiplier = Core.graphicsDevice.Viewport.Height / 360;
Screen.AddScreens([officeScreen, monitorScreen, overlayScreen, winScreen, loseScreen, menuScreen, loadingScreen]);
// Screen.SetScreen(ScreenTypes.OFFICE);
// Screen.SetOverlayScreen(ScreenTypes.OVERLAY);
// 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)));
// officeScreen.AddElement("test",
// new UIElement(testAtlas[0], Point.Zero)
// {Pressable = true, OnMousePress = () => Console.WriteLine("Pressed!")}
// );
// 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)));
@ -94,13 +88,11 @@ public class UIManager {
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
// 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:"});
@ -108,6 +100,7 @@ public class UIManager {
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});
@ -115,6 +108,7 @@ public class UIManager {
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)
@ -123,7 +117,7 @@ public class UIManager {
Pressable = true,
OnMousePress = () => {
Client.Player.username = usernameField.Text;
Client.Player.Username = usernameField.Text;
Client.Connect(field.Text, 9012);
Screen.SetScreen(ScreenTypes.LOADING);
}
@ -132,7 +126,7 @@ public class UIManager {
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.Player.Username = usernameField.Text;
Client.Connect("127.0.0.1", 9012);
Screen.SetScreen(ScreenTypes.LOADING);
@ -146,17 +140,14 @@ public class UIManager {
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
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(
@ -168,16 +159,11 @@ public class UIManager {
$"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);
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;
@ -198,8 +184,8 @@ public class UIManager {
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);
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();
@ -236,17 +222,17 @@ public class UIManager {
}
}
public static void ChangeDoorStateOpponent(Direction dir, bool state) { // TODO: overload to avoid excessive casting
public static void ChangeDoorStateOpponent(Direction dir, bool state) {
int stateInt = state ? 1 : 0;
switch ((int)dir){
case 0:
switch (dir){
case Direction.EAST:
monitorScreen["p2-office-door-left"].SetTexture(stateInt);
break;
case 1:
case Direction.NORTH:
monitorScreen["p2-office-door-centre"].SetTexture(stateInt);
break;
case 2:
case Direction.WEST:
monitorScreen["p2-office-door-right"].SetTexture(stateInt);
break;
}
@ -258,7 +244,7 @@ public class UIManager {
public static void ChangeMonitorState(bool state) {
Screen.SetScreen(state ? ScreenTypes.CAMERAS : ScreenTypes.OFFICE);
UpdateCameras([Client.Player.state.camera]);
UpdateCameras([Client.Player.State.Camera]);
}
public static void ChangeMonitorStateOpponent(bool state) {
@ -274,15 +260,15 @@ public class UIManager {
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;
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;
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));
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;
@ -291,7 +277,7 @@ public class UIManager {
enemyElement.SetTexture(lit);
}
if (!lit && Client.Player.state.monitorUp && enemies.Any(e => e.TypeId == (int)EnemyType.NEKO)){
if (!lit && Client.Player.State.MonitorUp && enemies.Any(e => e.Type == EnemyType.NEKO)){
SoundManager.StartNekoPurr();
}
else{
@ -302,7 +288,6 @@ public class UIManager {
public static void ChangeCameraOpponent(int id) {
monitorScreen["eye-opponent"].SetPosition(monitorScreen["room"+id].Bounds.Item1);
}
public static void Jumpscare(ClientEnemy enemy) {
@ -310,10 +295,6 @@ public class UIManager {
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() {
@ -322,7 +303,6 @@ public class UIManager {
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
SoundManager.StopNekoPurr();
// InputManager.AddListener(Keys.Space, DisplayMainMenu, InputTiming.PRESS, new InputListenerHook(true, true));
}
public static void ShowDeathScreen() {
@ -331,11 +311,10 @@ public class UIManager {
CommandManager.AllowGameControls(false);
SoundManager.StopAmbience();
SoundManager.StopNekoPurr();
// InputManager.AddListener(Keys.Space, DisplayMainMenu, InputTiming.PRESS, new InputListenerHook(true, true));
}
public static void ResetUI() {
foreach (Screen screen in Screen.Screens.Values){
foreach (Screen screen in Screen.GetAllScreens()){
screen.RemoveTemporary();
}
lightIndicators.Clear();
@ -343,13 +322,4 @@ public class UIManager {
timerElement.Stop();
}
// private static Point GetRoomUIPos((int x, int y) pos) {
// return new Point(336 + (32 * pos.x), 144 + (32 * pos.y));
// }
public static void SetLoadingScreenMessage(string text) {
}
}

View file

@ -1,29 +1,21 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGameLibrary;
using MonoGameLibrary.Graphics;
using MonoGameLibrary.Input;
using ONDClient.GUI;
using ONDClient.Net;
using ONDClient.Sound;
namespace ONDClient;
public class GameMain() : Core("OND", 1280, 720, false) {
// private GraphicsDeviceManager _graphics;
// private SpriteBatch _spriteBatch;
protected override void Initialize() {
Exiting += (_, _) => {
Client.Disconnect();
};
// Client.Connect("127.0.0.1", 9012);
CommandManager.InitInputListeners();
// InputManager.AddListener(InputManager.MouseButton.LEFT, (() => Console.WriteLine("LMB pressed at: " + InputManager.MouseState.Position)), InputTiming.PRESS, new InputListenerHook(true));
base.Initialize();
@ -35,8 +27,6 @@ public class GameMain() : Core("OND", 1280, 720, false) {
protected override void LoadContent() {
UIManager.LoadAssets();
SoundManager.LoadSounds();
// spriteBatch = new SpriteBatch(GraphicsDevice);
// font = Content.Load<SpriteFont>("font");
}
protected override void Update(GameTime gameTime) {
@ -58,8 +48,6 @@ public class GameMain() : Core("OND", 1280, 720, false) {
spriteBatch.Begin(samplerState:SamplerState.PointClamp);
Screen.DrawCurrentAndOverlay(spriteBatch);
// Screen.CurrentScreen.Draw(spriteBatch);
// spriteBatch.DrawString(font, "Elapsed time: " + gameTime.TotalGameTime.Milliseconds, new Vector2(10, 10), Color.White);
spriteBatch.End();
base.Draw(gameTime);

View file

@ -1,30 +1,25 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.JavaScript;
using GlobalClassLib;
using ONDClient.GUI;
using PacketLib;
using ONDClient.Net;
namespace ONDClient.Map;
public class ClientMapManager {
public static class ClientMapManager {
private static MapTileProjection[,] map = new MapTileProjection[5, 5];
public static MapTileProjection Get((int x, int y) coords) => map[coords.x, coords.y];
public static MapTileProjection Get(int tileId) => Get(IdToCoords(tileId));
private static bool inverted;
public static void InitMap(int[] connectors, int[] yourTiles, int[] opponentTiles, int[] litTiles, bool upsideDown ) {
(int id1, int id2, ConnectorType type, ClientPlayer? owner)[] connectorsData = new (int, int , ConnectorType, ClientPlayer?)[connectors.Length / 4];
for (int i = 0; i < connectors.Length / 4; i++){
connectorsData[i] = (connectors[i * 4], connectors[i * 4 + 1], (ConnectorType)connectors[i * 4 + 2], connectors[i * 4 + 3] == -1 ? null : Client.GetPlayer(connectors[i * 4 + 3]));
}
// ClientMapManager.InitMap(upsideDown);
inverted = upsideDown;
IdToCoords = upsideDown ? _IdToCoordsInverse : _IdToCoords;
CoordsToId = upsideDown ? _CoordsToIdInverse : _CoordsToId;
@ -62,13 +57,11 @@ public class ClientMapManager {
public static void InitConnectors(TileConnectorProjection[] connectors) {
foreach (var con in connectors){
// (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 GetConnector((int, int) id) => Get(id.Item1).GetConnector(id.Item2);
public static TileConnectorProjection? GetConnector((int, int) id) => Get(id.Item1).GetConnector(id.Item2);
public static TileConnectorProjection[] GetConnectors(int tileId) => Get(IdToCoords(tileId)).GetAllConnectors();

View file

@ -1,11 +1,10 @@
#nullable enable
using GlobalClassLib;
using ONDClient.Net;
namespace ONDClient.Map;
public class MapTileProjection : GlobalMapTile<TileConnectorProjection, MapTileProjection> {
public class MapTileProjection(int id)
: GlobalMapTile<TileConnectorProjection, MapTileProjection>(id, ClientMapManager.IdToCoords(id)) {
public ClientPlayer? Owner { get; set; }
public MapTileProjection(int id) : base(id, ClientMapManager.IdToCoords(id)) {
Lit = false;
}
}

View file

@ -1,4 +1,6 @@
#nullable enable
using GlobalClassLib;
using ONDClient.Net;
namespace ONDClient.Map;

View file

@ -1,19 +1,14 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using ONDClient.GUI;
using GlobalClassLib;
using LiteNetLib;
using LiteNetLib.Utils;
using ONDClient.Map;
using PacketLib;
namespace ONDClient;
namespace ONDClient.Net;
public class Client {
public static class Client {
public enum ConnectionState {
IDLE,
CONNECTING,
@ -24,20 +19,22 @@ public class Client {
GAME_IN_PROGRESS,
ERROR
}
public static string StatusText{ get; private set; }
public static ConnectionState State { get; set; } = ConnectionState.IDLE;
public static ClientPlayer Player { get; } = new();
public static ClientPlayer Opponent { get; } = new();
private static EventBasedNetListener listener = new();
private static NetManager client;
private static NetPeer server;
private static NetDataWriter writer;
private static NetPacketProcessor processor;
public static ClientPlayer Player { get; } = new();
public static ClientPlayer Opponent { get; } = new();
public static ClientPlayer GetPlayer(int pid) => Player.state.pid == pid ? Player : Opponent;
public static ClientPlayer GetPlayer(int pid) => Player.State.Pid == pid ? Player : Opponent;
public static void Init() {
writer = new NetDataWriter();
@ -48,11 +45,7 @@ public class Client {
processor.SubscribeReusable<JoinAcceptPacket>(OnJoinAccept);
processor.SubscribeReusable<UpdatePlayerPacket>(OnPlayerUpdate);
processor.SubscribeReusable<MapInitPacket>(OnMapInit);
processor.SubscribeReusable<OpponentInitPacket>(packet => { // TODO: move this to a method
Opponent.state = packet.state;
Opponent.username = packet.username;
State = ConnectionState.GAME_STARTING;
});
processor.SubscribeReusable<OpponentInitPacket>(OnOpponentInit);
client = new NetManager(listener){
AutoRecycle = true
@ -66,7 +59,7 @@ public class Client {
Console.WriteLine("Connected to Server");
server = peer;
State = ConnectionState.CONNECTED;
SendPacket(new JoinPacket {username = Player.username == "" ? "Anonymous" : Player.username}, DeliveryMethod.ReliableOrdered);
SendPacket(new JoinPacket {Username = Player.Username == "" ? "Anonymous" : Player.Username}, DeliveryMethod.ReliableOrdered);
};
listener.PeerDisconnectedEvent += (peer, info) => {
@ -76,14 +69,14 @@ public class Client {
StatusText = info.Reason.ToString();
};
}
public static void Connect(string endPoint, int port) {
State = ConnectionState.CONNECTING;
client.Start();
Console.WriteLine($"Connecting to server @ {endPoint}:{port}");
new Thread(() => client.Connect(endPoint, port, "")).Start(); // TODO: figure out how keys work
new Thread(() => client.Connect(endPoint, port, "")).Start();
}
public static void SendPacket<T>(T packet, DeliveryMethod deliveryMethod) where T : class, new() {
@ -94,7 +87,7 @@ public class Client {
}
}
public static void SendCommands(PlayerCommand[] pCommands) {
SendPacket(new PlayerCommandPacket{commands = pCommands}, DeliveryMethod.ReliableOrdered);
SendPacket(new PlayerCommandPacket{Commands = pCommands}, DeliveryMethod.ReliableOrdered);
}
private static void OnNetworkReceive(NetPeer peer, NetPacketReader reader, DeliveryMethod deliveryMethod) {
@ -102,23 +95,17 @@ public class Client {
}
private static void OnJoinAccept(JoinAcceptPacket packet) {
Console.WriteLine($"Accepted by server, pid: {packet.state.pid}");
Console.WriteLine($"Accepted by server, pid: {packet.State.Pid}");
State = ConnectionState.ACCEPTED;
Player.state = packet.state;
Player.State = packet.State;
}
#nullable enable
private static void OnMapInit(MapInitPacket packet) {
ClientMapManager.InitMap(packet.Connectors, packet.YourTiles, packet.OpponentTiles, packet.LitTiles, packet.UpsideDown);
}
public static void Update() {
client.PollEvents();
}
public static void OnPlayerUpdate(UpdatePlayerPacket packet) { // TODO: move this to a separate class
EventProcessor.Evaluate(packet.events);
//Player.state = Player.state.pid == 0 ? packet.stateP1 : packet.stateP2;
public static void OnPlayerUpdate(UpdatePlayerPacket packet) {
EventProcessor.Evaluate(packet.Events);
}
public static void Disconnect() {
@ -130,7 +117,19 @@ public class Client {
public static void StartServer() {
Process.Start("ONDServer");
// new Thread(() => Server.Start(9012)).Start();
}
#nullable enable
private static void OnMapInit(MapInitPacket packet) {
ClientMapManager.InitMap(packet.Connectors, packet.YourTiles, packet.OpponentTiles, packet.LitTiles, packet.UpsideDown);
}
private static void OnOpponentInit(OpponentInitPacket packet) {
Opponent.State = packet.State;
Opponent.Username = packet.Username;
State = ConnectionState.GAME_STARTING;
}
}

View file

@ -0,0 +1,8 @@
using PacketLib;
namespace ONDClient.Net;
public class ClientPlayer {
public PlayerState State;
public string Username;
}

View file

@ -6,12 +6,13 @@ using Microsoft.Xna.Framework.Input;
using MonoGameLibrary.Input;
using ONDClient.GUI;
using ONDClient.Map;
using ONDClient.Sound;
using PacketLib;
namespace ONDClient;
namespace ONDClient.Net;
public class CommandManager {
private static (string label, Keys key, Action action)[] keybinds = [
public static class CommandManager {
private static readonly (string label, Keys key, Action action)[] keybinds = [
("Toggle camera", Keys.Space, SendToggleCamera),
("Toggle left door", Keys.A, ToggleDoorLeft),
("Toggle centre door", Keys.W, ToggleDoorCentre),
@ -20,20 +21,20 @@ public class CommandManager {
("Toggle light", Keys.F, ToggleLight)
];
private static InputListenerHook allControlsHook{ get; } = new(true);
private static InputListenerHook AllControlsHook{ get; } = new(true);
public static void InitInputListeners() {
Array.ForEach(keybinds, tuple => InputManager.AddListener(tuple.label, tuple.key, () => tuple.action(), InputTiming.PRESS, allControlsHook));
Array.ForEach(keybinds, tuple => InputManager.AddListener(tuple.label, tuple.key, () => tuple.action(), InputTiming.PRESS, AllControlsHook));
}
public static void AllowGameControls(bool state) {
allControlsHook.Enabled = state;
AllControlsHook.Enabled = state;
}
private static void SendToggleCamera() {
Client.Player.state.monitorUp = !Client.Player.state.monitorUp;
UIManager.ChangeMonitorState(Client.Player.state.monitorUp);
Client.SendCommands([PlayerCommand.SET_MONITOR(Client.Player.state.monitorUp)]);
Client.Player.State.MonitorUp = !Client.Player.State.MonitorUp;
UIManager.ChangeMonitorState(Client.Player.State.MonitorUp);
Client.SendCommands([PlayerCommand.SET_MONITOR(Client.Player.State.MonitorUp)]);
SoundManager.PlayMonitorFlip();
}
@ -44,7 +45,7 @@ public class CommandManager {
private static Dictionary<Direction, TileConnectorProjection> currentDoorBinds = new();
private static void ToggleLight() => SendToggleLight(Client.Player.state.camera, !ClientMapManager.Get(Client.Player.state.camera).Lit);
private static void ToggleLight() => SendToggleLight(Client.Player.State.Camera, !ClientMapManager.Get(Client.Player.State.Camera).Lit);
private static void SendToggleDoor(Direction direction) {
if (Screen.CurrentScreen.Label == UIManager.ScreenTypes.CAMERAS){
@ -53,10 +54,10 @@ public class CommandManager {
}
if (direction == Direction.SOUTH) return;
Client.Player.state.doorStates[(int)direction] = !Client.Player.state.doorStates[(int)direction];
UIManager.ChangeDoorState(direction, Client.Player.state.doorStates[(int)direction]);
SoundManager.PlayDoor(Client.Player.state.doorStates[(int)direction]);
Client.SendCommands([PlayerCommand.SET_DOOR_OFFICE(direction, Client.Player.state.doorStates[(int)direction])]);
Client.Player.State.DoorStates[(int)direction] = !Client.Player.State.DoorStates[(int)direction];
UIManager.ChangeDoorState(direction, Client.Player.State.DoorStates[(int)direction]);
SoundManager.PlayDoor(Client.Player.State.DoorStates[(int)direction]);
Client.SendCommands([PlayerCommand.SET_DOOR_OFFICE(direction, Client.Player.State.DoorStates[(int)direction])]);
}
private static void SendToggleRemoteDoor(Direction direction) {
@ -67,19 +68,17 @@ public class CommandManager {
Client.SendCommands([PlayerCommand.SET_DOOR_REMOTE(connector.Id, connector.Blocked)]);
SoundManager.PlayDoorRemote(connector.Blocked);
UIManager.ChangeRemoteDoorState(connector.Id, connector.Blocked);
// Console.WriteLine("Changed door state to: " + (connector.Blocked ? "blocked" : "open") + " for door " + connector);
// add UIManager toggle door
}
public static void SendChangeCamera(int id) {
if (id == Client.Player.state.officeTileId || id == Client.Opponent.state.officeTileId) return;
if (id == Client.Player.State.OfficeTileId || id == Client.Opponent.State.OfficeTileId) return;
Client.Player.state.camera = id;
Client.Player.State.Camera = id;
UIManager.ChangeCamera(id);
SoundManager.PlayCameraSwitch();
Client.SendCommands([PlayerCommand.SWITCH_CAM(id)]);
MapTileProjection tile = ClientMapManager.Get(id);
// add UIManager switch camera
currentDoorBinds.Clear();
foreach (var c in tile.GetAllConnectors().Where(c => c.Type == ConnectorType.DOOR_REMOTE)){
Direction dir = c.GetDirection(tile);
@ -89,7 +88,7 @@ public class CommandManager {
}
private static void SendToggleLight(int id, bool state) {
if(!Client.Player.state.monitorUp || ClientMapManager.Get(id).Owner != Client.Player) return;
if(!Client.Player.State.MonitorUp || ClientMapManager.Get(id).Owner != Client.Player) return;
ClientMapManager.Get(id).Lit = state;
UIManager.UpdateCameras([id]);
SoundManager.PlayLight(state);

View file

@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GlobalClassLib;
using ONDClient.Enemies;
using ONDClient.GUI;
using ONDClient.Map;
using ONDClient.Sound;
using PacketLib;
namespace ONDClient.Net;
public static class EventProcessor {
private static readonly Dictionary<int, Action<GameEvent>> eventHandlers = new(){
[0] = _ => PlayerJoin(),
[1] = _ => PlayerLeave(),
[2] = e => SwitchCam(e.Args[0], e.Args[1]),
[3] = e => ChangeMonitorState(e.Args[0], e.Args[1] == 1),
[4] = e => ChangeDoorStateOffice(e.Args[0], e.Args[1], e.Args[2] == 1),
[5] = e => ChangeDoorStateRemote(e.Args[0], (e.Args[1], e.Args[2]), e.Args[3] == 1),
[6] = e => SpawnEnemy((EnemyType)e.Args[0], e.Args[1], e.Args[3]),
[7] = e => MoveEnemy(e.Args[0], e.Args[1]),
[8] = e => EnemyAttack(e.Args[0], e.Args[1]),
[9] = e => EnemyReset(e.Args[0], e.Args[1]),
[10] = e => SpotSetActive(e.Args[0], e.Args[1] == 1),
[11] = e => PlayerWin(e.Args[0]),
[12] = _ => StartGame(),
[13] = e => PowerTick(e.Args[0], e.Args[1]),
[14] = e => PowerOut(e.Args[0]),
[15] = e => ChangeLightState(e.Args[0], e.Args[1], e.Args[2] == 1),
[16] = _ => NekoAnger(),
[17] = _ => VentWalk(),
[18] = _ => NextPhase()
};
public static void Evaluate(GameEvent[] events) {
foreach (var e in events){
eventHandlers[e.Id](e);
}
}
private static void PlayerJoin() {
Console.WriteLine("E: Player joined");
}
private static void PlayerLeave() {
Console.WriteLine("E: Player left");
}
private static void SwitchCam(int pid, int camId) {
if (Client.Player.State.Pid != pid){
UIManager.ChangeCameraOpponent(camId);
return;
}
if (Client.Player.State.Camera != camId) Console.WriteLine("!!! DESYNC: CAMERA STATE");
Console.WriteLine($"E: player {pid} switched to camera {camId}");
}
private static void ChangeMonitorState(int pid, bool state) {
Console.WriteLine($"E: Player {pid} toggled monitor {(state ? "off" : "on")}");
if (pid != Client.Player.State.Pid){
UIManager.ChangeMonitorStateOpponent(state);
return;
}
if (Client.Player.State.MonitorUp != state) Console.WriteLine("!!! DESYNC: MONITOR STATE");
}
private static void ChangeDoorStateOffice(int pid, int doorId, bool state) {
Console.WriteLine($"E: Player {pid} {(state ? "closed" : "opened")} door {doorId}");
if (pid == Client.Player.State.Pid){
if (Client.Player.State.DoorStates[doorId] != state) Console.WriteLine("!!! DESYNC: DOOR STATE");
return;
}
Client.Opponent.State.DoorStates[doorId] = state;
UIManager.ChangeDoorStateOpponent((Direction)doorId, state);
}
private static void ChangeDoorStateRemote(int pid, (int tile1, int tile2) connectorId, bool state) {
Console.WriteLine($"E: Player {pid} {(state ? "closed" : "opened")} door {connectorId.tile1}-{connectorId.tile2}");
TileConnectorProjection connector = ClientMapManager.GetConnector(connectorId);
if (connector == null){
Console.WriteLine($"!!! Connector {connectorId} not found");
return;
}
if (pid == Client.Player.State.Pid){
if (connector.Blocked != state) Console.WriteLine("!!! DESYNC: REMOTE DOOR STATE");
return;
}
connector.Blocked = state;
UIManager.ChangeRemoteDoorState(connectorId, state);
}
private static void SpawnEnemy(EnemyType type, int id, int camId) {
Console.WriteLine($"E: Spawned enemy {type} at {camId}");
ClientEnemyManager.AddEnemyByTemplate(type, id, ClientMapManager.Get(camId));
UIManager.UpdateCameras([camId]);
}
private static void MoveEnemy(int id, int camId) {
Console.WriteLine($"E: Enemy {id} moved to {camId}");
int oldPos = ClientEnemyManager.Get(id).Location!.Id;
ClientEnemyManager.Move(id, ClientMapManager.Get(camId));
UIManager.UpdateCameras([oldPos, camId]);
SoundManager.PlayEnemyMove(ClientEnemyManager.Get(id));
}
private static void EnemyAttack(int enemyId, int pid) {
Console.WriteLine($"E: Enemy {enemyId} attacked player {pid}");
if (pid == Client.Player.State.Pid) {
UIManager.Jumpscare(ClientEnemyManager.Get(pid));
SoundManager.PlayJumpscare();
}
}
private static void EnemyReset(int id, int camId) {
Console.WriteLine($"E: Enemy {id} reset to {camId}");
int preResetPos = ClientEnemyManager.Get(id).Location!.Id;
ClientEnemyManager.Move(id, ClientMapManager.Get(camId));
UIManager.UpdateCameras([preResetPos, camId]);
SoundManager.PlayEnemyReset(ClientEnemyManager.Get(id));
}
private static void SpotSetActive(int id, bool state) {
ClientEnemy enemy = ClientEnemyManager.Get(id);
if (enemy.Type != EnemyType.SPOT) return;
Console.WriteLine($"E: Spot:{id} turned {(state ? "on" : " off")}");
ClientEnemyManager.Get(id).Sprite.SetTexture(state ? 0 : 1);
SoundManager.PlaySpotActivate();
}
private static void PlayerWin(int pid) {
Console.WriteLine($"E: Player {pid} won");
if(Client.Player.State.Pid == pid) UIManager.ShowVictoryScreen();
Client.Disconnect();
ClientEnemyManager.ClearEnemies();
}
private static void StartGame() {
Console.WriteLine("E: Game started");
UIManager.DisplayGameUI();
UIManager.StartTimer();
SoundManager.StartAmbience();
Client.State = Client.ConnectionState.GAME_IN_PROGRESS;
}
private static void PowerTick(int pid, int value) {
// Console.WriteLine($"E: power tick {e.Args[0]}: {e.Args[1]}");
if (pid == Client.Player.State.Pid){
Client.Player.State.Power = value;
}
else{
Client.Opponent.State.Power = value;
}
}
private static void PowerOut(int pid) {
Console.WriteLine($"E: Player {pid} powered out");
ClientMapManager.GetAllConnectors().Where(c =>
(c.Type == ConnectorType.DOOR_REMOTE || c.Type == ConnectorType.DOOR_OFFICE) &&
c.Owner == Client.GetPlayer(pid)).ToList().ForEach(c =>
{
c.Blocked = false;
if(c.Type == ConnectorType.DOOR_REMOTE)
UIManager.ChangeRemoteDoorState(c.Id, false);
});
foreach (var tile in ClientMapManager.GetAllTiles()){
tile.Lit = false;
}
if (pid == Client.Player.State.Pid){
UIManager.ChangeDoorState(Direction.EAST, false);
UIManager.ChangeDoorState(Direction.NORTH, false);
UIManager.ChangeDoorState(Direction.WEST, false);
CommandManager.AllowGameControls(false);
UIManager.ChangeMonitorState(false);
SoundManager.PlayPowerOut();
}
else{
UIManager.ChangeDoorStateOpponent(Direction.EAST, false);
UIManager.ChangeDoorStateOpponent(Direction.NORTH, false);
UIManager.ChangeDoorStateOpponent(Direction.WEST, false);
UIManager.ChangeMonitorStateOpponent(false);
}
}
private static void ChangeLightState(int pid, int camId, bool state) {
Console.WriteLine($"E: Player {pid} {(state ? "lit": "unlit")} tile {camId}");
if (pid == Client.Player.State.Pid){
if (ClientMapManager.Get(camId).Lit != state) Console.WriteLine("!!! DESYNC: LIGHT STATE");
return;
}
ClientMapManager.Get(camId).Lit = state;
UIManager.UpdateCameras([camId]);
}
private static void NekoAnger() {
SoundManager.PlayNekoAnger();
}
private static void VentWalk() {
SoundManager.PlayVentWalk();
}
private static void NextPhase() {
SoundManager.PlayBell();
}
}

View file

@ -2,8 +2,9 @@ using System;
using GlobalClassLib;
using Microsoft.Xna.Framework.Audio;
using MonoGameLibrary;
using ONDClient.Enemies;
namespace ONDClient;
namespace ONDClient.Sound;
public static class SoundManager {
private static SoundEffect ambience;
@ -128,6 +129,7 @@ public static class SoundManager {
nekoPurrInstance.Stop();
}
// unused
private static SoundEffectInstance GetRandomisedPitchInstance(SoundEffect effect) {
SoundEffectInstance instance = effect.CreateInstance();
instance.Pitch = (float)(random.NextDouble() - 0.5) / 5;
@ -136,7 +138,7 @@ public static class SoundManager {
}
public static void PlayEnemyMove(ClientEnemy enemy) {
switch ((EnemyType)enemy.TypeId){
switch (enemy.Type){
case EnemyType.NEKO:
PlayNekoMove();
break;
@ -150,7 +152,7 @@ public static class SoundManager {
}
public static void PlayEnemyReset(ClientEnemy enemy) {
switch ((EnemyType)enemy.TypeId){
switch (enemy.Type){
case EnemyType.NEKO:
PlayNekoMove();
break;