#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #include "stdafx.h" #include "crashingobject.h" #include #include const char* crashingObjectName = "crashing"; CrashingObject::CrashingObject() : box(0) { } CrashingObject::~CrashingObject() { removeBox(); } bool CrashingObject::update(World* world) { ObjectList* olist = world->getAllObjects(); ObjectList::Position pos; int numCollisions=0; for (olist->reset(pos); !olist->endOfList(pos); olist->advance(pos)) { // Make sure we don't accidentally choose ourselves if (olist->data(pos) != (Object*)this) { // Check to see if the current item is a bounded object if (isA(olist->data(pos), CrashingObject)) { if (intersects( *((CrashingObject*) olist->data(pos)) ) ) { numCollisions++; } } } } if (box && numCollisions==0) { return removeBox(); } else if (!box && numCollisions) { return addBox(); } return false; } bool CrashingObject::removeBox() { if (!box) return false; DrawPrimitiveList::Position pos = drawList.find(box); if (!drawList.endOfList(pos)) { drawList.deleteItem(pos); delete box; box=0; return true; } return false; } bool CrashingObject::addBox() { if (box) return false; box = new DrawRectangle; getBounds(*box); box->setColor(DrawColor::Cyan); drawList.addToEnd(box); return true; } void CrashingObject::print(ostream& out) const { // Print out a crashing object. // First, print out the class identifier and the open brace out << crashingObjectName << " { "; DrawPrimitiveList::Position pos; // Set up the header for the geometry out << drawableObjectString << " { " << endl; // Print out the origin. out << setw(4) << ' ' << origin << endl; // Print out the draw list. for (drawList.reset(pos); !drawList.endOfList(pos); drawList.advance(pos)) { // Make sure we don't print out the box. if (drawList.data(pos) != box) out << setw(4) << ' ' << *(drawList.data(pos)) << endl; } // Print out the ending character. out << "}" << endl; // Finally, print out the closing brace. out << " } " << endl; } void CrashingObject::read(istream& in) { char name[31], openBrace, closeBrace; // read in the first character if (in >> openBrace) { if (openBrace != '{') { // Assume that we actually read in the class identifier header, so read the rest // of it in. in >> setw(31) >> name >> openBrace; } // Now, read in the geometry. InteractableObject::read(in); // Finally, read in the closing brace; in >> closeBrace; } // Calculate the bounding area. Note that since this is the only time we modify the // draw list (other than if we add a box, but that box is within the bounds), we only // need to recalculate the bounding area once. calculateBounds(); }