public void Spawn(Network.SpawnerPayload payload) { GameObject generatedObject = Instantiate( this.AvailablePrefabs[payload.assetId], payload.position, Quaternion.Euler(0, 0, payload.rotation)); generatedObject.transform.localScale *= payload.scaleRatio; }
private void SpawnOnPayloadReception(Network.SpawnerPayload assetPayload) { switch (assetPayload.spawnerType) { case ObjectManagement.SpawnerType.Planet: this.PlanetSpawner.Spawn(assetPayload); break; case ObjectManagement.SpawnerType.Collectible: this.CollectibleSpawner.Spawn(assetPayload); break; case ObjectManagement.SpawnerType.Decor: this.DecorSpawner.Spawn(assetPayload); break; default: break; } }
private void Update() { this.TimeSinceLastPositionSending += Time.deltaTime; // Instantiate the local player if not already done. if (this.LocalPlayerSpawner.PlayerObject == null && this.PlanetSpawner.PlayerSpawningPlanet != null) { this.LocalPlayerSpawner.InstantiatePlayer(this); // Send the player position to the client if in a multiplayer game. if (this.Connection != null) { this.SendLocalPlayerPosition(); } } // If the user is the Host, or plays a solo game. if (this.Connection == null || (this.Connection != null && this.IsHost)) { // Spawn items. foreach (ObjectManagement.Spawner spawner in this.Spawners) { if (spawner.ShouldSpawn()) { Network.SpawnerPayload assetPayload = spawner.GetNextAssetPayload(); spawner.Spawn(assetPayload); // In a multiplayer game... if (this.Connection != null) { // ...send the item data to the Client. this.Connection.Write(assetPayload); } spawner.PrepareNextAsset(); } } } // In a multiplayer game... if (this.Connection != null) { // ...listen to payloads coming from the other player. Network.Payload payload = this.Connection.Read(); if (payload != null) { switch (((Network.BasePayload)payload).Code) { case Network.OpCode.PlayerCoordinates: if (this.RemotePlayerSpawner.PlayerObject == null) { this.RemotePlayerSpawner.InstantiatePlayer(((Network.PlayerCoordinates)payload).coordinates2D); } else { Physic.Coordinates2D coordinates = ((Network.PlayerCoordinates)payload).coordinates2D; this.RemotePlayerSpawner.coordinates2D = coordinates; } break; case Network.OpCode.Spawn: if (!this.IsHost) { this.SpawnOnPayloadReception(payload as Network.SpawnerPayload); } break; case Network.OpCode.Death: // Come back to menu if the other died. this.GameOver(true); break; default: break; } } // ...and send local player position to the other player if (this.TimeSinceLastPositionSending >= 1 / this.PositionSendingFrequency) { this.SendLocalPlayerPosition(); } } this.transform.Translate(this.GameSpeed.ScrollingSpeed * Time.deltaTime, 0, 0); // `transform` is a field of `MonoBehaviour`. this.HUD.UpdateDistance(0.1f, Time.deltaTime); this.GameSpeed.Increment(Time.deltaTime); // Check if the player is in the danger zone. if (this.LocalPlayerSpawner.PlayerObject.transform.position.x - this.transform.position.x < -11) { // If multiplayer, warn the other that death occurred. if (this.Connection != null) { this.Connection.Write(new Network.Death()); } this.GameOver(false); } // Switching universe at defined audio time 59 109, taking into account the Spawner delay float delay = (this.PlanetSpawner.transform.position.x - 10f - this.transform.position.x) / this.GameSpeed.ScrollingSpeed; this.PlanetSpawner.SwitchUniverse(this.MusicPlayer.AudioSource.time < 59 - delay || this.MusicPlayer.AudioSource.time > 108 - delay); }