I'm using component based design and am at the stage or re-factoring simple player input to be data driven.
Currently i have a PlayerControlComponent
, which an entity has if it should be player controlled. Currently only a single entity has this.
I have an InputSystem
that acts on entities with PlayerControlComponent
, PhysicalComponent
and MoveableComponent
components.
Below is the code that actually does the entity creation:
if (_InputActionManager.IsAction(_ActionFireBullet))
{
if (WorldManager.CanDoAction(playerControlComponent.CreateBulletActionId))
{
// Create a bullet and fire it
using (var entityTemplate = WorldManager.GetEntityTemplate(@"EntityTemplates\Bullet"))
{
var bulletPhysicalComponent = entityTemplate.Item.GetComponentOfType<PhysicalComponent>();
var bulletMoveableComponent = entityTemplate.Item.GetComponentOfType<MoveableComponent>();
bulletPhysicalComponent.Position = physicalComponent.Position + physicalComponent.Direction * physicalComponent.Radius;
bulletPhysicalComponent.Direction = physicalComponent.Direction;
bulletMoveableComponent.Velocity = moveableComponent.Velocity + physicalComponent.Direction * 200;
}
WorldManager.MarkActionDone(playerControlComponent.CreateBulletActionId);
}
}
I want to get to the stage where I can have an XML file that indicates some how that a given entity will create a new entity based on a given template and set initial values such as position, direction and velocity in the above example.
I want to avoid having to change code and maybe add systems and components for each type of thing I want to spawn.
For example, currently if I wanted to add the ability for an enemy to spawn a missile I'd need to define my missle entity template (fine), create a new component to mark the entity as being able to fire a missle, then create a system that uses that component (want to avoid), then create a system with code similar to the above (want to avoid).
As another example, say I want to change the speed of the bullet, currently I do that in code, but I want to be able to do that in a data-driven way.
How can I create and manage a way of creating an entity based on an event occuring, with properties based on the entity that created it? i.e. Player presses Fire, bullet spawned at ship location, moving away from ship.