Пример #1
0
void HelloPolycodeApp::createClientPlayer(int clientID) {
	ClientPlayer *player = new ClientPlayer();
	player->clientID = clientID;
	
	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);
	player->playerEntity = playerEntity;												
	
	player->state = 0;
	players.push_back(player);						
}
Пример #2
0
HelloPolycodeApp::HelloPolycodeApp(PolycodeView *view) {

#ifdef __APPLE__
	core = new CocoaCore(view, 640,480,false,false,0,0,90);	  
#else
	core = new SDLCore(view, 640,480,false,false,0,0,90);	  
#endif

	PhysicsScreen *screen = new PhysicsScreen(10, 50);

	ScreenShape *ceiling = new ScreenShape(ScreenShape::SHAPE_RECT, 640, 20);
	ceiling->setColor(0.0, 0.0, 0.0, 1.0);
	ceiling->setPosition(640/2, 10);
	screen->addPhysicsChild(ceiling, PhysicsScreenEntity::ENTITY_RECT, true);
	
	// Revolute Joint	
	ScreenShape *shape = new ScreenShape(ScreenShape::SHAPE_RECT, 10, 30);
	shape->setPosition(150, 20+15);
	screen->addPhysicsChild(shape, PhysicsScreenEntity::ENTITY_RECT, false);
	screen->createRevoluteJoint(shape, ceiling, 0, -15);
	screen->applyImpulse(shape, 10, 0);
	
	// Distance Joint	
	shape = new ScreenShape(ScreenShape::SHAPE_RECT, 10, 30);
	shape->setPosition(250, 20+25);
	screen->addPhysicsChild(shape, PhysicsScreenEntity::ENTITY_RECT, false);
	screen->createDistanceJoint(shape, ceiling, false);
	screen->applyImpulse(shape, 200, 0);
	
	ScreenLine *line = new ScreenLine(shape, ceiling);
	line->setColor(1.0, 0.0, 0.0, 1.0);
	screen->addChild(line);

	// Prismatic Joint	
	shape = new ScreenShape(ScreenShape::SHAPE_RECT, 10, 30);
	shape->setPosition(450, 20+25);
	screen->addPhysicsChild(shape, PhysicsScreenEntity::ENTITY_RECT, false);
	screen->createPrismaticJoint(ceiling, shape, Vector2(0,1), 0,0, false, 100, 0, true);

	ScreenEntity *lineAnchor = new ScreenEntity();
	lineAnchor->setPosition(450,10);
	line = new ScreenLine(shape, lineAnchor);
	line->setColor(0.0, 1.0, 0.0, 1.0);
	screen->addChild(line);
}
Пример #3
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;
		}
		
	}
}