First off, I’m new to LINQ, so I don’t really know the ins and outs of it. I’m attempting to use it in some code at the minute, and according to my diagnostics it appears to be about as fast as using a for loop in the same way. However, I’m not sure how well this would scale as the lists which I am working with could increase quite dramatically.
I’m using LINQ as part of a collision detection function (which is still in the works) and I’m using it to cull the list to only the ones that are relevant for the checks.
Here is the LINQ version:
partial class Actor {
public virtual bool checkActorsForCollision(Vector2 toCheck) {
Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);
if(!causingCollision) // skip if this actor doesn't collide
return false;
foreach(
Actor actor in
from a in GamePlay.actors
where a.causingCollision==true&&a.isAlive
select a
)
if( // ignore offscreen collisions, we don't care about them
(actor.location.X>GamePlay.onScreenMinimum.X)
&&
(actor.location.Y>GamePlay.onScreenMinimum.Y)
&&
(actor.location.X<GamePlay.onScreenMaximum.X)
&&
(actor.location.Y<GamePlay.onScreenMaximum.Y)
)
if(actor!=this) { // ignore collisions with self
Vector2 actorfloor=new Vector2((int)actor.location.X, (int)actor.location.Y);
if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
return true;
}
return false;
}
}
This is my previous method:
partial class Actor {
public virtual bool checkActorsForCollision(Vector2 toCheck) {
Vector2 floored=new Vector2((int)toCheck.X, (int)toCheck.Y);
if(!causingCollision) // skip if this actor doesn't collide
return false;
for(int i=0; i<GamePlay.actors.Count; i++)
if( // ignore offscreen collisions, we don't care about them
(GamePlay.actors[i].location.X>GamePlay.onScreenMinimum.X)
&&
(GamePlay.actors[i].location.Y>GamePlay.onScreenMinimum.Y)
&&
(GamePlay.actors[i].location.X<GamePlay.onScreenMaximum.X)
&&
(GamePlay.actors[i].location.Y<GamePlay.onScreenMaximum.Y)
)
if( // ignore collisions with self
(GamePlay.actors[i].isAlive)
&&
(GamePlay.actors[i]!=this)
&&
(GamePlay.actors[i].causingCollision)
) {
Vector2 actorfloor=
new Vector2(
(int)GamePlay.actors[i].location.X,
(int)GamePlay.actors[i].location.Y
);
if((floored.X==actorfloor.X)&&(floored.Y==actorfloor.Y))
return true;
}
return false;
}
}
At the minute, either run in almost no time (but run numerous times a second), but as the project builds and gets more intricate, this will be dealing with far more objects at once and the code to check for collisions will be more detailed.
Your code looks pretty good; I’m not a big fan of changing working code, but if you did want to rewrite it to be easier to read, here’s what I would do:
First, abstract away the predicate “is off the screen”. Perhaps make it a method of GamePlay. This business of checking every time whether the coordinates are in the bounds is (1) an implementation detail, and (2) making your code hard to read. It is possible that in the future you will have some more sophisticated mechanism for deciding whether an object is on the screen or not.
Second, abstract away the vector flooring operation. Perhaps make it a method of Vector. Note that this method should return a new vector, not mutate the existing vector.
Third, make an equality operator on vectors.
Fourth, name the method better. A predicate should have the form “IsFoo” or “HasFoo”. You’ve phrased it as a command, not as a question.
Fifth, you don’t need a loop at all.
Sixth, it is strange to say
somebool == true. Just saysomebool. The former means “if it is true that this bool is true”, which is needlessly complicated.Let’s see how this shakes out:
Look at how much easier that reads than your version of the method! None of this messing around with X and Y coordinates. Make helper methods do all that scutwork. The code now clearly expresses the semantics: tell me whether there are any collisions with other living, collision-causing actors on the screen.