5c0341b7a6534169101c68bd2d74e25bd3d6e7a6.svn-base (7813B)
1 #include "Terrain.h" 2 #include "DiagnosticDraw.h" 3 4 5 TerrainCollisionManager::TerrainCollisionManager(SDL_Surface* Col_Map) 6 { 7 mCol_Map = Col_Map; 8 } 9 10 bool TerrainCollisionManager::DetectTerrainIntersect(SDL_Rect* Bounds, int ttype1, int ttype2) 11 { 12 if (mCol_Map->format->format != SDL_PIXELFORMAT_INDEX8) 13 { 14 gCons->ConsPrintf("Non-INDEX8 Collision Map Detected\n"); 15 return false; 16 } 17 Uint8* PixMap = (Uint8*)mCol_Map->pixels; 18 if (Bounds->y < 0) 19 { 20 Bounds->y = 0; 21 } 22 if (Bounds->y > (mCol_Map->h - Bounds->h)) 23 { 24 Bounds->y = (mCol_Map->h - Bounds->h); 25 } 26 for (int y = Bounds->y; y < (Bounds->y + Bounds->h); y++) 27 { 28 if (Bounds->x < 0) 29 { 30 Bounds->x = 0; 31 } 32 if (Bounds->x > (mCol_Map->pitch - Bounds->w)) 33 { 34 Bounds->x = (mCol_Map->pitch - Bounds->w); 35 } 36 37 if (ttype2 == -1) 38 { 39 for (int x = Bounds->x; x < (Bounds->x + Bounds->w); x++) 40 { 41 if (PixMap[(y*mCol_Map->pitch) + x] == ttype1) 42 { 43 //gCons->ConsPrintf("ground collision detected \n"); 44 //gDiagDraw->LogPixel(x, y); 45 return true; 46 } 47 else 48 { 49 //gCons->ConsPrintf("no collision detected \n"); 50 } 51 } 52 } 53 else 54 { 55 for (int x = Bounds->x; x < (Bounds->x + Bounds->w); x++) 56 { 57 if ( (PixMap[(y*mCol_Map->pitch) + x] == ttype1) || (PixMap[(y*mCol_Map->pitch) + x] == ttype2) ) 58 { 59 //gCons->ConsPrintf("ground collision detected \n"); 60 //gDiagDraw->LogPixel(x, y); 61 return true; 62 } 63 } 64 } 65 66 } 67 return false; 68 } 69 70 bool TerrainCollisionManager::DetectWorldEscape(SDL_Rect* Bounds) 71 { 72 //if(Bounds->x) 73 return false; 74 } 75 76 void TerrainCollisionManager::DrawTerrain() 77 { 78 SDL_Rect bds; 79 SDL_Rect* Bounds = &bds; 80 Bounds->x = 0; 81 Bounds->y = 0; 82 Bounds->h = mCol_Map->h; 83 Bounds->w = mCol_Map->w; 84 85 if (mCol_Map->format->format != SDL_PIXELFORMAT_INDEX8) 86 { 87 gCons->ConsPrintf("Non-INDEX8 Collision Map Detected\n"); 88 } 89 Uint8* PixMap = (Uint8*)mCol_Map->pixels; 90 if (Bounds->y < 0) 91 { 92 Bounds->y = 0; 93 } 94 if (Bounds->y >(mCol_Map->h - Bounds->h)) 95 { 96 Bounds->y = (mCol_Map->h - Bounds->h); 97 } 98 for (int y = Bounds->y; y < (Bounds->y + Bounds->h); y++) 99 { 100 if (Bounds->x < 0) 101 { 102 Bounds->x = 0; 103 } 104 if (Bounds->x >(mCol_Map->pitch - Bounds->w)) 105 { 106 Bounds->x = (mCol_Map->pitch - Bounds->w); 107 } 108 109 for (int x = Bounds->x; x < (Bounds->x + Bounds->w); x++) 110 { 111 if ((PixMap[(y*mCol_Map->pitch) + x] == GROUND) || (PixMap[(y*mCol_Map->pitch) + x] == PLATFORM)) 112 { 113 //gCons->ConsPrintf("ground collision detected \n"); 114 gDiagDraw->LogPixel(x, y); 115 } 116 } 117 } 118 } 119 120 //colman_Character functions 121 colman_Character::colman_Character(SDL_Surface* Col_Map, SDL_Rect* PosData, SDL_Rect* DestData, int EdgeThickness) : TerrainCollisionManager(Col_Map) 122 { 123 mPlatformCollision = true; 124 125 mWallThickness = EdgeThickness; 126 127 mPosition = PosData; 128 mDestination = DestData; 129 } 130 131 bool colman_Character::DetectEdgeCollision(SDL_Rect* Bounds) 132 { 133 SDL_Rect LowerBounder; 134 LowerBounder.h = mWallThickness; 135 LowerBounder.w = Bounds->w; 136 LowerBounder.x = Bounds->x; 137 LowerBounder.y = ((Bounds->y + Bounds->h) - LowerBounder.h); 138 139 SDL_Rect UpperBounder; 140 UpperBounder.h = mWallThickness; 141 UpperBounder.w = Bounds->w; 142 UpperBounder.x = Bounds->x; 143 UpperBounder.y = Bounds->y; 144 145 SDL_Rect RightBounder; 146 RightBounder.w = mWallThickness; 147 RightBounder.x = ((Bounds->x + Bounds->w) - RightBounder.w); 148 RightBounder.h = Bounds->h; 149 RightBounder.y = Bounds->y; 150 151 SDL_Rect LeftBounder; 152 LeftBounder.w = mWallThickness; 153 LeftBounder.x = Bounds->x; 154 LeftBounder.h = Bounds->h; 155 LeftBounder.y = Bounds->y; 156 157 158 if (mPlatformCollision == true) 159 { 160 if ((DetectTerrainIntersect(&LowerBounder, GROUND, PLATFORM) == true) || 161 (DetectTerrainIntersect(&UpperBounder, GROUND ) == true) || 162 (DetectTerrainIntersect(&RightBounder, GROUND ) == true) || 163 (DetectTerrainIntersect(&LeftBounder , GROUND ) == true)) 164 { 165 return true; 166 } 167 else 168 { 169 return false; 170 } 171 } 172 else //PlatformCollision if false 173 { 174 if ((DetectTerrainIntersect(&LowerBounder, GROUND) == true) || 175 (DetectTerrainIntersect(&UpperBounder, GROUND) == true) || 176 (DetectTerrainIntersect(&RightBounder, GROUND) == true) || 177 (DetectTerrainIntersect(&LeftBounder , GROUND) == true)) 178 { 179 return true; 180 } 181 else 182 { 183 return false; 184 } 185 } 186 } 187 188 int colman_Character::DetectIncline(int StepSizeLimit) //StepSizeLimit is the smallest step that CANNOT be climbed 189 { 190 SDL_Rect Scanner = *mDestination; 191 Scanner.h = 1; 192 193 if (mPlatformCollision == true) 194 { 195 for (int i = 0; i < StepSizeLimit; i++) 196 { 197 Scanner.y = ((mDestination->y + mDestination->h) - StepSizeLimit) + i; 198 if (DetectTerrainIntersect(&Scanner, GROUND, PLATFORM) == true) 199 { 200 //gCons->ConsPrintf("Step is %i Pixels tall \n", (StepSizeLimit - i) ); 201 return (StepSizeLimit - i); 202 } 203 } 204 } 205 else 206 { 207 for (int i = 0; i < StepSizeLimit; i++) 208 { 209 Scanner.y = ((mDestination->y + mDestination->h) - StepSizeLimit) + i; 210 if (DetectTerrainIntersect(&Scanner, GROUND) == true) 211 { 212 //gCons->ConsPrintf("Step is %i Pixels tall \n", (StepSizeLimit - i) ); 213 return (StepSizeLimit - i); 214 } 215 } 216 } 217 return StepSizeLimit; 218 } 219 220 bool colman_Character::DetectFalling() 221 { 222 SDL_Rect GroundDetector = *mPosition; 223 GroundDetector.y = (mPosition->y + mPosition->h); 224 GroundDetector.h = 1; 225 226 return !( DetectTerrainIntersect(&GroundDetector, GROUND, PLATFORM)); 227 } 228 229 bool colman_Character::DetectDirectionalCollision(SDL_Rect* bounds, int dir) 230 { 231 SDL_Rect bumper = *bounds; 232 switch (dir) 233 { 234 case LEFT: 235 bumper.x -= 1; 236 bumper.w = 1; 237 bumper.h -= 4; 238 break; 239 case RIGHT: 240 bumper.x += bumper.w; 241 bumper.w = 1; 242 bumper.h -= 4; 243 break; 244 case UP: 245 bumper.h = 1; 246 break; 247 case DOWN: 248 bumper.y += bumper.h; 249 bumper.h = 1; 250 break; 251 } 252 253 if ((mPlatformCollision == true) && (dir == DOWN)) 254 { 255 if (DetectTerrainIntersect(&bumper, GROUND, PLATFORM)) 256 { 257 return true; 258 } 259 else 260 { 261 return false; 262 } 263 } 264 else //PlatformCollision if false 265 { 266 if (DetectTerrainIntersect(&bumper, GROUND)) 267 { 268 return true; 269 } 270 else 271 { 272 return false; 273 } 274 } 275 } 276 277 bool colman_Character::DetectSideCollision(SDL_Rect* Bounds) 278 { 279 280 SDL_Rect RightDetector; 281 RightDetector.w = 1; 282 RightDetector.y = Bounds->y; 283 RightDetector.x = (Bounds->x + Bounds->w); 284 RightDetector.h = Bounds->h - 1; 285 286 SDL_Rect LeftDetector; 287 LeftDetector.w = 1; 288 LeftDetector.y = Bounds->y; 289 LeftDetector.x = (Bounds->x - 1); 290 LeftDetector.h = Bounds->h - 1; 291 292 return (DetectTerrainIntersect(&LeftDetector, GROUND) | DetectTerrainIntersect(&RightDetector, GROUND)); 293 } 294 295 bool colman_Character::DetectSwim() 296 { 297 SDL_Rect Under; 298 Under.h = 1; 299 Under.w = mPosition->w; 300 Under.x = mPosition->x; 301 Under.y = (mPosition->y + 6);//6 shouldn't hardcoded 302 303 return DetectTerrainIntersect(&Under, WATER); 304 } 305 306 bool colman_Character::DetectSurface() 307 { 308 SDL_Rect Under; 309 Under.h = 1; 310 Under.w = mPosition->w; 311 Under.x = mPosition->x; 312 Under.y = (mPosition->y + 6);//6 shouldn't hardcoded 313 314 SDL_Rect Over = Under; 315 Over.y -= 1; 316 317 //gDiagDraw->LogDiagRect(Over); 318 //gDiagDraw->LogDiagRect(Under); 319 320 if ((DetectTerrainIntersect(&Under, WATER) == true) && (DetectTerrainIntersect(&Over, AIR) == true)) 321 { 322 //gCons->ConsPrintf("surface detected\n"); 323 return true; 324 } 325 else 326 { 327 return false; 328 } 329 } 330 331 int colman_Character::FindSurface() 332 { 333 SDL_Rect surface_scan = *mPosition; 334 surface_scan.w = 1; 335 surface_scan.h = 1; 336 for (int y = 0; y < mPosition->h; y++) 337 { 338 surface_scan.y += y; 339 if (DetectTerrainIntersect(&surface_scan, WATER)) 340 { 341 return surface_scan.y - 1; 342 gCons->ConsPrintf("Surface Found at y = %i", surface_scan.y); 343 } 344 } 345 return -1; 346 } 347 348 bool colman_Character::DetectWade() 349 { 350 SDL_Rect LowerBounder = *mPosition; 351 LowerBounder.h = mWallThickness; 352 LowerBounder.y = ((mPosition->y + mPosition->h) - LowerBounder.h); 353 354 return DetectTerrainIntersect(&LowerBounder, WATER); 355 }