Exemple #1
0
void Sprite::action(int xMove, int yMove)
{
    if (iType == PLAYER)
    {
        int pullStrength = 600;

        b2Vec2 PlayerForce(pullStrength * xMove,pullStrength * yMove);
        body->ApplyForce(PlayerForce, body->GetPosition());
    }
    return;
}
Exemple #2
0
void Application::run(int _argc, char **_argv) {
    m_Window.create(zeno::VideoMode(1280, 720), "Tower Defense", zeno::WindowStyle::Titlebar | zeno::WindowStyle::Close);

    glClearColor(100.0f / 255.0f, 149.0f / 255.0f, 247.0f / 255.0f, 1.0f);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    m_Running = true;

    initialise();

    PlayerForce player = PlayerForce(10);

    sNotification.registerCreepReachedEndCallback([&](const Creep& _creep, const CreepPath& _creepPath) {
        player.removeHealth(1);
        if (player.isAlive()) {
            std::cout << "Player health: " << player.getHealth() << std::endl;
        }
        else {
            std::cout << "Player died :/" << std::endl;
        }

        return false;
    });

    std::vector<zeno::Vector2u> pathWaypoints = {
            zeno::Vector2u(0,0),
            zeno::Vector2u(4,0),
            zeno::Vector2u(4,4),
            zeno::Vector2u(8,4),
            zeno::Vector2u(12,8),
            zeno::Vector2u(16,8),
    };

    CreepPath& creepPath = sCreepPath;

    creepPath.initialiseFromPathWaypoints(pathWaypoints);

    creepPath.finalisePath();


    float frameTime;
    float timer = 0.0f;
    float accumulator = 0.0f;

    float rate = 1.0f / 60.0f;

    unsigned int updates = 0;
    unsigned int renders = 0;

    float fraction = 1.0f;

    zeno::Clock clock;

    while (m_Running)
    {
        frameTime = clock.restart().asSeconds();

        if (frameTime > 0.25f)
        {
            frameTime = 0.25f;
            std::cout << "Frame time > 0.25s, spiral of deaaaaaaaaath!" << std::endl;
        }

        accumulator += frameTime;
        timer += frameTime;

        if (timer >= 1.0f)
        {
            timer -= 1.0f;
            //std::cout << "Updates: " << updates << ", Renders: " << renders << std::endl;
            updates = 0;
            renders = 0;
        }

        while (accumulator >= rate) {
            updates += 1;

            zeno::Event event;
            while (m_Window.pollEvent(event)) {
                if (event.type == zeno::Event::EventType::WindowClosed) {
                    m_Running = false;
                }
                if (event.type == zeno::Event::EventType::KeyUp) {
                    if (event.key.key == zeno::Keyboard::Key::Space) {
                        sCreepWaveManager.addWave(new BasicCreepWave(creepPath, 0.25f, 10));
                    }
                }
                if (event.type == zeno::Event::EventType::MouseButtonReleased) {
                    if (event.mouseButton.button == zeno::Mouse::Left) {
                        zeno::Vector2i tileCoords = getTileCoordinates(zeno::Vector2f(event.mouseButton.x, m_Window.getSize().y - event.mouseButton.y));

                        player.createTower("BasicTower", tileCoords);
                    }
                }
            }

            sEntityManager.update(rate);
            sTowerManager.update(rate);
            sProjectileManager.update(rate);
            sCreepWaveManager.update(rate);

            accumulator -= rate;
        }

        float alpha = accumulator / rate;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        zeno::Mat4x4 view = zeno::Mat4x4(1.0f);//::createTranslation(zeno::Vector3f(0.0f, 0.0f, 0.0f));
        zeno::Mat4x4 proj = zeno::Mat4x4::Orthographic2D(0.0f, static_cast<float>(m_Window.getSize().x), static_cast<float>(m_Window.getSize().y), 0.0f);

        sEntityManager.render(view, proj);
        sTowerManager.render(view, proj);
        sProjectileManager.render(view, proj);
        sCreepPath.render(view, proj);

        player.render(view, proj);

        m_Window.display();
        renders += 1;
    }

    m_Window.close();
}