diff --git a/CMakeLists.txt b/CMakeLists.txt index ea9ac1c..7c64376 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,6 +217,22 @@ add_executable(My3D src/options.c lib/gamacon/src/components/input_component.c lib/gamacon/include/components/input_component.h + lib/xmlparser/src/otherget.c + src/options.c + lib/gamacon/src/components/input_component.c + lib/gamacon/include/components/input_component.h + src/systems/teams_system.c + include/systems/teams_system.h + include/components/teams_component.h + src/components/teams_component.c + lib/gamacon/src/components/tag_component.c + lib/gamacon/include/components/tag_component.h + src/components/game_manager.c + include/components/game_manager.h + include/dpr_errors.h + include/components/game_display.h + src/components/game_display.c + src/systems/game_display_system.c ) add_compile_options(-W -Wall -Wextra -Wshadow) diff --git a/assets/ui/happiness.png b/assets/ui/happiness.png new file mode 100644 index 0000000..35fece0 Binary files /dev/null and b/assets/ui/happiness.png differ diff --git a/include/components/game_display.h b/include/components/game_display.h new file mode 100644 index 0000000..9f1bb27 --- /dev/null +++ b/include/components/game_display.h @@ -0,0 +1,24 @@ +// +// Created by anonymus-raccoon on 3/3/20. +// + +#ifndef _TEAMS_COMPONENT_C_ +#define _TEAMS_COMPONENT_H_ + +#include "component.h" + +typedef enum display_type +{ + HAPPINESS_DISPLAY +} display_type; + +struct game_display +{ + gc_component base; + display_type type; +}; + +const struct game_display game_display; +const struct gc_system game_display_system; + +#endif //_TEAMS_COMPONENT_C_ diff --git a/include/components/game_manager.h b/include/components/game_manager.h new file mode 100644 index 0000000..dc7824a --- /dev/null +++ b/include/components/game_manager.h @@ -0,0 +1,18 @@ +// +// Created by anonymus-raccoon on 3/3/20. +// + +#ifndef _TEAMS_COMPONENT_C_ +#define _TEAMS_COMPONENT_H_ + +#include "component.h" + +struct game_manager +{ + gc_component base; + int happiness; +}; + +const struct game_manager game_manager; + +#endif //_TEAMS_COMPONENT_C_ diff --git a/include/components/teams_component.h b/include/components/teams_component.h new file mode 100644 index 0000000..6db17f3 --- /dev/null +++ b/include/components/teams_component.h @@ -0,0 +1,22 @@ +// +// Created by anonymus-raccoon on 3/3/20. +// + +#ifndef _TEAMS_COMPONENT_C_ +#define _TEAMS_COMPONENT_H_ + +#include "component.h" + +struct teams_component +{ + gc_component base; + float next_teams; + float delay; + char **prefabs; + int *prefabs_size; + int prefab_count; +}; + +const struct teams_component teams_component; + +#endif //_TEAMS_COMPONENT_C_ diff --git a/include/dpr_errors.h b/include/dpr_errors.h new file mode 100644 index 0000000..58ad02b --- /dev/null +++ b/include/dpr_errors.h @@ -0,0 +1,11 @@ +// +// Created by anonymus-raccoon on 3/6/20. +// + +#ifndef _DPR_ERRORS_H_ +#define _DPR_ERRORS_H_ + +#define MULTIPLE_GAME_MGR_ERROR "Warning: two game manager exists, \ +behaviors are undefined.\n" + +#endif //_DPR_ERRORS_H_ diff --git a/include/systems/teams_system.h b/include/systems/teams_system.h new file mode 100644 index 0000000..ee190c3 --- /dev/null +++ b/include/systems/teams_system.h @@ -0,0 +1,12 @@ +// +// Created by anonymus-raccoon on 3/3/20. +// + +#ifndef _TEAMS_SYSTEM_H_ +#define _TEAMS_SYSTEM_H_ + +#include "system.h" + +const gc_system teams_system; + +#endif //_TEAMS_SYSTEM_H_ diff --git a/lib/gamacon b/lib/gamacon index db72c2c..3e0ba65 160000 --- a/lib/gamacon +++ b/lib/gamacon @@ -1 +1 @@ -Subproject commit db72c2c0f30b8b2bead72c9cc790647a9c5c6b13 +Subproject commit 3e0ba651102bb2f0dba0bd9554c77efaaf82c8de diff --git a/lib/my/my/my_getnbr.c b/lib/my/my/my_getnbr.c index e5832e0..6a837c1 100755 --- a/lib/my/my/my_getnbr.c +++ b/lib/my/my/my_getnbr.c @@ -55,7 +55,7 @@ int my_getnbr(const char *str) int start_index = -1; char c; - for (int i = 0; 1; i++) { + for (int i = 0; str[i]; i++) { c = str[i]; if ((c >= '0' && c <= '9') || c == '-' || c == '+') { if (start_index == -1) @@ -65,5 +65,7 @@ int my_getnbr(const char *str) else if (count > 0 || c == '\0') break; } + if (count <= 0) + return (0); return init_print(str, count, start_index); } diff --git a/lib/my/my/my_strdup.c b/lib/my/my/my_strdup.c index 1ae14cc..e95701b 100644 --- a/lib/my/my/my_strdup.c +++ b/lib/my/my/my_strdup.c @@ -14,6 +14,8 @@ char *my_strdup(const char *src) int length = my_strlen(src); char *ret = malloc(sizeof(char) * (length + 1)); + if (!ret) + return (NULL); for (int i = 0; i < length; i++) ret[i] = src[i]; ret[length] = '\0'; diff --git a/lib/my/my/tostr.c b/lib/my/my/tostr.c index ce3e977..025415c 100644 --- a/lib/my/my/tostr.c +++ b/lib/my/my/tostr.c @@ -54,6 +54,9 @@ char *tostr(int n) int count = get_new_size(n); char *ret = malloc(sizeof(char) * (count + 1)); + if (!ret) + return (NULL); + putnbr_in(n, base, ret, count); ret[count] = '\0'; return (ret); diff --git a/lib/my/src/#printf.c# b/lib/my/src/#printf.c# deleted file mode 100644 index c8715a9..0000000 --- a/lib/my/src/#printf.c# +++ /dev/null @@ -1,95 +0,0 @@ -/* -** EPITECH PROJECT, 2019 -** Sum stdarg -** File description: -** sum_stdarg -*/ - -#include "formaters.h" -#include "my.h" -#include -#include - -formater_t formaters[] = {{string_formater, 's'}, - {string_nonprintable_formater, 'S'}, - {char_formater, 'c'}, - {int_formater, 'i'}, - {int_formater, 'd'}, - {octal_formater, 'o'}, - {hexa_formater, 'x'}, - {big_hexa_formater, 'X'}, - {uint_formater, 'u'}, - {ptr_formater, 'p'}, - {ubinary_formater, 'b'}, - {float_formater, 'f'}, - {float_formater, 'F'}, - {no_format, '%'}, - {0, 0}}; - -const char modifiersCst[] = "#0-+ hl"; - -int format(va_list ap, char flag, char modifiers[MODIFIERS_SIZE]) -{ - for (int i = 0; formaters[i].flag; i++) { - if (formaters[i].flag == flag) { - return (formaters[i].func(ap, modifiers)); - } - } - return (0); -} - -int is_flag(char c) -{ - for (int i = 0; formaters[i].flag; i++) { - if (formaters[i].flag == c) { - return (1); - } - } - return (0); -} - -int is_modifier(char c) -{ - for (int i = 0; modifiersCst[i]; i++) { - if (modifiersCst[i] == c) - return (1); - } - return (0); -} - -int get_modifiers(const char *str, char flags[MODIFIERS_SIZE]) -{ - int i; - - for (i = 0; !is_flag(str[i]); i++) { - if (!is_modifier(str[i]) && !is_num(str[i])) - return (-1); - flags[i] = str[i]; - } - flags[i] = '\0'; - return (i); -} - -int my_printf(const char *str, ...) -{ - int count = 0; - va_list ap; - char modifiers[MODIFIERS_SIZE]; - int next_is_flag; - - va_start(ap, str); - for (int i = 0; str[i]; i++) { - if (str[i] == '%') { - next_is_flag = 1; - i++; - i += get_modifiers(&str[i], modifiers); - } else - next_is_flag = 0; - if (next_is_flag && is_flag(str[i])) - count += format(ap, str[i], modifiers); - else - count += write(1, &str[i], 1); - } - va_end(ap); - return (count); -} \ No newline at end of file diff --git a/lib/my/src/printf.c b/lib/my/src/printf.c index 451fc28..3fa08e7 100644 --- a/lib/my/src/printf.c +++ b/lib/my/src/printf.c @@ -11,20 +11,21 @@ #include formater_t formaters[] = {{string_formater, 's'}, - {string_nonprintable_formater, 'S'}, - {char_formater, 'c'}, - {int_formater, 'i'}, - {int_formater, 'd'}, - {octal_formater, 'o'}, - {hexa_formater, 'x'}, - {big_hexa_formater, 'X'}, - {uint_formater, 'u'}, - {ptr_formater, 'p'}, - {ubinary_formater, 'b'}, - {float_formater, 'f'}, - {float_formater, 'F'}, - {no_format, '%'}, - {0, 0}}; + {string_nonprintable_formater, 'S'}, + {char_formater, 'c'}, + {int_formater, 'i'}, + {int_formater, 'd'}, + {octal_formater, 'o'}, + {hexa_formater, 'x'}, + {big_hexa_formater, 'X'}, + {uint_formater, 'u'}, + {ptr_formater, 'p'}, + {ubinary_formater, 'b'}, + {float_formater, 'f'}, + {float_formater, 'F'}, + {no_format, '%'}, + {0, 0} +}; const char modifiersCst[] = "#0-+ hl"; diff --git a/lib/xmlparser b/lib/xmlparser index 91f72ab..91f3604 160000 --- a/lib/xmlparser +++ b/lib/xmlparser @@ -1 +1 @@ -Subproject commit 91f72ab5069f5fbc050e79b1c5da41216d0c4b24 +Subproject commit 91f3604772158a2ff8c17fb74459088004e4fe15 diff --git a/prefabs/game.gcprefab b/prefabs/game.gcprefab index 623b155..390779a 100644 --- a/prefabs/game.gcprefab +++ b/prefabs/game.gcprefab @@ -5,10 +5,12 @@ + + @@ -24,19 +26,22 @@ + + + + + + + + + - - - - - - - - - - - + + + + + \ No newline at end of file diff --git a/prefabs/teams/absent.gcprefab b/prefabs/teams/absent.gcprefab new file mode 100644 index 0000000..52cc769 --- /dev/null +++ b/prefabs/teams/absent.gcprefab @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/prefabs/teams/forgot_register.gcprefab b/prefabs/teams/forgot_register.gcprefab new file mode 100644 index 0000000..206b462 --- /dev/null +++ b/prefabs/teams/forgot_register.gcprefab @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/components/game_display.c b/src/components/game_display.c new file mode 100644 index 0000000..78acd75 --- /dev/null +++ b/src/components/game_display.c @@ -0,0 +1,63 @@ +/* +** EPITECH PROJECT, 2020 +** DPR +** File description: +** game_stats +*/ + +#include +#include +#include "dpr_errors.h" +#include "xml.h" +#include "component.h" +#include "utility.h" +#include "components/game_display.h" +#include + +static void ctr(void *component, va_list args) +{ + struct game_display *cmp = (struct game_display *)component; + + cmp->type = va_arg(args, display_type); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct game_display *cmp = (struct game_display *)component; + struct renderer *rend = GETCMP(entity, renderer); + + cmp->type = HAPPINESS_DISPLAY; + if (!rend || rend->type != GC_TXTREND) { + my_printf("Using a game display without a text renderer.\n"); + return; + } + ((gc_text *)rend->data)->text = malloc(sizeof(char) * 10); + if (((gc_text *)rend->data)->text) + ((gc_text *)rend->data)->text[0] = '\0'; +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct game_display game_display = { + base: { + name: "game_display", + size: sizeof(struct game_display), + dependencies: (char *[]) { + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/src/components/game_manager.c b/src/components/game_manager.c new file mode 100644 index 0000000..a28d875 --- /dev/null +++ b/src/components/game_manager.c @@ -0,0 +1,54 @@ +/* +** EPITECH PROJECT, 2020 +** DPR +** File description: +** game_stats +*/ + +#include "dpr_errors.h" +#include "xml.h" +#include "component.h" +#include "utility.h" +#include "components/game_manager.h" + +static void ctr(void *component, va_list args) +{ + struct game_manager *cmp = (struct game_manager *)component; + + cmp->happiness = va_arg(args, int); +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct game_manager *cmp = (struct game_manager *)component; + + cmp->happiness = xml_getintprop(n, "happiness"); + if (scene->get_entity_by_cmp(scene, "game_manager")) + my_printf(MULTIPLE_GAME_MGR_ERROR); +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct game_manager game_manager = { + base: { + name: "game_manager", + size: sizeof(struct game_manager), + dependencies: (char *[]) { + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/src/components/teams_component.c b/src/components/teams_component.c new file mode 100644 index 0000000..3f414cc --- /dev/null +++ b/src/components/teams_component.c @@ -0,0 +1,69 @@ +/* +** EPITECH PROJECT, 2020 +** Twac +** File description: +** camera_follow +*/ + +#include "xml.h" +#include "component.h" +#include "components/teams_component.h" +#include "utility.h" +#include + +static void ctr(void *component, va_list args) +{ + struct teams_component *cmp = (struct teams_component *)component; + + cmp->delay = va_arg(args, double); + cmp->next_teams = cmp->delay; +} + +static void fdctr(gc_entity *entity, gc_scene *scene, void *component, node *n) +{ + struct teams_component *cmp = (struct teams_component *)component; + + cmp->delay = xml_getfloatprop(n, "delay"); + cmp->next_teams = cmp->delay; + cmp->prefab_count = xml_getchildcount_filtered(n, "prefab"); + cmp->prefabs = malloc(sizeof(char *) * cmp->prefab_count); + cmp->prefabs_size = malloc(sizeof(int) * cmp->prefab_count); + n = n->child; + if (!cmp->prefabs || !cmp->prefab_count) { + cmp->prefabs = NULL; + cmp->prefabs_size = NULL; + cmp->prefab_count = 0; + return; + } + for (int i = 0; i < cmp->prefab_count; i++) { + cmp->prefabs[i] = xml_getproperty(n, "src"); + cmp->prefabs_size[i] = xml_getintprop(n, "height"); + n = n->next; + } +} + +static void dtr(void *component) +{ + (void)component; +} + +static char *serialize(void *component) +{ + (void)component; + return (NULL); +} + +const struct teams_component teams_component = { + base: { + name: "teams_component", + size: sizeof(struct teams_component), + dependencies: (char *[]) { + NULL + }, + ctr: &ctr, + fdctr: &fdctr, + dtr: &dtr, + serialize: &serialize, + destroy: &component_destroy + } +}; \ No newline at end of file diff --git a/src/game_loader.c b/src/game_loader.c index d468ab0..8da730b 100644 --- a/src/game_loader.c +++ b/src/game_loader.c @@ -8,10 +8,19 @@ #include "engine.h" #include "setup.h" #include "map_managment.h" +#include "components/teams_component.h" +#include "systems/teams_system.h" #include +#include "components/game_display.h" +#include "components/game_manager.h" int register_customcmps(gc_engine *engine) { + engine->add_component(engine, &game_manager); + engine->add_component(engine, &game_display); + engine->add_system(engine, &game_display_system); + engine->add_component(engine, &teams_component); + engine->add_system(engine, &teams_system); engine->finish_physics(engine); engine->add_callback(engine, "start_button", &start_button); engine->add_callback(engine, "options", &options); diff --git a/src/systems/game_display_system.c b/src/systems/game_display_system.c new file mode 100644 index 0000000..9adf5be --- /dev/null +++ b/src/systems/game_display_system.c @@ -0,0 +1,49 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** teams_system +*/ + +#include "entity.h" +#include "system.h" +#include +#include "components/game_display.h" +#include "components/game_manager.h" +#include "text.h" +#include "components/renderer.h" +#include + +static void update_entity(gc_engine *engine, void *system, gc_entity *entity, \ +float dtime) +{ + struct game_display *disp = GETCMP(entity, game_display); + struct renderer *rend = GETCMP(entity, renderer); + struct game_manager *manager; + gc_scene *scene = engine->scene; + gc_list *entities = scene->get_entity_by_cmp(scene, "game_manager"); + + if (!entities) + return; + manager = GETCMP(entities->data, game_manager); + if (rend->type != GC_TXTREND) + return; + if (disp->type == HAPPINESS_DISPLAY) + sprintf(((gc_text *)rend->data)->text, "%d%%", manager->happiness); +} + +static void destroy(void *system) +{ + (void)system; +} + +const gc_system game_display_system = { + name: "game_display_system", + component_name: "game_display", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &update_entity, + destroy: &destroy +}; \ No newline at end of file diff --git a/src/systems/teams_system.c b/src/systems/teams_system.c new file mode 100644 index 0000000..6f9a4fb --- /dev/null +++ b/src/systems/teams_system.c @@ -0,0 +1,77 @@ +/* +** EPITECH PROJECT, 2019 +** MUL_my_runner_2019 +** File description: +** teams_system +*/ + +#include "entity.h" +#include "system.h" +#include "prefab.h" +#include "components/teams_component.h" +#include +#include +#include +#include +#include +#include "my.h" + +static bool move_teams_up(gc_scene *scene, float amount) +{ + gc_list *list = scene->get_entity_by_cmp(scene, "tag_component"); + struct fixed_to_cam *fc; + struct tag_component *tc; + bool pm_timeout = false; + + for (; list; list = list->next) { + tc = GETCMP(list->data, tag_component); + if (my_strcmp(tc->tag, "teams")) + continue; + fc = GETCMP(list->data, fixed_to_cam); + if (!fc) + continue; + fc->pos.y -= amount; + if (fc->pos.y < 15) { + ((gc_entity *) list->data)->destroy(list->data, scene); + pm_timeout = true; + } + } + return (pm_timeout); +} + +static void update_entity(gc_engine *engine, void *system, gc_entity *entity, \ +float dtime) +{ + struct teams_component *team = GETCMP(entity, teams_component); + gc_scene *scene = engine->scene; + struct gc_list *m = scene->get_entity_by_cmp(scene, "game_manager"); + struct game_manager *manager; + int index; + + team->next_teams -= dtime; + if (team->next_teams < 0 && team->prefab_count) { + index = random() % team->prefab_count; + team->next_teams = team->delay; + if (move_teams_up(engine->scene, team->prefabs_size[index]) && m) { + manager = GETCMP(m->data, game_manager); + manager->happiness -= 10; + } + prefab_load(engine, team->prefabs[index]); + } +} + +static void destroy(void *system) +{ + (void)system; +} + +const gc_system teams_system = { + name: "teams_system", + component_name: "teams_component", + size: sizeof(gc_system), + ctr: NULL, + dtr: NULL, + check_dependencies: &system_check_dependencies, + update_entity: &update_entity, + destroy: &destroy +}; \ No newline at end of file