From fd0bcce5c75005c06c290dfa42c3584eb6b1843f Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Thu, 10 Feb 2022 21:22:06 +0800 Subject: [PATCH] add welcome stage and a little gui --- assets/exit_btn.png | Bin 0 -> 542 bytes assets/start_btn.png | Bin 0 -> 837 bytes include/game/component.hpp | 7 ++++++- include/game/gui.hpp | 5 +++++ include/game/sprite.hpp | 26 ----------------------- include/game/stages/welcome.hpp | 14 +++++++++++++ include/tinyengine/engine.hpp | 2 +- src/game/action.cpp | 26 +++++++++++------------ src/game/component.cpp | 10 ++++++++- src/game/entity.cpp | 3 ++- src/game/gui.cpp | 12 +++++++++++ src/game/main.cpp | 4 +++- src/game/sprite.cpp | 19 ----------------- src/game/stages/space.cpp | 35 +++++++++++++++++++++---------- src/game/stages/welcome.cpp | 32 ++++++++++++++++++++++++++++ src/tinyengine/engine.cpp | 1 + src/tinyengine/event.cpp | 5 +++++ src/tinyengine/inner_bmpfont.cpp | 27 ++++++++++++++++++++++++ 18 files changed, 153 insertions(+), 75 deletions(-) create mode 100644 assets/exit_btn.png create mode 100644 assets/start_btn.png create mode 100644 include/game/gui.hpp delete mode 100644 include/game/sprite.hpp create mode 100644 include/game/stages/welcome.hpp create mode 100644 src/game/gui.cpp delete mode 100644 src/game/sprite.cpp create mode 100644 src/game/stages/welcome.cpp diff --git a/assets/exit_btn.png b/assets/exit_btn.png new file mode 100644 index 0000000000000000000000000000000000000000..f1f79d006be792e94a18dbf031ef12b04895163a GIT binary patch literal 542 zcmV+(0^$9MP)Px$*hxe|R9J<@m%nSmKorNH!4fD1x3134V%I{aLKnB@b2>7{qO|8lgVA~^SyWP-JPv$u{*#8)dm3g+q_={9u8s% zdk?ZsAO=^VxE;3@Wd+fA0(mxt&#w(Y-!AM}jU~{*IYr|M{9NBU!vUBjpu)}>%d=_y zyC^Fd_PVDVNRwG(raJhr*MK&e!7xA!pVq1 zqK|8`a$-+I6@&*i!Ch5HK!sBVIHRX39K=Th;zB~r&Sb*JSi^uTkOG;O2p?di0ZJmN zsO0o9AtfOAG%xC8Ksloe55AgxE5q0O#6%g~CD1N8nsAQ%(f9Px%|4BqaR9J<@mrYC)K@`V-OL{S4j97%kNK}XzLsb$?F+x8S%7=;$xg9pQnctweZ2ol;-sC->CK~SU!@t`CoMENit>Y?4~Y`eC_7{SoeIsk2zXG~*9++7sdnXIAJZ=1ZMvxkn(9w8(h1pu0+ zp=lZot$xv0wXxx#<+$8bghNZ~hI@nn9b>~m(6DTpsi_43baeKJx%IwMfZ<{3h{kq_ z#&!)hS!H5JB^~`1ZGQt17OZ~ zPZ(-KLzWXZ=SVEb#E&c>)h__>^ga}ZId2i;q>UKJname = 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,