Browse Source

Particles

Pabloader 5 years ago
parent
commit
354ae3bdfd
6 changed files with 89 additions and 22 deletions
  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 View File

5
 
5
 
6
 namespace pabloader {
6
 namespace pabloader {
7
 class Bug : public Entity {
7
 class Bug : public Entity {
8
+    friend class BugParticle;
8
 public:
9
 public:
9
     Bug(Debuggers* game);
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
 #endif
21
 #endif

+ 5
- 1
include/debuggers.h View File

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

+ 23
- 0
src/bug.cpp View File

12
     DEBUG_COLOR = olc::RED;
12
     DEBUG_COLOR = olc::RED;
13
 #endif
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 View File

33
     auto sw = 8 * text.size();
33
     auto sw = 8 * text.size();
34
     auto sh = 8;
34
     auto sh = 8;
35
     game->DrawRect(pos.x, pos.y, size.x, size.y, hover ? olc::WHITE : olc::GREY);
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 View File

201
     if (buttons.size() > 0) {
201
     if (buttons.size() > 0) {
202
         buttons.front()->SetText(resumeText);
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
 void Debuggers::DrawSourceCode()
206
 void Debuggers::DrawSourceCode()
254
     case STATE_HELP:
250
     case STATE_HELP:
255
         buttons.push_back(new Button(this, 16, 64, 64, 16, "Back", prevState));
251
         buttons.push_back(new Button(this, 16, 64, 64, 16, "Back", prevState));
256
         break;
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
     default:
257
     default:
258
         break;
258
         break;
259
     }
259
     }
298
     bug->ResetPosition();
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
 void Debuggers::SpawnBonus()
310
 void Debuggers::SpawnBonus()
302
 {
311
 {
303
     auto bonus = CreateBonus();
312
     auto bonus = CreateBonus();
327
     return bug;
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
 Bonus* Debuggers::CreateBonus()
350
 Bonus* Debuggers::CreateBonus()
331
 {
351
 {
332
     for (auto bonus : bonuses) {
352
     for (auto bonus : bonuses) {

+ 28
- 16
src/debuggers_states.cpp View File

9
 
9
 
10
 bool Debuggers::GamePlay(float fElapsedTime)
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
     DrawSprite(0, 0, &backgroundSprite);
12
     DrawSprite(0, 0, &backgroundSprite);
25
 
13
 
26
     uint32_t bugsSize = bugs.size();
14
     uint32_t bugsSize = bugs.size();
38
                     if (programmer == player) {
26
                     if (programmer == player) {
39
                         bugsCatchedByPlayer++;
27
                         bugsCatchedByPlayer++;
40
                     }
28
                     }
29
+                    SpawnBugParticles(bug);
41
                     bug->Kill();
30
                     bug->Kill();
42
                     if (state == STATE_GAME)
31
                     if (state == STATE_GAME)
43
                         olc::SOUND::PlaySample(bugCatchSample);
32
                         olc::SOUND::PlaySample(bugCatchSample);
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
     for (auto& bonus : bonuses) {
55
     for (auto& bonus : bonuses) {
58
         if (bonus->IsActive()) {
56
         if (bonus->IsActive()) {
59
             bonus->Update(fElapsedTime);
57
             bonus->Update(fElapsedTime);
64
                     if (aliveBugs > 1) {
62
                     if (aliveBugs > 1) {
65
                         for (auto& bug : bugs) {
63
                         for (auto& bug : bugs) {
66
                             if (bug->IsAlive() && bug->IsActive()) {
64
                             if (bug->IsAlive() && bug->IsActive()) {
65
+                                SpawnBugParticles(bug);
67
                                 bug->Kill();
66
                                 bug->Kill();
68
                                 break;
67
                                 break;
69
                             }
68
                             }
127
 
126
 
128
     if (state == STATE_MAIN_MENU) {
127
     if (state == STATE_MAIN_MENU) {
129
         DrawMenu();
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
     return true;
137
     return true;
142
         }
147
         }
143
     }
148
     }
144
 
149
 
150
+    for (auto& particle : particles) {
151
+        if (particle->IsInScreen()) {
152
+            particle->Draw();
153
+        }
154
+    }
155
+
145
     for (auto& bonus : bonuses) {
156
     for (auto& bonus : bonuses) {
146
         if (bonus->IsActive()) {
157
         if (bonus->IsActive()) {
147
             bonus->Draw();
158
             bonus->Draw();
156
     DrawSourceCode();
167
     DrawSourceCode();
157
     DrawMenu();
168
     DrawMenu();
158
 
169
 
170
+    if (GetKey(olc::ESCAPE).bPressed) {
171
+        SwitchState(STATE_GAME);
172
+    }
173
+
159
     return true;
174
     return true;
160
 }
175
 }
161
 
176
 
166
     DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 - 16, "GAME OVER", olc::RED, 4);
181
     DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 - 16, "GAME OVER", olc::RED, 4);
167
 
182
 
168
     DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 + 16, "Bugs catched: " + std::to_string(bugsCatched), olc::CYAN, 2);
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
         SwitchState(STATE_MAIN_MENU);
186
         SwitchState(STATE_MAIN_MENU);
175
     }
187
     }
176
 
188
 

Loading…
Cancel
Save