Example #1
0
void SpawnSystem::update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt)
{
    if (!_respawn)
        return;

    _respawn = false;
    es.each<NodeComponent>([&] (entityx::Entity entity, NodeComponent&) {
        entity.destroy();
    });
    
    cocos2d::Vec2 ballSize(
        10.0f,
        10.0f
    );
    cocos2d::Vec2 leftPlankPos(
        _initParams.left->getContentSize().width * 0.5f,
        _initParams.fieldSize.y * 0.5f
        );
    cocos2d::Vec2 rightPlankPos(
        _initParams.fieldSize.x - _initParams.left->getContentSize().width * 0.5f,
        _initParams.fieldSize.y * 0.5f
        );
    float plankMaxSpeed = 3.0f;
    cocos2d::Vec2 plankSize(
        10,
        40
        );

    // creating ball
    for (int i = 0; i < _initParams.balls.size(); i++) {
        cocos2d::Vec2 ballPosition(
                                   rand() % int(_initParams.fieldSize.x * 0.5f) +
                                   _initParams.fieldSize.x * 0.25f,
                                   ballPosition.y = rand() % int(_initParams.fieldSize.y * 0.5f) +
                                   _initParams.fieldSize.y * 0.25f
                                   );
        int ballVelDisp = 200;
        cocos2d::Vec2 maxBallVel(6.0f, 2.0f);
        cocos2d::Vec2 ballVelocity(
                                   (rand() % ballVelDisp - ballVelDisp / 2) * (maxBallVel.x / ballVelDisp),
                                   (rand() % ballVelDisp - ballVelDisp / 2) * (maxBallVel.y / ballVelDisp)
                                   );
        
        entityx::Entity ballEntity = es.create();
        ballEntity.assign<NodeComponent>(_initParams.balls[i].get());
        ballEntity.assign<BallBodyComponent>(ballPosition, ballVelocity, ballSize);
    }
    // left plank
    entityx::Entity leftPlankEntity = es.create();
    leftPlankEntity.assign<NodeComponent>(_initParams.left.get());
    leftPlankEntity.assign<PlankComponent>(leftPlankPos, plankMaxSpeed, plankSize);
    leftPlankEntity.assign<PlayerNameComponent>("Player 1");

    // right plank
    entityx::Entity rightPlankEntity = es.create();
    rightPlankEntity.assign<NodeComponent>(_initParams.right.get());
    rightPlankEntity.assign<PlankComponent>(rightPlankPos, plankMaxSpeed, plankSize);
    rightPlankEntity.assign<PlayerNameComponent>("Player 2");
}
void soso::applyLinearForce(entityx::EntityManager &entities)
{
  ComponentHandle<LinearForce>  force;
  ComponentHandle<VerletBody>    body;
  for (auto __unused e : entities.entities_with_components(force, body)) {
    body->nudge(force->force);
  }
}
void soso::enforceBoundaries(entityx::EntityManager &entities)
{
  entityx::ComponentHandle<VerletBody> vc;
  entityx::ComponentHandle<Bounded> bc;
  for (auto e : entities.entities_with_components(vc, bc)) {
    if (! bc->contains(vc->position)) {
      e.destroy();
    }
  }
}
Camera::Handle Camera::getCamera(entityx::EntityManager &entities)
{
    Camera::Handle cameraHandle;
    auto count = 0;
    for (auto entity : entities.entities_with_components(cameraHandle))
    {
        (void)entity;
        assert(count++ == 0 && "there should only be one camera!");
    }
    return cameraHandle;
}
void SerializationSystem::update(entityx::EntityManager &entities, entityx::EventManager &events, double dt)
{
    if (not serializeFlag) {
        return;
    }
    serializeFlag = false;
    
    for (auto entity : entities.entities_for_debugging())
    {
        if(entity.has_component<Tag>()) {
            // already did this (saved game before,
            // don't need to do it again
            break;
        }
        entity.assign<Tag>();
    }
    
    std::cout << "printing json dump!\n";
    std::ofstream saveFile;
    saveFile.open("save.json");
    cereal::JSONOutputArchive archive( saveFile );
//    cereal::JSONOutputArchive archive( std::cout );
    //iterate over all entities
    for (auto entity : entities.entities_for_debugging())
    {
        archive( cereal::make_nvp("entity", entity) );
    }
    
    
    std::cout << "deleting all entities\n";
    for (auto entity : entities.entities_for_debugging())
    {
        entity.destroy();
    }
    std::cout << "reloading entities\n";
    std::ifstream loadFile;
    loadFile.open("save.json");
    cereal::JSONInputArchive archive2( loadFile );
    
    
}
    void UIRenderingSystem::update(entityx::EntityManager & entities, entityx::EventManager & events, entityx::TimeDelta dt)
    {
        prepareRendering();

        // TODO: sort text components based on font - bind texture atlas before text rendering
        entityx::ComponentHandle<UITextComponent> text_component;

        for(auto entity : entities.entities_with_components(text_component))
        {
            renderText(text_component);
        }
    }
std::string serializePositions(entityx::EntityManager &entities)
{
  ComponentHandle<Transform>      xf;
  ComponentHandle<InstanceShape>  instance;
  std::stringstream ss;

  for (auto __unused e : entities.entities_with_components(xf, instance))
  {
    ss << xf->position().x << "," << xf->position().y << "," << xf->position().z << "," << instance->_openness << std::endl;
  }
  
  return ss.str();
}
Example #8
0
File: ease.hpp Project: eco/vibrant
  void update(entityx::EntityManager& es, entityx::EventManager& events,
              entityx::TimeDelta dt) override
  {
    Easings<TargetComponent>::Handle easing;
    typename TargetComponent::Handle target;

    for (entityx::Entity entity : es.entities_with_components(easing, target))
    {
      // TODO: add events
      bool alive = easing->apply(target, dt);
      if (!alive) entity.remove<Easings<TargetComponent>>();
    }
  }
void soso::applyWanderingForce(entityx::EntityManager &entities)
{
  ComponentHandle<WanderingForce> force;
  ComponentHandle<VerletBody>      body;

  for (auto __unused e : entities.entities_with_components(force, body)) {
    auto heading = safeHeading(body->velocity());
    auto half_fov = force->fov_radians / 2;
    auto wander = ci::randFloat(- half_fov, half_fov);
    heading = glm::rotate(heading, wander, ci::vec3(0, 0, 1));

    auto f = heading * force->impulse;
    body->nudge(f);
  }
}
Example #10
0
void AnimationSystem::update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt)
{
    GraphicsComponent::Handle ghandle;
    AnimationComponent::Handle ahandle;

    for (auto entity : es.entities_with_components(ahandle, ghandle))
    {
        ahandle->animator.update(sf::seconds(dt));
        ahandle->animator.animate(ghandle->sprite);

        if (ahandle->firstAnimationProcessLaunch)
        {
            ahandle->firstAnimationProcessLaunch = false;
            sf::IntRect textureRect = ghandle->sprite.getTextureRect();
            ghandle->sprite.setOrigin(textureRect.left + textureRect.width / 2, textureRect.top - textureRect.height / 2);
        }
    }
}
Example #11
0
void LayoutSystem::update(entityx::EntityManager& es, entityx::EventManager& events,
                          entityx::TimeDelta dt)
{
  Layout::Handle layout;
  Body::Handle body;

  for (entityx::Entity entity : es.entities_with_components(layout, body))
  {
    assert(!layout->x.is_nil());
    assert(!layout->width.is_nil());
    assert(!layout->y.is_nil());
    assert(!layout->height.is_nil());

    body->position.x = layout->x.value();
    body->position.y = layout->y.value();
    body->size.x = layout->width.value();
    body->size.y = layout->height.value();
  }
}
Example #12
0
	void update(entityx::EntityManager &es, entityx::EventManager &events, double dt)
	{
		entityx::ComponentHandle<Moveable> moveable;
		entityx::ComponentHandle<Position> position;
		entityx::ComponentHandle<Player> player;

		for(entityx::Entity entity : es.entities_with_components(player))
		{
			float x = 0.0f;
			float y = 0.0f;

			const Uint8 *state = SDL_GetKeyboardState(NULL);
			if(state[SDL_SCANCODE_W])
			{
				y-=1.0f;
			}
			if(state[SDL_SCANCODE_A])
			{
				x-=1.0f;
			}
			if(state[SDL_SCANCODE_S])
			{
				y+=1.0f;
			}
			if(state[SDL_SCANCODE_D])
			{
				x+=1.0f;
			}
			if(state[SDL_SCANCODE_SPACE])
			{
				events.emit<PlayerInstructionLight>(entity);
			}
			if(x != 0.0f || y != 0.0f)
			{
				glm::vec2 direction(x,y);
				direction = glm::normalize(direction);// *(float) dt;
				events.emit<PlayerInstructionEvent>(direction,entity);
			}
			
		}
	}
Example #13
0
File: ease.hpp Project: eco/vibrant
  void update(entityx::EntityManager& es, entityx::EventManager& events,
              entityx::TimeDelta dt) override
  {
    FastEase<Time, Value, Comp>::Handle ease;
    typename Comp::Handle component;

    for (entityx::Entity entity : es.entities_with_components(ease, component))
    {
      ease->current += dt;
      if (ease->current < ease->total_time)
      {
        ease->value_function(component) =
            ease->easing_function(ease->current, ease->beginning, ease->change, ease->total_time);
      }
      else
      {
        ease->value_function(component) = ease->beginning + ease->change;
        entity.remove<FastEase<Time, Value, Comp>>();
      }
    }
  }
Example #14
0
void RenderableOrbit::update(entityx::EntityManager &entities)
{
    float  *pathGL;
    using namespace entityx;
    int pathSteps = 0;
    OrbitPath::Handle orbit;
    
    auto count = 0;
    for (Entity entity : entities.entities_with_components(orbit))
    {
        auto &path = orbit->path;
        if (path.empty())
        {
            std::cout << "empty orbit path\n";
            continue;
        }
        glBindVertexArray(orbit->vao);
        vao = orbit->vao;
        glBindBuffer(GL_ARRAY_BUFFER, orbit->vbo);
        setAttribute("position");
        check_gl_error();

        glBufferData(GL_ARRAY_BUFFER, sizeof(float)*path.size(), nullptr, GL_STREAM_DRAW);
        check_gl_error();
       
        pathGL = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
        check_gl_error();
        
        memcpy(pathGL, path.data(), sizeof(float)*path.size());
        transform = orbit->transform;
        
        drawCount = (int)(path.size())/3;
        
        check_gl_error();
        glUnmapBuffer(GL_ARRAY_BUFFER);
        check_gl_error();
        count++;
    }
}
Example #15
0
void PlayerSystem::update(entityx::EntityManager& entities, entityx::EventManager& events, double dt)
{
    if (!m_world->isServer()) {
        return;
    }

    PositionComponent::Handle positionComponent;
    PlayerComponent::Handle playerComponent;

    for (auto entity : entities.entities_with_components(positionComponent, playerComponent)) {
        calculateLoadedViewport(entity);

        //qCDebug(ORE_SERVER_ENTITY_LOGIC) << "pos: " << positionComponent->position();
        //qCDebug(ORE_IMPORTANT()) << "PROCESS PLAYER SYSTEM";

        Q_ASSERT(positionComponent->position().x >= 0.0);
        Q_ASSERT(positionComponent->position().y >= 0.0);

        if (positionComponent->positionDirty()) {
            m_world->m_server->sendPlayerMove(entity);

            positionComponent->setPositionDirty(false);

            const glm::vec2& currentChunkPosition = glm::vec2((positionComponent->position().x / BLOCK_SIZE), (positionComponent->position().y / BLOCK_SIZE));

            // conversion because glm::distance only allows float comparisons. and i don't want to
            // change player->lastLoadedChunk because that doesn't make sense as a float, and
            // would cause more issues
            const glm::vec2& lastChunk = glm::vec2(playerComponent->lastLoadedChunk);
            if (glm::distance(currentChunkPosition, lastChunk) > 20.0) {
                //qCDebug(ORE_IMPORTANT) << " server sending large world chunk..: ";
                m_world->m_server->sendLargeWorldChunkForPlayer(entity);
            }
        }
    }
}
Example #16
0
  void BehaviorSystem::update(entityx::EntityManager &es, entityx::EventManager &events, entityx::TimeDelta dt)
  {

    entityx::ComponentHandle<c::Character> anAlive;
    entityx::ComponentHandle<c::MapObject> anMapObject;
    entityx::ComponentHandle<c::Transformable> anTransform;

    if (mTurnOrder.empty())
    {
      for (entityx::Entity anEntity : es.entities_with_components(anAlive, anMapObject, anTransform)) {
        if (!anMapObject->destroyed)
          mTurnOrder.push(anEntity);
      }

    }
    else
    {
      bool anAction = false;
      entityx::Entity anEntity = mTurnOrder.front();
      anAlive = anEntity.component<c::Character>();
      anMapObject = anEntity.component<c::MapObject>();

      anTransform = anEntity.component<c::Transformable>();
      if (!anMapObject->destroyed)
      {
        if (anMapObject->health <= 0)
        {
          anMapObject->destroyed = true;

          anTransform->setRotation(90);

        }
        else
        {
          std::string anEntityName = anEntity.component<c::MapObject>()->name;
          sf::Vector2f anTargetPos;
          if (anAlive->behaviorFlags & BEHAVIOR_CONTROLLED)
          {
            anAction = mPlayerAction;
            if (!anAction)
            {
              while (!mKeyboardEventQueue.empty() && !mTurnOrder.empty())
              {

                if (mKeyboardEventQueue.front().theKeyCode == sf::Keyboard::Left)
                {
                  anTargetPos.x = -1.0f;
                  anTargetPos.y = 0.0f;
                  anAction = true;
                }
                else if (mKeyboardEventQueue.front().theKeyCode == sf::Keyboard::Right)
                {
                  anTargetPos.x = 1.0f;
                  anTargetPos.y = 0.0f;
                  anAction = true;
                }
                else if (mKeyboardEventQueue.front().theKeyCode == sf::Keyboard::Up)
                {
                  anTargetPos.x = 0.0f;
                  anTargetPos.y = -1.0f;
                  anAction = true;
                }
                else if (mKeyboardEventQueue.front().theKeyCode == sf::Keyboard::Down)
                {
                  anTargetPos.x = 0.0f;
                  anTargetPos.y = 1.0f;
                  anAction = true;
                }
                else if (mKeyboardEventQueue.front().theKeyCode == sf::Keyboard::A)
                {
                  if (anAlive->behaviorFlags & BEHAVIOR_AGRESSIVE)
                  {
                    mApp.mConsole.print(anEntityName + "is lowering it's gaurd.");
                    anAlive->behaviorFlags &= ~BEHAVIOR_AGRESSIVE;
                  }
                  else
                  {
                    mApp.mConsole.print(anEntityName + "is acting aggressive.");
                    anAlive->behaviorFlags |= BEHAVIOR_AGRESSIVE;
                  }
                  anAction = true;
                }
                mKeyboardEventQueue.pop();
              }
            }
            else
            {
              mPlayerAction = false;
            }
          }
          else if (anAlive->behaviorFlags & BEHAVIOR_WANDER)
          {
            GExL::Uint32 anDirection = GExL::random(0, 8);
            if (anDirection == 1)
            {
              anTargetPos.x = 0.0f;
              anTargetPos.y = -1.0f;
              anAction = true;
            }
            if (anDirection == 2)
            {
              anTargetPos.x = 1.0f;
              anTargetPos.y = 0.0f;
              anAction = true;
            }
            if (anDirection == 3)
            {
              anTargetPos.x = 0.0f;
              anTargetPos.y = 1.0f;
              anAction = true;
            }
            if (anDirection == 4)
            {
              anTargetPos.x = -1.0f;
              anTargetPos.y = 0.0f;
              anAction = true;
            }
          }
          if (anAction)
          {
            anTargetPos.x += anTransform->getPosition().x / Tilemap::TileSize;
            anTargetPos.y += anTransform->getPosition().y / Tilemap::TileSize;
            if (anAlive->behaviorFlags & BEHAVIOR_AGRESSIVE)
            {
              //mApp.mWorld.events.emit<DamageEvent>(anEntity, anTargetPos, static_cast<GExL::Uint32>(DAMAGE_MELEE), 0.0f);
            }
            mApp.mWorld.events.emit<MovementEvent>(anEntity, anTargetPos, MOVEMENT_WALK_BI);
            mTurnOrder.pop();

          }
        }
      }
      else
      {
        mTurnOrder.pop();
      }
    }
  }