forked from Yao-class-cpp-studio/battle_game_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gold(In fact, Brown) Hopper: A Special Enemy
- Loading branch information
Showing
14 changed files
with
264 additions
and
7 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#pragma once | ||
#include "battle_game/core/bullets/cannon_ball.h" | ||
#include "battle_game/core/bullets/killer_ball.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "battle_game/core/bullets/killer_ball.h" | ||
|
||
#include "battle_game/core/game_core.h" | ||
#include "battle_game/core/particles/particles.h" | ||
|
||
namespace battle_game::bullet { | ||
KillerBall::KillerBall(GameCore *core, | ||
uint32_t id, | ||
uint32_t unit_id, | ||
uint32_t player_id, | ||
glm::vec2 position, | ||
float rotation, | ||
float damage_scale) | ||
: Bullet(core, id, unit_id, player_id, position, rotation, damage_scale), | ||
death_count_down_(kTickPerSecond*game_core_->RandomInt(5,20)) { | ||
} | ||
|
||
void KillerBall::Render() { | ||
SetTransformation(position_, rotation_, glm::vec2{0.1f}); | ||
SetColor(game_core_->GetPlayerColor(player_id_)); | ||
SetTexture(BATTLE_GAME_ASSETS_DIR "textures/killerball.png"); | ||
DrawModel(0); | ||
} | ||
|
||
void KillerBall::Update() { | ||
if (!death_count_down_) { | ||
game_core_->PushEventRemoveBullet(id_); | ||
return; | ||
} | ||
death_count_down_--; | ||
|
||
auto &units = game_core_->GetUnits(); | ||
for (auto &unit : units) { | ||
if (unit.first == unit_id_) { | ||
continue; | ||
} | ||
if (unit.second->IsHit(position_)) { | ||
game_core_->PushEventDealDamage2(unit.first, id_, unit_id_, damage_scale_ * game_core_->RandomFloat() / 2); | ||
} | ||
} | ||
} | ||
|
||
KillerBall::~KillerBall() { | ||
for (int i = 0; i < 5; i++) { | ||
game_core_->PushEventGenerateParticle<particle::Smoke>( | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
#include "battle_game/core/bullet.h" | ||
|
||
namespace battle_game::bullet { | ||
class KillerBall : public Bullet { | ||
public: | ||
KillerBall(GameCore *core, | ||
uint32_t id, | ||
uint32_t unit_id, | ||
uint32_t player_id, | ||
glm::vec2 position, | ||
float rotation, | ||
float damage_scale); | ||
~KillerBall() override; | ||
void Render() override; | ||
void Update() override; | ||
|
||
private: | ||
int death_count_down_; | ||
}; | ||
} // namespace battle_game::bullet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include "goldhopper.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 hopper_model_index = 0xffffffffu; | ||
} // namespace | ||
|
||
GoldHopper::GoldHopper(GameCore *game_core, uint32_t id, uint32_t player_id) | ||
: Unit(game_core, id, player_id) { | ||
if (!~hopper_model_index) { | ||
auto mgr = AssetsManager::GetInstance(); | ||
std::vector<ObjectVertex> vertices; | ||
std::vector<uint32_t> 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<float>() * 2.0f; | ||
auto sin_theta = std::sin(theta); | ||
auto cos_theta = std::cos(theta); | ||
vertices.push_back({{sin_theta * 0.5f, cos_theta * 0.5f}, | ||
{0.0f, 0.0f}, | ||
{0.5f, 0.5f, 0.0f, 1.0f}}); | ||
indices.push_back(i); | ||
indices.push_back((i + 1) % precision); | ||
indices.push_back(precision); | ||
} | ||
vertices.push_back({{0.0f, 0.0f}, {0.0f, 0.0f}, {0.5f, 0.5f, 0.0f, 1.0f}}); | ||
vertices.push_back({{0.1f, 0.0f}, {0.0f, 0.0f}, {0.5f, 0.5f, 0.0f, 1.0f}}); | ||
vertices.push_back({{-0.1f, 0.0f}, {0.0f, 0.0f}, {0.5f, 0.5f, 0.0f, 1.0f}}); | ||
vertices.push_back({{0.0f, 0.7f}, {0.0f, 0.0f}, {0.5f, 0.5f, 0.0f, 1.0f}}); | ||
indices.push_back(precision+1); | ||
indices.push_back(precision+2); | ||
indices.push_back(precision+3); | ||
hopper_model_index = mgr->RegisterModel(vertices, indices); | ||
} | ||
OldHealth=GetHealth(); | ||
} | ||
|
||
void GoldHopper::Render() { | ||
battle_game::SetTransformation(position_, rotation_); | ||
battle_game::SetTexture(0); | ||
battle_game::SetColor({1,0.5,0,1}); | ||
battle_game::DrawModel(hopper_model_index); | ||
} | ||
|
||
void GoldHopper::RandomRun() | ||
{ | ||
if(!move_count_down_)return; | ||
float move_speed = (float) move_count_down_ / kTickPerSecond; | ||
float rotate_angular_speed = glm::radians(180.0f) * ((float)move_count_down_ / kTickPerSecond / 2); | ||
move_count_down_--; | ||
auto player = game_core_->GetPlayer(player_id_); | ||
if (player) { | ||
glm::vec2 offset{game_core_->RandomFloat()/2+0.5f}; | ||
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 = std::clamp(game_core_->RandomFloat()*3.0f-1.5f,-1.0f,1.0f); | ||
rotation_offset *= kSecondPerTick * rotate_angular_speed * GetSpeedScale(); | ||
game_core_->PushEventRotateUnit(id_, rotation_ + rotation_offset); | ||
} | ||
} | ||
|
||
void GoldHopper::Bang() | ||
{ | ||
if(!bang_count_down_)return; | ||
bang_count_down_--; | ||
if(health_>=0.4)return; | ||
float t = 0.4 / health_ * bang_count_down_ / kTickPerSecond / 10000; | ||
if(game_core_->RandomFloat() < t) | ||
{ | ||
game_core_->PushEventDealDamage(id_,id_,2*GetMaxHealth()); | ||
GenerateBullet<bullet::KillerBall>(position_, 0, GetDamageScale()); | ||
} | ||
} | ||
|
||
void GoldHopper::Update() | ||
{ | ||
if(OldHealth>GetHealth()+0.0001) | ||
{ | ||
uint32_t c=20*kTickPerSecond*(OldHealth-GetHealth()); | ||
bang_count_down_+=c,move_count_down_+=c; | ||
} | ||
OldHealth=GetHealth(); | ||
RandomRun(); | ||
Bang(); | ||
} | ||
|
||
bool GoldHopper::IsHit(glm::vec2 position) const { | ||
position = WorldToLocal(position); | ||
return position.x*position.x+position.y*position.y<0.25||(position.y>0&&7*fabs(position.x)+position.y<0.7); | ||
} | ||
|
||
float GoldHopper::GetDamageScale() const { | ||
return 3.0f; | ||
} | ||
|
||
float GoldHopper::GetSpeedScale() const { | ||
return 3.5f; | ||
} | ||
|
||
float GoldHopper::BasicMaxHealth() const { | ||
return 25.0f; | ||
} | ||
|
||
const char *GoldHopper::UnitName() const { | ||
return "GoldHopper"; | ||
} | ||
|
||
const char *GoldHopper::Author() const { | ||
return "MyeeYe"; | ||
} | ||
} // namespace battle_game::unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
#include "battle_game/core/unit.h" | ||
|
||
namespace battle_game::unit { | ||
class GoldHopper : public Unit { | ||
public: | ||
GoldHopper(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 RandomRun(); | ||
void Bang(); | ||
[[nodiscard]] float GetDamageScale() const override; | ||
[[nodiscard]] float GetSpeedScale() const override; | ||
[[nodiscard]] float BasicMaxHealth() const override; | ||
[[nodiscard]] const char *UnitName() const override; | ||
[[nodiscard]] const char *Author() const override; | ||
uint32_t move_count_down_{0}; | ||
uint32_t bang_count_down_{0}; | ||
float OldHealth{0}; | ||
}; | ||
} // namespace battle_game::unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters