public static void Handle(Peer peer, Reader reader, MessageReceived info) { MessageType messageType = (MessageType)info.Channel; switch (messageType) { case MessageType.Test: MTTest test = new MTTest(); test.Read(reader); Run(() => Debug.Log("Received MTTest with msg: " + test.Msg)); break; case MessageType.GameReady: Run(() => Inst.OnReceivedGameReady?.Invoke(peer)); break; case MessageType.RandomSeed: MTRandomSeed seed = new MTRandomSeed(); seed.Read(reader); Run(() => SafeRandom.Seed = seed.Seed); break; case MessageType.ShipChadburn: MTShipChadburn chad = new MTShipChadburn(); chad.Read(reader); Run(() => GameManager.Inst.SetShipChadburn(chad.PlayerTag, chad.ShipID, chad.ChadburnSetting)); break; case MessageType.ShipCourse: MTShipCourse course = new MTShipCourse(); course.Read(reader); Run(() => GameManager.Inst.SetShipCourse(course.PlayerTag, course.ShipID, course.Course)); break; case MessageType.ShipTarget: MTShipTarget target = new MTShipTarget(); target.Read(reader); Run(() => GameManager.Inst.SetShipTarget(target.PlayerTag, target.ShipID, target.HasTarget, target.TargetShipID)); break; case MessageType.SyncGame: MTSyncGame sync = new MTSyncGame(); sync.Read(reader); Run(() => GameManager.Inst.SyncGames(sync)); break; default: Run(() => Debug.LogWarning("Could not handle net message of type " + messageType)); break; } }
public void SyncGames(MTSyncGame sync) { // Sync projectiles for (int i = 0; i < sync.NumProjectiles; i++) { Projectile p = FindProjectileByID(sync.ProjectileIDs[i]); if (p) { p.transform.position += (sync.ProjectilePositions[i] - p.transform.position) * 0.5f; p.transform.rotation = Quaternion.Lerp(p.transform.rotation, Quaternion.Euler(sync.ProjectileRotations[i]), 0.5f); p.Velocity = Vector3.Lerp(p.Velocity, sync.ProjectileVelocities[i], 0.5f); } else { Debug.LogWarning("Sync projectile: could not find id " + sync.ProjectileIDs[i]); } } // Sync ships uint waterIngressSectionsCounter = 0; uint gunTurretsCounter = 0; uint damageZoneCounter = 0; uint damageZoneDamagesCounter = 0; for (int i = 0; i < sync.NumShips; i++) { Ship s = FindShipByID(sync.ShipIDs[i]); bool counterUpdated = false; if (s != null && !s.Equals(null)) { if (sync.ShipsHitpoints[i] <= 0 && !s.Destroyed) { s.DamageHull(float.MaxValue); } else if (!s.Destroyed) { s.HullHitpoints = Mathf.Lerp(s.HullHitpoints, sync.ShipsHitpoints[i], 0.5f); s.transform.position += (sync.ShipPositions[i] - s.transform.position) * 0.5f; s.transform.rotation = Quaternion.Lerp(s.transform.rotation, Quaternion.Euler(sync.ShipRotations[i]), 0.5f); s.Velocity = Vector3.Lerp(s.Velocity, sync.ShipVelocities[i], 0.5f); for (byte waterIngressSectionIndex = 0; waterIngressSectionIndex < sync.ShipsNumWaterIngressSections[i]; waterIngressSectionIndex++) { Ship.WaterIngressSection section = s.WaterIngressSections.Find(wis => wis.sectionID == sync.WaterIngressSectionsIDs[waterIngressSectionsCounter]); section.numHoles = Mathf.CeilToInt(Mathf.Lerp(section.numHoles, sync.WaterIngressNumHoles[waterIngressSectionsCounter], 0.5f)); section.waterLevel = Mathf.Lerp(section.waterLevel, sync.WaterIngressWaterLevels[waterIngressSectionsCounter++], 0.5f); } if (s.PlayerTag != thisPlayerTag) { s.Autopilot.Chadburn = sync.ShipChadburnSettings[i]; s.Autopilot.Course = sync.ShipCourses[i]; if (sync.ShipsHasTarget[i]) { s.Targeting.Target = FindShipByID(sync.ShipsTargetShipID[i]); } else { s.Targeting.Target = null; } } for (byte gunTurretIndex = 0; gunTurretIndex < sync.ShipsNumGunTurrets[i]; gunTurretIndex++) { GunTurret t = s.Armament.GunTurrets.Find(gt => gt.ID == sync.GunTurretIDs[gunTurretsCounter]); if (sync.GunTurretsDisabled[gunTurretsCounter]) { t.Disable(); } t.ReloadProgress = Mathf.Lerp(t.ReloadProgress, sync.GunTurretsReloadProgress[gunTurretsCounter], 0.5f); t.TurretRotation = Mathf.Lerp(t.TurretRotation, sync.GunTurretsRotation[gunTurretsCounter], 0.5f); t.TargetTurretRotation = Mathf.Lerp(t.TargetTurretRotation, sync.GunTurretsTargetRotation[gunTurretsCounter], 0.5f); t.GunsElevation = Mathf.Lerp(t.GunsElevation, sync.GunTurretsElevation[gunTurretsCounter], 0.5f); t.TargetGunsElevation = Mathf.Lerp(t.TargetGunsElevation, sync.GunTurretsTargetElevation[gunTurretsCounter], 0.5f); t.StabilizationAngle = Mathf.Lerp(t.StabilizationAngle, sync.GunTurretsStabilizationAngle[gunTurretsCounter++], 0.5f); } for (byte damageZoneIndex = 0; damageZoneIndex < sync.ShipNumDamageZones[i]; damageZoneIndex++) { DamageZone dz = s.DamageZones.Find(sdz => sdz.ID == sync.ShipDamageZoneIDs[damageZoneCounter]); for (byte damageIndex = 0; damageIndex < sync.DamageZoneNumDamages[damageZoneCounter]; damageIndex++) { DamageType dt = sync.DamageZoneDamages[damageZoneDamagesCounter++]; if (!dz.Damages.Contains(dt)) { dz.Damages.Add(dt); } } damageZoneCounter++; } counterUpdated = true; } } else { Debug.LogWarning("Sync ship: could not find id " + sync.ShipIDs[i]); } // Update counter in case they did not get updated inside the ifs if (!counterUpdated) { waterIngressSectionsCounter += sync.ShipsNumWaterIngressSections[i]; gunTurretsCounter += sync.ShipsNumGunTurrets[i]; for (int k = 0; k < sync.ShipNumDamageZones[i]; k++) { damageZoneDamagesCounter += sync.DamageZoneNumDamages[damageZoneCounter]; damageZoneCounter++; } } } }