Jump to content
  • Advertisement
Sign in to follow this  
lindon jason heaven

class & fuctions

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

just made a class to put up a ball and chain and make it swing for my new game, is it ok to put the function call in the class h file or do i have to put the code in a cpp and just call it from the class. just want to know if this is the way to use class or is it a bad idea. thanks my code.
// watch for multiple inclusions
#ifndef Ballchain
#define Ballchain

int Kill_Ball_Chain();
int Load_Ball_Chain();

extern BOB Chain;
extern BOB Ball;

#define swing_left +1
#define swing_right -1

//extern int BallS[];


class Ball_Chain
{
private:
    int Wind ;

    int Swing;
    int offset;
    int X,Y;
    int ball_x,ball_y;

public:
    int Direction ;

    Ball_Chain()
    {
        Direction = 1;
        offset = 0;
        Swing = 16;
        ball_x = 0;
        ball_y = 0;
    }// defult setting for the chain.

    int Move(int x,int y,int Chain_Lenth)
    {
        Ball_Chain::X = x;
        Ball_Chain::Y = y;

        Chain.y = Ball_Chain::Y;
        Chain.x = Ball_Chain::X;

        Chain.curr_frame = 0;

        Draw_BOB16(&Chain,lpddsback);

        Wind+=Direction;

        if (Wind>Swing || Wind&<-Swing)
        {
            Direction=-Direction;
        }

        for (int index=0;index<Chain_Lenth;index++)
        {
            Chain.curr_frame = 1;
            Chain.y +=16;
            Chain.x +=Wind;

            Draw_BOB16(&Chain,lpddsback);
        }
		
		
        if (ball_x+16<x && Direction == -1)
        {
            offset-=2;
        }
        else if (ball_x+16<x && Direction == +1)
        {
            offset+=2;
        }
	else if(ball_x+16>x && Direction == +1)
        {
            offset-=2;
        }
        else if(ball_x+16>x && Direction == -1)
        {
            offset+=2;
        }
        else if (ball_x+16 == x)
        {
            ball_y = Chain.y;
            offset = 0;
        }
		
        ball_x = Chain.x-16;
        ball_y = Chain.y + offset;
        Ball.x = ball_x;
        Ball.y = ball_y;

        Draw_BOB16(&Ball,lpddsback);

        return(1);
    }

    int Test_Collision_Ball(BOB_PTR bob)
    {
        // are these a valid bobs
        if (!bob)
            return(0);

        // get the radi of each rect
        int width1  = (bob->width>>1) - (bob->width>>3);
        int height1 = (bob->height>>1) - (bob->height>>3);

        int width2  = 24;
        int height2 = 24;

        // compute center of each rect
        int cx1 = bob->x + width1;
        int cy1 = bob->y + height1;

        int cx2 = ball_x + width2;
        int cy2 = ball_y + height2;

        // compute deltas
        int dx = abs(cx2 - cx1);
        int dy = abs(cy2 - cy1);

        // test if rects overlap
        if (dx < (width1+width2) && dy < (height1+height2))
            return(1);
        else
        // else no collision
        return(0);
    }
};

#endif;


Edit by Fruny - formatting.

Share this post


Link to post
Share on other sites
Advertisement
The header file is for the declaration, and the source (cpp) file is for the implementation. So all of the method code should go into the cpp file (unless it's templated or inlined).

In future use [ code][/ code] or [ source][/ source] tags around code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tera_Dragon
In future use [ code][/ code] or [ source][/ source] tags around code.


I'm on it. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!