Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotate with keyboard #493

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/textures/quicksilver.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/battle_game/core/bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ Bullet::Bullet(GameCore *core,
uint32_t player_id,
glm::vec2 position,
float rotation,
float damage_scale)
float damage_scale,
std::string type)
: Object(core, id, position, rotation),
unit_id_(unit_id),
player_id_(player_id),
damage_scale_(damage_scale) {
damage_scale_(damage_scale),
type_(type) {
}

Bullet::~Bullet() = default;

} // namespace battle_game
4 changes: 3 additions & 1 deletion src/battle_game/core/bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Bullet : public Object {
uint32_t player_id,
glm::vec2 position,
float rotation,
float damage_scale);
float damage_scale,
std::string type = "");
~Bullet() override;
std::string type_{};

protected:
uint32_t unit_id_{};
Expand Down
9 changes: 8 additions & 1 deletion src/battle_game/core/bullets/cannon_ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ CannonBall::CannonBall(GameCore *core,
float rotation,
float damage_scale,
glm::vec2 velocity)
: Bullet(core, id, unit_id, player_id, position, rotation, damage_scale),
: Bullet(core,
id,
unit_id,
player_id,
position,
rotation,
damage_scale,
"Cannonball"),
velocity_(velocity) {
}

Expand Down
9 changes: 8 additions & 1 deletion src/battle_game/core/bullets/rocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ Rocket::Rocket(GameCore *core,
float rotation,
float damage_scale,
glm::vec2 velocity)
: Bullet(core, id, unit_id, player_id, position, rotation, damage_scale),
: Bullet(core,
id,
unit_id,
player_id,
position,
rotation,
damage_scale,
"Rocket"),
velocity_(velocity) {
auto &units = game_core_->GetUnits();
auto player = game_core_->GetPlayer(player_id_);
Expand Down
1 change: 1 addition & 0 deletions src/battle_game/core/game_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ int GameCore::RandomInt(int low_bound, int high_bound) {
void GameCore::SetScene() {
AddObstacle<obstacle::Block>(glm::vec2{-3.0f, 4.0f});
AddObstacle<obstacle::River>(glm::vec2{3.0f, 0.0f});
AddObstacle<obstacle::Wood>(glm::vec2{0.0f, -4.0f});
AddObstacle<obstacle::ReboundingBlock>(glm::vec2{-10.0f, -10.0f},
0.78539816339744830961566084581988f);
AddObstacle<obstacle::ReboundingBlock>(glm::vec2{10.0f, -10.0f},
Expand Down
1 change: 1 addition & 0 deletions src/battle_game/core/obstacles/obstacles.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
#include "battle_game/core/obstacles/rebounding_block.h"
#include "battle_game/core/obstacles/river.h"
#include "battle_game/core/obstacles/safety_declaration.h"
#include "battle_game/core/obstacles/wood.h"
49 changes: 49 additions & 0 deletions src/battle_game/core/obstacles/wood.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "battle_game/core/obstacles/wood.h"

#include "battle_game/core/bullets/bullets.h"
#include "battle_game/core/game_core.h"

namespace battle_game::obstacle {

Wood::Wood(GameCore *game_core,
uint32_t id,
glm::vec2 position,
float rotation,
glm::vec2 scale)
: Obstacle(game_core, id, position, rotation) {
}

Wood::~Wood() {
for (int i = 0; i < 5; i++) {
game_core_->PushEventGenerateParticle<particle::Smoke>(
position_, rotation_, game_core_->RandomInCircle() * 2.0f, 0.5f,
glm::vec4{0.4f, 0.2f, 0.0f, 4.0f}, 3.0f);
}
}

bool Wood::IsBlocked(glm::vec2 p) const {
auto pl = WorldToLocal(p);
if (pl.x <= scale_.x && pl.x >= -scale_.x && pl.y <= scale_.y &&
pl.y >= -scale_.y) {
for (auto &bullet : game_core_->GetBullets()) {
std::string temp = bullet.second->type_;
if (bullet.second->GetPosition() == p && temp == "Cannonball") {
game_core_->PushEventRemoveObstacle(id_);
}
if (bullet.second->GetPosition() == p && temp == "Rocket") {
game_core_->AddObstacle<obstacle::Block>(position_, rotation_);
game_core_->PushEventRemoveObstacle(id_);
}
}
return true;
}
return false;
}

void Wood::Render() {
battle_game::SetColor(glm::vec4{0.8f, 0.4f, 0.0f, 1.0f});
battle_game::SetTexture(0);
battle_game::SetTransformation(position_, rotation_, scale_);
battle_game::DrawModel(0);
}
} // namespace battle_game::obstacle
21 changes: 21 additions & 0 deletions src/battle_game/core/obstacles/wood.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
#include "battle_game/core/obstacle.h"

namespace battle_game::obstacle {
class Wood : public Obstacle {
public:
Wood(GameCore *game_core,
uint32_t id,
glm::vec2 position,
float rotation = 0.0f,
glm::vec2 scale = glm::vec2{1.0f, 1.0f});
~Wood();

private:
[[nodiscard]] bool IsBlocked(glm::vec2 p) const override;
void Render() override;
glm::vec2 scale_{1.0f};
bool hit_by_cannonball_{false};
bool hit_by_rocket_{false};
};
} // namespace battle_game::obstacle
1 change: 1 addition & 0 deletions src/battle_game/core/selectable_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void GameCore::GeneratePrimaryUnitList() {
ADD_SELECTABLE_UNIT(unit::CritTank);
ADD_SELECTABLE_UNIT(unit::Railgun);
ADD_SELECTABLE_UNIT(unit::Udongein);
ADD_SELECTABLE_UNIT(unit::QuicksilverTank);

unit.reset();
}
Expand Down
113 changes: 113 additions & 0 deletions src/battle_game/core/units/quicksilver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "quicksilver.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 {
QuicksilverTank::QuicksilverTank(GameCore *game_core,
uint32_t id,
uint32_t player_id)
: Tank(game_core, id, player_id) {
Skill temp;
temp.name = "Quicksilver";
temp.description = "Sacrificing attacking ability for 5 time speed";
temp.time_remain = 0;
temp.time_total = 420;
temp.type = E;
temp.function = SKILL_ADD_FUNCTION(QuicksilverTank::Quicksilver);
skills_.push_back(temp);
}

void QuicksilverTank::Render() {
if (is_accelerate_ == false)
Tank::Render();
else {
SetTransformation(position_, rotation_, glm::vec2{1.2f});
SetColor(game_core_->GetPlayerColor(player_id_));
SetTexture("../../textures/quicksilver.png");
DrawModel(0);
}
}

void QuicksilverTank::Update() {
Move(3.0f, glm::radians(180.0f));
TurretRotate();
Fire();
Quicksilver();
}

void QuicksilverTank::Move(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();
if (is_accelerate_ == true) {
speed = speed * 5;
accelerate_time_ += 1;
}
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);
if (input_data.key_down[GLFW_KEY_E] && accelerate_count_down_ == 0) {
is_accelerate_ = true;
accelerate_time_ = 0;
}
if (accelerate_time_ == 240)
is_accelerate_ = false;
}
}

void QuicksilverTank::Fire() {
if (is_accelerate_ == false)
Tank::Fire();
}

void QuicksilverTank::Quicksilver() {
skills_[0].time_remain = accelerate_count_down_;
if (accelerate_count_down_) {
accelerate_count_down_--;
} else {
auto player = game_core_->GetPlayer(player_id_);
if (player) {
auto &input_data = player->GetInputData();
if (input_data.key_down[GLFW_KEY_E]) {
accelerate_count_down_ = 420;
}
}
}
}

bool QuicksilverTank::IsHit(glm::vec2 position) const {
return Tank::IsHit(position);
}

const char *QuicksilverTank::UnitName() const {
return "Quicksilver";
}

const char *QuicksilverTank::Author() const {
return "YYR";
}
} // namespace battle_game::unit
25 changes: 25 additions & 0 deletions src/battle_game/core/units/quicksilver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "battle_game/core/unit.h"
#include "battle_game/core/units/tiny_tank.h"

namespace battle_game::unit {
class QuicksilverTank : public Tank {
public:
QuicksilverTank(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 Move(float move_speed, float rotate_angular_speed);
void Fire();
void Quicksilver();
[[nodiscard]] const char *UnitName() const override;
[[nodiscard]] const char *Author() const override;

uint32_t fire_count_down_{0};
bool is_accelerate_{false};
int accelerate_time_{0};
int accelerate_count_down_{0};
};
} // namespace battle_game::unit
16 changes: 10 additions & 6 deletions src/battle_game/core/units/tiny_tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ 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);
float rotation_offset = 0.0f;
if (input_data.key_down[GLFW_KEY_LEFT]) {
rotation_offset += 1.0f;
}
if (input_data.key_down[GLFW_KEY_RIGHT]) {
rotation_offset -= 1.0f;
}
rotation_offset *=
kSecondPerTick * glm::radians(180.0f) * GetSpeedScale() * 0.5f;
turret_rotation_ += rotation_offset;
}
}

Expand All @@ -135,7 +139,7 @@ void Tank::Fire() {
auto player = game_core_->GetPlayer(player_id_);
if (player) {
auto &input_data = player->GetInputData();
if (input_data.mouse_button_down[GLFW_MOUSE_BUTTON_LEFT]) {
if (input_data.key_down[GLFW_KEY_UP]) {
auto velocity = Rotate(glm::vec2{0.0f, 20.0f}, turret_rotation_);
GenerateBullet<bullet::CannonBall>(
position_ + Rotate({0.0f, 1.2f}, turret_rotation_),
Expand Down
1 change: 1 addition & 0 deletions src/battle_game/core/units/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#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"
#include "battle_game/core/units/quicksilver.h"
#include "battle_game/core/units/rage_tank_yangyr.h"
#include "battle_game/core/units/railgun.h"
#include "battle_game/core/units/rebounding_sample_tank.h"
Expand Down