Skip to content

Commit

Permalink
add: kill animation and rounds
Browse files Browse the repository at this point in the history
  • Loading branch information
savalet committed Nov 25, 2024
1 parent f5721be commit 2afdd0a
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 35 deletions.
Binary file added assets/ammo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/duck_states.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/tree.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions include/hunter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define EXIT_SUCCESS 0
#define CURSOR_SIZE 32
#define DUCK_NBR 16
#define AMMO_COUNT 3
#define FRAMERATE 144
#define MICRO_TO_SEC(time) time.microseconds / 1000000.0

Expand All @@ -21,6 +22,7 @@ typedef struct {
sfVector2f pos;
sfVector2u size;
int version;
int touched;
} sprite_t;

typedef struct {
Expand All @@ -30,16 +32,25 @@ typedef struct {

typedef struct {
sfRenderWindow *window;
sfVector2u window_size;
h_clock_t clock;
h_clock_t clock2;
sprite_t background;
sprite_t tree;
sprite_t cursor;
sprite_t ducks[DUCK_NBR];
sprite_t ammos[AMMO_COUNT];
sfEvent *event;
int score;
int shoot;
int ammo;
sfText *score_text;
sfText *pause_text;
int paused;
int remaining_ducks;
float clock2_time;
int round;
double move_count;
} hunterinfo_t;

int hunter(void);
Expand All @@ -54,5 +65,9 @@ int move_cursor(hunterinfo_t *, sfVector2f);
int fill_ducks(hunterinfo_t *);
void display_ducks(hunterinfo_t *);
int draw_score(hunterinfo_t *);
int draw_pause(hunterinfo_t *);
int inc_score(hunterinfo_t *);
void display_ammos(hunterinfo_t *);
int fill_ammos(hunterinfo_t *);
int change_ammo_version(hunterinfo_t *, int);
#endif
69 changes: 54 additions & 15 deletions src/hunter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,62 @@
#include <stdbool.h>
#include <stdlib.h>


static
void handle_hunter_loop(hunterinfo_t *hf)
void display_all(hunterinfo_t *hf)
{
event_manager(hf);
hf->clock.time = sfClock_getElapsedTime(hf->clock.clock);
hf->clock2.time = sfClock_getElapsedTime(hf->clock2.clock);
sfRenderWindow_clear(hf->window, sfBlack);
sfRenderWindow_drawSprite(hf->window, hf->background.sprite, NULL);
display_ducks(hf);
sfRenderWindow_drawSprite(hf->window, hf->tree.sprite, NULL);
display_ammos(hf);
sfRenderWindow_drawSprite(hf->window, hf->cursor.sprite, NULL);
sfRenderWindow_drawText(hf->window, hf->score_text, NULL);
sfRenderWindow_display(hf->window);
}

static
void second_clock(hunterinfo_t *hf)
{
for (int i = 0; i < DUCK_NBR; i++)
if (!hf->ducks[i].touched)
move_duck(hf, (sfVector2f){ 1, 0 }, i);
else
move_duck(hf, (sfVector2f){ 0, 1 }, i);
}

static
void change_duck_version(hunterinfo_t *hf, int i)
{
if (!hf->ducks[i].touched)
hf->ducks[i].version = hf->ducks[i].version >= 2 ? 0 :
hf->ducks[i].version + 1;
else
hf->ducks[i].version = hf->ducks[i].version == 3 ? 4 :
hf->ducks[i].version;
}

static
void handle_hunter_loop(hunterinfo_t *hf)
{
event_manager(hf);
hf->clock.time = sfClock_getElapsedTime(hf->clock.clock);
hf->clock2.time = sfClock_getElapsedTime(hf->clock2.clock);
display_all(hf);
if (MICRO_TO_SEC(hf->clock.time) > 0.5) {
for (int i = 0; i < DUCK_NBR; i++)
hf->ducks[i].version++;
change_duck_version(hf, i);
sfClock_restart(hf->clock.clock);
}
if (MICRO_TO_SEC(hf->clock2.time) > 0.001) {
for (int i = 0; i < DUCK_NBR; i++)
move_duck(hf, (sfVector2f){ 1, 0 }, i);
if (MICRO_TO_SEC(hf->clock2.time) > hf->clock2_time) {
if (hf->move_count == 1000) {
hf->move_count = 0;
hf->clock2_time /= 2;
fill_ducks(hf);
hf->round++;
}
second_clock(hf);
hf->move_count++;
sfClock_restart(hf->clock2.clock);
}
}
Expand All @@ -41,28 +76,32 @@ void draw_all(hunterinfo_t *hf)
draw_background(hf);
draw_tree(hf);
fill_ducks(hf);
fill_ammos(hf);
draw_cursor(hf);
draw_score(hf);
draw_pause(hf);
create_clock(hf);
}

int hunter(void)
{
hunterinfo_t hf = { 0 };
hunterinfo_t hf = { 0, .ammo = AMMO_COUNT, .remaining_ducks = DUCK_NBR,
.ducks[DUCK_NBR - 1].sprite = NULL, .clock2_time = 0.001, .round = 1 };

create_window(1920, 1080, &hf);
draw_all(&hf);
sfRenderWindow_setMouseCursorVisible(hf.window, false);
sfRenderWindow_setFramerateLimit(hf.window, FRAMERATE);
while (sfRenderWindow_isOpen(hf.window)) {
hf.window_size = sfRenderWindow_getSize(hf.window);
while (hf.paused)
event_manager(&hf);
handle_hunter_loop(&hf);
if (hf.score == DUCK_NBR && hf.shoot) {
my_printf("WIN SCORE: %d, AC: %d\n", hf.score, hf.score /
hf.shoot);
return (EXIT_SUCCESS);
}
if (!hf.remaining_ducks)
return (my_printf("WIN SCORE: %d, AC: %.2f\n", hf.score, hf.score /
hf.shoot), EXIT_SUCCESS);
}
if (hf.shoot)
my_printf("SHOOT: %d, AC: %d\n", hf.shoot, hf.score / hf.shoot);
my_printf("SHOOT: %d, AC: %.2f\n", hf.shoot, hf.score / hf.shoot);
return EXIT_SUCCESS;
}
54 changes: 54 additions & 0 deletions src/renderer/ammo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
** EPITECH PROJECT, 2024
** __
** File description:
** _
*/

#include "hunter.h"
#include <SFML/Graphics.h>
#include <stdio.h>
#include <stdlib.h>

void display_ammos(hunterinfo_t *hf)
{
for (int i = 0; i < AMMO_COUNT; i++)
if (hf->ammos[i].sprite != NULL)
sfRenderWindow_drawSprite(hf->window, hf->ammos[i].sprite, NULL);
}

int fill_ammos(hunterinfo_t *hf)
{
hf->ammos[AMMO_COUNT - 1].sprite = NULL;
for (int i = 0; i < AMMO_COUNT; i++) {
hf->ammos[i].texture = sfTexture_createFromFile("assets/ammo.png",
NULL);
if (!hf->ammos[i].texture)
return EXIT_FAILURE;
hf->ammos[i].sprite = sfSprite_create();
sfSprite_setTexture(hf->ammos[i].sprite,
hf->ammos[i].texture, sfTrue);
sfSprite_setTextureRect(hf->ammos[i].sprite,
(sfIntRect){ 0, 0, 4, 7 });
hf->ammos[i].size = sfTexture_getSize(hf->ammos[i].texture);
hf->ammos[i].size.x /= 2;
sfSprite_setScale(hf->ammos[i].sprite, (sfVector2f){ 5, 5 });
sfSprite_setPosition(hf->ammos[i].sprite,
(sfVector2f){ 10 * (i + 1) + i * 20, 900});
hf->ammos[i].version = 0;
}
return EXIT_SUCCESS;
}

int change_ammo_version(hunterinfo_t *hf, int i)
{
if (hf->ammos[i].sprite == NULL)
return EXIT_SUCCESS;
if (hf->ammos[i].version == 2)
hf->ammos[i].version = 0;
if (i < 0)
return EXIT_FAILURE;
sfSprite_setTextureRect(hf->ammos[i].sprite, (sfIntRect){ 4 *
hf->ammos[i].version, 0, 4, 7 });
return EXIT_SUCCESS;
}
24 changes: 14 additions & 10 deletions src/renderer/duck.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ void display_ducks(hunterinfo_t *hf)

int fill_ducks(hunterinfo_t *hf)
{
hf->ducks[DUCK_NBR - 1].sprite = NULL;
for (int i = 0; i < DUCK_NBR; i++) {
hf->ducks[i].texture = sfTexture_createFromFile("assets/duck.png",
NULL);
hf->ducks[i].texture =
sfTexture_createFromFile("assets/duck_states.png", NULL);
if (!hf->ducks[i].texture)
return EXIT_FAILURE;
hf->ducks[i].sprite = sfSprite_create();
sfSprite_setTexture(hf->ducks[i].sprite,
hf->ducks[i].texture, sfTrue);
sfSprite_setTextureRect(hf->ducks[i].sprite,
(sfIntRect){ 110, 0, 110, 110 });
(sfIntRect){ 0, 0, 34, 30 });
hf->ducks[i].size = sfTexture_getSize(hf->ducks[i].texture);
hf->ducks[i].size.x /= 3;
hf->ducks[i].size.x *= 4;
hf->ducks[i].size.y *= 4;
hf->ducks[i].size.x /= 5;
sfSprite_setPosition(hf->ducks[i].sprite, (sfVector2f){
rand() % 600 - 600, rand() % 1000 + 50});
hf->ducks[i].version = rand() % 4;
sfSprite_setScale(hf->ducks[i].sprite, (sfVector2f){ 4, 4 });
hf->ducks[i].version = rand() % 3;
}
return EXIT_SUCCESS;
}
Expand All @@ -43,11 +45,13 @@ int move_duck(hunterinfo_t *hf, sfVector2f pos, int i)
{
if (hf->ducks[i].sprite == NULL)
return EXIT_SUCCESS;
if (hf->ducks[i].version == 3)
hf->ducks[i].version = 0;
if (hf->ducks[i].pos.y > 1100) {
hf->ducks[i].sprite = NULL;
return EXIT_SUCCESS;
}
sfSprite_move(hf->ducks[i].sprite, pos);
hf->ducks[i].pos = sfSprite_getPosition(hf->ducks[i].sprite);
sfSprite_setTextureRect(hf->ducks[i].sprite, (sfIntRect){ 110 *
hf->ducks[i].version, 0, 110, 110 });
sfSprite_setTextureRect(hf->ducks[i].sprite, (sfIntRect){ 34 *
hf->ducks[i].version, 0, 34, 30 });
return EXIT_SUCCESS;
}
25 changes: 25 additions & 0 deletions src/renderer/menu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** EPITECH PROJECT, 2024
** __
** File description:
** _
*/

#include "hunter.h"
#include "lib.h"
#include <SFML/Graphics.h>

int draw_pause(hunterinfo_t *hf)
{
sfFont *font = sfFont_createFromFile("assets/Jersey25-Regular.ttf");

if (!font)
return EXIT_FAILURE;
hf->pause_text = sfText_create();
sfText_setString(hf->pause_text, "PAUSE");
sfText_setFont(hf->pause_text, font);
sfText_setCharacterSize(hf->pause_text, 100);
sfText_setPosition(hf->pause_text, (sfVector2f){ (float)hf->window_size.x /
2, (float)hf->window_size.y / 2 });
return EXIT_SUCCESS;
}
55 changes: 45 additions & 10 deletions src/window/window_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,54 @@ int create_window(size_t x, size_t y, hunterinfo_t *hunterinfo)
return EXIT_SUCCESS;
}

static
void pause_game(hunterinfo_t *hf)
{
if (!hf->paused)
hf->paused = (sfRenderWindow_setFramerateLimit(hf->window, 0), 1);
else if (hf->paused)
hf->paused = (sfRenderWindow_setFramerateLimit(hf->window,
FRAMERATE), 0);
}

static
void reload(hunterinfo_t *hf)
{
hf->ammo = AMMO_COUNT;
fill_ammos(hf);
my_printf("RELOADED AMMO COUNT: %d\n", hf->ammo);
}

static
void kill_duck(hunterinfo_t *hf, int i)
{
hf->ducks[i].version = 3;
hf->ducks[i].touched = (move_duck(hf, (sfVector2f){ 0, 0 }, i), 1);
hf->score += 100;
hf->ammo--;
hf->ammos[hf->ammo].sprite = NULL;
hf->remaining_ducks--;
inc_score(hf);
my_printf("DUCK KILLED: %d\n", hf->score);
}

static
void shoot(hunterinfo_t *hf)
{
sfVector2i pos = sfMouse_getPositionRenderWindow(hf->window);

if (hf->ammo <= 0)
return;
my_printf("SHOOT %d, %d\n", pos.x, pos.y);
hf->shoot++;
for (int i = 0; i < DUCK_NBR; i++) {
if (pos.x < hf->ducks[i].pos.x || pos.x >
(hf->ducks[i].pos.x + hf->ducks[i].size.x) || pos.y <
hf->ducks[i].pos.y || pos.y >
(hf->ducks[i].pos.y + hf->ducks[i].size.y) ||
hf->ducks[i].sprite == NULL)
hf->ducks[i].sprite == NULL || hf->ducks[i].touched)
continue;
hf->ducks[i].sprite = NULL;
hf->score++;
inc_score(hf);
my_printf("SHOOTED SCORE: %d\n", hf->score);
kill_duck(hf, i);
}
}

Expand All @@ -48,13 +78,18 @@ void event_manager(hunterinfo_t *hf)
if (!hf->event)
hf->event = &event;
while (sfRenderWindow_pollEvent(hf->window, hf->event)) {
if (event.type == sfEvtMouseButtonPressed)
if (event.type == sfEvtKeyPressed && event.key.code == sfKeyR &&
!hf->paused)
reload(hf);
if (event.type == sfEvtKeyPressed && event.key.code == sfKeyEscape)
pause_game(hf);
if (event.type == sfEvtMouseButtonPressed && !hf->paused)
shoot(hf);
if (event.type == sfEvtClosed)
sfRenderWindow_close(hf->window);
if (event.type == sfEvtMouseMoved)
move_cursor(hf,
(sfVector2f){ event.mouseMove.x - (float)hf->cursor.size.x / 2,
event.mouseMove.y - (float)hf->cursor.size.y / 2 });
if (event.type == sfEvtMouseMoved && !hf->paused)
move_cursor(hf, (sfVector2f){ event.mouseMove.x -
(float)hf->cursor.size.x / 2, event.mouseMove.y -
(float)hf->cursor.size.y / 2 });
}
}

0 comments on commit 2afdd0a

Please sign in to comment.