示例#1
0
void HelloPolycodeApp::handleEvent(Event *event) {

	for(int i=0; i < players.size(); i++) {
		if(event->getDispatcher() == players[i]->client) {		
			ServerClientEvent *clientEvent = (ServerClientEvent*) event;
			Player *player = players[i];
			switch(clientEvent->getEventCode()) {
				case ServerClientEvent::EVENT_CLIENT_DATA:				
					switch(clientEvent->dataType) {
						case CLIENT_READY:
						{
							ServerClientInfo info = player->getClientInfo();
							server->sendReliableDataToAllClients((char*)&info, sizeof(ServerClientInfo), PLAYER_JOIN);
						}
						break;					
						case PLAYER_SHOOT:
						{
						
						}	
						break;
						case PACKET_TYPE_CLIENT_DATA:
							ClientData *clientData = (ClientData*) clientEvent->data;
							player->turning_state = clientData->turning_state;
							player->thrusting = clientData->thrusting;
						break;
					}
				break;
			}
		}
	}

	if(event->getDispatcher() == server) {
		ServerEvent *serverEvent = (ServerEvent*) event;
		switch(serverEvent->getEventCode()) {
		
			case ServerEvent::EVENT_CLIENT_DISCONNECTED:		
			{
				for(int i=0;i<players.size();i++) {
					if(players[i]->client == serverEvent->client) {			
						Player *player = players[i];
						players.erase(players.begin()+i);						
						
						gameScreen->removeChild(player->playerEntity);
						delete player->playerEntity;
						delete player;
					}
				}
			}	
			break;
			
			case ServerEvent::EVENT_CLIENT_CONNECTED:
			{
				Player *newPlayer = new Player();
				newPlayer->position.x = 640/2;
				newPlayer->position.y = 480/2;				
				newPlayer->client = serverEvent->client;												
				newPlayer->client->addEventListener(this, ServerClientEvent::EVENT_CLIENT_DATA);					
				ScreenEntity *playerEntity = new ScreenEntity();				
				playerEntity->setPositionMode(ScreenEntity::POSITION_CENTER);
				ScreenShape *playerBody = new ScreenShape(ScreenShape::SHAPE_CIRCLE, 30,30,3);				
				playerBody->setRotation(-90.0);
				playerEntity->addChild(playerBody);
				playerEntity->setPosition(640/2,480/2);
				gameScreen->addChild(playerEntity);
				newPlayer->playerEntity = playerEntity;												
				players.push_back(newPlayer);
				printf("Player connected\n");
				serverInfo.initialServerState = serverState;
				server->sendReliableDataToClient(newPlayer->client, (char*) &serverInfo, sizeof(ServerInfo), SERVER_INFO);
			}
			break;
		}
		
	}
}
void PolycodeRemoteDebugger::handleEvent(Event *event) {

    for(int i=0; i < debuggerClients.size(); i++) {
        if(event->getDispatcher() == debuggerClients[i]->client) {
            ServerClientEvent *clientEvent = (ServerClientEvent*) event;
            DebuggerClient *client = debuggerClients[i];
            switch(clientEvent->getEventCode()) {
            case ServerClientEvent::EVENT_CLIENT_DATA:
                switch(clientEvent->dataType) {
                case EVENT_DEBUG_PRINT:
                {
                    String printStr = String(clientEvent->data);
                    PolycodeConsole::print(printStr);
                    PolycodeConsole::print("\n");
                }
                break;
                case EVENT_DEBUG_ERROR:
                {

                    if(!hasErred) {
                        RemoteErrorData *data = (RemoteErrorData*)clientEvent->data;
                        PolycodeConsole::print("Error in file "+String(data->fileName)+" on line "+String::IntToString(data->lineNumber)+"\n");
                        PolycodeConsole::print(String(data->errorMessage)+"\n");
                        PolycodeConsole::print("Backtrace:\n");

                        CoreServices::getInstance()->getCore()->makeApplicationMain();
                    }

//							hasErred = true;

                }
                break;
                case EVENT_DEBUG_BACKTRACE_INFO:
                {

                    RemoteBacktraceData *data = (RemoteBacktraceData*)clientEvent->data;
                    PolycodeConsole::print("In file "+String(data->fileName)+" on line "+String::IntToString(data->lineNumber)+"\n");

                    PolycodeConsole::addBacktrace(String(data->fileName), data->lineNumber, projectManager->getActiveProject());

                }
                break;

                }
                break;
            }
        }
    }


    if(event->getDispatcher() == server) {
        ServerEvent *serverEvent = (ServerEvent*) event;
        switch(serverEvent->getEventCode()) {

        case ServerEvent::EVENT_CLIENT_DISCONNECTED:
        {
            for(int i=0; i<debuggerClients.size(); i++) {
                if(debuggerClients[i]->client == serverEvent->client) {
                    DebuggerClient *client = debuggerClients[i];
                    debuggerClients.erase(debuggerClients.begin()+i);
                    delete client;
                    PolycodeConsole::print("Remote debugger client disconnected...\n");
                }
            }
        }
        break;

        case ServerEvent::EVENT_CLIENT_CONNECTED:
        {
            DebuggerClient *newClient = new DebuggerClient();
            newClient->client = serverEvent->client;
            newClient->client->addEventListener(this, ServerClientEvent::EVENT_CLIENT_DATA);
            PolycodeConsole::print("Remote debugger client connected...\n");
            printf("CLIENT CONNECTED\n");
            debuggerClients.push_back(newClient);
        }
        break;
        }

    }

}