Esempio n. 1
0
bool Segment::operator==(const Segment& other) const {
	return true
		 && _isAliveSet(mgen::SHALLOW) == other._isAliveSet(mgen::SHALLOW)
		 && _isPosSet(mgen::SHALLOW) == other._isPosSet(mgen::SHALLOW)
		 && getAlive() == other.getAlive()
		 && getPos() == other.getPos();
}
Esempio n. 2
0
//--------------------------------------------------------------
void BCSplitBall::update(const float fDelta)
{
	if(!_bStart)
	{
		return;
	}

	list<SplitBall> newSplitBallList_;
	auto Iter_ = _SplitBallList.begin();
	while(Iter_ != _SplitBallList.end())
	{
		Iter_->update(fDelta);
		if(Iter_->getAlive())
		{
			if(Iter_->getSplit())
			{
				ofVec2f newDir_ = Iter_->vel.normalized().rotate(ofRandom(-5, 5));
				SplitBall newSplitBall_(Iter_->pos, newDir_, _SplitBallList.size());
				newSplitBallList_.push_back(newSplitBall_);
			}
			Iter_++;
		}
		else
		{
			Iter_ = _SplitBallList.erase(Iter_);
		}
	}

	_SplitBallList.splice(_SplitBallList.end(), newSplitBallList_);
}
Esempio n. 3
0
//更新道具位置,对于已经失效的道具从道具管理列表中移除
void ItemManager::updateItemPosition(float dt)
{
    cocos2d::Vector<Item *> toRemoveItemArray;
    
    if (!mItemArray.empty()) {
        for (int i = 0; i < mItemArray.size(); i++) {
            auto pItem = mItemArray.at(i);
            pItem->update(dt);
            if (!pItem->getAlive()) {
                toRemoveItemArray.pushBack(pItem);
            }
        }
    }
    
    for (int j = 0; j < toRemoveItemArray.size(); j++) {
        auto pToRemoveItem = toRemoveItemArray.at(j);
        removeItem(pToRemoveItem);
    }
}
Esempio n. 4
0
int RMessagePassing::thread_func(void *arg)
{
    // accept connection forever
    while( getAlive() ) {
        RSocket *new_socket = new RSocket;

        // accept new connection
        if( 0 != m_socket.accept(*new_socket) ) {
            dbg_pe("Accept new connection failed!");
            delete new_socket;
            break;
        }

        // create new receiving thread
        RMP_SocketThread *new_thread;
        new_thread = new RMP_SocketThread(new_socket, this);
        new_thread->setNodeMe(m_nodeMe);

        // start receiving thread
        new_thread->start();
    }

    return 0;
}
Esempio n. 5
0
int main(void){

	_delay_ms(100);

	USB_ConfigureClock();
	PORTR.DIRSET = 1 << 1;
	USB_Init();
	
	// Enable USB interrupts
	USB.INTCTRLA = USB_BUSEVIE_bm | USB_INTLVL_MED_gc;
	USB.INTCTRLB = USB_TRNIE_bm | USB_SETUPIE_bm;

	PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm;
	sei(); 

	// setup TWI bus for master-mode I2C comms
	TWIC.MASTER.BAUD = TWI_BAUD;
	TWIC.MASTER.CTRLA = TWI_MASTER_ENABLE_bm;  
	TWIC.MASTER.STATUS = TWI_MASTER_BUSSTATE_IDLE_gc;

	// setup TCC0 for sample timing
	TCC0.CTRLA = TC_CLKSEL_DIV256_gc;
	TCC0.CTRLB = TC0_CCAEN_bm | TC_WGMODE_SINGLESLOPE_gc;
	TCC0.INTCTRLB = TC_CCAINTLVL_LO_gc;
	TCC0.CCA = 120; 
	TCC0.PER = 0;

	// config PORTE for serial transmission
	PORTE.DIRSET = 1 << 3;
	USARTE0.BAUDCTRLA = 0x01;
	USARTE0.CTRLC =  USART_PMODE_EVEN_gc | USART_CHSIZE_8BIT_gc;
	USARTE0.CTRLB = USART_TXEN_bm | USART_CLK2X_bm;

	// configure general DMA settings
	DMA.CTRL = DMA_ENABLE_bm | DMA_DBUFMODE_DISABLED_gc | DMA_PRIMODE_RR0123_gc;

	// use DMA CH0 for transmitting data after a frame snapshot

	// reload SRC address register every transaction
	// increment SRC every packet
	// don't reload the destination address
	// the destination address is fixed
	DMA.CH0.ADDRCTRL = DMA_CH_SRCRELOAD_TRANSACTION_gc | DMA_CH_SRCDIR_INC_gc | DMA_CH_DESTRELOAD_NONE_gc | DMA_CH_DESTDIR_FIXED_gc;
//	DMA.CH1.ADDRCTRL = DMA_CH_SRCRELOAD_NONE_gc | DMA_CH_SRCDIR_FIXED_gc | DMA_CH_DESTRELOAD_TRANSACTION_gc | DMA_CH_DESTDIR_INC_gc;
	// trigger DMA transfer on data ready event - USARTE0.DATA is ready to get another byte
	DMA.CH0.TRIGSRC = DMA_CH_TRIGSRC_USARTE0_DRE_gc;
//	DMA.CH1.TRIGSRC = DMA_CH_TRIGSRC_USARTE0_RXC_gc;
	// eww.
	DMA.CH0.SRCADDR0 = ((uint32_t)(&sensorData) >> (8*0)) & 0xFF;
	DMA.CH0.SRCADDR1 = ((uint32_t)(&sensorData) >> (8*1)) & 0xFF;
	DMA.CH0.SRCADDR2 = ((uint32_t)(&sensorData) >> (8*2)) & 0xFF;
	DMA.CH0.DESTADDR0 = ((uint32_t)(&USARTE0.DATA) >> (8*0)) & 0xFF;
	DMA.CH0.DESTADDR1 = ((uint32_t)(&USARTE0.DATA) >> (8*1)) & 0xFF;
	DMA.CH0.DESTADDR2 = ((uint32_t)(&USARTE0.DATA) >> (8*2)) & 0xFF;
	// enable CH0, set to one byte bursts
	DMA.CH0.CTRLA = DMA_CH_ENABLE_bm | DMA_CH_SINGLE_bm | DMA_CH_BURSTLEN_1BYTE_gc; 

	getAlive();
	getCalibrationData();

	PORTR.OUTSET = 1 << 1;
	for (;;){}
}
Esempio n. 6
0
/** Event handler for the library USB Control Request reception event. */
bool EVENT_USB_Device_ControlRequest(USB_Request_Header_t* req){
	// zero out ep0_buf_in
	for (uint8_t i = 0; i < 64; i++) ep0_buf_in[i] = 0;
	usb_cmd = 0;
	if ((req->bmRequestType & CONTROL_REQTYPE_TYPE) == REQTYPE_VENDOR){
		switch(req->bRequest){
			case 0x00: // Info

				if (req->wIndex == 0){
					USB_ep0_send_progmem((uint8_t*)hwversion, sizeof(hwversion));
				}else if (req->wIndex == 1){
					USB_ep0_send_progmem((uint8_t*)fwversion, sizeof(fwversion));
				}
				
				return true;

			// bother a specified I2C address, return '1' if address ACKs, '0' if NACK
			// mnemonic - 0xBotherAddress
			case 0xBA: 
				ep0_buf_in[0] = botherAddress(req->wIndex, req->wValue);
				USB_ep0_send(1);
				return true;

			// start sampling
			// mnemonic - 0xConfigure7imer
			case 0xC7:
				usb_pipe_reset(&ep_in);
				if (req->wIndex != 0) {
					startConversion();
					ep0_buf_in[0] = 1;
					timeout_or_sampling_no_longer_enabled = 0;
					TCC0.CCA = req->wValue;
					TCC0.PER = 1 << 15;
				}
				else {
					TCC0.PER = 0;
					ep0_buf_in[0] = 0;
					timeout_or_sampling_no_longer_enabled = 1;
				}
				TCC0.CNT = 0;
				USB_ep0_send(1);
				return true;

			// return a bitmap of alive cells 
			// mnemonic - 0x5Can
			case 0x5C: 
				getAlive();
				_delay_ms(5);
				for (uint8_t i = 0; i < 8; i++) {ep0_buf_in[i] = bitmap[i];}
				USB_ep0_send(8);
				return true;

			// return calibration information
			// mnemonic - 0x6etCalibration
			case 0x6C: {
				getCalibrationData();
				_delay_ms(5);
				uint8_t offset = 40*req->wIndex+8*req->wValue;
				for (uint8_t i = 0; i < 8; i++) {ep0_buf_in[i] = calibrationData[offset+i];}
				USB_ep0_send(8);
				return true;
				}

			// disconnect from USB, jump to bootloader	
			case 0xBB: 
				USB_enter_bootloader();
				return true;
		}
	}
	return false;
}
Esempio n. 7
0
//--------------------------------------------------------------
bool SplitBall::getSplit()
{	
	bool bResult_ = canSplit && getAlive();
	canSplit = false;
	return bResult_;
}
Esempio n. 8
0
// Other methods
void Particle::update(){ // main method that controls all necessary movement
    // attraction and repulsion
    float dy = (ofGetWindowHeight() - ofGetMouseY()) - getY();
    float dx = ofGetMouseX() - getX();
    if (getAttract() && !getRepel()){
        float desired = atan2(dy,dx);
        float angdiff = desired-getAngle();
        if ((angdiff > 0 && abs(angdiff) <= PI) || (angdiff < 0 && abs(angdiff) > PI)){
            setAngle(getAngle() + getAlpha());
        }
        else if ((angdiff > 0 && abs(angdiff)>PI) || (angdiff < 0 && abs(angdiff) <= PI)){
            setAngle(getAngle() - getAlpha());
        }
//        if (desired+getAlpha()>getAngle() && desired-getAlpha()<getAngle()){
//            setSpeed(getSpeed()+getFlow());
//        }
//        else {
//            if (getSpeed()>ORBITAL_FACTOR*getFlow()){
//                setSpeed(getSpeed()-getFlow());
//            }
//        }
    }
    else if (getRepel() && !getAttract() && sqrt(pow(dy,2) + pow(dx,2)) <= BARRIER){
        float desired = atan2(dy,dx) > 0 ? atan2(dy,dx) - PI : atan2(dy,dx) + PI;
        float angdiff = desired - getAngle();
        if ((angdiff > 0 && abs(angdiff) <= PI) || (angdiff < 0 && abs(angdiff) > PI)){
            setAngle(getAngle() + getAlpha());
        }
        else if ((angdiff > 0 && abs(angdiff) > PI) || (angdiff < 0 && abs(angdiff) <= PI)){
            setAngle(getAngle() - getAlpha());
        }
//        if (desired+getAlpha()>getAngle() && desired-getAlpha()<getAngle()){
//            setSpeed(getSpeed()+getFlow());
//        }
//        else {
//            if (getSpeed()>ORBITAL_FACTOR*getFlow()){
//                setSpeed(getSpeed()-getFlow());
//            }
//        }
    }
    else if (getAttract() && getRepel()){
        if (getSpeed() > 0){
            setSpeed(getSpeed() - getFlow());
        }
    }
    // life
    if (getAlive()){
        setAngle(getAngle() + ofRandomf() * getAlpha());
        setSpeed(getSpeed() + ofRandomf() * getFlow());
        setRadius(getRadius() + ofRandomf() * RADIUS_NOISE);
    }
    // bounds
    if (getX() > ofGetWindowWidth() - getRadius() || getX() < getRadius()){
        setAngle(-getAngle() + PI);
    }
    if (getY() > ofGetWindowHeight() - getRadius() || getY() < getRadius()){
        setAngle(-getAngle());
    }
    // position + motion
    position += velocity;
    // color
    float r = getAttract() && getRepel() ? getColor().r + 1 : (getSpeed()/MAX_SPEED) * 255;
    float g = getAttract() ? getColor().g + 1 : (getY()/ofGetWindowHeight()) * 255;
    float b = getRepel() ? getColor().b + 1 : (1-getY()/ofGetWindowHeight()) * 255;
    setColor(r,g,b);
}
Esempio n. 9
0
int RMP_SocketThread::thread_func(void *arg)
{
    int             ret;

    std::string     nodeName;
    RDataStream     ds;
//    Scommand&       scommand = Scommand::instance();

    ru8             *buf;
    ru32            buf_len, msg_header_len;
    ru32            msg_header, msg_magic, msg_ver, msgType, msgID, 
                    data_len;


    // allocate buffer
    buf_len = 4096;
    buf = new ru8[buf_len];

    msg_header_len = 4*sizeof(ru32);

    // receive node name
    ret = m_socketRecv->recv(ds);
    if( ret == 0 ) {
        if( ds.read(nodeName) != 0 ) setAlive(0);
    } else {
        setAlive(0);
    }

    // receiving message forever
    while( getAlive() ) {
        // receive message header
        ret = m_socketRecv->recv_until(buf, msg_header_len);
        if( ret < 0 ) break;

        if( ret < msg_header_len ) {
            dbg_pw("Read message header error! only read %d/%d bytes\n",
                   ret, msg_header_len);
            break;
            //continue;
        }

        // get message header
        memcpy(&msg_header, buf + 0*sizeof(ru32), sizeof(ru32));
        memcpy(&msgType,    buf + 1*sizeof(ru32), sizeof(ru32));
        memcpy(&msgID,      buf + 2*sizeof(ru32), sizeof(ru32));
        memcpy(&data_len,   buf + 3*sizeof(ru32), sizeof(ru32));
        
        msg_magic = msg_header >> 16;
        msg_ver   = msg_header & 0xFFFF;

    
        if( msg_magic != RTK_MAGIC_MESSAGE ) {
            dbg_pw("Magic number is wrong: 0x%X (correct is: 0x%X)\n",
                   msg_magic, RTK_MAGIC_MESSAGE);
            continue;
        }

        // get message length & alloc buffer
        if( buf_len < data_len ) {
            delete [] buf;

            buf_len = data_len + 256;
            buf = new ru8[buf_len];
        }

        // read message data
        ret = m_socketRecv->recv_until(buf, data_len);
        if( ret < 0 ) break;
        if( ret < data_len ) {
            dbg_pw("Read message content error! Only read %d/%d bytes\n",
                   ret, data_len);
            break;
            continue;
        }

        // parse the datastream & process it
        RMessage *msg = new RMessage;
        msg->msgType = msgType;
        msg->msgID   = msgID;
        msg->data.fromRawData(buf, data_len);
        msg->data.seek(0);


        // process special messages
        if( msg->msgType == 0xFFFFFFF0 ) {
            // node add message
            RMP_Node *n  = new RMP_Node,
                     *n2 = NULL;

            ret = n->fromStream(msg->data);
            n->mpMe = m_mp;

            n2 = m_mp->getNodeMap()->get(n->nodeName);
            if( n2 == NULL ) {
                m_nodeOther = n;

                n->socketThread = this;
                if( m_nodeMe->sameProcess(*n) ) n->isSameProcess = 1;

                m_mp->getNodeMap()->insert(n);
            } else {
                m_nodeOther = n2;

                *n2 = *n;
                n2->socketThread = this;
                n2->mpMe = m_mp;
                if( m_nodeMe->sameProcess(*n2) ) n2->isSameProcess = 1;

                delete n;
            }

            delete msg;
            continue;
        } else if (msg->msgType == 0xFFFFFFF1 ) {
            // update node map message
            RMP_Node    *pn;
            int         inode = 0;

            while(1) {
                RMP_Node *n = new RMP_Node;
                if( 0 != n->fromStream(msg->data) ) break;

                /*
                printf("update node [%3d] ", inode++);
                n->print();
                */

                pn = m_mp->getNodeMap()->get(n->nodeName);
                if( pn == NULL ) {
                    if( m_nodeMe->sameProcess(*n) ) n->isSameProcess = 1;
                    n->mpMe = m_mp;

                    m_mp->getNodeMap()->insert(n, 0);
                } else {
                    *pn = *n;

                    if( m_nodeMe->sameProcess(*pn) ) pn->isSameProcess = 1;
                    pn->mpMe = m_mp;

                    delete n;
                }
            }

            // update pair-wise node
            pn = m_mp->getNodeMap()->get(nodeName);
            m_nodeOther = pn;

            // delete temp obj
            delete msg;
            continue;
        } else if( msg->msgType == 0xFFFFFFF2 ) {
            string command;

            msg->data.rewind();
            msg->data.read(command);
            scommand.Call(command);

            // delete temp obj
            delete msg;
            continue;
        }

        // process other message
        if( m_nodeOther != NULL ) msg->nodeSrc = *m_nodeOther;
        if( m_nodeMe != NULL)     msg->nodeDst = *m_nodeMe;
        m_mp->recvMessageSlot(msg);
    }

    // release all resources
    delete [] buf;
    buf = NULL;

    // remove node from node map
    dbg_pt("remove node: m_nodeOther->nodeName = %s\n", m_nodeOther->nodeName.c_str());
    m_mp->getNodeMap()->erase(m_nodeOther->nodeName);

    release();

    return 0;
}
Esempio n. 10
0
void GameState::deleteInactives() {
    //we need to be careful about invalid iterators
    //if we erase an iterator; it is invalidated.
    //the iterators around it are not.

    //iterate over graphical entities
    {
        auto it = mGraphicalEntities.begin();
        auto end = mGraphicalEntities.end();

        //iterator to next element
        auto nextIt = it;

        while(it != end) {
            ++nextIt;

            auto graphical_sp = *it;

            if(graphical_sp->getAlive() == false) {
                //'it' is invalidated
                mGraphicalEntities.erase(it);
            }
            //nextIt is not invalidated either way
            it = nextIt;
        }
    }

    //iterate over collision zones
    {
        auto it = mCollisionZones.begin();
        auto end = mCollisionZones.end();

        auto nextIt = it;

        while(it != end) {
            ++nextIt;

            auto colZone_sp = *it;

            if(colZone_sp->getAlive() == false) {
                mCollisionZones.erase(it);
            }

            it = nextIt;
        }
    }

    //iterare over scripts
    {
        auto it = mScripts.begin();
        auto end = mScripts.end();

        auto nextIt = it;

        while(it != end) {
            ++nextIt;

            Script* script_p = it->get();

            if(script_p->getAlive() == false) {
                mScripts.erase(it);
            }

            it = nextIt;
        }
    }
}