From 564b5f9f3b62273b117355f046febf3fb31f412d Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Wed, 15 Feb 2023 15:48:34 +0800 Subject: [PATCH 01/13] commit --- src/battle_game/core/selectable_units.cpp | 1 + src/battle_game/core/units/joker_tank.cpp | 165 ++++++++++++++++++++++ src/battle_game/core/units/joker_tank.h | 23 +++ src/battle_game/core/units/units.h | 1 + 4 files changed, 190 insertions(+) create mode 100644 src/battle_game/core/units/joker_tank.cpp create mode 100644 src/battle_game/core/units/joker_tank.h diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index fa7b1d01..1da04984 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -31,6 +31,7 @@ void GameCore::GeneratePrimaryUnitList() { * */ ADD_SELECTABLE_UNIT(unit::InfernoTank); ADD_SELECTABLE_UNIT(unit::Tank); + ADD_SELECTABLE_UNIT(unit::JokerTank); ADD_SELECTABLE_UNIT(unit::DoubleScatterTank); ADD_SELECTABLE_UNIT(unit::ThreeBodyMan); ADD_SELECTABLE_UNIT(unit::LMTank); diff --git a/src/battle_game/core/units/joker_tank.cpp b/src/battle_game/core/units/joker_tank.cpp new file mode 100644 index 00000000..98b3ad5c --- /dev/null +++ b/src/battle_game/core/units/joker_tank.cpp @@ -0,0 +1,165 @@ +#include "battle_game/core/bullets/bullets.h" +#include "battle_game/core/game_core.h" +#include "battle_game/graphics/graphics.h" +#include "joker_tank.h" + +namespace battle_game::unit { + +namespace { +uint32_t tank_body_model_index = 0xffffffffu; +uint32_t tank_turret_model_index = 0xffffffffu; +} // namespace + +JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) + : Unit(game_core, id, player_id) { + if (!~tank_body_model_index) { + auto mgr = AssetsManager::GetInstance(); + { + /* JokerTank Body */ + tank_body_model_index = mgr->RegisterModel( + { + {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + // distinguish front and back + {{0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + }, + {0, 1, 2, 1, 2, 3, 0, 2, 5, 2, 4, 5}); + } + + { + /* JokerTank Turret */ + std::vector turret_vertices; + std::vector turret_indices; + const int precision = 60; + const float inv_precision = 1.0f / float(precision); + for (int i = 0; i < precision; i++) { + auto theta = (float(i) + 0.5f) * inv_precision; + theta *= glm::pi() * 2.0f; + auto sin_theta = std::sin(theta); + auto cos_theta = std::cos(theta); + turret_vertices.push_back({{sin_theta * 0.5f, cos_theta * 0.5f}, + {0.0f, 0.0f}, + {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(i); + turret_indices.push_back((i + 1) % precision); + turret_indices.push_back(precision); + } + turret_vertices.push_back( + {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(precision + 1 + 0); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 3); + tank_turret_model_index = + mgr->RegisterModel(turret_vertices, turret_indices); + } + } +} + +void JokerTank::Render() { + battle_game::SetTransformation(position_, rotation_); + battle_game::SetTexture(0); + battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); + battle_game::DrawModel(tank_body_model_index); + battle_game::SetRotation(turret_rotation_); + battle_game::DrawModel(tank_turret_model_index); +} + +void JokerTank::Update() { + TankMove(3.0f, glm::radians(180.0f)); + TurretRotate(); + Fire(); +} + +void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + glm::vec2 offset{0.0f}; + if (input_data.key_down[GLFW_KEY_W]) { + offset.y += 1.0f; + } + if (input_data.key_down[GLFW_KEY_S]) { + offset.y -= 1.0f; + } + float speed = move_speed * GetSpeedScale(); + offset *= kSecondPerTick * speed; + auto new_position = + position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, + glm::vec3{0.0f, 0.0f, 1.0f}) * + glm::vec4{offset, 0.0f, 0.0f}}; + if (!game_core_->IsBlockedByObstacles(new_position)) { + game_core_->PushEventMoveUnit(id_, new_position); + } + float rotation_offset = 0.0f; + if (input_data.key_down[GLFW_KEY_A]) { + rotation_offset += 1.0f; + } + if (input_data.key_down[GLFW_KEY_D]) { + rotation_offset -= 1.0f; + } + rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); + game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); + } +} + +void JokerTank::TurretRotate() { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + auto diff = input_data.mouse_cursor_position - position_; + if (glm::length(diff) < 1e-4) { + turret_rotation_ = rotation_; + } else { + turret_rotation_ = std::atan2(diff.y, diff.x) - glm::radians(90.0f); + } + } +} + +void JokerTank::Fire() { + if (fire_count_down_ == 0) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { + auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); + GenerateBullet( + position_ + Rotate({0.0f, 1.2f}, turret_rotation_), + turret_rotation_, GetDamageScale(), velocity); + fire_count_down_ = kTickPerSecond; // Fire interval 1 second. + } + } + } + if (fire_count_down_) { + fire_count_down_--; + } +} + +bool JokerTank::IsHit(glm::vec2 position) const { + position = WorldToLocal(position); + return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && + position.y < 1.0f && position.x + position.y < 1.6f && + position.y - position.x < 1.6f; +} + +const char *JokerTank::UnitName() const { + return "Joker Tank"; +} + +const char *JokerTank::Author() const { + return "Joker zhs"; +} +} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/joker_tank.h b/src/battle_game/core/units/joker_tank.h new file mode 100644 index 00000000..1b0f86a3 --- /dev/null +++ b/src/battle_game/core/units/joker_tank.h @@ -0,0 +1,23 @@ +#pragma once +#include "battle_game/core/unit.h" + +namespace battle_game::unit { +class JokerTank : public Unit { + public: + JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id); + void Render() override; + void Update() override; + [[nodiscard]] bool IsHit(glm::vec2 position) const override; + + protected: + void TankMove(float move_speed, float rotate_angular_speed); + void TurretRotate(); + void Fire(); + [[nodiscard]] const char *UnitName() const override; + [[nodiscard]] const char *Author() const override; + + float turret_rotation_{0.0f}; + uint32_t fire_count_down_{0}; + uint32_t mine_count_down_{0}; +}; +} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index ebaa91e6..5ec14031 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -5,6 +5,7 @@ #include "battle_game/core/units/dark_fury.h" #include "battle_game/core/units/double_scatter_tank.h" #include "battle_game/core/units/inferno_tank.h" +#include "battle_game/core/units/joker_tank.h" #include "battle_game/core/units/lm_tank.h" #include "battle_game/core/units/mine_sample_tank.h" #include "battle_game/core/units/missile_tank.h" From c67088be37d8b4061b62e7cbd225db446aa06fb2 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sat, 18 Feb 2023 23:37:23 +0800 Subject: [PATCH 02/13] --- src/battle_game/core/CMakeLists.txt | 2 +- src/battle_game/core/bullets/bullets.h | 1 + src/battle_game/core/bullets/joker.cpp | 57 +++++++++++++++++++++++ src/battle_game/core/bullets/joker.h | 22 +++++++++ src/battle_game/core/units/joker_tank.cpp | 2 +- 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/battle_game/core/bullets/joker.cpp create mode 100644 src/battle_game/core/bullets/joker.h diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index 31e58c94..0f108ede 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,7 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files}) +target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp") target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/bullets.h b/src/battle_game/core/bullets/bullets.h index d1447761..d652fa4d 100644 --- a/src/battle_game/core/bullets/bullets.h +++ b/src/battle_game/core/bullets/bullets.h @@ -13,3 +13,4 @@ #include "battle_game/core/bullets/udongein_directional_bullet.h" #include "battle_game/core/bullets/warning_line.h" #include "battle_game/core/bullets/water_drop.h" +#include "battle_game/core/bullets/joker.h" diff --git a/src/battle_game/core/bullets/joker.cpp b/src/battle_game/core/bullets/joker.cpp new file mode 100644 index 00000000..49cacac8 --- /dev/null +++ b/src/battle_game/core/bullets/joker.cpp @@ -0,0 +1,57 @@ +#include "battle_game/core/bullets/cannon_ball.h" +#include "battle_game/core/game_core.h" +#include "battle_game/core/particles/particles.h" + +namespace battle_game::bullet { +Joker::Joker(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity) + : Bullet(core, id, unit_id, player_id, position, rotation, damage_scale), + velocity_(velocity) { +} + +void Joker::Render() { + SetTransformation(position_, rotation_, glm::vec2{0.1f}); + SetColor(game_core_->GetPlayerColor(player_id_)); + SetTexture("../../textures/particle3.png"); + DrawModel(0); +} + +void Joker::Update() { + float x = game_core_->RandomFloat(); + position_ += velocity_ * kSecondPerTick * x * 2.0f; + bool should_die = false; + if (game_core_->IsBlockedByObstacles(position_)) { + should_die = true; + } + + auto &units = game_core_->GetUnits(); + for (auto &unit : units) { + if (unit.first == unit_id_) { + continue; + } + if (unit.second->IsHit(position_)) { + game_core_->PushEventDealDamage( + unit.first, id_, damage_scale_ * (game_core_->RandomFloat()) * 20.0f); + should_die = true; + } + } + + if (should_die) { + game_core_->PushEventRemoveBullet(id_); + } +} + +Joker::~Joker() { + for (int i = 0; i < 5; i++) { + game_core_->PushEventGenerateParticle( + position_, rotation_, game_core_->RandomInCircle() * 2.0f, 0.2f, + glm::vec4{0.0f, 0.0f, 0.0f, 1.0f}, 3.0f); + } +} +} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/bullets/joker.h b/src/battle_game/core/bullets/joker.h new file mode 100644 index 00000000..c77804c0 --- /dev/null +++ b/src/battle_game/core/bullets/joker.h @@ -0,0 +1,22 @@ +#pragma once +#include "battle_game/core/bullet.h" + +namespace battle_game::bullet { +class Joker : public Bullet { + public: + Joker(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity); + ~Joker() override; + void Render() override; + void Update() override; + + private: + glm::vec2 velocity_{}; +}; +} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/units/joker_tank.cpp b/src/battle_game/core/units/joker_tank.cpp index 98b3ad5c..3ab8b2b8 100644 --- a/src/battle_game/core/units/joker_tank.cpp +++ b/src/battle_game/core/units/joker_tank.cpp @@ -136,7 +136,7 @@ void JokerTank::Fire() { auto &input_data = player->GetInputData(); if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); - GenerateBullet( + GenerateBullet( position_ + Rotate({0.0f, 1.2f}, turret_rotation_), turret_rotation_, GetDamageScale(), velocity); fire_count_down_ = kTickPerSecond; // Fire interval 1 second. From 574d9a42498cb2ac82ad94254f6b990b4cb8b59c Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 16:17:01 +0800 Subject: [PATCH 03/13] --- src/battle_game/core/selectable_units.cpp | 2 ++ src/battle_game/core/units/joker_tank.cpp | 14 +++++++++++++- src/battle_game/core/units/joker_tank.h | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index 1da04984..6aba7600 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -52,6 +52,8 @@ void GameCore::GeneratePrimaryUnitList() { ADD_SELECTABLE_UNIT(unit::CritTank); ADD_SELECTABLE_UNIT(unit::Railgun); ADD_SELECTABLE_UNIT(unit::Udongein); + ADD_SELECTABLE_UNIT(unit::LMTank); + unit.reset(); } diff --git a/src/battle_game/core/units/joker_tank.cpp b/src/battle_game/core/units/joker_tank.cpp index 3ab8b2b8..36780cfd 100644 --- a/src/battle_game/core/units/joker_tank.cpp +++ b/src/battle_game/core/units/joker_tank.cpp @@ -67,8 +67,14 @@ JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } + Skill skill; + skill.name = "HP->ATK"; + skill.description = "The lower HP, the higher damage"; + skill.type = P; + skills_.push_back(skill); } + void JokerTank::Render() { battle_game::SetTransformation(position_, rotation_); battle_game::SetTexture(0); @@ -82,6 +88,7 @@ void JokerTank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); + Passive(); } void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { @@ -129,6 +136,11 @@ void JokerTank::TurretRotate() { } } + float JokerTank::GetDamageScale() const { + + return 3.0 - 2 * GetHealthScale(); + } + void JokerTank::Fire() { if (fire_count_down_ == 0) { auto player = game_core_->GetPlayer(player_id_); @@ -136,7 +148,7 @@ void JokerTank::Fire() { auto &input_data = player->GetInputData(); if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); - GenerateBullet( + GenerateBullet( position_ + Rotate({0.0f, 1.2f}, turret_rotation_), turret_rotation_, GetDamageScale(), velocity); fire_count_down_ = kTickPerSecond; // Fire interval 1 second. diff --git a/src/battle_game/core/units/joker_tank.h b/src/battle_game/core/units/joker_tank.h index 1b0f86a3..b6de9d13 100644 --- a/src/battle_game/core/units/joker_tank.h +++ b/src/battle_game/core/units/joker_tank.h @@ -13,11 +13,16 @@ class JokerTank : public Unit { void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); + void Passive(); [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; + [[nodiscard]] float GetDamageScale() const override; + float turret_rotation_{0.0f}; uint32_t fire_count_down_{0}; uint32_t mine_count_down_{0}; + + }; } // namespace battle_game::unit \ No newline at end of file From b5f79e961dfa9c9d35df9d674fc65501d4115a37 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 17:07:57 +0800 Subject: [PATCH 04/13] --- src/battle_game/core/CMakeLists.txt | 2 +- src/battle_game/core/bullets/bullets.h | 2 +- src/battle_game/core/bullets/joker.cpp | 57 ------------------- src/battle_game/core/bullets/joker.h | 22 ------- src/battle_game/core/selectable_units.cpp | 2 +- .../units/{joker_tank.cpp => random_tank.cpp} | 51 ++++++++--------- .../units/{joker_tank.h => random_tank.h} | 8 +-- src/battle_game/core/units/units.h | 2 +- 8 files changed, 32 insertions(+), 114 deletions(-) delete mode 100644 src/battle_game/core/bullets/joker.cpp delete mode 100644 src/battle_game/core/bullets/joker.h rename src/battle_game/core/units/{joker_tank.cpp => random_tank.cpp} (83%) rename src/battle_game/core/units/{joker_tank.h => random_tank.h} (76%) diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index 0f108ede..4f7e9b2a 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,7 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp") +target_sources(${lib_name} PRIVATE ${source_files} ) target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/bullets.h b/src/battle_game/core/bullets/bullets.h index d652fa4d..775ab357 100644 --- a/src/battle_game/core/bullets/bullets.h +++ b/src/battle_game/core/bullets/bullets.h @@ -13,4 +13,4 @@ #include "battle_game/core/bullets/udongein_directional_bullet.h" #include "battle_game/core/bullets/warning_line.h" #include "battle_game/core/bullets/water_drop.h" -#include "battle_game/core/bullets/joker.h" + diff --git a/src/battle_game/core/bullets/joker.cpp b/src/battle_game/core/bullets/joker.cpp deleted file mode 100644 index 49cacac8..00000000 --- a/src/battle_game/core/bullets/joker.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "battle_game/core/bullets/cannon_ball.h" -#include "battle_game/core/game_core.h" -#include "battle_game/core/particles/particles.h" - -namespace battle_game::bullet { -Joker::Joker(GameCore *core, - uint32_t id, - uint32_t unit_id, - uint32_t player_id, - glm::vec2 position, - float rotation, - float damage_scale, - glm::vec2 velocity) - : Bullet(core, id, unit_id, player_id, position, rotation, damage_scale), - velocity_(velocity) { -} - -void Joker::Render() { - SetTransformation(position_, rotation_, glm::vec2{0.1f}); - SetColor(game_core_->GetPlayerColor(player_id_)); - SetTexture("../../textures/particle3.png"); - DrawModel(0); -} - -void Joker::Update() { - float x = game_core_->RandomFloat(); - position_ += velocity_ * kSecondPerTick * x * 2.0f; - bool should_die = false; - if (game_core_->IsBlockedByObstacles(position_)) { - should_die = true; - } - - auto &units = game_core_->GetUnits(); - for (auto &unit : units) { - if (unit.first == unit_id_) { - continue; - } - if (unit.second->IsHit(position_)) { - game_core_->PushEventDealDamage( - unit.first, id_, damage_scale_ * (game_core_->RandomFloat()) * 20.0f); - should_die = true; - } - } - - if (should_die) { - game_core_->PushEventRemoveBullet(id_); - } -} - -Joker::~Joker() { - for (int i = 0; i < 5; i++) { - game_core_->PushEventGenerateParticle( - position_, rotation_, game_core_->RandomInCircle() * 2.0f, 0.2f, - glm::vec4{0.0f, 0.0f, 0.0f, 1.0f}, 3.0f); - } -} -} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/bullets/joker.h b/src/battle_game/core/bullets/joker.h deleted file mode 100644 index c77804c0..00000000 --- a/src/battle_game/core/bullets/joker.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once -#include "battle_game/core/bullet.h" - -namespace battle_game::bullet { -class Joker : public Bullet { - public: - Joker(GameCore *core, - uint32_t id, - uint32_t unit_id, - uint32_t player_id, - glm::vec2 position, - float rotation, - float damage_scale, - glm::vec2 velocity); - ~Joker() override; - void Render() override; - void Update() override; - - private: - glm::vec2 velocity_{}; -}; -} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index 6aba7600..c9bbc80d 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -31,7 +31,7 @@ void GameCore::GeneratePrimaryUnitList() { * */ ADD_SELECTABLE_UNIT(unit::InfernoTank); ADD_SELECTABLE_UNIT(unit::Tank); - ADD_SELECTABLE_UNIT(unit::JokerTank); + ADD_SELECTABLE_UNIT(unit::RandomTank); ADD_SELECTABLE_UNIT(unit::DoubleScatterTank); ADD_SELECTABLE_UNIT(unit::ThreeBodyMan); ADD_SELECTABLE_UNIT(unit::LMTank); diff --git a/src/battle_game/core/units/joker_tank.cpp b/src/battle_game/core/units/random_tank.cpp similarity index 83% rename from src/battle_game/core/units/joker_tank.cpp rename to src/battle_game/core/units/random_tank.cpp index 36780cfd..eb97be91 100644 --- a/src/battle_game/core/units/joker_tank.cpp +++ b/src/battle_game/core/units/random_tank.cpp @@ -1,7 +1,7 @@ #include "battle_game/core/bullets/bullets.h" #include "battle_game/core/game_core.h" #include "battle_game/graphics/graphics.h" -#include "joker_tank.h" +#include "random_tank.h" namespace battle_game::unit { @@ -10,12 +10,12 @@ uint32_t tank_body_model_index = 0xffffffffu; uint32_t tank_turret_model_index = 0xffffffffu; } // namespace -JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) +RandomTank::RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id) : Unit(game_core, id, player_id) { if (!~tank_body_model_index) { auto mgr = AssetsManager::GetInstance(); { - /* JokerTank Body */ + /* RandomTank Body */ tank_body_model_index = mgr->RegisterModel( { {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, @@ -30,7 +30,7 @@ JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) } { - /* JokerTank Turret */ + /* RandomTank Turret */ std::vector turret_vertices; std::vector turret_indices; const int precision = 60; @@ -67,15 +67,11 @@ JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } - Skill skill; - skill.name = "HP->ATK"; - skill.description = "The lower HP, the higher damage"; - skill.type = P; - skills_.push_back(skill); + } -void JokerTank::Render() { +void RandomTank::Render() { battle_game::SetTransformation(position_, rotation_); battle_game::SetTexture(0); battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); @@ -84,14 +80,14 @@ void JokerTank::Render() { battle_game::DrawModel(tank_turret_model_index); } -void JokerTank::Update() { +void RandomTank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); - Passive(); + } -void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { +void RandomTank::TankMove(float move_speed, float rotate_angular_speed) { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -102,7 +98,8 @@ void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_S]) { offset.y -= 1.0f; } - float speed = move_speed * GetSpeedScale(); + float speed = + move_speed * GetSpeedScale() * 2 * game_core_->RandomFloat(); offset *= kSecondPerTick * speed; auto new_position = position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, @@ -118,12 +115,13 @@ void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_D]) { rotation_offset -= 1.0f; } - rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); + rotation_offset *= kSecondPerTick * rotate_angular_speed * + GetSpeedScale()* 2 * game_core_->RandomFloat(); game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); } } -void JokerTank::TurretRotate() { +void RandomTank::TurretRotate() { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -136,12 +134,9 @@ void JokerTank::TurretRotate() { } } - float JokerTank::GetDamageScale() const { - - return 3.0 - 2 * GetHealthScale(); - } + -void JokerTank::Fire() { +void RandomTank::Fire() { if (fire_count_down_ == 0) { auto player = game_core_->GetPlayer(player_id_); if (player) { @@ -150,8 +145,10 @@ void JokerTank::Fire() { auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); GenerateBullet( position_ + Rotate({0.0f, 1.2f}, turret_rotation_), - turret_rotation_, GetDamageScale(), velocity); - fire_count_down_ = kTickPerSecond; // Fire interval 1 second. + turret_rotation_, GetDamageScale() * 2.5 * game_core_->RandomFloat(), + velocity ); + fire_count_down_ = + kTickPerSecond * 1.5 * game_core_->RandomFloat(); // Fire interval 1 second. } } } @@ -160,18 +157,18 @@ void JokerTank::Fire() { } } -bool JokerTank::IsHit(glm::vec2 position) const { +bool RandomTank::IsHit(glm::vec2 position) const { position = WorldToLocal(position); return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && position.y < 1.0f && position.x + position.y < 1.6f && position.y - position.x < 1.6f; } -const char *JokerTank::UnitName() const { - return "Joker Tank"; +const char *RandomTank::UnitName() const { + return "Random Tank"; } -const char *JokerTank::Author() const { +const char *RandomTank::Author() const { return "Joker zhs"; } } // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/joker_tank.h b/src/battle_game/core/units/random_tank.h similarity index 76% rename from src/battle_game/core/units/joker_tank.h rename to src/battle_game/core/units/random_tank.h index b6de9d13..ba1c6938 100644 --- a/src/battle_game/core/units/joker_tank.h +++ b/src/battle_game/core/units/random_tank.h @@ -2,9 +2,9 @@ #include "battle_game/core/unit.h" namespace battle_game::unit { -class JokerTank : public Unit { +class RandomTank : public Unit { public: - JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id); + RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id); void Render() override; void Update() override; [[nodiscard]] bool IsHit(glm::vec2 position) const override; @@ -13,10 +13,10 @@ class JokerTank : public Unit { void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); - void Passive(); + [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; - [[nodiscard]] float GetDamageScale() const override; + float turret_rotation_{0.0f}; diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index 5ec14031..05b3711e 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -5,7 +5,7 @@ #include "battle_game/core/units/dark_fury.h" #include "battle_game/core/units/double_scatter_tank.h" #include "battle_game/core/units/inferno_tank.h" -#include "battle_game/core/units/joker_tank.h" +#include "battle_game/core/units/random_tank.h" #include "battle_game/core/units/lm_tank.h" #include "battle_game/core/units/mine_sample_tank.h" #include "battle_game/core/units/missile_tank.h" From d0baf0b327d006bcf3c8d18f4b2c8cec4b15bce8 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 17:08:57 +0800 Subject: [PATCH 05/13] Revert "" This reverts commit b5f79e961dfa9c9d35df9d674fc65501d4115a37. --- src/battle_game/core/CMakeLists.txt | 2 +- src/battle_game/core/bullets/bullets.h | 2 +- src/battle_game/core/bullets/joker.cpp | 57 +++++++++++++++++++ src/battle_game/core/bullets/joker.h | 22 +++++++ src/battle_game/core/selectable_units.cpp | 2 +- .../units/{random_tank.cpp => joker_tank.cpp} | 51 +++++++++-------- .../units/{random_tank.h => joker_tank.h} | 8 +-- src/battle_game/core/units/units.h | 2 +- 8 files changed, 114 insertions(+), 32 deletions(-) create mode 100644 src/battle_game/core/bullets/joker.cpp create mode 100644 src/battle_game/core/bullets/joker.h rename src/battle_game/core/units/{random_tank.cpp => joker_tank.cpp} (83%) rename src/battle_game/core/units/{random_tank.h => joker_tank.h} (76%) diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index 4f7e9b2a..0f108ede 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,7 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files} ) +target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp") target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/bullets.h b/src/battle_game/core/bullets/bullets.h index 775ab357..d652fa4d 100644 --- a/src/battle_game/core/bullets/bullets.h +++ b/src/battle_game/core/bullets/bullets.h @@ -13,4 +13,4 @@ #include "battle_game/core/bullets/udongein_directional_bullet.h" #include "battle_game/core/bullets/warning_line.h" #include "battle_game/core/bullets/water_drop.h" - +#include "battle_game/core/bullets/joker.h" diff --git a/src/battle_game/core/bullets/joker.cpp b/src/battle_game/core/bullets/joker.cpp new file mode 100644 index 00000000..49cacac8 --- /dev/null +++ b/src/battle_game/core/bullets/joker.cpp @@ -0,0 +1,57 @@ +#include "battle_game/core/bullets/cannon_ball.h" +#include "battle_game/core/game_core.h" +#include "battle_game/core/particles/particles.h" + +namespace battle_game::bullet { +Joker::Joker(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity) + : Bullet(core, id, unit_id, player_id, position, rotation, damage_scale), + velocity_(velocity) { +} + +void Joker::Render() { + SetTransformation(position_, rotation_, glm::vec2{0.1f}); + SetColor(game_core_->GetPlayerColor(player_id_)); + SetTexture("../../textures/particle3.png"); + DrawModel(0); +} + +void Joker::Update() { + float x = game_core_->RandomFloat(); + position_ += velocity_ * kSecondPerTick * x * 2.0f; + bool should_die = false; + if (game_core_->IsBlockedByObstacles(position_)) { + should_die = true; + } + + auto &units = game_core_->GetUnits(); + for (auto &unit : units) { + if (unit.first == unit_id_) { + continue; + } + if (unit.second->IsHit(position_)) { + game_core_->PushEventDealDamage( + unit.first, id_, damage_scale_ * (game_core_->RandomFloat()) * 20.0f); + should_die = true; + } + } + + if (should_die) { + game_core_->PushEventRemoveBullet(id_); + } +} + +Joker::~Joker() { + for (int i = 0; i < 5; i++) { + game_core_->PushEventGenerateParticle( + position_, rotation_, game_core_->RandomInCircle() * 2.0f, 0.2f, + glm::vec4{0.0f, 0.0f, 0.0f, 1.0f}, 3.0f); + } +} +} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/bullets/joker.h b/src/battle_game/core/bullets/joker.h new file mode 100644 index 00000000..c77804c0 --- /dev/null +++ b/src/battle_game/core/bullets/joker.h @@ -0,0 +1,22 @@ +#pragma once +#include "battle_game/core/bullet.h" + +namespace battle_game::bullet { +class Joker : public Bullet { + public: + Joker(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity); + ~Joker() override; + void Render() override; + void Update() override; + + private: + glm::vec2 velocity_{}; +}; +} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index c9bbc80d..6aba7600 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -31,7 +31,7 @@ void GameCore::GeneratePrimaryUnitList() { * */ ADD_SELECTABLE_UNIT(unit::InfernoTank); ADD_SELECTABLE_UNIT(unit::Tank); - ADD_SELECTABLE_UNIT(unit::RandomTank); + ADD_SELECTABLE_UNIT(unit::JokerTank); ADD_SELECTABLE_UNIT(unit::DoubleScatterTank); ADD_SELECTABLE_UNIT(unit::ThreeBodyMan); ADD_SELECTABLE_UNIT(unit::LMTank); diff --git a/src/battle_game/core/units/random_tank.cpp b/src/battle_game/core/units/joker_tank.cpp similarity index 83% rename from src/battle_game/core/units/random_tank.cpp rename to src/battle_game/core/units/joker_tank.cpp index eb97be91..36780cfd 100644 --- a/src/battle_game/core/units/random_tank.cpp +++ b/src/battle_game/core/units/joker_tank.cpp @@ -1,7 +1,7 @@ #include "battle_game/core/bullets/bullets.h" #include "battle_game/core/game_core.h" #include "battle_game/graphics/graphics.h" -#include "random_tank.h" +#include "joker_tank.h" namespace battle_game::unit { @@ -10,12 +10,12 @@ uint32_t tank_body_model_index = 0xffffffffu; uint32_t tank_turret_model_index = 0xffffffffu; } // namespace -RandomTank::RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id) +JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) : Unit(game_core, id, player_id) { if (!~tank_body_model_index) { auto mgr = AssetsManager::GetInstance(); { - /* RandomTank Body */ + /* JokerTank Body */ tank_body_model_index = mgr->RegisterModel( { {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, @@ -30,7 +30,7 @@ RandomTank::RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id) } { - /* RandomTank Turret */ + /* JokerTank Turret */ std::vector turret_vertices; std::vector turret_indices; const int precision = 60; @@ -67,11 +67,15 @@ RandomTank::RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } - + Skill skill; + skill.name = "HP->ATK"; + skill.description = "The lower HP, the higher damage"; + skill.type = P; + skills_.push_back(skill); } -void RandomTank::Render() { +void JokerTank::Render() { battle_game::SetTransformation(position_, rotation_); battle_game::SetTexture(0); battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); @@ -80,14 +84,14 @@ void RandomTank::Render() { battle_game::DrawModel(tank_turret_model_index); } -void RandomTank::Update() { +void JokerTank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); - + Passive(); } -void RandomTank::TankMove(float move_speed, float rotate_angular_speed) { +void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -98,8 +102,7 @@ void RandomTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_S]) { offset.y -= 1.0f; } - float speed = - move_speed * GetSpeedScale() * 2 * game_core_->RandomFloat(); + float speed = move_speed * GetSpeedScale(); offset *= kSecondPerTick * speed; auto new_position = position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, @@ -115,13 +118,12 @@ void RandomTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_D]) { rotation_offset -= 1.0f; } - rotation_offset *= kSecondPerTick * rotate_angular_speed * - GetSpeedScale()* 2 * game_core_->RandomFloat(); + rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); } } -void RandomTank::TurretRotate() { +void JokerTank::TurretRotate() { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -134,9 +136,12 @@ void RandomTank::TurretRotate() { } } - + float JokerTank::GetDamageScale() const { + + return 3.0 - 2 * GetHealthScale(); + } -void RandomTank::Fire() { +void JokerTank::Fire() { if (fire_count_down_ == 0) { auto player = game_core_->GetPlayer(player_id_); if (player) { @@ -145,10 +150,8 @@ void RandomTank::Fire() { auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); GenerateBullet( position_ + Rotate({0.0f, 1.2f}, turret_rotation_), - turret_rotation_, GetDamageScale() * 2.5 * game_core_->RandomFloat(), - velocity ); - fire_count_down_ = - kTickPerSecond * 1.5 * game_core_->RandomFloat(); // Fire interval 1 second. + turret_rotation_, GetDamageScale(), velocity); + fire_count_down_ = kTickPerSecond; // Fire interval 1 second. } } } @@ -157,18 +160,18 @@ void RandomTank::Fire() { } } -bool RandomTank::IsHit(glm::vec2 position) const { +bool JokerTank::IsHit(glm::vec2 position) const { position = WorldToLocal(position); return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && position.y < 1.0f && position.x + position.y < 1.6f && position.y - position.x < 1.6f; } -const char *RandomTank::UnitName() const { - return "Random Tank"; +const char *JokerTank::UnitName() const { + return "Joker Tank"; } -const char *RandomTank::Author() const { +const char *JokerTank::Author() const { return "Joker zhs"; } } // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/random_tank.h b/src/battle_game/core/units/joker_tank.h similarity index 76% rename from src/battle_game/core/units/random_tank.h rename to src/battle_game/core/units/joker_tank.h index ba1c6938..b6de9d13 100644 --- a/src/battle_game/core/units/random_tank.h +++ b/src/battle_game/core/units/joker_tank.h @@ -2,9 +2,9 @@ #include "battle_game/core/unit.h" namespace battle_game::unit { -class RandomTank : public Unit { +class JokerTank : public Unit { public: - RandomTank(GameCore *game_core, uint32_t id, uint32_t player_id); + JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id); void Render() override; void Update() override; [[nodiscard]] bool IsHit(glm::vec2 position) const override; @@ -13,10 +13,10 @@ class RandomTank : public Unit { void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); - + void Passive(); [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; - + [[nodiscard]] float GetDamageScale() const override; float turret_rotation_{0.0f}; diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index 05b3711e..5ec14031 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -5,7 +5,7 @@ #include "battle_game/core/units/dark_fury.h" #include "battle_game/core/units/double_scatter_tank.h" #include "battle_game/core/units/inferno_tank.h" -#include "battle_game/core/units/random_tank.h" +#include "battle_game/core/units/joker_tank.h" #include "battle_game/core/units/lm_tank.h" #include "battle_game/core/units/mine_sample_tank.h" #include "battle_game/core/units/missile_tank.h" From 32f3f94b5d9c0ce8bb9c1b6c658bde2cb20ca534 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 18:17:47 +0800 Subject: [PATCH 06/13] --- src/battle_game/core/units/vampire _tank.cpp | 175 +++++++++++++++++++ src/battle_game/core/units/vampire _tank.h | 24 +++ 2 files changed, 199 insertions(+) create mode 100644 src/battle_game/core/units/vampire _tank.cpp create mode 100644 src/battle_game/core/units/vampire _tank.h diff --git a/src/battle_game/core/units/vampire _tank.cpp b/src/battle_game/core/units/vampire _tank.cpp new file mode 100644 index 00000000..6dbd1801 --- /dev/null +++ b/src/battle_game/core/units/vampire _tank.cpp @@ -0,0 +1,175 @@ +#include "battle_game/core/bullets/bullets.h" +#include "battle_game/core/game_core.h" +#include "battle_game/graphics/graphics.h" +#include "tiny_tank.h" + +namespace battle_game::unit { + +namespace { +uint32_t tank_body_model_index = 0xffffffffu; +uint32_t tank_turret_model_index = 0xffffffffu; +} // namespace + +Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) + : Unit(game_core, id, player_id) { + if (!~tank_body_model_index) { + auto mgr = AssetsManager::GetInstance(); + { + /* Tank Body */ + tank_body_model_index = mgr->RegisterModel( + { + {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + // distinguish front and back + {{0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + }, + {0, 1, 2, 1, 2, 3, 0, 2, 5, 2, 4, 5}); + } + + { + /* Tank Turret */ + std::vector turret_vertices; + std::vector turret_indices; + const int precision = 60; + const float inv_precision = 1.0f / float(precision); + for (int i = 0; i < precision; i++) { + auto theta = (float(i) + 0.5f) * inv_precision; + theta *= glm::pi() * 2.0f; + auto sin_theta = std::sin(theta); + auto cos_theta = std::cos(theta); + turret_vertices.push_back({{sin_theta * 0.5f, cos_theta * 0.5f}, + {0.0f, 0.0f}, + {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(i); + turret_indices.push_back((i + 1) % precision); + turret_indices.push_back(precision); + } + turret_vertices.push_back( + {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(precision + 1 + 0); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 3); + tank_turret_model_index = + mgr->RegisterModel(turret_vertices, turret_indices); + } + } + Skill skill; + skill.name = "Vampire"; + skill.description = "Restore 40% of lost HP after each damage"; + skill.time_remain = 0; + skill.time_total = 480; + skill.type = E; + skill.function = SKILL_ADD_FUNCTION(TankYfl::SpeedUpClick); + skills_.push_back(skill); +} + +void Tank::Render() { + battle_game::SetTransformation(position_, rotation_); + battle_game::SetTexture(0); + battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); + battle_game::DrawModel(tank_body_model_index); + battle_game::SetRotation(turret_rotation_); + battle_game::DrawModel(tank_turret_model_index); +} + +void Tank::Update() { + TankMove(3.0f, glm::radians(180.0f)); + TurretRotate(); + Fire(); +} + +void Tank::TankMove(float move_speed, float rotate_angular_speed) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + glm::vec2 offset{0.0f}; + if (input_data.key_down[GLFW_KEY_W]) { + offset.y += 1.0f; + } + if (input_data.key_down[GLFW_KEY_S]) { + offset.y -= 1.0f; + } + float speed = move_speed * GetSpeedScale(); + offset *= kSecondPerTick * speed; + auto new_position = + position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, + glm::vec3{0.0f, 0.0f, 1.0f}) * + glm::vec4{offset, 0.0f, 0.0f}}; + if (!game_core_->IsBlockedByObstacles(new_position)) { + game_core_->PushEventMoveUnit(id_, new_position); + } + float rotation_offset = 0.0f; + if (input_data.key_down[GLFW_KEY_A]) { + rotation_offset += 1.0f; + } + if (input_data.key_down[GLFW_KEY_D]) { + rotation_offset -= 1.0f; + } + rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); + game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); + } +} + +void Tank::TurretRotate() { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + auto diff = input_data.mouse_cursor_position - position_; + if (glm::length(diff) < 1e-4) { + turret_rotation_ = rotation_; + } else { + turret_rotation_ = std::atan2(diff.y, diff.x) - glm::radians(90.0f); + } + } +} +void Vampire() { + +} +void Tank::Fire() { + if (fire_count_down_ == 0) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { + auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); + GenerateBullet( + position_ + Rotate({0.0f, 1.2f}, turret_rotation_), + turret_rotation_, GetDamageScale(), velocity); + fire_count_down_ = kTickPerSecond; // Fire interval 1 second. + } + } + } + if (fire_count_down_) { + fire_count_down_--; + } +} + +bool Tank::IsHit(glm::vec2 position) const { + position = WorldToLocal(position); + return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && + position.y < 1.0f && position.x + position.y < 1.6f && + position.y - position.x < 1.6f; +} + +const char *Tank::UnitName() const { + return "Tiny Tank"; +} + +const char *Tank::Author() const { + return "LazyJazz"; +} +} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/vampire _tank.h b/src/battle_game/core/units/vampire _tank.h new file mode 100644 index 00000000..a111dc2b --- /dev/null +++ b/src/battle_game/core/units/vampire _tank.h @@ -0,0 +1,24 @@ +#pragma once +#include "battle_game/core/unit.h" + +namespace battle_game::unit { +class Tank : public Unit { + public: + Tank(GameCore *game_core, uint32_t id, uint32_t player_id); + void Render() override; + void Update() override; + [[nodiscard]] bool IsHit(glm::vec2 position) const override; + + protected: + void TankMove(float move_speed, float rotate_angular_speed); + void TurretRotate(); + void Fire(); + void Vampire(); + [[nodiscard]] const char *UnitName() const override; + [[nodiscard]] const char *Author() const override; + + float turret_rotation_{0.0f}; + uint32_t fire_count_down_{0}; + uint32_t mine_count_down_{0}; +}; +} // namespace battle_game::unit From 1e85d0091866463a5d535576ad8cc500b243490e Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 18:17:54 +0800 Subject: [PATCH 07/13] --- src/battle_game/core/units/units.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index 5ec14031..7a3159b2 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -24,3 +24,4 @@ #include "battle_game/core/units/triple_shot_tank.h" #include "battle_game/core/units/udongein.h" #include "battle_game/core/units/zibeng_dog.h" +#include "battle_game/core/units/vampire_tank.h" From c0b3c6049a1d4d3ab3486cfc19f170b86efd57a8 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 18:48:51 +0800 Subject: [PATCH 08/13] n --- src/battle_game/core/units/vampire _tank.cpp | 28 +++++++++----------- src/battle_game/core/units/vampire _tank.h | 8 +++--- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/battle_game/core/units/vampire _tank.cpp b/src/battle_game/core/units/vampire _tank.cpp index 6dbd1801..cb028156 100644 --- a/src/battle_game/core/units/vampire _tank.cpp +++ b/src/battle_game/core/units/vampire _tank.cpp @@ -3,14 +3,12 @@ #include "battle_game/graphics/graphics.h" #include "tiny_tank.h" -namespace battle_game::unit { - -namespace { +namespace battle_game::unit + namespace { uint32_t tank_body_model_index = 0xffffffffu; uint32_t tank_turret_model_index = 0xffffffffu; } // namespace - -Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) +VampireTank::VampireTank(GameCore *game_core, uint32_t id, uint32_t player_id) : Unit(game_core, id, player_id) { if (!~tank_body_model_index) { auto mgr = AssetsManager::GetInstance(); @@ -77,7 +75,7 @@ Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) skills_.push_back(skill); } -void Tank::Render() { +void VampireTank::Render() { battle_game::SetTransformation(position_, rotation_); battle_game::SetTexture(0); battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); @@ -86,13 +84,13 @@ void Tank::Render() { battle_game::DrawModel(tank_turret_model_index); } -void Tank::Update() { +void VampireTank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); } -void Tank::TankMove(float move_speed, float rotate_angular_speed) { +void VampireTank::TankMove(float move_speed, float rotate_angular_speed) { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -124,7 +122,7 @@ void Tank::TankMove(float move_speed, float rotate_angular_speed) { } } -void Tank::TurretRotate() { +void VampireTank::TurretRotate() { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -136,10 +134,8 @@ void Tank::TurretRotate() { } } } -void Vampire() { - -} -void Tank::Fire() { + +void VampireTank::Fire() { if (fire_count_down_ == 0) { auto player = game_core_->GetPlayer(player_id_); if (player) { @@ -158,18 +154,18 @@ void Tank::Fire() { } } -bool Tank::IsHit(glm::vec2 position) const { +bool VampireTank::IsHit(glm::vec2 position) const { position = WorldToLocal(position); return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && position.y < 1.0f && position.x + position.y < 1.6f && position.y - position.x < 1.6f; } -const char *Tank::UnitName() const { +const char *VampireTank::UnitName() const { return "Tiny Tank"; } -const char *Tank::Author() const { +const char *VampireTank::Author() const { return "LazyJazz"; } } // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/vampire _tank.h b/src/battle_game/core/units/vampire _tank.h index a111dc2b..2b6acc45 100644 --- a/src/battle_game/core/units/vampire _tank.h +++ b/src/battle_game/core/units/vampire _tank.h @@ -2,18 +2,18 @@ #include "battle_game/core/unit.h" namespace battle_game::unit { -class Tank : public Unit { +class VampireTank : public Unit { public: - Tank(GameCore *game_core, uint32_t id, uint32_t player_id); + VampireTank(GameCore *game_core, uint32_t id, uint32_t player_id); void Render() override; void Update() override; [[nodiscard]] bool IsHit(glm::vec2 position) const override; - + protected: void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); - void Vampire(); + [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; From 11e995018129c26b3d14cf0164eb21d45aa2cfdd Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 19:43:47 +0800 Subject: [PATCH 09/13] --- src/battle_game/core/CMakeLists.txt | 3 +- src/battle_game/core/bullets/joker.cpp | 2 +- src/battle_game/core/game_core.cpp | 2 + src/battle_game/core/game_core.h | 2 +- src/battle_game/core/selectable_units.cpp | 1 + src/battle_game/core/units/joker_tank.cpp | 24 +++++----- src/battle_game/core/units/joker_tank.h | 5 +-- .../core/units/particles/bullet_hole.cpp | 27 ++++++++++++ .../core/units/particles/bullet_hole.h | 18 ++++++++ .../core/units/particles/explosion.cpp | 44 +++++++++++++++++++ .../core/units/particles/explosion.h | 20 +++++++++ .../core/units/particles/particles.h | 5 +++ .../core/units/particles/smoke.cpp | 35 +++++++++++++++ src/battle_game/core/units/particles/smoke.h | 25 +++++++++++ .../core/units/particles/thunderbolt.cpp | 27 ++++++++++++ .../core/units/particles/thunderbolt.h | 18 ++++++++ src/battle_game/core/units/units.h | 5 ++- 17 files changed, 242 insertions(+), 21 deletions(-) create mode 100644 src/battle_game/core/units/particles/bullet_hole.cpp create mode 100644 src/battle_game/core/units/particles/bullet_hole.h create mode 100644 src/battle_game/core/units/particles/explosion.cpp create mode 100644 src/battle_game/core/units/particles/explosion.h create mode 100644 src/battle_game/core/units/particles/particles.h create mode 100644 src/battle_game/core/units/particles/smoke.cpp create mode 100644 src/battle_game/core/units/particles/smoke.h create mode 100644 src/battle_game/core/units/particles/thunderbolt.cpp create mode 100644 src/battle_game/core/units/particles/thunderbolt.h diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index 0f108ede..a1960302 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,8 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp") +target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp" ) + target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/joker.cpp b/src/battle_game/core/bullets/joker.cpp index 49cacac8..29e3efb2 100644 --- a/src/battle_game/core/bullets/joker.cpp +++ b/src/battle_game/core/bullets/joker.cpp @@ -37,7 +37,7 @@ void Joker::Update() { } if (unit.second->IsHit(position_)) { game_core_->PushEventDealDamage( - unit.first, id_, damage_scale_ * (game_core_->RandomFloat()) * 20.0f); + unit.first, id_, damage_scale_ * (game_core_->RandomFloat()) * 25.0f); should_die = true; } } diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index f8845ba7..fcc1cb43 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -227,6 +227,7 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, } } }); + } void GameCore::PushEventRemoveObstacle(uint32_t obstacle_id) { @@ -243,6 +244,7 @@ void GameCore::PushEventRemoveBullet(uint32_t bullet_id) { bullets_.erase(bullet_id); } }); + } void GameCore::PushEventRemoveParticle(uint32_t particle_id) { diff --git a/src/battle_game/core/game_core.h b/src/battle_game/core/game_core.h index 0e81d8e4..4dd02f7b 100644 --- a/src/battle_game/core/game_core.h +++ b/src/battle_game/core/game_core.h @@ -153,7 +153,7 @@ class GameCore { event_queue_.emplace( [=]() { AddObstacle(position, rotation, args...); }); } - + template void PushEventGenerateParticle(glm::vec2 position, float rotation = 0.0f, diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index 6aba7600..a223d124 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -53,6 +53,7 @@ void GameCore::GeneratePrimaryUnitList() { ADD_SELECTABLE_UNIT(unit::Railgun); ADD_SELECTABLE_UNIT(unit::Udongein); ADD_SELECTABLE_UNIT(unit::LMTank); + unit.reset(); diff --git a/src/battle_game/core/units/joker_tank.cpp b/src/battle_game/core/units/joker_tank.cpp index 36780cfd..53e01eb9 100644 --- a/src/battle_game/core/units/joker_tank.cpp +++ b/src/battle_game/core/units/joker_tank.cpp @@ -67,11 +67,7 @@ JokerTank::JokerTank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } - Skill skill; - skill.name = "HP->ATK"; - skill.description = "The lower HP, the higher damage"; - skill.type = P; - skills_.push_back(skill); + } @@ -88,7 +84,7 @@ void JokerTank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); - Passive(); + } void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { @@ -102,7 +98,7 @@ void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_S]) { offset.y -= 1.0f; } - float speed = move_speed * GetSpeedScale(); + float speed = move_speed * GetSpeedScale() * 2 * game_core_->RandomFloat(); offset *= kSecondPerTick * speed; auto new_position = position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, @@ -118,7 +114,8 @@ void JokerTank::TankMove(float move_speed, float rotate_angular_speed) { if (input_data.key_down[GLFW_KEY_D]) { rotation_offset -= 1.0f; } - rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); + rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale() * + 2 * game_core_->RandomFloat(); game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); } } @@ -136,10 +133,7 @@ void JokerTank::TurretRotate() { } } - float JokerTank::GetDamageScale() const { - - return 3.0 - 2 * GetHealthScale(); - } + void JokerTank::Fire() { if (fire_count_down_ == 0) { @@ -148,10 +142,12 @@ void JokerTank::Fire() { auto &input_data = player->GetInputData(); if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); - GenerateBullet( + GenerateBullet( position_ + Rotate({0.0f, 1.2f}, turret_rotation_), turret_rotation_, GetDamageScale(), velocity); - fire_count_down_ = kTickPerSecond; // Fire interval 1 second. + fire_count_down_ = + kTickPerSecond * 1.5 * + game_core_->RandomFloat(); // Fire interval 1 second. } } } diff --git a/src/battle_game/core/units/joker_tank.h b/src/battle_game/core/units/joker_tank.h index b6de9d13..f90815d0 100644 --- a/src/battle_game/core/units/joker_tank.h +++ b/src/battle_game/core/units/joker_tank.h @@ -13,11 +13,10 @@ class JokerTank : public Unit { void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); - void Passive(); + [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; - [[nodiscard]] float GetDamageScale() const override; - + float turret_rotation_{0.0f}; uint32_t fire_count_down_{0}; diff --git a/src/battle_game/core/units/particles/bullet_hole.cpp b/src/battle_game/core/units/particles/bullet_hole.cpp new file mode 100644 index 00000000..ff6103fd --- /dev/null +++ b/src/battle_game/core/units/particles/bullet_hole.cpp @@ -0,0 +1,27 @@ +#include "battle_game/core/particles/bullet_hole.h" + +#include "battle_game/core/game_core.h" + +namespace battle_game::particle { +BulletHole::BulletHole(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration) + : Particle(game_core, id, position, rotation), duration_(duration) { +} + +void BulletHole::Render() { + SetTransformation(position_, rotation_, glm::vec2{1.0f}); + SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); + SetTexture("../../textures/bullet_hole.png"); + DrawModel(0); +} + +void BulletHole::Update() { + duration_--; + if (duration_ <= 0) { + game_core_->PushEventRemoveParticle(id_); + } +} +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/bullet_hole.h b/src/battle_game/core/units/particles/bullet_hole.h new file mode 100644 index 00000000..efca6853 --- /dev/null +++ b/src/battle_game/core/units/particles/bullet_hole.h @@ -0,0 +1,18 @@ +#pragma once +#include "battle_game/core/particle.h" + +namespace battle_game::particle { +class BulletHole : public Particle { + public: + BulletHole(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration); + void Render() override; + void Update() override; + + private: + uint32_t duration_{}; +}; +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/explosion.cpp b/src/battle_game/core/units/particles/explosion.cpp new file mode 100644 index 00000000..ccee432d --- /dev/null +++ b/src/battle_game/core/units/particles/explosion.cpp @@ -0,0 +1,44 @@ +#include "battle_game/core/particles/explosion.h" + +#include "battle_game/core/game_core.h" + +namespace battle_game::particle { +Explosion::Explosion(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration) + : Particle(game_core, id, position, rotation), duration_(duration) { +} + +void Explosion::Render() { + SetTransformation(position_, rotation_, glm::vec2{2.0f}); + SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); + SetTexture("../../textures/explosion.png"); + DrawModel(0); +} + +void Explosion::Update() { + if (should_damage_) { + auto &units = game_core_->GetUnits(); + for (auto &unit : units) { + auto position = unit.second->GetPosition(); + if (IsInExplosion(position)) { + game_core_->PushEventDealDamage(unit.first, id_, 10.0f); + } + } + should_damage_ = false; + } + duration_--; + if (duration_ <= 0) { + game_core_->PushEventRemoveParticle(id_); + } +} + +bool Explosion::IsInExplosion(glm::vec2 position) { + position = WorldToLocal(position); + return position.x > -1.6f && position.x < 1.6f && position.y > -2.0f && + position.y < 2.0f && position.x + position.y < 3.2f && + position.y - position.x < 3.2f; +} +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/explosion.h b/src/battle_game/core/units/particles/explosion.h new file mode 100644 index 00000000..e0194965 --- /dev/null +++ b/src/battle_game/core/units/particles/explosion.h @@ -0,0 +1,20 @@ +#pragma once +#include "battle_game/core/particle.h" + +namespace battle_game::particle { +class Explosion : public Particle { + public: + Explosion(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration); + void Render() override; + void Update() override; + bool IsInExplosion(glm::vec2 position); + + private: + uint32_t duration_{}; + bool should_damage_{true}; +}; +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/particles.h b/src/battle_game/core/units/particles/particles.h new file mode 100644 index 00000000..71510014 --- /dev/null +++ b/src/battle_game/core/units/particles/particles.h @@ -0,0 +1,5 @@ +#pragma once +#include "battle_game/core/particles/bullet_hole.h" +#include "battle_game/core/particles/explosion.h" +#include "battle_game/core/particles/smoke.h" +#include "battle_game/core/particles/thunderbolt.h" diff --git a/src/battle_game/core/units/particles/smoke.cpp b/src/battle_game/core/units/particles/smoke.cpp new file mode 100644 index 00000000..b44f4661 --- /dev/null +++ b/src/battle_game/core/units/particles/smoke.cpp @@ -0,0 +1,35 @@ +#include "battle_game/core/particles/smoke.h" + +#include "battle_game/core/game_core.h" + +namespace battle_game::particle { +Smoke::Smoke(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + glm::vec2 v, + float size, + glm::vec4 color, + float decay_scale) + : Particle(game_core, id, position, rotation), + v_(v), + size_(size), + color_(color), + decay_scale_(decay_scale) { +} + +void Smoke::Render() { + SetTransformation(position_, rotation_, glm::vec2{size_}); + SetColor(glm::vec4{glm::vec3{1.0f}, strength_} * color_); + SetTexture("../../textures/particle2.png"); + DrawModel(); +} + +void Smoke::Update() { + position_ += v_ * kSecondPerTick; + strength_ -= kSecondPerTick * decay_scale_; + if (strength_ < 0.0f) { + game_core_->PushEventRemoveParticle(id_); + } +} +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/smoke.h b/src/battle_game/core/units/particles/smoke.h new file mode 100644 index 00000000..a7623932 --- /dev/null +++ b/src/battle_game/core/units/particles/smoke.h @@ -0,0 +1,25 @@ +#pragma once +#include "battle_game/core/particle.h" + +namespace battle_game::particle { +class Smoke : public Particle { + public: + Smoke(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + glm::vec2 v, + float size = 0.2f, + glm::vec4 color = glm::vec4{1.0f}, + float decay_scale = 1.0f); + void Render() override; + void Update() override; + + private: + glm::vec2 v_{}; + float strength_{1.0f}; + float size_{}; + float decay_scale_{}; + glm::vec4 color_{}; +}; +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/thunderbolt.cpp b/src/battle_game/core/units/particles/thunderbolt.cpp new file mode 100644 index 00000000..b2176d6f --- /dev/null +++ b/src/battle_game/core/units/particles/thunderbolt.cpp @@ -0,0 +1,27 @@ +#include "battle_game/core/particles/thunderbolt.h" + +#include "battle_game/core/game_core.h" + +namespace battle_game::particle { +Thunderbolt::Thunderbolt(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration) + : Particle(game_core, id, position, rotation), duration_(duration) { +} + +void Thunderbolt::Render() { + SetTransformation(position_, rotation_, glm::vec2{1.5f}); + SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); + SetTexture("../../textures/thunderbolt.png"); + DrawModel(0); +} + +void Thunderbolt::Update() { + duration_--; + if (duration_ <= 0) { + game_core_->PushEventRemoveParticle(id_); + } +} +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/thunderbolt.h b/src/battle_game/core/units/particles/thunderbolt.h new file mode 100644 index 00000000..d2679e62 --- /dev/null +++ b/src/battle_game/core/units/particles/thunderbolt.h @@ -0,0 +1,18 @@ +#pragma once +#include "battle_game/core/particle.h" + +namespace battle_game::particle { +class Thunderbolt : public Particle { + public: + Thunderbolt(GameCore *game_core, + uint32_t id, + glm::vec2 position, + float rotation, + uint32_t duration); + void Render() override; + void Update() override; + + private: + uint32_t duration_{}; +}; +} // namespace battle_game::particle diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index 7a3159b2..8e403cb7 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -24,4 +24,7 @@ #include "battle_game/core/units/triple_shot_tank.h" #include "battle_game/core/units/udongein.h" #include "battle_game/core/units/zibeng_dog.h" -#include "battle_game/core/units/vampire_tank.h" + + + + From 32465bceb252a424a62aa833693de3ae7be20276 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:28:39 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E6=94=B9=E4=BA=86=E4=B8=80=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/battle_game/core/CMakeLists.txt | 2 +- src/battle_game/core/bullets/cannon_ball.cpp | 2 + src/battle_game/core/game_core.cpp | 2 + src/battle_game/core/game_core.h | 2 +- src/battle_game/core/selectable_units.cpp | 1 + src/battle_game/core/units/tiny_tank.cpp | 8 + src/battle_game/core/units/tiny_tank.h | 1 + src/battle_game/core/units/vampire _tank.cpp | 171 ------------------- src/battle_game/core/units/vampire _tank.h | 24 --- 9 files changed, 16 insertions(+), 197 deletions(-) delete mode 100644 src/battle_game/core/units/vampire _tank.cpp delete mode 100644 src/battle_game/core/units/vampire _tank.h diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index a1960302..01407825 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,7 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files} "bullets/joker.cpp" ) +target_sources(${lib_name} PRIVATE ${source_files} ) target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/cannon_ball.cpp b/src/battle_game/core/bullets/cannon_ball.cpp index 445d3929..16b6d64f 100644 --- a/src/battle_game/core/bullets/cannon_ball.cpp +++ b/src/battle_game/core/bullets/cannon_ball.cpp @@ -28,6 +28,7 @@ void CannonBall::Update() { bool should_die = false; if (game_core_->IsBlockedByObstacles(position_)) { should_die = true; + } auto &units = game_core_->GetUnits(); @@ -37,6 +38,7 @@ void CannonBall::Update() { } if (unit.second->IsHit(position_)) { game_core_->PushEventDealDamage(unit.first, id_, damage_scale_ * 10.0f); + should_die = true; } } diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index fcc1cb43..9d73c8fc 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -226,6 +226,7 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, PushEventKillUnit(dst_unit_id, src_unit_id); } } + }); } @@ -243,6 +244,7 @@ void GameCore::PushEventRemoveBullet(uint32_t bullet_id) { if (bullets_.count(bullet_id)) { bullets_.erase(bullet_id); } + }); } diff --git a/src/battle_game/core/game_core.h b/src/battle_game/core/game_core.h index 4dd02f7b..0e81d8e4 100644 --- a/src/battle_game/core/game_core.h +++ b/src/battle_game/core/game_core.h @@ -153,7 +153,7 @@ class GameCore { event_queue_.emplace( [=]() { AddObstacle(position, rotation, args...); }); } - + template void PushEventGenerateParticle(glm::vec2 position, float rotation = 0.0f, diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index a223d124..b0e22499 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -56,6 +56,7 @@ void GameCore::GeneratePrimaryUnitList() { + unit.reset(); } } // namespace battle_game diff --git a/src/battle_game/core/units/tiny_tank.cpp b/src/battle_game/core/units/tiny_tank.cpp index 99b61f59..642505a6 100644 --- a/src/battle_game/core/units/tiny_tank.cpp +++ b/src/battle_game/core/units/tiny_tank.cpp @@ -68,6 +68,13 @@ Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } + Skill temp; + temp.name = "Switch Mode"; + temp.description = "Switch Mode"; + temp.time_remain = 0; + temp.time_total = 480; + temp.type = E; + skills_.push_back(temp); } void Tank::Render() { @@ -83,6 +90,7 @@ void Tank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); + } void Tank::TankMove(float move_speed, float rotate_angular_speed) { diff --git a/src/battle_game/core/units/tiny_tank.h b/src/battle_game/core/units/tiny_tank.h index 507c8044..482e8ab6 100644 --- a/src/battle_game/core/units/tiny_tank.h +++ b/src/battle_game/core/units/tiny_tank.h @@ -13,6 +13,7 @@ class Tank : public Unit { void TankMove(float move_speed, float rotate_angular_speed); void TurretRotate(); void Fire(); + [[nodiscard]] const char *UnitName() const override; [[nodiscard]] const char *Author() const override; diff --git a/src/battle_game/core/units/vampire _tank.cpp b/src/battle_game/core/units/vampire _tank.cpp deleted file mode 100644 index cb028156..00000000 --- a/src/battle_game/core/units/vampire _tank.cpp +++ /dev/null @@ -1,171 +0,0 @@ -#include "battle_game/core/bullets/bullets.h" -#include "battle_game/core/game_core.h" -#include "battle_game/graphics/graphics.h" -#include "tiny_tank.h" - -namespace battle_game::unit - namespace { -uint32_t tank_body_model_index = 0xffffffffu; -uint32_t tank_turret_model_index = 0xffffffffu; -} // namespace -VampireTank::VampireTank(GameCore *game_core, uint32_t id, uint32_t player_id) - : Unit(game_core, id, player_id) { - if (!~tank_body_model_index) { - auto mgr = AssetsManager::GetInstance(); - { - /* Tank Body */ - tank_body_model_index = mgr->RegisterModel( - { - {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - {{-0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - {{0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - {{0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - // distinguish front and back - {{0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - {{-0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, - }, - {0, 1, 2, 1, 2, 3, 0, 2, 5, 2, 4, 5}); - } - - { - /* Tank Turret */ - std::vector turret_vertices; - std::vector turret_indices; - const int precision = 60; - const float inv_precision = 1.0f / float(precision); - for (int i = 0; i < precision; i++) { - auto theta = (float(i) + 0.5f) * inv_precision; - theta *= glm::pi() * 2.0f; - auto sin_theta = std::sin(theta); - auto cos_theta = std::cos(theta); - turret_vertices.push_back({{sin_theta * 0.5f, cos_theta * 0.5f}, - {0.0f, 0.0f}, - {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_indices.push_back(i); - turret_indices.push_back((i + 1) % precision); - turret_indices.push_back(precision); - } - turret_vertices.push_back( - {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_vertices.push_back( - {{-0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_vertices.push_back( - {{0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_vertices.push_back( - {{-0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_vertices.push_back( - {{0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); - turret_indices.push_back(precision + 1 + 0); - turret_indices.push_back(precision + 1 + 1); - turret_indices.push_back(precision + 1 + 2); - turret_indices.push_back(precision + 1 + 1); - turret_indices.push_back(precision + 1 + 2); - turret_indices.push_back(precision + 1 + 3); - tank_turret_model_index = - mgr->RegisterModel(turret_vertices, turret_indices); - } - } - Skill skill; - skill.name = "Vampire"; - skill.description = "Restore 40% of lost HP after each damage"; - skill.time_remain = 0; - skill.time_total = 480; - skill.type = E; - skill.function = SKILL_ADD_FUNCTION(TankYfl::SpeedUpClick); - skills_.push_back(skill); -} - -void VampireTank::Render() { - battle_game::SetTransformation(position_, rotation_); - battle_game::SetTexture(0); - battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); - battle_game::DrawModel(tank_body_model_index); - battle_game::SetRotation(turret_rotation_); - battle_game::DrawModel(tank_turret_model_index); -} - -void VampireTank::Update() { - TankMove(3.0f, glm::radians(180.0f)); - TurretRotate(); - Fire(); -} - -void VampireTank::TankMove(float move_speed, float rotate_angular_speed) { - auto player = game_core_->GetPlayer(player_id_); - if (player) { - auto &input_data = player->GetInputData(); - glm::vec2 offset{0.0f}; - if (input_data.key_down[GLFW_KEY_W]) { - offset.y += 1.0f; - } - if (input_data.key_down[GLFW_KEY_S]) { - offset.y -= 1.0f; - } - float speed = move_speed * GetSpeedScale(); - offset *= kSecondPerTick * speed; - auto new_position = - position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, - glm::vec3{0.0f, 0.0f, 1.0f}) * - glm::vec4{offset, 0.0f, 0.0f}}; - if (!game_core_->IsBlockedByObstacles(new_position)) { - game_core_->PushEventMoveUnit(id_, new_position); - } - float rotation_offset = 0.0f; - if (input_data.key_down[GLFW_KEY_A]) { - rotation_offset += 1.0f; - } - if (input_data.key_down[GLFW_KEY_D]) { - rotation_offset -= 1.0f; - } - rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); - game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); - } -} - -void VampireTank::TurretRotate() { - auto player = game_core_->GetPlayer(player_id_); - if (player) { - auto &input_data = player->GetInputData(); - auto diff = input_data.mouse_cursor_position - position_; - if (glm::length(diff) < 1e-4) { - turret_rotation_ = rotation_; - } else { - turret_rotation_ = std::atan2(diff.y, diff.x) - glm::radians(90.0f); - } - } -} - -void VampireTank::Fire() { - if (fire_count_down_ == 0) { - auto player = game_core_->GetPlayer(player_id_); - if (player) { - auto &input_data = player->GetInputData(); - if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { - auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); - GenerateBullet( - position_ + Rotate({0.0f, 1.2f}, turret_rotation_), - turret_rotation_, GetDamageScale(), velocity); - fire_count_down_ = kTickPerSecond; // Fire interval 1 second. - } - } - } - if (fire_count_down_) { - fire_count_down_--; - } -} - -bool VampireTank::IsHit(glm::vec2 position) const { - position = WorldToLocal(position); - return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && - position.y < 1.0f && position.x + position.y < 1.6f && - position.y - position.x < 1.6f; -} - -const char *VampireTank::UnitName() const { - return "Tiny Tank"; -} - -const char *VampireTank::Author() const { - return "LazyJazz"; -} -} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/vampire _tank.h b/src/battle_game/core/units/vampire _tank.h deleted file mode 100644 index 2b6acc45..00000000 --- a/src/battle_game/core/units/vampire _tank.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once -#include "battle_game/core/unit.h" - -namespace battle_game::unit { -class VampireTank : public Unit { - public: - VampireTank(GameCore *game_core, uint32_t id, uint32_t player_id); - void Render() override; - void Update() override; - [[nodiscard]] bool IsHit(glm::vec2 position) const override; - - protected: - void TankMove(float move_speed, float rotate_angular_speed); - void TurretRotate(); - void Fire(); - - [[nodiscard]] const char *UnitName() const override; - [[nodiscard]] const char *Author() const override; - - float turret_rotation_{0.0f}; - uint32_t fire_count_down_{0}; - uint32_t mine_count_down_{0}; -}; -} // namespace battle_game::unit From 5d909e0ee81436c9654f55f20148b097a17b2f56 Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 22:30:49 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E6=97=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/battle_game/core/units/tiny_tank.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/battle_game/core/units/tiny_tank.cpp b/src/battle_game/core/units/tiny_tank.cpp index 642505a6..6f156964 100644 --- a/src/battle_game/core/units/tiny_tank.cpp +++ b/src/battle_game/core/units/tiny_tank.cpp @@ -68,13 +68,6 @@ Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) mgr->RegisterModel(turret_vertices, turret_indices); } } - Skill temp; - temp.name = "Switch Mode"; - temp.description = "Switch Mode"; - temp.time_remain = 0; - temp.time_total = 480; - temp.type = E; - skills_.push_back(temp); } void Tank::Render() { From c3b90d31206ac2dcf829b9e4153abb93072f425c Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Sun, 19 Feb 2023 23:38:58 +0800 Subject: [PATCH 12/13] #502 --- src/battle_game/core/CMakeLists.txt | 2 +- src/battle_game/core/bullets/bullets.h | 1 + src/battle_game/core/bullets/pea.cpp | 59 +++++++ src/battle_game/core/bullets/pea.h | 22 +++ src/battle_game/core/selectable_units.cpp | 1 + .../core/units/particles/bullet_hole.cpp | 27 --- .../core/units/particles/bullet_hole.h | 18 -- .../core/units/particles/explosion.cpp | 44 ----- .../core/units/particles/explosion.h | 20 --- .../core/units/particles/particles.h | 5 - .../core/units/particles/smoke.cpp | 35 ---- src/battle_game/core/units/particles/smoke.h | 25 --- .../core/units/particles/thunderbolt.cpp | 27 --- .../core/units/particles/thunderbolt.h | 18 -- src/battle_game/core/units/strange_tank.cpp | 166 ++++++++++++++++++ src/battle_game/core/units/strange_tank.h | 24 +++ src/battle_game/core/units/units.h | 1 + 17 files changed, 275 insertions(+), 220 deletions(-) create mode 100644 src/battle_game/core/bullets/pea.cpp create mode 100644 src/battle_game/core/bullets/pea.h delete mode 100644 src/battle_game/core/units/particles/bullet_hole.cpp delete mode 100644 src/battle_game/core/units/particles/bullet_hole.h delete mode 100644 src/battle_game/core/units/particles/explosion.cpp delete mode 100644 src/battle_game/core/units/particles/explosion.h delete mode 100644 src/battle_game/core/units/particles/particles.h delete mode 100644 src/battle_game/core/units/particles/smoke.cpp delete mode 100644 src/battle_game/core/units/particles/smoke.h delete mode 100644 src/battle_game/core/units/particles/thunderbolt.cpp delete mode 100644 src/battle_game/core/units/particles/thunderbolt.h create mode 100644 src/battle_game/core/units/strange_tank.cpp create mode 100644 src/battle_game/core/units/strange_tank.h diff --git a/src/battle_game/core/CMakeLists.txt b/src/battle_game/core/CMakeLists.txt index 01407825..c07aa2c1 100644 --- a/src/battle_game/core/CMakeLists.txt +++ b/src/battle_game/core/CMakeLists.txt @@ -3,7 +3,7 @@ list(APPEND LIBRARY_LIST ${lib_name}) add_library(${lib_name}) file(GLOB_RECURSE source_files *.cpp *.h) -target_sources(${lib_name} PRIVATE ${source_files} ) +target_sources(${lib_name} PRIVATE ${source_files} "units/strange_tank.cpp" "units/strange_tank.h" "bullets/pea.h") target_link_libraries(${lib_name} PRIVATE grassland) target_include_directories(${lib_name} PRIVATE ${BATTLE_GAME_EXTERNAL_INCLUDE_DIRS} ${BATTLE_GAME_INCLUDE_DIR}) diff --git a/src/battle_game/core/bullets/bullets.h b/src/battle_game/core/bullets/bullets.h index d652fa4d..bafd13f9 100644 --- a/src/battle_game/core/bullets/bullets.h +++ b/src/battle_game/core/bullets/bullets.h @@ -14,3 +14,4 @@ #include "battle_game/core/bullets/warning_line.h" #include "battle_game/core/bullets/water_drop.h" #include "battle_game/core/bullets/joker.h" +#include "battle_game/core/bullets/pea.h" diff --git a/src/battle_game/core/bullets/pea.cpp b/src/battle_game/core/bullets/pea.cpp new file mode 100644 index 00000000..ee1c6fb4 --- /dev/null +++ b/src/battle_game/core/bullets/pea.cpp @@ -0,0 +1,59 @@ +#include "battle_game/core/bullets/pea.h" + +#include "battle_game/core/game_core.h" +#include "battle_game/core/particles/particles.h" + +namespace battle_game::bullet { +Pea::Pea(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity) + : Bullet(core, id, unit_id, player_id, position, rotation, damage_scale), + velocity_(velocity) { +} + +void Pea::Render() { + SetTransformation(position_, rotation_, glm::vec2{0.1f}); + SetColor(game_core_->GetPlayerColor(player_id_)); + SetTexture("../../textures/coin.png"); + DrawModel(0); +} + +void Pea::Update() { + position_ += velocity_ * kSecondPerTick; + bool should_die = false; + if (game_core_->IsBlockedByObstacles(position_)) { + game_core_->PushEventDealDamage(unit_id_, id_, damage_scale_ * 10.0f); + should_die = true; + + } + + auto &units = game_core_->GetUnits(); + for (auto &unit : units) { + if (unit.first == unit_id_) { + continue; + } + if (unit.second->IsHit(position_)) { + game_core_->PushEventDealDamage(unit.first, id_, damage_scale_ * 10.0f); + game_core_->PushEventDealDamage(unit_id_, id_, -damage_scale_ * 10.0f); + should_die = true; + } + } + + if (should_die) { + game_core_->PushEventRemoveBullet(id_); + } +} + +Pea::~Pea() { + for (int i = 0; i < 5; i++) { + game_core_->PushEventGenerateParticle( + position_, rotation_, game_core_->RandomInCircle() * 2.0f, 0.2f, + glm::vec4{0.0f, 0.0f, 0.0f, 1.0f}, 3.0f); + } +} +} // namespace battle_game::bullet diff --git a/src/battle_game/core/bullets/pea.h b/src/battle_game/core/bullets/pea.h new file mode 100644 index 00000000..e5d20204 --- /dev/null +++ b/src/battle_game/core/bullets/pea.h @@ -0,0 +1,22 @@ +#pragma once +#include "battle_game/core/bullet.h" + +namespace battle_game::bullet { +class Pea : public Bullet { + public: + Pea(GameCore *core, + uint32_t id, + uint32_t unit_id, + uint32_t player_id, + glm::vec2 position, + float rotation, + float damage_scale, + glm::vec2 velocity); + ~Pea() override; + void Render() override; + void Update() override; + + private: + glm::vec2 velocity_{}; +}; +} // namespace battle_game::bullet \ No newline at end of file diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index b0e22499..890deefb 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -53,6 +53,7 @@ void GameCore::GeneratePrimaryUnitList() { ADD_SELECTABLE_UNIT(unit::Railgun); ADD_SELECTABLE_UNIT(unit::Udongein); ADD_SELECTABLE_UNIT(unit::LMTank); + ADD_SELECTABLE_UNIT(unit::StrangeTank); diff --git a/src/battle_game/core/units/particles/bullet_hole.cpp b/src/battle_game/core/units/particles/bullet_hole.cpp deleted file mode 100644 index ff6103fd..00000000 --- a/src/battle_game/core/units/particles/bullet_hole.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "battle_game/core/particles/bullet_hole.h" - -#include "battle_game/core/game_core.h" - -namespace battle_game::particle { -BulletHole::BulletHole(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration) - : Particle(game_core, id, position, rotation), duration_(duration) { -} - -void BulletHole::Render() { - SetTransformation(position_, rotation_, glm::vec2{1.0f}); - SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); - SetTexture("../../textures/bullet_hole.png"); - DrawModel(0); -} - -void BulletHole::Update() { - duration_--; - if (duration_ <= 0) { - game_core_->PushEventRemoveParticle(id_); - } -} -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/bullet_hole.h b/src/battle_game/core/units/particles/bullet_hole.h deleted file mode 100644 index efca6853..00000000 --- a/src/battle_game/core/units/particles/bullet_hole.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "battle_game/core/particle.h" - -namespace battle_game::particle { -class BulletHole : public Particle { - public: - BulletHole(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration); - void Render() override; - void Update() override; - - private: - uint32_t duration_{}; -}; -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/explosion.cpp b/src/battle_game/core/units/particles/explosion.cpp deleted file mode 100644 index ccee432d..00000000 --- a/src/battle_game/core/units/particles/explosion.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "battle_game/core/particles/explosion.h" - -#include "battle_game/core/game_core.h" - -namespace battle_game::particle { -Explosion::Explosion(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration) - : Particle(game_core, id, position, rotation), duration_(duration) { -} - -void Explosion::Render() { - SetTransformation(position_, rotation_, glm::vec2{2.0f}); - SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); - SetTexture("../../textures/explosion.png"); - DrawModel(0); -} - -void Explosion::Update() { - if (should_damage_) { - auto &units = game_core_->GetUnits(); - for (auto &unit : units) { - auto position = unit.second->GetPosition(); - if (IsInExplosion(position)) { - game_core_->PushEventDealDamage(unit.first, id_, 10.0f); - } - } - should_damage_ = false; - } - duration_--; - if (duration_ <= 0) { - game_core_->PushEventRemoveParticle(id_); - } -} - -bool Explosion::IsInExplosion(glm::vec2 position) { - position = WorldToLocal(position); - return position.x > -1.6f && position.x < 1.6f && position.y > -2.0f && - position.y < 2.0f && position.x + position.y < 3.2f && - position.y - position.x < 3.2f; -} -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/explosion.h b/src/battle_game/core/units/particles/explosion.h deleted file mode 100644 index e0194965..00000000 --- a/src/battle_game/core/units/particles/explosion.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "battle_game/core/particle.h" - -namespace battle_game::particle { -class Explosion : public Particle { - public: - Explosion(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration); - void Render() override; - void Update() override; - bool IsInExplosion(glm::vec2 position); - - private: - uint32_t duration_{}; - bool should_damage_{true}; -}; -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/particles.h b/src/battle_game/core/units/particles/particles.h deleted file mode 100644 index 71510014..00000000 --- a/src/battle_game/core/units/particles/particles.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once -#include "battle_game/core/particles/bullet_hole.h" -#include "battle_game/core/particles/explosion.h" -#include "battle_game/core/particles/smoke.h" -#include "battle_game/core/particles/thunderbolt.h" diff --git a/src/battle_game/core/units/particles/smoke.cpp b/src/battle_game/core/units/particles/smoke.cpp deleted file mode 100644 index b44f4661..00000000 --- a/src/battle_game/core/units/particles/smoke.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include "battle_game/core/particles/smoke.h" - -#include "battle_game/core/game_core.h" - -namespace battle_game::particle { -Smoke::Smoke(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - glm::vec2 v, - float size, - glm::vec4 color, - float decay_scale) - : Particle(game_core, id, position, rotation), - v_(v), - size_(size), - color_(color), - decay_scale_(decay_scale) { -} - -void Smoke::Render() { - SetTransformation(position_, rotation_, glm::vec2{size_}); - SetColor(glm::vec4{glm::vec3{1.0f}, strength_} * color_); - SetTexture("../../textures/particle2.png"); - DrawModel(); -} - -void Smoke::Update() { - position_ += v_ * kSecondPerTick; - strength_ -= kSecondPerTick * decay_scale_; - if (strength_ < 0.0f) { - game_core_->PushEventRemoveParticle(id_); - } -} -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/smoke.h b/src/battle_game/core/units/particles/smoke.h deleted file mode 100644 index a7623932..00000000 --- a/src/battle_game/core/units/particles/smoke.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "battle_game/core/particle.h" - -namespace battle_game::particle { -class Smoke : public Particle { - public: - Smoke(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - glm::vec2 v, - float size = 0.2f, - glm::vec4 color = glm::vec4{1.0f}, - float decay_scale = 1.0f); - void Render() override; - void Update() override; - - private: - glm::vec2 v_{}; - float strength_{1.0f}; - float size_{}; - float decay_scale_{}; - glm::vec4 color_{}; -}; -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/thunderbolt.cpp b/src/battle_game/core/units/particles/thunderbolt.cpp deleted file mode 100644 index b2176d6f..00000000 --- a/src/battle_game/core/units/particles/thunderbolt.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "battle_game/core/particles/thunderbolt.h" - -#include "battle_game/core/game_core.h" - -namespace battle_game::particle { -Thunderbolt::Thunderbolt(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration) - : Particle(game_core, id, position, rotation), duration_(duration) { -} - -void Thunderbolt::Render() { - SetTransformation(position_, rotation_, glm::vec2{1.5f}); - SetColor(glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}); - SetTexture("../../textures/thunderbolt.png"); - DrawModel(0); -} - -void Thunderbolt::Update() { - duration_--; - if (duration_ <= 0) { - game_core_->PushEventRemoveParticle(id_); - } -} -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/particles/thunderbolt.h b/src/battle_game/core/units/particles/thunderbolt.h deleted file mode 100644 index d2679e62..00000000 --- a/src/battle_game/core/units/particles/thunderbolt.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "battle_game/core/particle.h" - -namespace battle_game::particle { -class Thunderbolt : public Particle { - public: - Thunderbolt(GameCore *game_core, - uint32_t id, - glm::vec2 position, - float rotation, - uint32_t duration); - void Render() override; - void Update() override; - - private: - uint32_t duration_{}; -}; -} // namespace battle_game::particle diff --git a/src/battle_game/core/units/strange_tank.cpp b/src/battle_game/core/units/strange_tank.cpp new file mode 100644 index 00000000..9cd8a293 --- /dev/null +++ b/src/battle_game/core/units/strange_tank.cpp @@ -0,0 +1,166 @@ +#include "strange_tank.h" #include "tiny_tank.h" + +#include "battle_game/core/bullets/bullets.h" +#include "battle_game/core/game_core.h" +#include "battle_game/graphics/graphics.h" + +namespace battle_game::unit { + +namespace { +uint32_t tank_body_model_index = 0xffffffffu; +uint32_t tank_turret_model_index = 0xffffffffu; +} // namespace + +StrangeTank::StrangeTank(GameCore *game_core, uint32_t id, uint32_t player_id) + : Unit(game_core, id, player_id) { + if (!~tank_body_model_index) { + auto mgr = AssetsManager::GetInstance(); + { + /* Tank Body */ + tank_body_model_index = mgr->RegisterModel( + { + {{-0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, 0.8f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{0.8f, -1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + // distinguish front and back + {{0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + {{-0.6f, 1.0f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, + }, + {0, 1, 2, 1, 2, 3, 0, 2, 5, 2, 4, 5}); + } + + { + /* Tank Turret */ + std::vector turret_vertices; + std::vector turret_indices; + const int precision = 60; + const float inv_precision = 1.0f / float(precision); + for (int i = 0; i < precision; i++) { + auto theta = (float(i) + 0.5f) * inv_precision; + theta *= glm::pi() * 2.0f; + auto sin_theta = std::sin(theta); + auto cos_theta = std::cos(theta); + turret_vertices.push_back({{sin_theta * 0.5f, cos_theta * 0.5f}, + {0.0f, 0.0f}, + {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(i); + turret_indices.push_back((i + 1) % precision); + turret_indices.push_back(precision); + } + turret_vertices.push_back( + {{0.0f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 0.0f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{-0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_vertices.push_back( + {{0.1f, 1.2f}, {0.0f, 0.0f}, {0.7f, 0.7f, 0.7f, 1.0f}}); + turret_indices.push_back(precision + 1 + 0); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 1); + turret_indices.push_back(precision + 1 + 2); + turret_indices.push_back(precision + 1 + 3); + tank_turret_model_index = + mgr->RegisterModel(turret_vertices, turret_indices); + } + } +} + +void StrangeTank::Render() { + battle_game::SetTransformation(position_, rotation_); + battle_game::SetTexture(0); + battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); + battle_game::DrawModel(tank_body_model_index); + battle_game::SetRotation(turret_rotation_); + battle_game::DrawModel(tank_turret_model_index); +} + +void StrangeTank::Update() { + TankMove(3.0f, glm::radians(180.0f)); + TurretRotate(); + Fire(); +} + +void StrangeTank::TankMove(float move_speed, float rotate_angular_speed) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + glm::vec2 offset{0.0f}; + if (input_data.key_down[GLFW_KEY_W]) { + offset.y += 1.0f; + } + if (input_data.key_down[GLFW_KEY_S]) { + offset.y -= 1.0f; + } + float speed = move_speed * GetSpeedScale(); + offset *= kSecondPerTick * speed; + auto new_position = + position_ + glm::vec2{glm::rotate(glm::mat4{1.0f}, rotation_, + glm::vec3{0.0f, 0.0f, 1.0f}) * + glm::vec4{offset, 0.0f, 0.0f}}; + if (!game_core_->IsBlockedByObstacles(new_position)) { + game_core_->PushEventMoveUnit(id_, new_position); + } + float rotation_offset = 0.0f; + if (input_data.key_down[GLFW_KEY_A]) { + rotation_offset += 1.0f; + } + if (input_data.key_down[GLFW_KEY_D]) { + rotation_offset -= 1.0f; + } + rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); + game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); + } +} + +void StrangeTank::TurretRotate() { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + auto diff = input_data.mouse_cursor_position - position_; + if (glm::length(diff) < 1e-4) { + turret_rotation_ = rotation_; + } else { + turret_rotation_ = std::atan2(diff.y, diff.x) - glm::radians(90.0f); + } + } +} + +void StrangeTank::Fire() { + if (fire_count_down_ == 0) { + auto player = game_core_->GetPlayer(player_id_); + if (player) { + auto &input_data = player->GetInputData(); + if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) { + auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_); + GenerateBullet( + position_ + Rotate({0.0f, 1.2f}, turret_rotation_), + turret_rotation_, GetDamageScale(), velocity); + fire_count_down_ = kTickPerSecond; // Fire interval 1 second. + } + } + } + if (fire_count_down_) { + fire_count_down_--; + } +} + +bool StrangeTank::IsHit(glm::vec2 position) const { + position = WorldToLocal(position); + return position.x > -0.8f && position.x < 0.8f && position.y > -1.0f && + position.y < 1.0f && position.x + position.y < 1.6f && + position.y - position.x < 1.6f; +} + +const char *StrangeTank::UnitName() const { + return "Strange Tank"; +} + +const char *StrangeTank::Author() const { + return "Joker zhs"; +} +} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/strange_tank.h b/src/battle_game/core/units/strange_tank.h new file mode 100644 index 00000000..cab2e3ae --- /dev/null +++ b/src/battle_game/core/units/strange_tank.h @@ -0,0 +1,24 @@ +#pragma once +#include "battle_game/core/unit.h" + +namespace battle_game::unit { +class StrangeTank : public Unit { + public: + StrangeTank(GameCore *game_core, uint32_t id, uint32_t player_id); + void Render() override; + void Update() override; + [[nodiscard]] bool IsHit(glm::vec2 position) const override; + + protected: + void TankMove(float move_speed, float rotate_angular_speed); + void TurretRotate(); + void Fire(); + + [[nodiscard]] const char *UnitName() const override; + [[nodiscard]] const char *Author() const override; + + float turret_rotation_{0.0f}; + uint32_t fire_count_down_{0}; + uint32_t mine_count_down_{0}; +}; +} // namespace battle_game::unit \ No newline at end of file diff --git a/src/battle_game/core/units/units.h b/src/battle_game/core/units/units.h index 8e403cb7..01a04b52 100644 --- a/src/battle_game/core/units/units.h +++ b/src/battle_game/core/units/units.h @@ -3,6 +3,7 @@ #include "battle_game/core/units/Whaooooo_tank.h" #include "battle_game/core/units/crit_tank.h" #include "battle_game/core/units/dark_fury.h" +#include "battle_game/core/units/strange_tank.h" #include "battle_game/core/units/double_scatter_tank.h" #include "battle_game/core/units/inferno_tank.h" #include "battle_game/core/units/joker_tank.h" From a4eed7e66216097a60b855da4fb27409a05be51a Mon Sep 17 00:00:00 2001 From: 971383129 <113427413+971383129@users.noreply.github.com> Date: Mon, 27 Feb 2023 21:37:22 +0800 Subject: [PATCH 13/13] --- src/battle_game/core/units/strange_tank.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/battle_game/core/units/strange_tank.cpp b/src/battle_game/core/units/strange_tank.cpp index 9cd8a293..70150f58 100644 --- a/src/battle_game/core/units/strange_tank.cpp +++ b/src/battle_game/core/units/strange_tank.cpp @@ -159,7 +159,6 @@ bool StrangeTank::IsHit(glm::vec2 position) const { const char *StrangeTank::UnitName() const { return "Strange Tank"; } - const char *StrangeTank::Author() const { return "Joker zhs"; }