Browse Source

Create falling bugs

Pabloader 5 years ago
parent
commit
49ec9bc8d0
9 changed files with 153 additions and 43 deletions
  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 View File

4
 #include "olcPixelGameEngine.h"
4
 #include "olcPixelGameEngine.h"
5
 #include "olcPGEX_Sound.h"
5
 #include "olcPGEX_Sound.h"
6
 #include "player.h"
6
 #include "player.h"
7
+#include "bug.h"
8
+#include <vector>
7
 
9
 
8
 namespace pabloader {
10
 namespace pabloader {
9
 class Awoorwae : public olc::PixelGameEngine {
11
 class Awoorwae : public olc::PixelGameEngine {
10
 private:
12
 private:
11
     Player* player;
13
     Player* player;
14
+    std::vector<Bug*> bugs;
12
 
15
 
13
 public:
16
 public:
14
     Awoorwae()
17
     Awoorwae()
15
     {
18
     {
16
         sAppName = "Awoorwa";
19
         sAppName = "Awoorwa";
17
-        player = new Player(this);
18
     }
20
     }
19
 
21
 
20
     ~Awoorwae()
22
     ~Awoorwae()
27
     bool OnUserUpdate(float fElapsedTime) override;
29
     bool OnUserUpdate(float fElapsedTime) override;
28
     bool OnUserDestroy() override;
30
     bool OnUserDestroy() override;
29
 
31
 
32
+private:
33
+    Bug* SpawnBug();
34
+
30
 public:
35
 public:
31
     olc::ResourcePack pack;
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 View File

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 View File

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

+ 4
- 3
include/player.h View File

3
 
3
 
4
 #include "entity.h"
4
 #include "entity.h"
5
 #include "olcPGE_Common.h"
5
 #include "olcPGE_Common.h"
6
-#include "olcPGEX_Sound.h"
7
 
6
 
8
 namespace pabloader {
7
 namespace pabloader {
9
 
8
 
10
 class Player : public Entity {
9
 class Player : public Entity {
11
 public:
10
 public:
12
     Player(Awoorwae* game);
11
     Player(Awoorwae* game);
13
-    olc::rcode Load() override;
14
     void Update(float dt) override;
12
     void Update(float dt) override;
15
     void Draw() override;
13
     void Draw() override;
16
 
14
 
17
 private:
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 View File


+ 58
- 2
src/awoorwae.cpp View File

7
 
7
 
8
 bool Awoorwae::OnUserCreate()
8
 bool Awoorwae::OnUserCreate()
9
 {
9
 {
10
+    SetPixelMode(olc::Pixel::MASK);
11
+    
10
     if (!olc::SOUND::InitialiseAudio()) {
12
     if (!olc::SOUND::InitialiseAudio()) {
11
         std::cerr << "Cannot init audio" << std::endl;
13
         std::cerr << "Cannot init audio" << std::endl;
12
         return false;
14
         return false;
17
         return false;
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
         return false;
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
     pack.ClearPack();
46
     pack.ClearPack();
47
+
48
+    // Creating entities
49
+    
50
+    player = new Player(this);
25
     return true;
51
     return true;
26
 }
52
 }
27
 
53
 
31
 
57
 
32
     player->Update(fElapsedTime);
58
     player->Update(fElapsedTime);
33
     player->Draw();
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
     return true;
79
     return true;
35
 }
80
 }
36
 
81
 
39
     olc::SOUND::DestroyAudio();
84
     olc::SOUND::DestroyAudio();
40
     return true;
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 View File

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 View File

1
 #include "entity.h"
1
 #include "entity.h"
2
 
2
 
3
 namespace pabloader {
3
 namespace pabloader {
4
-Entity::Entity(Awoorwae* game_)
5
-    : game(game_)
4
+Entity::Entity(Awoorwae* game)
6
 {
5
 {
7
     xv = 0;
6
     xv = 0;
8
     yv = 0;
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 View File

1
 #include "player.h"
1
 #include "player.h"
2
 #include "awoorwae.h"
2
 #include "awoorwae.h"
3
+#include "olcPGEX_Sound.h"
3
 #include <iostream>
4
 #include <iostream>
4
 
5
 
5
 namespace pabloader {
6
 namespace pabloader {
6
 Player::Player(Awoorwae* game_)
7
 Player::Player(Awoorwae* game_)
7
     : Entity(game_)
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
 void Player::Update(float dt)
16
 void Player::Update(float dt)
50
 
33
 
51
 #ifdef _DEBUG
34
 #ifdef _DEBUG
52
     game->DrawString(1, 10, std::to_string(x) + " " + std::to_string(y));
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
 #endif
37
 #endif
55
     bool userInput = false;
38
     bool userInput = false;
56
 
39
 
76
     if (x > game->ScreenWidth() - w / 2) {
59
     if (x > game->ScreenWidth() - w / 2) {
77
         xv *= -0.3;
60
         xv *= -0.3;
78
         x = game->ScreenWidth() - w / 2;
61
         x = game->ScreenWidth() - w / 2;
79
-        olc::SOUND::PlaySample(fallSample);
62
+        olc::SOUND::PlaySample(game->playerFallSample);
80
     }
63
     }
81
 
64
 
82
     if (x < w / 2) {
65
     if (x < w / 2) {
83
         xv *= -0.3;
66
         xv *= -0.3;
84
         x = w / 2;
67
         x = w / 2;
85
-        olc::SOUND::PlaySample(fallSample);
68
+        olc::SOUND::PlaySample(game->playerFallSample);
86
     }
69
     }
87
 
70
 
88
     if (y > game->ScreenHeight() - h / 2) {
71
     if (y > game->ScreenHeight() - h / 2) {
96
 
79
 
97
     bool newOnGround = y >= game->ScreenHeight() - h / 2;
80
     bool newOnGround = y >= game->ScreenHeight() - h / 2;
98
     if (newOnGround && !onGround) {
81
     if (newOnGround && !onGround) {
99
-        olc::SOUND::PlaySample(fallSample);
82
+        olc::SOUND::PlaySample(game->playerFallSample);
100
     }
83
     }
101
     onGround = newOnGround;
84
     onGround = newOnGround;
102
 
85
 
122
 {
105
 {
123
     int xOff = xv < 0 ? -1 : 0;
106
     int xOff = xv < 0 ? -1 : 0;
124
     if (std::abs(xv) < 1) {
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
     } else {
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
 #ifdef _DEBUG
112
 #ifdef _DEBUG
130
     float x, y;
113
     float x, y;

Loading…
Cancel
Save