Esempio n. 1
0
 void update(double dt) {
     entityManagerRef->visitEntitiesWithTypeMask(componentMask, [&](Entity<EntityManagerTypes...> &entity){
         auto &aiComponent = entity.template getComponent<EnemyAIComponent>();
         
         if (aiComponent.playerId == -1) {
             int playerMask = 0;
             playerMask |= 1 << getTypeId<PlayerDetailsComponent>();
             playerMask |= 1 << getTypeId<TransformComponent>();
             auto entities = entityManagerRef->getEntityIDsWithTypeMask(playerMask);
             aiComponent.playerId = entities[0];
         }
         
         switch (aiComponent.aiType) {
             case EnemyAIComponent::AIType::DUMB:
                 updateDumbAI(dt, entity);
                 break;
             case EnemyAIComponent::AIType::FLEE:
                 updateDumbAI(dt, entity);
                 break;
             case EnemyAIComponent::AIType::FOLLOW_FIRE:
                 updateDumbAI(dt, entity);
                 break;
                 
             default:
                 break;
         }
     });
 }
 void checkOffscreenPositions() {
     entityManagerRef->visitEntitiesWithTypeMask(componentMask, [](Entity<EntityManagerTypes...> &entity){
         auto &transformComponent = entity.template getComponent<TransformComponent>();
         auto &aabbComponent = entity.template getComponent<AABBComponent>();
         auto &diesWhenOffscreenComponent = entity.template getComponent<DiesWhenOffscreenComponent>();
         
         auto halfWidth = aabbComponent.width / 2.0;
         auto halfHeight = aabbComponent.height / 2.0;
         
         auto isOffscreen = (transformComponent.x - halfWidth > SCREEN_WIDTH ||
                             transformComponent.x + halfWidth < 0 ||
                             transformComponent.y - halfHeight > SCREEN_HEIGHT ||
                             transformComponent.y + halfHeight < 0);
         
         if (isOffscreen && diesWhenOffscreenComponent.cameOnscreen) {
             // only kill if we're offscreen and have been onscreen at some point
             printf("Killing entity %zu because it went offscreen\n", entity.getId()); 
             entity.kill();
         } else if (!isOffscreen && !diesWhenOffscreenComponent.cameOnscreen) {
             diesWhenOffscreenComponent.cameOnscreen = true;
         } else if (transformComponent.x > 10000 ||
                    transformComponent.x < -1000 ||
                    transformComponent.y > 10000 ||
                    transformComponent.y < -1000) {
             // just some failsafe stuff here
             entity.kill();
         }
     });
 }
 void moveEntities(double dt) {
     entityManagerRef->visitEntitiesWithTypeMask(componentMask, [&](Entity<EntityManagerTypes...> &entity){
         auto &transformComponent = entity.template getComponent<TransformComponent>();
         
         transformComponent.x += transformComponent.dx * dt;
         transformComponent.y += transformComponent.dy * dt;
         transformComponent.z += transformComponent.dz * dt;
     });
 }
 void renderSprites() {
     glUseProgram(spriteShaderProgramId);
     entityManagerRef->visitEntitiesWithTypeMask(componentMask, [&](Entity<EntityManagerTypes...> &entity){
         auto &aabbComponent = entity.template getComponent<AABBComponent>();
         auto &transformComponent = entity.template getComponent<TransformComponent>();
         
         Eigen::Translation<GLfloat, 3> translationMat((transformComponent.x - HALF_SCREEN_WIDTH) / HALF_SCREEN_WIDTH,
                                                       (transformComponent.y - HALF_SCREEN_HEIGHT) / HALF_SCREEN_HEIGHT,
                                                       0);
         Eigen::DiagonalMatrix<GLfloat, 3> scaleMat(aabbComponent.width / SCREEN_WIDTH,
                                                    aabbComponent.height / SCREEN_HEIGHT,
                                                    1);
         
         Eigen::Transform<GLfloat, 3, Eigen::Affine> transformMatrix = translationMat * scaleMat;
         
         boundsSprite.render(transformMatrix.matrix());
     });
 }