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); }
ScreenEntity *ScreenEntityInstance::loadObjectEntryIntoEntity(ObjectEntry *entry) { ScreenEntity *entity = NULL; ObjectEntry *entityType = (*entry)["type"]; if(entityType) { if(entityType->stringVal == "ScreenImage") { ObjectEntry *screenImageEntry = (*entry)["ScreenImage"]; String imagePath = (*screenImageEntry)["filePath"]->stringVal; ScreenImage *image = new ScreenImage(imagePath); ObjectEntry *screenShapeEntry = (*entry)["ScreenShape"]; applyScreenShape(screenShapeEntry, image); entity = image; } if(entityType->stringVal == "ScreenParticleEmitter") { ObjectEntry *emitterEntry = (*entry)["ScreenParticleEmitter"]; ScreenParticleEmitter *placingEmitter = new ScreenParticleEmitter((*emitterEntry)["texturePath"]->stringVal, Particle::BILLBOARD_PARTICLE, ParticleEmitter::CONTINUOUS_EMITTER, (*emitterEntry)["lifespan"]->NumberVal, (*emitterEntry)["particleCount"]->NumberVal, Vector3((*emitterEntry)["dirX"]->NumberVal, (*emitterEntry)["dirY"]->NumberVal, 0.0), Vector3((*emitterEntry)["gravX"]->NumberVal, (*emitterEntry)["gravY"]->NumberVal, 0.0), Vector3((*emitterEntry)["deviationX"]->NumberVal, (*emitterEntry)["deviationY"]->NumberVal, 0.0), Vector3((*emitterEntry)["radiusX"]->NumberVal, (*emitterEntry)["radiusY"]->NumberVal, 0.0)); placingEmitter->brightnessDeviation = (*emitterEntry)["brightnessDeviation"]->NumberVal; placingEmitter->particleSize = (*emitterEntry)["particleSize"]->NumberVal; placingEmitter->perlinModSize = (*emitterEntry)["perlinModSize"]->NumberVal; placingEmitter->perlinEnabled = (*emitterEntry)["perlinEnabled"]->boolVal; placingEmitter->particleSpeedMod = (*emitterEntry)["particleSpeedMod"]->NumberVal; placingEmitter->rotationSpeed = (*emitterEntry)["rotationSpeed"]->NumberVal; placingEmitter->rotationFollowsPath = (*emitterEntry)["rotationFollowsPath"]->boolVal; placingEmitter->useScaleCurves = (*emitterEntry)["useScaleCurves"]->boolVal; placingEmitter->useColorCurves = (*emitterEntry)["useColorCurves"]->boolVal; placingEmitter->setParticleBlendingMode((*emitterEntry)["particleBlendMode"]->intVal); placingEmitter->setWidth(placingEmitter->emitterRadius.x); placingEmitter->setHeight(placingEmitter->emitterRadius.y); parseObjectIntoCurve((*emitterEntry)["scaleCurve"], &placingEmitter->scaleCurve); parseObjectIntoCurve((*emitterEntry)["colorCurveR"], &placingEmitter->colorCurveR); parseObjectIntoCurve((*emitterEntry)["colorCurveG"], &placingEmitter->colorCurveG); parseObjectIntoCurve((*emitterEntry)["colorCurveB"], &placingEmitter->colorCurveB); parseObjectIntoCurve((*emitterEntry)["colorCurveA"], &placingEmitter->colorCurveA); entity = placingEmitter; } if(entityType->stringVal == "ScreenSprite") { ObjectEntry *screenSpriteEntry = (*entry)["ScreenSprite"]; String filePath = (*screenSpriteEntry)["filePath"]->stringVal; ScreenSprite *sprite = new ScreenSprite(filePath); String animName = (*screenSpriteEntry)["anim"]->stringVal; sprite->playAnimation(animName, -1, false); ObjectEntry *screenShapeEntry = (*entry)["ScreenShape"]; applyScreenShape(screenShapeEntry, sprite); entity = sprite; } if(entityType->stringVal == "ScreenEntityInstance") { ObjectEntry *screenInstanceEntry = (*entry)["ScreenEntityInstance"]; String filePath = (*screenInstanceEntry)["filePath"]->stringVal; ScreenEntityInstance *instance = new ScreenEntityInstance(filePath); entity = instance; } if(entityType->stringVal == "ScreenShape") { ObjectEntry *screenShapeEntry = (*entry)["ScreenShape"]; ScreenShape *shape = new ScreenShape(0, 1, 1); applyScreenShape(screenShapeEntry, shape); entity = shape; } if(entityType->stringVal == "ScreenSound") { ObjectEntry *screenSoundEntry = (*entry)["ScreenSound"]; String filePath = (*screenSoundEntry)["filePath"]->stringVal; Number refDistance = (*screenSoundEntry)["refDistance"]->NumberVal; Number maxDistance = (*screenSoundEntry)["maxDistance"]->NumberVal; Number volume = (*screenSoundEntry)["volume"]->NumberVal; Number pitch = (*screenSoundEntry)["pitch"]->NumberVal; ScreenSound *sound = new ScreenSound(filePath, refDistance, maxDistance); sound->getSound()->setVolume(volume); sound->getSound()->setPitch(pitch); sound->setWidth(50); sound->setHeight(50); entity = sound; } if(entityType->stringVal == "ScreenLabel") { ObjectEntry *screenLabelEntry = (*entry)["ScreenLabel"]; String text = (*screenLabelEntry)["text"]->stringVal; String font = (*screenLabelEntry)["font"]->stringVal; int size = (*screenLabelEntry)["size"]->intVal; int aaMode = (*screenLabelEntry)["aaMode"]->intVal; ScreenLabel *label = new ScreenLabel(text, size, font, aaMode); label->positionAtBaseline = false; ObjectEntry *screenShapeEntry = (*entry)["ScreenShape"]; applyScreenShape(screenShapeEntry, label); entity = label; } } if(!entity) { entity = new ScreenEntity(); } entity->ownsChildren = true; entity->setPositionMode(ScreenEntity::POSITION_CENTER); entity->color.r = (*entry)["colorR"]->NumberVal; entity->color.g = (*entry)["colorG"]->NumberVal; entity->color.b = (*entry)["colorB"]->NumberVal; entity->color.a = (*entry)["colorA"]->NumberVal; entity->blendingMode = (*entry)["blendMode"]->intVal; entity->scale.x = (*entry)["scaleX"]->NumberVal; entity->scale.y = (*entry)["scaleY"]->NumberVal; entity->position.x = (*entry)["posX"]->NumberVal; entity->position.y = (*entry)["posY"]->NumberVal; entity->setRotation((*entry)["rotation"]->NumberVal); entity->id = (*entry)["id"]->stringVal; String tagString = (*entry)["tags"]->stringVal; if(tagString != "") { std::vector<String> tags = tagString.split(","); for(int i=0; i < tags.size(); i++) { entity->addTag(tags[i]); } } ObjectEntry *props = (*entry)["props"]; if(props) { for(int i=0; i < props->length; i++) { ObjectEntry *prop = ((*props))[i]; if(prop) { entity->setEntityProp((*prop)["name"]->stringVal, (*prop)["value"]->stringVal); } } } ObjectEntry *children = (*entry)["children"]; if(children) { for(int i=0; i < children->length; i++) { ObjectEntry *childEntry = ((*children))[i]; ScreenEntity *childEntity = loadObjectEntryIntoEntity(childEntry); entity->addChild(childEntity); } } return entity; }
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; } } }