1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
#include "BlockGrid.h"
#include "minorGems/util/stringUtils.h"
void BlockGrid::initBlocks() {
for( int r=0; r<mBlockRows; r++ ) {
for( int c=0; c<mBlockColumns; c++ ) {
mBlocks[r][c] = NULL;
}
}
clearDirtyFlags();
}
BlockGrid::BlockGrid()
: mWorld( NULL ),
mDepthLayer( 0 ) {
initBlocks();
}
BlockGrid::BlockGrid( World *inWorld )
: mWorld( inWorld ),
mDepthLayer( 0 ) {
initBlocks();
}
BlockGrid::~BlockGrid() {
}
void BlockGrid::setWorld( World *inWorld ) {
mWorld = inWorld;
}
void BlockGrid::setDepthLayer( int inDepthLayer ) {
mDepthLayer = inDepthLayer;
}
char BlockGrid::add( Block *inBlock, int inX, int inY ) {
// 0,0 at center of our grid
int c = inX + mBlockColumns / 2;
int r = inY - 1;
if( c>=0 && c < mBlockColumns && r>=0 && r < mBlockRows ) {
// in bounds
if( ! isColumnFull( inX ) ) {
// there's room
inBlock->mX = inX;
inBlock->mY = inY;
inBlock->mDepthLayer = mDepthLayer;
mWorld->add( inBlock );
if( mBlocks[r][c] != NULL ) {
// other blocks in way
// cause all blocks at or above to rise
for( int r2=mBlockRows-2; r2>=r; r2-- ) {
if( mBlocks[r2][c] != NULL ) {
Block *b = mBlocks[r2][c];
// put in new grid spot
mBlocks[r2 + 1][c]= b;
mBlocks[r2][c] = NULL;
// rise to new world position
b->shiftTo( (r2 + 1) + 1 );
}
}
}
mBlocks[r][c] = inBlock;
mDirtyFlags[c] = true;
return true;
}
}
// failed to add
return false;
}
SimpleVector<Block *> *BlockGrid::getAllBlocks() {
SimpleVector<Block *> *returnVector = new SimpleVector<Block *>();
for( int r=0; r<mBlockRows; r++ ) {
for( int c=0; c<mBlockColumns; c++ ) {
if( mBlocks[r][c] != NULL ) {
returnVector->push_back( mBlocks[r][c] );
}
}
}
return returnVector;
}
Block *BlockGrid::getBlock( int inX, int inY ) {
int c = worldToGridC( inX );
int r = worldToGridR( inY );
if( c>=0 && c < mBlockColumns && r>=0 && r < mBlockRows ) {
// in bounds
return mBlocks[r][c];
}
return NULL;
}
Block *BlockGrid::removeBlock( int inX, int inY ) {
int c = worldToGridC( inX );
int r = worldToGridR( inY );
if( c>=0 && c < mBlockColumns && r>=0 && r < mBlockRows ) {
// in bounds
Block *returnBlock = mBlocks[r][c];
mBlocks[r][c] = NULL;
mWorld->remove( returnBlock );
// cancel any shift for this block
returnBlock->stopShifting();
// cause all blocks above to fall
for( int r2=r+1; r2<mBlockRows; r2++ ) {
if( mBlocks[r2][c] != NULL ) {
Block *b = mBlocks[r2][c];
// put in new grid spot
mBlocks[r2 - 1][c]= b;
mBlocks[r2][c] = NULL;
// fall to new world position
b->shiftTo( (r2 - 1) + 1 );
}
}
mDirtyFlags[c] = true;
return returnBlock;
}
return NULL;
}
char BlockGrid::isColumnFull( int inX ) {
int c = worldToGridC( inX );
if( c>=0 && c < mBlockColumns ) {
// in bounds
// check top spot only
if( mBlocks[ mBlockRows - 1 ][c] != NULL ) {
return true;
}
else {
return false;
}
}
else {
// out of bounds always full
return true;
}
}
SimpleVector<char*> *BlockGrid::getDirtyColumns() {
SimpleVector<char*> *returnVector = new SimpleVector<char *>();
for( int c=0; c<mBlockColumns; c++ ) {
if( mDirtyFlags[c] ) {
char *columnString = autoSprintf( "%d", c );
int r=0;
while( r<mBlockRows && mBlocks[r][c] != NULL ) {
char *blockString =
autoSprintf( "_%s", mBlocks[r][c]->getColorSignature() );
char *newColumnString = concatonate( columnString,
blockString );
delete [] blockString;
delete [] columnString;
columnString = newColumnString;
r++;
}
if( r == 0 ) {
// empty column
// add _ after column number
delete [] columnString;
columnString = autoSprintf( "%d_", c );
}
returnVector->push_back( columnString );
}
}
return returnVector;
}
void BlockGrid::clearDirtyFlags() {
for( int c=0; c<mBlockColumns; c++ ) {
mDirtyFlags[c] = false;
}
}
void BlockGrid::setColumns( SimpleVector<char*> *inColumns ) {
int numToSet = inColumns->size();
for( int i=0; i<numToSet; i++ ) {
int numParts;
char **parts =
split( *( inColumns->getElement( i ) ), "_", &numParts );
// first part is column number
if( numParts > 0 ) {
int c;
int numRead = sscanf( parts[0], "%d", &c );
if( numRead == 1 ) {
// first clear the column
int r=0;
while( r<mBlockRows && mBlocks[r][c] != NULL ) {
Block *b = mBlocks[r][c];
mBlocks[r][c] = NULL;
mWorld->remove( b );
delete b;
r++;
}
// now see if the new column has any blocks
for( int p=1; p<numParts; p++ ) {
// make sure part not empty
if( strcmp( parts[p], "" ) != 0 ) {
r = p-1;
// add blocks here
Block *b = new Block( parts[p] );
b->mX = gridToWorldX( c );
b->mY = gridToWorldY( r );
b->mDepthLayer = mDepthLayer;
mBlocks[r][c] = b;
mWorld->add( b );
}
}
}
}
for( int p=0; p<numParts; p++ ) {
delete [] parts[p];
}
delete [] parts;
}
}
|