From 28a397a6e8017320811d85d35b65049efb7d4b9a Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 16 Mar 2022 19:00:22 +0000 Subject: [PATCH] add item spawns in level 1 --- assets-cg/converters.py | 38 ++++++++++++++++-------- assets-cg/levels/lv1.txt | 10 +++---- src/game.c | 27 +++++++++++++++++ src/item.c | 64 +++++++++++++++++++--------------------- src/item.h | 4 +++ src/main.c | 15 ---------- src/render.c | 21 +++++++++---- 7 files changed, 106 insertions(+), 73 deletions(-) diff --git a/assets-cg/converters.py b/assets-cg/converters.py index 02e2e05..8f5a425 100644 --- a/assets-cg/converters.py +++ b/assets-cg/converters.py @@ -29,6 +29,26 @@ def convert(input, output, params, target): return 1 +MONSTER_IDS = { + "slime/1": 1, + "bat/2": 2, + "fire_slime/4": 3, + "gunslinger/8": 4, +} +ITEM_IDS = { + "life": 0, + "potion_atk": 1, + "potion_cooldown": 2, + "potion_def": 3, + "potion_freeze": 4, + "potion_hp": 5, + "potion_speed": 6, + "scepter1": 101, + "scepter2": 102, + "sword1": 103, + "sword2": 104, +} + def convert_level(input, params): RE_VALUES = { "name": re.compile(r'.*'), @@ -36,7 +56,8 @@ def convert_level(input, params): "spawner": re.compile(r'\d+,\d+'), "player_spawn": re.compile(r'\d+,\d+'), "wave": re.compile(r'\d+s(\s+\d+\*[a-z_]+/\d+)+'), - "delay": re.compile(r'\d+s') + "delay": re.compile(r'\d+s'), + "item": re.compile(r'[a-zA-Z_][a-zA-Z0-9_]*'), } with open(input, "r") as fp: lines = [l for l in fp.read().splitlines() if l] @@ -107,17 +128,9 @@ def convert_level(input, params): duration = int(duration[:-1]) for i, desc in enumerate(monsters): count, identity = desc.split("*") - if identity == "slime/1": - identity = 1 - elif identity == "bat/2": - identity = 2 - elif identity == "fire_slime/4": - identity = 3 - elif identity == "gunslinger/8": - identity = 4 - else: + if identity not in MONSTER_IDS: raise fxconv.FxconvError(f"unknown monster {identity}") - monsters[i] = (identity, int(count)) + monsters[i] = (MONSTER_IDS[identity], int(count)) m = bytes() for identity, amount in monsters: @@ -133,10 +146,9 @@ def convert_level(input, params): event_count += 1 elif key == "item": - raise fxconv.FxconvError("coming soon x3") events += fxconv.u32(LEVEL_EVENT_ITEM) events += fxconv.u32(2 * 65536) - events += fxconv.u32(the_item) + events += fxconv.u32(ITEM_IDS[value]) event_count += 1 o = fxconv.Structure() diff --git a/assets-cg/levels/lv1.txt b/assets-cg/levels/lv1.txt index 37e5f39..78398a0 100644 --- a/assets-cg/levels/lv1.txt +++ b/assets-cg/levels/lv1.txt @@ -6,11 +6,11 @@ player_spawn: 12,5 spawner: 18,5 spawner: 5,7 -wave: 8s 8*slime/1 -wave: 12s 10*slime/1 4*bat/2 -delay: 2s -wave: 4s 8*bat/2 -delay: 2s +wave: 12s 12*slime/1 +wave: 18s 10*slime/1 4*bat/2 +item: life +wave: 4s 6*bat/2 +item: sword1 wave: 8s 16*slime/1 8*bat/2 delay: 2s wave: 6s 8*slime/1 8*bat/2 2*fire_slime/4 diff --git a/src/game.c b/src/game.c index fc39f6c..30c93de 100644 --- a/src/game.c +++ b/src/game.c @@ -127,6 +127,33 @@ void game_next_event(game_t *g) for(int i = 0; i < event->wave->entry_count; i++) g->wave_left[i] = event->wave->entries[i].amount; } + if(event && event->type == LEVEL_EVENT_ITEM) { + int x=-1, y=-1; + for(int i = 0; i < 100; i++) { + int cx = rand() % g->map->width; + int cy = rand() % g->map->height; + map_cell_t const *cell = map_cell(g->map, cx, cy); + __auto_type tiles = g->map->tileset->tiles; + + if(!tiles[cell->base].solid && + (!cell->decor || !tiles[cell->decor].solid)) { + x = cx; + y = cy; + break; + } + } + + /* If a position can't be found, place the item on the player */ + if(x < 0 || y < 0) { + physical_t *p = getcomp(g->player, physical); + x = ffloor(p->x); + y = ffloor(p->y); + } + + entity_t *item = item_make(event->item, + (vec2){ fix(x)+fix(0.5), fix(y)+fix(0.5) }); + game_add_entity(g, item); + } } void game_shake(game_t *g, int amplitude, fixed_t duration) diff --git a/src/item.c b/src/item.c index 95819c9..2e69690 100644 --- a/src/item.c +++ b/src/item.c @@ -5,6 +5,33 @@ #include "comp/visible.h" #include +anim_t const *item_anim(int item) +{ + if(item == ITEM_LIFE) + return &anims_item_life; + else if(item == ITEM_POTION_ATK) + return &anims_item_potion_atk; + else if(item == ITEM_POTION_COOLDOWN) + return &anims_item_potion_cooldown; + else if(item == ITEM_POTION_DEF) + return &anims_item_potion_def; + else if(item == ITEM_POTION_FRZ) + return &anims_item_potion_frz; + else if(item == ITEM_POTION_HP) + return &anims_item_potion_hp; + else if(item == ITEM_POTION_SPD) + return &anims_item_potion_spd; + else if(item == ITEM_SCEPTER1) + return &anims_item_stick1; + else if(item == ITEM_SCEPTER2) + return &anims_item_stick2; + else if(item == ITEM_SWORD1) + return &anims_item_sword1; + else if(item == ITEM_SWORD2) + return &anims_item_sword2; + return NULL; +} + entity_t *item_make(int type, vec2 position) { entity_t *e = aoe_make(AOE_ITEM, position, fix(9999.0)); @@ -22,40 +49,9 @@ entity_t *item_make(int type, vec2 position) aoe->repeat_delay = 0; aoe->data.item.type = type; - if(type == ITEM_LIFE) { - visible_set_anim(e, &anims_item_life, 1); - } - else if(type == ITEM_POTION_ATK) { - visible_set_anim(e, &anims_item_potion_atk, 1); - } - else if(type == ITEM_POTION_COOLDOWN) { - visible_set_anim(e, &anims_item_potion_cooldown, 1); - } - else if(type == ITEM_POTION_DEF) { - visible_set_anim(e, &anims_item_potion_def, 1); - } - else if(type == ITEM_POTION_FRZ) { - visible_set_anim(e, &anims_item_potion_frz, 1); - } - else if(type == ITEM_POTION_HP) { - visible_set_anim(e, &anims_item_potion_hp, 1); - } - else if(type == ITEM_POTION_SPD) { - visible_set_anim(e, &anims_item_potion_spd, 1); - } - else if(type == ITEM_SCEPTER1) { - visible_set_anim(e, &anims_item_stick1, 1); - } - else if(type == ITEM_SCEPTER2) { - visible_set_anim(e, &anims_item_stick2, 1); - } - else if(type == ITEM_SWORD1) { - visible_set_anim(e, &anims_item_sword1, 1); - } - else if(type == ITEM_SWORD2) { - visible_set_anim(e, &anims_item_sword2, 1); - } - + anim_t const *anim = item_anim(type); + if(anim) + visible_set_anim(e, anim, 1); return e; } diff --git a/src/item.h b/src/item.h index cd9aab5..db5e6be 100644 --- a/src/item.h +++ b/src/item.h @@ -6,6 +6,7 @@ #include "comp/entity.h" #include "geometry.h" +#include "anim.h" enum { ITEM_LIFE = 0, @@ -24,6 +25,9 @@ enum { /**/ ITEM_EQUIPMENT_END, }; +/* Animation for each item. */ +anim_t const *item_anim(int item); + /* Create an item. This is just an AOE with a particular type. */ entity_t *item_make(int item, vec2 position); diff --git a/src/main.c b/src/main.c index 32c04f7..6be858e 100644 --- a/src/main.c +++ b/src/main.c @@ -144,21 +144,6 @@ int main(void) game_add_entity(&game, player); game.player = player; - int x=0, y=0; - for(int i = 0; i < 100; i++) { - y = rand() % game.map->height; - x = rand() % game.map->width; - map_cell_t const *cell = map_cell(game.map, x, y); - __auto_type tiles = game.map->tileset->tiles; - - if(!tiles[cell->base].solid && - (!cell->decor || !tiles[cell->decor].solid)) - break; - } - entity_t *item = item_make(ITEM_LIFE, - (vec2){ fix(x)+fix(0.5), fix(y)+fix(0.5) }); - game_add_entity(&game, item); - //--- // Main loop //--- diff --git a/src/render.c b/src/render.c index 22a115f..f1edf3a 100644 --- a/src/render.c +++ b/src/render.c @@ -4,11 +4,12 @@ #include "comp/mechanical.h" #include "comp/fighter.h" #include "comp/particle.h" +#include "anim.h" #include "enemies.h" +#include "game.h" +#include "item.h" #include "render.h" #include "skills.h" -#include "game.h" -#include "anim.h" #include #include @@ -299,6 +300,7 @@ static void render_entities(game_t const *g, camera_t const *camera, static int render_info_delay(int x, int y, level_event_t const *event) { + (void)event; extern bopti_image_t img_hud_delay; dimage(x, y, &img_hud_delay); return img_hud_delay.width; @@ -333,11 +335,18 @@ static int render_info_wave(int x, int y, level_event_t const *event, static int render_info_item(int x, int y, level_event_t const *event) { - int w; - dsize("Item", NULL, &w, NULL); + anim_t const *anim = item_anim(event->item); - dprint(x, y, C_WHITE, "Item"); - return w; + if(anim) { + anim_frame_render(x+5, y+5, anim->start[0]); + return anim->start[0]->w; + } + else { + int w; + dsize("Item", NULL, &w, NULL); + dprint(x, y, C_WHITE, "Item"); + return w; + } } static void render_info(int x, int y, game_t const *g)