From d5c002c2b2f3a310118bc1261f92439cf89d9f5a Mon Sep 17 00:00:00 2001 From: Bram Verhulst Date: Thu, 9 May 2024 13:50:52 +0200 Subject: [PATCH] Add Particles (basic) --- .idea/.idea.Motherload/.idea/workspace.xml | 78 ++++++++++----------- Game/Levels/World/WorldLevel.cpp | 29 ++++++-- Game/Levels/World/WorldLevel.h | 4 ++ Game/Particle/Particle.cpp | 17 +++-- Game/Particle/Particle.h | 6 +- Game/Player.cpp | 3 + Resources/particles/dirt_1.png | Bin 0 -> 364 bytes Resources/particles/dirt_2.png | Bin 0 -> 422 bytes Resources/particles/dirt_3.png | Bin 0 -> 340 bytes Resources/particles/dirt_4.png | Bin 0 -> 419 bytes Resources/particles/dirt_5.png | Bin 0 -> 368 bytes Resources/particles/dirt_6.png | Bin 0 -> 430 bytes Resources/particles/dirt_7.png | Bin 0 -> 426 bytes Resources/particles/dirt_8.png | Bin 0 -> 402 bytes 14 files changed, 87 insertions(+), 50 deletions(-) create mode 100644 Resources/particles/dirt_1.png create mode 100644 Resources/particles/dirt_2.png create mode 100644 Resources/particles/dirt_3.png create mode 100644 Resources/particles/dirt_4.png create mode 100644 Resources/particles/dirt_5.png create mode 100644 Resources/particles/dirt_6.png create mode 100644 Resources/particles/dirt_7.png create mode 100644 Resources/particles/dirt_8.png diff --git a/.idea/.idea.Motherload/.idea/workspace.xml b/.idea/.idea.Motherload/.idea/workspace.xml index 60db43f..b5ea7d8 100644 --- a/.idea/.idea.Motherload/.idea/workspace.xml +++ b/.idea/.idea.Motherload/.idea/workspace.xml @@ -11,28 +11,12 @@ - - - - - - - - - - - - - - - - + + - - - { + "keyToString": { + "C++ Project.Game.executor": "Debug", + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "ignore.virus.scanning.warn.message": "true", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "settings.editor.selected.configurable": "preferences.sourceCode.C++", + "vue.rearranger.settings.migration": "true" }, - "keyToStringList": { - "rider.external.source.directories": [ - "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\DecompilerCache", - "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\SourcesCache", - "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src" + "keyToStringList": { + "rider.external.source.directories": [ + "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\DecompilerCache", + "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\SourcesCache", + "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src" ] } -}]]> +} @@ -249,7 +235,8 @@ - + + - @@ -404,7 +399,8 @@ - diff --git a/Game/Levels/World/WorldLevel.cpp b/Game/Levels/World/WorldLevel.cpp index db124de..8a62ad3 100644 --- a/Game/Levels/World/WorldLevel.cpp +++ b/Game/Levels/World/WorldLevel.cpp @@ -61,8 +61,8 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), m_GridManager.GetTileAtWorldPos(Vector2f {700, -50})->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE); m_GridManager.GetTileAtWorldPos(Vector2f {750, -50})->SetTileType(GroundTileTypeManager::GetInstance()->HARD_MIDDLE); m_GridManager.GetTileAtWorldPos(Vector2f {800, -50})->SetTileType(GroundTileTypeManager::GetInstance()->HARD_RIGHT); - m_Meter = new GuiMeter("gui/main/fuel_guage.png", Vector2f{100, 100}, Vector2f{336, 146}, 100, TextureManager::GetInstance()); - Texture* test = new Texture("gui/main/fuel_guage.png"); + // m_Meter = new GuiMeter("gui/main/fuel_guage.png", Vector2f{100, 100}, Vector2f{336, 146}, 100, TextureManager::GetInstance()); + // Texture* test = new Texture("gui/main/fuel_guage.png"); } WorldLevel::~WorldLevel() { delete m_RefeulBuilding; @@ -73,7 +73,7 @@ WorldLevel::~WorldLevel() { } void WorldLevel::Update(float elapsedSec) { m_Fps = 1 / elapsedSec; - m_Meter->Update(elapsedSec); + // m_Meter->Update(elapsedSec); int mouseX, mouseY; SDL_GetMouseState(&mouseX, &mouseY); @@ -95,6 +95,23 @@ void WorldLevel::Update(float elapsedSec) { } } } + testCounter++; + std::vector ToDelete{}; + for(Particle* p : m_Particles) { + p->Update(elapsedSec); + if(p->IsDead()) { + ToDelete.emplace_back(p); + } + } + for (Particle* p : ToDelete) { + m_Particles.erase(std::remove(m_Particles.begin(), m_Particles.end(), p), m_Particles.end()); + delete p; + } + if(testCounter % 100 == 0) { + m_Particles.emplace_back(new Particle(Vector2f{100, 100}, Vector2f{float(utils::randRange(-200, 200)), 10},100, TextureManager::GetInstance()->GetTexture("particles/dirt_" + std::to_string(utils::randRange(1, 8)) + ".png"))); + } + + if (m_pSelectedTile != nullptr) { if (utils::isMouseDown(SDL_BUTTON_LEFT)) { m_pSelectedTile->SetTileType(GroundTileTypeManager::GetInstance()->AIR); @@ -180,6 +197,10 @@ void WorldLevel::Draw() const { m_RepairBuilding->Draw(); m_Player.Draw(); + for(Particle* p : m_Particles) { + p->Draw(); + } + utils::SetColor(Colors::GREEN); utils::DrawArrow(Vector2f{0, 0}, m_MousePos); @@ -194,7 +215,7 @@ void WorldLevel::Draw() const { screen->Draw(); } - m_Meter->Draw(); + // m_Meter->Draw(); } void WorldLevel::MouseMove(const Vector2f& mousePos) { m_MousePos = mousePos; diff --git a/Game/Levels/World/WorldLevel.h b/Game/Levels/World/WorldLevel.h index 53796c8..e9e7eb5 100644 --- a/Game/Levels/World/WorldLevel.h +++ b/Game/Levels/World/WorldLevel.h @@ -7,6 +7,7 @@ #include "Gui/Screens/ScreenManager.h" #include "Camera.h" #include "Gui/GuiMeter.h" +#include "Particle/Particle.h" class WorldLevel : public Level { @@ -27,6 +28,9 @@ public: std::vector m_Rects; + std::vector m_Particles; + int testCounter{ 0 }; + private: double m_Fps{ 0.0f }; diff --git a/Game/Particle/Particle.cpp b/Game/Particle/Particle.cpp index a83b9f5..bad2586 100644 --- a/Game/Particle/Particle.cpp +++ b/Game/Particle/Particle.cpp @@ -1,10 +1,19 @@ #include "pch.h" #include "Particle.h" -Particle::Particle(const Vector2f& pos, const Vector2f& velocity, Texture* pTexture): m_Position{ pos }, m_Velocity{ velocity }, m_pTexture{ pTexture } -{} +Particle::Particle(const Vector2f& pos, const Vector2f& velocity, float lifetime, Texture* pTexture): m_Position { pos }, m_Velocity { velocity }, m_LifeTime { lifetime }, + m_pTexture { pTexture } { +} void Particle::Update(float elapsedSec) { - + m_Acceleration = Vector2f { 0.0f, -9.81f * 20}; + m_Velocity += m_Acceleration * elapsedSec; + m_Position += m_Velocity * elapsedSec; + m_LifeTime -= elapsedSec; + m_Acceleration = Vector2f { 0.0f, 0.0f }; + } void Particle::Draw() const { - + m_pTexture->Draw(m_Position); +} +bool Particle::IsDead() const { + return m_LifeTime <= 0.0f; } diff --git a/Game/Particle/Particle.h b/Game/Particle/Particle.h index 5c55b21..a6237df 100644 --- a/Game/Particle/Particle.h +++ b/Game/Particle/Particle.h @@ -4,13 +4,17 @@ class Particle { public: Particle() = default; - Particle(const Vector2f& pos, const Vector2f& velocity, Texture* pTexture); + Particle(const Vector2f& pos, const Vector2f& velocity,float lifetime, Texture* pTexture); void Update(float elapsedSec); void Draw() const; + bool IsDead() const; + private: Vector2f m_Position; Vector2f m_Velocity; Vector2f m_Acceleration; + float m_LifeTime; + Texture* m_pTexture; }; diff --git a/Game/Player.cpp b/Game/Player.cpp index 51ff8db..2953179 100644 --- a/Game/Player.cpp +++ b/Game/Player.cpp @@ -166,6 +166,9 @@ void Player::Update(float elapsedTime, WorldLevel& level) { m_BobTimer = 0.0f; } + + + //check for keys if (m_State != PlayerState::Digging) { if (utils::isKeyDown(SDL_SCANCODE_W)) { diff --git a/Resources/particles/dirt_1.png b/Resources/particles/dirt_1.png new file mode 100644 index 0000000000000000000000000000000000000000..6627f8c738784532cff59c8776a6fa28b5da14dd GIT binary patch literal 364 zcmV-y0h9iTP)&bmB8Rk6sMNLwS|c%diem7_=$%jC`@h4OZ*+UHX^}}L%^tpg{+-<# z^B0@NTsJuYjyowmF#Q(0d|6?B`%QbdEQ2f&Ee+AreYilnVRJv&i|>WrVu zOod(6bXo^8=y_Vj3cX|#waQfjA%;5|=<)`bO~5pVD+J^Sh_D7&Tb=Hr4#?TC4{RM+ zm@)G%UmHj5mc9i9X&Om`-F-WQTj4$6qV~@J3my!X_qJUZ{5U^I@VhlKX)B10z7m|@^T$~g1#ow{B=B7Hn6R{96MD7 z_w9;;0MEL0olYUsA1?=*XF9Q=>y>i~WM`adOq%(w+;nUekR~|Ie}JLhwDtt0@e`*k z^Kc+f<3NR@m?rn9Z9P@AsE}Q`NBcqzr77n9c@pXhyitQ2FjRwn$L!iW$mxzu@ITf; zDA+~fDrG=`5iO$pb~`re6GuPbl?$4qfr8mHO0=sLqc(X!V!Z literal 0 HcmV?d00001 diff --git a/Resources/particles/dirt_3.png b/Resources/particles/dirt_3.png new file mode 100644 index 0000000000000000000000000000000000000000..4d73c28975abe32ae03c4899632beedfe819de43 GIT binary patch literal 340 zcmV-a0jvIrP)xNl`JAWm~cQlfvdAwqOrpP>C-KeS&)G`@?O2C+-b>kQ!L#?#!Gy zXXakd`-3e9ff-*nOdPaL5_QZ@ay1<&NRjnf`e0V~J(Kpk`X)K>@usENau~^b{dQeR z0H5o`yv@cYTTB$={2Y(WH0+p{oPhsro`V3K*R55Zq&R3g7MLkcZ$mZa44~J#RNl$1 zpPi@yLAq|{v3Z^JOqSdUXeC2*TsV? z`)U|y6nj+;Y`2Zio)&6A!|fvZl^p$yfC}BQwQ{kbVpx#Ibx^<AJ)WGAD|Enp zV;(>(=IebUH9U|VsfUb(**h0o{;KwC4jK)3nyAoI{I4OI`S;`2z5%eT@gkSv^FIIp N002ovPDHLkV1joFz32b{ literal 0 HcmV?d00001 diff --git a/Resources/particles/dirt_5.png b/Resources/particles/dirt_5.png new file mode 100644 index 0000000000000000000000000000000000000000..d6d25844ef7230d942085e7eb821230fbb4a86cb GIT binary patch literal 368 zcmV-$0gwKPP)w7a;YGeR%Rv4qm7RV33EbFH$WlJDOlkRcIY7uU)| z_9}D1av(Azl_$}$h}_ZDI*WCWV1XQ;h9Zx;p_T)obx_y2f6^3*$`HNEBq2Zq%r~*q zY#72$AWsv2>wp08dk~mh_9U>~2_iTcZ)pHsC6aFAh#uCe7Bp&#`Jk&23uFwEt3r#2 z)vJ4Q$O!UByty&9N}DJXU5kj|W&Fly8&&rxAeJ#ZjY?FW4|crYAM6W+g2C|Ciadk> O0000_hIH=T z+KC80=b<_So)0~BPk4J@-9#e;*W49z{}uDc}5VJ2XX--ow)(TyI>r-pRacr`#cKP z&SMmv=)$~^aF0A-PE>@0o^lE2NwO(aa5X0#Ng@wW^ykGr(cFBhS>IOyl1OmMac}+k zqBlPfMsB!Hu$(`TG?GZKIt9fdGdX$FC;^UE=_sdyTs9LW6GeNOv`8U{$N#;u=^c6W Y3$JGdzFTOHOaK4?07*qoM6N<$f*Dx9cK`qY literal 0 HcmV?d00001 diff --git a/Resources/particles/dirt_7.png b/Resources/particles/dirt_7.png new file mode 100644 index 0000000000000000000000000000000000000000..99ba308dec268cea9d9ae1e4b8649b8241c3693c GIT binary patch literal 426 zcmV;b0agBqP)ct=6|G$kqqq~VUQ8(G_c6Xk4 z-kGiA{4(xO`Z5nac}Qbfr31Md1d@+&#>Y(|a5ae~4ci(3^t_zPB=BV(wI%ynmr3Wl z2+%r9>;>OUz7gT>yx$X&Ai$l4W3M5dgR0J15~@Jfbv5XHeItvWr)RiB@NyWbBZVu) zg4(U`7X=~IBe$k7)|o&|M7d`wTq^8H;5JmCRj;T6YMlCY6(Hkiv%ed5<#myMmIHiP zE?LU}t=gVuPyD9VNgyD5tWyReICFsT&q`(vX^c7Ni3ol=9V={NZL_qN%^?uqSZ77( z?e;=#(@09-%r*<)Y|zshUapraz{Eb82nMDM&Me3vn^tn++8oaH$kD4TaQjbjDmAIQi57$^f%6Ac60-QM~eGy?}m)$+fA4-%I5 U<1q`v-~a#s07*qoM6N<$g267j5dZ)H literal 0 HcmV?d00001 diff --git a/Resources/particles/dirt_8.png b/Resources/particles/dirt_8.png new file mode 100644 index 0000000000000000000000000000000000000000..3917cf6f451a23e129f14dd5a424bf04cfd89cf3 GIT binary patch literal 402 zcmV;D0d4+?P)^F(k-FwygaB`zzleZKUdon@pyix#v4~ z?o2MXPt-08_T0X;*KX4$zN_zP$F*$UpK*PDV$IT-E#koLZ%Ts1O-B%5{AV?3AVxI2 zYivbcUCuqXs&yay8q<>Xl784V(3S0pz~W6d~YkOC+hR|cg4W2R)BR->-I zm!r_;y_WVDQF|*!M5BN}g!~o-g0Y4e0pqDr5g@%z&RZ)L)ilmI^&DHz#&UZJ1BpI{ zkwhE|BYGln{_w!cg=1~tM01#vLE?Pi3r;_H7u#$f+yKs;GkFUfFPr~w0767n*}c8u8!>&|T_0Q|$p8QV07*qoM6N<$f`^i?r~m)} literal 0 HcmV?d00001