1005 lines
31 KiB
C#
1005 lines
31 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Numerics;
|
|
|
|
namespace VoidScavengers.Game;
|
|
|
|
/// <summary>
|
|
/// Core game simulation for a single match instance
|
|
/// </summary>
|
|
public sealed class GameSimulation
|
|
{
|
|
public string MatchId { get; }
|
|
public int MapSeed { get; }
|
|
public float MapWidth { get; }
|
|
public float MapHeight { get; }
|
|
public long CurrentTick { get; private set; }
|
|
public GamePhase Phase { get; private set; } = GamePhase.WaitingForPlayers;
|
|
public float TimeRemaining { get; private set; }
|
|
|
|
private readonly Random _rng;
|
|
private readonly Dictionary<ulong, PlayerEntity> _players = new();
|
|
private readonly Dictionary<int, Asteroid> _asteroids = new();
|
|
private readonly Dictionary<int, ResourceNode> _resources = new();
|
|
private readonly Dictionary<int, DroppedItem> _droppedItems = new();
|
|
private readonly Dictionary<int, Projectile> _projectiles = new();
|
|
private readonly Dictionary<int, ExtractionZone> _extractionZones = new();
|
|
|
|
private readonly ConcurrentQueue<PlayerInput> _inputQueue = new();
|
|
private readonly List<GameEvent> _pendingEvents = new();
|
|
|
|
private int _nextEntityId = 1;
|
|
private int _nextProjectileId = 1;
|
|
|
|
// Game constants
|
|
public const float TickRate = 20f; // 20 ticks per second
|
|
public const float TickDelta = 1f / TickRate;
|
|
public const float MatchDuration = 300f; // 5 minutes
|
|
public const float PlayerSpeed = 120f;
|
|
public const float PlayerRadius = 12f;
|
|
public const int StartingHealth = 100;
|
|
public const float PickupRadius = 24f;
|
|
public const float ExtractionTime = 5f; // Seconds to extract
|
|
|
|
public GameSimulation(string matchId, int seed, float width = 2000f, float height = 2000f)
|
|
{
|
|
MatchId = matchId;
|
|
MapSeed = seed;
|
|
MapWidth = width;
|
|
MapHeight = height;
|
|
_rng = new Random(seed);
|
|
TimeRemaining = MatchDuration;
|
|
|
|
GenerateMap();
|
|
}
|
|
|
|
private void GenerateMap()
|
|
{
|
|
// Generate asteroids (obstacles)
|
|
int asteroidCount = 40 + _rng.Next(20);
|
|
for (int i = 0; i < asteroidCount; i++)
|
|
{
|
|
var asteroid = new Asteroid
|
|
{
|
|
Id = _nextEntityId++,
|
|
X = 100 + (float)_rng.NextDouble() * (MapWidth - 200),
|
|
Y = 100 + (float)_rng.NextDouble() * (MapHeight - 200),
|
|
Radius = 30 + (float)_rng.NextDouble() * 50,
|
|
Type = _rng.Next(3)
|
|
};
|
|
_asteroids[asteroid.Id] = asteroid;
|
|
}
|
|
|
|
// Generate resource nodes
|
|
int resourceCount = 60 + _rng.Next(30);
|
|
for (int i = 0; i < resourceCount; i++)
|
|
{
|
|
var typeRoll = _rng.NextDouble();
|
|
int resourceType = typeRoll < 0.6 ? 0 : (typeRoll < 0.9 ? 1 : 2); // 60% scrap, 30% energy, 10% rare
|
|
|
|
var resource = new ResourceNode
|
|
{
|
|
Id = _nextEntityId++,
|
|
X = 50 + (float)_rng.NextDouble() * (MapWidth - 100),
|
|
Y = 50 + (float)_rng.NextDouble() * (MapHeight - 100),
|
|
ResourceType = resourceType,
|
|
Amount = resourceType switch
|
|
{
|
|
0 => 3 + _rng.Next(5), // Scrap: 3-7
|
|
1 => 2 + _rng.Next(3), // Energy: 2-4
|
|
2 => 1 + _rng.Next(2), // Rare: 1-2
|
|
_ => 1
|
|
},
|
|
MaxAmount = 0 // Set below
|
|
};
|
|
resource.MaxAmount = resource.Amount;
|
|
_resources[resource.Id] = resource;
|
|
}
|
|
|
|
// Generate extraction zones (3-4 around map edges)
|
|
var zonePositions = new[]
|
|
{
|
|
(MapWidth * 0.1f, MapHeight * 0.5f),
|
|
(MapWidth * 0.9f, MapHeight * 0.5f),
|
|
(MapWidth * 0.5f, MapHeight * 0.1f),
|
|
(MapWidth * 0.5f, MapHeight * 0.9f)
|
|
};
|
|
|
|
for (int i = 0; i < zonePositions.Length; i++)
|
|
{
|
|
var zone = new ExtractionZone
|
|
{
|
|
Id = _nextEntityId++,
|
|
X = zonePositions[i].Item1,
|
|
Y = zonePositions[i].Item2,
|
|
Radius = 60f,
|
|
Active = i == 0, // First zone active at start
|
|
ActivatesAtTick = i == 0 ? 0 : (long)(TickRate * 60 * (i + 1)) // Stagger activation
|
|
};
|
|
_extractionZones[zone.Id] = zone;
|
|
}
|
|
}
|
|
|
|
public Vector2 GetSpawnPosition(int playerIndex, int totalPlayers)
|
|
{
|
|
// Spawn players in a circle around the center
|
|
float angle = (float)(2 * Math.PI * playerIndex / Math.Max(totalPlayers, 1));
|
|
float spawnRadius = Math.Min(MapWidth, MapHeight) * 0.35f;
|
|
float centerX = MapWidth / 2;
|
|
float centerY = MapHeight / 2;
|
|
|
|
return new Vector2(
|
|
centerX + (float)Math.Cos(angle) * spawnRadius,
|
|
centerY + (float)Math.Sin(angle) * spawnRadius
|
|
);
|
|
}
|
|
|
|
public PlayerEntity AddPlayer(ulong playerId, string name, Vector2 position)
|
|
{
|
|
var player = new PlayerEntity
|
|
{
|
|
Id = playerId,
|
|
Name = name,
|
|
X = position.X,
|
|
Y = position.Y,
|
|
Rotation = (float)_rng.NextDouble() * MathF.PI * 2,
|
|
Health = StartingHealth,
|
|
MaxHealth = StartingHealth,
|
|
Speed = PlayerSpeed,
|
|
Alive = true,
|
|
Scrap = 0,
|
|
Energy = 0,
|
|
Rare = 0,
|
|
EquippedWeaponId = null,
|
|
Inventory = new int?[6] // 6 inventory slots
|
|
};
|
|
_players[playerId] = player;
|
|
return player;
|
|
}
|
|
|
|
public void RemovePlayer(ulong playerId)
|
|
{
|
|
if (_players.TryGetValue(playerId, out var player))
|
|
{
|
|
if (player.Alive)
|
|
{
|
|
// Drop all items
|
|
DropPlayerLoot(player);
|
|
}
|
|
_players.Remove(playerId);
|
|
}
|
|
}
|
|
|
|
public void EnqueueInput(ulong playerId, PlayerInput input)
|
|
{
|
|
input.PlayerId = playerId;
|
|
_inputQueue.Enqueue(input);
|
|
}
|
|
|
|
public void StartGame()
|
|
{
|
|
Phase = GamePhase.InProgress;
|
|
CurrentTick = 0;
|
|
TimeRemaining = MatchDuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Advance simulation by one tick. Returns events that occurred.
|
|
/// </summary>
|
|
public List<GameEvent> Tick()
|
|
{
|
|
if (Phase != GamePhase.InProgress) return new List<GameEvent>();
|
|
|
|
_pendingEvents.Clear();
|
|
CurrentTick++;
|
|
TimeRemaining -= TickDelta;
|
|
|
|
// Process all queued inputs
|
|
while (_inputQueue.TryDequeue(out var input))
|
|
{
|
|
ProcessInput(input);
|
|
}
|
|
|
|
// Update all entities
|
|
UpdatePlayers();
|
|
UpdateProjectiles();
|
|
UpdateExtractionZones();
|
|
|
|
// Check win conditions
|
|
CheckEndConditions();
|
|
|
|
return new List<GameEvent>(_pendingEvents);
|
|
}
|
|
|
|
private void ProcessInput(PlayerInput input)
|
|
{
|
|
if (!_players.TryGetValue(input.PlayerId, out var player) || !player.Alive)
|
|
return;
|
|
|
|
switch (input.Type)
|
|
{
|
|
case InputType.Move:
|
|
player.InputDx = Math.Clamp(input.Dx, -1f, 1f);
|
|
player.InputDy = Math.Clamp(input.Dy, -1f, 1f);
|
|
player.Rotation = input.AimAngle;
|
|
break;
|
|
|
|
case InputType.Shoot:
|
|
TryShoot(player, input.AimAngle);
|
|
break;
|
|
|
|
case InputType.Action:
|
|
TryAction(player, input.TargetId);
|
|
break;
|
|
|
|
case InputType.Craft:
|
|
TryCraft(player, input.TargetId ?? 0);
|
|
break;
|
|
|
|
case InputType.Drop:
|
|
TryDrop(player, input.TargetId ?? 0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void UpdatePlayers()
|
|
{
|
|
foreach (var player in _players.Values)
|
|
{
|
|
if (!player.Alive) continue;
|
|
|
|
// Apply movement
|
|
float dx = player.InputDx * player.Speed * TickDelta;
|
|
float dy = player.InputDy * player.Speed * TickDelta;
|
|
|
|
float newX = player.X + dx;
|
|
float newY = player.Y + dy;
|
|
|
|
// Collision with asteroids
|
|
bool blocked = false;
|
|
foreach (var asteroid in _asteroids.Values)
|
|
{
|
|
float distSq = (newX - asteroid.X) * (newX - asteroid.X) +
|
|
(newY - asteroid.Y) * (newY - asteroid.Y);
|
|
float minDist = PlayerRadius + asteroid.Radius;
|
|
if (distSq < minDist * minDist)
|
|
{
|
|
blocked = true;
|
|
// Push out of collision
|
|
float dist = MathF.Sqrt(distSq);
|
|
if (dist > 0.001f)
|
|
{
|
|
float overlap = minDist - dist;
|
|
newX += (newX - asteroid.X) / dist * overlap;
|
|
newY += (newY - asteroid.Y) / dist * overlap;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Clamp to map bounds
|
|
newX = Math.Clamp(newX, PlayerRadius, MapWidth - PlayerRadius);
|
|
newY = Math.Clamp(newY, PlayerRadius, MapHeight - PlayerRadius);
|
|
|
|
player.X = newX;
|
|
player.Y = newY;
|
|
|
|
// Check resource pickup
|
|
CheckResourcePickup(player);
|
|
|
|
// Check dropped item pickup
|
|
CheckDroppedItemPickup(player);
|
|
|
|
// Update extraction progress
|
|
UpdateExtraction(player);
|
|
|
|
// Clear input for next tick
|
|
player.InputDx = 0;
|
|
player.InputDy = 0;
|
|
}
|
|
}
|
|
|
|
private void CheckResourcePickup(PlayerEntity player)
|
|
{
|
|
foreach (var resource in _resources.Values.ToList())
|
|
{
|
|
if (resource.Amount <= 0) continue;
|
|
|
|
float distSq = (player.X - resource.X) * (player.X - resource.X) +
|
|
(player.Y - resource.Y) * (player.Y - resource.Y);
|
|
|
|
if (distSq < PickupRadius * PickupRadius)
|
|
{
|
|
int amount = resource.Amount;
|
|
switch (resource.ResourceType)
|
|
{
|
|
case 0: player.Scrap += amount; break;
|
|
case 1: player.Energy += amount; break;
|
|
case 2: player.Rare += amount; break;
|
|
}
|
|
|
|
resource.Amount = 0;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Pickup,
|
|
PlayerId = player.Id,
|
|
TargetId = resource.Id,
|
|
Data = new { resourceType = resource.ResourceType, amount }
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CheckDroppedItemPickup(PlayerEntity player)
|
|
{
|
|
foreach (var drop in _droppedItems.Values.ToList())
|
|
{
|
|
float distSq = (player.X - drop.X) * (player.X - drop.X) +
|
|
(player.Y - drop.Y) * (player.Y - drop.Y);
|
|
|
|
if (distSq < PickupRadius * PickupRadius)
|
|
{
|
|
// Try to add to inventory
|
|
if (TryAddToInventory(player, drop.ItemId, drop.Quantity))
|
|
{
|
|
_droppedItems.Remove(drop.Id);
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Pickup,
|
|
PlayerId = player.Id,
|
|
TargetId = drop.Id,
|
|
Data = new { itemId = drop.ItemId, quantity = drop.Quantity }
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private bool TryAddToInventory(PlayerEntity player, int itemId, int quantity)
|
|
{
|
|
var itemDef = ItemDatabase.Get(itemId);
|
|
if (itemDef == null) return false;
|
|
|
|
// For weapons/tools/armor, find empty slot
|
|
if (!itemDef.Stackable)
|
|
{
|
|
for (int i = 0; i < player.Inventory.Length; i++)
|
|
{
|
|
if (player.Inventory[i] == null)
|
|
{
|
|
player.Inventory[i] = itemId;
|
|
// Auto-equip weapon if none equipped
|
|
if (itemDef.Type == ItemType.Weapon && player.EquippedWeaponId == null)
|
|
{
|
|
player.EquippedWeaponId = itemId;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
return false; // No room
|
|
}
|
|
|
|
// Resources are stored separately, always accepted
|
|
return true;
|
|
}
|
|
|
|
private void UpdateExtraction(PlayerEntity player)
|
|
{
|
|
bool inZone = false;
|
|
foreach (var zone in _extractionZones.Values)
|
|
{
|
|
if (!zone.Active) continue;
|
|
|
|
float distSq = (player.X - zone.X) * (player.X - zone.X) +
|
|
(player.Y - zone.Y) * (player.Y - zone.Y);
|
|
|
|
if (distSq < zone.Radius * zone.Radius)
|
|
{
|
|
inZone = true;
|
|
player.ExtractionProgress += TickDelta;
|
|
|
|
if (player.ExtractionProgress >= ExtractionTime)
|
|
{
|
|
// Player extracts!
|
|
ExtractPlayer(player);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!inZone)
|
|
{
|
|
player.ExtractionProgress = 0;
|
|
}
|
|
}
|
|
|
|
private void ExtractPlayer(PlayerEntity player)
|
|
{
|
|
player.Alive = false;
|
|
player.Extracted = true;
|
|
|
|
// Calculate total loot value
|
|
int totalValue = player.Scrap * 1 + player.Energy * 2 + player.Rare * 5;
|
|
foreach (var itemId in player.Inventory)
|
|
{
|
|
if (itemId.HasValue)
|
|
{
|
|
var item = ItemDatabase.Get(itemId.Value);
|
|
if (item != null) totalValue += item.LootValue;
|
|
}
|
|
}
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Extract,
|
|
PlayerId = player.Id,
|
|
Data = new { playerName = player.Name, totalValue }
|
|
});
|
|
}
|
|
|
|
private void TryShoot(PlayerEntity player, float angle)
|
|
{
|
|
if (player.EquippedWeaponId == null) return;
|
|
var weapon = ItemDatabase.Get(player.EquippedWeaponId.Value);
|
|
if (weapon == null || weapon.Type != ItemType.Weapon) return;
|
|
|
|
// Check fire rate cooldown
|
|
float cooldown = 1f / weapon.FireRate;
|
|
if (CurrentTick - player.LastShotTick < cooldown * TickRate) return;
|
|
|
|
player.LastShotTick = CurrentTick;
|
|
|
|
// Create projectile(s)
|
|
int pellets = weapon.PelletCount;
|
|
for (int i = 0; i < pellets; i++)
|
|
{
|
|
float spread = pellets > 1 ? (float)(_rng.NextDouble() - 0.5) * weapon.Spread : 0;
|
|
float finalAngle = angle + spread;
|
|
|
|
var proj = new Projectile
|
|
{
|
|
Id = _nextProjectileId++,
|
|
OwnerId = player.Id,
|
|
X = player.X,
|
|
Y = player.Y,
|
|
VelX = MathF.Cos(finalAngle) * weapon.ProjectileSpeed,
|
|
VelY = MathF.Sin(finalAngle) * weapon.ProjectileSpeed,
|
|
Damage = weapon.Damage,
|
|
Range = weapon.Range,
|
|
DistanceTraveled = 0,
|
|
ExplosionRadius = weapon.ExplosionRadius,
|
|
WeaponId = weapon.Id
|
|
};
|
|
_projectiles[proj.Id] = proj;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Projectile,
|
|
Data = new { proj.Id, proj.OwnerId, proj.X, proj.Y, proj.VelX, proj.VelY, weapon.Id }
|
|
});
|
|
}
|
|
}
|
|
|
|
private void UpdateProjectiles()
|
|
{
|
|
foreach (var proj in _projectiles.Values.ToList())
|
|
{
|
|
float dx = proj.VelX * TickDelta;
|
|
float dy = proj.VelY * TickDelta;
|
|
float moveDist = MathF.Sqrt(dx * dx + dy * dy);
|
|
|
|
proj.X += dx;
|
|
proj.Y += dy;
|
|
proj.DistanceTraveled += moveDist;
|
|
|
|
// Check range
|
|
if (proj.DistanceTraveled > proj.Range)
|
|
{
|
|
if (proj.ExplosionRadius > 0)
|
|
{
|
|
CreateExplosion(proj.X, proj.Y, proj.ExplosionRadius, proj.Damage, proj.OwnerId);
|
|
}
|
|
_projectiles.Remove(proj.Id);
|
|
continue;
|
|
}
|
|
|
|
// Check asteroid collision
|
|
bool hitAsteroid = false;
|
|
foreach (var asteroid in _asteroids.Values)
|
|
{
|
|
float distSq = (proj.X - asteroid.X) * (proj.X - asteroid.X) +
|
|
(proj.Y - asteroid.Y) * (proj.Y - asteroid.Y);
|
|
if (distSq < asteroid.Radius * asteroid.Radius)
|
|
{
|
|
hitAsteroid = true;
|
|
if (proj.ExplosionRadius > 0)
|
|
{
|
|
CreateExplosion(proj.X, proj.Y, proj.ExplosionRadius, proj.Damage, proj.OwnerId);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if (hitAsteroid)
|
|
{
|
|
_projectiles.Remove(proj.Id);
|
|
continue;
|
|
}
|
|
|
|
// Check player collision
|
|
foreach (var target in _players.Values)
|
|
{
|
|
if (target.Id == proj.OwnerId || !target.Alive) continue;
|
|
|
|
float distSq = (proj.X - target.X) * (proj.X - target.X) +
|
|
(proj.Y - target.Y) * (proj.Y - target.Y);
|
|
|
|
if (distSq < PlayerRadius * PlayerRadius)
|
|
{
|
|
if (proj.ExplosionRadius > 0)
|
|
{
|
|
CreateExplosion(proj.X, proj.Y, proj.ExplosionRadius, proj.Damage, proj.OwnerId);
|
|
}
|
|
else
|
|
{
|
|
DealDamage(target, proj.Damage, proj.OwnerId);
|
|
}
|
|
_projectiles.Remove(proj.Id);
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Out of bounds
|
|
if (proj.X < 0 || proj.X > MapWidth || proj.Y < 0 || proj.Y > MapHeight)
|
|
{
|
|
_projectiles.Remove(proj.Id);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CreateExplosion(float x, float y, float radius, int damage, ulong ownerId)
|
|
{
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Explosion,
|
|
Data = new { x, y, radius }
|
|
});
|
|
|
|
foreach (var target in _players.Values)
|
|
{
|
|
if (!target.Alive) continue;
|
|
|
|
float distSq = (x - target.X) * (x - target.X) + (y - target.Y) * (y - target.Y);
|
|
if (distSq < radius * radius)
|
|
{
|
|
// Damage falloff based on distance
|
|
float dist = MathF.Sqrt(distSq);
|
|
float falloff = 1f - (dist / radius);
|
|
int actualDamage = (int)(damage * falloff);
|
|
if (actualDamage > 0)
|
|
{
|
|
DealDamage(target, actualDamage, ownerId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DealDamage(PlayerEntity target, int damage, ulong? sourceId)
|
|
{
|
|
target.Health -= damage;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Damage,
|
|
PlayerId = target.Id,
|
|
SourceId = sourceId,
|
|
Data = new { damage, newHealth = target.Health }
|
|
});
|
|
|
|
if (target.Health <= 0)
|
|
{
|
|
KillPlayer(target, sourceId);
|
|
}
|
|
}
|
|
|
|
private void KillPlayer(PlayerEntity player, ulong? killerId)
|
|
{
|
|
player.Alive = false;
|
|
player.Health = 0;
|
|
|
|
var drops = DropPlayerLoot(player);
|
|
|
|
string? killerName = null;
|
|
if (killerId.HasValue && _players.TryGetValue(killerId.Value, out var killer))
|
|
{
|
|
killerName = killer.Name;
|
|
killer.Kills++;
|
|
}
|
|
player.Deaths++;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Death,
|
|
PlayerId = player.Id,
|
|
SourceId = killerId,
|
|
Data = new
|
|
{
|
|
playerName = player.Name,
|
|
killerName,
|
|
drops = drops.Select(d => new { d.Id, d.X, d.Y, d.ItemId, d.Quantity }).ToArray()
|
|
}
|
|
});
|
|
}
|
|
|
|
private List<DroppedItem> DropPlayerLoot(PlayerEntity player)
|
|
{
|
|
var drops = new List<DroppedItem>();
|
|
float dropAngle = 0;
|
|
float dropRadius = 30f;
|
|
|
|
// Drop resources as items
|
|
if (player.Scrap > 0)
|
|
{
|
|
drops.Add(CreateDrop(player.X, player.Y, 1, player.Scrap, ref dropAngle, dropRadius));
|
|
}
|
|
if (player.Energy > 0)
|
|
{
|
|
drops.Add(CreateDrop(player.X, player.Y, 2, player.Energy, ref dropAngle, dropRadius));
|
|
}
|
|
if (player.Rare > 0)
|
|
{
|
|
drops.Add(CreateDrop(player.X, player.Y, 3, player.Rare, ref dropAngle, dropRadius));
|
|
}
|
|
|
|
// Drop inventory items
|
|
foreach (var itemId in player.Inventory)
|
|
{
|
|
if (itemId.HasValue)
|
|
{
|
|
drops.Add(CreateDrop(player.X, player.Y, itemId.Value, 1, ref dropAngle, dropRadius));
|
|
}
|
|
}
|
|
|
|
// Clear player resources
|
|
player.Scrap = 0;
|
|
player.Energy = 0;
|
|
player.Rare = 0;
|
|
player.Inventory = new int?[6];
|
|
player.EquippedWeaponId = null;
|
|
|
|
return drops;
|
|
}
|
|
|
|
private DroppedItem CreateDrop(float x, float y, int itemId, int quantity, ref float angle, float radius)
|
|
{
|
|
var drop = new DroppedItem
|
|
{
|
|
Id = _nextEntityId++,
|
|
X = x + MathF.Cos(angle) * radius,
|
|
Y = y + MathF.Sin(angle) * radius,
|
|
ItemId = itemId,
|
|
Quantity = quantity
|
|
};
|
|
_droppedItems[drop.Id] = drop;
|
|
angle += MathF.PI / 4; // Spread drops around
|
|
return drop;
|
|
}
|
|
|
|
private void TryAction(PlayerEntity player, int? targetId)
|
|
{
|
|
// For now, action just triggers resource gathering from a nearby node
|
|
// Could be extended for interacting with terminals, doors, etc.
|
|
}
|
|
|
|
private void TryCraft(PlayerEntity player, int recipeId)
|
|
{
|
|
var recipe = RecipeDatabase.Get(recipeId);
|
|
if (recipe == null) return;
|
|
|
|
var (scrapCost, energyCost, rareCost) = recipe.GetCost();
|
|
|
|
if (player.Scrap < scrapCost || player.Energy < energyCost || player.Rare < rareCost)
|
|
return;
|
|
|
|
var item = ItemDatabase.Get(recipe.ResultItemId);
|
|
if (item == null) return;
|
|
|
|
// Check if there's room for non-stackable items
|
|
if (!item.Stackable)
|
|
{
|
|
bool hasRoom = player.Inventory.Any(s => s == null);
|
|
if (!hasRoom) return;
|
|
}
|
|
|
|
// Deduct resources
|
|
player.Scrap -= scrapCost;
|
|
player.Energy -= energyCost;
|
|
player.Rare -= rareCost;
|
|
|
|
// Add item
|
|
if (item.Stackable)
|
|
{
|
|
// Resources handled separately - shouldn't craft resources
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < player.Inventory.Length; i++)
|
|
{
|
|
if (player.Inventory[i] == null)
|
|
{
|
|
player.Inventory[i] = recipe.ResultItemId;
|
|
// Auto-equip weapon if none equipped
|
|
if (item.Type == ItemType.Weapon && player.EquippedWeaponId == null)
|
|
{
|
|
player.EquippedWeaponId = recipe.ResultItemId;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
player.ItemsCrafted++;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Craft,
|
|
PlayerId = player.Id,
|
|
Data = new { recipeId, resultItemId = recipe.ResultItemId }
|
|
});
|
|
}
|
|
|
|
private void TryDrop(PlayerEntity player, int slotIndex)
|
|
{
|
|
if (slotIndex < 0 || slotIndex >= player.Inventory.Length) return;
|
|
if (player.Inventory[slotIndex] == null) return;
|
|
|
|
int itemId = player.Inventory[slotIndex].Value;
|
|
player.Inventory[slotIndex] = null;
|
|
|
|
// If dropping equipped weapon, unequip
|
|
if (player.EquippedWeaponId == itemId)
|
|
{
|
|
player.EquippedWeaponId = null;
|
|
// Try to equip another weapon
|
|
foreach (var invItem in player.Inventory)
|
|
{
|
|
if (invItem.HasValue)
|
|
{
|
|
var def = ItemDatabase.Get(invItem.Value);
|
|
if (def?.Type == ItemType.Weapon)
|
|
{
|
|
player.EquippedWeaponId = invItem.Value;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
float dropAngle = player.Rotation + MathF.PI; // Drop behind player
|
|
var drop = new DroppedItem
|
|
{
|
|
Id = _nextEntityId++,
|
|
X = player.X + MathF.Cos(dropAngle) * 30f,
|
|
Y = player.Y + MathF.Sin(dropAngle) * 30f,
|
|
ItemId = itemId,
|
|
Quantity = 1
|
|
};
|
|
_droppedItems[drop.Id] = drop;
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.Drop,
|
|
PlayerId = player.Id,
|
|
Data = new { drop.Id, drop.X, drop.Y, drop.ItemId }
|
|
});
|
|
}
|
|
|
|
private void UpdateExtractionZones()
|
|
{
|
|
foreach (var zone in _extractionZones.Values)
|
|
{
|
|
if (!zone.Active && CurrentTick >= zone.ActivatesAtTick)
|
|
{
|
|
zone.Active = true;
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.ZoneActivate,
|
|
Data = new { zoneId = zone.Id, zone.X, zone.Y, zone.Radius }
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CheckEndConditions()
|
|
{
|
|
if (Phase != GamePhase.InProgress) return;
|
|
|
|
int alivePlayers = _players.Values.Count(p => p.Alive);
|
|
|
|
// Time expired
|
|
if (TimeRemaining <= 0)
|
|
{
|
|
EndGame("time_expired");
|
|
return;
|
|
}
|
|
|
|
// Last player standing (need at least 2 to start)
|
|
if (alivePlayers <= 1 && _players.Count >= 2)
|
|
{
|
|
EndGame("last_standing");
|
|
return;
|
|
}
|
|
|
|
// All players extracted
|
|
if (_players.Count > 0 && _players.Values.All(p => !p.Alive))
|
|
{
|
|
EndGame("all_eliminated_or_extracted");
|
|
}
|
|
}
|
|
|
|
private void EndGame(string reason)
|
|
{
|
|
Phase = GamePhase.Ended;
|
|
|
|
var winner = _players.Values
|
|
.Where(p => p.Alive || p.Extracted)
|
|
.OrderByDescending(p => p.Extracted ? 1 : 0)
|
|
.ThenByDescending(p => p.Kills)
|
|
.FirstOrDefault();
|
|
|
|
_pendingEvents.Add(new GameEvent
|
|
{
|
|
Type = EventType.GameEnd,
|
|
Data = new
|
|
{
|
|
reason,
|
|
winnerId = winner?.Id,
|
|
winnerName = winner?.Name
|
|
}
|
|
});
|
|
}
|
|
|
|
// ===== State Accessors =====
|
|
public IReadOnlyDictionary<ulong, PlayerEntity> Players => _players;
|
|
public IReadOnlyDictionary<int, Asteroid> Asteroids => _asteroids;
|
|
public IReadOnlyDictionary<int, ResourceNode> Resources => _resources;
|
|
public IReadOnlyDictionary<int, DroppedItem> DroppedItems => _droppedItems;
|
|
public IReadOnlyDictionary<int, Projectile> Projectiles => _projectiles;
|
|
public IReadOnlyDictionary<int, ExtractionZone> ExtractionZones => _extractionZones;
|
|
}
|
|
|
|
// ===== Entity Classes =====
|
|
public sealed class PlayerEntity
|
|
{
|
|
public ulong Id { get; set; }
|
|
public string Name { get; set; } = "";
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public float Rotation { get; set; }
|
|
public int Health { get; set; }
|
|
public int MaxHealth { get; set; }
|
|
public float Speed { get; set; }
|
|
public bool Alive { get; set; }
|
|
public bool Extracted { get; set; }
|
|
|
|
public int Scrap { get; set; }
|
|
public int Energy { get; set; }
|
|
public int Rare { get; set; }
|
|
public int?[] Inventory { get; set; } = new int?[6];
|
|
public int? EquippedWeaponId { get; set; }
|
|
|
|
public float InputDx { get; set; }
|
|
public float InputDy { get; set; }
|
|
public long LastShotTick { get; set; }
|
|
public float ExtractionProgress { get; set; }
|
|
|
|
public int Kills { get; set; }
|
|
public int Deaths { get; set; }
|
|
public int DamageDealt { get; set; }
|
|
public int ItemsCrafted { get; set; }
|
|
}
|
|
|
|
public sealed class Asteroid
|
|
{
|
|
public int Id { get; set; }
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public float Radius { get; set; }
|
|
public int Type { get; set; }
|
|
}
|
|
|
|
public sealed class ResourceNode
|
|
{
|
|
public int Id { get; set; }
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public int ResourceType { get; set; }
|
|
public int Amount { get; set; }
|
|
public int MaxAmount { get; set; }
|
|
}
|
|
|
|
public sealed class DroppedItem
|
|
{
|
|
public int Id { get; set; }
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public int ItemId { get; set; }
|
|
public int Quantity { get; set; }
|
|
}
|
|
|
|
public sealed class Projectile
|
|
{
|
|
public int Id { get; set; }
|
|
public ulong OwnerId { get; set; }
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public float VelX { get; set; }
|
|
public float VelY { get; set; }
|
|
public int Damage { get; set; }
|
|
public float Range { get; set; }
|
|
public float DistanceTraveled { get; set; }
|
|
public float ExplosionRadius { get; set; }
|
|
public int WeaponId { get; set; }
|
|
}
|
|
|
|
public sealed class ExtractionZone
|
|
{
|
|
public int Id { get; set; }
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
public float Radius { get; set; }
|
|
public bool Active { get; set; }
|
|
public long ActivatesAtTick { get; set; }
|
|
}
|
|
|
|
public enum GamePhase
|
|
{
|
|
WaitingForPlayers,
|
|
Countdown,
|
|
InProgress,
|
|
Ended
|
|
}
|
|
|
|
public sealed class PlayerInput
|
|
{
|
|
public ulong PlayerId { get; set; }
|
|
public InputType Type { get; set; }
|
|
public float Dx { get; set; }
|
|
public float Dy { get; set; }
|
|
public float AimAngle { get; set; }
|
|
public int? TargetId { get; set; }
|
|
public uint Seq { get; set; }
|
|
}
|
|
|
|
public enum InputType
|
|
{
|
|
Move,
|
|
Shoot,
|
|
Action,
|
|
Craft,
|
|
Drop
|
|
}
|
|
|
|
public sealed class GameEvent
|
|
{
|
|
public EventType Type { get; set; }
|
|
public ulong? PlayerId { get; set; }
|
|
public ulong? SourceId { get; set; }
|
|
public int? TargetId { get; set; }
|
|
public object? Data { get; set; }
|
|
}
|
|
|
|
public enum EventType
|
|
{
|
|
Spawn,
|
|
Despawn,
|
|
Damage,
|
|
Death,
|
|
Pickup,
|
|
Projectile,
|
|
Explosion,
|
|
Craft,
|
|
Drop,
|
|
Extract,
|
|
ZoneActivate,
|
|
GameEnd
|
|
}
|