Corrected issues with indices desyncing when objects are deleted

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 3821e87585
commit 8ee9e1de23
  1. BIN
      C++ProjectTemplate
  2. 133
      main.cpp
  3. 2
      pixelGameEngine.h
  4. 1
      states.h

Binary file not shown.

@ -109,17 +109,25 @@ class Object{
return; return;
} else { } else {
if (move.y<0) { if (move.y<0) {
if (objArrElement>0&&OBJECTS[objArrElement-1]->pos.y+OBJECTS[objArrElement-1]->originPoint.y>pos.y+originPoint.y+move.y) { while (objArrElement>0&&OBJECTS[objArrElement-1]->pos.y+OBJECTS[objArrElement-1]->originPoint.y>pos.y+originPoint.y+move.y) {
//printf("%p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement-1],OBJECTS[objArrElement-1]->name.c_str());
OBJECTS[objArrElement]=OBJECTS[objArrElement-1]; OBJECTS[objArrElement]=OBJECTS[objArrElement-1];
//printf(" %p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement-1],OBJECTS[objArrElement-1]->name.c_str());
OBJECTS[objArrElement-1]=this; OBJECTS[objArrElement-1]=this;
//printf(" %p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement-1],OBJECTS[objArrElement-1]->name.c_str());
OBJECTS[objArrElement]->objArrElement=objArrElement; OBJECTS[objArrElement]->objArrElement=objArrElement;
//printf(" %p(%s)(%d)<->%p(%s)(%d)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement]->objArrElement,OBJECTS[objArrElement-1],OBJECTS[objArrElement-1]->name.c_str(),objArrElement-1);
objArrElement--; objArrElement--;
} }
} else { } else {
if (objArrElement<OBJECTS.size()-1&&OBJECTS[objArrElement+1]->pos.y+OBJECTS[objArrElement+1]->originPoint.y<pos.y+originPoint.y+move.y) { while (objArrElement<OBJECTS.size()-1&&OBJECTS[objArrElement+1]->pos.y+OBJECTS[objArrElement+1]->originPoint.y<pos.y+originPoint.y+move.y) {
//printf("%p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement+1],OBJECTS[objArrElement+1]->name.c_str());
OBJECTS[objArrElement]=OBJECTS[objArrElement+1]; OBJECTS[objArrElement]=OBJECTS[objArrElement+1];
//printf(" %p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement+1],OBJECTS[objArrElement+1]->name.c_str());
OBJECTS[objArrElement+1]=this; OBJECTS[objArrElement+1]=this;
//printf(" %p(%s)<->%p(%s)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement+1],OBJECTS[objArrElement+1]->name.c_str());
OBJECTS[objArrElement]->objArrElement=objArrElement; OBJECTS[objArrElement]->objArrElement=objArrElement;
//printf(" %p(%s)(%d)<->%p(%s)(%d)\n",OBJECTS[objArrElement],OBJECTS[objArrElement]->name.c_str(),OBJECTS[objArrElement]->objArrElement,OBJECTS[objArrElement+1],OBJECTS[objArrElement+1]->name.c_str(),objArrElement+1);
objArrElement++; objArrElement++;
} }
} }
@ -526,6 +534,8 @@ goes on a very long time, I hope you can understand this is only for testing pur
if (OBJECTS[i]->temp) { if (OBJECTS[i]->temp) {
delete OBJECTS[i]; delete OBJECTS[i];
OBJECTS.erase(OBJECTS.begin()+i--); OBJECTS.erase(OBJECTS.begin()+i--);
} else {
OBJECTS[i]->objArrElement=i;
} }
} }
CurrentCutscene->CleanupCutscene(); CurrentCutscene->CleanupCutscene();
@ -622,6 +632,30 @@ goes on a very long time, I hope you can understand this is only for testing pur
} }
} }
}break; }break;
case GameState::GAME_WORLD:{
if (PlayerCanMove()) {
if (UpHeld()) {
if (PARTY_MEMBER_OBJ[0]->SmoothMove({0,-1})) {
UpdatePlayerTrail({0,-1});
}
}
if (DownHeld()) {
if (PARTY_MEMBER_OBJ[0]->SmoothMove({0,1})) {
UpdatePlayerTrail({0,1});
}
}
if (LeftHeld()) {
if (PARTY_MEMBER_OBJ[0]->SmoothMove({-1,0})) {
UpdatePlayerTrail({-1,0});
}
}
if (RightHeld()) {
if (PARTY_MEMBER_OBJ[0]->SmoothMove({1,0})) {
UpdatePlayerTrail({1,0});
}
}
}
}break;
case GameState::EDITOR:{ case GameState::EDITOR:{
if (TabHeld()) { if (TabHeld()) {
GAME_STATE=GameState::TILE_SELECT; GAME_STATE=GameState::TILE_SELECT;
@ -775,10 +809,6 @@ goes on a very long time, I hope you can understand this is only for testing pur
if (GetKey(F1).bPressed) { if (GetKey(F1).bPressed) {
ConsoleShow(F1,false); ConsoleShow(F1,false);
} }
if (GetKey(F2).bPressed) {
SaveMap(CURRENT_MAP);
printf("Map Saved\n");
}
switch (GAME_STATE) { switch (GAME_STATE) {
case GameState::TILE_SELECT:{ case GameState::TILE_SELECT:{
@ -801,7 +831,19 @@ goes on a very long time, I hope you can understand this is only for testing pur
SELECTED_TILE.y=(SELECTED_TILE.y+1)%(TILEMAP_SIZE_Y/32); SELECTED_TILE.y=(SELECTED_TILE.y+1)%(TILEMAP_SIZE_Y/32);
} }
}break; }break;
case GameState::GAME_WORLD:{
if (GetKey(F5).bPressed) {
GAME_STATE=GameState::EDITOR;
}
}break;
case GameState::EDITOR:{ case GameState::EDITOR:{
if (GetKey(F2).bPressed) {
SaveMap(CURRENT_MAP);
printf("Map Saved\n");
}
if (GetKey(F5).bPressed) {
GAME_STATE=GameState::GAME_WORLD;
}
if (GetKey(K1).bPressed) { if (GetKey(K1).bPressed) {
EDITING_LAYER=layer::COLLISION; EDITING_LAYER=layer::COLLISION;
EnableLayer(layer::COLLISION,true); EnableLayer(layer::COLLISION,true);
@ -834,6 +876,8 @@ goes on a very long time, I hope you can understand this is only for testing pur
if (OBJECTS[i]->GetPos()==HIGHLIGHTED_TILE*32) { if (OBJECTS[i]->GetPos()==HIGHLIGHTED_TILE*32) {
delete OBJECTS[i]; delete OBJECTS[i];
OBJECTS.erase(OBJECTS.begin()+i--); OBJECTS.erase(OBJECTS.begin()+i--);
} else {
OBJECTS[i]->objArrElement=i;
} }
} }
} }
@ -857,42 +901,11 @@ goes on a very long time, I hope you can understand this is only for testing pur
obj->drawn=false; obj->drawn=false;
} }
switch (GAME_STATE) { switch (GAME_STATE) {
case GameState::GAME_WORLD:{
DrawGameWorld();
}break;
case GameState::EDITOR:{ case GameState::EDITOR:{
for (int y=-1;y<HEIGHT/32+2;y++) { DrawGameWorld();
int yTileOffset = cameraPos.y/32;
for (auto&obj:OBJECTS) {
if (!obj->drawn&&obj->GetPos().y+obj->originPoint.y>(y+yTileOffset)*32&&obj->GetPos().y+obj->originPoint.y<=(y+yTileOffset+1)*32) {
obj->drawn=true;
SetDrawTarget(layer::DYNAMIC);
DrawPartialDecal(obj->GetPos()-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->GetScale(),obj->color);
}
}
for (int x=-1;x<WIDTH/32+2;x++) {
int xTileOffset = cameraPos.x/32;
if (x+xTileOffset>=0&&x+xTileOffset<MAP_WIDTH&&y+yTileOffset>=0&&y+yTileOffset<MAP_HEIGHT) {
if(MAP5[y+yTileOffset][x+xTileOffset]->tileX*MAP5[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::COLLISION);
DrawPartialSprite({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"]->sprite,{MAP5[y+yTileOffset][x+xTileOffset]->tileX*32,MAP5[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP4[y+yTileOffset][x+xTileOffset]->tileX*MAP4[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::BACKGROUND);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP4[y+yTileOffset][x+xTileOffset]->tileX*32,MAP4[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP3[y+yTileOffset][x+xTileOffset]->tileX*MAP3[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::GROUND);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP3[y+yTileOffset][x+xTileOffset]->tileX*32,MAP3[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP2[y+yTileOffset][x+xTileOffset]->tileX*MAP2[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::DYNAMIC);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP2[y+yTileOffset][x+xTileOffset]->tileX*32,MAP2[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP[y+yTileOffset][x+xTileOffset]->tileX*MAP[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::HIGH);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP[y+yTileOffset][x+xTileOffset]->tileX*32,MAP[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
}
}
}
SetDrawTarget(nullptr); SetDrawTarget(nullptr);
DrawRectDecal((HIGHLIGHTED_TILE)*32-cameraPos,{32,32},YELLOW); DrawRectDecal((HIGHLIGHTED_TILE)*32-cameraPos,{32,32},YELLOW);
/*if (EDITING_LAYER==layer::OBJECT) { /*if (EDITING_LAYER==layer::OBJECT) {
@ -950,6 +963,44 @@ goes on a very long time, I hope you can understand this is only for testing pur
FillRectDecal({0,0},{WIDTH,HEIGHT},Pixel(0,0,0,(int)CUTSCENE_FADE_VALUE)); FillRectDecal({0,0},{WIDTH,HEIGHT},Pixel(0,0,0,(int)CUTSCENE_FADE_VALUE));
}; };
void DrawGameWorld() {
for (int y=-1;y<HEIGHT/32+2;y++) {
int yTileOffset = cameraPos.y/32;
for (auto&obj:OBJECTS) {
if (!obj->drawn&&obj->GetPos().y+obj->originPoint.y>(y+yTileOffset)*32&&obj->GetPos().y+obj->originPoint.y<=(y+yTileOffset+1)*32) {
obj->drawn=true;
SetDrawTarget(layer::DYNAMIC);
DrawPartialDecal(obj->GetPos()-cameraPos,obj->spr->spr,{(obj->frameIndex%obj->spr->frames)*obj->spr->width,0},{obj->spr->width,obj->spr->spr->sprite->height},obj->GetScale(),obj->color);
}
}
for (int x=-1;x<WIDTH/32+2;x++) {
int xTileOffset = cameraPos.x/32;
if (x+xTileOffset>=0&&x+xTileOffset<MAP_WIDTH&&y+yTileOffset>=0&&y+yTileOffset<MAP_HEIGHT) {
if(MAP5[y+yTileOffset][x+xTileOffset]->tileX*MAP5[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::COLLISION);
DrawPartialSprite({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"]->sprite,{MAP5[y+yTileOffset][x+xTileOffset]->tileX*32,MAP5[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP4[y+yTileOffset][x+xTileOffset]->tileX*MAP4[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::BACKGROUND);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP4[y+yTileOffset][x+xTileOffset]->tileX*32,MAP4[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP3[y+yTileOffset][x+xTileOffset]->tileX*MAP3[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::GROUND);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP3[y+yTileOffset][x+xTileOffset]->tileX*32,MAP3[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP2[y+yTileOffset][x+xTileOffset]->tileX*MAP2[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::DYNAMIC);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP2[y+yTileOffset][x+xTileOffset]->tileX*32,MAP2[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
if(MAP[y+yTileOffset][x+xTileOffset]->tileX*MAP[y+yTileOffset][x+xTileOffset]->tileY!=225) {
SetDrawTarget(layer::HIGH);
DrawPartialDecal({x*32-fmod(cameraPos.x,32),y*32-fmod(cameraPos.y,32)},SPRITES["terrainmap.png"],{MAP[y+yTileOffset][x+xTileOffset]->tileX*32,MAP[y+yTileOffset][x+xTileOffset]->tileY*32},{32,32});
}
}
}
}
}
void DrawFancyStringDecal(const olc::vf2d& pos, const std::wstring& sText, const Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f }) { void DrawFancyStringDecal(const olc::vf2d& pos, const std::wstring& sText, const Pixel col = olc::WHITE, const olc::vf2d& scale = { 1.0f, 1.0f }) {
vf2d newpos=pos; vf2d newpos=pos;
for (int i=0;i<sText.length();i++) { for (int i=0;i<sText.length();i++) {

@ -3554,7 +3554,7 @@ namespace olc
nLastFPS = nFrameCount; nLastFPS = nFrameCount;
fFrameTimer -= 1.0f; fFrameTimer -= 1.0f;
std::string sTitle = "OneLoneCoder.com - Pixel Game Engine - " + sAppName + " - FPS: " + std::to_string(nFrameCount); std::string sTitle = "OneLoneCoder.com - Pixel Game Engine - " + sAppName + " - FPS: " + std::to_string(nFrameCount);
platform->SetWindowTitle("sTitle"); platform->SetWindowTitle(sTitle);
nFrameCount = 0; nFrameCount = 0;
} }
} }

@ -3,5 +3,6 @@ namespace GameState{
EDITOR, EDITOR,
TILE_SELECT, TILE_SELECT,
OBJ_SELECT, OBJ_SELECT,
GAME_WORLD,
}; };
} }
Loading…
Cancel
Save