Advertisement
Kitomas

new work inside globals.cpp

Jun 14th, 2024
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. //returns true only if resulting bounds have volume
  2. static inline bool get_tile_bounds(shape_quad& bounds){
  3.   //find the boundaries of all tiles that the player is touching
  4.    //(+= 11 so that an east/south subtile boundary is crossed only when
  5.    // a pixel of the player sprite actually overlaps a given tile)
  6.   bounds.b += shape::point(11, 11);
  7.   bounds /= 12; //convert pixels to subtiles
  8.    //
  9.   bounds.a.x = CLAMP(bounds.a.x, 0, TILESIZ_X*2); //skip subtiles that are off-screen
  10.   bounds.a.y = CLAMP(bounds.a.y, 0, TILESIZ_Y*2);  //(2 subtiles in a tile lengthwise)
  11.    //
  12.   bounds.b.x = CLAMP(bounds.b.x, 0, TILESIZ_X*2);
  13.   bounds.b.y = CLAMP(bounds.b.y, 0, TILESIZ_Y*2);
  14.  
  15.   s32 xmin, xmax,  ymin, ymax;
  16.  
  17.   if(abs(bounds.a.x)<abs(bounds.b.x)){ xmin = abs(bounds.a.x),  xmax = abs(bounds.b.x); }
  18.   else                               { xmin = abs(bounds.b.x),  xmax = abs(bounds.a.x); }
  19.  
  20.   if(abs(bounds.a.y)<abs(bounds.b.y)){ ymin = abs(bounds.a.y),  ymax = abs(bounds.b.y); }
  21.   else                               { ymin = abs(bounds.b.y),  ymax = abs(bounds.a.y); }
  22.  
  23.   return (xmax-xmin)>0 && (ymax-ymin)>0;
  24.  
  25. }
  26.  
  27.  
  28.  
  29. #define SO_LOOP_PATTERN 0
  30. bool subtiles_overlapping(shape::rect& object_rect_ref, shape::point* delta_p){
  31.   shape::rect object_rect = object_rect_ref;
  32.   shape_quad  bounds      = object_rect;
  33.  
  34.   shape::point delta(0, 0); //neutral delta by default
  35.  
  36.   if(!get_tile_bounds(bounds)){ //there are no subtiles to check; exit early
  37.     if(delta_p) *delta_p = delta;
  38.     return false;
  39.   }
  40.  
  41.   shape::point min = bounds.a;
  42.   shape::point max = bounds.b;
  43.   shape::point sub;             //current subtile position
  44.   shape::rect  col(0,0, 12,12); //subtile collider
  45.  
  46. #if SO_LOOP_PATTERN == 1
  47.   s32 xmid = (min.x+max.x)/2; //x midpoint
  48.   /* bottom-to-top vertically (y = max.y-1;  y >= min.y;  --y)
  49.        right-to-left on the left half  (x = xmid-1;  x >= min.x;  --x)
  50.        left-to-right on the right half (x = xmid  ;  x <  max.x;  ++x) */
  51.   //(tbd)
  52.  
  53. #else
  54.   for(sub.y = max.y-1;  sub.y >= min.y;  --sub.y) //bottom-to-top vertically
  55.   for(sub.x = min.x  ;  sub.x <  max.x;  ++sub.x) //left-to-right horizontally
  56.     if(subtile_is_collidable(sub.x, sub.y)){
  57.       col.x = sub.x*12;
  58.       col.y = sub.y*12;
  59.       if(rects_overlapping(object_rect, col, &delta))
  60.         object_rect += delta;
  61.  
  62.     }
  63.  
  64. #endif
  65.  
  66.  
  67.   delta.x = object_rect.x - object_rect_ref.x;
  68.   delta.y = object_rect.y - object_rect_ref.y;
  69.  
  70.   if(delta_p) *delta_p = delta;
  71.  
  72.   return delta.x!=0 || delta.y!=0;
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement