float RuntimeSpriteObject::GetPointY(const gd::String & name) const { if ( !name.empty() ) { const Point & point = GetCurrentSprite().GetPoint(name); return GetCurrentSFMLSprite().getTransform().transformPoint(point.GetX(), point.GetY()).y; } return GetY(); }
std::vector<Polygon2d> RuntimeSpriteObject::GetHitBoxes() const { if ( currentAnimation >= animations.size() ) { std::vector<Polygon2d> hitboxes; //Invalid animation, bail out. return hitboxes; } const sf::Sprite & currentSFMLSprite = GetCurrentSFMLSprite(); std::vector<Polygon2d> polygons = GetCurrentSprite().GetCollisionMask(); for (std::size_t i = 0;i<polygons.size();++i) { for (std::size_t j = 0;j<polygons[i].vertices.size();++j) { sf::Vector2f newVertice = currentSFMLSprite.getTransform().transformPoint( !isFlippedX ? polygons[i].vertices[j].x : GetCurrentSprite().GetSFMLSprite().getLocalBounds().width-polygons[i].vertices[j].x, !isFlippedY ? polygons[i].vertices[j].y : GetCurrentSprite().GetSFMLSprite().getLocalBounds().height-polygons[i].vertices[j].y); polygons[i].vertices[j] = newVertice; } } return polygons; }
bool RuntimeSpriteObject::CursorOnObject(RuntimeScene & scene, bool accurate) { #if defined(ANDROID) //TODO: Accurate test leads to strange result with touches. accurate = false; #endif RuntimeLayer & theLayer = scene.GetRuntimeLayer(layer); auto insideObject = [this, accurate](const sf::Vector2f & pos) { if (GetDrawableX() <= pos.x && GetDrawableX() + GetWidth() >= pos.x && GetDrawableY() <= pos.y && GetDrawableY() + GetHeight() >= pos.y) { int localX = static_cast<int>( pos.x - GetDrawableX() ); int localY = static_cast<int>( pos.y - GetDrawableY() ); return ( !accurate || GetCurrentSprite().GetSFMLTexture()->image.getPixel(localX , localY).a != 0); } }; for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { const auto & view = theLayer.GetCamera(cameraIndex).GetSFMLView(); sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), view); if (insideObject(mousePos)) return true; auto & touches = scene.GetInputManager().GetAllTouches(); for(auto & it : touches) { sf::Vector2f touchPos = scene.renderWindow->mapPixelToCoords(it.second, view); if (insideObject(touchPos)) return true; } } return false; }
bool RuntimeSpriteObject::CursorOnObject(RuntimeScene & scene, bool accurate) { RuntimeLayer & theLayer = scene.GetRuntimeLayer(layer); for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), theLayer.GetCamera(cameraIndex).GetSFMLView()); if (GetDrawableX() <= mousePos.x && GetDrawableX() + GetWidth() >= mousePos.x && GetDrawableY() <= mousePos.y && GetDrawableY() + GetHeight() >= mousePos.y) { int localX = static_cast<int>( mousePos.x - GetDrawableX() ); int localY = static_cast<int>( mousePos.y - GetDrawableY() ); return ( !accurate || GetCurrentSprite().GetSFMLTexture()->image.getPixel( localX , localY ).a != 0 ); } } return false; }
float RuntimeSpriteObject::GetCenterY() const { return GetCurrentSprite().GetCenter().GetY()*fabs(scaleY); }
float RuntimeSpriteObject::GetDrawableY() const { return Y - GetCurrentSprite().GetOrigin().GetY()*fabs(scaleY); }
/** * Y center is computed with the current sprite */ float RuntimeSpriteObject::GetCenterY() const { //Just need to multiply by the scale as it is the center return GetCurrentSprite().GetCentre().GetY()*fabs(scaleY); }
/** * Get the real X position of the sprite */ float RuntimeSpriteObject::GetDrawableX() const { return X - GetCurrentSprite().GetOrigine().GetX()*fabs(scaleX); }