Browse Source

Separate Playes class

Pabloader 5 years ago
parent
commit
c2b7985406
5 changed files with 64 additions and 25 deletions
  1. 1
    0
      .vscode/launch.json
  2. 2
    2
      pge/src/olcPGEX_Graphics2D.cpp
  3. BIN
      res/awoorwa.png
  4. 42
    17
      src/pabloader/awoorwae.cpp
  5. 19
    6
      src/pabloader/awoorwae.h

+ 1
- 0
.vscode/launch.json View File

4
     // Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
4
     // Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
5
     "version": "0.2.0",
5
     "version": "0.2.0",
6
     "configurations": [
6
     "configurations": [
7
+    
7
         {
8
         {
8
             "name": "(gdb-win) Launch Packer",
9
             "name": "(gdb-win) Launch Packer",
9
             "type": "cppdbg",
10
             "type": "cppdbg",

+ 2
- 2
pge/src/olcPGEX_Graphics2D.cpp View File

33
 		sx = std::min(sx, px); sy = std::min(sy, py);
33
 		sx = std::min(sx, px); sy = std::min(sy, py);
34
 		ex = std::max(ex, px); ey = std::max(ey, py);
34
 		ex = std::max(ex, px); ey = std::max(ey, py);
35
 
35
 
36
-		transform.Forward((float)sprite->width, -y0, px, py);
36
+		transform.Forward((float)sprite->width-x0, -y0, px, py);
37
 		sx = std::min(sx, px); sy = std::min(sy, py);
37
 		sx = std::min(sx, px); sy = std::min(sy, py);
38
 		ex = std::max(ex, px); ey = std::max(ey, py);
38
 		ex = std::max(ex, px); ey = std::max(ey, py);
39
 
39
 
89
 		sx = std::min(sx, px); sy = std::min(sy, py);
89
 		sx = std::min(sx, px); sy = std::min(sy, py);
90
 		ex = std::max(ex, px); ey = std::max(ey, py);
90
 		ex = std::max(ex, px); ey = std::max(ey, py);
91
 
91
 
92
-		transform.Forward((float)w, -yOff, px, py);
92
+		transform.Forward((float)w-xOff, -yOff, px, py);
93
 		sx = std::min(sx, px); sy = std::min(sy, py);
93
 		sx = std::min(sx, px); sy = std::min(sy, py);
94
 		ex = std::max(ex, px); ey = std::max(ey, py);
94
 		ex = std::max(ex, px); ey = std::max(ey, py);
95
 
95
 

BIN
res/awoorwa.png View File


+ 42
- 17
src/pabloader/awoorwae.cpp View File

18
 
18
 
19
 bool Awoorwae::OnUserUpdate(float fElapsedTime)
19
 bool Awoorwae::OnUserUpdate(float fElapsedTime)
20
 {
20
 {
21
-    Clear(olc::BLACK);
21
+    Clear(olc::VERY_DARK_CYAN);
22
 
22
 
23
     player->Update(fElapsedTime);
23
     player->Update(fElapsedTime);
24
     player->Draw();
24
     player->Draw();
29
     : game(game_)
29
     : game(game_)
30
 {
30
 {
31
     xv = 0;
31
     xv = 0;
32
-    yv = 30;
33
-    x = game->ScreenWidth() / 2;
34
-    y = game->ScreenHeight() / 2;
32
+    yv = 0;}
33
+
34
+Player::Player(Awoorwae* game_)
35
+    : Entity(game_)
36
+{
35
 }
37
 }
36
 
38
 
37
-olc::rcode Entity::Load()
39
+olc::rcode Player::Load()
38
 {
40
 {
39
-    return img.LoadFromPGESprFile("res/awoorwa.spr");
41
+    olc::rcode result = img.LoadFromPGESprFile("res/awoorwa.spr");
42
+    if (result) {
43
+        w = img.width / 4;
44
+        h = img.height / 4;
45
+    }
46
+    x = (game->ScreenWidth() - w) / 2;
47
+    y = game->ScreenHeight() / 2;
48
+    return result;
40
 }
49
 }
41
 
50
 
42
-void Entity::Update(float dt)
51
+void Player::Update(float dt)
43
 {
52
 {
44
     x += xv * dt;
53
     x += xv * dt;
45
     y += yv * dt;
54
     y += yv * dt;
49
     if (!onGround) {
58
     if (!onGround) {
50
         yv += 50 * dt;
59
         yv += 50 * dt;
51
         yv *= 1 - 0.05 * dt;
60
         yv *= 1 - 0.05 * dt;
61
+    } else {
62
+        xv *= 1 - 0.5 * dt;
52
     }
63
     }
53
 
64
 
54
     transform.Reset();
65
     transform.Reset();
76
         userInput = true;
87
         userInput = true;
77
     }
88
     }
78
 
89
 
79
-    if (x > game->ScreenWidth() - img.width / 2) {
90
+    if (game->GetKey(olc::J).bPressed) {
91
+        skin = 1 - skin;
92
+    }
93
+
94
+    if (x > game->ScreenWidth() - w / 2) {
80
         xv *= -0.3;
95
         xv *= -0.3;
81
-        x = game->ScreenWidth() - img.width / 2;
96
+        x = game->ScreenWidth() - w / 2;
82
     }
97
     }
83
 
98
 
84
-    if (x < img.width / 2) {
99
+    if (x < w / 2) {
85
         xv *= -0.3;
100
         xv *= -0.3;
86
-        x = img.width / 2;
101
+        x = w / 2;
87
     }
102
     }
88
 
103
 
89
-    if (y > game->ScreenHeight() - img.height / 4) {
104
+    if (y > game->ScreenHeight() - h / 2) {
90
         yv *= -0.3;
105
         yv *= -0.3;
91
 
106
 
92
         if (std::abs(yv) < 2) {
107
         if (std::abs(yv) < 2) {
93
             yv = 0;
108
             yv = 0;
94
         }
109
         }
95
-        y = game->ScreenHeight() - img.height / 4;
110
+        y = game->ScreenHeight() - h / 2;
96
     }
111
     }
97
 
112
 
98
-    onGround = y >= game->ScreenHeight() - img.height / 4;
113
+    onGround = y >= game->ScreenHeight() - h / 2;
114
+
115
+    if (onGround) {
116
+        tile = 2;
117
+        tileCounter = 30;
118
+    } else if (tileCounter <= 0) {
119
+        tile = 0;
120
+    } else {
121
+        tileCounter--;
122
+    }
99
 
123
 
100
     if (!userInput && std::abs(xv) < 2) {
124
     if (!userInput && std::abs(xv) < 2) {
101
         xv = 0;
125
         xv = 0;
106
     transform.Translate(x, y);
130
     transform.Translate(x, y);
107
 }
131
 }
108
 
132
 
109
-void Entity::Draw()
133
+void Player::Draw()
110
 {
134
 {
135
+    float xOff = xv < 0 ? -1: 0;
111
     if (std::abs(xv) < 1) {
136
     if (std::abs(xv) < 1) {
112
-        olc::GFX2D::DrawPartialSprite(&img, 0, img.height / 2, img.width, img.height / 2, transform, true);
137
+        olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, h + tile * h, w, h, transform, true);
113
     } else {
138
     } else {
114
-        olc::GFX2D::DrawPartialSprite(&img, 0, 0, img.width, img.height / 2, transform, true);
139
+        olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, 0 + tile * h, w, h, transform, true);
115
     }
140
     }
116
 #ifdef _DEBUG
141
 #ifdef _DEBUG
117
     float x, y;
142
     float x, y;

+ 19
- 6
src/pabloader/awoorwae.h View File

8
 class Awoorwae;
8
 class Awoorwae;
9
 
9
 
10
 class Entity {
10
 class Entity {
11
-private:
11
+protected:
12
     Awoorwae* game;
12
     Awoorwae* game;
13
     olc::Sprite img;
13
     olc::Sprite img;
14
     olc::GFX2D::Transform2D transform;
14
     olc::GFX2D::Transform2D transform;
15
     float x, y;
15
     float x, y;
16
     float xv, yv;
16
     float xv, yv;
17
+    int w, h;
18
+    int tile = 0;
19
+    int skin = 0;
20
+    int tileCounter = 0;
17
     bool onGround = false;
21
     bool onGround = false;
18
 
22
 
19
 public:
23
 public:
20
     Entity(Awoorwae* game_);
24
     Entity(Awoorwae* game_);
25
+    virtual ~Entity(){};
26
+
27
+    virtual olc::rcode Load() = 0;
28
+    virtual void Update(float dt) = 0;
29
+    virtual void Draw() = 0;
30
+};
21
 
31
 
22
-    void Update(float dt);
23
-    void Draw();
24
-    olc::rcode Load();
32
+class Player : public Entity {
33
+public:
34
+    Player(Awoorwae* game);
35
+    olc::rcode Load() override;
36
+    void Update(float dt) override;
37
+    void Draw() override;
25
 };
38
 };
26
 
39
 
27
 class Awoorwae : public olc::PixelGameEngine {
40
 class Awoorwae : public olc::PixelGameEngine {
28
 private:
41
 private:
29
-    Entity* player;
42
+    Player* player;
30
 
43
 
31
 public:
44
 public:
32
     Awoorwae()
45
     Awoorwae()
33
     {
46
     {
34
         sAppName = "Awoorwa";
47
         sAppName = "Awoorwa";
35
-        player = new Entity(this);
48
+        player = new Player(this);
36
     }
49
     }
37
 
50
 
38
     ~Awoorwae()
51
     ~Awoorwae()

Loading…
Cancel
Save