From 684583b73e1fc88af826840fd767cdf0105bec4b Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 10:07:56 +0800 Subject: [PATCH 01/14] Create 59_tank.cpp --- src/battle_game/core/units/59_tank.cpp | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/battle_game/core/units/59_tank.cpp diff --git a/src/battle_game/core/units/59_tank.cpp b/src/battle_game/core/units/59_tank.cpp new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/battle_game/core/units/59_tank.cpp @@ -0,0 +1 @@ + From 5b35092994e2097eb85a0d120e3d273c2c694211 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 10:08:34 +0800 Subject: [PATCH 02/14] Create 59_tank.h --- src/battle_game/core/units/59_tank.h | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/battle_game/core/units/59_tank.h diff --git a/src/battle_game/core/units/59_tank.h b/src/battle_game/core/units/59_tank.h new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/battle_game/core/units/59_tank.h @@ -0,0 +1 @@ + From 5e32cde22ddcf74b13ad3b13913374480ca7c646 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 10:10:16 +0800 Subject: [PATCH 03/14] Update 59_tank.h --- src/battle_game/core/units/59_tank.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/battle_game/core/units/59_tank.h b/src/battle_game/core/units/59_tank.h index 8b137891..e9bff55e 100644 --- a/src/battle_game/core/units/59_tank.h +++ b/src/battle_game/core/units/59_tank.h @@ -1 +1,23 @@ +#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(); + [[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}; +}; +} From 6db7fc5f1c5370149d316c5a52ce0b5a90d2ef25 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 10:10:43 +0800 Subject: [PATCH 04/14] Update 59_tank.cpp --- src/battle_game/core/units/59_tank.cpp | 165 +++++++++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/src/battle_game/core/units/59_tank.cpp b/src/battle_game/core/units/59_tank.cpp index 8b137891..92ac606b 100644 --- a/src/battle_game/core/units/59_tank.cpp +++ b/src/battle_game/core/units/59_tank.cpp @@ -1 +1,166 @@ +#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 + +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); + } + } +} + +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 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"; +} +} From bd76354f23a116f206f19e213ddb9c4708ba41cd Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 22:54:34 +0800 Subject: [PATCH 05/14] Update 59_tank.cpp --- src/battle_game/core/units/59_tank.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/battle_game/core/units/59_tank.cpp b/src/battle_game/core/units/59_tank.cpp index 92ac606b..fde0ab77 100644 --- a/src/battle_game/core/units/59_tank.cpp +++ b/src/battle_game/core/units/59_tank.cpp @@ -1,4 +1,4 @@ -#include "tiny_tank.h" +#include "59_tank.h" #include "battle_game/core/bullets/bullets.h" #include "battle_game/core/game_core.h" @@ -11,7 +11,7 @@ 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) +wu_Tank::wu_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(); @@ -70,7 +70,7 @@ Tank::Tank(GameCore *game_core, uint32_t id, uint32_t player_id) } } -void Tank::Render() { +void wu_Tank::Render() { battle_game::SetTransformation(position_, rotation_); battle_game::SetTexture(0); battle_game::SetColor(game_core_->GetPlayerColor(player_id_)); @@ -79,13 +79,13 @@ void Tank::Render() { battle_game::DrawModel(tank_turret_model_index); } -void Tank::Update() { +void wu_Tank::Update() { TankMove(3.0f, glm::radians(180.0f)); TurretRotate(); Fire(); } -void Tank::TankMove(float move_speed, float rotate_angular_speed) { +void wu_Tank::TankMove(float move_speed, float rotate_angular_speed) { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -117,7 +117,7 @@ void Tank::TankMove(float move_speed, float rotate_angular_speed) { } } -void Tank::TurretRotate() { +void wu_Tank::TurretRotate() { auto player = game_core_->GetPlayer(player_id_); if (player) { auto &input_data = player->GetInputData(); @@ -130,7 +130,7 @@ void Tank::TurretRotate() { } } -void Tank::Fire() { +void wu_Tank::Fire() { if (fire_count_down_ == 0) { auto player = game_core_->GetPlayer(player_id_); if (player) { @@ -149,18 +149,18 @@ void Tank::Fire() { } } -bool Tank::IsHit(glm::vec2 position) const { +bool wu_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 { +const char *wu_Tank::UnitName() const { return "Tiny Tank"; } -const char *Tank::Author() const { +const char *wu_Tank::Author() const { return "LazyJazz"; } } From a2e3f85f629c9791cba880d4b8bf4534f715610b Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 22:55:06 +0800 Subject: [PATCH 06/14] Update 59_tank.h --- src/battle_game/core/units/59_tank.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/battle_game/core/units/59_tank.h b/src/battle_game/core/units/59_tank.h index e9bff55e..5d81e61d 100644 --- a/src/battle_game/core/units/59_tank.h +++ b/src/battle_game/core/units/59_tank.h @@ -2,9 +2,9 @@ #include "battle_game/core/unit.h" namespace battle_game::unit { -class Tank : public Unit { +class wu_Tank : public Unit { public: - Tank(GameCore *game_core, uint32_t id, uint32_t player_id); + wu_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; From 88b305ed136ce9f7336d52a66f121715023b9100 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Sun, 12 Jan 2025 22:56:03 +0800 Subject: [PATCH 07/14] Update 59_tank.cpp --- src/battle_game/core/units/59_tank.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/battle_game/core/units/59_tank.cpp b/src/battle_game/core/units/59_tank.cpp index fde0ab77..e2589664 100644 --- a/src/battle_game/core/units/59_tank.cpp +++ b/src/battle_game/core/units/59_tank.cpp @@ -157,10 +157,10 @@ bool wu_Tank::IsHit(glm::vec2 position) const { } const char *wu_Tank::UnitName() const { - return "Tiny Tank"; + return "Wu Tank"; } const char *wu_Tank::Author() const { - return "LazyJazz"; + return "Xingyu Wu"; } } From f726eb19add4333dcc83ceefed453329d35039bc Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Mon, 13 Jan 2025 10:47:37 +0800 Subject: [PATCH 08/14] Update selectable_units.cpp --- src/battle_game/core/selectable_units.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/battle_game/core/selectable_units.cpp b/src/battle_game/core/selectable_units.cpp index d3c63f15..230e6a0f 100644 --- a/src/battle_game/core/selectable_units.cpp +++ b/src/battle_game/core/selectable_units.cpp @@ -22,6 +22,7 @@ void GameCore::GeneratePrimaryUnitList() { * TODO: Add Your Unit Here! * */ ADD_SELECTABLE_UNIT(unit::Tank); + ADD_SELECTABLE_UNIT(unit::wu_Tank); unit.reset(); } From a5bc4f9c9d88be8fe7f4b1fae0706d9bcc85d16d Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 17:51:28 +0800 Subject: [PATCH 09/14] Update game_core.cpp --- src/battle_game/core/game_core.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index 53f1acb7..e177a8ee 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -220,10 +220,13 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, float damage) { event_queue_.emplace([=]() { auto unit = GetUnit(dst_unit_id); + auto unit_2 = GetUnit(src_unit_id); if (unit) { unit->SetHealth(unit->GetHealth() - damage / unit->GetMaxHealth()); + unit_2->SetHealth(unit_2->GetHealth() + damage / unit_2->GetMaxHealth()); if (unit->GetHealth() <= 0.0f) { PushEventKillUnit(dst_unit_id, src_unit_id); + unit_2->SetHealth(unit_2->GetMaxHealth()); } } }); From 84b9fbcfd34cb3fa70618ac3cb97048e38deeb8a Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 18:40:08 +0800 Subject: [PATCH 10/14] Update game_core.cpp --- src/battle_game/core/game_core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index e177a8ee..d91b4b16 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -223,7 +223,7 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, auto unit_2 = GetUnit(src_unit_id); if (unit) { unit->SetHealth(unit->GetHealth() - damage / unit->GetMaxHealth()); - unit_2->SetHealth(unit_2->GetHealth() + damage / unit_2->GetMaxHealth()); + unit_2->SetHealth(unit_2->GetHealth() - 0.5 * damage / unit_2->GetMaxHealth()); if (unit->GetHealth() <= 0.0f) { PushEventKillUnit(dst_unit_id, src_unit_id); unit_2->SetHealth(unit_2->GetMaxHealth()); From fe910bb325fce1e5fb3d7b0fced037ebc0a2ea7d Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 18:58:50 +0800 Subject: [PATCH 11/14] Update game_core.cpp --- src/battle_game/core/game_core.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index d91b4b16..79fb31df 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -226,7 +226,7 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, unit_2->SetHealth(unit_2->GetHealth() - 0.5 * damage / unit_2->GetMaxHealth()); if (unit->GetHealth() <= 0.0f) { PushEventKillUnit(dst_unit_id, src_unit_id); - unit_2->SetHealth(unit_2->GetMaxHealth()); + unit_2->SetHealth(1.0f); } } }); From 540c0ae1aebcc870f3b10696c03033384664cfd8 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 21:58:58 +0800 Subject: [PATCH 12/14] Update game_core.cpp --- src/battle_game/core/game_core.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index 79fb31df..e7843db4 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -220,10 +220,8 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, float damage) { event_queue_.emplace([=]() { auto unit = GetUnit(dst_unit_id); - auto unit_2 = GetUnit(src_unit_id); if (unit) { unit->SetHealth(unit->GetHealth() - damage / unit->GetMaxHealth()); - unit_2->SetHealth(unit_2->GetHealth() - 0.5 * damage / unit_2->GetMaxHealth()); if (unit->GetHealth() <= 0.0f) { PushEventKillUnit(dst_unit_id, src_unit_id); unit_2->SetHealth(1.0f); From 966c09d645c75fa29aa61cd528c7f25479847289 Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 21:59:14 +0800 Subject: [PATCH 13/14] Update game_core.cpp --- src/battle_game/core/game_core.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/battle_game/core/game_core.cpp b/src/battle_game/core/game_core.cpp index e7843db4..53f1acb7 100644 --- a/src/battle_game/core/game_core.cpp +++ b/src/battle_game/core/game_core.cpp @@ -224,7 +224,6 @@ void GameCore::PushEventDealDamage(uint32_t dst_unit_id, unit->SetHealth(unit->GetHealth() - damage / unit->GetMaxHealth()); if (unit->GetHealth() <= 0.0f) { PushEventKillUnit(dst_unit_id, src_unit_id); - unit_2->SetHealth(1.0f); } } }); From db67fecfd62aa757d2f23dcaaa71efe7e7c14c4d Mon Sep 17 00:00:00 2001 From: wuxingyu24 Date: Thu, 16 Jan 2025 23:52:12 +0800 Subject: [PATCH 14/14] Update cannon_ball.cpp --- src/battle_game/core/bullets/cannon_ball.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/battle_game/core/bullets/cannon_ball.cpp b/src/battle_game/core/bullets/cannon_ball.cpp index 2ea94a5b..e39dd4e1 100644 --- a/src/battle_game/core/bullets/cannon_ball.cpp +++ b/src/battle_game/core/bullets/cannon_ball.cpp @@ -33,6 +33,7 @@ void CannonBall::Update() { auto &units = game_core_->GetUnits(); for (auto &unit : units) { if (unit.first == unit_id_) { + game_core_->PushEventDealDamage(unit.first, id_, damage_scale_ * 0.2f); continue; } if (unit.second->IsHit(position_)) {