Advertisement
GaelVanhalst

Armadilla: Ramp

Jan 25th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. public class RampScript : MonoBehaviour {
  2.  
  3.     //Size + positions + speed    
  4.     public bool OnRight = true;
  5.     public float LaunchSpeed = 12.0f;
  6.     private const float Width = 4;
  7.     private const float Length = 10;
  8.     private const float LaunchDistance = 1.0f;
  9.  
  10.     //Power
  11.     private const float SideWardsPower = 30.0f;
  12.  
  13.     void OnTriggerStay(Collider other)
  14.     {
  15.         if (!other.gameObject.CompareTag("Player")) return;
  16.  
  17.        
  18.         var playerScript = other.gameObject.GetComponent<Player>();
  19.         var playerXDelta = transform.position.x - playerScript.transform.position.x;
  20.         var direction = Input.GetAxis("Horizontal");
  21.         var checkDirection = direction;
  22.  
  23.         if (!OnRight) checkDirection *= -1;
  24.  
  25.         const float directionMinimum = 0.25f;
  26.  
  27.         if (!(checkDirection > directionMinimum)) return;
  28.  
  29.         //Speed up player on ramp
  30.         playerScript.AddExtraMovement(new Vector3(-SideWardsPower*Math.Sign(direction),0,0));
  31.  
  32.         //Launch player
  33.         if (Math.Abs(playerXDelta) <= LaunchDistance)
  34.         {
  35.             playerScript.LaunchInAir(LaunchSpeed);
  36.         }
  37.     }
  38.  
  39.     //Check if can spawn the ramp and spawn it when possible
  40.     static public bool CheckIfCanSpawnRampAndSpawnIt(GameObject row,Pool pool)
  41.     {
  42.         var scriptRow = row.GetComponent<LevelPieces>();
  43.  
  44.         var possiblePlaces= scriptRow.BuildableDimension.Where(AreaSuitable).ToList();
  45.  
  46.         if (possiblePlaces.Count <= 0) return false;
  47.         var place = possiblePlaces[Random.Range(0, possiblePlaces.Count)];
  48.         var leftPossible = (Math.Abs(place.XMax) - 5f > -0.01f);
  49.         var rightPossible = (Math.Abs(place.XMin) - 5f > -0.01f);
  50.  
  51.         var pos = new Vector3(0, place.Y,Random.Range(0,(place.ZMax - place.ZMin)-Length)+place.ZMin+Length*0.5f);
  52.         pos.z += row.transform.position.z;
  53.  
  54.  
  55.  
  56.         if (!leftPossible) PlaceRamp(false, row, pos,pool);
  57.         else if (!rightPossible) PlaceRamp(true, row, pos, pool);
  58.         else
  59.         {
  60.             switch (Random.Range(0, 3))
  61.             {
  62.                 case 0:
  63.                     PlaceRamp(true, row, pos, pool);
  64.                     break;
  65.                 case 1:
  66.                     PlaceRamp(false, row, pos,pool);
  67.                     break;
  68.                 case 2:
  69.                     PlaceRamp(true, row, pos, pool);
  70.                     PlaceRamp(false, row, pos, pool);
  71.                     break;
  72.             }
  73.         }
  74.  
  75.         return true;
  76.     }
  77.  
  78.     //Is area big enough to place
  79.     static bool AreaSuitable(DimensionsToBuildOn dimensions)
  80.     {
  81.         if (Math.Abs(dimensions.ZMax - dimensions.ZMin) < Length) return false;
  82.         if (Math.Abs(dimensions.XMax - dimensions.XMin) < Width) return false;
  83.  
  84.         return ((Math.Abs(dimensions.XMax) - 5f > -0.01f) || (Math.Abs(dimensions.XMin) - 5f > -0.01f));
  85.     }
  86.  
  87.     //Place the ramp
  88.     static void PlaceRamp(bool left, GameObject row,Vector3 pos,Pool pool)
  89.     {
  90.         var piecespawnPositon = pos;
  91.  
  92.         const float border = 5f;
  93.         GameObject part;
  94.         if (left)
  95.         {
  96.             piecespawnPositon.x = border;
  97.             part = pool.MakePoolItem(piecespawnPositon, Quaternion.identity);
  98.             if (part == null) return;
  99.             part.transform.GetChild(0).GetComponent<RampScript>().OnRight = false;
  100.  
  101.             part.transform.Rotate(0, 180, 0);
  102.         }
  103.         else
  104.         {
  105.             piecespawnPositon.x = -border;
  106.  
  107.             part = pool.MakePoolItem(piecespawnPositon, Quaternion.identity);
  108.             if (part == null) return;
  109.             part.transform.GetChild(0).GetComponent<RampScript>().OnRight = true;
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement