99 lines
2.9 KiB
C#
99 lines
2.9 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.GodotNodeInterfaces;
|
|
using Chickensoft.Introspection;
|
|
using GameJamDungeon;
|
|
using Godot;
|
|
using System.Linq;
|
|
|
|
public interface IDungeonRoom : INode3D
|
|
{
|
|
DungeonRoomLogic DungeonRoomLogic { get; }
|
|
public Marker3D PlayerSpawn { get; set; }
|
|
}
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class DungeonRoom : Node3D, IDungeonRoom, IProvide<DungeonRoomLogic>
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
DungeonRoomLogic IProvide<DungeonRoomLogic>.Value() => DungeonRoomLogic;
|
|
|
|
[Dependency] public IGameRepo GameRepo => this.DependOn<IGameRepo>();
|
|
|
|
public DungeonRoomLogic DungeonRoomLogic { get; set; } = default!;
|
|
|
|
[Node] public Marker3D PlayerSpawn { get; set; } = default!;
|
|
|
|
[Node] public Node3D ItemSpawnPoints { get; set; } = default!;
|
|
|
|
[Node] public Node3D EnemySpawnPoints { get; set; } = default!;
|
|
|
|
[Node] public ItemDatabase ItemDatabase { get; set; } = default!;
|
|
|
|
[Node] public EnemyDatabase EnemyDatabase { get; set; } = default!;
|
|
|
|
public DungeonRoomLogic.IBinding DungeonRoomBinding { get; set; } = default!;
|
|
|
|
public void Setup()
|
|
{
|
|
DungeonRoomLogic = new DungeonRoomLogic();
|
|
DungeonRoomLogic.Set(this as IDungeonRoom);
|
|
DungeonRoomLogic.Set(GameRepo);
|
|
ItemDatabase.SpawnItems();
|
|
SpawnItems();
|
|
SpawnEnemies();
|
|
}
|
|
|
|
private void SpawnItems()
|
|
{
|
|
var itemSpawnPoints = ItemSpawnPoints.GetChildren();
|
|
var rng = new RandomNumberGenerator();
|
|
rng.Randomize();
|
|
var numberOfItemsToSpawn = rng.RandiRange(1, itemSpawnPoints.Count);
|
|
itemSpawnPoints.Shuffle();
|
|
foreach (Marker3D spawnPoint in itemSpawnPoints)
|
|
{
|
|
if (numberOfItemsToSpawn <= 0)
|
|
break;
|
|
numberOfItemsToSpawn--;
|
|
|
|
var weights = ItemDatabase.Database.Select(x => x.SpawnRate).ToArray();
|
|
var database = ItemDatabase.Database.Select(x => x.Item).ToArray();
|
|
var selectedItem = database[rng.RandWeighted(weights)];
|
|
var duplicated = selectedItem.Duplicate((int)DuplicateFlags.UseInstantiation) as Node3D;
|
|
duplicated.Position = spawnPoint.Position;
|
|
AddChild(duplicated);
|
|
}
|
|
}
|
|
|
|
private void SpawnEnemies()
|
|
{
|
|
var rng = new RandomNumberGenerator();
|
|
rng.Randomize();
|
|
var enemySpawnPoints = EnemySpawnPoints.GetChildren();
|
|
var numberOfEnemiesToSpawn = rng.RandiRange(1, enemySpawnPoints.Count);
|
|
|
|
foreach (Marker3D spawnPoint in enemySpawnPoints)
|
|
{
|
|
if (numberOfEnemiesToSpawn <= 0)
|
|
break;
|
|
numberOfEnemiesToSpawn--;
|
|
|
|
var enemy = EnemyDatabase.EnemyList[rng.RandWeighted(EnemyDatabase.SpawnRate)];
|
|
var instantiatedEnemy = enemy.Instantiate<Enemy>();
|
|
instantiatedEnemy.Position = spawnPoint.Position;
|
|
AddChild(instantiatedEnemy);
|
|
}
|
|
}
|
|
|
|
public void OnResolved()
|
|
{
|
|
DungeonRoomBinding = DungeonRoomLogic.Bind();
|
|
|
|
GameRepo.SetPlayerGlobalPosition(PlayerSpawn.GlobalPosition);
|
|
|
|
DungeonRoomLogic.Start();
|
|
this.Provide();
|
|
}
|
|
}
|