aboutsummaryrefslogtreecommitdiff
path: root/src/player
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-01-28 03:01:13 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2024-01-28 15:17:49 +0100
commitfd53c7da8c5f2b79b87f624e313ff508072361ad (patch)
treedb569413cd3d42034a22368a7980aa99d7d55235 /src/player
parente49e3073c67af785447ddb0ecbdc521b68cf3ea9 (diff)
downloadjancity-fd53c7da8c5f2b79b87f624e313ff508072361ad.tar.gz
Big rushed update
Diffstat (limited to 'src/player')
-rw-r--r--src/player/CMakeLists.txt3
-rw-r--r--src/player/inc/human_player.h12
-rw-r--r--src/player/inc/player.h2
-rw-r--r--src/player/src/human_player.c23
-rw-r--r--src/player/src/human_player_gui.c170
-rw-r--r--src/player/src/player.c17
6 files changed, 205 insertions, 22 deletions
diff --git a/src/player/CMakeLists.txt b/src/player/CMakeLists.txt
index 3297619..b6c645a 100644
--- a/src/player/CMakeLists.txt
+++ b/src/player/CMakeLists.txt
@@ -18,4 +18,5 @@ target_link_libraries(player
util
PRIVATE
pad
- gui)
+ gui
+ terrain)
diff --git a/src/player/inc/human_player.h b/src/player/inc/human_player.h
index 9cfd498..19c4670 100644
--- a/src/player/inc/human_player.h
+++ b/src/player/inc/human_player.h
@@ -2,6 +2,9 @@
#define HUMAN_PLAYER_H
#include <camera.h>
+#include <gui/button.h>
+#include <gui/container.h>
+#include <gui/rounded_rect.h>
#include <keyboard.h>
#include <instance.h>
#include <input.h>
@@ -34,6 +37,15 @@ struct human_player
union peripheral *periph;
struct input in;
+ struct human_player_item
+ {
+ bool show;
+ struct gui_button show_btn;
+ struct gui_rounded_rect rr;
+ struct gui_container topcnt;
+ struct gui_button add_walker, add_car, exit;
+ } item;
+
struct sel_instance
{
enum sel_type
diff --git a/src/player/inc/player.h b/src/player/inc/player.h
index cc191ca..06463f9 100644
--- a/src/player/inc/player.h
+++ b/src/player/inc/player.h
@@ -18,7 +18,7 @@ typedef unsigned player_team;
enum
{
- PLAYER_MAX_UNITS = 5,
+ PLAYER_MAX_UNITS = 10,
PLAYER_MAX_BUILDINGS = 5
};
diff --git a/src/player/src/human_player.c b/src/player/src/human_player.c
index 6083ef3..b47f4dc 100644
--- a/src/player/src/human_player.c
+++ b/src/player/src/human_player.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--;
@@ -357,10 +356,26 @@ static void update_from_touch(struct human_player *const h,
}
}
+static bool hovering_units(const struct human_player *const h)
+{
+ const struct player *const pl = &h->pl;
+
+ for (size_t i = 0; i < sizeof pl->units / sizeof *pl->units; i++)
+ {
+ const struct unit *const u = &pl->units[i];
+ const struct instance *const in = &u->instance;
+
+ if (in->alive && cursor_collision(&h->cam, &in->r))
+ return true;
+ }
+
+ return false;
+}
+
static void update_from_keyboard_mouse(struct human_player *const h,
struct player_others *const o)
{
- const struct mouse *const m = &h->periph->kbm.mouse;
+ struct mouse *const m = &h->periph->kbm.mouse;
const struct keyboard *const k = &h->periph->kbm.keyboard;
const struct input *const in = &h->in;
@@ -377,6 +392,8 @@ static void update_from_keyboard_mouse(struct human_player *const h,
}
else if (input_mouse_justreleased(in, m, MOUSE_BUTTON_RIGHT))
move_units(h, o);
+ else
+ m->hovering = hovering_units(h);
}
void human_player_update(struct human_player *const h,
diff --git a/src/player/src/human_player_gui.c b/src/player/src/human_player_gui.c
index e985d9c..ce995c1 100644
--- a/src/player/src/human_player_gui.c
+++ b/src/player/src/human_player_gui.c
@@ -10,16 +10,146 @@
#include <gui/label.h>
#include <gui/progress_bar.h>
#include <gui/rounded_rect.h>
+#include <terrain.h>
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
-enum {X_OFF = 8, Y_OFF = 8, HP_Y = 32};
+enum {X_OFF = 8, Y_OFF = 8};
+
+static void on_item_pressed(void *const arg)
+{
+ bool *const show = arg;
+
+ *show ^= true;
+}
+
+static void on_add_car(void *const arg)
+{
+ struct human_player *const h = arg;
+ struct human_player_item *const it = &h->item;
+ const struct unit_cfg cfg =
+ {
+ .type = UNIT_CFG_TYPE_CAR,
+ .x = rand() % (MAP_W - 32),
+ .y = rand() % (MAP_H - 32),
+ };
+
+ player_create_unit(&cfg, &h->pl);
+ it->show = false;
+}
+
+static void on_add_walker(void *const arg)
+{
+ struct human_player *const h = arg;
+ struct human_player_item *const it = &h->item;
+ const struct unit_cfg cfg =
+ {
+ .type = UNIT_CFG_TYPE_WALKER,
+ .x = rand() % (MAP_W - 32),
+ .y = rand() % (MAP_H - 32),
+ };
+
+ player_create_unit(&cfg, &h->pl);
+ it->show = false;
+}
+
+static void on_exit(void *const arg)
+{
+ struct human_player *const h = arg;
+
+ h->periph->common.exit = true;
+}
+
+static void update_items(struct human_player *const h)
+{
+ struct human_player_item *const it = &h->item;
+
+ {
+ struct gui_rounded_rect *const r = &it->rr;
+
+ gui_rounded_rect_init(r);
+ r->common.hcentered = r->common.vcentered = true;
+ r->adjust = true;
+ }
+
+ {
+ struct gui_container *const c = &it->topcnt;
+
+ gui_container_init(c);
+ c->common.vcentered = true;
+ c->common.hcentered = true;
+ c->mode = GUI_CONTAINER_MODE_V;
+ c->spacing = 4;
+ gui_add_child(&it->rr.common, &c->common);
+ }
+
+ {
+ struct gui_button *const b = &it->add_car;
+
+ gui_button_init(b, GUI_BUTTON_TYPE_1);
+ b->common.hcentered = true;
+ b->u.type1.w = 80;
+ b->u.type1.label.text = "Add car";
+ b->on_pressed = on_add_car;
+ b->arg = h;
+ gui_add_child(&it->topcnt.common, &b->common);
+ }
+
+ {
+ struct gui_button *const b = &it->add_walker;
+
+ gui_button_init(b, GUI_BUTTON_TYPE_1);
+ b->u.type1.w = 80;
+ b->common.hcentered = true;
+ b->u.type1.label.text = "Add walker";
+ b->on_pressed = on_add_walker;
+ b->arg = h;
+ gui_add_child(&it->topcnt.common, &b->common);
+ }
+
+ {
+ struct gui_button *const b = &it->exit;
+
+ gui_button_init(b, GUI_BUTTON_TYPE_1);
+ b->u.type1.w = 80;
+ b->common.hcentered = true;
+ b->u.type1.label.text = "Exit";
+ b->on_pressed = on_exit;
+ b->arg = h;
+ gui_add_child(&it->topcnt.common, &b->common);
+ }
+
+ struct camera cam =
+ {
+ .cursor = h->cam.cursor
+ };
+
+ gui_update(&it->rr.common, h->periph, &cam, &h->in);
+}
void human_player_gui_update(struct human_player *const h)
{
+ struct human_player_item *const it = &h->item;
+ struct gui_button *const b = &it->show_btn;
+
+ gui_button_init(b, GUI_BUTTON_TYPE_SPRITE);
+ b->on_pressed = on_item_pressed;
+ b->arg = &it->show;
+ b->u.sprite.s = &gui_button_sprites[GUI_BUTTON_SMALL];
+ b->common.x = screen_w - b->u.sprite.s->w;
+
+ if (it->show)
+ update_items(h);
+
+ struct camera cam =
+ {
+ .cursor = h->cam.cursor
+ };
+
+ gui_update(&b->common, h->periph, &cam, &h->in);
}
static int render_sel_single_building(const struct human_player *const h,
@@ -41,14 +171,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);
@@ -99,37 +228,44 @@ static int render_sel_multiple(const struct human_player *const h,
static int render_top(const struct human_player *const h)
{
- struct gui_button b;
-
- gui_button_init(&b, GUI_BUTTON_TYPE_SPRITE);
- b.on_pressed = NULL;
- b.arg = NULL;
- b.u.sprite.s = &gui_button_sprites[GUI_BUTTON_SMALL];
- b.common.x = screen_w - b.u.sprite.s->w;
-
- return gui_render(&b.common);
+ return gui_render(&h->item.show_btn.common);
}
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)
: render_sel_multiple(h, &r.common);
}
+static int render_items(const struct human_player *const h)
+{
+ const struct human_player_item *const it = &h->item;
+
+ return gui_render(&it->rr.common);
+}
+
int human_player_gui_render(const struct human_player *const h)
{
if (render_top(h))
return -1;
else if (h->n_sel && render_sel(h))
return -1;
+ else if (h->item.show && render_items(h))
+ return -1;
return 0;
}
diff --git a/src/player/src/player.c b/src/player/src/player.c
index 5a69763..a3b0a52 100644
--- a/src/player/src/player.c
+++ b/src/player/src/player.c
@@ -1,6 +1,7 @@
#include <player.h>
#include <building.h>
#include <unit.h>
+#include <terrain.h>
#include <stddef.h>
void player_update(struct player *const p)
@@ -61,6 +62,22 @@ 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)
{
+ for (size_t i = 0; i < sizeof pl->units / sizeof *pl->units; i++)
+ {
+ const struct unit_cfg ucfg =
+ {
+ .type = rand() & 1 ? UNIT_CFG_TYPE_WALKER : UNIT_CFG_TYPE_CAR,
+ .x = rand() % (MAP_W - 32),
+ .y = rand() % (MAP_H - 32),
+ };
+
+ 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;