Exemplo n.º 1
0
void ButtonControlLayer::onLevelTurn()
{
    for (size_t i = 0; i < 4; i++)
    {
        if (_states[i] != NONE)
        {
            auto playerCreature = _level->getPlayerCreature();
            if (playerCreature != nullptr)
            {
                if (playerCreature->canMove(static_cast<Direction>(i)))
                {
                    playerCreature->queueMove(static_cast<Direction>(i));
                }
            }
            
            if (_states[i] == PRESSED)
            {
                _states[i] = APPLIED_IN_TURN;
            }
            else if (_states[i] == RELEASED_BEFORE_TURN)
            {
                _states[i] = NONE;
            }
            
            break;
        }
    }
}
Exemplo n.º 2
0
void Player::HandleMovement( const std::vector< borka::Tile >& lstTiles )
{
	// TODO: Clean this up. Also set up speed to slow down when moving diagonally.

	sf::Vector2f queueMove( 0, 0 );
	m_lastMove.x = m_lastMove.y = 0;

	if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) || sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
	{
		queueMove.x = -m_speed;
	}
	else if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) || sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
	{
		queueMove.x = m_speed;
	}

	if ( !IsMapCollision( lstTiles, queueMove ) )
	{
		m_lastMove.x = queueMove.x;
		Move( queueMove );
	}

	queueMove = sf::Vector2f( 0, 0 );
	if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) || sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
	{
		queueMove.y = -m_speed;
	}
	else if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Down ) || sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
	{
		queueMove.y = m_speed;
	}

	if ( !IsMapCollision( lstTiles, queueMove ) )
	{
		m_lastMove.y = queueMove.y;
		Move( queueMove );
	}
}
Exemplo n.º 3
0
int main(int argc, char **argv)
{
    if(argc != 6) {
        std::cout << "wrong args" << std::endl;
        return 1;
    }
    const char *sensorDeviceName = argv[1];
    const char *actuatorDeviceName = argv[2];
    const int cameraDeviceNum = atoi(argv[3]);
    const int port = atoi(argv[4]);
    const char *mode = argv[5];

    bool showThermal = false;
    bool showRGB = false;
    if(mode[0] == 't') {
        showThermal = true;
    } else if(mode[0] == 'r') {
        showRGB = true;
    } else {
        throw "wtf";
    }

    std::cout << "blah" << std::endl;

    std::shared_ptr<ApplicationCore> core = ApplicationCore::instantiate();

    auto sc = std::make_shared<ThermalSensorController>(core, sensorDeviceName, 115200);
    auto rc = std::make_shared<RgbController>(core, cameraDeviceNum);
    auto ac = std::make_shared<ActuatorController>("/dev/tty.usbserial-A9S3VTXD");
    auto ns = std::make_shared<NetService>(core);

    sc->init();
    rc->init();
    ac->init();
    ns->init(port);

    boost::asio::deadline_timer timer(*core->getIOService());
    std::function<void(const boost::system::error_code&)> captureStuff;
    GyroReading gyroReading;
    ThermoReading thermoReading;
    captureStuff = [&](const boost::system::error_code& /*e*/) { 
        //
        cv::Vec2d pos = ac->getCurrentPosition();

        rc->captureFrame();
        auto rgbFrame = rc->popFrame();

        if(showRGB && rgbFrame->rows > 0) {
            rapidjson::Document doc;
            auto &aloc = doc.GetAllocator();
            doc.SetObject();

            cv::Mat imgMat(rgbFrame->rows, rgbFrame->cols, CV_8UC4, cv::Scalar::all(0.0));
            cv::cvtColor(*rgbFrame, imgMat, CV_BGR2RGBA, 4); 

            cv::Size size(rgbFrame->cols*0.2, rgbFrame->rows*0.2);
            cv::resize(imgMat, imgMat, size);

            std::string imgDataB64 = tobase64(imgMat.data, imgMat.total()*4*sizeof(byte));
            rapidjson::Value val;
            val.SetString(imgDataB64.c_str(), doc.GetAllocator());
            doc.AddMember("data", val, aloc);

            doc.AddMember("type", "rgb_data", aloc);
            doc.AddMember("yaw", -pos[0], aloc);
            doc.AddMember("pitch", -pos[1], aloc);
            doc.AddMember("dataWidth", imgMat.cols, aloc);
            doc.AddMember("dataHeight", imgMat.rows, aloc);
            doc.AddMember("yawSize", 63.625, aloc);
            doc.AddMember("pitchSize", 35.789, aloc);

            ns->sendWSDoc(doc);
        }


        /*if(sc->popGyroReading(gyroReading)) {
            printf("Roll: %f, Pitch: %f, Yaw: %f.\n",
                gyroReading.roll, gyroReading.pitch, gyroReading.yaw
            );
        }*/
        sc->requestThermoReading();

        std::cout << "tick: " << timer.expires_at() << std::endl;

        if(showThermal && sc->popThermoReading(thermoReading)){
            rapidjson::Document doc;
            auto &aloc = doc.GetAllocator();
            doc.SetObject();
            doc.AddMember("type", "thermo_data", aloc);
            doc.AddMember("yaw", -pos[0], aloc);
            doc.AddMember("pitch", -pos[1], aloc);

            cv::Mat imgMat(4, 16, CV_8UC4, cv::Scalar::all(0.0));
            cv::Mat mat = thermoReading.img;

            for(int i = 0; i < mat.total(); i++) {
                int y = 3-(i%4);
                int x = i/4;
                double temp = mat.at<float>(0, i);

                if(
                        (x == 11 && y == 2)
                    ||  (x == 11 && y == 3)
                    ||  (x == 12 && y == 2)
                ) {
                    temp += 10.0;
                }

                //std::cout << (int)temp << " ";

                cv::Vec4b col = hsv(
                    300-300.0*(std::max(temp, 14.0)-14.0)/(40.0-14.0),
                    1, 1
                );
                if(temp <= 11.0) {
                    col = cv::Vec4b(30, 30, 50, 255);
                } else if(temp > 40.0) {
                    col = cv::Vec4b(255, 255, 255, 255);
                }
                imgMat.at<cv::Vec4b>(y, x) = col;
                //std::cout << std::endl;
            }

            std::string imgDataB64 = tobase64(imgMat.data, imgMat.total()*4*sizeof(byte));
            rapidjson::Value val;
            val.SetString(imgDataB64.c_str(), doc.GetAllocator());
            doc.AddMember("data", val, aloc);

            ns->sendWSDoc(doc);
        }

        timer.expires_from_now(boost::posix_time::milliseconds(interval));
        timer.async_wait(captureStuff);
    };
    timer.expires_from_now(boost::posix_time::milliseconds(interval));
    timer.async_wait(captureStuff);

    ns->registerCallback("move_actuator", [&](const rapidjson::Document &doc) {
        ac->stop();
        ActuatorMoveOrder order;
        order.posDeg = cv::Vec2d(
            std::max(-150.0, std::min(150.0, -doc["yaw"  ].GetDouble()/M_PI*180)),
            std::max(- 90.0, std::min( 90.0, -doc["pitch"].GetDouble()/M_PI*180))
        );
        order.duration = 3.5;
        ac->queueMove(order);
    });
    
    std::cout << "run" << std::endl;
    core->run();
}