diff --git a/PeopleVille/WorldBuilder/World.cs b/PeopleVille/WorldBuilder/World.cs new file mode 100644 index 0000000..d684afb --- /dev/null +++ b/PeopleVille/WorldBuilder/World.cs @@ -0,0 +1,14 @@ +using PeopleVille.Equipment; +using PeopleVille.Locations; +using PeopleVille.Persons; + +namespace PeopleVille.WorldBuilder +{ + public class World + { + public GameManager manager = new(); + public List Equipment = []; + public List People = []; + public List Locations = []; + } +} \ No newline at end of file diff --git a/PeopleVille/WorldBuilder/WorldBuilder.cs b/PeopleVille/WorldBuilder/WorldBuilder.cs index 71a6bf6..1d1de9c 100644 --- a/PeopleVille/WorldBuilder/WorldBuilder.cs +++ b/PeopleVille/WorldBuilder/WorldBuilder.cs @@ -1,75 +1,137 @@ using PeopleVille.Equipment; using PeopleVille.Locations; using PeopleVille.Persons; -using System; -using System.Collections.Generic; -using System.Text; -using static PeopleVille.WorldBuilder.WorldInterfaces; - +using PeopleVille; +using System.Reflection; namespace PeopleVille.WorldBuilder { - public class CitizenBuilder : ICitizenBuilder + public class WorldBuilder : + IWorldBuilder, + IGameManagerBuilder, + IEquipmentBuilder, + ILocationBuilder, + IPersonBuilder { - Person person; - List Citizens = new List(); + private World world; - public ICitizenBuilder CreateAdult(string name, int health) + public IGameManagerBuilder AddGameManager() { - person = new AdultCitizen { Name = name, Health = health, Inventory = new List() }; - Citizens.Add(person); + world = new World(); return this; } - public ICitizenBuilder CreateChild(string name, int health) + IEquipmentBuilder IGameManagerBuilder.AddEquipment() { - person = new ChildCitizen { Name = name, Health = health, Inventory = new List() }; - Citizens.Add(person); return this; } - public ICitizenBuilder WithGun(string name, int damage) + ILocationBuilder IEquipmentBuilder.AddLocations() { - person.Inventory.Add(new Gun { Name = name, Damage = damage }); return this; } - public ICitizenBuilder WithFood(string name, int healthPoints) + IPersonBuilder ILocationBuilder.AddPersons() { - person.Inventory.Add(new Food { Name = name, HealthPoints = healthPoints }); return this; } - public List BuildCitizens() + IWorldBuilder IPersonBuilder.EndWorldBuilding() { - return Citizens; - } - } - - public class TownBuilder : ITownBuilder - { - List locations = new List(); - - public ITownBuilder AddGunStore(string name) - { - locations.Add(new GunStore { Name = name, Inventory = new Dictionary() }); return this; } - public ITownBuilder AddEggStore(string name) + IEquipmentBuilder IEquipmentBuilder.FromRange(IEnumerable equipment) { - locations.Add(new EggStore { Name = name, Inventory = new Dictionary() }); + world.Equipment.AddRange(equipment); return this; } - public ITownBuilder AddBank(string name) + ILocationBuilder ILocationBuilder.FromRange(IEnumerable locations) { - locations.Add(new Bank { Name = name }); + world.Locations.AddRange(locations); return this; } - public List BuildTown() + IPersonBuilder IPersonBuilder.FromRange(IEnumerable people) { - return locations; + world.People.AddRange(people); + return this; + } + + World IWorldBuilder.Build() + { + return world; + } + + ILocationBuilder ILocationBuilder.FromFile(string pathToFile) + { + world.Locations.AddRange(LoadTypesFromAssembly(pathToFile)); + return this; + } + + IPersonBuilder IPersonBuilder.FromFile(string pathToFile) + { + world.People.AddRange(LoadTypesFromAssembly(pathToFile)); + return this; + } + + IEquipmentBuilder IEquipmentBuilder.FromFile(string pathToFile) + { + world.Equipment.AddRange(LoadTypesFromAssembly(pathToFile)); + return this; + } + + IEquipmentBuilder IEquipmentBuilder.FromFolder(string pathToFolder) + { + world.Equipment.AddRange(LoadTypesFromAssemblyFolder(pathToFolder)); + return this; + } + + ILocationBuilder ILocationBuilder.FromFolder(string pathToFolder) + { + world.Locations.AddRange(LoadTypesFromAssemblyFolder(pathToFolder)); + return this; + } + + IPersonBuilder IPersonBuilder.FromFolder(string pathToFolder) + { + world.People.AddRange(LoadTypesFromAssemblyFolder(pathToFolder)); + return this; + } + + private List LoadTypesFromAssemblyFolder(string pathToAssemblyFolder) where T : class + { + var instances = new List(); + string[] dlls = Directory.GetFiles(pathToAssemblyFolder, "*.dll"); + + foreach (var dll in dlls) + { + instances.AddRange(LoadTypesFromAssembly(dll)); + } + + return instances; + } + + private List LoadTypesFromAssembly(string pathToAssembly) where T : class + { + var instances = new List(); + + Assembly assembly = Assembly.LoadFrom(pathToAssembly); + + + var types = assembly.GetTypes().Where(t => + typeof(T).IsAssignableFrom(t) && + !t.IsInterface && + !t.IsAbstract + ); + + foreach (var type in types) + { + var instance = Activator.CreateInstance(type) as T; + if (instance != null) instances.Add(instance); + } + + return instances; } } } diff --git a/PeopleVille/WorldBuilder/WorldInterfaces.cs b/PeopleVille/WorldBuilder/WorldInterfaces.cs index c302c95..cde463f 100644 --- a/PeopleVille/WorldBuilder/WorldInterfaces.cs +++ b/PeopleVille/WorldBuilder/WorldInterfaces.cs @@ -1,30 +1,58 @@ -using PeopleVille.Locations; +using PeopleVille.Equipment; +using PeopleVille.Locations; using PeopleVille.Persons; namespace PeopleVille.WorldBuilder { - public class WorldInterfaces + public interface IWorldBuilder { - public interface IWorldBuilder - { - - } - - public interface ITownBuilder - { - ITownBuilder AddGunStore(string name); - ITownBuilder AddEggStore(string name); - ITownBuilder AddBank(string name); - List BuildTown(); - } - - public interface ICitizenBuilder - { - ICitizenBuilder CreateAdult(string name, int health); - ICitizenBuilder CreateChild(string name, int health); - ICitizenBuilder WithGun(string name, int damage); - ICitizenBuilder WithFood(string name, int healthPoints); - List BuildCitizens(); - } + IGameManagerBuilder AddGameManager(); + World Build(); } + + public interface IGameManagerBuilder + { + IEquipmentBuilder AddEquipment(); + } + + public interface IEquipmentBuilder + { + IEquipmentBuilder FromFolder(string pathToFolder); + IEquipmentBuilder FromFile(string pathToFile); + IEquipmentBuilder FromRange(IEnumerable equipment); + ILocationBuilder AddLocations(); + } + + public interface ILocationBuilder + { + ILocationBuilder FromFolder(string pathToFolder); + ILocationBuilder FromFile(string pathToFile); + ILocationBuilder FromRange(IEnumerable locations); + IPersonBuilder AddPersons(); + } + + public interface IPersonBuilder + { + IPersonBuilder FromFolder(string pathToFolder); + IPersonBuilder FromFile(string pathToFile); + IPersonBuilder FromRange(IEnumerable people); + IWorldBuilder EndWorldBuilding(); + } + + // public interface ITownBuilder + // { + // ITownBuilder AddGunStore(string name); + // ITownBuilder AddEggStore(string name); + // ITownBuilder AddBank(string name); + // List BuildTown(); + // } + + // public interface ICitizenBuilder + // { + // ICitizenBuilder CreateAdult(string name, int health); + // ICitizenBuilder CreateChild(string name, int health); + // ICitizenBuilder WithGun(string name, int damage); + // ICitizenBuilder WithFood(string name, int healthPoints); + // List BuildCitizens(); + // } }