Přidán build script, předělaná struktura, funkční spouštění serveru z clienta. Client je schopen fungovat po více her bez restartu. Bugfixy

This commit is contained in:
Perry 2026-03-21 21:23:33 +01:00
parent c942d23a87
commit 1a27dd6fab
22 changed files with 269 additions and 136 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
@ -37,9 +38,8 @@ public class Client {
public static ClientPlayer Opponent { get; } = new();
public static ClientPlayer GetPlayer(int pid) => Player.state.pid == pid ? Player : Opponent;
public static void Connect(string endPoint, int port) {
State = ConnectionState.CONNECTING;
public static void Init() {
writer = new NetDataWriter();
processor = new NetPacketProcessor();
@ -57,9 +57,6 @@ public class Client {
client = new NetManager(listener){
AutoRecycle = true
};
client.Start();
Console.WriteLine($"Connecting to server @ {endPoint}:{port}");
listener.NetworkReceiveEvent += (peer, reader, channel, method) => {
OnNetworkReceive(peer, reader, method);
@ -71,6 +68,13 @@ public class Client {
State = ConnectionState.CONNECTED;
SendPacket(new JoinPacket {username = Player.username == "" ? "Anonymous" : Player.username}, DeliveryMethod.ReliableOrdered);
};
}
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
}
@ -108,9 +112,18 @@ public class Client {
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 Disconnect() {
if (client == null || client.ConnectedPeersCount == 0) return;
client.DisconnectAll();
client.Stop();
State = ConnectionState.IDLE;
}
public static void StartServer() {
Process.Start("FNAF_Server");
// new Thread(() => Server.Start(9012)).Start();
}
}

View file

@ -99,4 +99,8 @@ public static class ClientEnemyManager {
return output.ToArray();
}
public static void ClearEnemies() {
enemies.Clear();
}
}

View file

@ -105,6 +105,8 @@ public class EventProcessor {
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

View file

@ -1,52 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<LangVersion>14</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<None Remove="Icon.ico"/>
<None Remove="Icon.bmp"/>
<None Remove="Input\**" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico">
<LogicalName>Icon.ico</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Icon.bmp">
<LogicalName>Icon.bmp</LogicalName>
</EmbeddedResource>
<EmbeddedResource Remove="Input\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="LiteNetLib" Version="1.3.1" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.*"/>
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.*"/>
</ItemGroup>
<ItemGroup>
<Folder Include="lib\" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGameLibrary">
<HintPath>lib\MonoGameLibrary.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GlobalClassLib\GlobalClassLib.csproj" />
<ProjectReference Include="..\PacketLib\PacketLib.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Remove="Input\**" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="CollectPackageReferences">
<Message Text="Restoring dotnet tools (this might take a while depending on your internet speed and should only happen upon building your project for the first time, or after upgrading MonoGame, or clearing your nuget cache)" Importance="High"/>
<Exec Command="dotnet tool restore"/>
</Target>
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<RollForward>Major</RollForward>
<PublishReadyToRun>false</PublishReadyToRun>
<TieredCompilation>false</TieredCompilation>
<LangVersion>14</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>../bin/Client/</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>../bin/Debug/Client/</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove="Icon.ico"/>
<None Remove="Icon.bmp"/>
<None Remove="Input\**" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Icon.ico">
<LogicalName>Icon.ico</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="Icon.bmp">
<LogicalName>Icon.bmp</LogicalName>
</EmbeddedResource>
<EmbeddedResource Remove="Input\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="LiteNetLib" Version="1.3.1" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.*"/>
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.*"/>
</ItemGroup>
<ItemGroup>
<Folder Include="link\" />
</ItemGroup>
<ItemGroup>
<Reference Include="MonoGameLibrary">
<HintPath>link\MonoGameLibrary.dll</HintPath>
</Reference>
<!-- <Reference Include="FNAF_Server">-->
<!-- <HintPath>link\FNAF_Server.dll</HintPath>-->
<!-- </Reference>-->
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GlobalClassLib\GlobalClassLib.csproj" />
<ProjectReference Include="..\PacketLib\PacketLib.csproj" />
<!-- <ProjectReference Include="..\FNAF_Server\FNAF_Server.csproj" />-->
</ItemGroup>
<ItemGroup>
<Compile Remove="Input\**" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="CollectPackageReferences">
<Message Text="Restoring dotnet tools (this might take a while depending on your internet speed and should only happen upon building your project for the first time, or after upgrading MonoGame, or clearing your nuget cache)" Importance="High"/>
<Exec Command="dotnet tool restore"/>
</Target>
</Project>

View file

@ -73,6 +73,8 @@ public class Screen {
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;
@ -87,7 +89,7 @@ public class Screen {
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) {
public UIElement AddElement(string id, UIElement element, bool temporary = false) {
elements.Add(id, element);
int insertIndex = elementsInDrawOrder.FindLastIndex(e => e.DrawPriority == element.DrawPriority);
@ -97,10 +99,25 @@ public class Screen {
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;

View file

@ -22,7 +22,7 @@ public class TimerUIElement : TextUIElement{
}
public void Start() {
stopwatch.Start();
stopwatch.Restart();
}
public void Stop() {

View file

@ -88,6 +88,14 @@ public class UIManager {
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);
@ -119,20 +127,70 @@ public class UIManager {
}
});
menuScreen.AddElement("host-button",
new TextUIElement(new(connectButton.Bounds.Item1.X, connectButton.Bounds.Item2.Y + 30), PixelMonoFont) {Text = "HOST"});
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);
@ -145,53 +203,10 @@ public class UIManager {
timerElement.Start();
}
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))});
lightIndicators.Add(id, monitorScreen.AddElement($"light{id}", new UIElement(MonitorAtlas["map-light-indicator"], point1){Visible = false}));
//
// 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));
monitorScreen.AddElement("eye-opponent", new UIElement([MonitorAtlas["eye-small-opponent-closed"], MonitorAtlas["eye-small-opponent-open"]], monitorScreen["room"+Client.Opponent.state.camera].Bounds.Item1));
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));
}
}
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)));
}
public static void AddEnemySprite(int id, UIElement sprite, UIElement jumpscareSprite = null) {
monitorScreen.AddElement($"enemy{id}", sprite);
monitorScreen.AddElement($"enemy{id}", sprite, true);
if (jumpscareSprite != null){
officeScreen.AddElement($"enemy{id}-jumpscare", jumpscareSprite);
officeScreen.AddElement($"enemy{id}-jumpscare", jumpscareSprite, true);
jumpscareSprite.Active = false;
jumpscareSprite.Visible = false;
}
@ -314,6 +329,16 @@ public class UIManager {
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));

View file

@ -16,6 +16,10 @@ public class GameMain() : Core("fnafkooo", 1280, 720, false) {
protected override void Initialize() {
Exiting += (_, _) => {
Client.Disconnect();
};
// Client.Connect("127.0.0.1", 9012);
CommandManager.InitInputListeners();
@ -23,6 +27,7 @@ public class GameMain() : Core("fnafkooo", 1280, 720, false) {
base.Initialize();
Client.Init();
UIManager.InitUI();
UIManager.DisplayMainMenu();
}
@ -35,9 +40,9 @@ public class GameMain() : Core("fnafkooo", 1280, 720, false) {
}
protected override void Update(GameTime gameTime) {
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
Keyboard.GetState().IsKeyDown(Keys.Escape))
if (Keyboard.GetState().IsKeyDown(Keys.Escape)){
Exit();
}
InputManager.NextInputCycle();
Screen.UpdateAll();
@ -59,4 +64,6 @@ public class GameMain() : Core("fnafkooo", 1280, 720, false) {
spriteBatch.End();
base.Draw(gameTime);
}
}

View file

@ -1,2 +1,5 @@
using var game = new FNAF_Clone.GameMain();
using System;
using var game = new FNAF_Clone.GameMain();
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
game.Run();

View file

@ -1 +0,0 @@
../../MonoGameLibrary/MonoGameLibrary/bin/Debug/net8.0/MonoGameLibrary.dll

View file

@ -0,0 +1 @@
../../MonoGameLibrary/MonoGameLibrary/bin/Release/net9.0/publish/MonoGameLibrary.dll