private IEnumerable <KMSelectable[]> TwitchProcessConnection(string command) { bool connect = command.StartsWith("connect"); if (selectedStar) { yield return new[] { selectedStar.Selectable } } ; string[] chains = command.Split(' ').Skip(1).Where(s => s.Length > 0).Join("").Split(';').Where(s => s.Length > 0).ToArray(); foreach (string chain in chains) { int[] ids = chain.Split('-').Join("").ToArray().Select(c => int.Parse(c.ToString())).ToArray(); for (int i = 0; i < ids.Length - 1; i++) { StarComponent star = stars[ids[i]]; StarComponent other = stars[ids[i + 1]]; if (star.connectedStars.Contains(other) == connect) { continue; } yield return(new[] { star.Selectable, other.Selectable }); } } }
//Creates gameobjects to represent data created by the MapGenerator class. void CreateMap() { starComponents = new StarComponent[mapGenerator.numberOfStars]; int index = 0; foreach (StarInfo item in mapGenerator.starInfos) { GameObject go = Instantiate(starPrefab, item.location, Quaternion.identity, transform); StarComponent sc = go.GetComponent <StarComponent> (); sc.Setup(item); starComponents [index] = sc; index++; } for (int i = 0; i < mapGenerator.numberOfStars; i++) { for (int j = i; j < mapGenerator.numberOfStars; j++) { if (mapGenerator.graph [i, j] == 1) { GameObject go = Instantiate(linePrefab, transform); go.GetComponent <LineRenderer> ().SetPositions(new Vector3[2] { (Vector3)mapGenerator.starInfos [i].location, (Vector3)mapGenerator.starInfos [j].location }); } } } }
private void Start() { moduleId = moduleIdCounter++; mapRotationSpeed = Random.onUnitSphere; List <KMSelectable> children = new List <KMSelectable>(); for (int i = 0; i < STARS_COUNT; i++) { StarComponent star = Instantiate(StarPrefab); star.transform.parent = MapContainer.transform; star.transform.localPosition = Random.rotation * (Vector3.forward * SPAWN_RADIUS); star.transform.localRotation = Quaternion.identity; star.transform.localScale = Vector3.one; stars[i] = star; star.StarInfo = StarInfo; star.Selectable.OnInteract += () => { OnStarPressed(star); return(false); }; star.Selectable.Parent = Selectable; children.Add(star.Selectable); } children.Add(ClearButton); children.Add(SubmitButton); Selectable.Children = children.ToArray(); Selectable.UpdateChildren(); Selectable.OnFocus += () => focused = true; Selectable.OnDefocus += () => focused = false; Module.OnActivate += Activate; }
private void Unselect() { if (selectedStar != null) { selectedStar.Selected = false; selectedStar = null; } }
private void OnSubmitButtonPressed() { if (solved || !activated) { return; } Unselect(); Debug.LogFormat("[Starmap Reconstruction #{0}] Submit pressed", moduleId); Starmap map = new Starmap(stars.Length); foreach (StarComponent star in stars) { int expectedAdjacentsCount = StarmapReconstructionData.GetAdjacentStarsCount(star.Race, star.Regime, BombInfo); int actualAdjacentsCount = star.connectedStars.Count; if (expectedAdjacentsCount != actualAdjacentsCount) { Debug.LogFormat("[Starmap Reconstruction #{0}] STRIKE: Star {1} has {2} connected stars. Expected: {3}", moduleId, star.Name, actualAdjacentsCount, expectedAdjacentsCount); Module.HandleStrike(); return; } foreach (StarComponent other in star.connectedStars) { map.Add(star.Id, other.Id); } } Debug.LogFormat("[Starmap Reconstruction #{0}] Submitted map: {1}", moduleId, map.ToShortString()); foreach (StarComponent star in stars) { KeyValuePair <string, int>?requiredDistance = StarmapReconstructionData.GetRequiredDistanceFrom(star.Name); if (requiredDistance == null) { continue; } string to = requiredDistance.Value.Key; int expectedDistance = requiredDistance.Value.Value; int otherIndex = stars.IndexOf(s => s.Name == to); if (otherIndex < 0) { continue; } StarComponent other = stars[otherIndex]; int actualDistance = map.GetDistance(star.Id, other.Id); if (expectedDistance != actualDistance) { Debug.LogFormat("[Starmap Reconstruction #{0}] STRIKE: Distance from {1} to {2} is {3}. Expected: {4}", moduleId, star.Name, other.Name, actualDistance, expectedDistance); Module.HandleStrike(); return; } } Debug.LogFormat("[Starmap Reconstruction #{0}] Module solved", moduleId); solved = true; Audio.PlayGameSoundAtTransform(KMSoundOverride.SoundEffect.CorrectChime, transform); Module.HandlePass(); }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit2D hitInfo = Physics2D.Raycast((Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hitInfo.collider != null && hitInfo.collider.GetComponent <StarComponent>() != null) { StarComponent sc = hitInfo.collider.GetComponent <StarComponent> (); if (sc.isNeighbour) { currentID = sc.id; EventSystem.Current.FireEvent(EventTypeEnum.WARPING, new EventData("Warping")); FindObjectOfType <CameraShake> ().StartShake(2f, SwitchLevel); } } } }
private void Update() { for (int i = 0; i < stars.Length; i++) { Vector3 move = Vector3.zero; StarComponent star = stars[i]; Vector3 pos = star.transform.localPosition; move += -pos.normalized * ATTRACTOR_POWER * Mathf.Pow(pos.magnitude, 2); foreach (StarComponent other in stars) { if (star == other) { continue; } Vector3 diff = other.transform.localPosition - pos; move -= diff.normalized * GRAVITATIONAL_CONSTANT / Mathf.Pow(Mathf.Max(diff.magnitude, PLANK_LENGTH), 2); if (star.connectedStars.Contains(other)) { move += diff.normalized * CORRIDOR_FORCE * Mathf.Pow(diff.magnitude, 2); } } star.Speed += move; } foreach (StarComponent star in stars) { star.ApplyForce(); } foreach (CorridorComponent corridor in corridors) { corridor.UpdatePosition(); } if (!focused) { mapRotationInertion = Mathf.Min(1f, mapRotationInertion + Time.deltaTime); } else { mapRotationInertion = Mathf.Max(0, mapRotationInertion - Time.deltaTime); } mapRotationSpeed = (mapRotationSpeed + Random.onUnitSphere * MAP_ROTATION_ACCELERATION * Time.deltaTime).normalized; mapRotation += mapRotationSpeed * Time.deltaTime * MAP_ROTATION_SPEED * mapRotationInertion; MapContainer.transform.localRotation = Quaternion.Euler(mapRotation); }
private void OnStarPressed(StarComponent star) { if (solved || !activated) { return; } if (selectedStar == null) { selectedStar = star; star.Selected = true; return; } if (selectedStar == star) { Unselect(); return; } if (star.connectedStars.Contains(selectedStar)) { selectedStar.connectedStars.Remove(star); star.connectedStars.Remove(selectedStar); CorridorComponent corridorToDelete = corridors.Find(c => (c.connection.Key == star && c.connection.Value == selectedStar) || (c.connection.Key == selectedStar && c.connection.Value == star) ); Audio.PlaySoundAtTransform("StarmapReconstructionStarsDisconnected", corridorToDelete.transform); corridors.Remove(corridorToDelete); Destroy(corridorToDelete.gameObject); } else { star.connectedStars.Add(selectedStar); selectedStar.connectedStars.Add(star); CorridorComponent corridor = Instantiate(CorridorPrefab); corridor.connection = new KeyValuePair <StarComponent, StarComponent>(star, selectedStar); corridor.transform.parent = MapContainer.transform; corridor.UpdatePosition(); Audio.PlaySoundAtTransform("StarmapReconstructionStarsConnected", corridor.transform); corridors.Add(corridor); } Unselect(); }
// Parser apply event void IParserEventSubscriber.Apply(ConfigNode node) { // Get any existing material we might have on this scaled version Material material = scaledVersion.GetComponent <Renderer>().sharedMaterial; ConfigNode data = node.GetNode(materialNodeName); // Check for bad condition (no material, no new material) if (material == null && data == null) { throw new Exception("Scaled version has no material information"); } // Are we a planet or moon? if (type.value != BodyType.Star) { // If we are not a star, we need a scaled space fader and a sphere collider if (scaledVersion.GetComponent <ScaledSpaceFader> () == null) { ScaledSpaceFader fader = scaledVersion.AddComponent <ScaledSpaceFader> (); fader.floatName = "_Opacity"; fader.celestialBody = owner; } // Add a sphere collider if we need one if (scaledVersion.GetComponent <SphereCollider> () == null) { SphereCollider collider = scaledVersion.AddComponent <SphereCollider>(); collider.center = Vector3.zero; collider.radius = 1000.0f; } // Generate new atmospheric body material if (type.value == BodyType.Atmospheric) { ScaledPlanetRimAerialLoader newMaterial = null; if (material != null && ScaledPlanetRimAerial.UsesSameShader(material)) { newMaterial = new ScaledPlanetRimAerialLoader(material); if (data != null) { Parser.LoadObjectFromConfigurationNode(newMaterial, data); } } else { newMaterial = Parser.CreateObjectFromConfigNode <ScaledPlanetRimAerialLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); if (newMaterial.rimColorRamp != null) { newMaterial.rimColorRamp.wrapMode = TextureWrapMode.Clamp; newMaterial.rimColorRamp.mipMapBias = 0.0f; } scaledVersion.GetComponent <Renderer>().sharedMaterial = newMaterial; } // Generate new vacuum body material else { ScaledPlanetSimpleLoader newMaterial = null; if (material != null && ScaledPlanetSimple.UsesSameShader(material)) { newMaterial = new ScaledPlanetSimpleLoader(material); if (data != null) { Parser.LoadObjectFromConfigurationNode(newMaterial, data); } } else { newMaterial = Parser.CreateObjectFromConfigNode <ScaledPlanetSimpleLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); scaledVersion.GetComponent <Renderer>().sharedMaterial = newMaterial; } } // Otherwise we are a star else { // Add the SunShaderController behavior if (scaledVersion.GetComponent <SunShaderController>() == null) { scaledVersion.AddComponent <SunShaderController>(); } // Add the ScaledSun behavior // TODO - apparently there can only be one of these (or it destroys itself) if (scaledVersion.GetComponent <ScaledSun>() == null) { scaledVersion.AddComponent <ScaledSun>(); } // Add the Kopernicus star componenet component = scaledVersion.AddComponent <StarComponent> (); lightShifter = new LightShifterLoader(); // Generate a new material for the star EmissiveMultiRampSunspotsLoader newMaterial = null; if (material != null && EmissiveMultiRampSunspots.UsesSameShader(material)) { newMaterial = new EmissiveMultiRampSunspotsLoader(material); if (data != null) { Parser.LoadObjectFromConfigurationNode(newMaterial, data); } } else { newMaterial = Parser.CreateObjectFromConfigNode <EmissiveMultiRampSunspotsLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); scaledVersion.GetComponent <Renderer>().sharedMaterial = newMaterial; } }
// Parser apply event void IParserEventSubscriber.Apply(ConfigNode node) { // Get any existing material we might have on this scaled version Material material = scaledVersion.renderer.sharedMaterial; ConfigNode data = node.GetNode (materialNodeName); // Check for bad condition (no material, no new material) if (material == null && data == null) { throw new Exception("Scaled version has no material information"); } // Are we a planet or moon? if (type.value != BodyType.Star) { // If we are not a star, we need a scaled space fader and a sphere collider if (scaledVersion.GetComponent<ScaledSpaceFader> () == null) { ScaledSpaceFader fader = scaledVersion.AddComponent<ScaledSpaceFader> (); fader.floatName = "_Opacity"; fader.celestialBody = owner; } // Add a sphere collider if we need one if (scaledVersion.GetComponent<SphereCollider> () == null) { SphereCollider collider = scaledVersion.AddComponent<SphereCollider>(); collider.center = Vector3.zero; collider.radius = 1000.0f; } // Generate new atmospheric body material if (type.value == BodyType.Atmospheric) { ScaledPlanetRimAerialLoader newMaterial = null; if (material != null) { newMaterial = new ScaledPlanetRimAerialLoader (material); if(data != null) Parser.LoadObjectFromConfigurationNode (newMaterial, data); } else { newMaterial = Parser.CreateObjectFromConfigNode<ScaledPlanetRimAerialLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); newMaterial.rimColorRamp.wrapMode = TextureWrapMode.Clamp; newMaterial.rimColorRamp.mipMapBias = 0.0f; scaledVersion.renderer.sharedMaterial = newMaterial; } // Generate new vacuum body material else { ScaledPlanetSimpleLoader newMaterial = null; if (material != null) { newMaterial = new ScaledPlanetSimpleLoader (material); if(data != null) Parser.LoadObjectFromConfigurationNode (newMaterial, data); } else { newMaterial = Parser.CreateObjectFromConfigNode<ScaledPlanetSimpleLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); scaledVersion.renderer.sharedMaterial = newMaterial; } } // Otherwise we are a star else { // Add the SunShaderController behavior if(scaledVersion.GetComponent<SunShaderController>() == null) scaledVersion.AddComponent<SunShaderController>(); // Add the ScaledSun behavior // TODO - apparently there can only be one of these (or it destroys itself) if(scaledVersion.GetComponent<ScaledSun>() == null) scaledVersion.AddComponent<ScaledSun>(); // Add the Kopernicus star componenet component = scaledVersion.AddComponent<StarComponent> (); // Generate a new material for the star EmissiveMultiRampSunspotsLoader newMaterial = null; if(material != null) { newMaterial = new EmissiveMultiRampSunspotsLoader (material); if(data != null) Parser.LoadObjectFromConfigurationNode (newMaterial, data); } else { newMaterial = Parser.CreateObjectFromConfigNode<EmissiveMultiRampSunspotsLoader> (data); } newMaterial.name = Guid.NewGuid().ToString(); scaledVersion.renderer.sharedMaterial = newMaterial; } }
// Parser Post Apply Event public void PostApply(ConfigNode node) { // If an orbit is defined, we orbit something if (orbit != null) { // If this body needs orbit controllers, create them if (generatedBody.orbitDriver == null) { generatedBody.orbitDriver = generatedBody.celestialBody.gameObject.AddComponent <OrbitDriver> (); generatedBody.orbitRenderer = generatedBody.celestialBody.gameObject.AddComponent <OrbitRenderer> (); } // Setup orbit generatedBody.orbitDriver.updateMode = OrbitDriver.UpdateMode.UPDATE; orbit.Apply(generatedBody); } // If a PQS version was definied if (pqs != null) { // ----------- DEBUG ------------- #if DEBUG Utility.DumpObjectProperties(pqs.pqsVersion.surfaceMaterial, " ---- Surface Material (Post PQS Loader) ---- "); Utility.GameObjectWalk(pqs.pqsVersion.gameObject, " "); #endif // ------------------------------- // Assign the generated PQS to our new world generatedBody.pqsVersion = pqs.pqsVersion; // Adjust the radius of the PQSs appropriately foreach (PQS p in generatedBody.pqsVersion.GetComponentsInChildren(typeof(PQS), true)) { p.radius = generatedBody.celestialBody.Radius; } } // Create our RingLoaders foreach (RingLoader ring in rings) { RingLoader.AddRing(generatedBody.scaledVersion.gameObject, ring.ring); } // If this body is a star if (scaledVersion.type.value == BodyType.Star) { // Get the Kopernicus star component from the scaled version StarComponent component = generatedBody.scaledVersion.GetComponent <StarComponent> (); // If we have defined a custom power curve, load it if (solarPowerCurve != null) { component.powerCurve = solarPowerCurve.curve; } } // We need to generate new scaled space meshes if // a) we are using a template and we've change either the radius or type of body // b) we aren't using a template if (((template != null) && (Math.Abs(template.radius - generatedBody.celestialBody.Radius) > 1.0 || template.type != scaledVersion.type.value)) || template == null) { const double rJool = 6000000.0; const float rScaled = 1000.0f; // Compute scale between Jool and this body float scale = (float)(generatedBody.celestialBody.Radius / rJool); generatedBody.scaledVersion.transform.localScale = new Vector3(scale, scale, scale); // Attempt to load a cached version of the scale space string CacheDirectory = KSPUtil.ApplicationRootPath + ScaledSpaceCacheDirectory; string CacheFile = CacheDirectory + "/" + generatedBody.name + ".bin"; Directory.CreateDirectory(CacheDirectory); if (File.Exists(CacheFile)) { if (!File.Exists(ScaledSpaceCacheDirectory + "/DEBUG")) { Logger.Active.Log("[Kopernicus]: Body.PostApply(ConfigNode): Loading cached scaled space mesh: " + generatedBody.name); generatedBody.scaledVersion.GetComponent <MeshFilter>().sharedMesh = Utility.DeserializeMesh(CacheFile); } } // Otherwise we have to generate the mesh else { Logger.Active.Log("[Kopernicus]: Body.PostApply(ConfigNode): Generating scaled space mesh: " + generatedBody.name); Mesh scaledVersionMesh = ComputeScaledSpaceMesh(generatedBody); generatedBody.scaledVersion.GetComponent <MeshFilter> ().sharedMesh = scaledVersionMesh; if (!File.Exists(ScaledSpaceCacheDirectory + "/DEBUG")) { Utility.SerializeMesh(scaledVersionMesh, CacheFile); } } // Apply mesh to the body SphereCollider collider = generatedBody.scaledVersion.GetComponent <SphereCollider>(); if (collider != null) { collider.radius = rScaled; } } // Post gen celestial body Utility.DumpObjectFields(generatedBody.celestialBody, " Celestial Body "); }