diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2021-07-03 00:49:03 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:20:20 +0200 |
| commit | 6b9f686913efc3725b2690033cd4f398e07076ba (patch) | |
| tree | e9aa91a6b9f617d78123ebe7ad272fc42a60d306 /src/building | |
| parent | c9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff) | |
| download | jancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz | |
Add project source code
Diffstat (limited to 'src/building')
| -rw-r--r-- | src/building/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/building/inc/building.h | 41 | ||||
| -rw-r--r-- | src/building/inc/building_type.h | 11 | ||||
| -rw-r--r-- | src/building/src/building.c | 80 |
4 files changed, 135 insertions, 0 deletions
diff --git a/src/building/CMakeLists.txt b/src/building/CMakeLists.txt new file mode 100644 index 0000000..3eb352e --- /dev/null +++ b/src/building/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(building "src/building.c") +target_include_directories(building PUBLIC "inc") +target_link_libraries(building PUBLIC gfx instance camera) diff --git a/src/building/inc/building.h b/src/building/inc/building.h new file mode 100644 index 0000000..7763b7d --- /dev/null +++ b/src/building/inc/building.h @@ -0,0 +1,41 @@ +#ifndef BUILDING_H +#define BUILDING_H + +#include <building_type.h> +#include <camera.h> +#include <gfx.h> +#include <instance.h> +#include <stdbool.h> +#include <stddef.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct building +{ + struct instance instance; + enum building_type type; +}; + +UTIL_STATIC_ASSERT(!offsetof(struct building, instance), "must be at offset zero"); + +struct building_cfg +{ + enum building_type type; + unsigned long x, y; +}; + +void building_create(const struct building_cfg *cfg, struct building *b); +int building_render(const struct building *b, const struct camera *cam, bool sel); +instance_hp building_maxhp(const struct building *b); +const char *building_str(const struct building *b); + +extern struct sprite building_sprites[MAX_BUILDING_TYPES]; + +#ifdef __cplusplus +} +#endif + +#endif /* BUILDING_H */ diff --git a/src/building/inc/building_type.h b/src/building/inc/building_type.h new file mode 100644 index 0000000..eca76b4 --- /dev/null +++ b/src/building/inc/building_type.h @@ -0,0 +1,11 @@ +#ifndef BUILDING_TYPE_H +#define BUILDING_TYPE_H + +enum building_type +{ + BUILDING_TYPE_BARRACKS, + + MAX_BUILDING_TYPES +}; + +#endif /* BUILDING_TYPE_H */ diff --git a/src/building/src/building.c b/src/building/src/building.c new file mode 100644 index 0000000..0b90370 --- /dev/null +++ b/src/building/src/building.c @@ -0,0 +1,80 @@ +#include <building.h> +#include <camera.h> +#include <gfx.h> +#include <stdbool.h> +#include <stddef.h> + +struct sprite building_sprites[MAX_BUILDING_TYPES]; + +instance_hp building_maxhp(const struct building *const b) +{ + static const instance_hp hp[] = + { + [BUILDING_TYPE_BARRACKS] = 100 + }; + + return hp[b->type]; +} + +int building_render(const struct building *const b, + const struct camera *const cam, const bool sel) +{ + if (!b->instance.alive) + return 0; + + struct sprite *const s = sprite_get(); + + if (!s || sprite_clone(&building_sprites[b->type], s)) + return -1; + + const struct instance_render_cfg cfg = + { + .i = &b->instance, + .prim_type = INSTANCE_RENDER_CFG_SPRITE, + .prim = {.s = s}, + .cam = cam, + .sel = sel, + .max_hp = building_maxhp(b) + }; + + return instance_render(&cfg); +} + +static void get_dimensions(const enum building_type type, short *const w, + short *const h) +{ + static const struct dim + { + short w, h; + } dim[] = + { + [BUILDING_TYPE_BARRACKS] = {.w = 68, .h = 66} + }; + + const struct dim *const d = &dim[type]; + *w = d->w; + *h = d->h; +} + +void building_create(const struct building_cfg *const cfg, + struct building *const b) +{ + struct instance *const i = &b->instance; + + get_dimensions(cfg->type, &i->r.w, &i->r.h); + b->type = cfg->type; + i->r.x = cfg->x; + i->r.y = cfg->y; + i->alive = true; + i->hp = building_maxhp(b); +} + +const char *building_str(const struct building *const b) +{ + static const char *const str[] = + { + [BUILDING_TYPE_BARRACKS] = "Barracks" + }; + + return str[b->type]; +} |
