Optimization of sending notification algorithm in C #?

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 ), /.

, ( , , ), , , -, , , . , , , . - , (), , ?

,

+3
3

. , . .

.

: .

OO - .

, vistior ", "? , ", ", . , , .

, ? , .

:

# 4 . , . IL IL. , , , . , . , . , , . , , . , , - .

:

, DLR. , . , , .

+3

Entity , , , ?

, , Laser ..

, .

public IObservable<Collision<T1, T2>> onCollisionsOf<T1, T2>()
    where T1 : Entity
    where T2 : Entity
{
    EntityTypeEnum t1 = T1.EntityType;
    EntityTypeEnum t2 = T2.EntityType;

    Subject<Collision<T1, T2>> obs = new Subject<Collision<T1, T2>>();
    _onCollisionInternal += delegate(Entity obj1, Entity obj2)
    {
       if (t1 < t2)
           obs.OnNext(new Collision<T1, T2>((T1) obj1, (T2) obj2));
       else
           obs.OnNext(new Collision<T1, T2>((T1) obj2, (T2) obj1));
    };
    return obs;
}
+1

, , : / /. IObservable < < T1, T2 → , .

_onCollisionInternal += delegate(T1 obj1, T2 obj2) {
  obs.OnNext(new Collision<T1, T2>( obj1, obj2));
};


public IObservable<Collision<T1, T2>> onCollisionsOf<T1, T2>()
        where T1 : Entity
        where T2 : Entity
    {
        Subject<Collision<T1, T2>> obs = new Subject<Collision<T1, T2>>();
        _onCollisionInternal += delegate(T1 obj1, T2 obj2) {
           obs.OnNext(new Collision<T1, T2>( obj1, obj2));
        };
        return obs;
    }

Observable<Collision<Laser, Player>> lp = onCollisionsOf<Laser, Player>();
Observable<Collision<Laser, Laser>> ll = onCollisionsOf<Laser, Laser>();
0
source

Source: https://habr.com/ru/post/1735150/


All Articles