Add bow
This commit is contained in:
parent
6bf8c2cb88
commit
d46ee05126
@ -87,6 +87,10 @@ void Game::run()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mouse_x, mouse_y;
|
||||||
|
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||||
|
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2);
|
||||||
|
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,11 +54,10 @@ void GameRenderer::draw_sprite(const Sprite sprite, const int x, const int y) co
|
|||||||
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
|
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameRenderer::draw_sprite_rotated(const Sprite sprite, const int x, const int y, const double angle, const int center_x, const int center_y) const
|
void GameRenderer::draw_sprite_rotated(const Sprite sprite, const int x, const int y, const double angle) const
|
||||||
{
|
{
|
||||||
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
||||||
const SDL_Point center = { .x = center_x, .y = center_y };
|
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
|
||||||
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, ¢er, SDL_FLIP_NONE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameRenderer::clear_screen(const int r, const int g, const int b, const int a) const
|
void GameRenderer::clear_screen(const int r, const int g, const int b, const int a) const
|
||||||
|
|||||||
@ -25,7 +25,7 @@ public:
|
|||||||
|
|
||||||
void draw_sprite(Sprite sprite, int x, int y) const;
|
void draw_sprite(Sprite sprite, int x, int y) const;
|
||||||
|
|
||||||
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle, int center_x, int center_y) const;
|
void draw_sprite_rotated(Sprite sprite, int x, int y, double angle) const;
|
||||||
|
|
||||||
void clear_screen(int r, int g, int b, int a) const;
|
void clear_screen(int r, int g, int b, int a) const;
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
Player::Player(GameRenderer *renderer) : renderer(renderer)
|
Player::Player(GameRenderer *renderer) : renderer(renderer)
|
||||||
{
|
{
|
||||||
hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40);
|
hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40);
|
||||||
|
bow_sprite = renderer->load_sprite("./assets/bow_arrow.png", 22, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::draw() const
|
void Player::draw() const
|
||||||
@ -13,6 +14,13 @@ void Player::draw() const
|
|||||||
renderer->screen_width / 2 - hero_sprite.width / 2,
|
renderer->screen_width / 2 - hero_sprite.width / 2,
|
||||||
renderer->screen_height / 2 - hero_sprite.height / 2
|
renderer->screen_height / 2 - hero_sprite.height / 2
|
||||||
);
|
);
|
||||||
|
|
||||||
|
renderer->draw_sprite_rotated(
|
||||||
|
bow_sprite,
|
||||||
|
renderer->screen_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30,
|
||||||
|
renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30,
|
||||||
|
angle * 180 / M_PI
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Player::update()
|
void Player::update()
|
||||||
|
|||||||
@ -7,7 +7,7 @@ class Player
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
GameRenderer *renderer;
|
GameRenderer *renderer;
|
||||||
Sprite hero_sprite;
|
Sprite hero_sprite, bow_sprite;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
double x = 0;
|
double x = 0;
|
||||||
@ -16,6 +16,8 @@ public:
|
|||||||
double x_vel = 0;
|
double x_vel = 0;
|
||||||
double y_vel = 0;
|
double y_vel = 0;
|
||||||
|
|
||||||
|
double angle = 0.0;
|
||||||
|
|
||||||
const double speed = 1.5;
|
const double speed = 1.5;
|
||||||
|
|
||||||
Player(GameRenderer *renderer);
|
Player(GameRenderer *renderer);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user