C# - finding different classes in foreach

Started by
5 comments, last by return0 13 years, 6 months ago
So I have a master list of all the items in my quad tree, and there are different classes like projectiles, enemies, players etc.. all sharing the base class QuadTreeItems. I am wondering if i can take the list of quadtree items and just cycle through enemies for instance. so below is my attempt to find all the enemies that collide with the projectiles:

//a new list to store items that are colliding with the bulletist<QT.IQuadTreeItem> collideItems = new List<QT.IQuadTreeItem>(10);            foreach (Actors.Projectile item in projectileItems)            {                collideItems.Clear();                //will put all quadtreeitems which collide with projectile item in list                quadTree.Query(item.BoundingBox, ref collideItems);                                                /////////////////////////////////                //This is the area which im trying to find all the zombie-only items that are in the collide list (the list includes the projectile                 //item) and it is fussing about not being able to convert the projectile to a zombie.                 foreach (Actors.Enemies.Zombie collideQTItem in collideItems)                {                    //here is code to deal the projectile damage to the zombie                }            }



Is there a way to do this? Thanks for looking guys.

Ed

http://www.youtube.com/user/bigrookdigital
Undead Empire for Xbox 360 --> http://bit.ly/dYdu3z
Gamerscore Tracker for Xbox 360 --> http://bit.ly/vI4T4X
www.bigrookgames.com
twitter.com/BigRookGames

Advertisement
foreach (var zombie in collideItems.OfType<Actors.Enemies.Zombie>()){    ...}
Mike Popoloski | Journal | SlimDX
Well, given any object you can do

if ( Object is MyType )

However i'm aware of no such way to do it in the foreach without branching on the above test.
Quote:Original post by Mike.Popoloski
*** Source Snippet Removed ***


awesome! works fine, thanks so much for the speedy response!

Regards,
Ed

http://www.youtube.com/user/bigrookdigital
Undead Empire for Xbox 360 --> http://bit.ly/dYdu3z
Gamerscore Tracker for Xbox 360 --> http://bit.ly/vI4T4X
www.bigrookgames.com
twitter.com/BigRookGames

Why have a base type if you are going to effectively roll your own dynamic dispatch?
Quote:Original post by return0
Why have a base type if you are going to effectively roll your own dynamic dispatch?


After trying to explain my reasoning for this, I realized that I don't neccessarily need to access them individually for many cases that I am, each will have an Update, Draw, Initialize functions and i am thinking it will be a lot simpler to do that without seperating them. Thank you very much for the question! But there are things that do need to be accessed individually, mostly like collision with certain objects and walls.

http://www.youtube.com/user/bigrookdigital
Undead Empire for Xbox 360 --> http://bit.ly/dYdu3z
Gamerscore Tracker for Xbox 360 --> http://bit.ly/vI4T4X
www.bigrookgames.com
twitter.com/BigRookGames

I'm glad the question provoked some more consideration about the design. Based on your response, I'm left wondering why you feel you want to test for certain objects or walls in the loop based on type rather than dynamically dispatching. By that I mean instead of;

foreach (Actors.Projectile item in projectileItems){    collideItems.Clear();                   quadTree.Query(item.BoundingBox, ref collideItems);          foreach (Actors.Enemies.Zombie collideQTItem in collideItems)    {        //here is code to deal the projectile damage to the zombie    }}


do

foreach (var projectile in projectiles){    collisionActors.Clear();                   quadTree.Query(item.BoundingBox, ref collisionActors);          foreach (var actor in collisionActors)     {        actor.ResolveCollision(projectile);    }}public interface ICollisionActor{    /*        set of collision data    */    void ResolveCollision(ICollisionActor _actor);}public interface IQuadTreeItem : ICollisionActor{       /*        stuff    */}public class Zombie : IQuadTreeItem {       /*        stuff    */    public void ResolveCollision(ICollisionActor _actor)    {        //here is code to deal the projectile damage to the zombie    }}

If the answer is that some IQuadTreeItems do not do anything in response to collisions and you do not want empty implementations, consider maintaining a separate list of items that do participate in collisions. This avoids the inextensible type check while iterating over the list of disparate things. If the reason is suspected performance gains on avoiding dynamic dispatch, I suggest you favour a clean, encapsulated and extensible design rather than speculatively attempting to optimise (in this case the "data oriented" approach would favour many lists, each catering for a different concrete type and processing them individually rather than branching).

This topic is closed to new replies.

Advertisement