/// Handle incoming commands from the client. void ClientCommandHandler(NetworkMessage netMsg) { switch (netMsg.msgType) { case LevelMsgType.LevelRequest: /// Player has notified of current position. Check to see if regions of interest /// need to be updated. { LevelSyncMessage m = netMsg.ReadMessage <LevelSyncMessage>(); // Debug.Log ("Got message: " + netMsg.conn.connectionId + " : "); sendPlayerUpdate(netMsg.conn.connectionId, m.playerPosition, m.visibleRadius); updatePlayerProxy(netMsg.conn.connectionId); } break; case LevelMsgType.LevelUpdate: /// Player has changed the level in a way that affects other players. { Debug.Log("Changed level"); BlockAddMessage m = netMsg.ReadMessage <BlockAddMessage>(); levelStructure.setBlock(m.px + 0.5f, m.pz + 0.5f, m.height, m.blocktype, netMsg.conn.connectionId); } break; case LevelMsgType.EmoteSingleSender: /// Receiving a single emote from a player. { SendEmoteMessageAndClientID m = netMsg.ReadMessage <SendEmoteMessageAndClientID> (); sendAllClientEmote(m.netId, m.emoteType); Debug.Log("Emote Received from Client."); } break; case LevelMsgType.ResourceUpdate: { ResourceTakeMessage m = netMsg.ReadMessage <ResourceTakeMessage> (); int height = Math.Max((int)(m.position.y), (int)WorldManager.minLevelHeight); int blockHeight = (int)(height - WorldManager.minLevelHeight); levelStructure.updateResource(m.position.x + 0.5f, m.position.z + 0.5f, blockHeight, m.amount); } break; case LevelMsgType.PlayerFlagRequest: { PlayerFlagMessage m = netMsg.ReadMessage <PlayerFlagMessage> (); int height = Math.Max((int)(m.position.y), (int)WorldManager.minLevelHeight); int blockHeight = (int)(height - WorldManager.minLevelHeight); levelStructure.updateFlag(m.position.x + 0.5f, m.position.z + 0.5f, blockHeight, netMsg.conn.connectionId); } break; default: { Debug.Log("Unexpected message type"); } break; } }
// Add a new block at the given position. The intent is to allow players to immediately // reflect actions in the local game, which may eventually be replaced when the update // is returned from the server. public void placeBlock(float x, float z, float height, int type) { // Coordinates for the region block - only horizontal elements. Vector3 llbpos = new Vector3(x, 0.0f, z); LocalLevelBlock llb = findLevelBlock(llbpos); int blockHeight = (int)(height - WorldManager.minLevelHeight); if (llb != null) { Vector3 regionpos = new Vector3(x - llb.region.blockCoordX, z - llb.region.blockCoordY, blockHeight); // llb should now be valid. // Make a local copy before signalling the change to the world. //llb.region.placeSingleBlock (localBrick, regionpos, llb.gobject.transform); } else // else no region - potential problem. { Debug.Log("No level block at " + llbpos); } Debug.Log("Add at " + x + " " + z + " " + blockHeight); BlockAddMessage m = new BlockAddMessage(); m.px = x; m.pz = z; m.height = blockHeight; m.blocktype = type; NetworkManager.singleton.client.Send(LevelMsgType.LevelUpdate, m); }