diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-12 22:34:23 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-12 23:18:57 +0200 |
| commit | 5226dc466354cd15dd50b3c11660db7cb38eabed (patch) | |
| tree | a885b4af03b88cb08709f87a1bb643241c505616 /src/player | |
| parent | b2af4c6bccc4263e5c7aa96efd0f94e1b7a38231 (diff) | |
| download | jancity-5226dc466354cd15dd50b3c11660db7cb38eabed.tar.gz | |
Split peripheral-related logic into its own component
This has several advantages:
- `camera` no longer needs to define public functions for each
peripheral type.
- Peripheral-related is now no longer tighly coupled to human_player,
so peripheral logic can be reused elsewhere e.g.: on menus.
- Makes camera_update_touch consistent compared to equivalent functions,
since now `pan` has now been moved to `camera` (as it should be).
Diffstat (limited to 'src/player')
| -rw-r--r-- | src/player/inc/human_player.h | 25 | ||||
| -rw-r--r-- | src/player/src/human_player.c | 62 |
2 files changed, 25 insertions, 62 deletions
diff --git a/src/player/inc/human_player.h b/src/player/inc/human_player.h index 08f47d9..ca52112 100644 --- a/src/player/inc/human_player.h +++ b/src/player/inc/human_player.h @@ -6,6 +6,7 @@ #include <instance.h> #include <mouse.h> #include <pad.h> +#include <peripheral.h> #include <player.h> #include <stdbool.h> #include <stddef.h> @@ -19,35 +20,17 @@ enum {MAX_SELECTED_INSTANCES = 4}; struct human_player_cfg { - enum human_player_periph - { - HUMAN_PLAYER_PERIPH_PAD, - HUMAN_PLAYER_PERIPH_TOUCH, - HUMAN_PLAYER_PERIPH_KEYBOARD_MOUSE - } sel_periph; - + enum peripheral_type sel_periph; struct player_cfg pl; int padn; + struct camera_dim dim; }; struct human_player { struct player pl; struct camera cam; - enum human_player_periph sel_periph; - - union - { - struct pad pad; - - struct human_player_kbm - { - struct mouse mouse; - struct keyboard keyboard; - bool long_press, pan; - unsigned int lp_t; - } kbm; - } periph; + union peripheral periph; struct sel_instance { diff --git a/src/player/src/human_player.c b/src/player/src/human_player.c index 3430035..14e4680 100644 --- a/src/player/src/human_player.c +++ b/src/player/src/human_player.c @@ -528,9 +528,7 @@ static bool update_from_pad(struct human_player *const h, struct player_others *const o) { bool ret = false; - struct pad *const p = &h->periph.pad; - - pad_update(p); + struct pad *const p = &h->periph.pad.pad; if (pad_justpressed(p, PAD_KEY_OPTIONS) || pad_justpressed(p, PAD_KEY_EXIT)) @@ -565,13 +563,10 @@ static bool update_from_touch(struct human_player *const h, { struct mouse *const m = &h->periph.kbm.mouse; struct keyboard *const k = &h->periph.kbm.keyboard; + struct peripheral_kbm *const kbm = &h->periph.kbm; + bool *const pan = &h->cam.pan; - mouse_update(m); - keyboard_update(k); - - struct human_player_kbm *const kbm = &h->periph.kbm; - - if (mouse_pressed(m, MOUSE_BUTTON_LEFT) && !kbm->pan) + if (mouse_pressed(m, MOUSE_BUTTON_LEFT) && !*pan) { enum {LONG_PRESS_THRESHOLD = 30}; @@ -585,10 +580,10 @@ static bool update_from_touch(struct human_player *const h, } else if (mouse_justreleased(m, MOUSE_BUTTON_LEFT)) { - if (!kbm->pan && !select_instances(h, o, false, true)) + if (!*pan && !select_instances(h, o, false, true)) move_units(h, o); - kbm->pan = false; + *pan = false; kbm->long_press = false; kbm->lp_t = 0; } @@ -602,9 +597,6 @@ static bool update_from_keyboard_mouse(struct human_player *const h, struct mouse *const m = &h->periph.kbm.mouse; struct keyboard *const k = &h->periph.kbm.keyboard; - mouse_update(m); - keyboard_update(k); - if (mouse_justreleased(m, MOUSE_BUTTON_LEFT)) { const bool shift_pressed = @@ -631,29 +623,24 @@ bool human_player_update(struct human_player *const h, gui_update(h); update_selected(h); update_target(h); + peripheral_update(&h->periph); - switch (h->sel_periph) + switch (h->periph.common.type) { - case HUMAN_PLAYER_PERIPH_PAD: + case PERIPHERAL_TYPE_PAD: ret = update_from_pad(h, o); - camera_update_pad(&h->cam, &h->periph.pad); break; - case HUMAN_PLAYER_PERIPH_TOUCH: - { - struct human_player_kbm *const kbm = &h->periph.kbm; - + case PERIPHERAL_TYPE_TOUCH: ret = update_from_touch(h, o); - kbm->pan |= camera_update_touch(&h->cam, &kbm->mouse); - } break; - case HUMAN_PLAYER_PERIPH_KEYBOARD_MOUSE: + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: ret = update_from_keyboard_mouse(h, o); - camera_update_mouse(&h->cam, &h->periph.kbm.mouse); break; } + camera_update(&h->cam, &h->periph); player_update(p); } @@ -730,15 +717,15 @@ int human_player_render(const struct human_player *const h, || gui_render(h)) return -1; - switch (h->sel_periph) + switch (h->periph.common.type) { - case HUMAN_PLAYER_PERIPH_PAD: + case PERIPHERAL_TYPE_PAD: /* Fall through. */ - case HUMAN_PLAYER_PERIPH_KEYBOARD_MOUSE: + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: cursor_render(&h->cam.cursor); break; - case HUMAN_PLAYER_PERIPH_TOUCH: + case PERIPHERAL_TYPE_TOUCH: break; default: @@ -756,20 +743,13 @@ int human_player_init(const struct human_player_cfg *const cfg, if (player_init(&cfg->pl, &h->pl)) return -1; - switch (h->sel_periph = cfg->sel_periph) + const struct peripheral_cfg p_cfg = { - case HUMAN_PLAYER_PERIPH_TOUCH: - /* Fall through. */ - case HUMAN_PLAYER_PERIPH_KEYBOARD_MOUSE: - mouse_init(&h->periph.kbm.mouse); - keyboard_init(&h->periph.kbm.keyboard); - break; - - case HUMAN_PLAYER_PERIPH_PAD: - pad_init(cfg->padn, &h->periph.pad); - break; - } + .type = cfg->sel_periph, + .padn = cfg->padn + }; + peripheral_init(&p_cfg, &h->periph); cursor_init(&h->cam.cursor); h->top_gui = true; memmove(h->gui_res, h->pl.resources, sizeof h->gui_res); |
