new world builder
This commit is contained in:
14
PeopleVille/WorldBuilder/World.cs
Normal file
14
PeopleVille/WorldBuilder/World.cs
Normal file
@@ -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<IEquipment> Equipment = [];
|
||||||
|
public List<Person> People = [];
|
||||||
|
public List<Location> Locations = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,75 +1,137 @@
|
|||||||
using PeopleVille.Equipment;
|
using PeopleVille.Equipment;
|
||||||
using PeopleVille.Locations;
|
using PeopleVille.Locations;
|
||||||
using PeopleVille.Persons;
|
using PeopleVille.Persons;
|
||||||
using System;
|
using PeopleVille;
|
||||||
using System.Collections.Generic;
|
using System.Reflection;
|
||||||
using System.Text;
|
|
||||||
using static PeopleVille.WorldBuilder.WorldInterfaces;
|
|
||||||
|
|
||||||
namespace PeopleVille.WorldBuilder
|
namespace PeopleVille.WorldBuilder
|
||||||
{
|
{
|
||||||
public class CitizenBuilder : ICitizenBuilder
|
public class WorldBuilder :
|
||||||
|
IWorldBuilder,
|
||||||
|
IGameManagerBuilder,
|
||||||
|
IEquipmentBuilder,
|
||||||
|
ILocationBuilder,
|
||||||
|
IPersonBuilder
|
||||||
{
|
{
|
||||||
Person person;
|
private World world;
|
||||||
List<Person> Citizens = new List<Person>();
|
|
||||||
|
|
||||||
public ICitizenBuilder CreateAdult(string name, int health)
|
public IGameManagerBuilder AddGameManager()
|
||||||
{
|
{
|
||||||
person = new AdultCitizen { Name = name, Health = health, Inventory = new List<IEquipment>() };
|
world = new World();
|
||||||
Citizens.Add(person);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICitizenBuilder CreateChild(string name, int health)
|
IEquipmentBuilder IGameManagerBuilder.AddEquipment()
|
||||||
{
|
{
|
||||||
person = new ChildCitizen { Name = name, Health = health, Inventory = new List<IEquipment>() };
|
|
||||||
Citizens.Add(person);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICitizenBuilder WithGun(string name, int damage)
|
ILocationBuilder IEquipmentBuilder.AddLocations()
|
||||||
{
|
{
|
||||||
person.Inventory.Add(new Gun { Name = name, Damage = damage });
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICitizenBuilder WithFood(string name, int healthPoints)
|
IPersonBuilder ILocationBuilder.AddPersons()
|
||||||
{
|
{
|
||||||
person.Inventory.Add(new Food { Name = name, HealthPoints = healthPoints });
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Person> BuildCitizens()
|
IWorldBuilder IPersonBuilder.EndWorldBuilding()
|
||||||
{
|
{
|
||||||
return Citizens;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TownBuilder : ITownBuilder
|
|
||||||
{
|
|
||||||
List<Location> locations = new List<Location>();
|
|
||||||
|
|
||||||
public ITownBuilder AddGunStore(string name)
|
|
||||||
{
|
|
||||||
locations.Add(new GunStore { Name = name, Inventory = new Dictionary<object, int>() });
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITownBuilder AddEggStore(string name)
|
IEquipmentBuilder IEquipmentBuilder.FromRange(IEnumerable<IEquipment> equipment)
|
||||||
{
|
{
|
||||||
locations.Add(new EggStore { Name = name, Inventory = new Dictionary<object, int>() });
|
world.Equipment.AddRange(equipment);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITownBuilder AddBank(string name)
|
ILocationBuilder ILocationBuilder.FromRange(IEnumerable<Location> locations)
|
||||||
{
|
{
|
||||||
locations.Add(new Bank { Name = name });
|
world.Locations.AddRange(locations);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Location> BuildTown()
|
IPersonBuilder IPersonBuilder.FromRange(IEnumerable<Person> people)
|
||||||
{
|
{
|
||||||
return locations;
|
world.People.AddRange(people);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
World IWorldBuilder.Build()
|
||||||
|
{
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
|
ILocationBuilder ILocationBuilder.FromFile(string pathToFile)
|
||||||
|
{
|
||||||
|
world.Locations.AddRange(LoadTypesFromAssembly<Location>(pathToFile));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPersonBuilder IPersonBuilder.FromFile(string pathToFile)
|
||||||
|
{
|
||||||
|
world.People.AddRange(LoadTypesFromAssembly<Person>(pathToFile));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEquipmentBuilder IEquipmentBuilder.FromFile(string pathToFile)
|
||||||
|
{
|
||||||
|
world.Equipment.AddRange(LoadTypesFromAssembly<IEquipment>(pathToFile));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
IEquipmentBuilder IEquipmentBuilder.FromFolder(string pathToFolder)
|
||||||
|
{
|
||||||
|
world.Equipment.AddRange(LoadTypesFromAssemblyFolder<IEquipment>(pathToFolder));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
ILocationBuilder ILocationBuilder.FromFolder(string pathToFolder)
|
||||||
|
{
|
||||||
|
world.Locations.AddRange(LoadTypesFromAssemblyFolder<Location>(pathToFolder));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
IPersonBuilder IPersonBuilder.FromFolder(string pathToFolder)
|
||||||
|
{
|
||||||
|
world.People.AddRange(LoadTypesFromAssemblyFolder<Person>(pathToFolder));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<T> LoadTypesFromAssemblyFolder<T>(string pathToAssemblyFolder) where T : class
|
||||||
|
{
|
||||||
|
var instances = new List<T>();
|
||||||
|
string[] dlls = Directory.GetFiles(pathToAssemblyFolder, "*.dll");
|
||||||
|
|
||||||
|
foreach (var dll in dlls)
|
||||||
|
{
|
||||||
|
instances.AddRange(LoadTypesFromAssembly<T>(dll));
|
||||||
|
}
|
||||||
|
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<T> LoadTypesFromAssembly<T>(string pathToAssembly) where T : class
|
||||||
|
{
|
||||||
|
var instances = new List<T>();
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,58 @@
|
|||||||
using PeopleVille.Locations;
|
using PeopleVille.Equipment;
|
||||||
|
using PeopleVille.Locations;
|
||||||
using PeopleVille.Persons;
|
using PeopleVille.Persons;
|
||||||
|
|
||||||
namespace PeopleVille.WorldBuilder
|
namespace PeopleVille.WorldBuilder
|
||||||
{
|
{
|
||||||
public class WorldInterfaces
|
|
||||||
{
|
|
||||||
public interface IWorldBuilder
|
public interface IWorldBuilder
|
||||||
{
|
{
|
||||||
|
IGameManagerBuilder AddGameManager();
|
||||||
|
World Build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ITownBuilder
|
public interface IGameManagerBuilder
|
||||||
{
|
{
|
||||||
ITownBuilder AddGunStore(string name);
|
IEquipmentBuilder AddEquipment();
|
||||||
ITownBuilder AddEggStore(string name);
|
|
||||||
ITownBuilder AddBank(string name);
|
|
||||||
List<Location> BuildTown();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ICitizenBuilder
|
public interface IEquipmentBuilder
|
||||||
{
|
{
|
||||||
ICitizenBuilder CreateAdult(string name, int health);
|
IEquipmentBuilder FromFolder(string pathToFolder);
|
||||||
ICitizenBuilder CreateChild(string name, int health);
|
IEquipmentBuilder FromFile(string pathToFile);
|
||||||
ICitizenBuilder WithGun(string name, int damage);
|
IEquipmentBuilder FromRange(IEnumerable<IEquipment> equipment);
|
||||||
ICitizenBuilder WithFood(string name, int healthPoints);
|
ILocationBuilder AddLocations();
|
||||||
List<Person> BuildCitizens();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ILocationBuilder
|
||||||
|
{
|
||||||
|
ILocationBuilder FromFolder(string pathToFolder);
|
||||||
|
ILocationBuilder FromFile(string pathToFile);
|
||||||
|
ILocationBuilder FromRange(IEnumerable<Location> locations);
|
||||||
|
IPersonBuilder AddPersons();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IPersonBuilder
|
||||||
|
{
|
||||||
|
IPersonBuilder FromFolder(string pathToFolder);
|
||||||
|
IPersonBuilder FromFile(string pathToFile);
|
||||||
|
IPersonBuilder FromRange(IEnumerable<Person> people);
|
||||||
|
IWorldBuilder EndWorldBuilding();
|
||||||
|
}
|
||||||
|
|
||||||
|
// public interface ITownBuilder
|
||||||
|
// {
|
||||||
|
// ITownBuilder AddGunStore(string name);
|
||||||
|
// ITownBuilder AddEggStore(string name);
|
||||||
|
// ITownBuilder AddBank(string name);
|
||||||
|
// List<Location> 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<Person> BuildCitizens();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user