bool overlap(sf::Sprite& sprite1, sf::Sprite& sprite2) { sf::FloatRect rectangle1 = sprite1.getGlobalBounds(); sf::FloatRect rectangle2 = sprite2.getGlobalBounds(); // Rectangles with negative dimensions are allowed, so we must handle them correctly // Compute the min and max of the first rectangle on both axes float r1MinX = std::min(rectangle1.left, rectangle1.left + rectangle1.width); //left float r1MaxX = std::max(rectangle1.left, rectangle1.left + rectangle1.width);//right float r1MinY = std::min(rectangle1.top, rectangle1.top + rectangle1.height); // top float r1MaxY = std::max(rectangle1.top, rectangle1.top + rectangle1.height); // bottom of the maincharactersprite // Compute the min and max of the second rectangle on both axes float r2MinX = std::min(rectangle2.left, rectangle2.left + rectangle2.width);//left float r2MaxX = std::max(rectangle2.left, rectangle2.left + rectangle2.width);//right float r2MinY = std::min(rectangle2.top, rectangle2.top + rectangle2.height);//top of the blocksprite float r2MaxY = std::max(rectangle2.top, rectangle2.top + rectangle2.height); // bottom // Compute the intersection boundaries float interLeft = std::max(r1MinX, r2MinX);//get the most right one float interTop = std::max(r1MinY, r2MinY);//get the higher one float interRight = std::min(r1MaxX, r2MaxX);// get the most left one float interBottom = std::min(r1MaxY, r2MaxY);// the the lowest one //want to check if bottom of the mainCharacterSprite and top of the Block sprite are overlaping // If the intersection is valid (positive non zero area), then there is an intersection return ((interLeft <= interRight) && (interTop <= interBottom)); }
void set_color() { int opaque{abs(static_cast<int>(m_strength*255.0f))}; if (opaque > 255) { opaque = 255; } if (m_strength < 0.0f) { sf::Color light_red{m_light_red}; light_red.a = opaque; m_sprite.setColor(light_red); } else { sf::Color light_blue{m_light_blue}; light_blue.a = opaque; m_sprite.setColor(light_blue); } }
/*! * \param Obj The object * \return The bounding box of the object */ sf::IntRect Collision::GetBoundingBox(const sf::Sprite &Obj) { const sf::Vector2f &Position(Obj.GetPosition()); const sf::IntRect &SubRect(Obj.GetSubRect()); return sf::IntRect(Position.x, Position.y, Position.x + SubRect.GetWidth(), Position.y + SubRect.GetHeight()); }
float distance(sf::Sprite spA, sf::Sprite spB) { return sqrt((spA.getPosition().x - spB.getPosition().x) * (spA.getPosition().x - spB.getPosition().x) + (spA.getPosition().y - spB.getPosition().y) * (spA.getPosition().y - spB.getPosition().y)); }
void Update(float fTime) { FRect.left += dx * fTime; // x Collision(0); if (!bOnGround) dy = dy + 0.0005f * fTime; FRect.top += dy * fTime; bOnGround = false; // y Collision(1); fCurrentFrame += 0.005f * fTime; if (fCurrentFrame > 3) fCurrentFrame -= 3; if(dx > 0) SSprite.setTextureRect(sf::IntRect(112 + 30 * int(fCurrentFrame), 144, 16, 16)); if(dx < 0) SSprite.setTextureRect(sf::IntRect(112 + 30 * int(fCurrentFrame) + 16, 144, -16, 16)); SSprite.setPosition(FRect.left - g_fOffsetX, FRect.top - g_fOffsetY); dx = 0; }
void StateGameMenu::Update() { if(Gamepad::isButtonPressed(id, Gamepad::BUTTON_A)) { if(toggle) { MenuFX::PlaySfx(MenuFX::SELECT); StateManager::Pop(); } else { MenuFX::PlaySfx(MenuFX::BACK); Music::Play(Music::MENU); StateManager::Pop(); StateManager::Pop(); } return; } if(sf::Joystick::getAxisPosition(id,sf::Joystick::Y) < -50 && !toggle) { MenuFX::PlaySfx(MenuFX::CHANGE); toggle = true; menu_pointer_sprt.setPosition(208, 178); } else if(sf::Joystick::getAxisPosition(id,sf::Joystick::Y) > 50 && toggle) { MenuFX::PlaySfx(MenuFX::CHANGE); toggle = false; menu_pointer_sprt.setPosition(225, 203); } }
void afficheConstructible (sf::RenderWindow &App, sf::Sprite &SpriteTower ,sf::Shape &PolygonPorte,Constructible* maCons,int cote, int offSetX, int offSetY ) { // affichage du sprite de la construction int i = maCons->getPosition()->getI(); int j = maCons->getPosition()->getJ(); drawHexagoneIJ ( i,j, SpriteTower, cote, offSetX, offSetY); //SpriteTower.SetCenter(SpriteTower.GetSize().x/2,SpriteTower.GetSize().y/2); sf::Vector2f positionAvant = SpriteTower.getPosition(); SpriteTower.setPosition(positionAvant.x-SpriteTower.getTextureRect().height,positionAvant.y-SpriteTower.getTextureRect().width); App.draw(SpriteTower); // affichage de la porté std::set<Case *> cibles = maCons->getCaseAPorteDEffet(); std::set<Case *>::iterator its; for ( its= cibles.begin() ; its != cibles.end() ; ++its ) { drawHexagoneIJ ( (*its)->getI() , (*its)->getJ(), PolygonPorte, cote, offSetX, offSetY); App.draw(PolygonPorte); } // les point de vue ( mon dieu c'est moche le code graphique ) double centreX = SpriteTower.getTextureRect().width /2 ;// SpriteTower.getTexture().getSize().x/2; double centreY = SpriteTower.getTextureRect().height/2 ; /*sf::Shape pv = sf::Shape::Line (0,0, 0+maCons->getPV()/2 ,0,5,sf::Color::Black); drawHexagoneIJ ( i,j, pv, cote, offSetX, offSetY); pv.Move(0-centreX,0-centreY); App.draw(pv);*/ }
sf::Vector2f getCenter(const sf::Sprite& sprite) { sf::Vector2f center = sprite.getPosition(); center.x += sprite.getTextureRect().width / 2.f; center.y += sprite.getTextureRect().height / 2.f; return center; }
bool isAbove(sf::Sprite sp2) { if ((sp2.getPosition().y < sprite.getPosition().y)!=playerAbove) { playerAbove=!playerAbove; return true; } return false; }
bool overlap(sf::Sprite& sprite1, sf::Sprite& sprite2) { sf::FloatRect rectangle1 = sprite1.getGlobalBounds(); sf::FloatRect rectangle2 = sprite2.getGlobalBounds(); // Rectangles with negative dimensions are allowed, so we must handle them correctly // Compute the min and max of the first rectangle on both axes float r1MinX = std::min(rectangle1.left, rectangle1.left + rectangle1.width); float r1MaxX = std::max(rectangle1.left, rectangle1.left + rectangle1.width); float r1MinY = std::min(rectangle1.top, rectangle1.top + rectangle1.height); float r1MaxY = std::max(rectangle1.top, rectangle1.top + rectangle1.height); // Compute the min and max of the second rectangle on both axes float r2MinX = std::min(rectangle2.left, rectangle2.left + rectangle2.width); float r2MaxX = std::max(rectangle2.left, rectangle2.left + rectangle2.width); float r2MinY = std::min(rectangle2.top, rectangle2.top + rectangle2.height); float r2MaxY = std::max(rectangle2.top, rectangle2.top + rectangle2.height); // Compute the intersection boundaries float interLeft = std::max(r1MinX, r2MinX); float interTop = std::max(r1MinY, r2MinY); float interRight = std::min(r1MaxX, r2MaxX); float interBottom = std::min(r1MaxY, r2MaxY); // If the intersection is valid (positive non zero area), then there is an intersection return ((interLeft <= interRight) && (interTop <= interBottom)); }
sf::Vector2f Collision2D::GetSize(const sf::Sprite& sprite) { // Return the size of the sprite adjusting for scale const sf::IntRect iOriginalSize = sprite.getTextureRect(); const sf::Vector2f fScale = sprite.getScale(); return sf::Vector2f(iOriginalSize.width * fScale.x, iOriginalSize.height * fScale.y); }
void movement(sf::Sprite& character, sf::RenderWindow& window) { float moveSpeed = 0.1, moveSpeedR = -0.1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) { moveSpeed /= 2.2; moveSpeedR /= 2.2; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) { moveSpeed *= 2.2; moveSpeedR *= 2.2; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { character.move(0, moveSpeedR); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { character.move(0, moveSpeed); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { character.move(moveSpeed, 0); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { character.move(moveSpeedR, 0); } }
bool testForStanding(sf::Sprite sp2) { if ((sp2.getGlobalBounds().left+sp2.getGlobalBounds().width > sprite.getPosition().x) && (sp2.getGlobalBounds().left < sprite.getPosition().x+sprite.getTextureRect().width*sprite.getScale().x)) { return true; } return false; }
void drawSprite(int x, int y, double w, double h, TextureIndex textureIndex) { freeSprite.setPosition(x, y); freeSprite.setTexture(getTexture(textureIndex)); freeSprite.setScale(w / getTexture(textureIndex).getSize().x, h / getTexture(textureIndex).getSize().y); window->draw(freeSprite); }
Bala::Bala(float _v, sf::Sprite& _cannon, sf::Image& imagen) { float cannonA, cannonX, cannonY, cannonH; //Cargo sprite sprite.SetImage(imagen); sprite.SetCenter(imagen.GetWidth()/2, imagen.GetHeight()/2); //Inicializo variables dt = 0.0f; cannonA = _cannon.GetRotation(); cannonX = _cannon.GetPosition().x; cannonY = _cannon.GetPosition().y; //Tomo el tamaño sobre el eje x como el largo del cañon //porque sin rotar la misma se encuentra en horizontal cannonH = _cannon.GetSize().x; //Descompongo la velocidad inicial vx = _v * (cos(cannonA/180*PI)); vy = _v * (sin(cannonA/180*PI)); //Posicion inicial bala según la posición del cañon //usando trigonometria xi = cannonX + ((cos(cannonA/180*PI) * cannonH)); yi = cannonY - ((sin(cannonA/180*PI) * cannonH)); sprite.SetPosition(xi, yi); }
bool PixelPerfectTest(const sf::Sprite& Object1, const sf::Sprite& Object2, sf::Uint8 AlphaLimit) { sf::FloatRect Intersection; if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) { sf::IntRect O1SubRect = Object1.getTextureRect(); sf::IntRect O2SubRect = Object2.getTextureRect(); sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture()); sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture()); // Loop through our pixels for (int i = Intersection.left; i < Intersection.left+Intersection.width; i++) { for (int j = Intersection.top; j < Intersection.top+Intersection.height; j++) { sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j); sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j); // Make sure pixels fall within the sprite's subrect if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 && o1v.x < O1SubRect.width && o1v.y < O1SubRect.height && o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) { if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x)+O1SubRect.left, (int)(o1v.y)+O1SubRect.top) > AlphaLimit && Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x)+O2SubRect.left, (int)(o2v.y)+O2SubRect.top) > AlphaLimit) return true; } } } } return false; }
void cursor::setTexture(string cursorPass) { sprite.setTexture(textureManager.get(cursorPass)); sprite.setOrigin((Collision::GetSpriteSize(sprite).x / sprite.getScale().x / 2), (Collision::GetSpriteSize(sprite).y / sprite.getScale().y / 2)); sprite.rotate(-90); sprite.setColor(sf::Color(255, 255, 255, settings.getCursorAplha())); }
void init() { m_first_trip = false; m_current_state = IDLE; getBody()->GetFixtureList()->SetUserData("Checkpoint"); getBody()->SetUserData(this); //Load Texture s_texture = "Assets/Game/cp_ps.png"; m_text_size = sf::Vector2u(42, 94); m_sprite.setTexture(ResourceManager<sf::Texture>::instance()->get(s_texture)); m_sprite.setOrigin(m_text_size.x / 2, m_text_size.y / 2); m_sprite.setPosition(vHelper::toSF(getBody()->GetPosition())); addFrames(frame_idle, 0, 0, 1, 42, 94, 1.0f); addFrames(frame_trip, 1, 0, 5, 42, 94, 1.0f); addFrames(frame_active, 2, 0, 3, 42, 94, 1.0f); m_animator.addAnimation(IDLE, frame_idle, sf::seconds(1.0f)); m_animator.addAnimation(TRIP, frame_trip, sf::seconds(0.4f)); m_animator.addAnimation(ACTIVE, frame_active, sf::seconds(0.3f)); //Load Audio s_noise = "Assets/Audio/Game/cp.wav"; m_noise.setBuffer(ResourceManager<sf::SoundBuffer>::instance()->get(s_noise)); }
void _Log::load_image(){ if( image_loaded == false && image_path.length() != 0 ) { texture_1.loadFromFile( image_path ); Sprite.setTexture( texture_1, false ); sf::IntRect spr_data = Sprite.getTextureRect(); float x,y = WINDOW_HEIGHT - spr_data.height; switch( align ){ case 1: x = 0.f; break; case 2: x = WINDOW_WIDTH * 0.25 - spr_data.width /2; break; case 3: x = WINDOW_WIDTH * 0.5 - spr_data.width /2; break; case 4: x = WINDOW_WIDTH * 0.75 - spr_data.width /2; break; case 5: x = WINDOW_WIDTH - spr_data.width; break; } Sprite.setPosition( x, y ); cout << x << " " << y << endl; image_loaded = true; } }
//Enemy sprite render. void update(float deltaTime, char** levelMap, struct config* config) { if(mDirection == 0) {mMovement.x = 0; mMovement.y = -mSpeed;} if(mDirection == 1) {mMovement.x = mSpeed; mMovement.y = 0;} if(mDirection == 2) {mMovement.x = 0; mMovement.y = mSpeed;} if(mDirection == 3) {mMovement.x = mSpeed; mMovement.y = 0;} mRect.left += mMovement.x * deltaTime; collision(levelMap, config); mRect.top += mMovement.y * deltaTime; collision(levelMap, config); //Enemy animation. mCurrentFrame += mAnimationSpeed * deltaTime; if(mCurrentFrame > mFrameCount) mCurrentFrame -= mFrameCount; if(mMovement.x > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 925, mRect.width, mRect.height)); if(mMovement.x < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 667, mRect.width, mRect.height)); if(mMovement.y > 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 535, mRect.width, mRect.height)); if(mMovement.y < 0) mSprite.setTextureRect(sf::IntRect(mRect.width * int(mCurrentFrame), 787, mRect.width, mRect.height)); mSprite.setPosition(mRect.left - offsetX, mRect.top - offsetY); mTextName.setPosition(mRect.left - offsetX, mRect.top - mTextName.getCharacterSize() - offsetY); }
/** *\fn void moveLeft(void) *\brief allows the Tamagotchi to move *\param None *\return Null */ void moveLeft(sf::Sprite &t, sf::Texture &t1, sf::Texture &t2) { sf::Vector2f pos = t.getPosition(); if(pos.x < 2) { D = 0; } if(pos.x > 600) { D = 1; } if(D == 0) { t.move(+1, 0); t.setTexture(t2, 1); } if(D == 1) { t.move(-1, 0); t.setTexture(t1, 1); } }
//Player constructor. Player(sf::Texture& texture, sf::Texture& hpImage, int x, int y, sf::Font& font) { mSprite.setTexture(texture); mRect = sf::FloatRect(x, y, 120, 110); //Character x, y, width, height. mName = "Player 1"; mTextName.setString(mName); mTextName.setFont(font); mTextName.setCharacterSize(30); mTextName.setStyle(sf::Text::Bold); mTextName.setColor(sf::Color::Red); mHpSprite.setTexture(hpImage); mHpSprite.setTextureRect(sf::IntRect(0, 0, 100, 10)); mSprite.setTextureRect(sf::IntRect(0, 15, 120, 110)); mSpeed = 0.1; mMovement.x = 0; mMovement.y = 0; mCurrentFrame = 0; mAnimationSpeed = 0.005; mIsAlive = true; mHP = 100; mMaxHp = 100; mMP = 100; }
void Camara::moveRight(sf::Sprite &personaje){ if(desplazamientoCamara==0){ //comprobamos que la posicion del personaje sea mayor que la camara y que el centro de la camara sea menor del fondo if(personaje.getPosition().x >= camara->getSize().x*0.6 && camara->getCenter().x <=(background.getLocalBounds().width*0.79)){ //if(personaje.getPosition().x >= camara->getSize().x*0.6 && camara->getCenter().x <=(mapa->_tileWidth*mapa->_width*0.79)){ std::cout<<( _tileWidth_Map*_width_Map*0.79)<<std::endl; //con background.getLocalBounds().width*0.79 calculo la parte derecha del mapa para fijar la camara camara->move(kVel,0); desplazamientoCamara=desplazamientoCamara+kVel; } /*if(personaje.getPosition().x >= camara->getSize().x*0.6 && camara->getCenter().x <=(background.getLocalBounds().width*0.79)){ //con background.getLocalBounds().width*0.79 calculo la parte derecha del mapa para fijar la camara camara->move(kVel,0); desplazamientoCamara=desplazamientoCamara+kVel; }*/ }else{ //con esto averiguamos el margen derecho para saber cuando parar la camara al llegar al final del mapa int margen= background.getLocalBounds().width-(background.getLocalBounds().width*0.79); if(personaje.getPosition().x>= camara->getSize().x*0.6 + desplazamientoCamara && camara->getCenter().x<=(personaje.getPosition().x+margen) && (personaje.getPosition().x+margen)<(_tileWidth_Map*_width_Map)){ //if(personaje.getPosition().x>= camara->getSize().x*0.6 + desplazamientoCamara && camara->getCenter().x<=(background.getLocalBounds().width*0.79)){ //if(personaje.getPosition().x>= camara->getSize().x*0.6 + desplazamientoCamara && camara->getCenter().x<=(mapa->_tileWidth*mapa->_width*0.79)){ camara->move(kVel,0); desplazamientoCamara=desplazamientoCamara+kVel; } } }
bool ImageButton(const sf::Sprite& sprite, const sf::Vector2f& size, const int framePadding, const sf::Color& bgColor, const sf::Color& tintColor) { const sf::Texture* texturePtr = sprite.getTexture(); if (!texturePtr) { return false; } return ::imageButtonImpl(*texturePtr, static_cast<sf::FloatRect>(sprite.getTextureRect()), size, framePadding, bgColor, tintColor); }
// fucntion to handle player input during the game void Inputer(sf::Sprite &playerSprite, float deltaTime) { // Check the boundry int boundry[4] = { 0, 0, 4000, 4000 }; // declare vector2f variable to store player position sf::Vector2f pos = playerSprite.getPosition(); // move player at a velocity of 300 * variable deltaTime float velocity = 300.0f * deltaTime; // take key input from player, check boundary, and move accordingly at stored velocity if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if (pos.x > boundry[0]) playerSprite.move(-velocity, 0); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { if (pos.x < boundry[2]) playerSprite.move(velocity, 0); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { if (pos.y > boundry[1]) playerSprite.move(0, -velocity); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { if (pos.y < boundry[3]) playerSprite.move(0, velocity); } }
//Drop item constructor. DropItem(sf::Texture& texture, std::string type, int effect, int x, int y) { mSprite.setTexture(texture); mRect = sf::FloatRect(x, y, 32, 32); mSprite.setTextureRect(sf::IntRect(0, 0, 32, 32)); mCurrentFrame = 0; mEffectValue = effect; mIsMarkedForRemoval = false; }
void resize(sf::Sprite& sprite, float width, float height) { int local_width = sprite.getTextureRect().width; int local_height = sprite.getTextureRect().height; if ((local_width > 0) && (local_height > 0)) sprite.setScale(width / local_width, height / local_height); }
void Camera::follow(sf::Sprite& sp, float d) { Vector2D vd(0, d); vd.rotate( sp.getRotation()); setRotation(sp.getRotation()); setCenter( sp.getPosition() + vd); }
sf::IntRect Colision::getIntRect(const sf::Sprite &sprite) { const sf::Vector2f &pos(sprite.GetPosition()); const sf::IntRect &sub_rect(sprite.GetSubRect()); sf::IntRect int_rect((int)pos.x, (int)pos.y, (int)(pos.x + sub_rect.GetWidth()), (int)(pos.y + sub_rect.GetHeight())); return (int_rect); }
SpriteNode(const sf::Texture& t, sf::Vector2f pos) : mSprite(t) { sf::FloatRect bounds = mSprite.getLocalBounds(); mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f); mSprite.setPosition(pos); };