Browse Source

Rotating bugs

Pabloader 5 years ago
parent
commit
10fc788141
6 changed files with 30 additions and 16 deletions
  1. 1
    0
      .gitignore
  2. 2
    0
      include/bug.h
  3. 8
    5
      pge/src/olcPGEX_Graphics2D.cpp
  4. 11
    4
      src/bug.cpp
  5. 4
    3
      src/debuggers.cpp
  6. 4
    4
      src/programmer.cpp

+ 1
- 0
.gitignore View File

@@ -4,3 +4,4 @@ debuggers
4 4
 *.o
5 5
 *.pgp
6 6
 *.pgs
7
+*.res

+ 2
- 0
include/bug.h View File

@@ -15,6 +15,8 @@ public:
15 15
 
16 16
 private:
17 17
     int kind = 0;
18
+    float rotation = 0;
19
+    int8_t rotationSpeed;
18 20
 };
19 21
 }
20 22
 

+ 8
- 5
pge/src/olcPGEX_Graphics2D.cpp View File

@@ -8,8 +8,8 @@ namespace olc
8 8
 			return;
9 9
 
10 10
 		// Work out bounding rectangle of sprite
11
-		float ex, ey;
12
-		float sx, sy;		
11
+		float ex=-1, ey=-1;
12
+		float sx, sy;
13 13
 		float px, py;
14 14
 
15 15
 		float x0 = 0;
@@ -63,8 +63,8 @@ namespace olc
63 63
 			return;
64 64
 
65 65
 		// Work out bounding rectangle of sprite
66
-		float ex, ey;
67
-		float sx, sy;		
66
+		float ex=-1, ey=-1;
67
+		float sx, sy;
68 68
 		float px, py;
69 69
 
70 70
 		float xOff = 0;
@@ -108,7 +108,10 @@ namespace olc
108 108
 			{
109 109
 				float ox, oy;
110 110
 				transform.Backward(i, j, ox, oy);
111
-				pge->Draw((int32_t)i, (int32_t)j, sprite->GetPixel((int32_t)(xOff+x0+ox+0.5f), (int32_t)(yOff+y0+oy+0.5f)));
111
+				int32_t xx = (int32_t)(xOff+x0+ox+0.5f);
112
+				int32_t yy = (int32_t)(yOff+y0+oy+0.5f);
113
+				if (xx >= x0 && xx < x0 + w && yy >= y0 && yy < y0 + h) 
114
+					pge->Draw((int32_t)i, (int32_t)j, sprite->GetPixel(xx, yy));
112 115
 			}
113 116
 		}
114 117
 	}

+ 11
- 4
src/bug.cpp View File

@@ -4,8 +4,8 @@
4 4
 #include <random>
5 5
 
6 6
 namespace pabloader {
7
-Bug::Bug(Debuggers* game_)
8
-    : Entity(game_)
7
+Bug::Bug(Debuggers* game)
8
+    : Entity(game)
9 9
 {
10 10
     w = game->enemiesSprite.width / 4;
11 11
     h = game->enemiesSprite.height / 4;
@@ -15,6 +15,12 @@ void Bug::Update(float dt)
15 15
 {
16 16
     y += yv * dt;
17 17
     yv += 30 * dt;
18
+
19
+    rotation += 3.14 / 8 * rotationSpeed * dt;
20
+
21
+    transform.Reset();
22
+    transform.Rotate(rotation);
23
+    transform.Translate(x, y);
18 24
 }
19 25
 
20 26
 void Bug::Draw()
@@ -22,7 +28,7 @@ void Bug::Draw()
22 28
 #ifdef _DEBUG
23 29
     game->DrawRect(x - w / 2, y - h / 2, w, h, olc::RED);
24 30
 #endif
25
-    game->DrawPartialSprite(x, y, &game->enemiesSprite, 0, kind * h, w, h, 1, true);
31
+    olc::GFX2D::DrawPartialSprite(&game->enemiesSprite, 0, kind * h, w, h, transform, true);
26 32
 }
27 33
 
28 34
 bool Bug::IsActive()
@@ -32,10 +38,11 @@ bool Bug::IsActive()
32 38
 
33 39
 void Bug::ResetPosition()
34 40
 {
35
-    y = -20 - std::rand() % 100;
41
+    y = -20 - std::rand() % game->ScreenHeight();
36 42
     x = w + std::rand() % (game->ScreenWidth() - w * 2);
37 43
     yv = 0;
38 44
     kind = std::rand() % 4;
45
+    rotationSpeed = -8 + rand()%17;
39 46
 }
40 47
 
41 48
 void Bug::Kill()

+ 4
- 3
src/debuggers.cpp View File

@@ -1,8 +1,8 @@
1 1
 #include "debuggers.h"
2 2
 #include <cmath>
3
+#include <ctime>
3 4
 #include <iostream>
4 5
 #include <random>
5
-#include <ctime>
6 6
 
7 7
 namespace pabloader {
8 8
 std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ")
@@ -140,15 +140,16 @@ bool Debuggers::OnUserUpdate(float fElapsedTime)
140 140
         programmer->Draw();
141 141
     }
142 142
 
143
-    Player* player = dynamic_cast<Player*>(programmers.at(0));
143
+    Programmer* player = programmers.at(0);
144 144
 
145 145
     if (bugsSize == 0 && player->IsOnGround()) {
146 146
         SpawnBug();
147 147
     }
148 148
 
149
-    if (bugsCatched >= bugsSize * (15 * programmers.size()) && player->GetSkin() < bugsSize) {
149
+    if (bugsCatched >= 15 * bugsSize * programmers.size() && player->GetSkin() < bugsSize) {
150 150
         player->UpgradeLevel();
151 151
         SpawnBug();
152
+        SpawnBug();
152 153
         SpawnProgrammer();
153 154
     }
154 155
 

+ 4
- 4
src/programmer.cpp View File

@@ -10,7 +10,7 @@ Programmer::Programmer(Debuggers* game)
10 10
     w = game->playerSprite.width / 4;
11 11
     h = game->playerSprite.height / 4;
12 12
     x = (game->ScreenWidth() - w) / 2;
13
-    y = game->GameScreenHeight() / 2;
13
+    y = h / 2;
14 14
 }
15 15
 
16 16
 void Programmer::Think()
@@ -49,7 +49,7 @@ void Programmer::Update(float dt)
49 49
         yv += (70 + skin * 30) * dt;
50 50
         yv *= 1 - 0.05 * dt;
51 51
     } else {
52
-        xv *= 1 - 0.5 * dt;
52
+        xv *= 1 - dt;
53 53
     }
54 54
 
55 55
     transform.Reset();
@@ -127,9 +127,9 @@ void Programmer::Draw()
127 127
 {
128 128
     int xOff = xv < 0 ? -1 : 0;
129 129
     if (std::abs(xv) < 1) {
130
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, h + tile * h, w, h, transform, true);
130
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, h + tile * h, w - xOff, h, transform, true);
131 131
     } else {
132
-        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, 0 + tile * h, w, h, transform, true);
132
+        olc::GFX2D::DrawPartialSprite(&game->playerSprite, skin * w + xOff, 0 + tile * h, w - xOff, h, transform, true);
133 133
     }
134 134
 #ifdef _DEBUG
135 135
     game->DrawRect(x - w / 2, y - h / 2, w, h, olc::GREEN);

Loading…
Cancel
Save