Pabloader 4 år sedan
förälder
incheckning
354ae3bdfd
6 ändrade filer med 89 tillägg och 22 borttagningar
  1. 8
    0
      include/bug.h
  2. 5
    1
      include/debuggers.h
  3. 23
    0
      src/bug.cpp
  4. 1
    1
      src/button.cpp
  5. 24
    4
      src/debuggers.cpp
  6. 28
    16
      src/debuggers_states.cpp

+ 8
- 0
include/bug.h Visa fil

@@ -5,9 +5,17 @@
5 5
 
6 6
 namespace pabloader {
7 7
 class Bug : public Entity {
8
+    friend class BugParticle;
8 9
 public:
9 10
     Bug(Debuggers* game);
10 11
 };
12
+
13
+class BugParticle: public Entity {
14
+public:
15
+    BugParticle(Debuggers* game);
16
+    void ResetParticle(Bug* bug, uint8_t index);
17
+};
18
+
11 19
 }
12 20
 
13 21
 #endif

+ 5
- 1
include/debuggers.h Visa fil

@@ -26,6 +26,8 @@ private:
26 26
     std::list<Bonus*> bonuses;
27 27
     std::list<Button*> buttons;
28 28
 
29
+    std::list<BugParticle*> particles;
30
+
29 31
     std::map<BonusType, uint32_t> bonusesCatched;
30 32
 
31 33
     uint32_t bugsCatched = 0;
@@ -71,8 +73,10 @@ private:
71 73
     void SpawnPlayer();
72 74
     void SpawnProgrammer();
73 75
     void SpawnBug();
76
+    void SpawnBugParticles(Bug* bug);
74 77
     void SpawnBonus();
75 78
     Bug* CreateBug();
79
+    BugParticle* CreateBugParticle();
76 80
     Bonus* CreateBonus();
77 81
 
78 82
 private: // Gamemodes
@@ -100,7 +104,7 @@ public:
100 104
     int hoverSample;
101 105
     int clickSample;
102 106
 
103
-    const int gravity = 25;
107
+    const int gravity = 20;
104 108
 #ifdef _DEBUG
105 109
     bool debug = false;
106 110
 #endif

+ 23
- 0
src/bug.cpp Visa fil

@@ -12,4 +12,27 @@ Bug::Bug(Debuggers* game)
12 12
     DEBUG_COLOR = olc::RED;
13 13
 #endif
14 14
 }
15
+
16
+BugParticle::BugParticle(Debuggers* game)
17
+    : Entity(game, &game->enemiesSprite)
18
+{
19
+    size.x = sprite->width / 8;
20
+    size.y = sprite->height / 8;
21
+#ifdef _DEBUG
22
+    DEBUG_COLOR = olc::MAGENTA;
23
+#endif
24
+}
25
+
26
+void BugParticle::ResetParticle(Bug* bug, uint8_t index)
27
+{
28
+    vel.x = bug->vel.x + ((index & 0b01) ? 10: -10);
29
+    vel.y = bug->vel.y + ((index & 0b10) ? 10: -10);
30
+
31
+    pos = bug->pos;
32
+
33
+    rotation = bug->rotation;
34
+
35
+    tile.x = bug->tile.x * 2 + ((index & 0b01) ? 1: 0);
36
+    tile.y = bug->tile.y * 2 + ((index & 0b10) ? 1: 0);
37
+}
15 38
 }

+ 1
- 1
src/button.cpp Visa fil

@@ -33,6 +33,6 @@ void Button::Draw()
33 33
     auto sw = 8 * text.size();
34 34
     auto sh = 8;
35 35
     game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
36
-    game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text);
36
+    game->DrawString(pos.x + (size.x - sw) / 2, pos.y + (size.y - sh) / 2, text, hover ? olc::WHITE : olc::GREY);
37 37
 }
38 38
 }

+ 24
- 4
src/debuggers.cpp Visa fil

@@ -201,10 +201,6 @@ void Debuggers::DrawMenu()
201 201
     if (buttons.size() > 0) {
202 202
         buttons.front()->SetText(resumeText);
203 203
     }
204
-
205
-    if (GetKey(olc::ESCAPE).bPressed && state == STATE_PAUSE_MENU) {
206
-        SwitchState(STATE_GAME);
207
-    }
208 204
 }
209 205
 
210 206
 void Debuggers::DrawSourceCode()
@@ -254,6 +250,10 @@ void Debuggers::SwitchState(GameState newState)
254 250
     case STATE_HELP:
255 251
         buttons.push_back(new Button(this, 16, 64, 64, 16, "Back", prevState));
256 252
         break;
253
+    case STATE_GAME_OVER:
254
+        buttons.push_back(new Button(this, ScreenWidth() / 2 - 64, ScreenHeight() / 2 + 40, 128, 16, "Try again", STATE_GAME));
255
+        buttons.push_back(new Button(this, ScreenWidth() / 2 - 64, ScreenHeight() / 2 + 72, 128, 16, "Main menu", STATE_MAIN_MENU));
256
+        break;
257 257
     default:
258 258
         break;
259 259
     }
@@ -298,6 +298,15 @@ void Debuggers::SpawnBug()
298 298
     bug->ResetPosition();
299 299
 }
300 300
 
301
+void Debuggers::SpawnBugParticles(Bug* bug)
302
+{
303
+    for (auto i = 0; i < 4; i++) {
304
+        auto particle = CreateBugParticle();
305
+        particle->ResetPosition();
306
+        particle->ResetParticle(bug, i);
307
+    }
308
+}
309
+
301 310
 void Debuggers::SpawnBonus()
302 311
 {
303 312
     auto bonus = CreateBonus();
@@ -327,6 +336,17 @@ Bug* Debuggers::CreateBug()
327 336
     return bug;
328 337
 }
329 338
 
339
+BugParticle* Debuggers::CreateBugParticle()
340
+{
341
+    for (auto particle : particles) {
342
+        if (!particle->IsActive() || !particle->IsAlive())
343
+            return particle;
344
+    }
345
+    auto particle = new BugParticle(this);
346
+    particles.push_back(particle);
347
+    return particle;
348
+}
349
+
330 350
 Bonus* Debuggers::CreateBonus()
331 351
 {
332 352
     for (auto bonus : bonuses) {

+ 28
- 16
src/debuggers_states.cpp Visa fil

@@ -9,18 +9,6 @@ namespace pabloader {
9 9
 
10 10
 bool Debuggers::GamePlay(float fElapsedTime)
11 11
 {
12
-    if (state == STATE_GAME) {
13
-        if (!IsFocused() || GetKey(olc::ESCAPE).bPressed) {
14
-            SwitchState(STATE_PAUSE_MENU);
15
-            return true;
16
-        }
17
-
18
-        if (sourceCode.size() == 0) {
19
-            SwitchState(STATE_GAME_OVER);
20
-            return true;
21
-        }
22
-    }
23
-
24 12
     DrawSprite(0, 0, &backgroundSprite);
25 13
 
26 14
     uint32_t bugsSize = bugs.size();
@@ -38,6 +26,7 @@ bool Debuggers::GamePlay(float fElapsedTime)
38 26
                     if (programmer == player) {
39 27
                         bugsCatchedByPlayer++;
40 28
                     }
29
+                    SpawnBugParticles(bug);
41 30
                     bug->Kill();
42 31
                     if (state == STATE_GAME)
43 32
                         olc::SOUND::PlaySample(bugCatchSample);
@@ -54,6 +43,15 @@ bool Debuggers::GamePlay(float fElapsedTime)
54 43
         }
55 44
     }
56 45
 
46
+    for (auto& particle : particles) {
47
+        if (particle->IsInScreen()) {
48
+            particle->Update(fElapsedTime);
49
+            particle->Draw();
50
+        } else {
51
+            particle->Kill();
52
+        }
53
+    }
54
+
57 55
     for (auto& bonus : bonuses) {
58 56
         if (bonus->IsActive()) {
59 57
             bonus->Update(fElapsedTime);
@@ -64,6 +62,7 @@ bool Debuggers::GamePlay(float fElapsedTime)
64 62
                     if (aliveBugs > 1) {
65 63
                         for (auto& bug : bugs) {
66 64
                             if (bug->IsAlive() && bug->IsActive()) {
65
+                                SpawnBugParticles(bug);
67 66
                                 bug->Kill();
68 67
                                 break;
69 68
                             }
@@ -127,6 +126,12 @@ bool Debuggers::GamePlay(float fElapsedTime)
127 126
 
128 127
     if (state == STATE_MAIN_MENU) {
129 128
         DrawMenu();
129
+    } else if (state == STATE_GAME) {
130
+        if (!IsFocused() || GetKey(olc::ESCAPE).bPressed) {
131
+            SwitchState(STATE_PAUSE_MENU);
132
+        } else if (sourceCode.size() == 0) {
133
+            SwitchState(STATE_GAME_OVER);
134
+        }
130 135
     }
131 136
 
132 137
     return true;
@@ -142,6 +147,12 @@ bool Debuggers::PauseMenu(float fElapsedTime)
142 147
         }
143 148
     }
144 149
 
150
+    for (auto& particle : particles) {
151
+        if (particle->IsInScreen()) {
152
+            particle->Draw();
153
+        }
154
+    }
155
+
145 156
     for (auto& bonus : bonuses) {
146 157
         if (bonus->IsActive()) {
147 158
             bonus->Draw();
@@ -156,6 +167,10 @@ bool Debuggers::PauseMenu(float fElapsedTime)
156 167
     DrawSourceCode();
157 168
     DrawMenu();
158 169
 
170
+    if (GetKey(olc::ESCAPE).bPressed) {
171
+        SwitchState(STATE_GAME);
172
+    }
173
+
159 174
     return true;
160 175
 }
161 176
 
@@ -166,11 +181,8 @@ bool Debuggers::GameOver(float dt)
166 181
     DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 - 16, "GAME OVER", olc::RED, 4);
167 182
 
168 183
     DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 + 16, "Bugs catched: " + std::to_string(bugsCatched), olc::CYAN, 2);
169
-    DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 + 34, "Press SPACE to continue");
170
-
171
-    // TODO main menu
172 184
 
173
-    if (GetKey(olc::SPACE).bPressed) {
185
+    if (GetKey(olc::ESCAPE).bPressed) {
174 186
         SwitchState(STATE_MAIN_MENU);
175 187
     }
176 188
 

Loading…
Avbryt
Spara