Advertisement
Guest User

BarMagnet Mod [Besiege]

a guest
Aug 31st, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using spaar.ModLoader;
  4. using TheGuysYouDespise;
  5. using UnityEngine;
  6.  
  7. namespace Blocks {
  8.     public class BarMagnetLoader : spaar.ModLoader.Mod {
  9.         public override string Name { get { return "BarMagnet"; } }
  10.         public override string DisplayName { get { return "BarMagnet"; } }
  11.         public override string Author { get { return "ITR"; } }
  12.         public override Version Version { get { return new Version(1,0); } }
  13.  
  14.        
  15.         public override void OnLoad() {
  16.             Game.OnSimulationToggle += ResetList;
  17.             spaar.Commands.RegisterHelpMessage("BarMagnet commands:\n   MaxDistance [int]\n MagnetForce [float]");
  18.             spaar.Commands.RegisterCommand("MaxDistance",(args,namedArgs) => {
  19.                 try {
  20.                     BarMagnet.maxDistance = float.Parse(args[0]);
  21.                 }
  22.                 catch {
  23.                     return "MaxDistance is "+BarMagnet.maxDistance.ToString();
  24.                 }
  25.                 return "MaxDistance was set to "+BarMagnet.maxDistance.ToString();
  26.             },"Changes the MaxDistance of the magnets (lower numbers make it less laggy)");
  27.  
  28.             spaar.Commands.RegisterCommand("MagnetForce",(args,namedArgs) => {
  29.                 try {
  30.                     BarMagnet.forceMultiplier = float.Parse(args[0]);
  31.                 }
  32.                 catch {
  33.                     return "MagnetForce is "+BarMagnet.forceMultiplier.ToString();
  34.                 }
  35.                 return "MagnetForce was set to "+BarMagnet.forceMultiplier.ToString();
  36.             },"Changes the force of the magnets");
  37.            
  38.  
  39.             /// <Block-loading-info>
  40.             /// Place .obj file in Mods/Blocks/Obj
  41.             /// Place texture in Mods/Blocks/Textures
  42.             /// Place any additional resources in Mods/Blocks/Resources
  43.             /// </Block-loading-info>
  44.  
  45.             #region set offset position, scale and rotation for the mesh
  46.             VisualOffset offset = new VisualOffset(new Vector3(1f,1f,1f),Vector3.zero,Vector3.zero);
  47.             #endregion
  48.  
  49.             #region set transform properties of the icon
  50.             Icon icon = new Icon(1f,new Vector3(0f,0f,0f),new Vector3(0f,0f,0f));
  51.             #endregion
  52.             #region add a compound collider
  53.             List<ColliderComposite> compoundCollider = new List<ColliderComposite> { new ColliderComposite(new Vector3(0.9f,0.9f,1.8f),new Vector3(0f,0f,1.05f),new Vector3(0f,0f,0f)) };
  54.             #endregion
  55.             #region add a set of block properties
  56.             BlockProperties blockProp = new BlockProperties().ToggleModeEnabled("Inverted",false);
  57.             #endregion
  58.             #region add any needed resources for the block
  59.             List<NeededResource> neededResources = new List<NeededResource>();
  60.             #endregion
  61.             #region add scripts to the block
  62.             Type[] scripts = new Type[] { typeof(BarMagnet) };
  63.             //to add a script just replace YourBlockScript below, add more through adding a "," and then write typeof(AnotherScript)
  64.             //Type[] scripts = new Type[] { typeof(YourBlockScript) };
  65.             #endregion
  66.  
  67.             //Parameter explanation: ID,     objName,       textureName,  blockName, icon, properties,        collider,       resources, mass, offset, scripts, show collider
  68.             BlockLoader.AddModBlock(0xC7,"BarMagnet.obj","BarMagnet.png","BarMagnet",icon,blockProp,compoundCollider,neededResources,0.5f,offset,scripts,false);
  69.         }
  70.  
  71.         public override void OnUnload() { }
  72.  
  73.         public static List<BarMagnet> barMagnetList = new List<BarMagnet>();
  74.  
  75.         public static void ResetList(bool isSimulating) {
  76.             if(!isSimulating)
  77.                 barMagnetList = new List<BarMagnet>();
  78.         }
  79.  
  80.         public static int AddToList(BarMagnet barMagnet) {
  81.             int ret = barMagnetList.Count;
  82.             barMagnetList.Add(barMagnet);
  83.             return ret;
  84.         }
  85.  
  86.     }
  87.  
  88.     public class BarMagnet : MonoBehaviour {
  89.         int myID = -1;
  90.         bool inverted = false;
  91.         public static float maxDistance = 100f;
  92.         public static float forceMultiplier = 200;
  93.         public Vector3 pos1;
  94.         public Vector3 pos2;
  95.         public Rigidbody rigidBody;
  96.  
  97.         public void Start() {
  98.             if(AddPiece.isSimulating) {
  99.                 myID = BarMagnetLoader.AddToList(this);
  100.                 rigidBody = GetComponent<Rigidbody>();
  101.                 inverted = GetComponent<MyBlockInfo>().toggleModeEnabled;
  102.             }
  103.         }
  104.        
  105.         public void FixedUpdate() {
  106.             if(inverted){
  107.                 pos1 = transform.position + transform.forward*2;
  108.                 pos2 = transform.position;
  109.             }
  110.             else{
  111.                 pos2 = transform.position + transform.forward*2;
  112.                 pos1 = transform.position;
  113.             }
  114.             if(myID!=-1) {
  115.                 for(int i = 0;i<myID;i++) {
  116.                     CalculateForce(BarMagnetLoader.barMagnetList[i]);
  117.                 }
  118.             }
  119.         }
  120.  
  121.         public void CalculateForce(BarMagnet barMagnet) {
  122.             if(Vector3.Distance(transform.position+transform.forward,barMagnet.transform.position+barMagnet.transform.forward)>maxDistance)
  123.                 return;
  124.             if(pos1==barMagnet.pos2||pos2==barMagnet.pos1) {
  125.                 return;
  126.             }
  127.             if(Vector3.Distance(pos1,barMagnet.pos2)<0.3f) {
  128.                 barMagnet.rigidBody.AddForceAtPosition((pos1-barMagnet.pos2).normalized,barMagnet.pos2);
  129.                 rigidBody.AddForceAtPosition((barMagnet.pos2-pos1).normalized,pos1);
  130.                 return;
  131.             }
  132.             if(Vector3.Distance(pos2,barMagnet.pos1)<0.3f) {
  133.                 barMagnet.rigidBody.AddForceAtPosition((pos2-barMagnet.pos1).normalized,barMagnet.pos1);
  134.                 rigidBody.AddForceAtPosition((barMagnet.pos1-pos2).normalized,pos2);
  135.                 return;
  136.             }
  137.             Vector3 Pp, Pn, Np, Nn;
  138.             Pp = barMagnet.pos1-pos1;
  139.             Pn = barMagnet.pos2-pos1;
  140.             Np = barMagnet.pos1-pos2;
  141.             Nn = barMagnet.pos2-pos2;
  142.             Pp = Pp.normalized/(Vector3.SqrMagnitude(Pp));
  143.             Pn = Pn.normalized/(Vector3.SqrMagnitude(Pn));
  144.             Np = Np.normalized/(Vector3.SqrMagnitude(Np));
  145.             Nn = Nn.normalized/(Vector3.SqrMagnitude(Nn));
  146.  
  147.             if(Pp.magnitude>100f) {
  148.                 Pp/= Pp.magnitude/100f;
  149.             }
  150.             if(Pn.magnitude>100f) {
  151.                 Pn/= Pn.magnitude/100f;
  152.             }
  153.             if(Np.magnitude>100f) {
  154.                 Np/= Np.magnitude/100f;
  155.             }
  156.             if(Nn.magnitude>100f) {
  157.                 Nn /= Nn.magnitude/100f;
  158.             }
  159.             barMagnet.rigidBody.AddForceAtPosition((Pp-Np)*forceMultiplier,barMagnet.pos1);
  160.             barMagnet.rigidBody.AddForceAtPosition((Nn-Pn)*forceMultiplier,barMagnet.pos2);
  161.             rigidBody.AddForceAtPosition((Pn-Pp)*forceMultiplier,pos1);
  162.             rigidBody.AddForceAtPosition((Np-Nn)*forceMultiplier,pos2);
  163.         }
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement