diff --git a/assets/exit_btn.png b/assets/exit_btn.png new file mode 100644 index 0000000..f1f79d0 Binary files /dev/null and b/assets/exit_btn.png differ diff --git a/assets/start_btn.png b/assets/start_btn.png new file mode 100644 index 0000000..bed33f8 Binary files /dev/null and b/assets/start_btn.png differ diff --git a/include/game/component.hpp b/include/game/component.hpp index b60f5b9..a22fa2a 100644 --- a/include/game/component.hpp +++ b/include/game/component.hpp @@ -70,6 +70,8 @@ public: FreeRotation = 0x02, }; + static constexpr int InfBullet = -1; + void Init(const std::string& name, Type type, BulletCmpt::Type bulletType, @@ -77,7 +79,8 @@ public: int damage, float shootSpeed, float maxSpeed, - float duration) { + float duration, + int bulletAmount = InfBullet) { this->name = name; this->type = type; this->bulletType = bulletType; @@ -87,6 +90,7 @@ public: this->shootDuration = duration; this->coolDown = 0; this->maxSpeed = maxSpeed; + this->bulletAmount = bulletAmount; } void Release() {} @@ -103,6 +107,7 @@ public: float coolDown; float maxSpeed; Entity* owner; + int bulletAmount; }; class SpaceshipArmorCmpt: public Component { diff --git a/include/game/gui.hpp b/include/game/gui.hpp new file mode 100644 index 0000000..48861a2 --- /dev/null +++ b/include/game/gui.hpp @@ -0,0 +1,5 @@ +#pragma once + +#include "tinyengine/tinyengine.hpp" + +bool Button(Texture* texture, const Point& pos, const Size& size); diff --git a/include/game/sprite.hpp b/include/game/sprite.hpp deleted file mode 100644 index ccc7b66..0000000 --- a/include/game/sprite.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include "tinyengine/pch.hpp" -#include "tinyengine/libmath.hpp" - -class Sprite { -public: - virtual ~Sprite() = default; - - void Move(const Point& offset) { position_ += offset; dirty_ = true; } - void MoveTo(const Point& p) { position_ = p; dirty_ = true; } - void Rotate(const float degreeDelta) { rotation_ += degreeDelta; dirty_ = true; } - void RotateTo(const float degree) { rotation_ = degree; dirty_ = true; } - - bool TryCalcView(); - const Mat44& GetModel() const { return modelMat_; } - - virtual void Update() { oldPosition_ = position_; } - -private: - Point position_ = {0, 0}; - Point oldPosition_; - float rotation_ = 0; - bool dirty_ = false; - Mat44 modelMat_; -}; diff --git a/include/game/stages/welcome.hpp b/include/game/stages/welcome.hpp new file mode 100644 index 0000000..ac2a39e --- /dev/null +++ b/include/game/stages/welcome.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include "tinyengine/tinyengine.hpp" + +class WelcomeScence: public Scence { +public: + void OnInit() override; + void OnRender() override; + +private: + Unique startImage_; + Unique exitImage_; + Camera camera_; +}; diff --git a/include/tinyengine/engine.hpp b/include/tinyengine/engine.hpp index 88ddb1a..ae2ad95 100644 --- a/include/tinyengine/engine.hpp +++ b/include/tinyengine/engine.hpp @@ -14,7 +14,7 @@ public: void Init(const std::string& title, const Size& size, Scence* scence); void Shutdown(); - void Exit() { shouldExit_ = true; } + void Exit() { glfwSetWindowShouldClose(window_, 1); } bool ShouldExit() const { return glfwWindowShouldClose(window_); } void Update(float deltaTime); diff --git a/src/game/action.cpp b/src/game/action.cpp index ccc269d..ac0146a 100644 --- a/src/game/action.cpp +++ b/src/game/action.cpp @@ -9,11 +9,11 @@ void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir) { Entity* bullet; bullet = weapon.ShootBullet(dir); - Bullets.Add(bullet); - - weapon.coolDown = weapon.shootDuration; - - Sounds["shoot"]->Play(); + if (bullet) { + Bullets.Add(bullet); + weapon.coolDown = weapon.shootDuration; + Sounds["shoot"]->Play(); + } } void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) { @@ -25,14 +25,12 @@ void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) { Entity* bullet; bullet = weapon.ShootMissile(dir, target); - - // FIXME the rotation have some bugs - bullet->Use()->rotation = Degrees(std::acos(-Normalize(dir).y)); - - Bullets.Add(bullet); - - weapon.coolDown = weapon.shootDuration; - - Sounds["shoot"]->Play(); + if (bullet) { + // FIXME the rotation have some bugs + bullet->Use()->rotation = Degrees(std::acos(-Normalize(dir).y)); + Bullets.Add(bullet); + weapon.coolDown = weapon.shootDuration; + Sounds["shoot"]->Play(); + } } diff --git a/src/game/component.cpp b/src/game/component.cpp index a354e64..fef8bdd 100644 --- a/src/game/component.cpp +++ b/src/game/component.cpp @@ -2,6 +2,10 @@ #include "game/entity.hpp" Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) { + if (bulletAmount == 0) return nullptr; + if (bulletAmount != InfBullet) { + bulletAmount --; + } Entity* bullet = CreateBullet(damage, owner, maxSpeed); bullet->Use()->speed = Normalize(dir) * shootSpeed; bullet->Use()->position = owner->Get()->position; @@ -9,9 +13,13 @@ Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) { } Entity* SpaceshipWeaponCmpt::ShootMissile(const Point& dir, Entity* target) { + if (bulletAmount == 0) return nullptr; + if (bulletAmount != InfBullet) { + bulletAmount --; + } Entity* bullet = CreateMissile(damage, owner, maxSpeed, target); if (target) { - bullet->Use()->rotation = Degrees(std::acos(Dot(dir, Normalize(target->Get()->position - owner->Get()->position)))); + bullet->Use()->rotation = Degrees(std::acos(Dot(dir, Normalize(target->Get()->position - owner->Get()->position)))); } bullet->Use()->speed = Normalize(dir) * shootSpeed; bullet->Use()->position = owner->Get()->position; diff --git a/src/game/entity.cpp b/src/game/entity.cpp index 0b2924b..2149e38 100644 --- a/src/game/entity.cpp +++ b/src/game/entity.cpp @@ -41,7 +41,8 @@ Entity* CreateFightShip() { LazerDamage, LazerShooterSpeed, LazerShooterMaxSpeed, - LazerShooterCooldown); + LazerShooterCooldown, + 10); entity->Add(Size{16, 16}); entity->Add(FreightLife); diff --git a/src/game/gui.cpp b/src/game/gui.cpp new file mode 100644 index 0000000..ee16175 --- /dev/null +++ b/src/game/gui.cpp @@ -0,0 +1,12 @@ +#include "game/gui.hpp" +#include "tinyengine/event.hpp" + +bool Button(Texture* texture, const Point& pos, const Size& size) { + Renderer::DrawTexture(texture, nullptr, pos, size); + if (IsLeftPressing() && + IsPointInRect(GetMousePosition(), Rect{pos.x - size.w / 2, pos.y - size.h / 2, size.w, size.h})) { + return true; + } else { + return false; + } +} diff --git a/src/game/main.cpp b/src/game/main.cpp index 8a27ade..6859ac3 100644 --- a/src/game/main.cpp +++ b/src/game/main.cpp @@ -1,6 +1,8 @@ #include "game/stages/gamelogo.hpp" #include "game/stages/space.hpp" +#include "game/stages/welcome.hpp" #include "game/constants.hpp" -RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, SpaceScence) +RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, WelcomeScence) +// RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, SpaceScence) // RUN_WINDOW("Space Sector", GameWindowSize.w, GameWindowSize.h, GameLogoScence) diff --git a/src/game/sprite.cpp b/src/game/sprite.cpp deleted file mode 100644 index 94071b0..0000000 --- a/src/game/sprite.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "game/sprite.hpp" - -bool Sprite::TryCalcView() { - if (dirty_) { - float theta = Radians(rotation_); - float sinTheta = std::sin(theta); - float cosTheta = std::cos(theta); - modelMat_ = Mat44({ - cosTheta, -sinTheta, 0, position_.x, - sinTheta, cosTheta, 0, position_.y, - 0, 0, 1, 0, - 0, 0, 0, 1, - }); - dirty_ = false; - return true; - } else { - return false; - } -} diff --git a/src/game/stages/space.cpp b/src/game/stages/space.cpp index f92d45b..6711443 100644 --- a/src/game/stages/space.cpp +++ b/src/game/stages/space.cpp @@ -98,14 +98,14 @@ void SpaceScence::renderMiniMap() { } void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmpt* weapon2) { - Rect mapRect = {0, 0, 200, 100}; - mapRect.y = GameWindowSize.h - mapRect.h; - mapRect.x = GameWindowSize.w - mapRect.w - 1; + Rect weaponInfoRect = {0, 0, 300, 100}; + weaponInfoRect.y = GameWindowSize.h - weaponInfoRect.h; + weaponInfoRect.x = GameWindowSize.w - weaponInfoRect.w - 1; - Renderer::SetDrawColor(Color{0, 0, 0, 255}); - Renderer::FillRect(mapRect); - Renderer::SetDrawColor(Color{255, 255, 255, 255}); - Renderer::DrawRect(mapRect); + Renderer::SetDrawColor(Color{0, 0, 0, 1}); + Renderer::FillRect(weaponInfoRect); + Renderer::SetDrawColor(Color{1, 1, 1, 1}); + Renderer::DrawRect(weaponInfoRect); Point offset = {10, 10}; auto& font = engine.GetInnerBmpFont(); @@ -113,15 +113,28 @@ void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmp if (weapon1) { font.Render(weapon1->name, 20, - Point{mapRect.x, mapRect.y} + offset, - Color{0, 200, 0, 255}); + Point{weaponInfoRect.x, weaponInfoRect.y} + offset, + Color{0, 0.8, 0, 1}); + if (weapon1->bulletAmount != SpaceshipWeaponCmpt::InfBullet) { + font.Render("[" + std::to_string(weapon1->bulletAmount) + "]", + 20, + Point{weaponInfoRect.x + weaponInfoRect.w - 50, weaponInfoRect.y} + offset, + Color{0, 0.8, 0, 1}); + } offset.y += 20; } if (weapon2) { font.Render(weapon2->name, 20, - Point{mapRect.x, mapRect.y} + offset, - Color{0, 50, 0, 255}); + Point{weaponInfoRect.x, weaponInfoRect.y} + offset, + Color{0, 0.8, 0, 1}); + + if (weapon2->bulletAmount != SpaceshipWeaponCmpt::InfBullet) { + font.Render("[" + std::to_string(weapon2->bulletAmount) + "]", + 20, + Point{weaponInfoRect.x + weaponInfoRect.w - 100, weaponInfoRect.y} + offset, + Color{0, 0.8, 0, 1}); + } } } diff --git a/src/game/stages/welcome.cpp b/src/game/stages/welcome.cpp new file mode 100644 index 0000000..5594c2d --- /dev/null +++ b/src/game/stages/welcome.cpp @@ -0,0 +1,32 @@ +#include "game/stages/welcome.hpp" +#include "game/gui.hpp" +#include "game/stages/space.hpp" + +void WelcomeScence::OnInit() { + startImage_.reset(new Texture("assets/start_btn.png")); + exitImage_.reset(new Texture("assets/exit_btn.png")); +} + +void WelcomeScence::OnRender() { + Renderer::SetCamera(camera_); + auto& font = engine.GetInnerBmpFont(); + std::string title = "SPACE SECTOR"; + int pt = 70; + font.Render(title, pt, + Point{(GameWindowSize.w - title.size() * pt) / 2, 100}, + Color{0.39, 0.6, 1, 1}); + + title = "-------MADE FOR 1M GAMES-------"; + pt = 20; + font.Render(title, pt, + Point{(GameWindowSize.w - title.size() * pt) / 2, 200}, + Color{0.8, 0.8, 1, 1}); + + if (Button(exitImage_.get(), Point{(GameWindowSize.w) / 2, 550}, Size{100, 100})) { + engine.Exit(); + } + + if (Button(startImage_.get(), Point{(GameWindowSize.w) / 2, 400}, Size{100, 100})) { + engine.ChangeScence(new SpaceScence); + } +} diff --git a/src/tinyengine/engine.cpp b/src/tinyengine/engine.cpp index 614be92..d1c9c56 100644 --- a/src/tinyengine/engine.cpp +++ b/src/tinyengine/engine.cpp @@ -87,6 +87,7 @@ void Engine::Shutdown() { } void Engine::Update(float deltaTime) { + deltaTime = std::min(deltaTime, 0.3f); if (scence_) scence_->OnUpdate(deltaTime); } diff --git a/src/tinyengine/event.cpp b/src/tinyengine/event.cpp index f560699..b9138ba 100644 --- a/src/tinyengine/event.cpp +++ b/src/tinyengine/event.cpp @@ -1,6 +1,11 @@ #include "tinyengine/event.hpp" #include "tinyengine/engine.hpp" +struct { + bool left = false; + bool right = false; +} MouseState; + void OnWindowResize(GLFWwindow* window, int width, int height) { Renderer::SetViewport(0, 0, width, height); } diff --git a/src/tinyengine/inner_bmpfont.cpp b/src/tinyengine/inner_bmpfont.cpp index a5378e9..09665c2 100644 --- a/src/tinyengine/inner_bmpfont.cpp +++ b/src/tinyengine/inner_bmpfont.cpp @@ -336,6 +336,33 @@ InnerBmpFont::InnerBmpFont() { 0,0,1,1,0,0,0,0, 0,0,1,1,0,0,0,0, 0,0,0,0,0,0,0,0}); + saveChar('[', + {0,0,0,0,0,0,0,0, + 0,0,1,1,1,1,0,0, + 0,0,1,0,0,0,0,0, + 0,0,1,0,0,0,0,0, + 0,0,1,0,0,0,0,0, + 0,0,1,0,0,0,0,0, + 0,0,1,1,1,1,0,0, + 0,0,0,0,0,0,0,0}); + saveChar(']', + {0,0,0,0,0,0,0,0, + 0,0,1,1,1,1,0,0, + 0,0,0,0,0,1,0,0, + 0,0,0,0,0,1,0,0, + 0,0,0,0,0,1,0,0, + 0,0,0,0,0,1,0,0, + 0,0,1,1,1,1,0,0, + 0,0,0,0,0,0,0,0}); + saveChar('-', + {0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0}); errorChar_ = fontMat({1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,1,