示例#1
0
bool Drone::CollisionTest(SmartPtr<GameObject> o)
{
//    if (o->GetType() != GameObjectType("Bullet")) return false;
    if (o->GetType() == GameObjectType("Drone")) return false;
	if (GetType() == o->GetType()) return false;
	if (mBoundingShape.GetPtr() == NULL) return false;
	if (o->GetBoundingShape().GetPtr() == NULL) return false;
	return mBoundingShape->CollisionTest(o->GetBoundingShape());
}
示例#2
0
bool Asteroid::CollisionTest(SmartPtr<GameObject> o)
{
    // Don't collide with objects of the same type
	if (GetType() == o->GetType()) return false;
	// If there's no bounding shape, don't do anything
	if (mBoundingShape.GetPtr() == NULL) return false;
	// If colliding object doesn't have a pointer to shape, don't do anything
	if (o->GetBoundingShape().GetPtr() == NULL) return false;
	// Otherwise, return the result of a collision test between bounding shapes
	return mBoundingShape->CollisionTest(o->GetBoundingShape());
}
示例#3
0
void Ore::OnCollision(const GameObjectList& objects)
{
    SmartPtr<GameObject> object;
    GameObjectType const* type = NULL;

    GameObjectList::const_iterator it = objects.begin();
    GameObjectList::const_iterator end = objects.end();

    for (; it != end; ++it)
    {
        object = *it;
        type = &(object->GetType());

        // Compare Object type
        if((*type) == GameObjectType("Spaceship"))
        {
            mWorld->RemoveObject(this);
            cout << "ORE COLIDES WITH SPACESHIP\n";
        }
    }
}
示例#4
0
void Drone::OnCollision(const GameObjectList& objects)
{
    SmartPtr<GameObject> object;
    GameObjectType const* type = NULL;

    GameObjectList::const_iterator it = objects.begin();
    GameObjectList::const_iterator end = objects.end();

    for (; it != end; ++it)
    {
//        const GameObjectType * type = &((*it)->GetType() );

        object = *it;
        type = &(object->GetType());
//        const char * type_name = object->GetType().GetTypeName();
//        cout << type_name << ";" << endl;

        // Compare Object type
        if((*type) == GameObjectType("Bullet"))
        {
            // TODO add damage related stuff to GameObject which should allow you to bypass this whole awkward pointer stuff here...
            Bullet* b = (Bullet*) object.GetPtr();
//            cout << "colision with bullet" << endl;
//            Bullet* b = dynamic_cast<Bullet*>(object.GetPtr());

            int damage = b->GetDamage();
            int health = mHealth;
            int newHealth = mHealth - damage;

            DealDamage(b->GetDamage());
            //cout << "HEALTH " << mHealth << endl;
            // Check if alive
            if(mHealth <= 0) mWorld->RemoveObject(this);
        }
    }
}
示例#5
0
void Asteroid::OnCollision(const GameObjectList& objects)
{
    SmartPtr<GameObject> object;
    GameObjectType const* type = NULL;

    GameObjectList::const_iterator it = objects.begin();
    GameObjectList::const_iterator end = objects.end();

    for (; it != end; ++it)
    {
//        const GameObjectType * type = &((*it)->GetType() );
        object = *it;
        type = &(object->GetType());
//        const char * type_name = object->GetType().GetTypeName();
//        cout << type_name << ";" << endl;

        // Compare Object type
        if((*type) == GameObjectType("Bullet"))
        {
            mWorld->RemoveObject(this);
            // @todo mWorld-> see if you can do the spliting of asteroids from here rather than the asteroids.cpp
        }
    }
}
示例#6
0
void Asteroids::OnObjectRemoved(GameWorld* world, SmartPtr<GameObject> object)
{
    if (object->GetType() == GameObjectType("Asteroid"))
    {
        SmartPtr<GameObject> explosion = CreateExplosion();
        explosion->SetPosition(object->GetPosition());
        explosion->SetRotation(object->GetRotation());
        mGameWorld->AddObject(explosion);

        mAsteroidCount--;

        if(object->GetScale() > 0.2) {
            float newVel[4][2] = { {-1.0,-1.0}, { 1.0,-1.0},
                                   { 1.0, 1.0}, {-1.0, 1.0} };
            GLVector3f obPos = object->GetPosition();

            for(int i = 0; i < 4; i++)
            {
                SmartPtr<GameObject> newAsteroid = CreateAsteroid(obPos.x, obPos.y);
                float f = 5;    //force
                newAsteroid->SetVelocity(GLVector3f(newVel[i][0] * f + (rand() % 4), newVel[i][1] * f + (rand() % 4), 0.0));
                newAsteroid->SetScale(object->GetScale()/2);
                mGameWorld->AddObject(newAsteroid);
                mAsteroidCount++;
            }
        }

        if (mAsteroidCount <= 0)
        {
            SetTimer(500, START_NEXT_LEVEL);
        }
    }
    if (object->GetType() == GameObjectType("AsteroidOre"))
    {
        SmartPtr<GameObject> explosion = CreateExplosion();
        explosion->SetPosition(object->GetPosition());
        explosion->SetRotation(object->GetRotation());
        mGameWorld->AddObject(explosion);

        float newPos[4][2] = { {-1.0,-1.0}, { 1.0,-1.0},
                               { 1.0, 1.0}, {-1.0, 1.0} };
        GLVector3f obPos = object->GetPosition();
        float obRot = object->GetRotation();
        int sep = 5; // how much to separate the ore fromt he position of the asteroid

        for(int i = 0; i < 4; i++)
        {
            SmartPtr<GameObject> ore = CreateOre();
            ore->SetPosition(GLVector3f(obPos.x + newPos[i][0] * sep + (rand() % 4), obPos.y + newPos[i][1] * sep + (rand() % 4), 0.0));
            ore->SetRotation(obRot);
            mGameWorld->AddObject(ore);
        }

        int noA = 1 + (rand() % 3);
        cout << "RAND A: " << noA << endl;

        for (int i = 0; i < noA; i++)
        {
            SmartPtr<GameObject> newAsteroid = CreateAsteroid(obPos.x, obPos.y);
            float rA = rand() % 360; // a random angle
            cout << "RAND ANGLE: " << rA << endl;
            newAsteroid->SetVelocity(GLVector3f( cos(DEG2RAD * rA), sin(DEG2RAD * rA), 0.0));
            cout << "Object Scale: " << object->GetScale() << endl;
            newAsteroid->SetScale(object->GetScale());
            mGameWorld->AddObject(newAsteroid);
            mAsteroidCount++;
        }
    }
}
示例#7
0
void Asteroids::OnMouseButton(int button, int state, int x, int y)
{

    if(state == GLUT_DOWN)
    {
        if(button == GLUT_RIGHT_BUTTON)
        {
            mMoveCamera = true;

            // Calculate world coordinates
            const int zl = mGameWindow->GetZoomLevel();
            GLVector3f m2w((x - mGameDisplay->GetWidth()/2)/zl + mCameraFocus->x, (mGameDisplay->GetHeight()/2 - y)/zl + mCameraFocus->y, 0.0);

            SmartPtr<GameObject> checkSelect = mGameWorld->GetOnClickSelect(m2w);
            SmartPtr<Order> o = NULL;
            if(checkSelect.GetPtr() == NULL) {
                o = new Order(ORDER_MOVE);
                o->SetDestination(m2w);
            } else {
                o = new Order(ORDER_ATTACK);
                o->SetTarget(checkSelect);
            }
            mGameWorld->AssignOrderToSelected(o);
        }
        if(button == GLUT_LEFT_BUTTON)
        {
//            cout << endl;
//            cout << "GLUT MOUSE: " << x << "\t" << y << endl;

//            GLVector2i mouseVector(GLVector2i(x, mGameDisplay->GetHeight() - y));
//            cout << "GUI COORDS: " << mouseVector.x << "\t" << mouseVector.y << endl;

//            float dw = mGameDisplay->GetWidth();
//            float dh = mGameDisplay->GetHeight();
//            cout << "DISPLAY WH: " << dw << "\t" << dh << endl;

            // Get game zoom level
//            cout << "ZOOM LEVEL: " << zl << endl;

//            GLVector3f mouse2world((x - dw/2)/zl + mCameraFocus->x, (dh/2 - y)/zl + mCameraFocus->y, 0.0);
//
//            cout << "M2W COORDS: " << mouse2world.x << "\t" << mouse2world.y << endl;
//            cout << "CAM  FOCUS: " << mCameraFocus->x << "\t" << mCameraFocus->y << endl;

//            mGameWorld->AddObject(CreateAsteroid(x,y));
//            mGameWorld->AddObject(CreateAsteroid());
//            mAsteroidCount++;

            // get zoom level of the game to calculate world coordinates
            const int zl = mGameWindow->GetZoomLevel();
            // Calculate the world coordinate corresponding to mouse coordinates (m2w = mouse to world).
            GLVector3f m2w((x - mGameDisplay->GetWidth()/2)/zl + mCameraFocus->x, (mGameDisplay->GetHeight()/2 - y)/zl + mCameraFocus->y, 0.0);


            // Make the selection box visible
            mSelectionBox->SetVisible(true);
            // Set the starting point for the box to be the mouse coordinates
            mSelectionBox->SetStart(GLVector2i(x,y));
            // Set the second point of the box (where mouse is) to also be the mouse coordinates to reset it
            mSelectionBox->SetMouse(GLVector2i(x,y));
            // Set the world coordinates coresponding to  the mouse coordinates
            mSelectionBox->SetWorldCoord1(m2w);
            mSelectionBox->SetWorldCoord2(m2w);

            SmartPtr<GameObject> selectedGO = mGameWorld->GetOnClickSelect(m2w);
            if(selectedGO.GetPtr() != NULL){
                if(selectedGO->GetType() == GameObjectType("Drone"))
                {
                    selectedGO->MakeSelected();
                }
            }
            cout << "CLICK FOUND: " << selectedGO.GetPtr() << endl;

            //mGameWorld->AddObject(CreateAsteroid(mouse2world.x, mouse2world.y));
//            SmartPtr<GameObject> a = CreateAsteroid(mGameWorld->GetHeight(), mGameWorld->GetWidth());
            //a->SetVelocity(GLVector3f(0.0,-100.0,0.0));
            //mGameWorld->AddObject(a);
            //mAsteroidCount++;
        }
    }
    else if (state == GLUT_UP)
    {
        mSelectionBox->SetVisible(false);
        mMoveCamera = false;
    }
}