Skip to content

Instantly share code, notes, and snippets.

@davidwallacejackson
Last active August 29, 2015 14:21
Show Gist options
  • Save davidwallacejackson/ae318c1a534857f1d1ae to your computer and use it in GitHub Desktop.
Save davidwallacejackson/ae318c1a534857f1d1ae to your computer and use it in GitHub Desktop.
using UnityEngine;
namespace LD32
{
public class BaseBehaviour : MonoBehaviour
{
BehaviourMessageBus _messageBus;
public BehaviourMessageBus MessageBus
{
get
{
return _messageBus;
}
}
public virtual void Awake()
{
//resolve MessageBus before we do anything else:
_messageBus = GetComponent<BehaviourMessageBus>();
if (_messageBus == null)
{
//we couldn't find a message bus -- we'll have to
//create one
_messageBus = gameObject.AddComponent<BehaviourMessageBus>();
}
}
void OnDestroy()
{
MessageBus.OnDestroy.Invoke(this);
}
}
}
using UnityEngine;
using System.Collections;
namespace LD32
{
public class BehaviourMessageBus : MonoBehaviour
{
IntEvent _damage = new IntEvent();
public IntEvent Damage { get { return _damage; } }
BehaviourEvent _onDestroy = new BehaviourEvent();
public BehaviourEvent OnDestroy { get { return _onDestroy; } }
//other events can go here, using the above as a template
//their arguments are simply determined by their type
}
}
using UnityEngine;
using System.Collections;
namespace LD32
{
public class BulletController : BaseBehaviour
{
int damageAmount;
void OnCollisionEnter2D(Collision2D collision)
{
var targetBus = collision.gameObject.GetComponent<BehaviourMessageBus>();
if (targetBus != null)
{
targetBus.Damage.Invoke(this.damageAmount);
}
}
}
}
using UnityEngine;
using System.Collections;
namespace LD32
{
public class BulletController : BaseBehaviour
{
int damageAmount;
void OnCollisionEnter2D(Collision2D collision)
{
collision.gameObject.GetMessageBus().Damage.Invoke(this.damageAmount);
}
}
}
using UnityEngine;
using UnityEngine.Events;
namespace LD32
{
public class FloatEvent : UnityEvent<float> { }
public class IntEvent : UnityEvent<int> { }
public class GameObjectEvent : UnityEvent<GameObject> { }
public class NoArgEvent : UnityEvent { }
public class VectorEvent : UnityEvent<Vector2> { }
}
using UnityEngine;
namespace LD32
{
public static class ExtensionMethods
{
public static BehaviourMessageBus GetMessageBus(this GameObject go)
{
var messageBus = go.GetComponent<BehaviourMessageBus>();
if (messageBus == null)
{
//no message bus yet -- make one!
messageBus = go.AddComponent<BehaviourMessageBus>();
}
return messageBus;
}
public static BehaviourMessageBus GetMessageBus(this Component comp)
{
return comp.gameObject.GetMessageBus();
}
}
}
using UnityEngine;
using System.Collections;
namespace LD32
{
public class Health : BaseBehaviour
{
int _health;
public int maxHealth;
public int health
{
get
{
return _health;
}
}
public override void Awake()
{
base.Awake();
MessageBus.Damage.AddListener(Damage);
}
public override void Start()
{
_health = maxHealth;
}
public void Damage(int damageAmount)
{
_health -= damageAmount;
if (_health <= 0)
{
Destroy(this.gameObject);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment