Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- EvilSensor.cs 2014-11-11 22:29:17.661597900 -0500
- +++ EvilSensor_new.cs 2014-11-11 21:09:43.166512400 -0500
- @@ -8,69 +8,116 @@
- [MyEntityComponentDescriptor(typeof(MyObjectBuilder_SensorBlock))]
- public class EvilSensor : MyGameLogicComponent
- {
- + // Initialize them variables
- static System.String[] OreNames;
- IMySensorBlock Sensor;
- public override void Close()
- {
- + // State changed! I'm not sure how this part works since sensor_StateChanged returns void? Maybe someone could enlighten me?
- + // I'm probably confused by the use of the -= operator.
- Sensor.StateChanged -= sensor_StateChanged;
- }
- public override MyObjectBuilder_EntityBase GetObjectBuilder(bool copy = false)
- {
- + // This is a required override, but we return null and let the caller handle the exception
- return null;
- }
- public override void Init(MyObjectBuilder_EntityBase objectBuilder)
- {
- + // If OreNames is empty, get the String[] of names from one of our references
- if (OreNames == null)
- {
- MyDefinitionManager.Static.GetOreTypeNames(out OreNames);
- }
- -
- + // Define that Sensor is a SensorBloack
- Sensor = Entity as IMySensorBlock;
- + // State changed! I'm not sure how this part works since sensor_StateChanged returns void? Maybe someone could enlighten me?
- + // I'm probably confused by the use of the -= operator.
- Sensor.StateChanged += sensor_StateChanged;
- }
- void sensor_StateChanged(bool obj)
- {
- - if(!obj) return;
- + // I think this part is depending on whether you entered or exited the range. !obj would be when you exited the range, so nothing happens.
- + if (!obj) return;
- + // Define some mo' variables
- string ore = null;
- + string sensorName = Sensor.CustomName.ToLowerInvariant();
- +
- var oreAmount = 100;
- - foreach(var o in OreNames)
- + var distance = 1.5f;
- +
- + string dataString = "";
- +
- + //Loop through the ore names in the array and see if the sensor's name contains one of them
- + foreach (var o in OreNames)
- {
- - if (Sensor.CustomName.StartsWith(o, System.StringComparison.InvariantCultureIgnoreCase))
- + if (sensorName.Contains(o.ToLowerInvariant()))
- {
- ore = o;
- - try
- - {
- - oreAmount = System.Convert.ToInt32(Sensor.CustomName.Substring(Sensor.CustomName.LastIndexOf(" ") + 1));
- - }
- - catch (System.FormatException)
- - {
- - oreAmount = 100;
- - }
- break;
- }
- }
- -
- + // If the sensor name does not contain an ore name, we exit here
- if (ore == null)
- return;
- -
- - // We want to spawn ore and throw it at entity which entered sensor
- + // See if the sensor name contains the "data string" as defined by beginning with "[" and ending with "]"
- + if (sensorName.Contains("[") && sensorName.Contains("]"))
- + {
- + /*
- + * At this point, we parse the "data string" from the name,
- + * check to see what variables it contains and their order, then we parse our data from it.
- + */
- + try
- + {
- + dataString = sensorName.Slice(sensorName.IndexOf("[") + 1, sensorName.LastIndexOf("]")).ToLowerInvariant().Replace(" ", "");
- + if (dataString.Contains("amt:") && dataString.Contains("dst:"))
- + {
- + if (dataString.IndexOf("amt:") < dataString.IndexOf("dst:")) {
- + oreAmount = System.Convert.ToInt32(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));;
- + distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + } else {
- + distance = System.Convert.ToSingle(dataString.Slice(dataString.IndexOf(":") + 1, dataString.IndexOf(";")));
- + oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- + }
- + else if (dataString.Contains("amt:"))
- + {
- + oreAmount = System.Convert.ToInt32(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- + else if (dataString.Contains("dst:"))
- + {
- + distance = System.Convert.ToSingle(dataString.Substring(dataString.LastIndexOf(":") + 1));
- + }
- +
- + }
- + catch (System.Exception)
- + {
- + // If something got fubar'ed, set the defaults and try to continue without crashing the damn game
- + oreAmount = 100;
- + distance = 1.5f;
- + }
- + }
- + // Create the object to be spawned
- MyObjectBuilder_FloatingObject floatingBuilder = new MyObjectBuilder_FloatingObject();
- + // Specify that the object is an ore inventory item of the amount & type specified by the user
- floatingBuilder.Item = new MyObjectBuilder_InventoryItem() { Amount = oreAmount, Content = new MyObjectBuilder_Ore() { SubtypeName = ore } };
- - floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important
- - floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * 1.5f, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
- + floatingBuilder.PersistentFlags = MyPersistentEntityFlags2.InScene; // Very important, but I'm not sure why. I guess if not flagged as InScene, it won't be recognized as actually being there would be my guess.
- + // Set the postition and orientation of the object in the world.
- + floatingBuilder.PositionAndOrientation = new MyPositionAndOrientation(Sensor.WorldMatrix.Translation + Sensor.WorldMatrix.Forward * distance, Sensor.WorldMatrix.Forward, Sensor.WorldMatrix.Up);
- + // Check to make sure this is a server instance of the mod (I think it runs on client as well), otherwise a duplicate, non-functional object will be spawned by the client as well. (Duplicate bug)
- if (Sandbox.ModAPI.MyAPIGateway.Multiplayer.IsServer)
- {
- + // Finally, we actually create & spawn the damn thing in the world here. Enjoy!
- var floatingObject = Sandbox.ModAPI.MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(floatingBuilder);
- }
- - // Now it only creates ore, we will throw it later
- }
- public override void MarkForClose()
- @@ -105,4 +152,21 @@
- {
- }
- }
- +
- + public static class Extensions
- + {
- + /// <summary>
- + /// Get the string slice between the two indexes.
- + /// Inclusive for start index, exclusive for end index.
- + /// </summary>
- + public static string Slice(this string source, int start, int end)
- + {
- + if (end < 0) // Keep this for negative end support
- + {
- + end = source.Length + end;
- + }
- + int len = end - start; // Calculate length
- + return source.Substring(start, len); // Return Substring of length
- + }
- + }
- }
Advertisement
Add Comment
Please, Sign In to add comment