Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 19 Jun 2013
Offline Last Active Aug 27 2014 12:32 PM

Topics I've Started

Creating flexible behaviour without scripting?

22 July 2013 - 01:32 PM

I'm creating a simple shmup from scratch. This is how my base Enemy class looks like:

class Enemy
        Enemy(int ntype, double nx, double ny, double nvx, double nvy):
            type(ntype), x(nx), y(ny), vx(nvx), vy(nvy) {};
        virtual ~Enemy() {};

        void Move(){x+=vx;y+=vy;};
        virtual void Logic()=0;
        virtual void Draw();

        bool IsDead(){return health<0;}
        void ReduceHealth(int damage){health-=damage;}
        SDL_Rect GetLoc(){return loc;}
        int type;
        int health;
        double x, y;
        double vx, vy;
        SDL_Rect loc, source;

        void GetVectorToPlayer(double &x, double &y);
        double GetAngleToPlayer();

The Logic() function is the main function that defines the enemies' behaviours. It can influence both its movements and shooting patterns. For example, it may look like this:


    double angle=GetAngleToPlayer();
    if(globalFrameCounter%120==0)  //will shoot every two seconds
        for(int i=-3;i<=3;i+=1)  //will shoot seven bullets in the player's direction
            bulletManager->AddEnemyBullet(B_BULLET1, x, y, 5, angle+i); //bullet sprite, location, velocity and direction

Finally, all the enemies are created on the vector via the manager, like this (I have only one BasicEnemy subclass for now, so the function will be expanded int the future):

void EnemyManager::AddEnemy(int type, double x, double y, double vx, double vy)
    enemies.push_back(std::unique_ptr<BasicEnemy>(new BasicEnemy(type, x, y, vx, vy)));

Now, my problem is, what if I want to have a lot of different types of enemy behaviours. In the current state, it would force me to write a separate subclass with a new Logic() function for every possible behaviour. That's obviously the wrong approach, since it would force me to create tens and tens of subclasses. I also don't want to use any scripting language like Lua, because I have no knowledge of them and it feels like using a sledgehammer to crack a nut.

So... is there an easier way that I'm not seeing?

[SDL2] A (yet another) simple Pong clone to review

19 June 2013 - 12:29 PM

As the first step made a simple Pong game, both to get a bit of practice and to have a nice code foundation for future projects. I've used SDL2 and a tiny bit of sdl2_mixer, just to get the hang of it.

I'm quite happy with the code, although I guess some things could have been done better, eg. file structure. The only thing I really don't like is how I had to divide paddle's input functions to InputRight() and InputLeft(), but I didn't know any other good way of doing this (aside from making subclasses for left and right paddle, which didn't seem worth the effort to me).



(it's also my first time using Git, I hope I didn't mess anything up)


Regarding SDL, I have a few small "I have no idea why this happens" issues, mostly regarding toggling fullscreen with SDL_SetWindowFullscreen. For convenience, I've written these in my repo's "issues" section.