Beispiel #1
0
    public bool AddBehaviour(IEntityBehaviour behaviour)
    {
        // Get collision modes of the attachment
        List <EntityAttachmentCollisionMode> collisionModes = behaviour.GetAttachmentCollisionModes();
        // Initialize collision booleans
        bool collidesWithSelf      = collisionModes.Contains(EntityAttachmentCollisionMode.Self),
             collidesWithInherited = collisionModes.Contains(EntityAttachmentCollisionMode.Inherited);

        Type t = behaviour.GetType();

        // Check if the new attachment collides with any existing attachments
        if (collidesWithSelf && _typedBehaviours.GetValueOrDefault(t, new List <IEntityBehaviour> ()).Count > 0)
        {
            return(false);
        }
        if (collidesWithInherited && _inheritedTypedBehaviours.GetValueOrDefault(t, new List <IEntityBehaviour> ()).Count > 0)
        {
            return(false);
        }

        // Remove from previous entity
        IEntity previous = behaviour.GetPossessingEntity();

        previous.RemoveBehaviour(behaviour);

        // Add and initialize
        _behaviours.Add(behaviour);
        _typedBehaviours.Initialize(t, new List <IEntityBehaviour> ());
        _typedBehaviours[t].Add(behaviour);
        Type cur = t;

        while (cur.BaseType != null)
        {
            cur = cur.BaseType;
            _inheritedTypedBehaviours.Initialize(cur, new List <IEntityBehaviour> ());
            _inheritedTypedBehaviours[cur].Add(behaviour);
        }

        behaviour.AttachTo(this);

        return(true);
    }
Beispiel #2
0
    public bool RemoveBehaviour(IEntityBehaviour behaviour)
    {
        if (!_behaviours.Contains(behaviour))
        {
            return(false);
        }

        _behaviours.Remove(behaviour);

        Type t = behaviour.GetType();

        _typedBehaviours.Initialize(t, new List <IEntityBehaviour> ());
        _typedBehaviours[t].Remove(behaviour);

        while (t.BaseType != null)
        {
            t = t.BaseType;
            _inheritedTypedBehaviours.Initialize(t, new List <IEntityBehaviour> ());
            _inheritedTypedBehaviours[t].Remove(behaviour);
        }

        return(true);
    }