Advertisement
Guest User

Katamari Damacy

a guest
Jul 25th, 2015
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using UnityEngine;
  7.  
  8. namespace Katamari
  9. {
  10.  
  11.     [spaar.Mod("Katamari",author="ITR",version="1.0")]
  12.     public class MainClass : MonoBehaviour
  13.     {
  14.         public BallScript ballScript = null;
  15.  
  16.         public static string UP = "up";
  17.         public static string DOWN = "down";
  18.         public static string LEFT = "left";
  19.         public static string RIGHT = "right";
  20.  
  21.         public static bool SuperSticky = false;
  22.  
  23.         public void Start() {
  24.             spaar.Commands.RegisterHelpMessage("Katamari commands:\n    UP [key]\n  DOWN [key]\n    LEFT [key]\n    RIGHT [key]\n   acceleration [acceleration]\n   maxSpeedMult [speed]\n  posDiv [div]\n  posDiv [mult]\n SuperSticky [bool]");
  25.  
  26.             spaar.Commands.RegisterCommand("UP",(args,namedArgs) => {
  27.                 if(args.Length<1) {
  28.                     return "Current UP key is "+UP;
  29.                 }
  30.                 string key = args[0].Replace("_"," ").ToLower();
  31.                 try {
  32.                     Input.GetKey(key);
  33.                 }
  34.                 catch {
  35.                     return "Not a valid key";
  36.                 }
  37.                 UP = key;
  38.                 return "Set UP to "+UP;
  39.             },"Changes the UP key");
  40.  
  41.             spaar.Commands.RegisterCommand("DOWN",(args,namedArgs) => {
  42.                 if(args.Length<1) {
  43.                     return "Current DOWN key is "+DOWN;
  44.                 }
  45.                 string key = args[0].Replace("_"," ").ToLower();
  46.                 try {
  47.                     Input.GetKey(key);
  48.                 }
  49.                 catch {
  50.                     return "Not a valid key";
  51.                 }
  52.                 DOWN = key;
  53.                 return "Set DOWN to "+DOWN;
  54.             },"Changes the DOWN key");
  55.  
  56.             spaar.Commands.RegisterCommand("LEFT",(args,namedArgs) => {
  57.                 if(args.Length<1) {
  58.                     return "Current LEFT key is "+LEFT;
  59.                 }
  60.                 string key = args[0].Replace("_"," ").ToLower();
  61.                 try {
  62.                     Input.GetKey(key);
  63.                 }
  64.                 catch {
  65.                     return "Not a valid key";
  66.                 }
  67.                 LEFT = key;
  68.                 return "Set LEFT to "+LEFT;
  69.             },"Changes the LEFT key");
  70.  
  71.             spaar.Commands.RegisterCommand("RIGHT",(args,namedArgs) => {
  72.                 if(args.Length<1) {
  73.                     return "Current RIGHT key is "+RIGHT;
  74.                 }
  75.                 string key = args[0].Replace("_"," ").ToLower();
  76.                 try {
  77.                     Input.GetKey(key);
  78.                 }
  79.                 catch {
  80.                     return "Not a valid key";
  81.                 }
  82.                 RIGHT = key;
  83.                 return "Set RIGHT to "+RIGHT;
  84.             },"Changes the RIGHT key");
  85.  
  86.             spaar.Commands.RegisterCommand("acceleration",(args,namedArgs) => {
  87.                 if(args.Length<1) {
  88.                     return "Current acceleration "+BallScript.acceleration;
  89.                 }
  90.                 try {
  91.                     BallScript.acceleration = float.Parse(args[0]);
  92.                 }
  93.                 catch {
  94.                     return "Could not parse acceleration";
  95.                 }
  96.                 return "Set acceleration to "+BallScript.acceleration;
  97.             },"Changes the acceleration");
  98.  
  99.             spaar.Commands.RegisterCommand("maxSpeedMult",(args,namedArgs) => {
  100.                 if(args.Length<1) {
  101.                     return "Current maxSpeedMult "+BallScript.maxSpeedMult;
  102.                 }
  103.                 try {
  104.                     BallScript.maxSpeedMult = float.Parse(args[0]);
  105.                 }
  106.                 catch {
  107.                     return "Could not parse speed";
  108.                 }
  109.                 return "Set maxSpeedMult to "+BallScript.maxSpeedMult;
  110.             },"Changes the maxSpeedMult");
  111.  
  112.             spaar.Commands.RegisterCommand("posDiv",(args,namedArgs) => {
  113.                 if(args.Length<1) {
  114.                     return "Current posDiv "+Sticky.posDiv;
  115.                 }
  116.                 try {
  117.                     Sticky.posDiv = float.Parse(args[0]);
  118.                 }
  119.                 catch {
  120.                     return "Could not parse div";
  121.                 }
  122.                 return "Set posDiv to "+Sticky.posDiv;
  123.             },"Changes the amount blocks get displaced when put on the spinning thingy");
  124.  
  125.             spaar.Commands.RegisterCommand("posMult",(args,namedArgs) => {
  126.                 if(args.Length<1) {
  127.                     return "Current posMult "+Sticky.posMult;
  128.                 }
  129.                 try {
  130.                     Sticky.posMult = float.Parse(args[0]);
  131.                 }
  132.                 catch {
  133.                     return "Could not parse mult";
  134.                 }
  135.                 return "Set posMult to "+Sticky.posMult;
  136.             },"Changes the amount blocks get displaced when put on the spinning thingy");
  137.  
  138.             spaar.Commands.RegisterCommand("SuperSticky",(args,namedArgs) => {
  139.                 if(args.Length<1) {
  140.                     return "Current SuperSticky "+SuperSticky;
  141.                 }
  142.                 try {
  143.                     SuperSticky = bool.Parse(args[0]);
  144.                 }
  145.                 catch {
  146.                     return "Could not parse bool";
  147.                 }
  148.                 return "Set SuperSticky to "+SuperSticky;
  149.             },"Duh");
  150.  
  151.  
  152.         }
  153.  
  154.         public void FixedUpdate() {
  155.             if(ballScript==null) {
  156.                 if(AddPiece.isSimulating) {
  157.                     GameObject startingBlock = GameObject.Find("bgeL0");
  158.                     if(startingBlock!=null) {
  159.                         ballScript = startingBlock.AddComponent<BallScript>();
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     public class BallScript : MonoBehaviour {
  167.         public static Rigidbody rigidBody;
  168.         float originalMass = 1;
  169.  
  170.         public static Vector3 position = Vector3.zero;
  171.  
  172.         public void Start() {
  173.             Collider col = GetComponent<Collider>();
  174.             Destroy(col);
  175.             col = gameObject.AddComponent<SphereCollider>();
  176.             rigidBody = GetComponent<Rigidbody>();
  177.             originalMass = rigidBody.mass;
  178.             rigidbody.mass = 5;
  179.             gameObject.AddComponent<Sticky>();
  180.             size = 2*2*2;
  181.  
  182.         }
  183.  
  184.         public static float acceleration = 30000;
  185.         public static float size = 0;
  186.  
  187.         public void FixedUpdate() {
  188.             if(!AddPiece.isSimulating) {
  189.                 rigidBody.mass = originalMass;
  190.                 Destroy(this);
  191.             }
  192.             Vector3 left = new Vector3(Camera.main.transform.forward.x,0,Camera.main.transform.forward.z).normalized;
  193.             Vector3 forw = new Vector3(left.z,0,-left.x);
  194.  
  195.             if(Input.GetKey(MainClass.UP)) {
  196.                 rigidBody.AddTorque(forw*Time.fixedDeltaTime*acceleration,ForceMode.Acceleration);
  197.             }
  198.             if(Input.GetKey(MainClass.DOWN)) {
  199.                 rigidBody.AddTorque(-forw*Time.fixedDeltaTime*acceleration,ForceMode.Acceleration);
  200.             }
  201.             if(Input.GetKey(MainClass.LEFT)) {
  202.                 rigidBody.AddTorque(left*Time.fixedDeltaTime*acceleration,ForceMode.Acceleration);
  203.             }
  204.             if(Input.GetKey(MainClass.RIGHT)) {
  205.                 rigidBody.AddTorque(-left*Time.fixedDeltaTime*acceleration,ForceMode.Acceleration);
  206.             }
  207.             Vector3 vec = rigidbody.angularVelocity;
  208.             if(vec.magnitude*Mathf.Sqrt(size)>maxSpeedMult) {
  209.                 rigidbody.angularVelocity = vec.normalized*maxSpeedMult;
  210.             }
  211.         }
  212.        
  213.         public static float maxSpeedMult = 1.7f;
  214.     }
  215.  
  216.     public class Sticky : MonoBehaviour {
  217.         public static float posDiv = 20;
  218.         public static float posMult = 19;
  219.         public static string nameToCheck = "";
  220.  
  221.         public void Start() {
  222.             FinishLine finishLine = FindObjectOfType<FinishLine>();
  223.             if(finishLine!=null) {
  224.                 nameToCheck = finishLine.nameToCheck;
  225.                 Debug.Log(nameToCheck);
  226.             }
  227.         }
  228.  
  229.         public void OnCollisionEnter(Collision col) {
  230.             if(col.gameObject.name=="Katamari")
  231.                 return;
  232.  
  233.             Rigidbody rb = col.gameObject.GetComponent<Rigidbody>();
  234.             if(rb!=null) {
  235.                 List<GameObject> toBeAttached = new List<GameObject>(){col.gameObject};
  236.                 List<Joint> jointsOfAttached = new List<Joint>();
  237.                 jointsOfAttached.AddRange(col.gameObject.GetComponentsInChildren<Joint>());
  238.                 for(int i = 0;i<jointsOfAttached.Count;i++) {
  239.                     if(jointsOfAttached[i].connectedBody!=null) {
  240.                         if(!toBeAttached.Contains(jointsOfAttached[i].connectedBody.gameObject)) {
  241.                             toBeAttached.Add(jointsOfAttached[i].connectedBody.gameObject);
  242.                             jointsOfAttached.AddRange(jointsOfAttached[i].connectedBody.GetComponentsInChildren<Joint>());
  243.                         }
  244.                     }
  245.                 }
  246.                 for(int i = 0;i<toBeAttached.Count;i++) {
  247.                     for(int j = 0;j<toBeAttached[i].transform.childCount;j++) {
  248.                         GameObject go = toBeAttached[i].transform.GetChild(j).gameObject;
  249.                         if(!toBeAttached.Contains(go)) {
  250.                             toBeAttached.Add(go);
  251.                         }
  252.                     }
  253.                 }
  254.                 float totalMass = 0;
  255.                 float totalSize = 0;
  256.                 for(int i = 0;i<toBeAttached.Count;i++) {
  257.                     Vector3 s = Vector3.zero;
  258.                     try {
  259.                         totalMass += toBeAttached[i].GetComponent<Rigidbody>().mass;
  260.                         s = toBeAttached[i].GetComponent<Renderer>().bounds.size;
  261.                     }
  262.                     catch {
  263.                         continue;
  264.                     }
  265.                     totalSize += s.x*s.y*s.z;
  266.                 }
  267.  
  268.  
  269.                 if((totalMass<=BallScript.rigidBody.mass*1.1&&totalSize<BallScript.size*1.1f)||MainClass.SuperSticky) {
  270.                     BallScript.rigidBody.mass += totalMass;
  271.                     BallScript.size += totalSize;
  272.                     List<Component> des = new List<Component>();
  273.                     foreach(GameObject go in toBeAttached) {
  274.                         if(go.name!=nameToCheck)
  275.                             go.name = "Katamari";
  276.                         go.transform.parent = transform;
  277.  
  278.                         BleedOnJointBreak bleedOnJointBreak = go.GetComponent<BleedOnJointBreak>();
  279.                         if(bleedOnJointBreak!=null) {
  280.                             bleedOnJointBreak.injuryControllerCode.Kill();
  281.                             bleedOnJointBreak.AddToPercentageBar();
  282.                         }
  283.  
  284.                         Component[] components = go.GetComponents(typeof(Component));
  285.                         for(int i = 0;i<components.Length;i++) {
  286.                             string comName = components[i].ToString().ToLower();
  287.                             if(comName.Contains("transform")||comName.Contains("collider")||comName.Contains("ballscript")||comName.Contains("mesh")) {
  288.                                 continue;
  289.                             }
  290.                             try {
  291.                                 Destroy(components[i]);
  292.                             }
  293.                             catch {
  294.                                 des.Add(components[i]);
  295.                             }
  296.                         }
  297.                         go.transform.localPosition *= posMult/posDiv;
  298.                     }
  299.                     foreach(Component d in des) {
  300.                         Destroy(d);
  301.                     }
  302.                 }
  303.             }
  304.         }
  305.  
  306.         public void FixedUpdate() {
  307.             if(!AddPiece.isSimulating)
  308.                 Destroy(this);
  309. //          if(transform.localPosition.magnitude>2)
  310. //              transform.localPosition -= transform.localPosition.normalized*Time.fixedDeltaTime;
  311.         }
  312.     }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement