mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2024-12-23 01:59:43 +01:00
Refactor entityclass::entityhlinecollide
The code quality has been improved in several ways: - Variables are `const`-qualified where possible. - Storing pointers to the objects referenced so `entities` doesn't need to keep being indexed, and the objects can then be referred to with better, longer names like `person` and `line`. - Early returns are used so the most important code doesn't keep being pushed to the right. - The comment from `entityclass::collisioncheck` is moved here, because it should be in this function and not at the callsite of the function. The existing comment in the function is made redundant with the more descriptive variable names, and is removed. - The comment has also been improved to be more concise and clear.
This commit is contained in:
parent
8b62bb1d65
commit
f23e99820f
1 changed files with 21 additions and 19 deletions
|
@ -4242,31 +4242,35 @@ static int yline( int a, int b )
|
|||
return 1;
|
||||
}
|
||||
|
||||
bool entityclass::entityhlinecollide( int t, int l )
|
||||
bool entityclass::entityhlinecollide(const int person_idx, const int line_idx)
|
||||
{
|
||||
if (!INBOUNDS_VEC(t, entities) || !INBOUNDS_VEC(l, entities))
|
||||
if (!INBOUNDS_VEC(person_idx, entities) || !INBOUNDS_VEC(line_idx, entities))
|
||||
{
|
||||
vlog_error("entityhlinecollide() out-of-bounds!");
|
||||
return false;
|
||||
}
|
||||
|
||||
//Returns true is entity t collided with the horizontal line l.
|
||||
if(entities[t].xp + entities[t].cx+entities[t].w>=entities[l].xp)
|
||||
const entclass* person = &entities[person_idx];
|
||||
const entclass* line = &entities[line_idx];
|
||||
|
||||
const bool in_vicinity =
|
||||
person->xp + person->cx + person->w >= line->xp &&
|
||||
person->xp + person->cx <= line->xp + line->w;
|
||||
if (!in_vicinity)
|
||||
{
|
||||
if(entities[t].xp + entities[t].cx<=entities[l].xp+entities[l].w)
|
||||
{
|
||||
int linetemp = 0;
|
||||
|
||||
linetemp += yline(entities[t].yp, entities[l].yp);
|
||||
linetemp += yline(entities[t].yp + entities[t].h, entities[l].yp);
|
||||
linetemp += yline(entities[t].oldyp, entities[l].yp);
|
||||
linetemp += yline(entities[t].oldyp + entities[t].h, entities[l].yp);
|
||||
|
||||
if (linetemp > -4 && linetemp < 4) return true;
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
/* Here we compare the person's old position versus their new one.
|
||||
* All points are either above or below the line. Else, it's a collision. */
|
||||
int linetemp = 0;
|
||||
|
||||
linetemp += yline(person->yp, line->yp);
|
||||
linetemp += yline(person->yp + person->h, line->yp);
|
||||
linetemp += yline(person->oldyp, line->yp);
|
||||
linetemp += yline(person->oldyp + person->h, line->yp);
|
||||
|
||||
return linetemp > -4 && linetemp < 4;
|
||||
}
|
||||
|
||||
bool entityclass::entityvlinecollide( int t, int l )
|
||||
|
@ -4850,8 +4854,6 @@ void entityclass::collisioncheck(int i, int j, bool scm /*= false*/)
|
|||
case 4: //Person vs horizontal line!
|
||||
if(game.deathseq==-1)
|
||||
{
|
||||
//Here we compare the person's old position versus his new one versus the line.
|
||||
//All points either be above or below it. Otherwise, there was a collision this frame.
|
||||
if (entities[j].onentity > 0)
|
||||
{
|
||||
if (entityhlinecollide(i, j))
|
||||
|
|
Loading…
Reference in a new issue