Adding a little bow to my game
So I am working on a small game, my second ever, to learn C++ and gamedev.
It is about a little guy who shoots arrows. (Today I recolored the player character to be a dark elf, because I am reading Malazan, and I am easily influenced -- Malazan fans are now furiously shouting at the monitor that Tiste Andii are not dark elfs)
My newest addition is a little bow that faces the direction the player is shooting:
It is very crude full of hardcoded magic numbers:
void Engine::renderWeapon(std::shared_ptr<Entity> e) {
SDL_Rect crop = calcTilePos(BOW);
SDL_Rect pos = {(int)e->mDimensions->radiusCentre.first - 26,
(int)e->mDimensions->radiusCentre.second - 32, 64, 64};
int x, y;
SDL_GetMouseState(&x, &y);
std::pair<float, float> mousePositionVector = calculateProjectileVector(
e->mDimensions->radiusCentre, std::make_pair(x, y));
float rotation =
angleFromVector(mousePositionVector.first, mousePositionVector.second);
SDL_RenderCopyEx(mRenderer, mTexture, &crop, &pos, rotation + 90, nullptr,
SDL_FLIP_NONE);
}
It calculates a vector based on the mouse position relative to the player, then turns that vector to degrees. Finally I add 90 to it before rendering, so not the tip of the bow, but the front of it looks in the direction of shooting.
It looks a little weird because of the low pixel count scaled to the high resolution the bow is covering too much of the player character, but I will write that off as charm, and move on.