Shuriken::Shuriken(const sf::Vector2f& position, const sf::Vector2f& velocity, Game* game) : Entity(position, game, "shuriken " + std::to_string(Id)) { ++Id; sprite_.setTexture(game->getTextureManager().getTexture("spriteSheet")); sprite_.setTextureRect(sf::IntRect(0, 310, 20, 20)); sprite_.setOrigin(sprite_.getLocalBounds().left + sprite_.getLocalBounds().width / 2.f, sprite_.getLocalBounds().top + sprite_.getLocalBounds().height / 2.f); b2BodyDef bodyDef; bodyDef.position.Set(pixelsToMeters(position.x), pixelsToMeters(position.y)); bodyDef.type = b2_dynamicBody; b2PolygonShape shape; shape.SetAsBox(pixelsToMeters(static_cast<float>(20)) / 2.f, pixelsToMeters(static_cast<float>(20)) / 2.f); body_ = game->getWorld()->CreateBody(&bodyDef); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.f; fixtureDef.restitution = 0.f; fixtureDef.isSensor = true; body_->CreateFixture(&fixtureDef); body_->SetFixedRotation(false); body_->SetUserData(this); body_->SetGravityScale(0.f); body_->ApplyLinearImpulse(b2Vec2(velocity.x, velocity.y), body_->GetWorldCenter(), true); body_->SetAngularVelocity(10.f); sprite_.setPosition(metersToPixels(body_->GetPosition().x), metersToPixels(body_->GetPosition().y)); }
Finish::Finish(const sf::Vector2f& position, Game* game) : Entity(position, game, "finish") { sprite_.setTexture(game->getTextureManager().getTexture("spriteSheet")); sprite_.setTextureRect(sf::IntRect(0, 335, 32, 32)); sprite_.setOrigin(sprite_.getLocalBounds().left + sprite_.getLocalBounds().width / 2.f, sprite_.getLocalBounds().top + sprite_.getLocalBounds().height / 2.f); b2BodyDef bodyDef; bodyDef.position.Set(pixelsToMeters(position.x), pixelsToMeters(position.y)); b2PolygonShape shape; shape.SetAsBox(pixelsToMeters(static_cast<float>(sprite_.getTexture()->getSize().x)) / 2.f, pixelsToMeters(static_cast<float>(sprite_.getTexture()->getSize().y)) / 2.f); body_ = game->getWorld()->CreateBody(&bodyDef); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.f; fixtureDef.isSensor = true; body_->CreateFixture(&fixtureDef); body_->SetGravityScale(0.f); body_->SetFixedRotation(true); body_->SetUserData(this); sprite_.setPosition(metersToPixels(body_->GetPosition().x), metersToPixels(body_->GetPosition().y)); }
void Renderer::drawCircle(const b2Vec2& center, float32 radius, Uint8 red, Uint8 green, Uint8 blue, Uint8 alpha, bool filled){ b2Vec2 coords = toScreenCoords(center); if(filled){ filledCircleRGBA(renderer, (Sint16) coords.x, (Sint16) coords.y, (Sint16) metersToPixels(radius), red, green, blue, alpha); }else{ circleRGBA(renderer, (Sint16) coords.x, (Sint16) coords.y, (Sint16) metersToPixels(radius), red, green, blue, alpha); } }
void Ball::draw() { glColor3f(1.f, 1.f, 1.f); ballImage->drawAt(metersToPixels(Xpos), metersToPixels(Ypos), true); if (ballStatsLabel) ballStatsLabel->draw(1200, 550); if (xPosLabel) xPosLabel->draw(1200, 520); if (yPosLabel) yPosLabel->draw(1200, 500); if (accelYLabel) accelYLabel->draw(1200, 480); if (accelXLabel) accelXLabel->draw(1200, 460); if (yVelLabel) yVelLabel->draw(1200, 440); if (xVelLabel) xVelLabel->draw(1200, 420); }
Bullet::Bullet(const sf::Vector2f& position, int direction, Game* game) : Entity(position, game, "bullet " + std::to_string(Id)) , Speed(0.5f) , TextureWidth(16) , TextureHeight(16) { ++Id; sprite_.setTexture(game->getTextureManager().getTexture("spriteSheet")); sprite_.setTextureRect(sf::IntRect(0, 0, TextureWidth, TextureHeight)); sprite_.setOrigin(sprite_.getLocalBounds().left + sprite_.getLocalBounds().width / 2.f, sprite_.getLocalBounds().top + sprite_.getLocalBounds().height / 2.f); b2BodyDef bodyDef; bodyDef.position.Set(pixelsToMeters(position.x), pixelsToMeters(position.y)); bodyDef.type = b2_dynamicBody; b2PolygonShape shape; shape.SetAsBox(pixelsToMeters(static_cast<float>(TextureWidth)) / 2.f, pixelsToMeters(static_cast<float>(TextureHeight)) / 2.f); body_ = game->getWorld()->CreateBody(&bodyDef); b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = 1.f; fixtureDef.restitution = 0.f; fixtureDef.isSensor = true; body_->CreateFixture(&fixtureDef); body_->SetFixedRotation(true); body_->SetUserData(this); body_->SetGravityScale(0.f); body_->ApplyLinearImpulse(b2Vec2(Speed * direction, 0.f), body_->GetWorldCenter(), true); sprite_.setPosition(metersToPixels(body_->GetPosition().x), metersToPixels(body_->GetPosition().y)); }
void Bullet::update(sf::Time delta) { sprite_.setPosition(metersToPixels(body_->GetPosition().x), metersToPixels(body_->GetPosition().y)); }
b2Vec2 Renderer::toScreenCoords(const b2Vec2 &position){ return b2Vec2 ( std::round(width * PADDING_PERCENT) + metersToPixels(position.x), std::round(height * PADDING_PERCENT) + metersToPixels(position.y) ); }
void Shuriken::update(sf::Time delta) { sprite_.setPosition(metersToPixels(body_->GetPosition().x), metersToPixels(body_->GetPosition().y)); sprite_.setRotation(body_->GetAngle() * (180.f / 3.14159f)); }