translate wall grids when shifting stamps by more than 4 pixels

This commit is contained in:
jacob1 2017-10-04 20:56:50 -04:00
parent 4ff0a9f52c
commit 97c4123533
2 changed files with 36 additions and 11 deletions

View File

@ -162,6 +162,7 @@ void GameSave::InitVars()
gravityMode = 0; gravityMode = 0;
airMode = 0; airMode = 0;
edgeMode = 0; edgeMode = 0;
translated.x = translated.y = 0;
} }
bool GameSave::Collapsed() bool GameSave::Collapsed()
@ -266,6 +267,7 @@ vector2d GameSave::Translate(vector2d translate)
Expand(); Expand();
int nx, ny; int nx, ny;
vector2d pos; vector2d pos;
vector2d translateReal = translate;
float minx = 0, miny = 0, maxx = 0, maxy = 0; float minx = 0, miny = 0, maxx = 0, maxy = 0;
// determine minimum and maximum position of all particles / signs // determine minimum and maximum position of all particles / signs
for (size_t i = 0; i < signs.size(); i++) for (size_t i = 0; i < signs.size(); i++)
@ -320,24 +322,25 @@ vector2d GameSave::Translate(vector2d translate)
// call Transform to do the transformation we wanted when calling this function // call Transform to do the transformation we wanted when calling this function
translate = v2d_add(translate, v2d_multiply_float(backCorrection, CELL)); translate = v2d_add(translate, v2d_multiply_float(backCorrection, CELL));
Transform(m2d_identity, translate, Transform(m2d_identity, translate, translateReal,
(blockWidth + backCorrection.x + frontCorrection.x) * CELL, (blockWidth + backCorrection.x + frontCorrection.x) * CELL,
(blockHeight + backCorrection.y + frontCorrection.y) * CELL (blockHeight + backCorrection.y + frontCorrection.y) * CELL
); );
// return how much we corrected. This is used to offset the position of the current stamp // return how much we corrected. This is used to offset the position of the current stamp
// otherwise it would attempt to recenter it with the current height // otherwise it would attempt to recenter it with the current size
return v2d_add(v2d_multiply_float(backCorrection, -CELL), v2d_multiply_float(frontCorrection, CELL)); return v2d_add(v2d_multiply_float(backCorrection, -CELL), v2d_multiply_float(frontCorrection, CELL));
} }
void GameSave::Transform(matrix2d transform, vector2d translate) void GameSave::Transform(matrix2d transform, vector2d translate)
{ {
if(Collapsed()) if (Collapsed())
Expand(); Expand();
int width = blockWidth*CELL, height = blockHeight*CELL, newWidth, newHeight; int width = blockWidth*CELL, height = blockHeight*CELL, newWidth, newHeight;
vector2d tmp, ctl, cbr; vector2d tmp, ctl, cbr;
vector2d cornerso[4]; vector2d cornerso[4];
vector2d translateReal = translate;
// undo any translation caused by rotation // undo any translation caused by rotation
cornerso[0] = v2d_new(0,0); cornerso[0] = v2d_new(0,0);
cornerso[1] = v2d_new(width-1,0); cornerso[1] = v2d_new(width-1,0);
@ -357,12 +360,15 @@ void GameSave::Transform(matrix2d transform, vector2d translate)
translate = v2d_sub(translate,tmp); translate = v2d_sub(translate,tmp);
newWidth = floor(cbr.x+0.5f)-floor(ctl.x+0.5f)+1; newWidth = floor(cbr.x+0.5f)-floor(ctl.x+0.5f)+1;
newHeight = floor(cbr.y+0.5f)-floor(ctl.y+0.5f)+1; newHeight = floor(cbr.y+0.5f)-floor(ctl.y+0.5f)+1;
Transform(transform, translate, newWidth, newHeight); Transform(transform, translate, translateReal, newWidth, newHeight);
} }
void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, int newHeight) // transform is a matrix describing how we want to rotate this save
// translate can vary depending on whether the save is bring rotated, or if a normal translate caused it to expand
// translateReal is the original amount we tried to translate, used to calculate wall shifting
void GameSave::Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight)
{ {
if(Collapsed()) if (Collapsed())
Expand(); Expand();
if (newWidth>XRES) newWidth = XRES; if (newWidth>XRES) newWidth = XRES;
@ -416,14 +422,30 @@ void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, i
particles[i].vx = vel.x; particles[i].vx = vel.x;
particles[i].vy = vel.y; particles[i].vy = vel.y;
} }
// translate walls and other grid items when the stamp is shifted more than 4 pixels in any direction
int translateX = 0, translateY = 0;
if (translateReal.x > 0 && ((int)translated.x%CELL == 3
|| (translated.x < 0 && (int)translated.x%CELL == 0)))
translateX = CELL;
else if (translateReal.x < 0 && ((int)translated.x%CELL == -3
|| (translated.x > 0 && (int)translated.x%CELL == 0)))
translateX = -CELL;
if (translateReal.y > 0 && ((int)translated.y%CELL == 3
|| (translated.y < 0 && (int)translated.y%CELL == 0)))
translateY = CELL;
else if (translateReal.y < 0 && ((int)translated.y%CELL == -3
|| (translated.y > 0 && (int)translated.y%CELL == 0)))
translateY = -CELL;
for (y=0; y<blockHeight; y++) for (y=0; y<blockHeight; y++)
for (x=0; x<blockWidth; x++) for (x=0; x<blockWidth; x++)
{ {
pos = v2d_new(x*CELL+CELL*0.4f, y*CELL+CELL*0.4f); pos = v2d_new(x*CELL+CELL*0.4f+translateX, y*CELL+CELL*0.4f+translateY);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate); pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = pos.x/CELL; nx = pos.x/CELL;
ny = pos.y/CELL; ny = pos.y/CELL;
if (nx<0 || nx>=newBlockWidth || ny<0 || ny>=newBlockHeight) if (pos.x<0 || nx>=newBlockWidth || pos.y<0 || ny>=newBlockHeight)
continue; continue;
if (blockMap[y][x]) if (blockMap[y][x])
{ {
@ -441,6 +463,7 @@ void GameSave::Transform(matrix2d transform, vector2d translate, int newWidth, i
velocityYNew[ny][nx] = velocityY[y][x]; velocityYNew[ny][nx] = velocityY[y][x];
ambientHeatNew[ny][nx] = ambientHeat[y][x]; ambientHeatNew[ny][nx] = ambientHeat[y][x];
} }
translated = v2d_add(m2d_multiply_v2d(transform, translated), translateReal);
for (int j = 0; j < blockHeight; j++) for (int j = 0; j < blockHeight; j++)
{ {

View File

@ -78,7 +78,7 @@ public:
std::vector<char> Serialise(); std::vector<char> Serialise();
vector2d Translate(vector2d translate); vector2d Translate(vector2d translate);
void Transform(matrix2d transform, vector2d translate); void Transform(matrix2d transform, vector2d translate);
void Transform(matrix2d transform, vector2d translate, int newWidth, int newHeight); void Transform(matrix2d transform, vector2d translate, vector2d translateReal, int newWidth, int newHeight);
void Expand(); void Expand();
void Collapse(); void Collapse();
@ -103,6 +103,8 @@ public:
private: private:
bool expanded; bool expanded;
bool hasOriginalData; bool hasOriginalData;
// number of pixels translated. When translating CELL pixels, shift all CELL grids
vector2d translated;
std::vector<char> originalData; std::vector<char> originalData;