Jump to content

  • Log In with Google      Sign In   
  • Create Account


adrian17

Member Since 19 Jun 2013
Offline Last Active Yesterday, 04:35 AM
-----

Posts I've Made

In Topic: Creating flexible behaviour without scripting?

22 July 2013 - 04:46 PM

For the shmup I created, I implemented an Enemy Action framework.  Each enemy had a list of these actions it executed in order.  Each individual action was a simple, specific action.  Like an action to move to a specified location, an action to start firing weapons, an action to chase the player, an action to hold position for a period of time. 

 

Actually, I have implemented something similar - for bullets with complex movements. Every complex bullet has its own vector of instructions which are executed at specified time frames. It's not perfect, since I would also love to make a bullet spawn other bullets with an instruction. Aside from that, I'm very happy with this.

 

That's how it looks like:

//BasicEnemy::Logic()    

        for(int i=0;i<360;i+=15)
        {
            bulletManager->AddEnemyComplexBullet(B_BULLET1, x, y, basev, i-a, 0, 0);
            bulletManager->AddEnemyBulletData(45, BULLET_ANGULAR_VELOCITY, SET, 5);
            bulletManager->AddEnemyBulletData(45, BULLET_ACCEL, SET, -0.7);
            bulletManager->AddEnemyBulletData(55, BULLET_ANGULAR_VELOCITY, SET, 0);
            bulletManager->AddEnemyBulletData(55, BULLET_ACCEL, SET, 0);
        }

Anyway, that was the first thing I thought about when thinking about enemy behaviors, but unfortunately it won't work. That's simply because, no matter how complex the instruction is, it won't allow looping (...but I hope I'm wrong about that). If I want to shoot 100 bullets in a circle, writing 100 instructions for each shot makes no sense. That's exactly why I couldn't make the bullet spawn other bullets.

 

You could separate it out to an "EnemyAI" sort of class, that chooses the right behavior based on the AI (it can have states for example and change state depending on surroundings), which is then applied to the enemy. Then you would write these different states and corresponding behaviours, and the logic for choosing between a state.

I don't really need to use states and respond to any behavior, because I don't need the enemies to respond to anything whatsoever. I just need a clean way to set a behavior once, when creating the enemy or declare it when the program begins and then assign it to enemy during its creation.

(and as "behavior" I just mean either "at a given frame since creation, set/change velocity and direction" or "shoot a bullet/a lot of bullets in a given direction" OR "...with a random offset" OR "...in the player's direction" OR "in the player's direction with a random offset" OR ... )


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

24 June 2013 - 04:16 PM

Oh, I see... I was convinced that the only way to check the special keys was was by using event.key.keysym.mod, I didn't even try scanning it as any other key. Thanks a lot!


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

24 June 2013 - 10:08 AM

Okay, thanks. It looks really nice, actually. I've used your example and moved all in-game input handling directly to Game::HandleEvents.Right now it's in a side branch Here.

Unfortunately, I'm still having problems with scanning keymods. It's definitely my fault, but I don't know how to implement this properly. Right now my experiments looks like this:

    while(SDL_PollEvent(&event))
    {
        SDLsystem->Input();
        if( event.type == SDL_KEYDOWN )
        {
            if(event.key.keysym.mod == SDLK_LALT) {SDL_SetWindowTitle(window, "alt down");}
            SDL_Keycode key=event.key.keysym.sym;
            if(key == SDLK_ESCAPE )
                {nextState = State::MAINMENU;}
            //checking normal keys
        }
        else if(event.type == SDL_KEYUP)
        {
            if(event.key.keysym.mod == SDLK_LALT) {SDL_SetWindowTitle(window, "alt up");}
            SDL_Keycode key = event.key.keysym.sym;
            //checking normal keys
        }
    }

And the key isn't detected at all. 

If I change the operator in the statement to & (like shown in some code examples): 

if(event.key.keysym.mod & SDLK_LALT) 

It detects Shift (?) being pushed and released but only when Ctrl is pushed at the same time (??). Something very similar happens if i try to detect the Shift key.

How am I supposed to do it? It works properly when I try to check if Alt and Enter are pushed together, but not when I'm checking for Alt alone.


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

24 June 2013 - 07:48 AM

A simple way to solve this would be to pass the SDLKey values that are being listened for to the Paddle constructor:

int leftX = /* ... */;
int rightX = /* ... */;
 
Paddle left(leftX, SDLK_w, SDLK_s);
Paddle right(rightX, SDLK_UP, SDLK_DOWN);

 

That sounds good, although with my current setup I can't make it work - I can pass SDLK values to the constructor, but the problem arises then I try to use it in the case statement - because case needs constant values to work with. It doesn't want to compile even when I define key values as const int.

void Paddle::InputLeft()
{
    if( event.type == SDL_KEYDOWN )
    {
        switch( event.key.keysym.sym)
       {
            case keyUp:       {vy =- basev; up=1;} break; //error: "this" it not a constant expression
            case keyDown:     {vy = basev; down=1;} break;
            default: ;
       }
    }
    if( event.type == SDL_KEYUP )
    {
        switch( event.key.keysym.sym )
        {
            case keyUp:       {up=0; if(down==1) vy=basev; else vy = 0;} break;
            case keyDown:     {down=0; if(up==1) vy=-basev; else vy = 0;}; break;
            default: ;
        }
    }
}

(by the way, the above code shows why I don't think that the second approach you've shown would work with my current setup - in your code the paddle would stop moving as soon as any key release is detected - it would work fine until a player presses both keys at the same time and releases only one of them. That's why I've added the extra "if(down==1)" statements. )

I will think about this more, but I have some stranger issues right now, like SDL_SetTextureAlphaMod stopping working after toggling fullscreen/window and problems with detecting special keys (for some reason, shift key gets detected with a second of lag and its release isn't detected at all).


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

20 June 2013 - 09:13 AM

I've seen the video, went through your code a bunch of times, checked the SDL documentation, and i still can't explain why it happens...

 

By the way, the examples above are basically what MIX_FreeChunk() does, and the reason why the 2nd might crash the game, if because is "point->allocated" isn't 1, then it doesn't contain the audio buffer and the "point->abuf" shouldn't be deleted.

 

Sorry, but i can't really help anymore.

 

Thanks anyway. I guess the only thing left for me right now is asking at the SDL Forums.


PARTNERS