Beispiel #1
0
void Level::loadExit(MvUint tileX, MvUint tileY)
{
    LOGME_CALL("Level::loadExit");
    LOGME_DEBUG("Exit at %d, %d", tileX, tileY);

    auto e = mEcsContext.e();
    auto c = mEcsContext.c();
    EngineContext& ctx = mEngineContext;

    MvAnimation::Ptr aSpawn(new MvAnimation(RepeatMode::Loop));

    for (int i = 0; i < 3; ++i) {
        StaticDrawable::Ptr ptr(new StaticDrawable(ctx, Tex::Blob, BlueSpawn[i]));
        aSpawn->addDrawable(std::move(ptr));
        aSpawn->addFrame(i, sf::milliseconds(240));
    }

    Entity eSpawn = createEntity();
    CrawlComponents& cSpawn = c->attach<Transform, Renderable, Sensor>(eSpawn);

    Renderable* cRenderable = cSpawn.get<Renderable>();
    Sensor*     cSensor     = cSpawn.get<Sensor>();
    Transform*  cTransform  = cSpawn.get<Transform>();

    cRenderable->setLayer(def::layer::BackgroundNear);
    cRenderable->setDrawable(std::move(aSpawn));
    cTransform->setPosition(tileX * TW, tileY * TH);
    cSensor->setShapeCircle(2.f, sf::Vector2f(tileX * TW, tileY * TH));
    cSensor->setCollisionCategory(Collision::World);
    cSensor->setCollisionWith(Collision::Player);
    cSensor->setCategory(Category::ExitSensor);

    mEcsContext.s()->get<PhysicsSystem>()->initialize(eSpawn);
}
Beispiel #2
0
void Level::receive(const BulletSpawn &entity)
{
    LOGME_CALL("Level::receive<BulletSpawn>");

    Transform* cTransform = mEcsContext.c()->get(entity.spawner).get<Transform>();
    LOGME_ASSERT(nullptr != cTransform, "Transform component is required");

    sf::Vector2f pos = cTransform->getPosition();

    const int bulletNum = 12;
    for (int i = 0; i < bulletNum; ++i)
    {
        float fi = i * 2 * M_PI / bulletNum;

        Entity e = createEntity();
        CrawlComponents& d = mEcsContext.c()->attach<Transform, RigidBody, Renderable, Kinematic>(e);

        Kinematic*  cKinematic  = d.get<Kinematic>();
        Renderable* cRenderable = d.get<Renderable>();
        RigidBody*  cRigidBody  = d.get<RigidBody>();
        Transform*  cTransform  = d.get<Transform>();

        sf::Vector2f v(1.0f * cos(fi), 1.0f * sin(fi));
        cKinematic->setLinearVelocity(120.f * v);
        cTransform->setPosition(pos);

        StaticDrawable::Ptr dBullet(new StaticDrawable(mEngineContext, Tex::Blob,
            sf::IntRect(14 * 16, 16, 5, 4)));
        cRenderable->setDrawable(std::move(dBullet));

        cRigidBody->setBodyDynamic();
        cRigidBody->setBulletMode(true);
        cRigidBody->setCollisionCategory(Collision::Bullet);
        cRigidBody->setCollisionWith(Collision::Player);
        cRigidBody->setCollisionWith(Collision::World);
        cRigidBody->setShapeBox(5, 4);

        mEcsContext.s()->get<PhysicsSystem>()->initialize(e);
    }
}
Beispiel #3
0
void Level::loadLevel(MvId level)
{
    LOGME_CALL("Level::loadLevel");
    LOGME_INFO("Loading level %d", level);

    reset();
//    loadWorldBounds();

    // Level image is 4x4
    const MvUint col = level % 4;
    const MvUint row = level / 4;

    const MvUint x0 = col * WorldTileWidth;
    const MvUint y0 = row * WorldTileHeight;

    const MvUint W = WorldTileWidth;
    const MvUint H = WorldTileHeight;

    for (MvUint j = 0; j < H; ++j) {
        for (MvUint i = 0; i < W; ++i) {
            handleColor(i, j, mData.getPixel(x0 + i, y0 + j));
        }
    }

    auto c = mEcsContext.c();

    Entity world = createEntity();
    CrawlComponents& cWorld = c->attach<Renderable>(world);

    Renderable* cRenderable = cWorld.get<Renderable>();

    MvDrawable* tilemap = mTileMap.get();
    tilemap->setPosition(-8, -8);

    cRenderable->setDrawable(std::move(mTileMap));
    cRenderable->setLayer(def::layer::BackgroundNear);

    LOGME_INFO("Level loaded. Created entities: %d, to destroy: %d",
               mCreatedEntities.size(), mDeadEntities.size());
}
Beispiel #4
0
void Level::loadPlayerSpawner(MvUint tileX, MvUint tileY)
{
    LOGME_CALL("Level::loadPlayerSpawner");
    LOGME_DEBUG("Player at %d, %d", tileX, tileY);
    auto c = mEcsContext.c();
    EngineContext& ctx = mEngineContext;

    MvAnimation::Ptr aSpawn(new MvAnimation(RepeatMode::Loop));
    {
        for (int i = 0; i < 3; ++i) {
            StaticDrawable::Ptr ptr(new StaticDrawable(ctx, Tex::Blob, GreenSpawn[i]));
            aSpawn->addDrawable(std::move(ptr));
            aSpawn->addFrame(i, sf::milliseconds(240));
        }

        mve::Entity eSpawn = createEntity();
        CrawlComponents& cSpawn = c->attach<Transform, Renderable>(eSpawn);

        Renderable* cRenderable = cSpawn.get<Renderable>();
        Transform*  cTransform  = cSpawn.get<Transform>();

        cRenderable->setLayer(def::layer::BackgroundNear);
        cRenderable->setDrawable(std::move(aSpawn));
        cTransform->setPosition(tileX * TW, tileY * TH - 6);
    }

    // Initialize the player
    DirectionalDrawable::Ptr dPlayer(new DirectionalDrawable);

    for (std::size_t i = 0; i < Direction::Count; ++i) {
        MvAnimation::Ptr aIdle(new MvAnimation(RepeatMode::Loop));
        MvAnimation::Ptr aMove(new MvAnimation(RepeatMode::Loop));

        StaticDrawable::Ptr ptr;
        for (std::size_t j = 0; j < AnimIdleSize; ++j) {
            ptr.reset(new StaticDrawable(ctx, Tex::Blob, PlayerIdle[i * AnimIdleSize + j]));
            aIdle->addDrawable(std::move(ptr));
            aIdle->addFrame(j, AnimIdleSpeed);
        }

        for (std::size_t j = 0; j < AnimMoveSize; ++j) {
            ptr.reset(new StaticDrawable(ctx, Tex::Blob, PlayerMove[i * AnimMoveSize + j]));
            aMove->addDrawable(std::move(ptr));
            aMove->addFrame(j, AnimMoveSpeed);
        }

        dPlayer->setDrawable(CharState::Idle, i, std::move(aIdle));
        dPlayer->setDrawable(CharState::Move, i, std::move(aMove));
    }

    mPlayerEntity = createEntity();
    CrawlComponents& cPlayer = c->attach<Transform, Kinematic, Renderable, State,
            RigidBody, Attributes>(mPlayerEntity);

    Attributes* cAttributes     = cPlayer.get<Attributes>();
    Kinematic*  cKinematic      = cPlayer.get<Kinematic>();
    Renderable* cRenderable     = cPlayer.get<Renderable>();
    RigidBody*  cRigidBody      = cPlayer.get<RigidBody>();
    State*      cState          = cPlayer.get<State>();
    Transform*  cTransform      = cPlayer.get<Transform>();

    cAttributes->baseSpeed = 150.0f; // px/s
    cKinematic->setDirection(Direction::Down);
    cRenderable->setDrawable(std::move(dPlayer), DrawableType::Directional);
    cRenderable->setLayer(def::layer::ForegroundMiddle);
    cRigidBody->disableRotation();
    cRigidBody->setBodyDynamic();
    cRigidBody->setCollisionCategory(Collision::Player);
    cRigidBody->setCollisionWith(Collision::Bullet);
    cRigidBody->setCollisionWith(Collision::Enemy);
    cRigidBody->setCollisionWith(Collision::World);
    cRigidBody->setShapeBox(10, 10, sf::Vector2f(0, 1));
    cState->setState(CharState::Idle);
    cTransform->setPosition(tileX * TW, tileY * TH);

    mPhysics->initialize(mPlayerEntity);
}
Beispiel #5
0
void Level::loadEnemySpawner(MvUint tileX, MvUint tileY, MvUint feature)
{
    LOGME_CALL("Level::loadEnemySpawner");
    LOGME_DEBUG("Spawning enemy at %d, %d", tileX * TW, tileY * TH);
    auto e = mEcsContext.e();
    auto c = mEcsContext.c();

    EngineContext& ctx = mEngineContext;

    sf::Time frames[] = {
        sf::milliseconds(3600 + randOf(-480, 480)),
        sf::milliseconds(1200),
        sf::milliseconds(120),
        sf::milliseconds(1200)
    };

    MvAnimation::Ptr dEnemy(new MvAnimation(RepeatMode::Loop));
    StaticDrawable::Ptr ptr;

    ptr.reset(new StaticDrawable(ctx, Tex::Blob, EnemyFrames[1]));
    dEnemy->addDrawable(std::move(ptr));

    ptr.reset(new StaticDrawable(ctx, Tex::Blob, EnemyFrames[0]));
    dEnemy->addDrawable(std::move(ptr));

    dEnemy->addFrame(0, frames[0]);
    dEnemy->addFrame(1, frames[1]);
    dEnemy->addFrame(0, frames[2]);
    dEnemy->addFrame(1, frames[3]);

    Entity enemy = createEntity();
    CrawlComponents& cEnemy = c->attach<Attributes, Behavior, Renderable,
                                        RigidBody, Sensor, Transform>(enemy);

    Attributes* cAttributes = cEnemy.get<Attributes>();
    Behavior*   cBehavior   = cEnemy.get<Behavior>();
    Renderable* cRenderable = cEnemy.get<Renderable>();
    RigidBody*  cEnemyBody  = cEnemy.get<RigidBody>();
    Sensor*     cSensor     = cEnemy.get<Sensor>();
    Transform*  cTransform  = cEnemy.get<Transform>();

    cAttributes->baseSpeed = 50.0f;
    cAttributes->visionRange = 50.0f;
    if (feature & 0xE0) {
        cAttributes->canShootBullets = true;
        cAttributes->hasShootBullets = false;
    }

    // Values working with animation frames
    cBehavior->addAction(Behavior::Sleeping,   frames[0]);
    cBehavior->addAction(Behavior::Gazing,     frames[1]);
    cBehavior->addAction(Behavior::Sleeping,   frames[2]);
    cBehavior->addAction(Behavior::Gazing,     frames[3]);

    cTransform->setPosition(tileX * TW, tileY * TH);
    cRenderable->setDrawable(std::move(dEnemy), mve::DrawableType::Default);
    cRenderable->setLayer(def::layer::ForegroundMiddle);

    cSensor->setCategory(Category::EnemySensor);
    cSensor->setCollisionCategory(Collision::Enemy);
    cSensor->setCollisionWith(Collision::Player);
    cSensor->setShapeCircle(cAttributes->visionRange);
    cSensor->setAttachToEntity();

    cEnemyBody->enableCollision();
    cEnemyBody->disableRotation();
    cEnemyBody->setBodyKinematic();
    cEnemyBody->setShapeBox(9, 10);
    cEnemyBody->setDensity(10.f);
    cEnemyBody->setCollisionCategory(Collision::Enemy);
    cEnemyBody->setCollisionWith(Collision::Player | Collision::World);

    mEcsContext.s()->get<PhysicsSystem>()->initialize(enemy);
}