From f23e99820f0e396a51fa2e1b92a442997c5f6fae Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 13 Feb 2023 17:12:45 -0800 Subject: [PATCH] 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. --- desktop_version/src/Entity.cpp | 40 ++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/desktop_version/src/Entity.cpp b/desktop_version/src/Entity.cpp index f70246d7..f2f21e4a 100644 --- a/desktop_version/src/Entity.cpp +++ b/desktop_version/src/Entity.cpp @@ -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))