Browse Source

Score counting, display source code, graphics

Pabloader 4 years ago
parent
commit
0a30e66e8d

+ 1
- 1
.gitignore View File

@@ -1,5 +1,5 @@
1 1
 awoorwa
2
-awoorwae
2
+debuggers
3 3
 *.exe
4 4
 *.o
5 5
 *.pgp

+ 3
- 3
.vscode/launch.json View File

@@ -26,10 +26,10 @@
26 26
             ]
27 27
         },
28 28
         {
29
-            "name": "(gdb-win) Launch Awoorwae",
29
+            "name": "(gdb-win) Launch Debuggers",
30 30
             "type": "cppdbg",
31 31
             "request": "launch",
32
-            "program": "${workspaceFolder}\\awoorwae.exe",
32
+            "program": "${workspaceFolder}\\debuggers.exe",
33 33
             "args": [],
34 34
             "stopAtEntry": false,
35 35
             "cwd": "${workspaceFolder}",
@@ -49,7 +49,7 @@
49 49
             "name": "(gdb) Launch",
50 50
             "type": "cppdbg",
51 51
             "request": "launch",
52
-            "program": "${workspaceFolder}/awoorwae",
52
+            "program": "${workspaceFolder}/debuggers",
53 53
             "args": [],
54 54
             "stopAtEntry": false,
55 55
             "cwd": "${workspaceFolder}",

+ 4
- 4
Makefile View File

@@ -3,7 +3,7 @@ LDFLAGS=
3 3
 INCLUDES=-Iinclude -Ipge/include
4 4
 ifeq ($(OS),Windows_NT)
5 5
 	LIBRARIES=-luser32 -lgdi32 -lopengl32 -lgdiplus -lwinmm
6
-	EXECUTABLE=awoorwae.exe
6
+	EXECUTABLE=debuggers.exe
7 7
 	PACKER=packer/packer.exe
8 8
 ifneq ($(DEBUG),1)
9 9
 	LDFLAGS+=--machine=windows
@@ -13,7 +13,7 @@ endif
13 13
 	MAKE=mingw32-make
14 14
 else
15 15
 	LIBRARIES=-lX11 -lGL -lpthread -lpng -lasound
16
-	EXECUTABLE=awoorwae
16
+	EXECUTABLE=debuggers
17 17
 	PACKER=packer/packer
18 18
 	CC=gcc
19 19
 	CPP=g++
@@ -28,9 +28,9 @@ IMAGES=$(wildcard res/*.png)
28 28
 
29 29
 OBJECTS=$(SOURCES:.cpp=.o)
30 30
 SPRITES=$(IMAGES:.png=.pgs)
31
-RESOURCES=$(SPRITES) $(wildcard res/*.wav)
31
+RESOURCES=$(SPRITES) $(wildcard res/*.wav) src/debuggers.cpp
32 32
 
33
-PACK=awoorwae.pgp
33
+PACK=debuggers.pgp
34 34
 
35 35
 all: $(SOURCES) $(PACK) $(EXECUTABLE)
36 36
 

+ 0
- 43
include/awoorwae.h View File

@@ -1,43 +0,0 @@
1
-#ifndef AWOORWAE_H
2
-#define AWOORWAE_H
3
-
4
-#include "olcPixelGameEngine.h"
5
-#include "olcPGEX_Sound.h"
6
-#include "player.h"
7
-#include "bug.h"
8
-#include <vector>
9
-
10
-namespace pabloader {
11
-class Awoorwae : public olc::PixelGameEngine {
12
-private:
13
-    Player* player;
14
-    std::vector<Bug*> bugs;
15
-
16
-public:
17
-    Awoorwae()
18
-    {
19
-        sAppName = "Awoorwa";
20
-    }
21
-
22
-    ~Awoorwae()
23
-    {
24
-        delete player;
25
-    }
26
-
27
-public:
28
-    bool OnUserCreate() override;
29
-    bool OnUserUpdate(float fElapsedTime) override;
30
-    bool OnUserDestroy() override;
31
-
32
-private:
33
-    Bug* SpawnBug();
34
-
35
-public:
36
-    olc::ResourcePack pack;
37
-    olc::Sprite playerSprite;
38
-    olc::Sprite enemiesSprite;
39
-    int playerFallSample;
40
-};
41
-}
42
-
43
-#endif

+ 2
- 1
include/bug.h View File

@@ -6,11 +6,12 @@
6 6
 namespace pabloader {
7 7
 class Bug : public Entity {
8 8
 public:
9
-    Bug(Awoorwae* game);
9
+    Bug(Debuggers* game);
10 10
     void Update(float dt) override;
11 11
     void Draw() override;
12 12
     bool IsActive();
13 13
     void ResetPosition();
14
+    void Kill();
14 15
 
15 16
 private:
16 17
     int kind = 0;

+ 51
- 0
include/debuggers.h View File

@@ -0,0 +1,51 @@
1
+#ifndef DEBUGGERS_H
2
+#define DEBUGGERS_H
3
+
4
+#include "bug.h"
5
+#include "olcPGEX_Sound.h"
6
+#include "olcPixelGameEngine.h"
7
+#include "player.h"
8
+#include <vector>
9
+#include <string>
10
+
11
+namespace pabloader {
12
+class Debuggers : public olc::PixelGameEngine {
13
+private:
14
+    Player* player;
15
+    std::vector<Bug*> bugs;
16
+    uint32_t bugsCatched = 0;
17
+    uint32_t bugsMissed = 0;
18
+
19
+    uint8_t linesOfCode = 5;
20
+    uint16_t totalLines;
21
+    std::vector<std::string> sourceCode;
22
+
23
+public:
24
+    Debuggers()
25
+    {
26
+        sAppName = "Debuggers";
27
+    }
28
+
29
+public:
30
+    bool OnUserCreate() override;
31
+    bool OnUserUpdate(float fElapsedTime) override;
32
+    bool OnUserDestroy() override;
33
+    int GameScreenHeight() { return ScreenHeight() - linesOfCode * 10; };
34
+
35
+private:
36
+    void SpawnBug();
37
+    Bug* CreateBug();
38
+    bool GameOver();
39
+
40
+public:
41
+    olc::ResourcePack pack;
42
+    olc::Sprite playerSprite;
43
+    olc::Sprite enemiesSprite;
44
+    olc::Sprite backgroundSprite;
45
+    int playerFallSample;
46
+    int bugFallSample;
47
+    int bugCatchSample;
48
+};
49
+}
50
+
51
+#endif

+ 3
- 3
include/entity.h View File

@@ -5,18 +5,18 @@
5 5
 #include "olcPixelGameEngine.h"
6 6
 
7 7
 namespace pabloader {    
8
-class Awoorwae;
8
+class Debuggers;
9 9
 
10 10
 class Entity {
11 11
 protected:
12
-    Awoorwae* game;
12
+    Debuggers* game;
13 13
     olc::GFX2D::Transform2D transform;
14 14
     float x, y;
15 15
     float xv, yv;
16 16
     int w, h;
17 17
 
18 18
 public:
19
-    Entity(Awoorwae* game_);
19
+    Entity(Debuggers* game_);
20 20
     virtual ~Entity(){};
21 21
     
22 22
     virtual void Update(float dt) = 0;

+ 10
- 7
include/player.h View File

@@ -7,16 +7,19 @@
7 7
 namespace pabloader {
8 8
 
9 9
 class Player : public Entity {
10
+private:
11
+    uint8_t tile = 0;
12
+    uint8_t skin = 0;
13
+    uint32_t tileCounter = 0;
14
+    bool onGround = false;
15
+
10 16
 public:
11
-    Player(Awoorwae* game);
17
+    Player(Debuggers* game);
12 18
     void Update(float dt) override;
13 19
     void Draw() override;
14
-
15
-private:
16
-    int tile = 0;
17
-    int skin = 0;
18
-    int tileCounter = 0;
19
-    bool onGround = false;
20
+    bool IsOnGround() { return onGround; }
21
+    uint8_t GetSkin() { return skin; }
22
+    void UpgradeLevel() { skin = std::min(skin + 1, 3); }
20 23
 };
21 24
 }
22 25
 

+ 2
- 2
pge/include/olcPixelGameEngine.h View File

@@ -377,10 +377,10 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
377 377
 		// Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
378 378
 		void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
379 379
 		// Draws an entire sprite at location (x,y)
380
-		void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1);
380
+		void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1, bool centered = false);
381 381
 		// Draws an area of a sprite at location (x,y), where the
382 382
 		// selected area is (ox,oy) to (ox+w,oy+h)
383
-		void DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale = 1);
383
+		void DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale = 1, bool centered = false);
384 384
 		// Draws a single line of text
385 385
 		void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1);
386 386
 		// Clears entire draw target to Pixel

+ 20
- 6
pge/src/olcPixelGameEngine.cpp View File

@@ -512,45 +512,59 @@ namespace olc
512 512
 		}
513 513
 	}
514 514
 
515
-	void PixelGameEngine::DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale)
515
+	void PixelGameEngine::DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale, bool centered)
516 516
 	{
517 517
 		if (sprite == nullptr)
518 518
 			return;
519 519
 
520
+		int32_t xOff = 0;
521
+		int32_t yOff = 0;
522
+		if (centered) {
523
+			xOff += sprite->width /2;
524
+			yOff += sprite->height /2;
525
+		}
526
+
520 527
 		if (scale > 1)
521 528
 		{
522 529
 			for (int32_t i = 0; i < sprite->width; i++)
523 530
 				for (int32_t j = 0; j < sprite->height; j++)
524 531
 					for (uint32_t is = 0; is < scale; is++)
525 532
 						for (uint32_t js = 0; js < scale; js++)
526
-							Draw(x + (i*scale) + is, y + (j*scale) + js, sprite->GetPixel(i, j));
533
+							Draw(x + (i*scale) + is - xOff, y + (j*scale) + js - yOff, sprite->GetPixel(i, j));
527 534
 		}
528 535
 		else
529 536
 		{
530 537
 			for (int32_t i = 0; i < sprite->width; i++)
531 538
 				for (int32_t j = 0; j < sprite->height; j++)
532
-					Draw(x + i, y + j, sprite->GetPixel(i, j));
539
+					Draw(x + i - xOff, y + j - yOff, sprite->GetPixel(i, j));
533 540
 		}
534 541
 	}
535 542
 
536
-	void PixelGameEngine::DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale)
543
+	void PixelGameEngine::DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale, bool centered)
537 544
 	{
538 545
 		if (sprite == nullptr)
539 546
 			return;
540 547
 
548
+		int32_t xOff = 0;
549
+		int32_t yOff = 0;
550
+		if (centered) {
551
+			xOff += w /2;
552
+			yOff += h /2;
553
+		}
554
+
541 555
 		if (scale > 1)
542 556
 		{
543 557
 			for (int32_t i = 0; i < w; i++)
544 558
 				for (int32_t j = 0; j < h; j++)
545 559
 					for (uint32_t is = 0; is < scale; is++)
546 560
 						for (uint32_t js = 0; js < scale; js++)
547
-							Draw(x + (i*scale) + is, y + (j*scale) + js, sprite->GetPixel(i + ox, j + oy));
561
+							Draw(x + (i*scale) + is - xOff, y + (j*scale) + js - yOff, sprite->GetPixel(i + ox, j + oy));
548 562
 		}
549 563
 		else
550 564
 		{
551 565
 			for (int32_t i = 0; i < w; i++)
552 566
 				for (int32_t j = 0; j < h; j++)
553
-					Draw(x + i, y + j, sprite->GetPixel(i + ox, j + oy));
567
+					Draw(x + i - xOff, y + j - yOff, sprite->GetPixel(i + ox, j + oy));
554 568
 		}
555 569
 	}
556 570
 

BIN
res/bg.png View File


BIN
res/bug_catch.wav View File


BIN
res/bug_fall.wav View File


BIN
res/player.png View File


+ 0
- 98
src/awoorwae.cpp View File

@@ -1,98 +0,0 @@
1
-#include "awoorwae.h"
2
-#include <cmath>
3
-#include <iostream>
4
-#include <string>
5
-
6
-namespace pabloader {
7
-
8
-bool Awoorwae::OnUserCreate()
9
-{
10
-    SetPixelMode(olc::Pixel::MASK);
11
-    
12
-    if (!olc::SOUND::InitialiseAudio()) {
13
-        std::cerr << "Cannot init audio" << std::endl;
14
-        return false;
15
-    }
16
-
17
-    if (!pack.LoadPack("awoorwae.pgp")) {
18
-        std::cerr << "Cannot load resource pack res/awoorwae.pgp" << std::endl;
19
-        return false;
20
-    }
21
-
22
-    // Load Player
23
-
24
-    std::string fileName = "res/player.pgs";
25
-    if (!playerSprite.LoadFromPGESprFile(fileName, &pack)) {
26
-        std::cerr << "[Load player] File " << fileName << " is not found" << std::endl;
27
-        return false;
28
-    }
29
-
30
-    fileName = "res/fall.wav";
31
-    playerFallSample = olc::SOUND::LoadAudioSample(fileName, &pack);
32
-    if (playerFallSample < 0) {
33
-        std::cerr << "[Load fall] File " << fileName << " is not found" << std::endl;
34
-        return false;
35
-    }
36
-
37
-    // Load enemies
38
-
39
-    fileName = "res/enemies.pgs";
40
-    if (!enemiesSprite.LoadFromPGESprFile(fileName, &pack)) {
41
-        std::cerr << "[Load enemies] File " << fileName << " is not found" << std::endl;
42
-        return false;
43
-    }
44
-
45
-    // Free Memory
46
-    pack.ClearPack();
47
-
48
-    // Creating entities
49
-    
50
-    player = new Player(this);
51
-    return true;
52
-}
53
-
54
-bool Awoorwae::OnUserUpdate(float fElapsedTime)
55
-{
56
-    Clear(olc::VERY_DARK_CYAN);
57
-
58
-    player->Update(fElapsedTime);
59
-    player->Draw();
60
-
61
-    for (auto bug : bugs) {
62
-        if (bug->IsActive()) {
63
-            bug->Update(fElapsedTime);
64
-            bug->Draw();
65
-            if (player->Collides(bug)) {
66
-                // TODO smth
67
-            }
68
-        }
69
-    }
70
-
71
-#ifdef _DEBUG
72
-    DrawString(1, 28, "Bugs: " + std::to_string(bugs.size()));
73
-#endif
74
-    if (GetKey(olc::B).bPressed) {
75
-        auto bug = SpawnBug();
76
-        bug->ResetPosition();
77
-    }
78
-
79
-    return true;
80
-}
81
-
82
-bool Awoorwae::OnUserDestroy()
83
-{
84
-    olc::SOUND::DestroyAudio();
85
-    return true;
86
-}
87
-
88
-Bug* Awoorwae::SpawnBug()
89
-{
90
-    for (auto bug : bugs) {
91
-        if (!bug->IsActive())
92
-            return bug;
93
-    }
94
-    auto bug = new Bug(this);
95
-    bugs.push_back(bug);
96
-    return bug;
97
-}
98
-}

+ 12
- 7
src/bug.cpp View File

@@ -1,10 +1,10 @@
1 1
 #include "bug.h"
2
-#include "awoorwae.h"
2
+#include "debuggers.h"
3 3
 #include <iostream>
4 4
 #include <random>
5 5
 
6 6
 namespace pabloader {
7
-Bug::Bug(Awoorwae* game_)
7
+Bug::Bug(Debuggers* game_)
8 8
     : Entity(game_)
9 9
 {
10 10
     w = game->enemiesSprite.width / 4;
@@ -14,27 +14,32 @@ Bug::Bug(Awoorwae* game_)
14 14
 void Bug::Update(float dt)
15 15
 {
16 16
     y += yv * dt;
17
-    yv += 50 * dt;
17
+    yv += 30 * dt;
18 18
 }
19 19
 
20 20
 void Bug::Draw()
21 21
 {
22 22
 #ifdef _DEBUG
23
-    game->DrawRect(x, y, w, h, olc::RED);
23
+    game->DrawRect(x - w / 2, y - h / 2, w, h, olc::RED);
24 24
 #endif
25
-    game->DrawPartialSprite(x, y, &game->enemiesSprite, 0, kind * h, w, h);
25
+    game->DrawPartialSprite(x, y, &game->enemiesSprite, 0, kind * h, w, h, 1, true);
26 26
 }
27 27
 
28 28
 bool Bug::IsActive()
29 29
 {
30
-    return y < game->ScreenHeight();
30
+    return y - h / 2 < game->GameScreenHeight();
31 31
 }
32 32
 
33 33
 void Bug::ResetPosition()
34 34
 {
35
-    y = -20;
35
+    y = -20 - std::rand() % 100;
36 36
     x = w + std::rand() % (game->ScreenWidth() - w * 2);
37 37
     yv = 0;
38 38
     kind = std::rand() % 4;
39 39
 }
40
+
41
+void Bug::Kill()
42
+{
43
+    y = game->GameScreenHeight() + 100;
44
+}
40 45
 }

+ 199
- 0
src/debuggers.cpp View File

@@ -0,0 +1,199 @@
1
+#include "debuggers.h"
2
+#include <cmath>
3
+#include <iostream>
4
+
5
+namespace pabloader {
6
+std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
7
+{
8
+    str.erase(str.find_last_not_of(chars) + 1);
9
+    return str;
10
+}
11
+
12
+bool Debuggers::OnUserCreate()
13
+{
14
+    SetPixelMode(olc::Pixel::MASK);
15
+
16
+    if (!olc::SOUND::InitialiseAudio()) {
17
+        std::cerr << "Cannot init audio" << std::endl;
18
+        return false;
19
+    }
20
+
21
+    if (!pack.LoadPack("debuggers.pgp")) {
22
+        std::cerr << "Cannot load resource pack res/debuggers.pgp" << std::endl;
23
+        return false;
24
+    }
25
+
26
+    // Load Player
27
+
28
+    std::string fileName = "res/player.pgs";
29
+    if (!playerSprite.LoadFromPGESprFile(fileName, &pack)) {
30
+        std::cerr << "[Load player] File " << fileName << " is not found" << std::endl;
31
+        return false;
32
+    }
33
+
34
+    fileName = "res/fall.wav";
35
+    playerFallSample = olc::SOUND::LoadAudioSample(fileName, &pack);
36
+    if (playerFallSample < 0) {
37
+        std::cerr << "[Load fall] File " << fileName << " is not found" << std::endl;
38
+        return false;
39
+    }
40
+
41
+    // Load enemies
42
+
43
+    fileName = "res/enemies.pgs";
44
+    if (!enemiesSprite.LoadFromPGESprFile(fileName, &pack)) {
45
+        std::cerr << "[Load enemies] File " << fileName << " is not found" << std::endl;
46
+        return false;
47
+    }
48
+
49
+    fileName = "res/bug_fall.wav";
50
+    bugFallSample = olc::SOUND::LoadAudioSample(fileName, &pack);
51
+    if (bugFallSample < 0) {
52
+        std::cerr << "[Load bug fall] File " << fileName << " is not found" << std::endl;
53
+        return false;
54
+    }
55
+
56
+    fileName = "res/bug_catch.wav";
57
+    bugCatchSample = olc::SOUND::LoadAudioSample(fileName, &pack);
58
+    if (bugCatchSample < 0) {
59
+        std::cerr << "[Load bug catch] File " << fileName << " is not found" << std::endl;
60
+        return false;
61
+    }
62
+
63
+    // Load background
64
+
65
+    fileName = "res/bg.pgs";
66
+    if (!backgroundSprite.LoadFromPGESprFile(fileName, &pack)) {
67
+        std::cerr << "[Load background] File " << fileName << " is not found" << std::endl;
68
+        return false;
69
+    }
70
+
71
+    fileName = "src/debuggers.cpp";
72
+    auto streamBuffer = pack.GetStreamBuffer(fileName);
73
+    if (streamBuffer.data == nullptr) {
74
+        std::cerr << "[Load source] File " << fileName << " is not found" << std::endl;
75
+        return false;
76
+    }
77
+    std::istream is(&streamBuffer);
78
+    std::string line;
79
+    while (std::getline(is, line)) {
80
+        if (!rtrim(line).empty())
81
+            sourceCode.push_back(line);
82
+    }
83
+
84
+    totalLines = sourceCode.size();
85
+
86
+    // Free Memory
87
+    pack.ClearPack();
88
+
89
+    // Creating entities
90
+
91
+    player = new Player(this);
92
+    return true;
93
+}
94
+
95
+bool Debuggers::OnUserUpdate(float fElapsedTime)
96
+{
97
+    if (!IsFocused())
98
+        return true;
99
+
100
+    if (sourceCode.size() == 0) {
101
+        return GameOver();
102
+    }
103
+
104
+    DrawSprite(0, 0, &backgroundSprite);
105
+
106
+    DrawString(1, 1, "A,S,D - move; SPACE - jump", olc::CYAN);
107
+    DrawString(1, 10, "Don't let the bugs destroy your code!", olc::YELLOW);
108
+
109
+    player->Update(fElapsedTime);
110
+    player->Draw();
111
+
112
+    uint32_t bugsSize = bugs.size();
113
+
114
+    if (bugsSize == 0 && player->IsOnGround()) {
115
+        SpawnBug();
116
+    }
117
+
118
+    if (bugsCatched >= bugsSize * 25 && player->GetSkin() < bugsSize) {
119
+        player->UpgradeLevel();
120
+        SpawnBug();
121
+    }
122
+
123
+    for (auto bug : bugs) {
124
+        if (bug->IsActive()) {
125
+            bug->Update(fElapsedTime);
126
+            bug->Draw();
127
+            if (player->Collides(bug)) {
128
+                bugsCatched++;
129
+                bug->Kill();
130
+                olc::SOUND::PlaySample(bugCatchSample);
131
+                SpawnBug();
132
+            }
133
+        } else {
134
+            bugsMissed++;
135
+            sourceCode.erase(sourceCode.begin());
136
+
137
+            SpawnBug();
138
+            olc::SOUND::PlaySample(bugFallSample);
139
+        }
140
+    }
141
+
142
+    linesOfCode = (sourceCode.size() * 4 / totalLines)  +1;
143
+
144
+#ifdef _DEBUG
145
+    DrawString(1, 37, "Bugs: " + std::to_string(bugs.size()));
146
+    if (GetKey(olc::B).bPressed) {
147
+        SpawnBug();
148
+    }
149
+#endif
150
+    DrawString(1, 19, "Bugs catched: " + std::to_string(bugsCatched));
151
+    DrawString(1, 28, "Bugs missed: " + std::to_string(bugsMissed));
152
+
153
+    int y = GameScreenHeight();
154
+    FillRect(0, y - 1, ScreenWidth(), ScreenHeight() - y + 1, olc::VERY_DARK_GREY);
155
+
156
+    for (auto line : sourceCode) {
157
+        DrawString(1, y, line);
158
+        y += 10;
159
+        if (y >= ScreenHeight())
160
+            break;
161
+    }
162
+
163
+    return true;
164
+}
165
+
166
+bool Debuggers::GameOver()
167
+{
168
+    DrawSprite(0, 0, &backgroundSprite);
169
+    DrawString(ScreenWidth() / 2 - 143, ScreenHeight() / 2 - 15, "GAME OVER", olc::BLACK, 4);
170
+    DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 - 16, "GAME OVER", olc::RED, 4);
171
+
172
+    DrawString(ScreenWidth() / 2 - 144, ScreenHeight() / 2 + 16, "Bugs catched: " + std::to_string(bugsCatched), olc::CYAN, 2);
173
+
174
+    return true;
175
+}
176
+
177
+bool Debuggers::OnUserDestroy()
178
+{
179
+    olc::SOUND::DestroyAudio();
180
+    return true;
181
+}
182
+
183
+void Debuggers::SpawnBug()
184
+{
185
+    auto bug = CreateBug();
186
+    bug->ResetPosition();
187
+}
188
+
189
+Bug* Debuggers::CreateBug()
190
+{
191
+    for (auto bug : bugs) {
192
+        if (!bug->IsActive())
193
+            return bug;
194
+    }
195
+    auto bug = new Bug(this);
196
+    bugs.push_back(bug);
197
+    return bug;
198
+}
199
+}

+ 3
- 3
src/entity.cpp View File

@@ -1,7 +1,7 @@
1 1
 #include "entity.h"
2 2
 
3 3
 namespace pabloader {
4
-Entity::Entity(Awoorwae* game)
4
+Entity::Entity(Debuggers* game)
5 5
 {
6 6
     xv = 0;
7 7
     yv = 0;
@@ -10,8 +10,8 @@ Entity::Entity(Awoorwae* game)
10 10
 
11 11
 bool Entity::Collides(Entity* entity)
12 12
 {
13
-    bool collidesX = (x < entity->x + entity->w) && (entity->x < x + w);
14
-    bool collidesY = (y < entity->y + entity->h) && (entity->y < y + h);
13
+    bool collidesX = (x - w / 2 < entity->x + entity->w / 2) && (entity->x - entity->w / 2 < x + w / 2);
14
+    bool collidesY = (y - h / 2 < entity->y + entity->h / 2) && (entity->y - entity->h / 2 < y + h / 2);
15 15
     return collidesX && collidesY;
16 16
 }
17 17
 }

+ 2
- 2
src/main.cpp View File

@@ -1,8 +1,8 @@
1
-#include "awoorwae.h"
1
+#include "debuggers.h"
2 2
 
3 3
 int main()
4 4
 {
5
-    pabloader::Awoorwae game;
5
+    pabloader::Debuggers game;
6 6
     if (game.ConstructAuto(320, 240))
7 7
         game.Start();
8 8
 

+ 22
- 17
src/player.cpp View File

@@ -1,16 +1,16 @@
1 1
 #include "player.h"
2
-#include "awoorwae.h"
2
+#include "debuggers.h"
3 3
 #include "olcPGEX_Sound.h"
4 4
 #include <iostream>
5 5
 
6 6
 namespace pabloader {
7
-Player::Player(Awoorwae* game_)
7
+Player::Player(Debuggers* game_)
8 8
     : Entity(game_)
9 9
 {
10 10
     w = game->playerSprite.width / 4;
11 11
     h = game->playerSprite.height / 4;
12 12
     x = (game->ScreenWidth() - w) / 2;
13
-    y = game->ScreenHeight() / 2;
13
+    y = game->GameScreenHeight() / 2;
14 14
 }
15 15
 
16 16
 void Player::Update(float dt)
@@ -21,7 +21,7 @@ void Player::Update(float dt)
21 21
     xv *= 1 - 0.2 * dt;
22 22
 
23 23
     if (!onGround) {
24
-        yv += 50 * dt;
24
+        yv += (70 + skin * 20) * dt;
25 25
         yv *= 1 - 0.05 * dt;
26 26
     } else {
27 27
         xv *= 1 - 0.5 * dt;
@@ -29,11 +29,13 @@ void Player::Update(float dt)
29 29
 
30 30
     transform.Reset();
31 31
 
32
-    game->DrawString(1, 1, "A,D - move; SPACE - jump", olc::CYAN);
33
-
34 32
 #ifdef _DEBUG
35
-    game->DrawString(1, 10, std::to_string(x) + " " + std::to_string(y));
36
-    game->DrawString(1, 19, std::to_string(xv) + " " + std::to_string(yv));
33
+    // game->DrawString(1, 19, std::to_string(x) + " " + std::to_string(y));
34
+    // game->DrawString(1, 28, std::to_string(xv) + " " + std::to_string(yv));
35
+
36
+    if (game->GetKey(olc::J).bPressed) {
37
+        skin = (skin + 1) % 4;
38
+    }
37 39
 #endif
38 40
     bool userInput = false;
39 41
 
@@ -43,17 +45,20 @@ void Player::Update(float dt)
43 45
     }
44 46
 
45 47
     if (game->GetKey(olc::A).bHeld) {
46
-        xv -= 100 * dt;
48
+        xv -= (100 + skin * 50) * dt;
47 49
         userInput = true;
48 50
     }
49 51
 
50 52
     if (game->GetKey(olc::D).bHeld) {
51
-        xv += 100 * dt;
53
+        xv += (100 + skin * 50) * dt;
52 54
         userInput = true;
53 55
     }
54 56
 
55
-    if (game->GetKey(olc::J).bPressed) {
56
-        skin = 1 - skin;
57
+    if (game->GetKey(olc::S).bHeld) {
58
+        if (!onGround) {
59
+            yv += (100 + skin * 50) * dt;
60
+        }
61
+        userInput = true;
57 62
     }
58 63
 
59 64
     if (x > game->ScreenWidth() - w / 2) {
@@ -68,16 +73,16 @@ void Player::Update(float dt)
68 73
         olc::SOUND::PlaySample(game->playerFallSample);
69 74
     }
70 75
 
71
-    if (y > game->ScreenHeight() - h / 2) {
76
+    if (y > game->GameScreenHeight() - h / 2) {
72 77
         yv *= -0.3;
73 78
 
74 79
         if (std::abs(yv) < 2) {
75 80
             yv = 0;
76 81
         }
77
-        y = game->ScreenHeight() - h / 2;
82
+        y = game->GameScreenHeight() - h / 2;
78 83
     }
79 84
 
80
-    bool newOnGround = y >= game->ScreenHeight() - h / 2;
85
+    bool newOnGround = y >= game->GameScreenHeight() - h / 2;
81 86
     if (newOnGround && !onGround) {
82 87
         olc::SOUND::PlaySample(game->playerFallSample);
83 88
     }
@@ -85,7 +90,7 @@ void Player::Update(float dt)
85 90
 
86 91
     if (onGround) {
87 92
         tile = 2;
88
-        tileCounter = 30;
93
+        tileCounter = static_cast<uint32_t>(0.1 / dt);
89 94
     } else if (tileCounter <= 0) {
90 95
         tile = 0;
91 96
     } else {
@@ -112,7 +117,7 @@ void Player::Draw()
112 117
 #ifdef _DEBUG
113 118
     float x, y;
114 119
     transform.Forward(0, 0, x, y);
115
-    game->Draw((int)x, (int)y, olc::RED);
120
+    game->DrawRect((int)x - w / 2, (int)y - h / 2, w, h, olc::RED);
116 121
 #endif
117 122
 }
118 123
 }

Loading…
Cancel
Save