Jump to content
  • Advertisement
Sign in to follow this  
EdBoon

C# - finding different classes in foreach

This topic is 2837 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 bullet
ist<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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
*** Source Snippet Removed ***


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

Regards,
Ed

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!