From 559cfda2da267b2402851cd89168c99828fc202e Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Tue, 15 Feb 2022 22:50:31 +0800 Subject: [PATCH] add cursor; finished select scence --- assets/cursor.png | Bin 0 -> 156 bytes assets/go_btn.png | Bin 0 -> 365 bytes assets/switch_btn.png | Bin 0 -> 250 bytes assets/tilesheet.png | Bin 920 -> 1462 bytes include/game/action.hpp | 8 + include/game/ai.hpp | 9 + include/game/component.hpp | 52 +++++ include/game/constants.hpp | 33 +++- include/game/entity.hpp | 8 +- include/game/global.hpp | 5 + include/game/gui.hpp | 2 +- include/game/init_info.hpp | 12 ++ include/game/quick_list.hpp | 15 +- include/game/stages/select.hpp | 23 +++ include/game/stages/space.hpp | 5 +- include/game/system.hpp | 13 ++ include/tinyengine/camera.hpp | 1 + include/tinyengine/ecs/entity.hpp | 2 + include/tinyengine/event.hpp | 7 + include/tinyengine/libmath.hpp | 35 +++- src/game/action.cpp | 33 ++++ src/game/ai.cpp | 50 +++++ src/game/component.cpp | 4 +- src/game/controllers/fightship_controller.cpp | 27 +-- .../controllers/freightship_controller.cpp | 8 +- src/game/entity.cpp | 22 ++- src/game/global.cpp | 5 +- src/game/gui.cpp | 12 +- src/game/init_info.cpp | 0 src/game/main.cpp | 8 +- src/game/stages/gamelogo.cpp | 4 +- src/game/stages/select.cpp | 187 ++++++++++++++++++ src/game/stages/space.cpp | 68 ++++++- src/game/stages/welcome.cpp | 19 +- src/game/system.cpp | 41 +++- src/tinyengine/engine.cpp | 7 +- src/tinyengine/event.cpp | 52 ++++- src/tinyengine/inner_bmpfont.cpp | 18 ++ 38 files changed, 717 insertions(+), 78 deletions(-) create mode 100644 assets/cursor.png create mode 100644 assets/go_btn.png create mode 100644 assets/switch_btn.png create mode 100644 include/game/ai.hpp create mode 100644 include/game/init_info.hpp create mode 100644 include/game/stages/select.hpp create mode 100644 src/game/ai.cpp create mode 100644 src/game/init_info.cpp create mode 100644 src/game/stages/select.cpp diff --git a/assets/cursor.png b/assets/cursor.png new file mode 100644 index 0000000000000000000000000000000000000000..b90bfadc2025d34bca93c23dbca17820ba192a31 GIT binary patch literal 156 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}VV*9IArY-_ zuNm?IRWTnp_W!?Rxq54oWEFqLRTmM1%_o^SD&15YHa3Px$CrLy>R7i=vltB`LFbqZWMMiPKUEFXI58~2$xbz%e!V`E6**JF*8Q`4C=8;&VO#|V z0N(E>8EB!wBmrm!gpqR@5c>^8-TkjNr{qG@^GxmQOnG@juK*HB8)gd_N3WftH zvM98DF(5VoA|~L1gUPgQ>WZ`7rQe{=dAAS;@-d+8^Vk7+SbvN!M1$LG$1oGw00000 LNkvXXu0mjf<%gLl literal 0 HcmV?d00001 diff --git a/assets/switch_btn.png b/assets/switch_btn.png new file mode 100644 index 0000000000000000000000000000000000000000..6606a06b546a5630c76e868901d43913cbbe4d57 GIT binary patch literal 250 zcmeAS@N?(olHy`uVBq!ia0vp^0zj<5!3HFyJAa%3QjEnx?oJHr&dIz4a<+K7IEHw1 zCZ{wo?e6UU_y6bj`47+Uuji@%_pkZFiyWSR^=xWvTnAS$HXmbHe$xA&aL^9t9QC#v z+vgd%EsELTnZR5p{(w2#V*10W$FmOO5mKcmG{iMz{m wfA?#=?p!Z^uT;cX=q*#%UV|)#US0+U&Eg!p6t;uRK=(6vy85}Sb4q9e0I?5W@&Et; literal 0 HcmV?d00001 diff --git a/assets/tilesheet.png b/assets/tilesheet.png index aa495d4d36f3e5a1111d5932d2ef87e21138cbf6..23190af005d0f3b68557f7d21e45dff3cec2b3b5 100644 GIT binary patch delta 1388 zcmV-y1(W)i2eu24Fn?>6eFO{oztcc{-hreP8vYIb4wG>GX~tY1LNFNT(Mk z2`+pzDIy}@d_7(6{W)1(Oc$NsNo9-9=Q8u!dcDKBeZ8+*P?7*$AAfGF+$AD&L;aB- z8t(n7tejE6)qkB-R@%L+G5Fy+?Orzg5e=y*0p^!hymMzy*;_2FcwkH>R!A=$b_G!@dgs#PqJQ^2GG6oI86; z=9gAH(>y`f`(3UjEf{>5HHDw~Gm|aaB$$}qm6^#F|9|}WkyhhmG1&WUWhSckOGi`j zxq+?cixhgltxcmUnFEOAlVB=a{0ExJM*qQFdbn*aP!#BnCi(Z3|D;(CiEKafug#17 zK6E|bB_SG>HUX4(bNN~FPwBL+S`k}M*Ubg)4%{kDn!Vt+9Bq|d+3}M=SO0-L@sC}G zeS%-intuRsbdQXC;o$%A=U*Zs^7^YU`el1{{81qIp6>^D`Zpkx0 zJPA~6JzxD)$1X`Ak`J~@0(Bk-!FSg@x3-bWNPnl-wO3#Yw0b*G*Ea9($lTgSs{3e< zCn9q7+N@vq!w<%_zs#jhIdls^iYfT{g`oEmyN6m;)caf(g1-OdcL^>8b&rgDPo94% zc0jLQo0YbwkF0wHu6qHii|L{SP$Ph?=eK)V=^f6o7Z5xd8|qKV(u(y}PPYo!_ViKg z^?&}hShd}>ds(^o{%ay)@A<3&6qL*Xe*5YZF>MBI-rp%HQ$6^dfno2BV{iNA_qJ}z z&&!LE*OC%k+qVzp{9Nd4G@u`=cp|4+#f1To!d52gjHI|NK4c1^=1q z*#i0a#2GK@y6xsx7Zz-nkpOK1=mKs_0@SXO1W^p&nZ(mQjCV?sV>R7h`)H^!r*CPp%lUM?XxFtbU@I?y2 zHX#X;lV}2n6apM)^CuY0D=URBq$cwnvq54WcRFBmqbQ004k` z5?=H%{WP^hjnf6%Ru(?S!HN8zK~cSTbg<7`_~A#st5?fx3pcur9n)q{)Cgc|k1Ajr zo?SL}Ds7J{P|kjSIPh`mkq}iPDu_`%KW=*hUxlKE?Wmx+wkP;itkRxK&q%LfwjOI6 z;$0Sjc>dRra`yA%B#7&0;sCfT`ow9^^S_4J7H(Y5T_=4cK^1aY#0egohWWVdNroyF u=XveA0c1)Z)B)GPhwGC;127oWgZ}|xHA#iMRvWDV0000F|9KiA4h!GkQ1|5x{Ln(P!#BBxbkS7n} z!N9xX9+nO*cHB^L=@xpBJ?xVApglx~J=LKEw;((y{y7LjMy2F!1b!W8(o4DUOeN?$p}*jWR}o_-RUjQn{j! z4fnfmQLgCIJW`K z>otEd*P%>;Y=6Eci@A>adQW$!<7Cm?``yYAJ=+{Wc`v*r&Wm7Xb<^nOIyaX8Dr&7Ll;uQQ_ zH?G;1Md~8~mx35jCO|e{vt9G8lR$%0@Xh~?Ws)F%+JB2{>sne8`oO;er$8Hb2S(lY zw_j3RTi22|2XzsVrR74s?b9b2<&XJkgY(tY`}{lzdi&|Cm>&csCo=kv%hv-3bZNOD zSAT!!9YMrwC%;s#xK7ZlPXSLmp(6pJWjpz9FN2I>Usd<;(PRDg-Fx-*y`4?@@a~P~ zRflNVCVzp(oIuss>rLR^&gO~g5S`fe!fLfzPr_6x72D%%b)lWu!``8$QKM^XRSQyY zW)8lABsd$PJPEW0*IMRPkLWliz@MK5-Tdd&&MlCKy+gWJz1zWkTv>6eMhKJ%pbEHK z5}> map; + +private: + void generateMap(const Size& size); +}; + +class EnergyProductCmpt: public Component { +public: + void Init(int amount, float duration) { + this->productAmount = amount; + this->duration = duration; + this->cooldown = duration; + this->amount = 0; + } + bool IsCoolDowning() { return cooldown > 0; } + void Release() {} + + float duration; + float cooldown; + int productAmount; + int amount; +}; + +class GroupCmpt: public Component { +public: + void Init(int group) { + groupIdx = group; + } + void Release() {} + + int groupIdx; +}; + +class AICmpt: public Component { +public: + using AIFunc = std::function; + + void Init(AIFunc func) { + this->func = func; + } + void Release() {} + + AIFunc func; +}; diff --git a/include/game/constants.hpp b/include/game/constants.hpp index bd2017c..98f8281 100644 --- a/include/game/constants.hpp +++ b/include/game/constants.hpp @@ -2,16 +2,35 @@ #include "tinyengine/libmath.hpp" +constexpr float FightShipMaxSpeed = 400; +constexpr int FightShipLife = 10; +constexpr float FightShipRotationDegree = 6; +constexpr float FightShipAccelration = 200; + constexpr float FreightShipMaxSpeed = 100; -constexpr float FightShipMaxSpeed = 250; -constexpr int FreightLife = 10; -constexpr float LazerDamage = 2; -constexpr float LazerShooterSpeed = 500; -constexpr float LazerShooterMaxSpeed = 1000; -constexpr float LazerShooterCooldown = 0.3; +constexpr int FreightShipLife = 15; +constexpr float FreightShipAccelerate = 100; + +constexpr float LazerDamage = 3.4; +constexpr float LazerShooterSpeed = 800; +constexpr float LazerShooterMaxSpeed = 2000; +constexpr float LazerShooterCooldown = 0.15; + constexpr float MissileRotateDegree = 150; +constexpr float CoreMaxHp = 100; +constexpr int CoreEnergyProductDuration = 2; +constexpr int CoreEnergyProductAmount = 1; + constexpr Size GameWindowSize = {1024, 720}; constexpr Rect BulletRefreshArea{-100, -100, 1124, 820}; -constexpr Rect SpaceshipRefreshArea{-1024 * 3, -720 * 3, 1024 * 7, 720 * 7}; +constexpr int BlockSize = 16; + +constexpr int PlayerGroup = 0; +constexpr int Enemy1Group = 1; +constexpr int Enemy2Group = 2; +constexpr int Enemy3Group = 3; + +constexpr float EntityRenderSize = 20; +constexpr float EntityCollisionSize = 16; diff --git a/include/game/entity.hpp b/include/game/entity.hpp index 64d09b8..5158aff 100644 --- a/include/game/entity.hpp +++ b/include/game/entity.hpp @@ -4,7 +4,7 @@ #include "game/component.hpp" #include "game/global.hpp" -Entity* CreateFreightShip(); -Entity* CreateFightShip(); -Entity* CreateBullet(int damage, Entity* owner, float maxSpeed); -Entity* CreateMissile(int damage, Entity* owner, float maxSpeed, Entity* target); +Entity* CreateFreightShip(int group); +Entity* CreateFightShip(int group); +Entity* CreateBullet(int group, int damage, Entity* owner, float maxSpeed); +Entity* CreateMissile(int group, int damage, Entity* owner, float maxSpeed, Entity* target); diff --git a/include/game/global.hpp b/include/game/global.hpp index f7b61ca..27f4731 100644 --- a/include/game/global.hpp +++ b/include/game/global.hpp @@ -2,6 +2,7 @@ #include "tinyengine/tinyengine.hpp" #include "game/quick_list.hpp" +#include "game/init_info.hpp" extern Context ECSContext; extern SystemManager SystemMgr; @@ -13,6 +14,10 @@ extern QuickList Entities; extern Entity* PlayerSpaceship; +extern std::array, 4> Groups; + +extern GameInitInfo InitInfo; + void LoadResources(); Point MapGlobal2PlayerCoord(const Point& pos); Point MapPlayerCoord2Global(const Point& pos); diff --git a/include/game/gui.hpp b/include/game/gui.hpp index 48861a2..c0561bd 100644 --- a/include/game/gui.hpp +++ b/include/game/gui.hpp @@ -2,4 +2,4 @@ #include "tinyengine/tinyengine.hpp" -bool Button(Texture* texture, const Point& pos, const Size& size); +bool Button(Texture* texture, const Point& pos, const Size& size, bool hflip = false); diff --git a/include/game/init_info.hpp b/include/game/init_info.hpp new file mode 100644 index 0000000..692d511 --- /dev/null +++ b/include/game/init_info.hpp @@ -0,0 +1,12 @@ +#pragma once + +enum PlaneType { + FreightShip = 1, + FightShip, +}; + +struct GameInitInfo { + int planeType; + int groupNum; + int planeNum; +}; diff --git a/include/game/quick_list.hpp b/include/game/quick_list.hpp index 9345626..54629e3 100644 --- a/include/game/quick_list.hpp +++ b/include/game/quick_list.hpp @@ -5,12 +5,15 @@ template class QuickList final { public: + using condition_func = std::function; + using destroy_func = std::function; + void Add(const T& elem) { datas_.push_back(elem); } - void RemoveAll(std::function findFunc, - std::function destroyFunc = nullptr) { + void RemoveAll(condition_func findFunc, + destroy_func destroyFunc = nullptr) { std::size_t idx = 0; while (idx < datas_.size()) { if (datas_.size() > idx && findFunc(datas_[idx])) { @@ -24,7 +27,13 @@ public: } } } - void Clear() { datas_.clear(); } + + void Clear(condition_func condition = nullptr, destroy_func destroy = nullptr) { + if (destroy && condition) { + RemoveAll(condition, destroy); + } + datas_.clear(); + } // for debug void PrintAll(std::function func = nullptr) { diff --git a/include/game/stages/select.hpp b/include/game/stages/select.hpp new file mode 100644 index 0000000..8339123 --- /dev/null +++ b/include/game/stages/select.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include "tinyengine/tinyengine.hpp" + +class SelectScence: public Scence { +public: + void OnInit() override; + void OnRender() override; + +private: + Unique switchBtn_; + Unique goBtn_; + Camera camera_; + + void renderTitle(); + void renderSelectTitle(); + void renderConfigTitle(); + void renderSelectShip(); + void renderProperties(); + void renderGoBtn(); + void renderGroupNumPanel(); + void renderPlaneNumPanel(); +}; diff --git a/include/game/stages/space.hpp b/include/game/stages/space.hpp index c5b3267..fda1bef 100644 --- a/include/game/stages/space.hpp +++ b/include/game/stages/space.hpp @@ -6,6 +6,7 @@ #include "game/system.hpp" #include "game/controllers/freightship_controller.hpp" #include "game/controllers/fightship_controller.hpp" +#include "game/ai.hpp" class SpaceScence: public Scence { public: @@ -15,13 +16,15 @@ public: void OnQuit() override; private: - Unique freightController_; Unique fightController_; + void renderBackground(); void renderGUI(); void renderMiniMap(); void renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmpt* weapon2); Camera guiCamera_; Camera gameCamera_; + + std::vector stars_; }; diff --git a/include/game/system.hpp b/include/game/system.hpp index ff3638d..5e532d6 100644 --- a/include/game/system.hpp +++ b/include/game/system.hpp @@ -56,3 +56,16 @@ private: void renderEntity(Entity* entity, const RenderCmpt&, float rotation); void renderCollideBox(Entity* entity); }; + +class EnergyProductSystem: public UpdateSystem { +public: + void Update(float) override; + +private: + void coolDown(EnergyProductCmpt& cmpt, float dt); +}; + +class AIUpdateSystem: public UpdateSystem { +public: + void Update(float) override; +}; diff --git a/include/tinyengine/camera.hpp b/include/tinyengine/camera.hpp index f74cc8a..06bd2a3 100644 --- a/include/tinyengine/camera.hpp +++ b/include/tinyengine/camera.hpp @@ -11,6 +11,7 @@ public: void Scale(const Point& scale); void SetAnchor(const Point& anchor); bool TryCalcView(); + const Point& GetPosition() const { return position_; } private: Mat44 viewMat_; diff --git a/include/tinyengine/ecs/entity.hpp b/include/tinyengine/ecs/entity.hpp index 0700803..de33e81 100644 --- a/include/tinyengine/ecs/entity.hpp +++ b/include/tinyengine/ecs/entity.hpp @@ -133,3 +133,5 @@ private: } } }; + +using EntityPtr = Entity*; diff --git a/include/tinyengine/event.hpp b/include/tinyengine/event.hpp index 9781b8e..600cdc7 100644 --- a/include/tinyengine/event.hpp +++ b/include/tinyengine/event.hpp @@ -5,6 +5,13 @@ void OnWindowResize(GLFWwindow* window, int width, int height); bool IsKeyPressing(int key); +void InitEvent(const Size& windowInitSize); bool IsLeftPressing(); bool IsRightPressing(); +bool IsLeftPressed(); +bool IsRightPressed(); Point GetMousePosition(); + +void EventUpdate(); + +void MouseBtnCallback(GLFWwindow* window, int button, int action, int mods); diff --git a/include/tinyengine/libmath.hpp b/include/tinyengine/libmath.hpp index e20a076..74f86ff 100644 --- a/include/tinyengine/libmath.hpp +++ b/include/tinyengine/libmath.hpp @@ -102,7 +102,36 @@ inline bool IsPointInRect(const Point& p, const Rect& r) { return r.x <= p.x && r.y <= p.y && r.x + r.w >= p.x && r.y + r.h >= p.y; } -/* a matrix stored in row major*/ +template +class Mat { +public: + Mat(const Size& size): size_(size) { + data_.resize(size.w * size.h); + } + + T Get(const Point& p) const { + return data_.at(p.x * size_.h + p.y); + } + + T& Get(const Point& p) { + return data_.at(p.x * size_.h + p.y); + } + void Set(const Point& p, const T& value) { + data_.at(p.y + p.x * size_.h) = value; + } + bool InMat(const Point& p) const { + return p.x >= 0 && p.y >= 0 && p.x < size_.w && p.y < size_.h; + } + inline void Fill(const T& t) { data_.fill(t); } + + const Size& GetSize() const { return size_; } + +private: + std::vector data_; + Size size_; +}; + +/* a matrix stored in col major*/ class Mat44{ public: static const Mat44 Eye; @@ -172,3 +201,7 @@ inline Mat44 CreateSRT(const Point& pos, const Point& scale, float degree) { 0, 0, 0, 1, }); } + +inline float Distance(const Point& p1, const Point& p2) { + return Len(p1 - p2); +} diff --git a/src/game/action.cpp b/src/game/action.cpp index ac0146a..935c0db 100644 --- a/src/game/action.cpp +++ b/src/game/action.cpp @@ -34,3 +34,36 @@ void Shoot(SpaceshipWeaponCmpt& weapon, const Point& dir, Entity* target) { } } +void MoveLeft(MotionCmpt& motion) { + motion.acceleration += Point{-FreightShipAccelerate, 0}; +} + +void MoveRight(MotionCmpt& motion) { + motion.acceleration += Point{FreightShipAccelerate, 0}; +} + +void MoveUp(MotionCmpt& motion) { + motion.acceleration += Point{0, -FreightShipAccelerate}; +} + +void MoveDown(MotionCmpt& motion) { + motion.acceleration += Point{0, FreightShipAccelerate}; +} + +void SpeedUp(MotionCmpt& motion, FightShipCmpt& ship) { + motion.acceleration = Rotate(Point{0, FightShipAccelration}, + -ship.degree); +} + +void SpeedDown(MotionCmpt& motion, FightShipCmpt& ship) { + motion.acceleration = Rotate(Point{0, -FightShipAccelration}, + -ship.degree); +} + +void TurnLeft(FightShipCmpt& ship) { + ship.degree += FightShipRotationDegree; +} + +void TurnRight(FightShipCmpt& ship) { + ship.degree -= FightShipRotationDegree; +} diff --git a/src/game/ai.cpp b/src/game/ai.cpp new file mode 100644 index 0000000..cff2761 --- /dev/null +++ b/src/game/ai.cpp @@ -0,0 +1,50 @@ +#include "game/ai.hpp" + +void FreightShipAI(Entity* self) { + auto pmove = PlayerSpaceship->Get(); + auto smove = self->Get(); + auto dir = pmove->position - smove->position; + if (Len2(dir) > 250000) { + auto motion = self->Use(); + if (dir.x < 0) { + MoveLeft(*motion); + } else { + MoveRight(*motion); + } + if (dir.y < 0) { + MoveUp(*motion); + } else { + MoveDown(*motion); + } + } else { + Shoot(*self->Get()->weapon, dir); + } +} + +void FightShipAI(Entity* self) { + auto pmove = PlayerSpaceship->Get(); + auto smove = self->Get(); + auto dir = pmove->position - smove->position; + + auto motion = self->Use(); + auto ship = self->Use(); + + auto nspd = Normalize(motion->speed), + ndir = Normalize(dir); + + SpeedUp(*motion, *ship); + + auto cross = Cross(nspd, ndir); + if (cross > std::sin(Radians(80))) { + TurnLeft(*ship); + } else if (cross < std::sin(Radians(10))) { + TurnRight(*ship); + } + // if (Cross(Normalize(motion->speed), Normalize(dir)) < std::sin(Radians(10))) { + // if (ship->weapon2->bulletAmount > 0 && Random(1.0f, 100.0f) < 5) { + // Shoot(*ship->weapon2, dir, PlayerSpaceship); + // } else { + // Shoot(*ship->weapon1, dir); + // } + // } +} diff --git a/src/game/component.cpp b/src/game/component.cpp index fef8bdd..d6802e9 100644 --- a/src/game/component.cpp +++ b/src/game/component.cpp @@ -6,7 +6,7 @@ Entity* SpaceshipWeaponCmpt::ShootBullet(const Point& dir) { if (bulletAmount != InfBullet) { bulletAmount --; } - Entity* bullet = CreateBullet(damage, owner, maxSpeed); + Entity* bullet = CreateBullet(owner->Get()->groupIdx, damage, owner, maxSpeed); bullet->Use()->speed = Normalize(dir) * shootSpeed; bullet->Use()->position = owner->Get()->position; return bullet; @@ -17,7 +17,7 @@ Entity* SpaceshipWeaponCmpt::ShootMissile(const Point& dir, Entity* target) { if (bulletAmount != InfBullet) { bulletAmount --; } - Entity* bullet = CreateMissile(damage, owner, maxSpeed, target); + Entity* bullet = CreateMissile(owner->Get()->groupIdx, damage, owner, maxSpeed, target); if (target) { bullet->Use()->rotation = Degrees(std::acos(Dot(dir, Normalize(target->Get()->position - owner->Get()->position)))); } diff --git a/src/game/controllers/fightship_controller.cpp b/src/game/controllers/fightship_controller.cpp index e0a2e2f..fec850f 100644 --- a/src/game/controllers/fightship_controller.cpp +++ b/src/game/controllers/fightship_controller.cpp @@ -7,33 +7,38 @@ FightShipController::FightShipController(Entity* entity) void FightShipController::Update(float dt) { const float spd = 100; auto motionCmpt = entity_->Use(); - float& rotation = entity_->Use()->degree; + auto ship = entity_->Use(); if (IsKeyPressing(GLFW_KEY_A)) { - rotation += 2; + TurnLeft(*ship); } if (IsKeyPressing(GLFW_KEY_D)) { - rotation -= 2; + TurnRight(*ship); } if (IsKeyPressing(GLFW_KEY_S)) { - motionCmpt->acceleration = Rotate(Point{0, spd}, -rotation); + SpeedUp(*motionCmpt, *ship); } if (IsKeyPressing(GLFW_KEY_W)) { - motionCmpt->acceleration = Rotate(Point{0, -spd}, -rotation); + SpeedDown(*motionCmpt, *ship); } motionCmpt->speed = Rotate(Point{0, -1} * Len(motionCmpt->speed), - -rotation); + -ship->degree); - auto fightShip = entity_->Use(); - auto weapon1 = fightShip->weapon1, - weapon2 = fightShip->weapon2; auto moveCmpt = entity_->Get(); if (IsLeftPressing()) { - weaponShoot(weapon1, *moveCmpt); + if (entity_->Has()) { + weaponShoot(entity_->Use()->weapon1, *moveCmpt); + } else if (entity_->Has()) { + weaponShoot(entity_->Use()->weapon, *moveCmpt); + } } if (IsRightPressing()) { - weaponShoot(weapon2, *moveCmpt); + if (entity_->Has()) { + weaponShoot(entity_->Use()->weapon2, *moveCmpt); + } else if (entity_->Has()) { + weaponShoot(entity_->Use()->weapon, *moveCmpt); + } } } diff --git a/src/game/controllers/freightship_controller.cpp b/src/game/controllers/freightship_controller.cpp index b0ba880..232d889 100644 --- a/src/game/controllers/freightship_controller.cpp +++ b/src/game/controllers/freightship_controller.cpp @@ -8,16 +8,16 @@ void FreightShipController::Update(float dt) { const float spd = 100; auto motionCmpt = entity_->Use(); if (IsKeyPressing(GLFW_KEY_A)) { - motionCmpt->acceleration += Point{-spd, 0}; + MoveLeft(*motionCmpt); } if (IsKeyPressing(GLFW_KEY_D)) { - motionCmpt->acceleration += Point{spd, 0}; + MoveRight(*motionCmpt); } if (IsKeyPressing(GLFW_KEY_S)) { - motionCmpt->acceleration += Point{0, spd}; + MoveDown(*motionCmpt); } if (IsKeyPressing(GLFW_KEY_W)) { - motionCmpt->acceleration += Point{0, -spd}; + MoveUp(*motionCmpt); } if (IsLeftPressing()) { diff --git a/src/game/entity.cpp b/src/game/entity.cpp index 2149e38..d525fcb 100644 --- a/src/game/entity.cpp +++ b/src/game/entity.cpp @@ -1,7 +1,7 @@ #include "game/entity.hpp" #include "game/constants.hpp" -Entity* CreateFreightShip() { +Entity* CreateFreightShip(int group) { Entity* entity = ECSContext.CreateEntity(); entity->Add(Point{0, 0}); entity->Add(Point{0, 0}, FreightShipMaxSpeed); @@ -13,14 +13,15 @@ Entity* CreateFreightShip() { LazerDamage, LazerShooterSpeed, LazerShooterMaxSpeed, - LazerShooterCooldown); - entity->Add(Size{16, 16}); - entity->Add(FreightLife); + LazerShooterCooldown + 1); + entity->Add(Size{EntityCollisionSize, EntityCollisionSize}); + entity->Add(FreightShipLife); entity->Add(weapon); + entity->Add(group); return entity; } -Entity* CreateFightShip() { +Entity* CreateFightShip(int group) { Entity* entity = ECSContext.CreateEntity(); entity->Add(Point{0, 0}); entity->Add(Point{0, 0}, FightShipMaxSpeed); @@ -44,28 +45,31 @@ Entity* CreateFightShip() { LazerShooterCooldown, 10); - entity->Add(Size{16, 16}); - entity->Add(FreightLife); + entity->Add(Size{EntityCollisionSize, EntityCollisionSize}); + entity->Add(FightShipLife); entity->Add(weapon1, weapon2); + entity->Add(group); return entity; } -Entity* CreateBullet(int damage, Entity* owner, float maxSpeed) { +Entity* CreateBullet(int group, int damage, Entity* owner, float maxSpeed) { Entity* entity = ECSContext.CreateEntity(); entity->Add(Point{0, 0}); entity->Add(Point{0, 0}, maxSpeed); entity->Add(GameTileSheet->GetTile(0, 1)); entity->Add(BulletCmpt::Bullet, damage, owner); entity->Add(Size{8, 8}); + entity->Add(group); return entity; } -Entity* CreateMissile(int damage, Entity* owner, float maxSpeed, Entity* target) { +Entity* CreateMissile(int group, int damage, Entity* owner, float maxSpeed, Entity* target) { Entity* entity = ECSContext.CreateEntity(); entity->Add(Point{0, 0}); entity->Add(Point{0, 0}, maxSpeed); entity->Add(GameTileSheet->GetTile(2, 1)); entity->Add(BulletCmpt::Missile, damage, owner, target); entity->Add(Size{10, 10}); + entity->Add(group); return entity; } diff --git a/src/game/global.cpp b/src/game/global.cpp index 950b5de..255f6b8 100644 --- a/src/game/global.cpp +++ b/src/game/global.cpp @@ -9,7 +9,10 @@ std::unordered_map> Sounds; QuickList Bullets; QuickList Entities; -Entity* PlayerSpaceship; +Entity* PlayerSpaceship = nullptr; +GameInitInfo InitInfo; + +std::array, 4> Groups; void loadImages() { GameTileSheet.reset(new TileSheet("assets/tilesheet.png", 8, 8)); diff --git a/src/game/gui.cpp b/src/game/gui.cpp index ee16175..660c53b 100644 --- a/src/game/gui.cpp +++ b/src/game/gui.cpp @@ -1,11 +1,13 @@ #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; +bool Button(Texture* texture, const Point& pos, const Size& size, bool hflip) { + Renderer::DrawTexture(texture, nullptr, pos, size, 0, hflip ? Renderer::FlipFlag::Horizontal : Renderer::FlipFlag::NoFlip); + Rect rect = Rect{pos.x - size.w / 2, pos.y - size.h / 2, size.w, size.h}; + if (IsPointInRect(GetMousePosition(), rect)) { + Renderer::SetDrawColor(Color{1, 1, 1, 0.2}); + Renderer::FillRect(rect); + return IsLeftPressed(); } else { return false; } diff --git a/src/game/init_info.cpp b/src/game/init_info.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/game/main.cpp b/src/game/main.cpp index 6859ac3..499bae7 100644 --- a/src/game/main.cpp +++ b/src/game/main.cpp @@ -1,8 +1,10 @@ #include "game/stages/gamelogo.hpp" #include "game/stages/space.hpp" #include "game/stages/welcome.hpp" +#include "game/stages/select.hpp" #include "game/constants.hpp" -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) +// RUN_WINDOW("Space War v1.0", GameWindowSize.w, GameWindowSize.h, SelectScence) +// RUN_WINDOW("Space War v1.0", GameWindowSize.w, GameWindowSize.h, SpaceScence) +RUN_WINDOW("Space War v1.0", GameWindowSize.w, GameWindowSize.h, WelcomeScence) +// RUN_WINDOW("Space War v1.0", GameWindowSize.w, GameWindowSize.h, GameLogoScence) diff --git a/src/game/stages/gamelogo.cpp b/src/game/stages/gamelogo.cpp index 30e4f7a..42d0009 100644 --- a/src/game/stages/gamelogo.cpp +++ b/src/game/stages/gamelogo.cpp @@ -1,5 +1,5 @@ #include "game/stages/gamelogo.hpp" -#include "game/stages/space.hpp" +#include "game/stages/welcome.hpp" constexpr float TitlePixel = 40.0f; constexpr float AuthorInfoPixel = 20.0f; @@ -41,6 +41,6 @@ void GameLogoScence::OnRender() { } if (duration > 4) { - engine.ChangeScence(new SpaceScence); + engine.ChangeScence(new WelcomeScence); } } diff --git a/src/game/stages/select.cpp b/src/game/stages/select.cpp new file mode 100644 index 0000000..bf2a001 --- /dev/null +++ b/src/game/stages/select.cpp @@ -0,0 +1,187 @@ +#include "game/stages/select.hpp" +#include "game/constants.hpp" +#include "game/gui.hpp" +#include "game/global.hpp" +#include "game/stages/space.hpp" + +void SelectScence::OnInit() { + // TODO move it to welcome stage + LoadResources(); + + switchBtn_.reset(new Texture("assets/switch_btn.png")); + goBtn_.reset(new Texture("assets/go_btn.png")); + + Renderer::SetCamera(camera_); + InitInfo.planeType = 1; + InitInfo.planeNum = 5; + InitInfo.groupNum = 2; +} + +void SelectScence::OnRender() { + renderTitle(); + renderSelectTitle(); + renderSelectShip(); + renderProperties(); + + renderConfigTitle(); + renderGroupNumPanel(); + renderPlaneNumPanel(); + + renderGoBtn(); +} + +void SelectScence::renderTitle() { + std::string title = "PREPARATION ROOM"; + engine.GetInnerBmpFont().Render(title, + 40, + Point{(GameWindowSize.w - title.length() * 40) / 2, 50}, + Color{0.5, 0.5, 0, 1}); +} + +void SelectScence::renderSelectTitle() { + engine.GetInnerBmpFont().Render("SELECT PLANE", + 30, + Point{80, 150}, + Color{0.2, 0.2, 0.8, 1}); +} + +void SelectScence::renderConfigTitle() { + engine.GetInnerBmpFont().Render("CONFIG WAR", + 30, + Point{600, 150}, + Color{0.2, 0.2, 0.8, 1}); +} + +void SelectScence::renderSelectShip() { + if (Button(switchBtn_.get(), Point{100, 300}, switchBtn_->GetSize() * 2)) { + InitInfo.planeType ++; + if (InitInfo.planeType > 2) { + InitInfo.planeType = 1; + } + } + if (Button(switchBtn_.get(), Point{400, 300}, switchBtn_->GetSize() * 2, true)) { + InitInfo.planeType --; + if (InitInfo.planeType < 1) { + InitInfo.planeType = 2; + } + } + + static float rotation = 0; + if (InitInfo.planeType == FreightShip) { + Renderer::DrawTile(GameTileSheet->GetTile(0, 0), + Point{250, 300}, + Size{64, 64}, + rotation); + } else if (InitInfo.planeType == FightShip) { + Renderer::DrawTile(GameTileSheet->GetTile(1, 0), + Point{250, 300}, + Size{64, 64}, + rotation); + } + rotation += 2; +} + +void SelectScence::renderProperties() { + auto& font = engine.GetInnerBmpFont(); + if (InitInfo.planeType == 1) { + font.Render("HP: " + std::to_string(FreightShipLife), + 20, + Point{80, 400}, + Color{1, 0.1, 0.1, 1}); + font.Render("MAX SPEED: " + std::to_string(int(FreightShipMaxSpeed)), + 20, + Point{80, 420}, + Color{1, 1, 0, 1}); + font.Render("MAX ACCELERATION: " + std::to_string(int(FreightShipAccelerate)), + 20, + Point{80, 440}, + Color{0.7, 0.7, 0, 1}); + font.Render("FREE SLOW SHOOT", + 20, + Point{80, 460}, + Color{0, 0.7, 0.7, 1}); + font.Render("FREE MOVE", + 20, + Point{80, 480}, + Color{0, 0.7, 0.7, 1}); + font.Render("ONE WEAPON", + 20, + Point{80, 500}, + Color{0, 0.7, 0.7, 1}); + } else if (InitInfo.planeType == 2) { + font.Render("HP: " + std::to_string(FightShipLife), + 20, + Point{80, 400}, + Color{1, 0.1, 0.1, 1}); + font.Render("MAX SPEED: " + std::to_string(int(FightShipMaxSpeed)), + 20, + Point{80, 420}, + Color{1, 1, 0, 1}); + font.Render("MAX ACCELERATION: " + std::to_string(int(FightShipAccelration)), + 20, + Point{80, 440}, + Color{0.7, 0.7, 0, 1}); + font.Render("DIRECT FAST SHOOT", + 20, + Point{80, 460}, + Color{0, 0.7, 0.7, 1}); + font.Render("DIRECT MOVE", + 20, + Point{80, 480}, + Color{0, 0.7, 0.7, 1}); + font.Render("TWO WEAPON", + 20, + Point{80, 500}, + Color{0, 0.7, 0.7, 1}); + } +} + +void SelectScence::renderGroupNumPanel() { + auto& font = engine.GetInnerBmpFont(); + font.Render("GROUP NUMBER:", + 20, + Point{600, 300}, + Color{0.6, 0.3, 0.8, 1}); + + if (Button(switchBtn_.get(), Point{870, 310}, Size{switchBtn_->GetSize().w, 20})) { + InitInfo.groupNum --; + if (InitInfo.groupNum <= 1) + InitInfo.groupNum = 4; + } + font.Render(std::to_string(InitInfo.groupNum), 20, Point{890, 300}, Color{0.6, 0.3, 0.8, 1}); + if (Button(switchBtn_.get(), Point{920, 310}, Size{switchBtn_->GetSize().w, 20}, true)) { + InitInfo.groupNum ++; + if (InitInfo.groupNum > 4) + InitInfo.groupNum = 2; + } +} + +void SelectScence::renderPlaneNumPanel() { + auto& font = engine.GetInnerBmpFont(); + font.Render("PLANE NUMBER:", + 20, + Point{600, 400}, + Color{0.3, 0.6, 0.8, 1}); + + if (Button(switchBtn_.get(), Point{870, 410}, Size{switchBtn_->GetSize().w, 20})) { + InitInfo.planeNum --; + if (InitInfo.planeNum <= 0) + InitInfo.planeNum = 100; + } + font.Render(std::to_string(InitInfo.planeNum), 20, Point{890, 400}, Color{0.6, 0.3, 0.8, 1}); + if (Button(switchBtn_.get(), Point{960, 410}, Size{switchBtn_->GetSize().w, 20}, true)) { + InitInfo.planeNum ++; + if (InitInfo.planeNum > 100) + InitInfo.planeNum = 1; + } +} + +void SelectScence::renderGoBtn() { + if (Button(goBtn_.get(), + Point{800, 600}, + goBtn_->GetSize() * 4)) { + engine.ChangeScence(new SpaceScence); + } else { + engine.GetInnerBmpFont().Render("GO", 40, Point{760, 570}, Color{1, 0, 1, 1}); + } +} diff --git a/src/game/stages/space.cpp b/src/game/stages/space.cpp index 6711443..becb0e8 100644 --- a/src/game/stages/space.cpp +++ b/src/game/stages/space.cpp @@ -2,40 +2,64 @@ void SpaceScence::OnInit() { Renderer::SetClearColor(Color{0, 0, 0, 255}); - LoadResources(); Entities.Clear(); Bullets.Clear(); - PlayerSpaceship = CreateFightShip(); + PlayerSpaceship = CreateFightShip(PlayerGroup); Entities.Add(PlayerSpaceship); PlayerSpaceship->Use()->position = Point{400, 400}; - Entity* enemy = CreateFreightShip(); + Entity* enemy = CreateFreightShip(Enemy1Group); + enemy->Add(FreightShipAI); enemy->Use()->position = Point{100, 100}; Entities.Add(enemy); - enemy = CreateFreightShip(); + enemy = CreateFreightShip(Enemy1Group); + enemy->Add(FreightShipAI); enemy->Use()->position = Point{200, 100}; Entities.Add(enemy); - enemy = CreateFreightShip(); + enemy = CreateFreightShip(Enemy1Group); + enemy->Add(FreightShipAI); enemy->Use()->position = Point{100, 200}; Entities.Add(enemy); - // freightController_.reset(new FreightShipController(spaceship_)); + // Entity* enemy = CreateFightShip(Enemy1Group); + // enemy->Add(FightShipAI); + // enemy->Use()->position = Point{100, 100}; + // Entities.Add(enemy); + + // enemy = CreateFightShip(Enemy1Group); + // enemy->Add(FightShipAI); + // enemy->Use()->position = Point{200, 100}; + // Entities.Add(enemy); + + // enemy = CreateFightShip(Enemy1Group); + // enemy->Add(FightShipAI); + // enemy->Use()->position = Point{100, 200}; + // Entities.Add(enemy); + fightController_.reset(new FightShipController(PlayerSpaceship)); gameCamera_.SetAnchor(GameWindowSize / 2); SystemMgr.Clear(); + SystemMgr.AddUpdateSystem(new AIUpdateSystem); SystemMgr.AddUpdateSystem(new WeaponCooldownSystem); + SystemMgr.AddUpdateSystem(new EnergyProductSystem); SystemMgr.AddUpdateSystem(new MissileUpdateSystem); SystemMgr.AddUpdateSystem(new PhysicalSystem); SystemMgr.AddUpdateSystem(new ColliRectCorrectSystem); SystemMgr.AddUpdateSystem(new CollideSystem); SystemMgr.AddUpdateSystem(new CleanupSystem); SystemMgr.AddRenderSystem(new RenderEntitySystem); + + for (int i = 0; i < 100; i++) { + Point p{Random(0, GameWindowSize.w), + Random(0, GameWindowSize.h)}; + stars_.push_back(p); + } } void SpaceScence::OnUpdate(float dt) { @@ -46,12 +70,33 @@ void SpaceScence::OnUpdate(float dt) { void SpaceScence::OnRender() { Renderer::SetCamera(gameCamera_); + renderBackground(); SystemMgr.Render(); Renderer::SetCamera(guiCamera_); renderGUI(); } +void SpaceScence::renderBackground() { + Renderer::SetDrawColor(Color{1, 1, 1, 1}); + + Point p; + p.x = std::floor(gameCamera_.GetPosition().x / GameWindowSize.w) * GameWindowSize.w; + p.y = std::floor(gameCamera_.GetPosition().y / GameWindowSize.h) * GameWindowSize.h; + + for (auto& star : stars_) { + Point pos{p.x + star.x, p.y + star.y}; + if (pos.x < gameCamera_.GetPosition().x) { + pos.x += GameWindowSize.w; + } + if (pos.y < gameCamera_.GetPosition().y) { + pos.y += GameWindowSize.h; + } + Rect drawrect = {pos.x - GameWindowSize.w / 2, pos.y - GameWindowSize.h / 2, 2, 2}; + Renderer::FillRect(drawrect); + } +} + void SpaceScence::renderGUI() { Renderer::SetCamera(guiCamera_); @@ -123,6 +168,7 @@ void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmp } offset.y += 20; } + if (weapon2) { font.Render(weapon2->name, 20, @@ -135,8 +181,18 @@ void SpaceScence::renderWeapons(SpaceshipWeaponCmpt* weapon1, SpaceshipWeaponCmp Point{weaponInfoRect.x + weaponInfoRect.w - 100, weaponInfoRect.y} + offset, Color{0, 0.8, 0, 1}); } + offset.y += 20; } + // font.Render("ENERGY", + // 20, + // Point{weaponInfoRect.x, weaponInfoRect.y} + offset, + // Color{0.9, 0.3, 0.83, 1}); + // std::string energyAmount = std::to_string(PlayerCore->Get()->amount); + // font.Render(energyAmount, + // 20, + // Point{weaponInfoRect.x + weaponInfoRect.w - 20 - 20 * energyAmount.size(), weaponInfoRect.y} + offset, + // Color{0.9, 0.5, 0.8, 1}); } void SpaceScence::OnQuit() { diff --git a/src/game/stages/welcome.cpp b/src/game/stages/welcome.cpp index 5594c2d..9374588 100644 --- a/src/game/stages/welcome.cpp +++ b/src/game/stages/welcome.cpp @@ -1,16 +1,23 @@ #include "game/stages/welcome.hpp" #include "game/gui.hpp" -#include "game/stages/space.hpp" +#include "game/constants.hpp" +#include "game/stages/select.hpp" void WelcomeScence::OnInit() { startImage_.reset(new Texture("assets/start_btn.png")); exitImage_.reset(new Texture("assets/exit_btn.png")); + + GLFWimage image; + image.pixels = stbi_load("assets/cursor.png", &image.width, &image.height, nullptr, 0); + auto cursor = glfwCreateCursor(&image, image.width / 2, image.height / 2); + glfwSetCursor(engine.GetWindow(), cursor); + stbi_image_free(image.pixels); } void WelcomeScence::OnRender() { Renderer::SetCamera(camera_); auto& font = engine.GetInnerBmpFont(); - std::string title = "SPACE SECTOR"; + std::string title = "SPACE WAR"; int pt = 70; font.Render(title, pt, Point{(GameWindowSize.w - title.size() * pt) / 2, 100}, @@ -22,11 +29,17 @@ void WelcomeScence::OnRender() { Point{(GameWindowSize.w - title.size() * pt) / 2, 200}, Color{0.8, 0.8, 1, 1}); + title = "HTTPS://GITEE.COM/VISUALGMQ/SPACE-SECTOR.GIT"; + pt = 10; + font.Render(title, pt, + Point{(GameWindowSize.w - title.size() * pt) / 2, 710}, + 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); + engine.ChangeScence(new SelectScence); } } diff --git a/src/game/system.cpp b/src/game/system.cpp index a583ca2..496f932 100644 --- a/src/game/system.cpp +++ b/src/game/system.cpp @@ -7,7 +7,6 @@ void PhysicalSystem::Update(float dt) { physicalStep(entity, dt, *entity->Use(), *entity->Use()); - } } @@ -93,7 +92,8 @@ void CollideSystem::Update(float dt) { for (auto& bullet : Bullets) { for (auto& entity: Entities) { if (entity->Has() && - bullet->Get()->owner != entity && + entity->Has() && + bullet->Get()->groupIdx != entity->Get()->groupIdx && IsRectsIntersect(bullet->Get()->rect, entity->Get()->rect)) { if (entity->Has()) { @@ -119,10 +119,7 @@ void CleanupSystem::Update(float dt) { Entities.RemoveAll([](const EntityPtr& entity){ if (entity->Has() && - entity->Get()->hp <= 0 || - entity->Has() && - !IsPointInRect(MapGlobal2PlayerCoord(entity->Get()->position), - SpaceshipRefreshArea)) { + entity->Get()->hp <= 0) { return true; } else { return false; @@ -186,13 +183,41 @@ void RenderEntitySystem::renderEntity(Entity* entity, const RenderCmpt& renderCm Renderer::DrawTexture(renderCmpt.texture, nullptr, pos, - Size{0, 0}, + Size{EntityRenderSize, EntityRenderSize}, rotation); } else { Renderer::DrawTile(renderCmpt.tile, pos, - Size{0, 0}, + Size{EntityRenderSize, EntityRenderSize}, rotation); } } } + +void EnergyProductSystem::Update(float dt) { + for (auto& entity: Entities) { + if (entity->Has()) { + coolDown(*entity->Use(), dt); + } + } +} + +void EnergyProductSystem::coolDown(EnergyProductCmpt& cmpt, float dt) { + if (cmpt.IsCoolDowning()) { + cmpt.cooldown -= dt; + } else { + cmpt.cooldown = cmpt.duration; + cmpt.amount += cmpt.productAmount; + } +} + +void AIUpdateSystem::Update(float) { + for (auto& entity : Entities) { + if (entity->Has()) { + auto func = entity->Get()->func; + if (func) { + func(entity); + } + } + } +} diff --git a/src/tinyengine/engine.cpp b/src/tinyengine/engine.cpp index d1c9c56..54b86f6 100644 --- a/src/tinyengine/engine.cpp +++ b/src/tinyengine/engine.cpp @@ -8,6 +8,7 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) { if (!glfwInit()) { FATAL_ERROR("glfw init failed"); } + InitEvent(size); glfwSetErrorCallback(error_callback); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -27,7 +28,8 @@ void Engine::Init(const std::string& title, const Size& size, Scence* scence) { glfwMakeContextCurrent(window_); - glfwSetFramebufferSizeCallback(engine.GetWindow(), OnWindowResize); + glfwSetFramebufferSizeCallback(window_, OnWindowResize); + glfwSetMouseButtonCallback(window_, MouseBtnCallback); if (!gladLoadGL(glfwGetProcAddress)) { glfwTerminate(); @@ -75,6 +77,7 @@ void Engine::SwapContext() { } void Engine::PollEvent() { + EventUpdate(); glfwPollEvents(); } @@ -87,7 +90,7 @@ void Engine::Shutdown() { } void Engine::Update(float deltaTime) { - deltaTime = std::min(deltaTime, 0.3f); + deltaTime = std::min(deltaTime, 0.03f); if (scence_) scence_->OnUpdate(deltaTime); } diff --git a/src/tinyengine/event.cpp b/src/tinyengine/event.cpp index b9138ba..c65650d 100644 --- a/src/tinyengine/event.cpp +++ b/src/tinyengine/event.cpp @@ -1,10 +1,43 @@ #include "tinyengine/event.hpp" #include "tinyengine/engine.hpp" -struct { +struct MouseState { bool left = false; bool right = false; -} MouseState; +}; + +struct { + MouseState oldState; + MouseState state; +} MouseContext; + +void MouseBtnCallback(GLFWwindow* window, int button, int action, int mods) { + if (action == GLFW_PRESS) { + if (button == GLFW_MOUSE_BUTTON_LEFT) { + MouseContext.state.left = true; + } + if (button == GLFW_MOUSE_BUTTON_RIGHT) { + MouseContext.state.right = true; + } + } else if (action == GLFW_RELEASE) { + if (button == GLFW_MOUSE_BUTTON_LEFT) { + MouseContext.state.left = false; + } + if (button == GLFW_MOUSE_BUTTON_RIGHT) { + MouseContext.state.right = false; + } + } +} + +void EventUpdate() { + MouseContext.oldState = MouseContext.state; +} + +Size WindowInitSize; + +void InitEvent(const Size& windowInitSize) { + WindowInitSize = windowInitSize; +} void OnWindowResize(GLFWwindow* window, int width, int height) { Renderer::SetViewport(0, 0, width, height); @@ -15,15 +48,24 @@ bool IsKeyPressing(int key) { } bool IsLeftPressing() { - return glfwGetMouseButton(engine.GetWindow(), GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; + return MouseContext.state.left; } bool IsRightPressing() { - return glfwGetMouseButton(engine.GetWindow(), GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS; + return MouseContext.state.right; +} + +bool IsLeftPressed() { + return MouseContext.state.left && !MouseContext.oldState.left; +} + +bool IsRightPressed() { + return MouseContext.state.right && !MouseContext.oldState.right; } Point GetMousePosition() { double x, y; glfwGetCursorPos(engine.GetWindow(), &x, &y); - return Point{float(x), float(y)}; + return Point{float(x) * WindowInitSize.w / engine.GetWindowSize().w, + float(y) * WindowInitSize.h / engine.GetWindowSize().h}; } diff --git a/src/tinyengine/inner_bmpfont.cpp b/src/tinyengine/inner_bmpfont.cpp index 09665c2..3a4a7f3 100644 --- a/src/tinyengine/inner_bmpfont.cpp +++ b/src/tinyengine/inner_bmpfont.cpp @@ -363,6 +363,24 @@ InnerBmpFont::InnerBmpFont() { 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}); + saveChar('/', + {0,0,0,0,0,0,0,0, + 0,0,0,0,0,1,1,0, + 0,0,0,0,1,1,1,0, + 0,0,0,1,1,0,0,0, + 0,0,0,1,1,0,0,0, + 0,1,1,1,0,0,0,0, + 0,1,1,0,0,0,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, + 0,0,0,0,0,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,0, + 0,0,0,0,0,1,1,0}); errorChar_ = fontMat({1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,1,