Compare commits
2 Commits
38caa63be7
...
83621a2811
Author | SHA1 | Date |
---|---|---|
Xavier Del Campo Romero | 83621a2811 | |
Xavier Del Campo Romero | 40767ced91 |
|
@ -80,10 +80,136 @@ sprite(NAME grass
|
|||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit1
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit2
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit3
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit4
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit5
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME unit6
|
||||
X 384
|
||||
Y 0
|
||||
BPP 4
|
||||
CX 384
|
||||
CY 48
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_down_left
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_down_right
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_mid_down
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_mid_top
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_mid_v
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_up_left
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME sel_up_right
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME checkbox
|
||||
X 384
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME line_edit_left
|
||||
X 0
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
sprite(NAME line_edit_mid
|
||||
X 0
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT FALSE)
|
||||
|
||||
sprite(NAME line_edit_right
|
||||
X 0
|
||||
Y 0
|
||||
CX 0
|
||||
CY 0
|
||||
TRANSPARENT TRUE)
|
||||
|
||||
level(NAME city1)
|
||||
|
||||
container(NAME jancity
|
||||
SPRITES
|
||||
checkbox
|
||||
grass
|
||||
sidewalk
|
||||
roof1
|
||||
|
@ -94,4 +220,20 @@ container(NAME jancity
|
|||
btn_right
|
||||
btn_small
|
||||
font
|
||||
unit1
|
||||
unit2
|
||||
unit3
|
||||
unit4
|
||||
unit5
|
||||
unit6
|
||||
sel_down_left
|
||||
sel_down_right
|
||||
sel_mid_down
|
||||
sel_mid_top
|
||||
sel_mid_v
|
||||
sel_up_left
|
||||
sel_up_right
|
||||
line_edit_left
|
||||
line_edit_mid
|
||||
line_edit_right
|
||||
)
|
||||
|
|
BIN
res/btn_left.bmp
Before Width: | Height: | Size: 586 B |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.6 KiB |
BIN
res/btn_mid.bmp
Before Width: | Height: | Size: 542 B |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.6 KiB |
Before Width: | Height: | Size: 586 B |
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 490 B |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 2.0 KiB |
After Width: | Height: | Size: 8.0 KiB |
After Width: | Height: | Size: 47 KiB |
|
@ -35,6 +35,7 @@ function(sprite)
|
|||
VERBATIM)
|
||||
add_custom_target(${SPRITE_NAME}_img
|
||||
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${SPRITE_NAME})
|
||||
add_dependencies(${PROJECT_NAME} ${SPRITE_NAME}_img)
|
||||
add_dependencies(${SPRITE_NAME}_img tools)
|
||||
endif()
|
||||
endfunction()
|
||||
|
@ -73,6 +74,7 @@ function(sound)
|
|||
VERBATIM)
|
||||
add_custom_target(${SOUND_NAME}_snd
|
||||
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${SOUND_NAME})
|
||||
add_dependencies(${PROJECT_NAME} ${SOUND_NAME}_snd)
|
||||
add_dependencies(${SOUND_NAME}_snd tools)
|
||||
endif()
|
||||
endfunction()
|
||||
|
|
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 1.6 KiB |
After Width: | Height: | Size: 822 B |
After Width: | Height: | Size: 822 B |
After Width: | Height: | Size: 822 B |
After Width: | Height: | Size: 150 B |
After Width: | Height: | Size: 182 B |
After Width: | Height: | Size: 822 B |
After Width: | Height: | Size: 822 B |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 9.1 KiB |
After Width: | Height: | Size: 9.1 KiB |
|
@ -56,7 +56,7 @@ int cursor_render(const struct cursor *const c)
|
|||
break;
|
||||
|
||||
case CURSOR_STATE_PRESSED:
|
||||
s->u += CAMERA_CURSOR_WIDTH;
|
||||
s->u += CAMERA_CURSOR_WIDTH * 2;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ static void update_speed(struct camera *const cam, const struct mouse *const m)
|
|||
{
|
||||
MAX_SPEED = 10,
|
||||
STEP = 1,
|
||||
THRESHOLD_X = CAMERA_CURSOR_WIDTH / 2,
|
||||
THRESHOLD_Y = CAMERA_CURSOR_HEIGHT / 2
|
||||
THRESHOLD_X = CAMERA_CURSOR_WIDTH,
|
||||
THRESHOLD_Y = CAMERA_CURSOR_HEIGHT
|
||||
};
|
||||
|
||||
const struct cursor *const c = &cam->cursor;
|
||||
|
|
|
@ -73,6 +73,108 @@ static const struct container c[] =
|
|||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &terrain_sprites[GRASS]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit1",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_1_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit2",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_2_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit3",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_3_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit4",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_4_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit5",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_5_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "unit6",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &unit_sprites[UNIT_6_SPRITE]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_down_left",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_DOWN_LEFT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_down_right",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_DOWN_RIGHT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_mid_down",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_DOWN]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_mid_top",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_TOP]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_mid_v",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_VERT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_up_left",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_UP_LEFT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "sel_up_right",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_UP_RIGHT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "checkbox",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_checkbox_sprite
|
||||
},
|
||||
|
||||
{
|
||||
.path = "line_edit_left",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_LEFT]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "line_edit_mid",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_MID]
|
||||
},
|
||||
|
||||
{
|
||||
.path = "line_edit_right",
|
||||
.type = CONTAINER_TYPE_SPRITE,
|
||||
.data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_RIGHT]
|
||||
}
|
||||
};
|
||||
|
||||
static bool init;
|
||||
|
|
|
@ -15,6 +15,7 @@ struct gui_rounded_rect
|
|||
{
|
||||
struct gui_common common;
|
||||
unsigned short w, h;
|
||||
unsigned char r, g, b;
|
||||
bool adjust;
|
||||
};
|
||||
|
||||
|
@ -27,13 +28,15 @@ enum
|
|||
GUI_ROUNDED_RECT_MID_VERT,
|
||||
GUI_ROUNDED_RECT_DOWN_LEFT,
|
||||
GUI_ROUNDED_RECT_DOWN_RIGHT,
|
||||
GUI_ROUNDED_RECT_MID,
|
||||
GUI_ROUNDED_RECT_MID_TOP,
|
||||
GUI_ROUNDED_RECT_MID_DOWN,
|
||||
|
||||
MAX_GUI_ROUNDED_RECT_SPRITES
|
||||
};
|
||||
|
||||
extern struct sprite gui_rounded_rect_sprites[MAX_GUI_ROUNDED_RECT_SPRITES];
|
||||
|
||||
/* Pad one line to avoid redefinition. */
|
||||
UTIL_STATIC_ASSERT(!offsetof(struct gui_rounded_rect, common),
|
||||
"unexpected offset for struct gui_rounded_rect");
|
||||
|
||||
|
|
|
@ -134,24 +134,24 @@ static int render_rect(const struct gui_rounded_rect *const r,
|
|||
rect_get_or_ret(sel, -1);
|
||||
semitrans_rect_init(sel);
|
||||
|
||||
const struct sprite *const mid = &refs[GUI_ROUNDED_RECT_MID],
|
||||
const struct sprite *const mid = &refs[GUI_ROUNDED_RECT_MID_TOP],
|
||||
*const vert = &refs[GUI_ROUNDED_RECT_MID_VERT];
|
||||
|
||||
sel->x = x + vert->w;
|
||||
sel->y = y + mid->h;
|
||||
sel->w = r->w - (vert->w * 2);
|
||||
sel->h = r->h - (mid->h * 2);
|
||||
sel->r = 72;
|
||||
sel->g = 66;
|
||||
sel->b = 56;
|
||||
sel->r = r->r;
|
||||
sel->g = r->g;
|
||||
sel->b = r->b;
|
||||
rect_sort(sel);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int render_mid(const struct gui_rounded_rect *const r,
|
||||
const short x, const short y)
|
||||
const short x, const short y, const struct sprite *const s)
|
||||
{
|
||||
const short mid_w = refs[GUI_ROUNDED_RECT_MID].w,
|
||||
const short mid_w = s->w,
|
||||
top_w = refs[GUI_ROUNDED_RECT_UP_LEFT].w,
|
||||
w = r->w - (top_w * 2),
|
||||
rem_mid = w % mid_w,
|
||||
|
@ -166,7 +166,7 @@ static int render_mid(const struct gui_rounded_rect *const r,
|
|||
{
|
||||
sprite_get_or_ret(m, -1);
|
||||
|
||||
if (sprite_clone(&refs[GUI_ROUNDED_RECT_MID], m))
|
||||
if (sprite_clone(s, m))
|
||||
return -1;
|
||||
|
||||
m->x = a.x;
|
||||
|
@ -186,13 +186,15 @@ static int render_mid(const struct gui_rounded_rect *const r,
|
|||
static int render_midtop(const struct gui_rounded_rect *const r,
|
||||
const short x, const short y)
|
||||
{
|
||||
return render_mid(r, x, y);
|
||||
return render_mid(r, x, y, &refs[GUI_ROUNDED_RECT_MID_TOP]);
|
||||
}
|
||||
|
||||
static int render_middown(const struct gui_rounded_rect *const r,
|
||||
const short x, const short y)
|
||||
{
|
||||
return render_mid(r, x, y + r->h - refs[GUI_ROUNDED_RECT_MID].h);
|
||||
const struct sprite *const s = &refs[GUI_ROUNDED_RECT_MID_DOWN];
|
||||
|
||||
return render_mid(r, x, y + r->h - s->h, s);
|
||||
}
|
||||
|
||||
static int render(const struct gui_common *const g)
|
||||
|
@ -242,8 +244,8 @@ static void add_child(struct gui_common *const parent,
|
|||
|
||||
short w, h;
|
||||
const short ref_w = refs[GUI_ROUNDED_RECT_MID_VERT].w,
|
||||
ref_h = refs[GUI_ROUNDED_RECT_MID].h,
|
||||
min_w = ref_w * 2 + refs[GUI_ROUNDED_RECT_MID].w,
|
||||
ref_h = refs[GUI_ROUNDED_RECT_MID_TOP].h,
|
||||
min_w = ref_w * 2 + refs[GUI_ROUNDED_RECT_MID_TOP].w,
|
||||
min_h = ref_h * 2 + refs[GUI_ROUNDED_RECT_MID_VERT].h;
|
||||
|
||||
child->cb->get_dim(child, &w, &h);
|
||||
|
|
|
@ -736,9 +736,10 @@ int menu_gamecfg(struct menu_common *const c, struct net_host *const h,
|
|||
|
||||
if (start)
|
||||
{
|
||||
struct game_cfg cfg =
|
||||
const struct game_cfg cfg =
|
||||
{
|
||||
.p = &c->p
|
||||
.p = &c->p,
|
||||
.map = "city1.txt"
|
||||
};
|
||||
|
||||
return game(&cfg);
|
||||
|
|
|
@ -71,12 +71,5 @@ int menu(void)
|
|||
cursor_init(&c.cam.cursor);
|
||||
peripheral_init(&cfg, &c.p);
|
||||
settings_load("settings.ini", &c.s);
|
||||
|
||||
const struct game_cfg gcfg =
|
||||
{
|
||||
.p = &c.p,
|
||||
.map = "city1.txt"
|
||||
};
|
||||
|
||||
return game(&gcfg);
|
||||
return menu_main(&c);
|
||||
}
|
||||
|
|
|
@ -279,8 +279,7 @@ static void update_selected(struct human_player *const h)
|
|||
{
|
||||
struct sel_instance *const si = &h->sel[i];
|
||||
|
||||
if (si->d.i
|
||||
&& (!si->d.i->alive || si->type == INSTANCE_TYPE_UNIT))
|
||||
if (si->d.i && !si->d.i->alive)
|
||||
{
|
||||
si->d.i = NULL;
|
||||
h->n_sel--;
|
||||
|
|
|
@ -41,14 +41,13 @@ static int render_sel_single_unit(const struct human_player *const h,
|
|||
const struct sel_instance *const sel, struct gui_common *const r)
|
||||
{
|
||||
const struct unit *const u = sel->d.u;
|
||||
enum {CARRY_X = 96, CARRY_Y = 8};
|
||||
|
||||
struct gui_label ul;
|
||||
|
||||
gui_label_init(&ul);
|
||||
ul.common.x = X_OFF;
|
||||
ul.common.y = Y_OFF;
|
||||
ul.text = unit_str(u);
|
||||
ul.common.hcentered = true;
|
||||
ul.common.vcentered = true;
|
||||
ul.text = u->name;
|
||||
gui_add_child(r, &ul.common);
|
||||
|
||||
return gui_render(r);
|
||||
|
@ -112,12 +111,18 @@ static int render_top(const struct human_player *const h)
|
|||
|
||||
static int render_sel(const struct human_player *const h)
|
||||
{
|
||||
const short wlim = 120;
|
||||
struct gui_rounded_rect r;
|
||||
short exp_w;
|
||||
|
||||
gui_rounded_rect_init(&r);
|
||||
r.common.x = 16;
|
||||
r.w = screen_w - (r.common.x * 2);
|
||||
r.h = screen_h / 4;
|
||||
r.common.hcentered = true;
|
||||
exp_w = screen_w - 32;
|
||||
r.w = exp_w > wlim ? wlim : exp_w;
|
||||
r.h = 40;
|
||||
r.r = 10;
|
||||
r.g = 137;
|
||||
r.b = 225;
|
||||
r.common.y = screen_h - r.h;
|
||||
|
||||
return h->n_sel == 1 ? render_sel_single(h, &r.common)
|
||||
|
|
|
@ -61,6 +61,18 @@ int player_create_building(const struct building_cfg *const cfg, struct player *
|
|||
|
||||
int player_init(const struct player_cfg *const cfg, struct player *const pl)
|
||||
{
|
||||
const struct unit_cfg ucfg =
|
||||
{
|
||||
.x = cfg->x,
|
||||
.y = cfg->y
|
||||
};
|
||||
|
||||
if (player_create_unit(&ucfg, pl))
|
||||
{
|
||||
fprintf(stderr, "%s: player_create_unit failed\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
pl->alive = true;
|
||||
pl->color = cfg->color;
|
||||
pl->team = cfg->team;
|
||||
|
|
|
@ -133,6 +133,8 @@ int terrain_init(const char *const path, struct terrain_map *const map)
|
|||
FILE *const f = fopen(path, "rb");
|
||||
unsigned w, h;
|
||||
|
||||
*map = (const struct terrain_map){0};
|
||||
|
||||
if (!f)
|
||||
{
|
||||
fprintf(stderr, "%s: fopen(3): %s\n", __func__, strerror(errno));
|
||||
|
|
|
@ -33,17 +33,16 @@ struct unit
|
|||
{
|
||||
struct instance instance;
|
||||
enum unit_type type;
|
||||
const char *name;
|
||||
|
||||
enum unit_dir
|
||||
{
|
||||
UNIT_DIR_N,
|
||||
UNIT_DIR_NE,
|
||||
UNIT_DIR_E,
|
||||
UNIT_DIR_SE,
|
||||
UNIT_DIR_S,
|
||||
UNIT_DIR_SW,
|
||||
UNIT_DIR_W,
|
||||
UNIT_DIR_NW
|
||||
UNIT_DIR_S,
|
||||
UNIT_DIR_N,
|
||||
UNIT_DIR_E,
|
||||
|
||||
MAX_UNIT_DIRS
|
||||
} dir;
|
||||
|
||||
enum unit_state state;
|
||||
|
@ -61,7 +60,6 @@ UTIL_STATIC_ASSERT(!offsetof(struct unit, instance), "must be at offset zero");
|
|||
|
||||
struct unit_cfg
|
||||
{
|
||||
enum unit_type type;
|
||||
unsigned long x, y;
|
||||
};
|
||||
|
||||
|
@ -72,20 +70,20 @@ bool unit_target_valid(const struct unit *u, const struct unit_target *t);
|
|||
void unit_set_target(struct unit *u, const struct unit_target *t);
|
||||
void unit_move_to(struct unit *u, unsigned long x, unsigned long y);
|
||||
void unit_update(struct unit *u);
|
||||
const char *unit_str(const struct unit *u);
|
||||
|
||||
enum
|
||||
{
|
||||
UNIT_SPRITE_N,
|
||||
UNIT_SPRITE_NE,
|
||||
UNIT_SPRITE_E,
|
||||
UNIT_SPRITE_SE,
|
||||
UNIT_SPRITE_S,
|
||||
UNIT_1_SPRITE,
|
||||
UNIT_2_SPRITE,
|
||||
UNIT_3_SPRITE,
|
||||
UNIT_4_SPRITE,
|
||||
UNIT_5_SPRITE,
|
||||
UNIT_6_SPRITE,
|
||||
|
||||
MAX_UNIT_SPRIES
|
||||
MAX_UNIT_SPRITES
|
||||
};
|
||||
|
||||
extern struct sprite unit_sprites[MAX_UNIT_SPRIES];
|
||||
extern struct sprite unit_sprites[MAX_UNIT_SPRITES];
|
||||
|
||||
enum unit_sound
|
||||
{
|
||||
|
|
|
@ -3,6 +3,13 @@
|
|||
|
||||
enum unit_type
|
||||
{
|
||||
UNIT_TYPE_1,
|
||||
UNIT_TYPE_2,
|
||||
UNIT_TYPE_3,
|
||||
UNIT_TYPE_4,
|
||||
UNIT_TYPE_5,
|
||||
UNIT_TYPE_6,
|
||||
|
||||
MAX_UNIT_TYPES
|
||||
};
|
||||
|
||||
|
|
|
@ -8,10 +8,10 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct sprite unit_sprites[MAX_UNIT_SPRIES];
|
||||
struct sprite unit_sprites[MAX_UNIT_SPRITES];
|
||||
struct sound unit_sounds[MAX_UNIT_SOUNDS];
|
||||
|
||||
enum {N_FRAMES = 5};
|
||||
enum {N_FRAMES = 3};
|
||||
|
||||
static void move_unit(struct unit *const u, const fix16_t sx, const fix16_t sy)
|
||||
{
|
||||
|
@ -21,38 +21,18 @@ static void move_unit(struct unit *const u, const fix16_t sx, const fix16_t sy)
|
|||
u->ry -= sy;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_NE:
|
||||
u->rx += sx;
|
||||
u->ry -= sy;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_E:
|
||||
u->rx += sx;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_SE:
|
||||
u->rx += sx;
|
||||
u->ry += sy;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_S:
|
||||
u->ry += sy;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_SW:
|
||||
u->rx -= sx;
|
||||
u->ry += sy;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_W:
|
||||
u->rx -= sx;
|
||||
break;
|
||||
|
||||
case UNIT_DIR_NW:
|
||||
u->rx -= sx;
|
||||
u->ry -= sy;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -61,17 +41,39 @@ static void move_unit(struct unit *const u, const fix16_t sx, const fix16_t sy)
|
|||
static void get_speed(const struct unit *const u, fix16_t *const x,
|
||||
fix16_t *const y)
|
||||
{
|
||||
static const struct speed
|
||||
struct speed
|
||||
{
|
||||
fix16_t x, y;
|
||||
} speed[1];
|
||||
} s;
|
||||
|
||||
switch (u->type)
|
||||
{
|
||||
case UNIT_TYPE_1:
|
||||
case UNIT_TYPE_2:
|
||||
case UNIT_TYPE_3:
|
||||
case UNIT_TYPE_4:
|
||||
case UNIT_TYPE_5:
|
||||
case UNIT_TYPE_6:
|
||||
{
|
||||
static const struct speed ref =
|
||||
{
|
||||
.x = FIX16_C_FROM_FLOAT(0.4),
|
||||
.y = FIX16_C_FROM_FLOAT(0.4)
|
||||
};
|
||||
|
||||
s = ref;
|
||||
}
|
||||
break;
|
||||
|
||||
case MAX_UNIT_TYPES:
|
||||
break;
|
||||
}
|
||||
|
||||
const struct speed *const s = &speed[u->type];
|
||||
const int dx = abs(u->rx - u->tx);
|
||||
const int dy = abs(u->ry - u->ty);
|
||||
|
||||
*x = dx < s->x ? dx : s->x;
|
||||
*y = dy < s->y ? dy : s->y;
|
||||
*x = dx < s.x ? dx : s.x;
|
||||
*y = dy < s.y ? dy : s.y;
|
||||
}
|
||||
|
||||
static enum unit_dir get_direction(const struct unit *const u)
|
||||
|
@ -79,18 +81,7 @@ static enum unit_dir get_direction(const struct unit *const u)
|
|||
const fix16_t x = u->rx, y = u->ry, tx = u->tx, ty = u->ty;
|
||||
enum unit_dir dir = 0;
|
||||
|
||||
if (x != tx && y != ty)
|
||||
{
|
||||
if (x > tx && y > ty)
|
||||
dir = UNIT_DIR_NW;
|
||||
else if (x < tx && y > ty)
|
||||
dir = UNIT_DIR_NE;
|
||||
else if (x < tx && y < ty)
|
||||
dir = UNIT_DIR_SE;
|
||||
else
|
||||
dir = UNIT_DIR_SW;
|
||||
}
|
||||
else if (x != tx)
|
||||
if (x != tx)
|
||||
{
|
||||
if (x > tx)
|
||||
dir = UNIT_DIR_W;
|
||||
|
@ -155,7 +146,7 @@ static bool must_move(const struct unit *const u)
|
|||
|
||||
void unit_update(struct unit *const u)
|
||||
{
|
||||
const struct instance *const i = &u->instance;
|
||||
struct instance *const i = &u->instance;
|
||||
|
||||
if (!i->alive)
|
||||
return;
|
||||
|
@ -185,8 +176,8 @@ void unit_update(struct unit *const u)
|
|||
else
|
||||
u->frame.i = 0;
|
||||
|
||||
u->instance.r.x = fix16_to_int(u->rx);
|
||||
u->instance.r.y = fix16_to_int(u->ry);
|
||||
i->r.x = fix16_to_int(u->rx);
|
||||
i->r.y = fix16_to_int(u->ry);
|
||||
}
|
||||
|
||||
void unit_set_target(struct unit *const u, const struct unit_target *const t)
|
||||
|
@ -209,171 +200,23 @@ void unit_move_to(struct unit *const u, const unsigned long x, const unsigned lo
|
|||
u->ty = y > y_off ? fix16_from_int(y - y_off) : 0;
|
||||
}
|
||||
|
||||
static int get_ux(const struct unit *const u)
|
||||
static int adjust_walker(const struct unit *const u, struct sprite *const s,
|
||||
struct instance_render_off *const off)
|
||||
{
|
||||
switch (u->dir)
|
||||
{
|
||||
case UNIT_DIR_N:
|
||||
return 0;
|
||||
|
||||
case UNIT_DIR_NE:
|
||||
/* Fall through. */
|
||||
case UNIT_DIR_NW:
|
||||
return 1;
|
||||
|
||||
case UNIT_DIR_E:
|
||||
/* Fall through. */
|
||||
case UNIT_DIR_W:
|
||||
return 2;
|
||||
|
||||
case UNIT_DIR_SE:
|
||||
/* Fall through. */
|
||||
case UNIT_DIR_SW:
|
||||
return 3;
|
||||
|
||||
case UNIT_DIR_S:
|
||||
return 4;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return -1;
|
||||
s->w = u->instance.r.w;
|
||||
s->h = u->instance.r.h;
|
||||
s->u += u->dir * s->w;
|
||||
s->v += u->frame.i * s->h;
|
||||
off->x = 0;
|
||||
off->y = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef const struct
|
||||
static int adjust_sprite(const struct unit *const u, struct sprite *const s,
|
||||
struct instance_render_off *const off)
|
||||
{
|
||||
const struct sprite *s;
|
||||
char xo, x[N_FRAMES], w[N_FRAMES];
|
||||
short y;
|
||||
short h;
|
||||
} anim_dim;
|
||||
|
||||
static anim_dim *peasant_anim(const struct unit *const u)
|
||||
{
|
||||
#if 0
|
||||
static anim_dim t[] =
|
||||
{
|
||||
{
|
||||
.s = &unit_sprites[UNIT_SPRITE_N],
|
||||
.xo = 5,
|
||||
.x = {0, 1, 1, 2, 1},
|
||||
.w = {25, 22, 24, 22, 24},
|
||||
.y = 2,
|
||||
.h = 31
|
||||
},
|
||||
|
||||
{
|
||||
.s = &unit_sprites[UNIT_SPRITE_NE],
|
||||
.xo = 11,
|
||||
.x = {0, -4, -1, -4, -2},
|
||||
.w = {18, 26, 22, 23, 20},
|
||||
.y = 2,
|
||||
.h = 31
|
||||
},
|
||||
|
||||
{
|
||||
.s = &unit_sprites[UNIT_SPRITE_E],
|
||||
.xo = 10,
|
||||
.x = {0, -6, -1, -6, -3},
|
||||
.w = {14, 26, 17, 24, 19},
|
||||
.y = 2,
|
||||
.h = 33
|
||||
},
|
||||
|
||||
{
|
||||
.s = &unit_sprites[UNIT_SPRITE_SE],
|
||||
.xo = 6,
|
||||
.x = {0, 1, 2, 0, 0},
|
||||
.w = {20, 22, 18, 21, 21},
|
||||
.y = 2,
|
||||
.h = 31
|
||||
},
|
||||
|
||||
{
|
||||
.s = &unit_sprites[UNIT_SPRITE_S],
|
||||
.xo = 7,
|
||||
.x = {0, 1, 0, 0, 0},
|
||||
.w = {24, 23, 24, 23, 24},
|
||||
.y = 2,
|
||||
.h = 33
|
||||
}
|
||||
};
|
||||
|
||||
const int ux = get_ux(u);
|
||||
|
||||
if (ux < 0)
|
||||
return NULL;
|
||||
|
||||
return &t[ux];
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct render_cfg
|
||||
{
|
||||
struct instance_render_off off;
|
||||
struct instance_render_quad qcfg;
|
||||
};
|
||||
|
||||
static anim_dim *peasant_quad(const struct unit *const u)
|
||||
{
|
||||
return peasant_anim(u);
|
||||
}
|
||||
|
||||
static void adjust_quad(const struct unit *const u, anim_dim *const dim,
|
||||
struct render_cfg *const rcfg)
|
||||
{
|
||||
const unsigned char n = u->frame.i;
|
||||
short u_off = 0;
|
||||
|
||||
for (unsigned char i = 0; i < n; i++)
|
||||
u_off += dim->w[i];
|
||||
|
||||
struct instance_render_quad *const qcfg = &rcfg->qcfg;
|
||||
|
||||
qcfg->u = u_off;
|
||||
qcfg->w = dim->w[n];
|
||||
qcfg->h = dim->h;
|
||||
|
||||
struct instance_render_off *const off = &rcfg->off;
|
||||
|
||||
off->x = dim->xo;
|
||||
off->y = dim->y;
|
||||
|
||||
switch (u->dir)
|
||||
{
|
||||
case UNIT_DIR_SW:
|
||||
/* Fall through. */
|
||||
case UNIT_DIR_W:
|
||||
/* Fall through. */
|
||||
case UNIT_DIR_NW:
|
||||
qcfg->xflip = true;
|
||||
off->x += dim->x[n];
|
||||
break;
|
||||
|
||||
default:
|
||||
qcfg->xflip = false;
|
||||
off->x += dim->x[n];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static int unit_quad(const struct unit *const u, struct render_cfg *const rcfg)
|
||||
{
|
||||
struct instance_render_quad *const qcfg = &rcfg->qcfg;
|
||||
|
||||
static anim_dim *(*const f[1])(const struct unit *);
|
||||
|
||||
anim_dim *const dim = f[u->type](u);
|
||||
|
||||
if (!dim)
|
||||
return -1;
|
||||
|
||||
adjust_quad(u, dim, rcfg);
|
||||
|
||||
if (quad_from_sprite(dim->s, qcfg->q))
|
||||
if (u->type >= UNIT_TYPE_1 && u->type <= UNIT_TYPE_6
|
||||
&& adjust_walker(u, s, off))
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
|
@ -385,22 +228,29 @@ int unit_render(const struct unit *const u, const struct camera *const cam,
|
|||
if (!u->instance.alive)
|
||||
return 0;
|
||||
|
||||
struct render_cfg rcfg;
|
||||
sprite_get_or_ret(s, -1);
|
||||
|
||||
quad_get_or_ret(q, -1);
|
||||
rcfg.qcfg.q = q;
|
||||
struct instance_render_off off;
|
||||
|
||||
if (unit_quad(u, &rcfg))
|
||||
if (sprite_clone(&unit_sprites[u->type], s))
|
||||
{
|
||||
fprintf(stderr, "%s: sprite_clone failed\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
else if (adjust_sprite(u, s, &off))
|
||||
{
|
||||
fprintf(stderr, "%s: adjust_sprite failed\n", __func__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const struct instance_render_cfg cfg =
|
||||
{
|
||||
.i = &u->instance,
|
||||
.prim_type = INSTANCE_RENDER_CFG_QUAD,
|
||||
.prim = {.quad = &rcfg.qcfg},
|
||||
.prim_type = INSTANCE_RENDER_CFG_SPRITE,
|
||||
.prim.s = s,
|
||||
.off = &off,
|
||||
.cam = cam,
|
||||
.sel = sel,
|
||||
.off = &rcfg.off
|
||||
.sel = sel
|
||||
};
|
||||
|
||||
return instance_render(&cfg);
|
||||
|
@ -409,40 +259,119 @@ int unit_render(const struct unit *const u, const struct camera *const cam,
|
|||
static void get_dimensions(const enum unit_type type, short *const w,
|
||||
short *const h)
|
||||
{
|
||||
static const struct dim
|
||||
switch (type)
|
||||
{
|
||||
short w, h;
|
||||
} dim[1];
|
||||
case UNIT_TYPE_1:
|
||||
case UNIT_TYPE_2:
|
||||
case UNIT_TYPE_3:
|
||||
case UNIT_TYPE_4:
|
||||
case UNIT_TYPE_5:
|
||||
case UNIT_TYPE_6:
|
||||
*w = *h = 16;
|
||||
break;
|
||||
|
||||
const struct dim *const d = &dim[type];
|
||||
*w = d->w;
|
||||
*h = d->h;
|
||||
case MAX_UNIT_TYPES:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static const char *get_name(const enum unit_type type)
|
||||
{
|
||||
enum {FEMALE, MALE} gender = 0;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case UNIT_TYPE_2:
|
||||
case UNIT_TYPE_5:
|
||||
gender = FEMALE;
|
||||
break;
|
||||
|
||||
case UNIT_TYPE_1:
|
||||
case UNIT_TYPE_3:
|
||||
case UNIT_TYPE_4:
|
||||
case UNIT_TYPE_6:
|
||||
gender = MALE;
|
||||
break;
|
||||
|
||||
case MAX_UNIT_TYPES:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *const female[] =
|
||||
{
|
||||
"Amelia",
|
||||
"Asher",
|
||||
"Aurora",
|
||||
"Ava",
|
||||
"Charlotte",
|
||||
"Dana",
|
||||
"Ellie",
|
||||
"Emma",
|
||||
"Ezra",
|
||||
"Haley",
|
||||
"Harper",
|
||||
"Isabella",
|
||||
"Luna",
|
||||
"Mire",
|
||||
"Olivia",
|
||||
"Sophia",
|
||||
};
|
||||
|
||||
static const char *const male[] =
|
||||
{
|
||||
"Oliver",
|
||||
"Xavi",
|
||||
"Benjamin",
|
||||
"Elijah",
|
||||
"Ethan",
|
||||
"Jack",
|
||||
"James",
|
||||
"Jan",
|
||||
"Kyte",
|
||||
"Leo",
|
||||
"Levi",
|
||||
"Liam",
|
||||
"Luca",
|
||||
"Lucas",
|
||||
"Michael",
|
||||
"Noah",
|
||||
};
|
||||
|
||||
switch (gender)
|
||||
{
|
||||
case FEMALE:
|
||||
{
|
||||
const size_t i = rand() % (sizeof female / sizeof *female);
|
||||
|
||||
return female[i];
|
||||
}
|
||||
|
||||
case MALE:
|
||||
{
|
||||
const size_t i = rand() % (sizeof male / sizeof *male);
|
||||
|
||||
return male[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void unit_create(const struct unit_cfg *const cfg, struct unit *const u)
|
||||
{
|
||||
struct instance *const i = &u->instance;
|
||||
const enum unit_type type = UNIT_TYPE_1 + (rand() % (UNIT_TYPE_6 + 1));
|
||||
|
||||
*u = (const struct unit)
|
||||
{
|
||||
.instance =
|
||||
{
|
||||
.alive = true,
|
||||
},
|
||||
|
||||
.type = cfg->type,
|
||||
.instance.alive = true,
|
||||
.dir = UNIT_DIR_S,
|
||||
.rx = fix16_from_int(cfg->x),
|
||||
.ry = fix16_from_int(cfg->y)
|
||||
.ry = fix16_from_int(cfg->y),
|
||||
.type = type,
|
||||
.name = get_name(type)
|
||||
};
|
||||
|
||||
get_dimensions(cfg->type, &i->r.w, &i->r.h);
|
||||
get_dimensions(u->type, &i->r.w, &i->r.h);
|
||||
unit_stop(u);
|
||||
}
|
||||
|
||||
const char *unit_str(const struct unit *const u)
|
||||
{
|
||||
static const char *const str[1];
|
||||
|
||||
return str[u->type];
|
||||
}
|
||||
|
|