Przeglądaj źródła

Create falling bugs

Pabloader 4 lat temu
rodzic
commit
49ec9bc8d0
9 zmienionych plików z 153 dodań i 43 usunięć
  1. 9
    1
      include/awoorwae.h
  2. 20
    0
      include/bug.h
  3. 2
    7
      include/entity.h
  4. 4
    3
      include/player.h
  5. BIN
      res/enemies.png
  6. 58
    2
      src/awoorwae.cpp
  7. 40
    0
      src/bug.cpp
  8. 9
    2
      src/entity.cpp
  9. 11
    28
      src/player.cpp

+ 9
- 1
include/awoorwae.h Wyświetl plik

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

+ 20
- 0
include/bug.h Wyświetl plik

@@ -0,0 +1,20 @@
1
+#ifndef BUG_H
2
+#define BUG_H
3
+
4
+#include "entity.h"
5
+
6
+namespace pabloader {
7
+class Bug : public Entity {
8
+public:
9
+    Bug(Awoorwae* game);
10
+    void Update(float dt) override;
11
+    void Draw() override;
12
+    bool IsActive();
13
+    void ResetPosition();
14
+
15
+private:
16
+    int kind = 0;
17
+};
18
+}
19
+
20
+#endif

+ 2
- 7
include/entity.h Wyświetl plik

@@ -10,23 +10,18 @@ class Awoorwae;
10 10
 class Entity {
11 11
 protected:
12 12
     Awoorwae* game;
13
-    olc::Sprite img;
14 13
     olc::GFX2D::Transform2D transform;
15 14
     float x, y;
16 15
     float xv, yv;
17 16
     int w, h;
18
-    int tile = 0;
19
-    int skin = 0;
20
-    int tileCounter = 0;
21
-    bool onGround = false;
22 17
 
23 18
 public:
24 19
     Entity(Awoorwae* game_);
25 20
     virtual ~Entity(){};
26
-
27
-    virtual olc::rcode Load() = 0;
21
+    
28 22
     virtual void Update(float dt) = 0;
29 23
     virtual void Draw() = 0;
24
+    bool Collides(Entity* entity);
30 25
 };
31 26
 }
32 27
 

+ 4
- 3
include/player.h Wyświetl plik

@@ -3,19 +3,20 @@
3 3
 
4 4
 #include "entity.h"
5 5
 #include "olcPGE_Common.h"
6
-#include "olcPGEX_Sound.h"
7 6
 
8 7
 namespace pabloader {
9 8
 
10 9
 class Player : public Entity {
11 10
 public:
12 11
     Player(Awoorwae* game);
13
-    olc::rcode Load() override;
14 12
     void Update(float dt) override;
15 13
     void Draw() override;
16 14
 
17 15
 private:
18
-    int fallSample;
16
+    int tile = 0;
17
+    int skin = 0;
18
+    int tileCounter = 0;
19
+    bool onGround = false;
19 20
 };
20 21
 }
21 22
 

BIN
res/enemies.png Wyświetl plik


+ 58
- 2
src/awoorwae.cpp Wyświetl plik

@@ -7,6 +7,8 @@ namespace pabloader {
7 7
 
8 8
 bool Awoorwae::OnUserCreate()
9 9
 {
10
+    SetPixelMode(olc::Pixel::MASK);
11
+    
10 12
     if (!olc::SOUND::InitialiseAudio()) {
11 13
         std::cerr << "Cannot init audio" << std::endl;
12 14
         return false;
@@ -17,11 +19,35 @@ bool Awoorwae::OnUserCreate()
17 19
         return false;
18 20
     }
19 21
 
20
-    if (!player->Load())
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;
21 27
         return false;
28
+    }
22 29
 
23
-    SetPixelMode(olc::Pixel::MASK);
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
24 46
     pack.ClearPack();
47
+
48
+    // Creating entities
49
+    
50
+    player = new Player(this);
25 51
     return true;
26 52
 }
27 53
 
@@ -31,6 +57,25 @@ bool Awoorwae::OnUserUpdate(float fElapsedTime)
31 57
 
32 58
     player->Update(fElapsedTime);
33 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
+
34 79
     return true;
35 80
 }
36 81
 
@@ -39,4 +84,15 @@ bool Awoorwae::OnUserDestroy()
39 84
     olc::SOUND::DestroyAudio();
40 85
     return true;
41 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
+}
42 98
 }

+ 40
- 0
src/bug.cpp Wyświetl plik

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

+ 9
- 2
src/entity.cpp Wyświetl plik

@@ -1,10 +1,17 @@
1 1
 #include "entity.h"
2 2
 
3 3
 namespace pabloader {
4
-Entity::Entity(Awoorwae* game_)
5
-    : game(game_)
4
+Entity::Entity(Awoorwae* game)
6 5
 {
7 6
     xv = 0;
8 7
     yv = 0;
8
+    Entity::game = game;
9
+}
10
+
11
+bool Entity::Collides(Entity* entity)
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);
15
+    return collidesX && collidesY;
9 16
 }
10 17
 }

+ 11
- 28
src/player.cpp Wyświetl plik

@@ -1,33 +1,16 @@
1 1
 #include "player.h"
2 2
 #include "awoorwae.h"
3
+#include "olcPGEX_Sound.h"
3 4
 #include <iostream>
4 5
 
5 6
 namespace pabloader {
6 7
 Player::Player(Awoorwae* game_)
7 8
     : Entity(game_)
8 9
 {
9
-}
10
-
11
-olc::rcode Player::Load()
12
-{
13
-    const std::string spriteFile = "res/player.pgs";
14
-    const std::string fallFile = "res/fall.wav";
15
-    if (img.LoadFromPGESprFile(spriteFile, &game->pack)) {
16
-        w = img.width / 4;
17
-        h = img.height / 4;
18
-        x = (game->ScreenWidth() - w) / 2;
19
-        y = game->ScreenHeight() / 2;
20
-    } else {
21
-        std::cerr << "File " << spriteFile << " is not found" << std::endl;
22
-        return olc::FAIL;
23
-    }
24
-    fallSample = olc::SOUND::LoadAudioSample(fallFile, &game->pack);
25
-    if (fallSample < 0) {
26
-        std::cerr << "File " << fallFile << " is not found" << std::endl;
27
-        return olc::FAIL;
28
-    }
29
-
30
-    return olc::OK;
10
+    w = game->playerSprite.width / 4;
11
+    h = game->playerSprite.height / 4;
12
+    x = (game->ScreenWidth() - w) / 2;
13
+    y = game->ScreenHeight() / 2;
31 14
 }
32 15
 
33 16
 void Player::Update(float dt)
@@ -50,7 +33,7 @@ void Player::Update(float dt)
50 33
 
51 34
 #ifdef _DEBUG
52 35
     game->DrawString(1, 10, std::to_string(x) + " " + std::to_string(y));
53
-    game->DrawString(1, 18, std::to_string(xv) + " " + std::to_string(yv));
36
+    game->DrawString(1, 19, std::to_string(xv) + " " + std::to_string(yv));
54 37
 #endif
55 38
     bool userInput = false;
56 39
 
@@ -76,13 +59,13 @@ void Player::Update(float dt)
76 59
     if (x > game->ScreenWidth() - w / 2) {
77 60
         xv *= -0.3;
78 61
         x = game->ScreenWidth() - w / 2;
79
-        olc::SOUND::PlaySample(fallSample);
62
+        olc::SOUND::PlaySample(game->playerFallSample);
80 63
     }
81 64
 
82 65
     if (x < w / 2) {
83 66
         xv *= -0.3;
84 67
         x = w / 2;
85
-        olc::SOUND::PlaySample(fallSample);
68
+        olc::SOUND::PlaySample(game->playerFallSample);
86 69
     }
87 70
 
88 71
     if (y > game->ScreenHeight() - h / 2) {
@@ -96,7 +79,7 @@ void Player::Update(float dt)
96 79
 
97 80
     bool newOnGround = y >= game->ScreenHeight() - h / 2;
98 81
     if (newOnGround && !onGround) {
99
-        olc::SOUND::PlaySample(fallSample);
82
+        olc::SOUND::PlaySample(game->playerFallSample);
100 83
     }
101 84
     onGround = newOnGround;
102 85
 
@@ -122,9 +105,9 @@ void Player::Draw()
122 105
 {
123 106
     int xOff = xv < 0 ? -1 : 0;
124 107
     if (std::abs(xv) < 1) {
125
-        olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, h + tile * h, w, h, transform, true);
108
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, h + tile * h, w, h, transform, true);
126 109
     } else {
127
-        olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, 0 + tile * h, w, h, transform, true);
110
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, 0 + tile * h, w, h, transform, true);
128 111
     }
129 112
 #ifdef _DEBUG
130 113
     float x, y;

Loading…
Anuluj
Zapisz