Re: On the benefits of reflection
Tinco Andringa <[email protected]> Tue, 19 Mar 2013 22:56:28 +0100
| Newsgroups | gmane.games.devel.sweng |
|---|---|
| Message-ID | <CAGW=8RrKMXGcGEWPRm03=K5zyghKmhdN6uWs4mjJg7pxVOz59A@mail.gmail.com> |
>> Sort of related to reflection, recently I made a game as an academic
>> exercise in javascript using an aspect oriented programming library.
>> AOP has a similar philosophy of taking separation of concerns to the
>> next level. It worked out very well, you can have the game engine
>> drive the views without having any hooks or view related code in the
>> engine. Granted ofcourse this was a javascript exercise, not at all as
>> resource constrained as a big console game.
>
>
> Sounds cool. Do you have some specific examples?
So it kind of works sort of like an inverted events system. For
example using events:
class Object {
TakeDamage(int damage) {
var total = max(armor - damage, 0);
hp -= total;
Events.Trigger(new DamageTakenEvent(this, damage, total));
}
}
Events.On<DamageTakenEvent>( e =>
Console.WriteLine(e.Object.Name + " took: " + e.total + " damage.");
);
Using aspect oriented programming:
class Object {
TakeDamage(int damage) {
var total = max(armor - damage, 0);
hp -= total;
}
}
AOP.Around(Object.TakeDamage, (invocation, params, object) =>
var total = object.HP;
invocation.proceed(params);
total -= object.HP;
Console.WriteLine(object.Name + " took: " + total + " damage.");
);
Note that I don't really know what C# aop looks like, I'm not sure you
can be that succinct when using method references (delegations) but
that's the gist of it. You can see now the game logic function
TakeDamage has no awareness of any observers, so there's a clean
separation of concerns. But also notice that the AOP advice can not
see what happens in the method so has to recalculate the total.
In my own 'big' game I'm using the events approach btw, I'm not fully
convinced AOP is the way to go, but it is interesting.
_______________________________________________
Sweng-Gamedev mailing list
[email protected]
http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com