|
In a previous blog post, I discussed something that I'm building on top of LINQ called CLINQ. CLINQ allows me to write a query once and without having to poll the result set to force a re-enumeration, the result set is automatically notified of changes to the source. For example, if I write a query that has a sort and a filter on a source collection, then using CLINQ, the result set will automatically place new items in the appropriate sort order, and the result set is automatically modified when the source collection loses items, and when it gains items that pass the result set's filter predicate.
So, this is fantastically cool in a business scenario and I'm working on some proof of concept stuff that will show off how CLINQ works as well as publish the source code for CLINQ. However, I thought - what would it look like if I could make some WPF games with CLINQ?
Take, for example, a space strategy game. Let's say you want to bind your radar display to a list of nearby ships. This is a pretty typical scenario, but instead of re-polling your underlying set of all ships (populated by your network messaging layer, of course), what if you could define a query indicating nearby ships and that would automatically update based on network messages received without you having to do any real work?
It might look something like this:
ContinousCollection<RadarBlip> _nearbyObjects =
from radarObject in _networkObjects
where radarObject.3DDistance(this.Position) <= this.RadarScanRadius
orderby radarObject.3DDistance(this.Position)
select radarObject;
radarControl.DataContext = _nearbyObjects;
Knowing WPF, you know that you can do pretty much anything you want with data templates. You could have a data template for each type of radar object (using a DataTemplateSelector that switches on something like radarObject.ObjectType!) and display a different image or maybe even 3D model (not sure if that's possible yet in Orcas...). The fantastic part about this is that you can have multiple queries on the same source set that all update independently as the source set is modified - and you as a developer are relieved from the burden of data propagation.
In your example, you use this.Position ... will the collection dynamically
change as this.Position changes?
Good point. Right now, "this.Position" isn't a part of the source data so
it won't have any control over the result set. Basically if any property of
any item in the source collection changes, then it is re-evaluated against
the filter predicate to see if it belongs in the output. If that passes, it
is sent down the chain to any attached grouping or sorting adapters, making
sure that everything remains consistent even though the source data is in
flux.