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

@ -55,25 +55,22 @@ public class NekoEnemy : Enemy {
Server.SendUpdateToAll([GameEvent.NEKO_ANGERED(Location.Owner.state.pid, Id)]);
}
if (movementOpportunity.CheckAndRoll() || (Aggressive && GameLogic.NSecondUpdate)){
if (Target == null){
if (Server.P1.state.power > Server.P2.state.power){
Target = Server.P2;
}
else if (Server.P1.state.power < Server.P2.state.power){
Target = Server.P1;
}
else{
if (GameLogic.PhaseCounter > 1){
Target = Server.Players[new Random().Next(2)];
SetDifficulty(8);
}
else{
return;
}
if (Target == null){
if (Server.P1.state.power > Server.P2.state.power){
Target = Server.P2;
}
else if (Server.P1.state.power < Server.P2.state.power){
Target = Server.P1;
}
else{
if (GameLogic.PhaseCounter > 1){
Target = Server.Players[new Random().Next(2)];
SetDifficulty(8);
}
}
}
if (Target != null && (movementOpportunity.CheckAndRoll() || (Aggressive && GameLogic.NSecondUpdate))){
Pathfinder.Decision decision = pathfinder.DecideNext(MapManager.Get(Target.state.officeTileId));
switch (decision.type){
case Pathfinder.Decision.MoveType:

View file

@ -8,6 +8,14 @@
<LangVersion>14</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>../bin/Debug/Server</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>../bin/Server</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteNetLib" Version="1.3.1" />
</ItemGroup>

View file

@ -11,7 +11,7 @@ public class GameLogic {
public const int START_CAMERA = 12;
private static MovementOpportunity secondCycleTimer = new(1000);
private static MovementOpportunity gamePhaseTimer = new(30_000);
private static MovementOpportunity gamePhaseTimer = new(60_000);
public static bool NSecondUpdate{ get; private set; } = false;
public static MapTile P1Office;
@ -84,7 +84,7 @@ public class GameLogic {
// EnemyManager.AddEnemy(new MareEnemy(10)).Spawn(MapManager.Get(22));
EnemyManager.AddEnemy(new LurkEnemy(4)).Spawn(MapManager.Get(2));
EnemyManager.AddEnemy(new NekoEnemy(4)).Spawn(MapManager.Get(22));
EnemyManager.AddEnemy(new NekoEnemy(4)).Spawn(MapManager.Get(0));
}
public static void Update() {
@ -122,9 +122,16 @@ public class GameLogic {
}
public static void DeclareWinner(ServerPlayer player) {
Server.SendUpdateToAll([GameEvent.PLAYER_WIN(player.state.pid)]);
if (Server.Players.Count == 2){
Server.SendUpdateToAll([GameEvent.PLAYER_WIN(player.state.pid)]);
Console.WriteLine("Player " + player.state.pid + " won!");
}
Thread.Sleep(1000);
Server.Stop();
Console.WriteLine("Player " + player.state.pid + " won!");
if (!Server.AutoStop){
Program.Restart();
}
}
private static (int p1Usage, int p2Usage) CalculatePowerUsage() {
@ -158,7 +165,7 @@ public class GameLogic {
PhaseCounter++;
Server.SendUpdateToAll([GameEvent.NEXT_PHASE()]);
int roll = random.Next(3);
int roll = spawnOrder.Count > 0 ? random.Next(3) : random.Next(1,3);
switch (roll){
case 0:
int spawnRoll = random.Next(spawnOrder[0].Count);

View file

@ -1,9 +1,18 @@
using System.Security.Cryptography.X509Certificates;
using System.Diagnostics;
namespace FNAF_Server;
public class Program {
public static void Main(string[] args) {
if (args.Contains("--persistent")){
Server.AutoStop = false;
}
Server.Start(9012);
}
public static void Restart() {
Console.WriteLine("Game concluded -> restarting server");
Process.Start(new ProcessStartInfo{ FileName = Environment.ProcessPath, UseShellExecute = false });
Environment.Exit(0);
}
}

View file

@ -15,6 +15,8 @@ public class Server {
public static ServerPlayer P2;
public static readonly Dictionary<int, ServerPlayer> Players = new();
public static ServerPlayer OtherPlayer(ServerPlayer player) => player.state.pid == P1.state.pid ? P2 : P1;
public static bool AutoStop{ get; set; } = true;
private static EventBasedNetListener listener;
private static NetManager server;
@ -138,6 +140,8 @@ public class Server {
}
public static void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) {
GameLogic.DeclareWinner(OtherPlayer(Players.Values.First(p => Equals(p.peer, peer))));
if (peer.Tag != null) {
Players.Remove(peer.Id);
}
@ -193,6 +197,7 @@ public class Server {
public static void Stop()
{
isRunning = false;
server.Stop();
}
}