-
Notifications
You must be signed in to change notification settings - Fork 77
Components ( Deprecated info )
A component is a game brick with some game variables. In ACTORS framework components often used with [Serializable] attribute for easy editing via unity inspector. You don't put logic inside of components. _ You can use some initialization code if it's needed. _
Where {NAME} - is the name of a component. The most convenient way will be making template inside of your IDE for fast generating.
namespace Homebrew
{
public class Component{NAME} : IComponent
{
// body
}
public static class ExtenstionComponent{NAME}
{
public static Component{NAME} Component{NAME}(this int entity)
{
return Storage<Component{NAME}>.Instance.components[entity];
}
public static bool HasComponent{NAME}(this int entity)
{
return Storage<Component{NAME}>.Instance.HasComponent(entity);
}
}
}
An example:
using System;
namespace Homebrew
{
[Serializable]
public class ComponentHealth : IComponent
{
public int HP = 1;
public int HPMax = 5;
}
public static class ExtensionComponentHealth
{
public static ComponentHealth ComponentHealth(this int entity) { return Storage<ComponentHealth>.Instance.components[entity]; }
public static bool HasComponentHealth(this int entity) { return Storage<ComponentHealth>.Instance.HasComponent(entity); }
}
}
var entity = 0; // entity with id 0
var cObject = entity.ComponentObject(); // taking ComponentObject without any checks
Check if an entity has component:
var entity = 0; // entity with id 0
if (entity.HasComponentObject()) {} // return bool
Check if an entity has one ore more components and get them.
ComponentAnimation cAnimation;
ComponentMotion cMotion;
if (entity.Get(out cAnimation, out cMotion))
{
// do something
}
Components can be added via Actor class in the setup method.
public class ActorPlayer: Actor
{
[FoldoutGroup("Setup")]
public ComponentHealth componentHealth;
[FoldoutGroup("Setup")]
public ComponentMotion componentMotion;
// this method is used for adding components
protected override void Setup()
{
// add serialized component variables
Add(componentHealth);
Add(componentMotion);
// add by type
Add<ComponentPlayer>();
}
}
Another way of adding components is using EntityComposer.
var composer = new EntityComposer(2);
var cMotion = composer.Add<ComponentMotion>();
cMotion.direction = Vector2.right;
var cObject = composer.Add<ComponentObject>();
cObject.obj = gameObject;
cObject.transform = transform;
composer.Deploy();
I will add more info about how to add components and work with actor/entity composer.
Use ISetup
when you want to add initialization logic to your component.
The framework will handle the setup method when a component added to an entity.
[Serializable]
public class ComponentHealth : IComponent, ISetup
{
public int HP = 1;
public int HPMax = 5;
public Animator animator;
public void Setup(int entity)
{
// strange example
animator = entity.GameObject().GetComponent<Animator>();
}
}
The framework already have one component to use. It's ComponentObject
.
ComponentObject represents unity game object and transform. Actor class will automatically add this component when created.