Quad Pang
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Member Functions | Private Attributes
Ball Class Reference

Ball is bouncing Quad. More...

#include <Ball.h>

Inheritance diagram for Ball:
Quad_impl Quad_impl Quad Quad Quad Quad

List of all members.

Public Member Functions

 Ball ()
 Ball (const Ball &)
 yes you can safely copy Ball!
BallReincarnate ()
 Create child ball.
void Capture (Bongo *bong)
 Catch bongo and control it's position.
 Ball ()
 Ball (const Ball &)
 yes you can safely copy Ball!
BallReincarnate ()
 Create child ball.
void Capture (Bongo *bong)
 Catch bongo and control it's position.
Overloaded functions from Quad and Quad_impl
bool Init (XRes *resource, XCongo *xcongo)
 Read Quad from resource file.
bool Display ()
 Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.
const char * ClassName () const
 Used for RTTI.
void SetDirection (const double dir[3])
 This function first remebers last position and then calls Quad_impl::SetDirection.
bool Move (const double *forse)
 When Ball is moved it will move captured Bongo as well.
bool Init (XRes *resource, XCongo *xcongo)
 Read Quad from resource file.
bool Display ()
 Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.
const char * ClassName () const
 Used for RTTI.
void SetDirection (const double dir[3])
 Set direction of moving.
bool Move (const double *forse)

Private Attributes

int mLives
 Number of times, ball will reincarnate before it is killed.
double mLastPos [3]
 Last position is position when Ball changed direction last time.
double mSpeed
 Speed of the Ball.
BongompBongo
 Captured bongo. This is 0 most of the time.

Detailed Description

Ball is bouncing Quad.

See also:
Quad.

Definition at line 16 of file Ball.h.


Constructor & Destructor Documentation

Definition at line 16 of file Ball.cpp.

Referenced by Reincarnate().

: Quad_impl(), mpBongo(0)
{
}
Ball::Ball ( const Ball ob)

yes you can safely copy Ball!

Definition at line 21 of file Ball.cpp.

References mLastPos, mLives, mpBongo, and mSpeed.

                         : Quad_impl(ob)
{

    this->mLives = ob.mLives;
    memcpy(this->mLastPos, ob.mLastPos, sizeof(mLastPos) );
    mpBongo = ob.mpBongo;
    mSpeed = ob.mSpeed;
}
Ball::Ball ( const Ball )

yes you can safely copy Ball!


Member Function Documentation

void Ball::Capture ( Bongo bong)

Catch bongo and control it's position.

When bonog is killed it will be captured by the ball and bounce around. On the other side Ball will become transparent so bongo can still be visible to the user.

Parameters:
bongCaptured Bongo.

Definition at line 158 of file Ball.cpp.

References Quad_impl::mColor, mpBongo, Quad_impl::mSize, and Quad_impl::Size().

Referenced by CollisionMaster::Collision().

{ 
    mSize = 3 * bong->Size(); 
    mColor[3] = 0.5; 
    mpBongo = bong; 
}
void Ball::Capture ( Bongo bong)

Catch bongo and control it's position.

When bonog is killed it will be captured by the ball and bounce around. On the other side Ball will become transparent so bongo can still be visible to the user.

Parameters:
bongCaptured Bongo.
const char* Ball::ClassName ( ) const [virtual]

Used for RTTI.

Returns:
Name of the actual class.
See also:
Factory

Reimplemented from Quad_impl.

const char * Ball::ClassName ( ) const [virtual]

Used for RTTI.

Returns:
Name of the actual class.
See also:
Factory

Reimplemented from Quad_impl.

Definition at line 81 of file Ball.cpp.

Referenced by CollisionMaster::Collision().

{
    return "Ball";
}
bool Ball::Display ( ) [virtual]

Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.

Returns:
true if ok, false on error.

Reimplemented from Quad_impl.

bool Ball::Display ( ) [virtual]

Draw itself in global cotext. Since quads are intented to be used with opengl, drawing context is global.

Returns:
true if ok, false on error.

Reimplemented from Quad_impl.

Definition at line 88 of file Ball.cpp.

References Quad_impl::mColor, mLastPos, Quad_impl::mPos, and Quad_impl::mSize.

{
    glPushMatrix();
      
    // enable blending only if ball is transparent
    // this can only happend when Bongo is captured ...
    if (mColor[3] < 1)
    {
        glEnable(GL_BLEND);
        glDepthMask(GL_FALSE);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    }
    
    // set light and materal properties
    GLfloat mat_specular[] = { mColor[0], mColor[1], mColor[2], mColor[3] };
    GLfloat mat_shininess[] = { 50.0 };
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  
    // draw helper line from ball to the floor
    glBegin(GL_LINES);
        glVertex3f(mPos[0], 0.0, mPos[2]);
        glVertex3f(mPos[0], mPos[1], mPos[2]);
    glEnd();

    // Draw helper line on the floor Y==0
    glBegin(GL_LINES);
    glVertex3f(mLastPos[0], 0.01, mLastPos[2]);
    glVertex3f(mPos[0], 0.01, mPos[2]);
    glEnd();

    // Draw ball
    glTranslatef(mPos[0], mPos[1], mPos[2]);
    glutSolidSphere(mSize/2, int(mSize * 30), int(mSize * 22) );   /* draw sun */
    
    // disable back blending if ball is transparent 
    if (mColor[3] < 1)
    {
        glDepthMask(GL_TRUE);
        glDisable(GL_BLEND);
    }

    glPopMatrix();
    return true;
}
bool Ball::Init ( XRes resource,
XCongo xcongo 
) [virtual]

Read Quad from resource file.

Parameters:
resourceResource file contained description of Quad object.
xcongoGlobal settings file contained parameters not specific to this quad.
Returns:
true if ok, false on error.

normalize direction

Reimplemented from Quad_impl.

Definition at line 31 of file Ball.cpp.

References XRes::DoubleValue(), XRes::IntValue(), Quad_impl::mColor, Quad_impl::mDirection, mLastPos, mLives, Quad_impl::mMass, Quad_impl::mPos, Quad_impl::mPower, Quad_impl::mSize, mSpeed, and vec_ofpow().

{
    if (!resource->DoubleValue("mSize", &mSize) )
    {
        fprintf(stderr, "error: reading ball size\n");
        return false;
    }

    if (!resource->DoubleValue("mPos.mX", &mPos[0])
        || !resource->DoubleValue("mPos.mY",&mPos[1])
        || !resource->DoubleValue("mPos.mZ", &mPos[2]))
    {
        fprintf(stderr, "error: reading ball position\n");
        return false;
    }
    // set the last position to be current position
    memcpy(mLastPos, mPos, sizeof(mLastPos) );
    if (!resource->DoubleValue("mColor.red", &mColor[0])
        || !resource->DoubleValue("mColor.green",&mColor[1])
        || !resource->DoubleValue("mColor.blue", &mColor[2])
        || !resource->DoubleValue("mOpaque", &mColor[3]) )
    {
        fprintf(stderr, "error: reading ball color\n");
        return false;
    }

    if (!resource->DoubleValue("mDirection.mX", &mDirection[0])
        || !resource->DoubleValue("mDirection.mY",&mDirection[1])
        || !resource->DoubleValue("mDirection.mZ", &mDirection[2])
        || !resource->DoubleValue("mPower", &mPower) 
        || !resource->DoubleValue("mSpeed", &mSpeed) )
    {
        fprintf(stderr, "error: reading ball direction\n");
        return false;
    }
    
    if (!resource->IntValue("mLives", &mLives) )
    {
        fprintf(stderr, "error: reading ball lives\n");
        return false;   
    }
    
    vec_ofpow(mDirection, mSpeed);
    
    mMass = mSize * mSize * M_PI;

    return true;
}
bool Ball::Init ( XRes resource,
XCongo xcongo 
) [virtual]

Read Quad from resource file.

Parameters:
resourceResource file contained description of Quad object.
xcongoGlobal settings file contained parameters not specific to this quad.
Returns:
true if ok, false on error.

Reimplemented from Quad_impl.

bool Ball::Move ( const double *  forse)

When Ball is moved it will move captured Bongo as well.

Definition at line 166 of file Ball.cpp.

References Quad_impl::Move(), and mpBongo.

Referenced by CollisionMaster::Collision().

{
    if (mpBongo)
        mpBongo->Move(forse);
    return Quad_impl::Move(forse);
}
bool Ball::Move ( const double *  forse)

Create child ball.

Returns:
Pointer to child ball. If ball doesn't have more incarations it will return 0.

Definition at line 143 of file Ball.cpp.

References Ball(), Bongo::Free(), Bongo::Lives(), mLives, mpBongo, and Bongo::SetLives().

Referenced by CollisionMaster::Collision().

{
    if (--mLives <= 0)
        return 0;

    if (mpBongo)
    {
        mpBongo->Free();
        mpBongo->SetLives(mpBongo->Lives() + 1);
    }
    
    mpBongo = 0;
    return new Ball(*this);
}

Create child ball.

Returns:
Pointer to child ball. If ball doesn't have more incarations it will return 0.
void Ball::SetDirection ( const double  dir[3]) [virtual]

Set direction of moving.

Reimplemented from Quad_impl.

void Ball::SetDirection ( const double  dir[3]) [virtual]

This function first remebers last position and then calls Quad_impl::SetDirection.

Reimplemented from Quad_impl.

Definition at line 136 of file Ball.cpp.

References mLastPos, and Quad_impl::mPos.

Referenced by CollisionMaster::Collision().

{
    memcpy(mLastPos, mPos, sizeof(mPos) );
    Quad_impl::SetDirection(dir);
}

Member Data Documentation

double Ball::mLastPos [private]

Last position is position when Ball changed direction last time.

It is used to draw helper lines bellow the Ball so user can easier determine direction in which Ball is moving.

Definition at line 73 of file Ball.h.

Referenced by Ball(), Display(), Init(), and SetDirection().

int Ball::mLives [private]

Number of times, ball will reincarnate before it is killed.

Definition at line 64 of file Ball.h.

Referenced by Ball(), Init(), and Reincarnate().

Bongo * Ball::mpBongo [private]

Captured bongo. This is 0 most of the time.

Definition at line 77 of file Ball.h.

Referenced by Ball(), Capture(), Move(), and Reincarnate().

double Ball::mSpeed [private]

Speed of the Ball.

Definition at line 75 of file Ball.h.

Referenced by Ball(), and Init().


The documentation for this class was generated from the following files: