From be38080bd6be2619e08e2d35ed8817026514b021 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Thu, 17 Jul 2025 00:40:13 -0500 Subject: [PATCH] ballistics: implement hud and projectile type switching --- src/demo/ballistics.cpp | 61 ++++++++++++++++++++++++++++++++++++--- src/demo/ballistics.hpp | 7 +++-- src/demo/scene.hpp | 5 +++- src/platform/graphics.cpp | 51 +++++++++++++++++--------------- 4 files changed, 93 insertions(+), 31 deletions(-) diff --git a/src/demo/ballistics.cpp b/src/demo/ballistics.cpp index 2fb5f1d..e452339 100644 --- a/src/demo/ballistics.cpp +++ b/src/demo/ballistics.cpp @@ -2,6 +2,7 @@ #include "math/transform.hpp" #include "platform/graphics_primitive.hpp" +#include "platform/font.hpp" #include "physics/particle.hpp" @@ -24,7 +25,7 @@ namespace demo { } } - void ballistics::fire() + void ballistics::a() { projectile * projectile; @@ -55,7 +56,7 @@ namespace demo { projectile->particle.inverse_mass = 1.0f / 10.0f; projectile->particle.velocity = vec3(0.0f, 0.0f, 10.0f); projectile->particle.acceleration = vec3(0.0f, 0.6f, 0.0f); - projectile->particle.damping = 0.9f; + projectile->particle.damping = 0.99f; break; case projectile_type::LASER: projectile->particle.inverse_mass = 1.0f / 0.1f; @@ -68,15 +69,27 @@ namespace demo { } projectile->particle.position = vec3(0.0f, 1.5f, 0.0f); - //projectile->start_time = FIXME; + projectile->start_time = tick; projectile->type = current_projectile_type; //projectile->particle.clear_accumulator(); FIXME projectile->particle.force_accum = vec3(0, 0, 0); } + void ballistics::x() + { + switch (current_projectile_type) { + default: [[fallthrough]]; + case projectile_type::PISTOL: current_projectile_type = projectile_type::ARTILLERY; break; + case projectile_type::ARTILLERY: current_projectile_type = projectile_type::FIREBALL; break; + case projectile_type::FIREBALL: current_projectile_type = projectile_type::LASER; break; + case projectile_type::LASER: current_projectile_type = projectile_type::PISTOL; break; + } + } + void ballistics::update() { float duration = 1.0f / 60.0f; + tick += 1; for (int i = 0; i < max_projectiles; i++) { projectile * projectile = &projectiles[i]; @@ -86,14 +99,51 @@ namespace demo { projectile->particle.integrate(duration); if (projectile->particle.position.z > 200.0f || - projectile->particle.position.y < 0.0f) { + projectile->particle.position.y < 0.0f || + projectile->start_time < tick - 600) { projectile->type = projectile_type::NONE; } } } + void ballistics::draw_hud(ta_parameter_writer& writer) + { + const int title_length = 16; + const int title_width_2 = (font::ter_u12n.hori_advance * title_length) >> 1; + const int framebuffer_width_2 = framebuffer::framebuffer.px_width >> 1; + const int x = framebuffer_width_2 - title_width_2; + vec3 center_p = vec3(x, 5, 10); + + font::ter_u12n.global(writer); + font::ter_u12n.draw_string(writer, center_p, "demo: ballistics", 0xffffffff); + + const char * label; + switch (current_projectile_type) { + default: [[fallthrough]]; + case projectile_type::PISTOL: label = "pistol"; break; + case projectile_type::ARTILLERY: label = "artillery"; break; + case projectile_type::FIREBALL: label = "fireball"; break; + case projectile_type::LASER: label = "laser"; break; + } + + vec3 p(5, 10, 10); + font::ter_u12n.draw_string(writer, p, "projectile_type:", 0xffffffff); + p.x += font::ter_u12n.hori_advance * 17; + font::ter_u12n.draw_string(writer, p, label, 0xffffffff); + } + void ballistics::draw(ta_parameter_writer& writer, const mat4x4& trans) { + // punch through + draw_hud(writer); + + writer.append() = + ta_global_parameter::end_of_list(para_control::para_type::end_of_list); + + // opaque + + draw_axis(writer, trans * scale(5.f)); + draw_grid(writer, trans * translate(vec3(0, 0, 50)) * scale(vec3(20, 10, 100))); for (int i = 0; i < max_projectiles; i++) { @@ -103,5 +153,8 @@ namespace demo { projectile->draw(writer, trans); } + + writer.append() = + ta_global_parameter::end_of_list(para_control::para_type::end_of_list); } } diff --git a/src/demo/ballistics.hpp b/src/demo/ballistics.hpp index 6c52ed8..b10d2f5 100644 --- a/src/demo/ballistics.hpp +++ b/src/demo/ballistics.hpp @@ -19,7 +19,7 @@ namespace demo { { physics::particle particle; projectile_type type; - uint32_t start_time; + int32_t start_time; void draw(ta_parameter_writer& writer, const mat4x4& trans); }; @@ -27,10 +27,13 @@ namespace demo { constexpr static int max_projectiles = 32; projectile projectiles[max_projectiles]; projectile_type current_projectile_type; + int32_t tick; void init() override; - void fire() override; + void a() override; + void x() override; void update() override; void draw(ta_parameter_writer& writer, const mat4x4& trans) override; + void draw_hud(ta_parameter_writer& writer); }; }; diff --git a/src/demo/scene.hpp b/src/demo/scene.hpp index aa82b0c..48ec1c9 100644 --- a/src/demo/scene.hpp +++ b/src/demo/scene.hpp @@ -7,7 +7,10 @@ namespace demo { struct scene { virtual void init() = 0; - virtual void fire() = 0; + virtual void a() {} + virtual void b() {} + virtual void x() {} + virtual void y() {} virtual void update() = 0; virtual void draw(ta_parameter_writer& writer, const mat4x4& trans) = 0; }; diff --git a/src/platform/graphics.cpp b/src/platform/graphics.cpp index 10a7eb5..5763bf0 100644 --- a/src/platform/graphics.cpp +++ b/src/platform/graphics.cpp @@ -91,22 +91,12 @@ namespace graphics { } } - demo::ballistics b; + demo::ballistics _ballistics; + demo::scene * current_scene = &_ballistics; void draw() { - font::ter_u12n.global(writer); - //font::ter_u12n.draw_mat4(writer, vec3(10, 22, 10), view_trans, 0xffffffff, 12); - - writer.append() = - ta_global_parameter::end_of_list(para_control::para_type::end_of_list); - - draw_axis(writer, view_trans * scale(0.5f)); - - b.draw(writer, view_trans * scale(0.05f)); - - writer.append() = - ta_global_parameter::end_of_list(para_control::para_type::end_of_list); + current_scene->draw(writer, view_trans * scale(0.05f)); } void init() @@ -114,7 +104,7 @@ namespace graphics { core_init(); framebuffer::scaler_init(); - b.init(); + current_scene->init(); // read while (spg_status::vsync(holly.SPG_STATUS)); @@ -167,9 +157,15 @@ namespace graphics { view_trans = view_trans * translate(vec3(0, 0, -0.9)); } - void view_transform() + void input_events() { - static int last_a[4] = {}; + struct button_state { + uint8_t a; + uint8_t b; + uint8_t x; + uint8_t y; + }; + static button_state last[4] = {}; for (int port_ix = 0; port_ix < 4; port_ix++) { auto& port = input::state.port[port_ix]; @@ -181,14 +177,21 @@ namespace graphics { auto& data = data_fields.data; - float dx = static_cast(data.analog_coordinate_axis[2] - 0x80) * 0.001; - float dy = static_cast(data.analog_coordinate_axis[3] - 0x80) * 0.001; + //float dx = static_cast(data.analog_coordinate_axis[2] - 0x80) * 0.001; + //float dy = static_cast(data.analog_coordinate_axis[3] - 0x80) * 0.001; bool a = ft0::data_transfer::digital_button::a(data.digital_button) == 0; - if (last_a[port_ix] == 0 && a) { - b.fire(); - } - last_a[port_ix] = a; + bool b = ft0::data_transfer::digital_button::b(data.digital_button) == 0; + bool x = ft0::data_transfer::digital_button::x(data.digital_button) == 0; + bool y = ft0::data_transfer::digital_button::y(data.digital_button) == 0; + if (last[port_ix].a == 0 && a) current_scene->a(); + if (last[port_ix].b == 0 && b) current_scene->b(); + if (last[port_ix].x == 0 && x) current_scene->x(); + if (last[port_ix].y == 0 && y) current_scene->y(); + last[port_ix].a = a; + last[port_ix].b = b; + last[port_ix].x = x; + last[port_ix].y = y; //view_trans = view_trans * rotate_x(dx) * rotate_y(dy); } @@ -197,8 +200,8 @@ namespace graphics { void step() { - view_transform(); - b.update(); + input_events(); + current_scene->update(); writer.offset = 0; draw();