Thursday, October 27, 2011

Inheriting From Physical Object

As I mentioned in the last post, I realized I should be inheriting from a Super Class for classes that represent a physical object.  So, I made the PhysicalObject class:
PhysicalObject::PhysicalObject(sf::RenderWindow *pApp, cpSpace *pSpace, std::string ImageName, std::string GlowImageName, cpVect Location, cpFloat Angle, cpFloat Speed, cpFloat Depth, cpFloat RotSpeed) : mpApp(pApp), mpSpace(pSpace), mpImage(NULL), mpGlowImage(NULL), mpSprite(NULL), mpGlowSprite(NULL), mDepth(Depth) { // Load the image mpImage = Utilities::ImageGet(ImageName.c_str()); mpSprite = new sf::Sprite(); mpSprite->SetImage(*mpImage); mpSprite->SetOrigin(mpImage->GetWidth()/2, mpImage->GetHeight()/2); // Set to middle of screen mpSprite->SetPosition(pApp->GetWidth()/2, pApp->GetHeight()/2); if (GlowImageName.size() > 0) { mpGlowImage = Utilities::ImageGet(GlowImageName.c_str()); mpGlowSprite = new sf::Sprite(); mpGlowSprite->SetImage(*mpGlowImage); mpGlowSprite->SetOrigin(mpGlowImage->GetWidth()/2, mpGlowImage->GetHeight()/2); // Set to middle of screen mpGlowSprite->SetPosition(pApp->GetWidth()/2, pApp->GetHeight()/2); // for glow, set to additive blend mpGlowSprite->SetBlendMode(sf::Blend::Add); } mpBody = cpBodyNew(1.0f, 1.0f); cpBodySetPos(mpBody, Location); // Set the Velocity mpBody->v.x=cos( Angle)*Speed; mpBody->v.y=sin( Angle)*Speed; cpBodySetAngle(mpBody, Angle); mpBody->w = RotSpeed; mpShape = cpCircleShapeNew(mpBody, (mpImage->GetWidth() + mpImage->GetHeight())/4, cpvzero); mpShape->e = 0.0f; // not elatics, no bouncing mpShape->u = 0.0f; // 0.0 is frictionless cpSpaceAddBody(mpSpace, mpBody); cpSpaceAddShape(mpSpace, mpShape); }

The constructor handles all the physics stuff (except defining collision types and callbacks) and the image stuff.

I also made a Draw in the PhysicalObject class:
BOOL PhysicalObject::Draw(cpVect PlayerLoc, sf::Color GlowColor) { cpFloat Angle = cpBodyGetAngle(mpBody); cpFloat Itemx = (cpFloat)mpApp->GetWidth()/2; cpFloat Itemy = (cpFloat)mpApp->GetHeight()/2; BOOL bDraw = TRUE; mpSprite->SetRotation(Angle*(180.0f/PI)); if (!cpveql(PlayerLoc, cpvzero)) { // Locate the sprite based off player's location Itemx = (cpFloat)(mpApp->GetWidth()/2) + (mpBody->p.x - PlayerLoc.x); Itemy = (cpFloat)(mpApp->GetHeight()/2) + (mpBody->p.y - PlayerLoc.y); if (!((Itemx > -(cpFloat)mpImage->GetWidth()) && (Itemx < (cpFloat)mpApp->GetWidth() + (cpFloat)mpImage->GetWidth()) && (Itemy > -(cpFloat)mpImage->GetHeight()) && (Itemy < (cpFloat)mpApp->GetHeight() + (cpFloat)mpImage->GetHeight()))) { bDraw = FALSE; } } if (bDraw) { mpSprite->SetPosition(Itemx, Itemy); // Draw shadow 1st if depth if (mDepth > 0.001f) { cpFloat Radius = cpCircleShapeGetRadius(mpShape); sf::Shape oCircle = sf::Shape::Circle(Itemx+5.0, Itemy+5.0, mDepth*Radius*0.80f, sf::Color(0, 0, 0, 150)); mpApp->Draw(oCircle); } mpApp->Draw(*mpSprite); // If we get glow alpha and we have glow image if (GlowColor.a && mpGlowSprite) { if (!cpveql(PlayerLoc, cpvzero)) { mpGlowSprite->SetPosition(Itemx, Itemy); } else { mpGlowSprite->SetPosition(mpApp->GetWidth()/2, mpApp->GetHeight()/2); } mpGlowSprite->SetRotation(Angle*(180.0f/PI)); mpGlowSprite->SetColor(GlowColor); mpApp->Draw(*mpGlowSprite); } } return TRUE; }

As you can see, we handle the images and the glow images here.  We also added a Depth member to draw shadows under objects.

Here's the latest screenshot:

No comments:

Post a Comment