1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-22 17:49:43 +01:00

Refactor blocks to not use the 'active' system

This removes the variables obj.nblocks, as well as removing the 'active'
attribute from the block object. Now every block is guaranteed to be
real without having to check the 'active' variable.

Removing a block while iterating now uses the removeblock_iter() macro.
This commit is contained in:
Misa 2020-04-03 16:11:02 -07:00 committed by Ethan Lee
parent 7689241d3a
commit f10ac88c1a
4 changed files with 168 additions and 216 deletions

View file

@ -7,7 +7,6 @@ blockclass::blockclass()
void blockclass::clear() void blockclass::clear()
{ {
active = false;
type = 0; type = 0;
trigger = 0; trigger = 0;

View file

@ -16,7 +16,6 @@ public:
void setblockcolour(std::string col); void setblockcolour(std::string col);
public: public:
//Fundamentals //Fundamentals
bool active;
SDL_Rect rect; SDL_Rect rect;
int type; int type;
int trigger; int trigger;

View file

@ -42,8 +42,6 @@ bool entityclass::checktowerspikes(int t)
void entityclass::init() void entityclass::init()
{ {
nblocks = 0;
skipblocks = false; skipblocks = false;
skipdirblocks = false; skipdirblocks = false;
platformtile = 0; platformtile = 0;
@ -68,7 +66,6 @@ void entityclass::init()
} }
flags.resize(100); flags.resize(100);
blocks.resize(500);
collect.resize(100); collect.resize(100);
customcollect.resize(100); customcollect.resize(100);
} }
@ -784,320 +781,286 @@ void entityclass::generateswnwave( int t )
void entityclass::createblock( int t, int xp, int yp, int w, int h, int trig /*= 0*/ ) void entityclass::createblock( int t, int xp, int yp, int w, int h, int trig /*= 0*/ )
{ {
if(nblocks == 0) k = blocks.size();
{
//If there are no active blocks, Z=0;
k = 0;
nblocks++;
}
else
{
int i = 0;
k = -1;
while (i < nblocks)
{
if (!blocks[i].active)
{
k = i;
i = nblocks;
}
i++;
}
if (k == -1)
{
k = nblocks;
nblocks++;
}
}
blocks[k].clear(); blockclass block;
blocks[k].active = true;
switch(t) switch(t)
{ {
case BLOCK: //Block case BLOCK: //Block
blocks[k].type = BLOCK; block.type = BLOCK;
blocks[k].xp = xp; block.xp = xp;
blocks[k].yp = yp; block.yp = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
nblocks++;
break; break;
case TRIGGER: //Trigger case TRIGGER: //Trigger
blocks[k].type = TRIGGER; block.type = TRIGGER;
blocks[k].x = xp; block.x = xp;
blocks[k].y = yp; block.y = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
blocks[k].trigger = trig; block.trigger = trig;
nblocks++;
break; break;
case DAMAGE: //Damage case DAMAGE: //Damage
blocks[k].type = DAMAGE; block.type = DAMAGE;
blocks[k].x = xp; block.x = xp;
blocks[k].y = yp; block.y = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
nblocks++;
break; break;
case DIRECTIONAL: //Directional case DIRECTIONAL: //Directional
blocks[k].type = DIRECTIONAL; block.type = DIRECTIONAL;
blocks[k].x = xp; block.x = xp;
blocks[k].y = yp; block.y = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
blocks[k].trigger = trig; block.trigger = trig;
nblocks++;
break; break;
case SAFE: //Safe block case SAFE: //Safe block
blocks[k].type = SAFE; block.type = SAFE;
blocks[k].xp = xp; block.xp = xp;
blocks[k].yp = yp; block.yp = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
nblocks++;
break; break;
case ACTIVITY: //Activity Zone case ACTIVITY: //Activity Zone
blocks[k].type = ACTIVITY; block.type = ACTIVITY;
blocks[k].x = xp; block.x = xp;
blocks[k].y = yp; block.y = yp;
blocks[k].wp = w; block.wp = w;
blocks[k].hp = h; block.hp = h;
blocks[k].rectset(xp, yp, w, h); block.rectset(xp, yp, w, h);
//Ok, each and every activity zone in the game is initilised here. "Trig" in this case is a variable that //Ok, each and every activity zone in the game is initilised here. "Trig" in this case is a variable that
//assigns all the details. //assigns all the details.
switch(trig) switch(trig)
{ {
case 0: //testing zone case 0: //testing zone
blocks[k].prompt = "Press ENTER to explode"; block.prompt = "Press ENTER to explode";
blocks[k].script = "intro"; block.script = "intro";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=1; trig=1;
break; break;
case 1: case 1:
blocks[k].prompt = "Press ENTER to talk to Violet"; block.prompt = "Press ENTER to talk to Violet";
blocks[k].script = "talkpurple"; block.script = "talkpurple";
blocks[k].setblockcolour("purple"); block.setblockcolour("purple");
trig=0; trig=0;
break; break;
case 2: case 2:
blocks[k].prompt = "Press ENTER to talk to Vitellary"; block.prompt = "Press ENTER to talk to Vitellary";
blocks[k].script = "talkyellow"; block.script = "talkyellow";
blocks[k].setblockcolour("yellow"); block.setblockcolour("yellow");
trig=0; trig=0;
break; break;
case 3: case 3:
blocks[k].prompt = "Press ENTER to talk to Vermilion"; block.prompt = "Press ENTER to talk to Vermilion";
blocks[k].script = "talkred"; block.script = "talkred";
blocks[k].setblockcolour("red"); block.setblockcolour("red");
trig=0; trig=0;
break; break;
case 4: case 4:
blocks[k].prompt = "Press ENTER to talk to Verdigris"; block.prompt = "Press ENTER to talk to Verdigris";
blocks[k].script = "talkgreen"; block.script = "talkgreen";
blocks[k].setblockcolour("green"); block.setblockcolour("green");
trig=0; trig=0;
break; break;
case 5: case 5:
blocks[k].prompt = "Press ENTER to talk to Victoria"; block.prompt = "Press ENTER to talk to Victoria";
blocks[k].script = "talkblue"; block.script = "talkblue";
blocks[k].setblockcolour("blue"); block.setblockcolour("blue");
trig=0; trig=0;
break; break;
case 6: case 6:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_station_1"; block.script = "terminal_station_1";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 7: case 7:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_1"; block.script = "terminal_outside_1";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 8: case 8:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_2"; block.script = "terminal_outside_2";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 9: case 9:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_3"; block.script = "terminal_outside_3";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 10: case 10:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_4"; block.script = "terminal_outside_4";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 11: case 11:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_5"; block.script = "terminal_outside_5";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 12: case 12:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_outside_6"; block.script = "terminal_outside_6";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 13: case 13:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_finallevel"; block.script = "terminal_finallevel";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 14: case 14:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_station_2"; block.script = "terminal_station_2";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 15: case 15:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_station_3"; block.script = "terminal_station_3";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 16: case 16:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_station_4"; block.script = "terminal_station_4";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 17: case 17:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_warp_1"; block.script = "terminal_warp_1";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 18: case 18:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_warp_2"; block.script = "terminal_warp_2";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 19: case 19:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_lab_1"; block.script = "terminal_lab_1";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 20: case 20:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_lab_2"; block.script = "terminal_lab_2";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 21: case 21:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_secretlab"; block.script = "terminal_secretlab";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 22: case 22:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_shipcomputer"; block.script = "terminal_shipcomputer";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 23: case 23:
blocks[k].prompt = "Press ENTER to activate terminals"; block.prompt = "Press ENTER to activate terminals";
blocks[k].script = "terminal_radio"; block.script = "terminal_radio";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 24: case 24:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "terminal_jukebox"; block.script = "terminal_jukebox";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 25: case 25:
blocks[k].prompt = "Passion for Exploring"; block.prompt = "Passion for Exploring";
blocks[k].script = "terminal_juke1"; block.script = "terminal_juke1";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 26: case 26:
blocks[k].prompt = "Pushing Onwards"; block.prompt = "Pushing Onwards";
blocks[k].script = "terminal_juke2"; block.script = "terminal_juke2";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 27: case 27:
blocks[k].prompt = "Positive Force"; block.prompt = "Positive Force";
blocks[k].script = "terminal_juke3"; block.script = "terminal_juke3";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 28: case 28:
blocks[k].prompt = "Presenting VVVVVV"; block.prompt = "Presenting VVVVVV";
blocks[k].script = "terminal_juke4"; block.script = "terminal_juke4";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 29: case 29:
blocks[k].prompt = "Potential for Anything"; block.prompt = "Potential for Anything";
blocks[k].script = "terminal_juke5"; block.script = "terminal_juke5";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 30: case 30:
blocks[k].prompt = "Predestined Fate"; block.prompt = "Predestined Fate";
blocks[k].script = "terminal_juke6"; block.script = "terminal_juke6";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 31: case 31:
blocks[k].prompt = "Pipe Dream"; block.prompt = "Pipe Dream";
blocks[k].script = "terminal_juke7"; block.script = "terminal_juke7";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 32: case 32:
blocks[k].prompt = "Popular Potpourri"; block.prompt = "Popular Potpourri";
blocks[k].script = "terminal_juke8"; block.script = "terminal_juke8";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 33: case 33:
blocks[k].prompt = "Pressure Cooker"; block.prompt = "Pressure Cooker";
blocks[k].script = "terminal_juke9"; block.script = "terminal_juke9";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 34: case 34:
blocks[k].prompt = "ecroF evitisoP"; block.prompt = "ecroF evitisoP";
blocks[k].script = "terminal_juke10"; block.script = "terminal_juke10";
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
case 35: case 35:
blocks[k].prompt = "Press ENTER to activate terminal"; block.prompt = "Press ENTER to activate terminal";
blocks[k].script = "custom_"+customscript; block.script = "custom_"+customscript;
blocks[k].setblockcolour("orange"); block.setblockcolour("orange");
trig=0; trig=0;
break; break;
} }
nblocks++;
break; break;
} }
blocks.push_back(block);
} }
void entityclass::removeentity(int t) void entityclass::removeentity(int t)
@ -1107,39 +1070,31 @@ void entityclass::removeentity(int t)
void entityclass::removeallblocks() void entityclass::removeallblocks()
{ {
for(int i=0; i<nblocks; i++) blocks[i].clear(); blocks.clear();
nblocks=0;
} }
void entityclass::removeblock( int t ) void entityclass::removeblock( int t )
{ {
blocks[t].clear(); blocks.erase(blocks.begin() + t);
int i = nblocks - 1;
while (i >= 0 && !(blocks[i].active))
{
nblocks--;
i--;
}
} }
void entityclass::removeblockat( int x, int y ) void entityclass::removeblockat( int x, int y )
{ {
for (int i = 0; i < nblocks; i++) for (size_t i = 0; i < blocks.size(); i++)
{ {
if(blocks[i].xp == int(x) && blocks[i].yp == int(y)) removeblock(i); if(blocks[i].xp == int(x) && blocks[i].yp == int(y)) removeblock_iter(i);
} }
} }
void entityclass::removetrigger( int t ) void entityclass::removetrigger( int t )
{ {
for(int i=0; i<nblocks; i++) for(size_t i=0; i<blocks.size(); i++)
{ {
if(blocks[i].type == TRIGGER) if(blocks[i].type == TRIGGER)
{ {
if (blocks[i].trigger == t) if (blocks[i].trigger == t)
{ {
blocks[i].active = false; removeblock_iter(i);
removeblock(i);
} }
} }
} }
@ -3800,9 +3755,9 @@ bool entityclass::checkdamage()
temph = entities[i].h; temph = entities[i].h;
rectset(tempx, tempy, tempw, temph); rectset(tempx, tempy, tempw, temph);
for (int j=0; j<nblocks; j++) for (size_t j=0; j<blocks.size(); j++)
{ {
if (blocks[j].type == DAMAGE && blocks[j].active) if (blocks[j].type == DAMAGE)
{ {
if(help.intersects(blocks[j].rect, temprect)) if(help.intersects(blocks[j].rect, temprect))
{ {
@ -3828,9 +3783,9 @@ bool entityclass::scmcheckdamage()
temph = entities[i].h; temph = entities[i].h;
rectset(tempx, tempy, tempw, temph); rectset(tempx, tempy, tempw, temph);
for (int j=0; j<nblocks; j++) for (size_t j=0; j<blocks.size(); j++)
{ {
if (blocks[j].type == DAMAGE && blocks[j].active) if (blocks[j].type == DAMAGE)
{ {
if(help.intersects(blocks[j].rect, temprect)) if(help.intersects(blocks[j].rect, temprect))
{ {
@ -3866,9 +3821,9 @@ int entityclass::checktrigger()
temph = entities[i].h; temph = entities[i].h;
rectset(tempx, tempy, tempw, temph); rectset(tempx, tempy, tempw, temph);
for (int j=0; j<nblocks; j++) for (size_t j=0; j<blocks.size(); j++)
{ {
if (blocks[j].type == TRIGGER && blocks[j].active) if (blocks[j].type == TRIGGER)
{ {
if (help.intersects(blocks[j].rect, temprect)) if (help.intersects(blocks[j].rect, temprect))
{ {
@ -3895,9 +3850,9 @@ int entityclass::checkactivity()
temph = entities[i].h; temph = entities[i].h;
rectset(tempx, tempy, tempw, temph); rectset(tempx, tempy, tempw, temph);
for (int j=0; j<nblocks; j++) for (size_t j=0; j<blocks.size(); j++)
{ {
if (blocks[j].type == ACTIVITY && blocks[j].active) if (blocks[j].type == ACTIVITY)
{ {
if (help.intersects(blocks[j].rect, temprect)) if (help.intersects(blocks[j].rect, temprect))
{ {
@ -3930,9 +3885,9 @@ bool entityclass::cblocks( int t )
bool entityclass::checkplatform() bool entityclass::checkplatform()
{ {
//Return true if rectset intersects a moving platform, setups px & py to the platform x & y //Return true if rectset intersects a moving platform, setups px & py to the platform x & y
for (int i = 0; i < nblocks; i++) for (size_t i = 0; i < blocks.size(); i++)
{ {
if (blocks[i].active) if (true) //FIXME: remove this later (no more 'active')
{ {
if (blocks[i].type == BLOCK) if (blocks[i].type == BLOCK)
{ {
@ -3950,9 +3905,9 @@ bool entityclass::checkplatform()
bool entityclass::checkblocks() bool entityclass::checkblocks()
{ {
for (int i = 0; i < nblocks; i++) for (size_t i = 0; i < blocks.size(); i++)
{ {
if (blocks[i].active) if (true) //FIXME: remove this later (no more 'active')
{ {
if(!skipdirblocks) if(!skipdirblocks)
{ {

View file

@ -198,7 +198,6 @@ public:
std::vector<int> collect; std::vector<int> collect;
std::vector<int> customcollect; std::vector<int> customcollect;
int nblocks;
bool skipblocks, skipdirblocks; bool skipblocks, skipdirblocks;
int platformtile; int platformtile;