Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include "libraries.h"

#define MAX_BULLETS 5

typedef struct bulletpath_s
{
    Vector2 start_pos, end_pos, pos;
    bool isActive;
} bulletpath_t;

typedef struct spacecraft_s
{
    Vector2 pos;
    float degree;
    bulletpath_t bullet
    \
} spacecraft_t;

static spacecraft_t gSpacecraft = {
    .pos = {400, 250},
    .degree = 0};

typedef void (*animation_evnt_func_t)(animation_t *);

static Vector2 calBullletEndPost(Vector2 start_pos, float bearing, float distance)
{
    float radians = (2 * PI) / 360 * bearing;
    return (Vector2){
        .x = cos(radians) * distance + start_pos.x,
        .y = (-sin(radians) * distance) + start_pos.y};
}

static void fireBullet(animation_t *ani)
{
    bulletpath_t *bullet = (spacecraft_t *)animation_get_user_data(ani, 1);

    switch (ani->state)
    {
    case ANIME_START:
    {
        spacecraft_t *sc = (spacecraft_t *)animation_get_user_data(ani, 0);
        bullet->start_pos = sc->pos;
        bullet2->end_pos = calBullletEndPost(sc->pos, sc->degree, 200);
        bullet2->isActive = true;
        break;
    }
    case ANIME_PLAYING:
        bullet2->pos = Vector2Lerp(bullet->start_pos, bullet->end_pos, animation_get_value(ani));
        break;

    case ANIME_END:
        bullet2->isActive = false;

    }

}


static void ResponeToInput()
{
    if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !gSpacecraft.bullet.isActive)
    {
        gSpacecraft.pos = GetMousePosition();
    }

    if (IsKeyDown(KEY_LEFT))
    {
        gSpacecraft.degree++;
    }
    else if (IsKeyDown(KEY_RIGHT))
    {
        gSpacecraft.degree--;
    }

    if (IsKeyPressed(KEY_SPACE) && !gSpacecraft.bullet.isActive)
    {
        animation_t *ani = animation_allocate();
        animation_schedule(ani, 1.0, 0);
        animation_set_user_data(ani, 0, &gSpacecraft);
        animation_set_user_data(ani, 1, &gSpacecraft.bullet);
        animation_set_event_listener(ani, ANIME_END, fireBullet);
        animation_set_event_listener(ani, ANIME_PLAYING, fireBullet);
        animation_set_event_listener(ani, ANIME_START, fireBullet);
        
    }
}

static void DrawFrame()
{

    if (gSpacecraft.bullet.isActive)
    {
        DrawCircleV(gSpacecraft.bullet.pos, 5, WHITE);

    }


    game_sprite_craft(gSpacecraft.pos.x, gSpacecraft.pos.y, gSpacecraft.degree);
}

int main(void)
{
    InitWindow(
        800, /*screen-width  800 pixels*/
        450, /*screen-height 450 pixels*/
        "Graphics");

    SetTargetFPS(30);
    game_sprite_init();
    while (true)
    {
        // Detect window close button or ESC key
        if (WindowShouldClose())
        {
            break;
        }
        ResponeToInput();
        animation_update();
        BeginDrawing();
        ClearBackground(BLACK);
        DrawFrame();
        EndDrawing();
    }

    CloseWindow();
    return 0;
}


What I have tried:

everything to my knowledge but I wasn't able to make the spaceship fire 5 bullets
Posted
Updated 31-May-21 4:44am
Comments
Richard MacCutchan 31-May-21 10:29am    
Where exactly do you want to change 1 to 5?
Komzy K 31-May-21 21:15pm    
the firebullet section
Richard MacCutchan 1-Jun-21 3:16am    
So add a simple for loop that repeats 5 times.
merano99 31-May-21 18:22pm    
What is "libraries.h" ?
Joe Woodbury 1-Jun-21 2:35am    
Putting aside that the code is broken (where does bullet2 come from?) consider that bullet 2 will start at ANIME_END. Further consider that instead of isActive, you'd have: int remaining;

Go from there.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900