Sorry for the title, I could not think of a better way to describe the problem. Basically, I'm trying to implement a collision system in the game. I want to be able to register a “collision handler” that handles any collision of two objects (given in any order) that can be passed to specific types. Therefore, if Player : Ship : Entityand Laser : Particle : Entity, and the handlers for (Ship, Particle)and are (Laser, Entity)registered than for the collision (Laser, Player), both handlers should be notified with arguments in the correct order and the collision (Laser, Laser)should only notify about the second handler.
The code snippet says a thousand words, so here is what I'm doing right now (naive method):
public IObservable<Collision<T1, T2>> onCollisionsOf<T1, T2>()
where T1 : Entity
where T2 : Entity
{
Type t1 = typeof(T1);
Type t2 = typeof(T2);
Subject<Collision<T1, T2>> obs = new Subject<Collision<T1, T2>>();
_onCollisionInternal += delegate(Entity obj1, Entity obj2)
{
if (t1.IsAssignableFrom(obj1.GetType()) && t2.IsAssignableFrom(obj2.GetType()))
obs.OnNext(new Collision<T1, T2>((T1) obj1, (T2) obj2));
else if (t1.IsAssignableFrom(obj2.GetType()) && t2.IsAssignableFrom(obj1.GetType()))
obs.OnNext(new Collision<T1, T2>((T1) obj2, (T2) obj1));
};
return obs;
}
(, ~ 2 FPS ), /.
, ( , , ), , , -, , , . , , , . - , (), , ?
,