[FL-1149] Dolphin: stage 3 (#422)
* dolphin scenes refactoring & restructuring phase 1 * Passport app: mood and level display, opens from main menu and dolphin scenes. Dolphin scenes: minor refactoring (WIP) * dolphin scene app restruct * use gui defines for screen size * mv passport to dolphin dir Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
15
applications/dolphin/scenes/assets/emotes.h
Normal file
15
applications/dolphin/scenes/assets/emotes.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
static const char* emotes_list[] = {
|
||||
"(O_o)", "(!_?)", "(^_^)", "(*__*)", "(@_@)", "(X_x)", "(>_<)", "(^ ^)", "(^_^)",
|
||||
"(-_-)", "(~_~)", "(#^.^#)", "(^ ^)", "(^.^)", "(-.-)", "zZzZ", "(^_-)", "(^_-)",
|
||||
"(+_+)", "(+o+)", "(' ')", "('-')", "('.')", "('_')", "(* > *)", "(o o)", "(^_^)",
|
||||
"(^O^)", "(^o^)", "(^o^)", "(._.)", "(_^_)", "('_')", "('_;)", "(T_T)", "(;_;)",
|
||||
"(ー_ー)", "(-.-)", "(^o^)", "(-_-)", "(=_=)", "(=^ ^=)", "(. .)", "(._.)", "( ^m^)",
|
||||
"(?_?)", "(*^_^*)", "(^<^)", "(^.^)", "(^·^)", "(^.^)", "(^_^.)", "(^_^)", "(^^)",
|
||||
"(^J^)", "(*^.^*)", "(#^.^#)", "(~o~)", "(^o^)", "(-o-)", "(^. ^)", "(^o^)", "(*^0^*)",
|
||||
"(*_*)", "(~ o ~)", "(~_~)", "(p_-)", "d[-_-]b", "(^0_0^)", "- ^ -"};
|
||||
|
||||
static const char* dialogues_list[] = {
|
||||
"Let's hack!\n\nbla bla bla\nbla bla..",
|
||||
};
|
145
applications/dolphin/scenes/assets/items.c
Normal file
145
applications/dolphin/scenes/assets/items.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#include <gui/elements.h>
|
||||
#include "applications.h"
|
||||
#include "items_i.h"
|
||||
|
||||
const Item TV = {
|
||||
.layer = 7,
|
||||
.timeout = 10,
|
||||
.x = 160,
|
||||
.y = 34,
|
||||
.icon = I_TV_20x24,
|
||||
.action_name = "Use",
|
||||
.draw = draw_tv,
|
||||
.callback = smash_tv};
|
||||
|
||||
const Item Painting = {
|
||||
.layer = 3,
|
||||
.timeout = 20,
|
||||
.x = 160,
|
||||
.y = 10,
|
||||
.icon = I_Home_painting_17x20,
|
||||
.action_name = "Inspect",
|
||||
.draw = NULL,
|
||||
.callback = inspect_painting};
|
||||
|
||||
const Item Sofa = {
|
||||
.layer = 4,
|
||||
.timeout = 100,
|
||||
.x = 250,
|
||||
.y = 34,
|
||||
.icon = I_Sofa_40x13,
|
||||
.action_name = "Sit",
|
||||
.draw = NULL,
|
||||
.callback = sofa_sit};
|
||||
|
||||
const Item PC = {
|
||||
.layer = 4,
|
||||
.timeout = 100,
|
||||
.x = 400,
|
||||
.y = 10,
|
||||
.icon = I_PC_22x29,
|
||||
.action_name = "Use",
|
||||
.draw = NULL,
|
||||
.callback = pc_callback};
|
||||
|
||||
const Item* Home[ITEMS_NUM] = {&TV, &Sofa, &Painting, &PC};
|
||||
const Item** Scenes[1] = {*&Home};
|
||||
|
||||
const Item** get_scene(SceneState* state) {
|
||||
return Scenes[state->scene_id];
|
||||
}
|
||||
|
||||
static void dolphin_scene_start_app(SceneState* state, const FlipperApplication* flipper_app) {
|
||||
furi_assert(state);
|
||||
furi_assert(flipper_app);
|
||||
|
||||
state->scene_app_thread = furi_thread_alloc();
|
||||
|
||||
furi_assert(flipper_app->app);
|
||||
furi_assert(flipper_app->name);
|
||||
|
||||
furi_thread_set_name(state->scene_app_thread, flipper_app->name);
|
||||
furi_thread_set_stack_size(state->scene_app_thread, flipper_app->stack_size);
|
||||
furi_thread_set_callback(state->scene_app_thread, flipper_app->app);
|
||||
furi_thread_start(state->scene_app_thread);
|
||||
}
|
||||
|
||||
const void scene_activate_item_callback(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
const Item* near = is_nearby(state);
|
||||
if(near && state->use_pending == true) {
|
||||
state->action_timeout = near->timeout;
|
||||
near->callback(canvas, state);
|
||||
state->use_pending = false;
|
||||
} else if(near) {
|
||||
near->callback(canvas, state);
|
||||
}
|
||||
}
|
||||
|
||||
const Item* is_nearby(SceneState* state) {
|
||||
furi_assert(state);
|
||||
uint8_t item = 0;
|
||||
bool found = false;
|
||||
const Item** current = get_scene(state);
|
||||
while(item < ITEMS_NUM) {
|
||||
int32_t rel =
|
||||
(DOLPHIN_CENTER + DOLPHIN_WIDTH / 2 -
|
||||
(current[item]->x - state->player_global.x) * PARALLAX(current[item]->layer));
|
||||
if(abs(rel) <= DOLPHIN_WIDTH / 2) {
|
||||
found = !found;
|
||||
break;
|
||||
}
|
||||
++item;
|
||||
}
|
||||
return found ? current[item] : NULL;
|
||||
}
|
||||
|
||||
void draw_tv(Canvas* canvas, void* state) {
|
||||
furi_assert(state);
|
||||
SceneState* s = state;
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_box(
|
||||
canvas, (TV.x + 3 - s->player_global.x) * PARALLAX(TV.layer), TV.y + 4, 16, 20);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_bitmap_mode(canvas, true);
|
||||
}
|
||||
|
||||
void smash_tv(Canvas* canvas, void* state) {
|
||||
furi_assert(state);
|
||||
SceneState* s = state;
|
||||
s->player_flipped = true;
|
||||
canvas_set_bitmap_mode(canvas, true);
|
||||
canvas_draw_icon_name(
|
||||
canvas, ((TV.x - 5) - s->player_global.x) * PARALLAX(TV.layer), TV.y - 2, I_FX_Bang_32x6);
|
||||
canvas_set_bitmap_mode(canvas, false);
|
||||
if(s->action_timeout < TV.timeout - 2) {
|
||||
elements_multiline_text_framed(canvas, 80, 24, "Bang!");
|
||||
}
|
||||
}
|
||||
|
||||
void sofa_sit(Canvas* canvas, void* state) {
|
||||
furi_assert(state);
|
||||
SceneState* s = state;
|
||||
// temp fix pos
|
||||
s->player_global.x = 154;
|
||||
s->dolphin_gfx = A_FX_Sitting_40x27;
|
||||
s->dolphin_gfx_b = I_FX_SittingB_40x27;
|
||||
}
|
||||
|
||||
void inspect_painting(Canvas* canvas, void* state) {
|
||||
furi_assert(state);
|
||||
SceneState* s = state;
|
||||
if(s->use_pending) {
|
||||
dolphin_scene_start_app(s, &FLIPPER_SCENE_APPS[0]);
|
||||
}
|
||||
}
|
||||
|
||||
void pc_callback(Canvas* canvas, void* state) {
|
||||
furi_assert(state);
|
||||
SceneState* s = state;
|
||||
if(s->use_pending) {
|
||||
dolphin_scene_start_app(s, &FLIPPER_SCENE_APPS[1]);
|
||||
}
|
||||
}
|
8
applications/dolphin/scenes/assets/items.h
Normal file
8
applications/dolphin/scenes/assets/items.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include "dolphin/scenes/scene.h"
|
||||
|
||||
#define ITEMS_NUM 4
|
||||
|
||||
const Item* is_nearby(SceneState* state);
|
||||
const Item** get_scene(SceneState* state);
|
||||
const void scene_activate_item_callback(SceneState* state, Canvas* canvas);
|
8
applications/dolphin/scenes/assets/items_i.h
Normal file
8
applications/dolphin/scenes/assets/items_i.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include "items.h"
|
||||
|
||||
void smash_tv(Canvas* canvas, void* state);
|
||||
void draw_tv(Canvas* canvas, void* state);
|
||||
void sofa_sit(Canvas* canvas, void* state);
|
||||
void inspect_painting(Canvas* canvas, void* state);
|
||||
void pc_callback(Canvas* canvas, void* state);
|
139
applications/dolphin/scenes/scene.c
Normal file
139
applications/dolphin/scenes/scene.c
Normal file
@@ -0,0 +1,139 @@
|
||||
#include <furi.h>
|
||||
#include <api-hal.h>
|
||||
#include "scene.h"
|
||||
|
||||
static SceneAppGui* scene_app_gui = NULL;
|
||||
static ValueMutex* scene_state_mutex = NULL;
|
||||
|
||||
void dolphin_scene_redraw(Canvas* canvas, void* ctx) {
|
||||
furi_assert(canvas);
|
||||
furi_assert(ctx);
|
||||
|
||||
SceneState* state = (SceneState*)acquire_mutex((ValueMutex*)ctx, 25);
|
||||
if(state == NULL) return; // redraw fail
|
||||
uint32_t t = xTaskGetTickCount();
|
||||
|
||||
canvas_clear(canvas);
|
||||
dolphin_scene_render(state, canvas, t);
|
||||
dolphin_scene_render_state(state, canvas);
|
||||
release_mutex((ValueMutex*)ctx, state);
|
||||
}
|
||||
|
||||
void dolphin_scene_handle_input(SceneState* state, InputEvent* input) {
|
||||
// printf("[kb] event: %02x %s\n", input->key, input->state ? "pressed" : "released");
|
||||
dolphin_scene_handle_user_input(state, input);
|
||||
}
|
||||
|
||||
void dolphin_scene_tick_handler(SceneState* state, uint32_t t, uint32_t dt) {
|
||||
// printf("t: %d, dt: %d\n", t, dt);
|
||||
dolphin_scene_coordinates(state, dt);
|
||||
dolphin_scene_update_state(state, t, dt);
|
||||
}
|
||||
|
||||
static void scene_engine_tick_callback(void* p) {
|
||||
osMessageQueueId_t event_queue = p;
|
||||
AppEvent event;
|
||||
event.type = EventTypeTick;
|
||||
osMessageQueuePut(event_queue, (void*)&event, 0, 0);
|
||||
}
|
||||
|
||||
static void scene_engine_input_callback(InputEvent* input_event, void* ctx) {
|
||||
osMessageQueueId_t event_queue = ctx;
|
||||
AppEvent event;
|
||||
event.type = EventTypeKey;
|
||||
event.value.input = *input_event;
|
||||
osMessageQueuePut(event_queue, (void*)&event, 0, osWaitForever);
|
||||
}
|
||||
|
||||
void scene_alloc() {
|
||||
printf("scene_alloc: start\r\n");
|
||||
furi_assert(scene_app_gui == NULL);
|
||||
furi_assert(scene_state_mutex == NULL);
|
||||
|
||||
// SceneAppGui
|
||||
scene_app_gui = furi_alloc(sizeof(SceneAppGui));
|
||||
scene_app_gui->mqueue = osMessageQueueNew(2, sizeof(AppEvent), NULL);
|
||||
scene_app_gui->gui = furi_record_open("gui");
|
||||
scene_app_gui->view_port = view_port_alloc();
|
||||
scene_app_gui->timer =
|
||||
osTimerNew(scene_engine_tick_callback, osTimerPeriodic, scene_app_gui->mqueue, NULL);
|
||||
printf("scene_alloc: timer %p\r\n", scene_app_gui->timer);
|
||||
// Scene State
|
||||
SceneState* scene_state = furi_alloc(sizeof(SceneState));
|
||||
scene_state->player.y = DOLPHIN_DEFAULT_Y;
|
||||
scene_state->player.x = DOLPHIN_CENTER;
|
||||
scene_state->player_global.x = random() % WORLD_WIDTH / 4;
|
||||
scene_state->screen.x = scene_state->player.x;
|
||||
scene_state->screen.y = scene_state->player.y;
|
||||
|
||||
scene_state_mutex = furi_alloc(sizeof(ValueMutex));
|
||||
furi_check(init_mutex(scene_state_mutex, scene_state, sizeof(SceneState)));
|
||||
|
||||
// Open GUI and register fullscreen view_port
|
||||
view_port_draw_callback_set(scene_app_gui->view_port, dolphin_scene_redraw, scene_state_mutex);
|
||||
view_port_input_callback_set(
|
||||
scene_app_gui->view_port, scene_engine_input_callback, scene_app_gui->mqueue);
|
||||
gui_add_view_port(scene_app_gui->gui, scene_app_gui->view_port, GuiLayerMain);
|
||||
view_port_enabled_set(scene_app_gui->view_port, true);
|
||||
printf("scene_alloc: complete\r\n");
|
||||
}
|
||||
|
||||
void scene_free() {
|
||||
printf("scene_free: start\r\n");
|
||||
view_port_enabled_set(scene_app_gui->view_port, false);
|
||||
gui_remove_view_port(scene_app_gui->gui, scene_app_gui->view_port);
|
||||
|
||||
SceneState* scene_state = (SceneState*)acquire_mutex_block(scene_state_mutex);
|
||||
furi_assert(scene_state);
|
||||
free(scene_state);
|
||||
release_mutex(scene_state_mutex, scene_state);
|
||||
delete_mutex(scene_state_mutex);
|
||||
free(scene_state_mutex);
|
||||
scene_state_mutex = NULL;
|
||||
|
||||
furi_check(osTimerDelete(scene_app_gui->timer) == osOK);
|
||||
furi_record_close("gui");
|
||||
view_port_free(scene_app_gui->view_port);
|
||||
furi_check(osMessageQueueDelete(scene_app_gui->mqueue) == osOK);
|
||||
free(scene_app_gui);
|
||||
scene_app_gui = NULL;
|
||||
printf("scene_free: complete\r\n");
|
||||
}
|
||||
|
||||
int32_t scene_app(void* p) {
|
||||
api_hal_power_insomnia_enter();
|
||||
scene_alloc();
|
||||
|
||||
osTimerStart(scene_app_gui->timer, 40);
|
||||
|
||||
uint32_t t = xTaskGetTickCount();
|
||||
uint32_t prev_t = 0;
|
||||
|
||||
while(1) {
|
||||
AppEvent event;
|
||||
if(osMessageQueueGet(scene_app_gui->mqueue, &event, 0, osWaitForever) == osOK) {
|
||||
SceneState* scene_state = (SceneState*)acquire_mutex_block(scene_state_mutex);
|
||||
if(event.type == EventTypeTick) {
|
||||
t = xTaskGetTickCount();
|
||||
dolphin_scene_tick_handler(scene_state, t, (t - prev_t) % 1024);
|
||||
prev_t = t;
|
||||
} else if(event.type == EventTypeKey) {
|
||||
if(event.value.input.key == InputKeyBack &&
|
||||
event.value.input.type == InputTypeShort) {
|
||||
release_mutex(scene_state_mutex, scene_state);
|
||||
break;
|
||||
|
||||
} else {
|
||||
dolphin_scene_handle_input(scene_state, &event.value.input);
|
||||
}
|
||||
}
|
||||
release_mutex(scene_state_mutex, scene_state);
|
||||
view_port_update(scene_app_gui->view_port);
|
||||
}
|
||||
}
|
||||
|
||||
osTimerStop(scene_app_gui->timer);
|
||||
scene_free();
|
||||
api_hal_power_insomnia_exit();
|
||||
return 0;
|
||||
}
|
139
applications/dolphin/scenes/scene.h
Normal file
139
applications/dolphin/scenes/scene.h
Normal file
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi.h>
|
||||
#include <gui/gui_i.h>
|
||||
#include <u8g2/u8g2.h>
|
||||
|
||||
#ifndef ARRSIZE
|
||||
#define ARRSIZE(arr) (sizeof(arr) / sizeof(arr[0]))
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
||||
#endif
|
||||
|
||||
#ifndef CLAMP
|
||||
#define CLAMP(x, upper, lower) (MIN(upper, MAX(x, lower)))
|
||||
#endif
|
||||
|
||||
// global
|
||||
#define SCALE 32
|
||||
// screen
|
||||
|
||||
#define SCREEN_WIDTH GUI_DISPLAY_WIDTH
|
||||
#define SCREEN_HEIGHT GUI_DISPLAY_HEIGHT
|
||||
#define BONDARIES_X_LEFT 40
|
||||
#define BONDARIES_X_RIGHT 88
|
||||
|
||||
// player
|
||||
#define DOLPHIN_WIDTH 32
|
||||
#define DOLPHIN_HEIGHT 32
|
||||
#define DOLPHIN_CENTER (SCREEN_WIDTH / 2 - DOLPHIN_WIDTH / 2)
|
||||
#define SPEED_X 2
|
||||
#define ACTIONS_NUM 5
|
||||
#define DOLPHIN_DEFAULT_Y 20
|
||||
// world
|
||||
#define WORLD_WIDTH 2048
|
||||
#define WORLD_HEIGHT 64
|
||||
|
||||
#define LAYERS 8
|
||||
#define SCENE_ZOOM 9
|
||||
#define DOLPHIN_LAYER 6
|
||||
#define PARALLAX_MOD 7
|
||||
#define PARALLAX(layer) layer / PARALLAX_MOD - layer
|
||||
|
||||
#define DIALOG_PROGRESS 250
|
||||
|
||||
enum Actions { SLEEP = 0, IDLE, WALK, EMOTE, INTERACT, MINDCONTROL };
|
||||
|
||||
static const uint16_t default_timeout[] =
|
||||
{[SLEEP] = 300, [IDLE] = 100, [WALK] = 100, [EMOTE] = 50, [INTERACT] = 10, [MINDCONTROL] = 50};
|
||||
|
||||
typedef enum {
|
||||
EventTypeTick,
|
||||
EventTypeKey,
|
||||
} EventType;
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
InputEvent input;
|
||||
} value;
|
||||
EventType type;
|
||||
} AppEvent;
|
||||
|
||||
typedef struct {
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
} Vec2;
|
||||
|
||||
typedef struct {
|
||||
osMessageQueueId_t mqueue;
|
||||
Gui* gui;
|
||||
ViewPort* view_port;
|
||||
osTimerId_t* timer;
|
||||
} SceneAppGui;
|
||||
|
||||
typedef struct {
|
||||
uint8_t layer;
|
||||
uint16_t timeout;
|
||||
int32_t x;
|
||||
int32_t y;
|
||||
IconName icon;
|
||||
char action_name[16];
|
||||
void (*draw)(Canvas* canvas, void* model);
|
||||
void (*callback)(Canvas* canvas, void* model);
|
||||
} Item;
|
||||
|
||||
typedef struct {
|
||||
///
|
||||
Vec2 player;
|
||||
Vec2 player_global;
|
||||
Vec2 player_v;
|
||||
Vec2 screen;
|
||||
|
||||
IconName dolphin_gfx;
|
||||
IconName dolphin_gfx_b; // temp
|
||||
|
||||
bool player_flipped;
|
||||
bool use_pending;
|
||||
// dolphin_scene_debug
|
||||
bool debug;
|
||||
|
||||
uint8_t player_anim;
|
||||
uint8_t scene_id;
|
||||
|
||||
uint8_t emote_id;
|
||||
uint8_t previous_emote;
|
||||
|
||||
uint8_t dialogue_id;
|
||||
uint8_t previous_dialogue;
|
||||
|
||||
uint32_t action_timeout;
|
||||
uint8_t poi;
|
||||
|
||||
uint8_t action;
|
||||
uint8_t next_action;
|
||||
uint8_t prev_action;
|
||||
|
||||
int8_t zoom_v;
|
||||
uint8_t scene_zoom;
|
||||
uint8_t dialog_progress;
|
||||
|
||||
FuriThread* scene_app_thread;
|
||||
} SceneState;
|
||||
|
||||
void dolphin_scene_render(SceneState* state, Canvas* canvas, uint32_t t);
|
||||
void dolphin_scene_render_dolphin(SceneState* state, Canvas* canvas);
|
||||
void dolphin_scene_handle_user_input(SceneState* state, InputEvent* input);
|
||||
void dolphin_scene_coordinates(SceneState* state, uint32_t dt);
|
||||
|
||||
void dolphin_scene_render_state(SceneState* state, Canvas* canvas);
|
||||
void dolphin_scene_update_state(SceneState* state, uint32_t t, uint32_t dt);
|
||||
|
||||
void dolphin_scene_redraw(Canvas* canvas, void* ctx);
|
||||
void dolphin_scene_tick_handler(SceneState* state, uint32_t t, uint32_t dt);
|
||||
void dolphin_scene_handle_input(SceneState* state, InputEvent* input);
|
71
applications/dolphin/scenes/scene_controls.c
Normal file
71
applications/dolphin/scenes/scene_controls.c
Normal file
@@ -0,0 +1,71 @@
|
||||
#include <furi.h>
|
||||
#include <gui/elements.h>
|
||||
#include "scene.h"
|
||||
|
||||
void dolphin_scene_handle_user_input(SceneState* state, InputEvent* input) {
|
||||
furi_assert(state);
|
||||
furi_assert(input);
|
||||
|
||||
// dolphin_scene_debug
|
||||
if(input->type == InputTypeShort) {
|
||||
if(input->key == InputKeyUp) {
|
||||
state->debug = !state->debug;
|
||||
}
|
||||
}
|
||||
// toggle mind control on any user interaction
|
||||
if(input->type == InputTypePress) {
|
||||
if(input->key == InputKeyLeft || input->key == InputKeyRight || input->key == InputKeyOk) {
|
||||
state->action = MINDCONTROL;
|
||||
}
|
||||
}
|
||||
// zoom poc for tests
|
||||
if(input->type == InputTypePress) {
|
||||
if(input->key == InputKeyDown) {
|
||||
state->zoom_v = SPEED_X;
|
||||
}
|
||||
} else if(input->type == InputTypeRelease) {
|
||||
if(input->key == InputKeyDown) {
|
||||
state->zoom_v = -SPEED_X * 2;
|
||||
state->dialog_progress = 0;
|
||||
}
|
||||
}
|
||||
// mind control
|
||||
if(state->action == MINDCONTROL) {
|
||||
if(input->type == InputTypePress) {
|
||||
if(input->key == InputKeyRight) {
|
||||
state->player_flipped = false;
|
||||
state->player_v.x = SPEED_X;
|
||||
} else if(input->key == InputKeyLeft) {
|
||||
state->player_flipped = true;
|
||||
state->player_v.x = -SPEED_X;
|
||||
}
|
||||
} else if(input->type == InputTypeRelease) {
|
||||
if(input->key == InputKeyRight || input->key == InputKeyLeft) {
|
||||
state->player_v.x = 0;
|
||||
}
|
||||
} else if(input->type == InputTypeShort) {
|
||||
if(input->key == InputKeyOk) {
|
||||
state->prev_action = MINDCONTROL;
|
||||
state->action = INTERACT;
|
||||
state->use_pending = true;
|
||||
state->action_timeout = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dolphin_scene_coordinates(SceneState* state, uint32_t dt) {
|
||||
furi_assert(state);
|
||||
|
||||
// global pos
|
||||
state->player_global.x = CLAMP(state->player_global.x + state->player_v.x, WORLD_WIDTH, 0);
|
||||
|
||||
// zoom handlers
|
||||
state->scene_zoom = CLAMP(state->scene_zoom + state->zoom_v, SCENE_ZOOM, 0);
|
||||
state->player.x = CLAMP(state->player.x - (state->zoom_v * (SPEED_X * 2)), DOLPHIN_CENTER, 0);
|
||||
state->player.y = CLAMP(state->player.y - (state->zoom_v * SPEED_X / 2), DOLPHIN_DEFAULT_Y, 3);
|
||||
|
||||
//center screen
|
||||
state->screen.x = state->player_global.x - state->player.x;
|
||||
state->player_anim = (state->player_global.x / 10) % 2;
|
||||
}
|
100
applications/dolphin/scenes/scene_dolphin_state.c
Normal file
100
applications/dolphin/scenes/scene_dolphin_state.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#include <furi.h>
|
||||
#include "scene.h"
|
||||
#include "assets/emotes.h"
|
||||
|
||||
static uint16_t roll_new(uint16_t prev, uint16_t max) {
|
||||
uint16_t val = 999;
|
||||
while(val != prev) {
|
||||
val = random() % max;
|
||||
break;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
static void scene_proceed_action(SceneState* state) {
|
||||
furi_assert(state);
|
||||
|
||||
state->prev_action = state->action;
|
||||
state->action = (state->prev_action != state->next_action) ?
|
||||
state->next_action :
|
||||
roll_new(state->next_action, ACTIONS_NUM);
|
||||
state->action_timeout = default_timeout[state->action];
|
||||
}
|
||||
|
||||
static void scene_dolphin_go_to_poi(SceneState* state) {
|
||||
furi_assert(state);
|
||||
if(state->player_global.x < state->poi) {
|
||||
state->player_flipped = false;
|
||||
state->player_v.x = SPEED_X / 2;
|
||||
} else if(state->player_global.x > state->poi) {
|
||||
state->player_flipped = true;
|
||||
state->player_v.x = -SPEED_X / 2;
|
||||
}
|
||||
}
|
||||
|
||||
static void scene_action_handler(SceneState* state) {
|
||||
furi_assert(state);
|
||||
if(state->action == MINDCONTROL && state->player_v.x != 0) {
|
||||
state->action_timeout = default_timeout[state->action];
|
||||
}
|
||||
|
||||
if(state->action_timeout > 0) {
|
||||
state->action_timeout--;
|
||||
} else {
|
||||
if(random() % 1000 > 500) {
|
||||
state->next_action = roll_new(state->prev_action, ACTIONS_NUM);
|
||||
state->poi = roll_new(state->player_global.x, WORLD_WIDTH / 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dolphin_scene_update_state(SceneState* state, uint32_t t, uint32_t dt) {
|
||||
furi_assert(state);
|
||||
scene_action_handler(state);
|
||||
UNUSED(dialogues_list);
|
||||
|
||||
switch(state->action) {
|
||||
case WALK:
|
||||
if(state->player_global.x == state->poi) {
|
||||
state->player_v.x = 0;
|
||||
scene_proceed_action(state);
|
||||
} else {
|
||||
scene_dolphin_go_to_poi(state);
|
||||
}
|
||||
break;
|
||||
case EMOTE:
|
||||
state->player_flipped = false;
|
||||
if(state->action_timeout == 0) {
|
||||
scene_proceed_action(state);
|
||||
state->emote_id = roll_new(state->previous_emote, ARRSIZE(emotes_list));
|
||||
break;
|
||||
}
|
||||
case INTERACT:
|
||||
if(state->action_timeout == 0) {
|
||||
if(state->prev_action == MINDCONTROL) {
|
||||
state->action = MINDCONTROL;
|
||||
} else {
|
||||
scene_proceed_action(state);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SLEEP:
|
||||
if(state->poi != 154) { // temp
|
||||
state->poi = 154;
|
||||
} else if(state->player_global.x != state->poi) {
|
||||
scene_dolphin_go_to_poi(state);
|
||||
} else {
|
||||
state->player_v.x = 0;
|
||||
if(state->action_timeout == 0) {
|
||||
state->poi = roll_new(state->player_global.x, WORLD_WIDTH / 4);
|
||||
scene_proceed_action(state);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if(state->action_timeout == 0) {
|
||||
scene_proceed_action(state);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
197
applications/dolphin/scenes/scene_gfx.c
Normal file
197
applications/dolphin/scenes/scene_gfx.c
Normal file
@@ -0,0 +1,197 @@
|
||||
#include <furi.h>
|
||||
#include "scene.h"
|
||||
#include "assets/emotes.h"
|
||||
#include "assets/items.h"
|
||||
#include <gui/elements.h>
|
||||
|
||||
const char* action_str[] = {"Sleep", "Idle", "Walk", "Emote", "Use", "MC"};
|
||||
|
||||
static void scene_draw_hint(SceneState* state, Canvas* canvas, bool glitching) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
char buf[32];
|
||||
|
||||
const Item* near = is_nearby(state);
|
||||
if(near) {
|
||||
int32_t hint_pos_x = (near->x - state->player_global.x) * PARALLAX(near->layer) + 25;
|
||||
int8_t hint_pos_y = near->y < 15 ? near->y + 4 : near->y - 16;
|
||||
|
||||
strcpy(buf, near->action_name);
|
||||
if(glitching) {
|
||||
for(size_t g = 0; g != state->action_timeout; g++) {
|
||||
buf[(g * 23) % strlen(buf)] = ' ' + (random() % g * 17) % ('z' - ' ');
|
||||
}
|
||||
}
|
||||
|
||||
canvas_draw_str(canvas, hint_pos_x, hint_pos_y, buf);
|
||||
}
|
||||
}
|
||||
|
||||
static void scene_draw_current_emote(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
elements_multiline_text_framed(canvas, 80, 20, (char*)emotes_list[state->emote_id]);
|
||||
}
|
||||
|
||||
static void scene_draw_sleep_emote(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
char dialog_str[] = "zZzZ...";
|
||||
char buf[64];
|
||||
// 2do - sofa x pos getter
|
||||
if(state->player_global.x == 154 && state->action_timeout % 100 < 30) {
|
||||
if(state->dialog_progress < strlen(dialog_str)) {
|
||||
if(state->action_timeout % 5 == 0) state->dialog_progress++;
|
||||
dialog_str[state->dialog_progress] = '\0';
|
||||
snprintf(buf, state->dialog_progress, dialog_str);
|
||||
// bubble vs just text?
|
||||
//elements_multiline_text_framed(canvas, 80, 20, buf);
|
||||
canvas_draw_str(canvas, 80, 20, buf);
|
||||
}
|
||||
|
||||
} else {
|
||||
state->dialog_progress = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void scene_draw_dialog(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
char dialog_str[64];
|
||||
char buf[64];
|
||||
|
||||
strcpy(dialog_str, (char*)dialogues_list[state->dialogue_id]);
|
||||
|
||||
if(state->dialog_progress <= strlen(dialog_str)) {
|
||||
if(state->action_timeout % 2 == 0) state->dialog_progress++;
|
||||
dialog_str[state->dialog_progress] = '\0';
|
||||
snprintf(buf, state->dialog_progress, dialog_str);
|
||||
} else {
|
||||
snprintf(buf, 64, dialog_str);
|
||||
}
|
||||
|
||||
elements_multiline_text_framed(canvas, 68, 16, buf);
|
||||
}
|
||||
|
||||
/*
|
||||
static void draw_idle_emote(SceneState* state, Canvas* canvas){
|
||||
if(state->action_timeout % 50 < 40 && state->prev_action == MINDCONTROL){
|
||||
elements_multiline_text_framed(canvas, 68, 16, "WUT?!");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void dolphin_scene_render_dolphin(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
if(state->scene_zoom == SCENE_ZOOM) {
|
||||
state->dolphin_gfx = I_DolphinExcited_64x63;
|
||||
} else if(state->action == SLEEP && state->player_global.x == 154) { // 2do - sofa x pos getter
|
||||
state->dolphin_gfx = A_FX_Sitting_40x27;
|
||||
state->dolphin_gfx_b = I_FX_SittingB_40x27;
|
||||
} else if(state->action != INTERACT) {
|
||||
if(state->player_v.x < 0 || state->player_flipped) {
|
||||
if(state->player_anim == 0) {
|
||||
state->dolphin_gfx = I_WalkL1_32x32;
|
||||
state->dolphin_gfx_b = I_WalkLB1_32x32;
|
||||
|
||||
} else {
|
||||
state->dolphin_gfx = I_WalkL2_32x32;
|
||||
state->dolphin_gfx_b = I_WalkLB2_32x32;
|
||||
}
|
||||
} else if(state->player_v.x > 0 || !state->player_flipped) {
|
||||
if(state->player_anim == 0) {
|
||||
state->dolphin_gfx = I_WalkR1_32x32;
|
||||
state->dolphin_gfx_b = I_WalkRB1_32x32;
|
||||
|
||||
} else {
|
||||
state->dolphin_gfx = I_WalkR2_32x32;
|
||||
state->dolphin_gfx_b = I_WalkRB2_32x32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
canvas_set_bitmap_mode(canvas, true);
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_icon_name(canvas, state->player.x, state->player.y, state->dolphin_gfx_b);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_icon_name(canvas, state->player.x, state->player.y, state->dolphin_gfx);
|
||||
canvas_set_bitmap_mode(canvas, false);
|
||||
}
|
||||
|
||||
static bool item_screen_bounds(int32_t pos) {
|
||||
return pos > -SCREEN_WIDTH && pos < (SCREEN_WIDTH * 2);
|
||||
}
|
||||
|
||||
void dolphin_scene_render(SceneState* state, Canvas* canvas, uint32_t t) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
const Item** current_scene = get_scene(state);
|
||||
|
||||
for(uint8_t l = 0; l < LAYERS; l++) {
|
||||
if(state->scene_zoom < SCENE_ZOOM) {
|
||||
for(uint8_t i = 0; i < ITEMS_NUM; i++) {
|
||||
int32_t item_pos = (current_scene[i]->x - state->player_global.x);
|
||||
if(item_screen_bounds(item_pos)) {
|
||||
if(current_scene[i]->draw) current_scene[i]->draw(canvas, state);
|
||||
|
||||
if(l == current_scene[i]->layer) {
|
||||
canvas_draw_icon_name(
|
||||
canvas,
|
||||
item_pos * PARALLAX(l),
|
||||
current_scene[i]->y,
|
||||
current_scene[i]->icon);
|
||||
canvas_set_bitmap_mode(canvas, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(l == 0) canvas_draw_line(canvas, 0, 42, 128, 42);
|
||||
}
|
||||
|
||||
if(l == DOLPHIN_LAYER) dolphin_scene_render_dolphin(state, canvas);
|
||||
}
|
||||
}
|
||||
|
||||
void dolphin_scene_render_state(SceneState* state, Canvas* canvas) {
|
||||
furi_assert(state);
|
||||
furi_assert(canvas);
|
||||
|
||||
char buf[64];
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
|
||||
// dolphin_scene_debug
|
||||
if(state->debug) {
|
||||
sprintf(
|
||||
buf,
|
||||
"x:%ld>%d %ld %s",
|
||||
state->player_global.x,
|
||||
state->poi,
|
||||
state->action_timeout,
|
||||
action_str[state->action]);
|
||||
canvas_draw_str(canvas, 0, 13, buf);
|
||||
}
|
||||
|
||||
if(state->scene_zoom == SCENE_ZOOM)
|
||||
scene_draw_dialog(state, canvas);
|
||||
else if(state->action == EMOTE)
|
||||
scene_draw_current_emote(state, canvas);
|
||||
else if(state->action == MINDCONTROL)
|
||||
scene_draw_hint(state, canvas, state->action_timeout > 45);
|
||||
else if(state->action == INTERACT)
|
||||
scene_activate_item_callback(state, canvas);
|
||||
else if(state->action == SLEEP)
|
||||
scene_draw_sleep_emote(state, canvas);
|
||||
/*
|
||||
else if(state->action == IDLE)
|
||||
draw_idle_emote(state, canvas);
|
||||
*/
|
||||
}
|
Reference in New Issue
Block a user