Ejemplo n.º 1
0
bool Drawable::collide(Drawable &item)
{
   Rect rect1 = item.boundingRect();
   Rect rect2 = boundingRect();

   Point points1[4];
   points1[0] = rect1.top_right();
   points1[1] = rect1.top_left();
   points1[2] = rect1.bottom_left();
   points1[3] = rect1.bottom_right();

   Point points2[4];
   points2[0] = rect2.top_right();
   points2[1] = rect2.top_left();
   points2[2] = rect2.bottom_left();
   points2[3] = rect2.bottom_right();

   for (int i = 0; i < 4; ++i)
   {
      /*
	If only on corner of this is on item, or if only one of the
	item corner is on this, we return true because it collides.
	Otherwisely, we return false since it doesn't.
      */

      if (isOn(points1[i]))
	 return true;
      if (item.isOn(points2[i]))
	 return true;
   }
   return false;
}
Ejemplo n.º 2
0
bool GameMap::collide(Drawable &spr)
{
   DrawableRect tmp;
   if (colH != -1)
   {
      Rect rect = spr.boundingRect();
      tmp.setPos(rect.x, rect.y + rect.h - colH);
      tmp.resize(rect.w, colH);
   }
   Drawable &col = colH != -1 ? tmp : spr;

   for (unsigned int i = 0; i < tiles.size(); ++i)
   {
      Sprite &tile = tilesets[tiles[i].tileset];
      tile.setTile(tiles[i].tileX, tiles[i].tileY, tileWidth, tileHeight);
      
      // Coord relative to the map !
      tile.setPos(getX() + tiles[i].x, getY() + tiles[i].y);

      Line line;

      if (tiles[i].type.right)
      {
	 line.setPos(getX() + tiles[i].x + tileWidth, getY() + tiles[i].y);
	 line.setPoint(getX() + tiles[i].x + tileWidth, 
		       getY() + tiles[i].y + tileHeight);
	 if (col.collide(line))
	    return true;
      }

      if (tiles[i].type.left)
      {
	 line.setPos(getX() + tiles[i].x, getY() + tiles[i].y);
	 line.setPoint(getX() + tiles[i].x, getY() + tiles[i].y + tileHeight);
	 if (col.collide(line))
	    return true;
      }

      if (tiles[i].type.up)
      {
	 line.setPos(getX() + tiles[i].x, getY() + tiles[i].y);
	 line.setPoint(getX() + tiles[i].x + tileWidth, getY() + tiles[i].y);
	 if (col.collide(line))
	    return true;
      }

      if (tiles[i].type.down)
      {
	 line.setPos(getX() + tiles[i].x, getY() + tiles[i].y + tileHeight);
	 line.setPoint(getX() + tiles[i].x + tileWidth, 
		       getY() + tiles[i].y + tileHeight);
	 if (col.collide(line))
	    return true;
      }

      if (tiles[i].type.content)
	 if (col.collide(tile))
	    return true; // If it collides with only one tile, it collides..
   }
   return false; // It didn't collide with any tiles.
}