Can someone help me with my move function for my main sprite?
My code:
void Move(RenderWindow* window, Vector2i UPDOWN, Vector2i LEFTRIGHT, Vector2f UPDOWNSPEED, Vector2f LEFTRIGHTSPEED, Sprite collider) {
if (window->hasFocus()) {
if (GetAsyncKeyState(UPDOWN.x) && collisionDir != Up) {
if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Up; }
else {
sprite.move(0.0f, UPDOWNSPEED.x);
//Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(0.0f, UPDOWNSPEED.x));
}
}
if (GetAsyncKeyState(UPDOWN.y) && collisionDir != Down) {
if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Down; }
else{
sprite.move(0.0f, UPDOWNSPEED.y);
//Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(0.0f, UPDOWNSPEED.y));
}
}
if (GetAsyncKeyState(LEFTRIGHT.x) && collisionDir != Left) {
if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Left; }
else {
sprite.move(LEFTRIGHTSPEED.x, 0.0f);
//Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(LEFTRIGHTSPEED.x, 0.0f));
}
}
if (GetAsyncKeyState(LEFTRIGHT.y) && collisionDir != Right) {
if (collisionDir == None && Collision::PixelPerfectTest(sprite, collider)) { collisionDir = Right; }
else {
sprite.move(LEFTRIGHTSPEED.y, 0.0f);
//Game::World::tileMap.setCamera(Game::World::tileMap.getCamera() + sf::Vector2f(LEFTRIGHTSPEED.y, 0.0f));
}
}
if (!Collision::PixelPerfectTest(sprite, collider)) { collisionDir = None; }
}
}
all of the collisionDir is for detecting which side the collider is at
if anyone has a better way i'd love to do it
if you switch positions quick enough, you can travel through a collider
and if you walk diagnally you can travel with them
any suggestions for better way to detect which side the collider is at?
or just a better way to do movement in general, i guess
here is proper use for calling Move func btw
Game::Entity::g_eLocal.Move(window, Vector2i(VK_UP, VK_DOWN), Vector2i(VK_LEFT, VK_RIGHT), Vector2f(-5.0f, 5.0f), Vector2f(-5.0f, 5.0f), Wall);