Steve with his moving legs.

This commit is contained in:
革匊习习中 2022-08-01 17:19:30 +08:00
parent 7f9985d5eb
commit 47b32bf8a4
3 changed files with 31 additions and 12 deletions

View File

@ -43,7 +43,7 @@ public:
virtual void OnUpdate(float delta) = 0; virtual void OnUpdate(float delta) = 0;
virtual void OnLaterUpdate(float delta) = 0; virtual void OnLaterUpdate(float delta) = 0;
void OnRender() virtual void OnRender()
{ {
Rect dst = Rect dst =
{ {

View File

@ -11,6 +11,9 @@ export
}; };
struct Rect : D2D1_RECT_F struct Rect : D2D1_RECT_F
{ {
Vector2 size() const&
{
return { right - left, bottom - top };
}
}; };
} }

View File

@ -8,17 +8,30 @@ import Input;
export struct Steve : GameObject export struct Steve : GameObject
{ {
static constexpr const float ground_height = 50;
static constexpr const float y_pos = 100;
float a, v, h; float a, v, h;
bool jumping = false; bool jumping = false;
float time = 0.0f;
Rect IdleRect = { 76, 2, 164, 96 };
Rect NormalRect = { 1678, 2, 1766, 96 };
Rect CloseEyeRect = { 1766, 2, 1854, 96 };
Rect RightRect = { 1854, 2, 1942, 96 };
Rect LeftRect = { 1942, 2, 2030, 96 };
Rect BigEyeRect = { 2030, 2, 2118, 96 };
Rect DeadRect = { 2122, 2, 2202, 96 };
Rect CrawlingRightRect = { 2203, 2, 2321, 96 };
Rect CrawlingLeftRect = { 2321, 2, 2439, 96 };
enum Status { Idle, Running, Jumping, Freefall, Crawling, Dead } status; enum Status { Idle, Running, Jumping, Freefall, Crawling, Dead } status;
virtual void OnStartUp() override virtual void OnStartUp() override
{ {
imge = Texture::LoadFromFile(L"steve.png"); imge = Texture::LoadFromFile(L"steve.png");
imge.rect = { 1678, 2, 1766, 96 }; imge.rect = IdleRect;
Posi = { 94, 100 }; Posi = { y_pos, ground_height };
Anch = { 0.5f,0 }; Anch = { 0.5f,0 };
Size = { 88, 94 }; Size = { 88, 94 };
@ -30,25 +43,28 @@ export struct Steve : GameObject
} }
virtual void OnUpdate(float delta) override virtual void OnUpdate(float delta) override
{ {
time += delta;
switch (status) switch (status)
{ {
case Steve::Idle: case Steve::Idle:
if (KeyBoard::GetKey(VK_SPACE)) status = Running; if (KeyBoard::GetKey(VK_SPACE)) status = Running;
imge.rect = IdleRect;
break; break;
case Steve::Running: case Steve::Running:
if (KeyBoard::GetKey(VK_SPACE)) if (KeyBoard::GetKey(VK_SPACE))
{ {
status = Jumping; status = Jumping;
goto _jump_; break;
} }
imge.rect = (int)(time / 0.1) % 2 ? LeftRect : RightRect;
break; break;
case Steve::Jumping: case Steve::Jumping:
_jump_: imge.rect = NormalRect;
if ((h < 60 || KeyBoard::GetKey(VK_SPACE)) && h < 120.f) if ((h < 75 || KeyBoard::GetKey(VK_SPACE)) && h < 150.f)
{ {
v = 1000.f; v = 1000.f;
h += v * delta; h += v * delta;
Posi.y = 100 + h; Posi.y = ground_height + h;
} }
else else
{ {
@ -58,13 +74,13 @@ export struct Steve : GameObject
case Steve::Freefall: case Steve::Freefall:
a = -8000.f; a = -8000.f;
v += a * delta; v += a * delta;
if (v < -1000) v = -1200; if (v < -1200) v = -1200;
h += v * delta; h += v * delta;
Posi.y = 100 + h; Posi.y = ground_height + h;
if (Posi.y <= 100) if (Posi.y <= ground_height)
{ {
status = Running; status = Running;
Posi.y = 100; Posi.y = ground_height;
} }
break; break;
case Steve::Crawling: case Steve::Crawling: