diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-10 15:06:01 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-03-10 15:32:46 +0100 |
commit | 09183eb08755b20804f26a4341ab30bcea89a697 (patch) | |
tree | 947cbb58e67ebda5053ec27250d1c53ea8341a88 /editor | |
parent | abc0ca438938d9ccad9097a671556988b7625696 (diff) |
editor: add placeholder character walking around
Diffstat (limited to 'editor')
-rw-r--r-- | editor/app.cpp | 2 | ||||
-rw-r--r-- | editor/app.hpp | 4 | ||||
-rw-r--r-- | editor/character.cpp | 51 | ||||
-rw-r--r-- | editor/character.hpp | 3 | ||||
-rw-r--r-- | editor/draw.cpp | 22 | ||||
-rw-r--r-- | editor/events.cpp | 4 | ||||
-rw-r--r-- | editor/keys.hpp | 1 | ||||
-rw-r--r-- | editor/update.cpp | 9 |
8 files changed, 89 insertions, 7 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index 5fb7ed79..80599ed1 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -6,6 +6,7 @@ #include "loader/loader.hpp" #include "world.hpp" #include "src/anim-atlas.hpp" +#include "character.hpp" #include <cerrno> #include <cstdlib> #include <cstring> @@ -32,6 +33,7 @@ app::app(fm_settings&& opts) : maybe_initialize_chunk_(coord, w[coord]); reset_camera_offset(); inspectors.reserve(16); + _character = std::make_unique<character_wip>(); } app::~app() diff --git a/editor/app.hpp b/editor/app.hpp index ada61331..56660845 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -23,6 +23,7 @@ struct tile_atlas; struct tile_editor; struct fm_settings; struct anim_atlas; +struct character_wip; struct cursor_state final { Optional<Vector2i> pixel; @@ -74,8 +75,10 @@ private: void set_cursor(); void maybe_initialize_chunk(const chunk_coords& pos, chunk& c) override; void maybe_initialize_chunk_(const chunk_coords& pos, chunk& c); + void update_character(float dt); void draw() override; + void draw_character(); void on_mouse_move(const mouse_move_event& event) noexcept override; void on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept override; @@ -147,6 +150,7 @@ private: key_set keys; std::array<int, key_set::COUNT> key_modifiers = {}; std::vector<popup_target> inspectors; + std::unique_ptr<character_wip> _character; cursor_state cursor; popup_target _popup_target; diff --git a/editor/character.cpp b/editor/character.cpp index d1f648c6..27692fbf 100644 --- a/editor/character.cpp +++ b/editor/character.cpp @@ -13,9 +13,43 @@ template <typename T> constexpr T sgn(T val) { return T(T(0) < val) - T(val < T(0)); } constexpr int tile_size_1 = iTILE_SIZE2.sum()/2, - framerate = 24, move_speed = tile_size_1 * 2/3; + framerate = 96, move_speed = tile_size_1 * 2; constexpr float frame_time = 1.f/framerate; constexpr auto inv_tile_size = 1 / TILE_SIZE2; +constexpr Vector2b bbox_size(12); + +constexpr auto arrows_to_dir(bool L, bool R, bool U, bool D) +{ + if (L == R) + L = R = false; + if (U == D) + U = D = false; + + using enum rotation; + struct { + int lr = 0, ud = 0; + rotation r = N; + } dir; + + if (L && U) + dir = { -1, 0, W }; + else if (L && D) + dir = { 0, 1, S }; + else if (R && U) + dir = { 0, -1, N }; + else if (R && D) + dir = { 1, 0, E }; + else if (L) + dir = { -1, 1, SW }; + else if (D) + dir = { 1, 1, SE }; + else if (R) + dir = { 1, -1, NE }; + else if (U) + dir = { -1, -1, NW }; + + return dir; +} } // namespace @@ -41,16 +75,23 @@ Vector2 character_wip::move_vec(int left_right, int top_bottom) return c * Vector2(sgn(left_right), sgn(top_bottom)).normalized(); } -void character_wip::tick(world& w, float dt, int left_right, int top_bottom) +void character_wip::tick(world& w, float dt, bool L, bool R, bool U, bool D) { - if (!left_right && !top_bottom) + auto [lr, ud, rot] = arrows_to_dir(L, R, U, D); + + if (!lr & !ud) { delta = 0; return; } int nframes = allocate_frame_time(dt); - const auto vec = move_vec(left_right, top_bottom); + + if (!nframes) + return; + + const auto vec = move_vec(lr, ud); + r = rot; for (int i = 0; i < nframes; i++) { @@ -62,7 +103,7 @@ void character_wip::tick(world& w, float dt, int left_right, int top_bottom) offset_ = Vector2(std::fmod(offset_[0], TILE_SIZE2[0]), std::fmod(offset_[1], TILE_SIZE2[1])); auto [c, t] = w[pos_]; const auto& r = c.rtree(); - auto center = Vector2(pos_.local()) * TILE_SIZE2 + offset; + auto center = Vector2(pos_.local()) * TILE_SIZE2 + offset_; auto half = Vector2(bbox_size)*.5f; auto min = center - half; auto max = center + half; diff --git a/editor/character.hpp b/editor/character.hpp index b201be8f..86f634c5 100644 --- a/editor/character.hpp +++ b/editor/character.hpp @@ -12,14 +12,13 @@ struct character_wip final { character_wip(); ~character_wip(); - void tick(world& w, float dt, int left_right, int top_bottom); + void tick(world& w, float dt, bool L, bool R, bool U, bool D); std::shared_ptr<anim_atlas> walk_anim; global_coords pos; std::size_t frame = 0; float delta = 0; Vector2 offset; - Vector2ub bbox_size; rotation r = rotation::NE; private: diff --git a/editor/draw.cpp b/editor/draw.cpp index ca926152..7ce5c7bf 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -7,8 +7,10 @@ #include "draw/anim.hpp" #include "src/camera-offset.hpp" #include "src/world.hpp" +#include "character.hpp" #include <Magnum/Math/Color.h> #include <Magnum/Math/Vector3.h> +#include <Magnum/GL/Renderer.h> #include "src/RTree.hpp" namespace floormat { @@ -140,8 +142,28 @@ void app::draw_collision_boxes() shader.set_tint({1, 1, 1, 1}); } +void app::draw_character() +{ + GL::Renderer::enable(GL::Renderer::Feature::DepthTest); + GL::Renderer::setDepthMask(false); + + auto& shader = M->shader(); + auto& mesh = M->meshes().anim; + const auto sz = M->window_size(); + auto& c = *_character; + const auto [minx, maxx, miny, maxy] = M->get_draw_bounds(); + + const with_shifted_camera_offset o{shader, c.pos.chunk(), {minx, miny}, {maxx, maxy}}; + if (floormat_main::check_chunk_visible(shader.camera_offset(), sz)) + mesh.draw(shader, *c.walk_anim, c.r, c.frame, c.pos.local(), Vector2b(c.offset), tile_shader::character_depth_offset); + + GL::Renderer::setDepthMask(true); + GL::Renderer::disable(GL::Renderer::Feature::DepthTest); +} + void app::draw() { + draw_character(); if (_render_bboxes) draw_collision_boxes(); if (_editor.current_tile_editor() || _editor.current_scenery_editor()) diff --git a/editor/events.cpp b/editor/events.cpp index cd3e4b9a..2fb8b567 100644 --- a/editor/events.cpp +++ b/editor/events.cpp @@ -139,6 +139,10 @@ auto app::resolve_keybinding(int k_, int mods_) const -> std::tuple<key, int> case SDLK_q | CTRL: return { key_quit, mods }; case SDLK_n | CTRL: return { key_new_file, mods }; case SDLK_ESCAPE: return { key_escape, mods }; + case SDLK_LEFT: return { key_left, mods }; + case SDLK_RIGHT: return { key_right, mods }; + case SDLK_UP: return { key_up, mods }; + case SDLK_DOWN: return { key_down, mods }; } } } diff --git a/editor/keys.hpp b/editor/keys.hpp index 982e3c4a..902b6c95 100644 --- a/editor/keys.hpp +++ b/editor/keys.hpp @@ -14,6 +14,7 @@ enum kmod : int { enum key : unsigned { key_noop, key_camera_up, key_camera_left, key_camera_right, key_camera_down, key_camera_reset, + key_left, key_right, key_up, key_down, key_NO_REPEAT, key_rotate_tile, key_mode_none, key_mode_floor, key_mode_walls, key_mode_scenery, diff --git a/editor/update.cpp b/editor/update.cpp index 16a155f3..e9f178a1 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -5,6 +5,7 @@ #include "main/clickable.hpp" #include "floormat/events.hpp" #include "floormat/main.hpp" +#include "character.hpp" #include "chunk.inl" namespace floormat { @@ -208,6 +209,13 @@ void app::update_world(float dt) c.with_scenery_update(sc.index(), [&] { return sc.update(dt); }); } + + +void app::update_character(float dt) +{ + _character->tick(M->world(), dt, keys[key_left], keys[key_right], keys[key_up], keys[key_down]); +} + void app::set_cursor() { if (!cursor.in_imgui) @@ -234,6 +242,7 @@ void app::update(float dt) update_world(dt); do_camera(dt, keys, get_key_modifiers()); clear_non_repeated_keys(); + update_character(dt); set_cursor(); M->world().maybe_collect(); |