Browse Source

Player's 4-th stage

Change raw variables to vectors
Pabloader 5 years ago
parent
commit
555375ec58
12 changed files with 187 additions and 118 deletions
  1. 1
    1
      Makefile
  2. 10
    0
      TODO.md
  3. 4
    0
      include/debuggers.h
  4. 12
    11
      include/entity.h
  5. 2
    2
      include/programmer.h
  6. BIN
      res/player.png
  7. 2
    3
      src/bonus.cpp
  8. 1
    1
      src/bug.cpp
  9. 72
    40
      src/debuggers.cpp
  10. 35
    15
      src/entity.cpp
  11. 6
    4
      src/player.cpp
  12. 42
    41
      src/programmer.cpp

+ 1
- 1
Makefile View File

28
 
28
 
29
 OBJECTS=$(SOURCES:.cpp=.o)
29
 OBJECTS=$(SOURCES:.cpp=.o)
30
 SPRITES=$(IMAGES:.png=.pgs)
30
 SPRITES=$(IMAGES:.png=.pgs)
31
-RESOURCES=$(SPRITES) $(wildcard res/*.wav) src/debuggers.cpp
31
+RESOURCES=$(SPRITES) $(wildcard res/*.wav) src/programmer.cpp
32
 
32
 
33
 PACK=debuggers.pgp
33
 PACK=debuggers.pgp
34
 
34
 

+ 10
- 0
TODO.md View File

1
+ # TODO
2
+ - [ ] Bugs only catchable by 2 programmers
3
+ - [ ] Bugs only catchable with debugger
4
+ - [ ] Bugs only catchable with unit tests
5
+ - [ ] Bugs immune to debugger
6
+ - [ ] Bugs immune to unit tests
7
+ - [ ] Reduce programmers spawn rate (maybe cubic function?)
8
+ - [ ] Add obstacles to prevent flying across the screen
9
+   - [ ] Edits by customer
10
+   - [ ] QA engineers

+ 4
- 0
include/debuggers.h View File

24
     std::map<BonusType, uint32_t> bonusesCatched;
24
     std::map<BonusType, uint32_t> bonusesCatched;
25
 
25
 
26
     uint32_t bugsCatched = 0;
26
     uint32_t bugsCatched = 0;
27
+    uint32_t bugsCatchedByPlayer = 0;
27
     uint32_t bugsMissed = 0;
28
     uint32_t bugsMissed = 0;
28
 
29
 
29
     uint8_t linesOfCode = 5;
30
     uint8_t linesOfCode = 5;
71
     int bugCatchSample;
72
     int bugCatchSample;
72
 
73
 
73
     const int gravity = 30;
74
     const int gravity = 30;
75
+#ifdef _DEBUG
76
+    bool debug = true;
77
+#endif
74
 };
78
 };
75
 }
79
 }
76
 
80
 

+ 12
- 11
include/entity.h View File

13
     Debuggers* game;
13
     Debuggers* game;
14
     olc::Sprite* sprite;
14
     olc::Sprite* sprite;
15
     olc::GFX2D::Transform2D transform;
15
     olc::GFX2D::Transform2D transform;
16
-    float x, y;
17
-    float xv = 0, yv = 0;
18
-    int w, h;
16
+    olc::vf2d pos;
17
+    olc::vf2d vel;
18
+    olc::vi2d size;
19
 
19
 
20
-    uint8_t tileX = 0;
21
-    uint8_t tileY = 0;
22
-
23
-    uint8_t tileCols = 4;
24
-    uint8_t tileRows = 4;
20
+    olc::v2d_generic<uint8_t> tile{ 0, 0 };
21
+    olc::v2d_generic<uint8_t> tileDim{ 4, 4 };
25
 
22
 
26
     float rotation = 0;
23
     float rotation = 0;
27
     float rotationSpeed = 0;
24
     float rotationSpeed = 0;
25
+    bool alive = true;
28
 #ifdef _DEBUG
26
 #ifdef _DEBUG
29
     olc::Pixel DEBUG_COLOR;
27
     olc::Pixel DEBUG_COLOR;
30
 #endif
28
 #endif
36
     virtual void Update(float dt);
34
     virtual void Update(float dt);
37
     virtual void Draw();
35
     virtual void Draw();
38
 
36
 
39
-    float GetX() { return x; }
40
-    float GetY() { return y; }
37
+    float GetX() { return pos.x; }
38
+    float GetY() { return pos.y; }
39
+
40
+    bool IsInScreen();
41
 
41
 
42
     bool Collides(Entity* entity);
42
     bool Collides(Entity* entity);
43
-    float Dist(Entity* entity) { return std::hypot(x - entity->x, y - entity->y); }
43
+    float Dist(Entity* entity) { return (pos - entity->pos).mag(); }
44
 
44
 
45
     virtual bool IsActive();
45
     virtual bool IsActive();
46
+    virtual bool IsAlive();
46
     virtual void ResetPosition();
47
     virtual void ResetPosition();
47
     virtual void Kill();
48
     virtual void Kill();
48
 };
49
 };

+ 2
- 2
include/programmer.h View File

21
     virtual void Update(float dt) override;
21
     virtual void Update(float dt) override;
22
     virtual void Draw() override;
22
     virtual void Draw() override;
23
     bool IsOnGround() { return onGround; }
23
     bool IsOnGround() { return onGround; }
24
-    uint8_t GetSkin() { return tileX; }
25
-    void UpgradeLevel() { tileX = std::min(tileX + 1, tileCols - 1); }
24
+    uint8_t GetSkin() { return tile.x; }
25
+    void UpgradeLevel() { tile.x = std::min(tile.x + 1, tileDim.x - 1); }
26
 };
26
 };
27
 }
27
 }
28
 
28
 

BIN
res/player.png View File


+ 2
- 3
src/bonus.cpp View File

7
 Bonus::Bonus(Debuggers* game)
7
 Bonus::Bonus(Debuggers* game)
8
     : Entity(game, &game->bonusesSprite)
8
     : Entity(game, &game->bonusesSprite)
9
 {
9
 {
10
-    tileCols = 1;
11
-    tileRows = 3;
10
+    tileDim = { 1, 3 };
12
 #ifdef _DEBUG
11
 #ifdef _DEBUG
13
     DEBUG_COLOR = olc::YELLOW;
12
     DEBUG_COLOR = olc::YELLOW;
14
 #endif
13
 #endif
16
 
15
 
17
 BonusType Bonus::GetType()
16
 BonusType Bonus::GetType()
18
 {
17
 {
19
-    return static_cast<BonusType>(tileX * 4 + tileY);
18
+    return static_cast<BonusType>(tile.x * 4 + tile.y);
20
 }
19
 }
21
 
20
 
22
 void Bonus::ResetPosition()
21
 void Bonus::ResetPosition()

+ 1
- 1
src/bug.cpp View File

7
 Bug::Bug(Debuggers* game)
7
 Bug::Bug(Debuggers* game)
8
     : Entity(game, &game->enemiesSprite)
8
     : Entity(game, &game->enemiesSprite)
9
 {
9
 {
10
-    tileCols = 1;
10
+    tileDim.x = 1;
11
 #ifdef _DEBUG
11
 #ifdef _DEBUG
12
     DEBUG_COLOR = olc::RED;
12
     DEBUG_COLOR = olc::RED;
13
 #endif
13
 #endif

+ 72
- 40
src/debuggers.cpp View File

1
 #include "debuggers.h"
1
 #include "debuggers.h"
2
+#include <algorithm>
2
 #include <cmath>
3
 #include <cmath>
3
 #include <ctime>
4
 #include <ctime>
4
 #include <iostream>
5
 #include <iostream>
11
     return str;
12
     return str;
12
 }
13
 }
13
 
14
 
15
+template <>
16
+float Debuggers::GetRandom<float>(float from, float to)
17
+{
18
+    if (from > to) {
19
+        std::swap(from, to);
20
+    }
21
+    std::uniform_real_distribution<float> dist(from, to);
22
+    return dist(generator);
23
+}
24
+
25
+template <>
26
+int Debuggers::GetRandom<int>(int from, int to)
27
+{
28
+    if (from > to) {
29
+        std::swap(from, to);
30
+    }
31
+    std::uniform_int_distribution<int> dist(from, to - 1);
32
+    return dist(generator);
33
+}
34
+
14
 bool Debuggers::OnUserCreate()
35
 bool Debuggers::OnUserCreate()
15
 {
36
 {
16
     SetPixelMode(olc::Pixel::MASK);
37
     SetPixelMode(olc::Pixel::MASK);
79
         return false;
100
         return false;
80
     }
101
     }
81
 
102
 
82
-    fileName = "src/debuggers.cpp";
103
+    fileName = "src/programmer.cpp";
83
     auto streamBuffer = pack.GetStreamBuffer(fileName);
104
     auto streamBuffer = pack.GetStreamBuffer(fileName);
84
     if (streamBuffer.data == nullptr) {
105
     if (streamBuffer.data == nullptr) {
85
         std::cerr << "[Load source] File " << fileName << " is not found" << std::endl;
106
         std::cerr << "[Load source] File " << fileName << " is not found" << std::endl;
117
     DrawSprite(0, 0, &backgroundSprite);
138
     DrawSprite(0, 0, &backgroundSprite);
118
 
139
 
119
     uint32_t bugsSize = bugs.size();
140
     uint32_t bugsSize = bugs.size();
141
+    auto aliveBugs = std::count_if(bugs.begin(), bugs.end(), [](Bug* bug) { return bug->IsActive() && bug->IsAlive(); });
142
+
143
+    Programmer* player = programmers.at(0);
120
 
144
 
121
     for (auto bug : bugs) {
145
     for (auto bug : bugs) {
122
         if (bug->IsActive()) {
146
         if (bug->IsActive()) {
125
             for (auto& programmer : programmers) {
149
             for (auto& programmer : programmers) {
126
                 if (programmer->Collides(bug)) {
150
                 if (programmer->Collides(bug)) {
127
                     bugsCatched++;
151
                     bugsCatched++;
152
+                    if (programmer == player) {
153
+                        bugsCatchedByPlayer++;
154
+                    }
128
                     bug->Kill();
155
                     bug->Kill();
129
                     olc::SOUND::PlaySample(bugCatchSample);
156
                     olc::SOUND::PlaySample(bugCatchSample);
130
                     SpawnBug();
157
                     SpawnBug();
131
                 }
158
                 }
132
             }
159
             }
133
-        } else {
160
+        } else if (bug->IsAlive()) {
134
             bugsMissed++;
161
             bugsMissed++;
135
             sourceCode.erase(sourceCode.begin());
162
             sourceCode.erase(sourceCode.begin());
136
 
163
 
139
         }
166
         }
140
     }
167
     }
141
 
168
 
142
-    Programmer* player = programmers.at(0);
143
-
144
     for (auto& bonus : bonuses) {
169
     for (auto& bonus : bonuses) {
145
         if (bonus->IsActive()) {
170
         if (bonus->IsActive()) {
146
             bonus->Update(fElapsedTime);
171
             bonus->Update(fElapsedTime);
147
             bonus->Draw();
172
             bonus->Draw();
148
             if (player->Collides(bonus)) {
173
             if (player->Collides(bonus)) {
149
-                bonusesCatched[bonus->GetType()]++;
174
+                auto type = bonus->GetType();
175
+                if (type == BONUS_DOCUMENTATION) {
176
+                    if (aliveBugs > 1) {
177
+                        for (auto& bug : bugs) {
178
+                            if (bug->IsAlive() && bug->IsActive()) {
179
+                                bug->Kill();
180
+                                break;
181
+                            }
182
+                        }
183
+                    }
184
+                } else {
185
+                    bonusesCatched[type]++;
186
+                }
150
                 bonus->Kill();
187
                 bonus->Kill();
188
+                // TODO play pickup sound
151
             }
189
             }
190
+        } else {
191
+            bonus->Kill();
152
         }
192
         }
153
     }
193
     }
154
 
194
 
165
         SpawnBug();
205
         SpawnBug();
166
     }
206
     }
167
 
207
 
168
-    if (bugsCatched >= 15 * bugsSize * programmers.size() && player->GetSkin() < bugsSize) {
208
+    auto programmersSize = programmers.size();
209
+    if (bugsCatchedByPlayer >= 15 * programmersSize * programmersSize && player->GetSkin() < programmersSize) {
169
         player->UpgradeLevel();
210
         player->UpgradeLevel();
170
         SpawnBug();
211
         SpawnBug();
171
-        SpawnBug();
172
         SpawnProgrammer();
212
         SpawnProgrammer();
173
     }
213
     }
174
 
214
 
175
-#ifdef _DEBUG
176
-    DrawString(1, 37, "Bugs: " + std::to_string(bugs.size()));
177
-    DrawString(1, 46, "Progs: " + std::to_string(programmers.size()));
178
-    DrawString(1, 55, "Bonuses: " + std::to_string(bonuses.size()));
179
-    DrawString(1, 64, "DBG: " + std::to_string(bonusesCatched[BONUS_DEBUGGER]));
180
-    DrawString(1, 73, "UTS: " + std::to_string(bonusesCatched[BONUS_UNIT_TEST]));
181
-    DrawString(1, 82, "DOC: " + std::to_string(bonusesCatched[BONUS_DOCUMENTATION]));
182
-    
183
-    if (GetKey(olc::B).bPressed) {
215
+    if (bugsCatched > 10 * bugsSize) {
184
         SpawnBug();
216
         SpawnBug();
185
     }
217
     }
186
-    if (GetKey(olc::N).bPressed) {
218
+
219
+    if (GetRandom<float>() < 0.01 * fElapsedTime) {
187
         SpawnBonus();
220
         SpawnBonus();
188
     }
221
     }
189
-    if (GetKey(olc::P).bPressed) {
190
-        SpawnProgrammer();
222
+
223
+#ifdef _DEBUG
224
+    if (debug) {
225
+        DrawString(1, 37, "Bg: " + std::to_string(bugs.size()) + " Ab: " + std::to_string(aliveBugs));
226
+        DrawString(1, 46, "Ps: " + std::to_string(programmers.size()) + " Bn: " + std::to_string(bonuses.size()));
227
+        DrawString(1, 55, "Cp: " + std::to_string(bugsCatchedByPlayer));
228
+        DrawString(1, 64, "DBG: " + std::to_string(bonusesCatched[BONUS_DEBUGGER]));
229
+        DrawString(1, 73, "UTS: " + std::to_string(bonusesCatched[BONUS_UNIT_TEST]));
230
+
231
+        if (GetKey(olc::B).bPressed) {
232
+            SpawnBug();
233
+        }
234
+        if (GetKey(olc::N).bPressed) {
235
+            SpawnBonus();
236
+        }
237
+        if (GetKey(olc::P).bPressed) {
238
+            SpawnProgrammer();
239
+        }
240
+    }
241
+    if (GetKey(olc::K0).bReleased) {
242
+        debug = !debug;
191
     }
243
     }
192
 #endif
244
 #endif
193
     DrawString(1, 1, "A,S,D - move; SPACE - jump", olc::CYAN);
245
     DrawString(1, 1, "A,S,D - move; SPACE - jump", olc::CYAN);
240
 Bug* Debuggers::CreateBug()
292
 Bug* Debuggers::CreateBug()
241
 {
293
 {
242
     for (auto bug : bugs) {
294
     for (auto bug : bugs) {
243
-        if (!bug->IsActive())
295
+        if (!bug->IsActive() || !bug->IsAlive())
244
             return bug;
296
             return bug;
245
     }
297
     }
246
     auto bug = new Bug(this);
298
     auto bug = new Bug(this);
264
     olc::SOUND::DestroyAudio();
316
     olc::SOUND::DestroyAudio();
265
     return true;
317
     return true;
266
 }
318
 }
267
-
268
-template <>
269
-float Debuggers::GetRandom<float>(float from, float to)
270
-{
271
-    if (from > to) {
272
-        std::swap(from, to);
273
-    }
274
-    std::uniform_real_distribution<double> dist(from, to);
275
-    return dist(generator);
276
-}
277
-
278
-template <>
279
-int Debuggers::GetRandom<int>(int from, int to)
280
-{
281
-    if (from > to) {
282
-        std::swap(from, to);
283
-    }
284
-    std::uniform_int_distribution<int> dist(from, to - 1);
285
-    return dist(generator);
286
-}
287
 }
319
 }

+ 35
- 15
src/entity.cpp View File

2
 #include "debuggers.h"
2
 #include "debuggers.h"
3
 
3
 
4
 namespace pabloader {
4
 namespace pabloader {
5
-Entity::Entity(Debuggers* game, olc::Sprite* s): sprite(s), w(s->width / 4), h(s->height / 4)
5
+Entity::Entity(Debuggers* game, olc::Sprite* s)
6
+    : sprite(s)
7
+    , pos(0, 0)
8
+    , vel(0, 0)
9
+    , size(s->width / 4, s->height / 4)
6
 {
10
 {
7
     Entity::game = game;
11
     Entity::game = game;
8
 }
12
 }
9
 
13
 
10
 void Entity::Update(float dt)
14
 void Entity::Update(float dt)
11
 {
15
 {
12
-    y += yv * dt;
13
-    yv += game->gravity * dt;
16
+    pos += vel * dt;
17
+    pos.y += game->gravity * dt;
14
 
18
 
15
     rotation += rotationSpeed * dt;
19
     rotation += rotationSpeed * dt;
16
 
20
 
17
     transform.Reset();
21
     transform.Reset();
18
     transform.Rotate(rotation);
22
     transform.Rotate(rotation);
19
-    transform.Translate(x, y);
23
+    transform.Translate(pos.x, pos.y);
20
 }
24
 }
21
 
25
 
22
 bool Entity::Collides(Entity* entity)
26
 bool Entity::Collides(Entity* entity)
23
 {
27
 {
24
-    bool collidesX = (x - w / 2 < entity->x + entity->w / 2) && (entity->x - entity->w / 2 < x + w / 2);
25
-    bool collidesY = (y - h / 2 < entity->y + entity->h / 2) && (entity->y - entity->h / 2 < y + h / 2);
28
+    bool collidesX = (pos.x - size.x / 2 < entity->pos.x + entity->size.x / 2) && (entity->pos.x - entity->size.x / 2 < pos.x + size.x / 2);
29
+    bool collidesY = (pos.y - size.y / 2 < entity->pos.y + entity->size.y / 2) && (entity->pos.y - entity->size.y / 2 < pos.y + size.y / 2);
26
     return collidesX && collidesY;
30
     return collidesX && collidesY;
27
 }
31
 }
28
 
32
 
29
 void Entity::Draw()
33
 void Entity::Draw()
30
 {
34
 {
31
 #ifdef _DEBUG
35
 #ifdef _DEBUG
32
-    game->DrawRect(x - w / 2, y - h / 2, w, h, DEBUG_COLOR);
36
+    if (game->debug) {
37
+        game->DrawRect(pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y, DEBUG_COLOR);
38
+    }
33
 #endif
39
 #endif
34
-    olc::GFX2D::DrawPartialSprite(sprite, tileX * w, tileY * h, w, h, transform, true);
40
+    olc::GFX2D::DrawPartialSprite(sprite, tile.x * size.x, tile.y * size.y, size.x, size.y, transform, true);
35
 }
41
 }
36
 
42
 
37
 bool Entity::IsActive()
43
 bool Entity::IsActive()
38
 {
44
 {
39
-    return y - h / 2 < game->GameScreenHeight();
45
+    return pos.y - size.y / 2 < game->GameScreenHeight();
46
+}
47
+bool Entity::IsInScreen()
48
+{
49
+    return pos.x > -size.x / 2 && pos.y > -size.y
50
+        && pos.x < game->ScreenWidth() + size.x / 2
51
+        && pos.x < game->ScreenHeight() + size.y / 2;
52
+}
53
+
54
+bool Entity::IsAlive()
55
+{
56
+    return alive;
40
 }
57
 }
41
 
58
 
42
 void Entity::ResetPosition()
59
 void Entity::ResetPosition()
43
 {
60
 {
44
-    y = game->GetRandom(-20, -game->ScreenHeight());
45
-    x = game->GetRandom(w, game->ScreenWidth() - w);
46
-    yv = 0;
47
-    tileX = game->GetRandom<int>(0, tileCols);
48
-    tileY = game->GetRandom<int>(0, tileRows);
61
+    pos.x = game->GetRandom(size.x, game->ScreenWidth() - size.x);
62
+    pos.y = game->GetRandom(-20, -game->ScreenHeight());
63
+    vel.x = 0;
64
+    vel.y = 0;
65
+    tile.x = game->GetRandom<int>(0, tileDim.x);
66
+    tile.y = game->GetRandom<int>(0, tileDim.y);
49
     rotationSpeed = game->GetRandom(-3.14f, 3.14f);
67
     rotationSpeed = game->GetRandom(-3.14f, 3.14f);
68
+    alive = true;
50
 }
69
 }
51
 
70
 
52
 void Entity::Kill()
71
 void Entity::Kill()
53
 {
72
 {
54
-    y = game->ScreenHeight() + 100;
73
+    pos.y = game->ScreenHeight() + 100;
74
+    alive = false;
55
 }
75
 }
56
 }
76
 }

+ 6
- 4
src/player.cpp View File

19
     walkRight = game->GetKey(olc::D).bHeld;
19
     walkRight = game->GetKey(olc::D).bHeld;
20
     moveDown = game->GetKey(olc::S).bHeld;
20
     moveDown = game->GetKey(olc::S).bHeld;
21
 #ifdef _DEBUG
21
 #ifdef _DEBUG
22
-    // game->DrawString(1, 19, std::to_string(x) + " " + std::to_string(y));
23
-    // game->DrawString(1, 28, std::to_string(xv) + " " + std::to_string(yv));
22
+    if (game->debug) {
23
+        // game->DrawString(1, 19, std::to_string(x) + " " + std::to_string(y));
24
+        // game->DrawString(1, 28, std::to_string(xv) + " " + std::to_string(yv));
24
 
25
 
25
-    if (game->GetKey(olc::J).bPressed) {
26
-        tileX = (tileX + 1) % tileCols;
26
+        if (game->GetKey(olc::J).bPressed) {
27
+            tile.x = (tile.x + 1) % tileDim.x;
28
+        }
27
     }
29
     }
28
 #endif
30
 #endif
29
 }
31
 }

+ 42
- 41
src/programmer.cpp View File

7
 Programmer::Programmer(Debuggers* game)
7
 Programmer::Programmer(Debuggers* game)
8
     : Entity(game, &game->playerSprite)
8
     : Entity(game, &game->playerSprite)
9
 {
9
 {
10
-    x = (game->ScreenWidth() - w) / 2;
11
-    y = h / 2;
12
-    tileCols = 3;
10
+    pos = { (float)(game->ScreenWidth() - size.x) / 2, (float)size.y / 2 };
13
 
11
 
14
 #ifdef _DEBUG
12
 #ifdef _DEBUG
15
     DEBUG_COLOR = olc::GREEN;
13
     DEBUG_COLOR = olc::GREEN;
22
     float dist = 1e100;
20
     float dist = 1e100;
23
     for (auto bug : game->bugs) {
21
     for (auto bug : game->bugs) {
24
         float d = bug->Dist(this);
22
         float d = bug->Dist(this);
25
-        if (d < dist && bug->GetY() > 0) {
23
+        if (d < dist && bug->IsInScreen() && bug->IsAlive()) {
26
             dist = d;
24
             dist = d;
27
             closest = bug;
25
             closest = bug;
28
         }
26
         }
29
     }
27
     }
30
 
28
 
31
     if (closest != nullptr) {
29
     if (closest != nullptr) {
32
-        doJump = closest->GetY() < y;
33
-        moveDown = closest->GetY() > y;
34
-        walkLeft = closest->GetX() < x;
35
-        walkRight = closest->GetX() > x;
30
+        doJump = closest->GetY() < pos.y;
31
+        moveDown = closest->GetY() > pos.y;
32
+        walkLeft = closest->GetX() < pos.x;
33
+        walkRight = closest->GetX() > pos.x;
36
     } else {
34
     } else {
37
         doJump = false;
35
         doJump = false;
38
         walkLeft = false;
36
         walkLeft = false;
43
 
41
 
44
 void Programmer::Update(float dt)
42
 void Programmer::Update(float dt)
45
 {
43
 {
46
-    x += xv * dt;
47
-    y += yv * dt;
44
+    pos += vel * dt;
48
 
45
 
49
-    xv *= 1 - 0.2 * dt;
46
+    vel.x *= 1 - 0.2 * dt;
50
 
47
 
51
     if (!onGround) {
48
     if (!onGround) {
52
-        yv += (70 + tileX * 30) * dt;
53
-        yv *= 1 - 0.05 * dt;
49
+        vel.y += (70 + tile.x * 30) * dt;
50
+        vel.y *= 1 - 0.05 * dt;
54
     } else {
51
     } else {
55
-        xv *= 1 - dt;
52
+        vel.x *= 1 - 3 * dt;
56
     }
53
     }
57
 
54
 
58
     transform.Reset();
55
     transform.Reset();
60
     bool userInput = false;
57
     bool userInput = false;
61
 
58
 
62
     if (doJump && onGround) {
59
     if (doJump && onGround) {
63
-        yv = -100 - 50 * tileX;
60
+        vel.y = -100 - tile.x * 30;
64
         userInput = true;
61
         userInput = true;
65
     }
62
     }
66
 
63
 
67
     if (walkLeft) {
64
     if (walkLeft) {
68
-        xv -= (150 + tileX * 50) * dt;
65
+        vel.x -= (150 + tile.x * 30) * dt;
69
         userInput = true;
66
         userInput = true;
70
     }
67
     }
71
 
68
 
72
     if (walkRight) {
69
     if (walkRight) {
73
-        xv += (150 + tileX * 50) * dt;
70
+        vel.x += (150 + tile.x * 30) * dt;
74
         userInput = true;
71
         userInput = true;
75
     }
72
     }
76
 
73
 
77
     if (moveDown) {
74
     if (moveDown) {
78
         if (!onGround) {
75
         if (!onGround) {
79
-            yv += (150 + tileX * 50) * dt;
76
+            vel.y += (150 + tile.x * 30) * dt;
80
         }
77
         }
81
         userInput = true;
78
         userInput = true;
82
     }
79
     }
83
 
80
 
84
-    if (x > game->ScreenWidth() - w / 2) {
85
-        xv *= -0.3;
86
-        x = game->ScreenWidth() - w / 2;
81
+    if (pos.x > game->ScreenWidth() - size.x / 2) {
82
+        vel.x *= -0.3;
83
+        pos.x = game->ScreenWidth() - size.x / 2;
87
         olc::SOUND::PlaySample(game->playerFallSample);
84
         olc::SOUND::PlaySample(game->playerFallSample);
88
     }
85
     }
89
 
86
 
90
-    if (x < w / 2) {
91
-        xv *= -0.3;
92
-        x = w / 2;
87
+    if (pos.x < size.x / 2) {
88
+        vel.x *= -0.3;
89
+        pos.x = size.x / 2;
93
         olc::SOUND::PlaySample(game->playerFallSample);
90
         olc::SOUND::PlaySample(game->playerFallSample);
94
     }
91
     }
95
 
92
 
96
-    if (y > game->GameScreenHeight() - h / 2) {
97
-        yv *= -0.3;
93
+    auto groundLevel = game->GameScreenHeight() - size.y / 2 - 1;
98
 
94
 
99
-        if (std::abs(yv) < 2) {
100
-            yv = 0;
95
+    if (pos.y > groundLevel) {
96
+        vel.y *= -0.3;
97
+
98
+        if (std::abs(vel.y) < 2) {
99
+            vel.y = 0;
101
         }
100
         }
102
-        y = game->GameScreenHeight() - h / 2;
101
+        pos.y = groundLevel;
103
     }
102
     }
104
 
103
 
105
-    bool newOnGround = y >= game->GameScreenHeight() - h / 2;
104
+    bool newOnGround = pos.y >= groundLevel;
106
     if (newOnGround && !onGround) {
105
     if (newOnGround && !onGround) {
107
         olc::SOUND::PlaySample(game->playerFallSample);
106
         olc::SOUND::PlaySample(game->playerFallSample);
108
     }
107
     }
109
     onGround = newOnGround;
108
     onGround = newOnGround;
110
 
109
 
111
     if (onGround) {
110
     if (onGround) {
112
-        tileY = 2;
111
+        tile.y = 2;
113
         tileCounter = static_cast<uint32_t>(0.1 / dt);
112
         tileCounter = static_cast<uint32_t>(0.1 / dt);
114
     } else if (tileCounter <= 0) {
113
     } else if (tileCounter <= 0) {
115
-        tileY = 0;
114
+        tile.y = 0;
116
     } else {
115
     } else {
117
         tileCounter--;
116
         tileCounter--;
118
     }
117
     }
119
 
118
 
120
-    if (!userInput && std::abs(xv) < 2) {
121
-        xv = 0;
119
+    if (!userInput && std::abs(vel.x) < 2) {
120
+        vel.x = 0;
122
     }
121
     }
123
-    if (xv < 0) {
122
+    if (vel.x < 0) {
124
         transform.Scale(-1, 1);
123
         transform.Scale(-1, 1);
125
     }
124
     }
126
-    transform.Translate(x, y);
125
+    transform.Translate(pos.x, pos.y);
127
 }
126
 }
128
 
127
 
129
 void Programmer::Draw()
128
 void Programmer::Draw()
130
 {
129
 {
131
-    int xOff = xv < 0 ? -1 : 0;
132
-    if (std::abs(xv) < 1) {
133
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, tileX * w + xOff, h + tileY * h, w - xOff, h, transform, true);
130
+    int xOff = vel.x < 0 ? -1 : 0;
131
+    if (std::abs(vel.x) < 1) {
132
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, size.y + tile.y * size.y, size.x - xOff, size.y, transform, true);
134
     } else {
133
     } else {
135
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, tileX * w + xOff, 0 + tileY * h, w - xOff, h, transform, true);
134
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, tile.x * size.x + xOff, 0 + tile.y * size.y, size.x - xOff, size.y, transform, true);
136
     }
135
     }
137
 #ifdef _DEBUG
136
 #ifdef _DEBUG
138
-    game->DrawRect(x - w / 2, y - h / 2, w, h, DEBUG_COLOR);
137
+    if (game->debug) {
138
+        game->DrawRect(pos.x - size.x / 2, pos.y - size.y / 2, size.x, size.y, DEBUG_COLOR);
139
+    }
139
 #endif
140
 #endif
140
 }
141
 }
141
 }
142
 }

Loading…
Cancel
Save