1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-25 22:18:30 +02: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()
{
active = false;
type = 0;
trigger = 0;

View File

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

View File

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

View File

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