Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RampScript : MonoBehaviour {
- //Size + positions + speed
- public bool OnRight = true;
- public float LaunchSpeed = 12.0f;
- private const float Width = 4;
- private const float Length = 10;
- private const float LaunchDistance = 1.0f;
- //Power
- private const float SideWardsPower = 30.0f;
- void OnTriggerStay(Collider other)
- {
- if (!other.gameObject.CompareTag("Player")) return;
- var playerScript = other.gameObject.GetComponent<Player>();
- var playerXDelta = transform.position.x - playerScript.transform.position.x;
- var direction = Input.GetAxis("Horizontal");
- var checkDirection = direction;
- if (!OnRight) checkDirection *= -1;
- const float directionMinimum = 0.25f;
- if (!(checkDirection > directionMinimum)) return;
- //Speed up player on ramp
- playerScript.AddExtraMovement(new Vector3(-SideWardsPower*Math.Sign(direction),0,0));
- //Launch player
- if (Math.Abs(playerXDelta) <= LaunchDistance)
- {
- playerScript.LaunchInAir(LaunchSpeed);
- }
- }
- //Check if can spawn the ramp and spawn it when possible
- static public bool CheckIfCanSpawnRampAndSpawnIt(GameObject row,Pool pool)
- {
- var scriptRow = row.GetComponent<LevelPieces>();
- var possiblePlaces= scriptRow.BuildableDimension.Where(AreaSuitable).ToList();
- if (possiblePlaces.Count <= 0) return false;
- var place = possiblePlaces[Random.Range(0, possiblePlaces.Count)];
- var leftPossible = (Math.Abs(place.XMax) - 5f > -0.01f);
- var rightPossible = (Math.Abs(place.XMin) - 5f > -0.01f);
- var pos = new Vector3(0, place.Y,Random.Range(0,(place.ZMax - place.ZMin)-Length)+place.ZMin+Length*0.5f);
- pos.z += row.transform.position.z;
- if (!leftPossible) PlaceRamp(false, row, pos,pool);
- else if (!rightPossible) PlaceRamp(true, row, pos, pool);
- else
- {
- switch (Random.Range(0, 3))
- {
- case 0:
- PlaceRamp(true, row, pos, pool);
- break;
- case 1:
- PlaceRamp(false, row, pos,pool);
- break;
- case 2:
- PlaceRamp(true, row, pos, pool);
- PlaceRamp(false, row, pos, pool);
- break;
- }
- }
- return true;
- }
- //Is area big enough to place
- static bool AreaSuitable(DimensionsToBuildOn dimensions)
- {
- if (Math.Abs(dimensions.ZMax - dimensions.ZMin) < Length) return false;
- if (Math.Abs(dimensions.XMax - dimensions.XMin) < Width) return false;
- return ((Math.Abs(dimensions.XMax) - 5f > -0.01f) || (Math.Abs(dimensions.XMin) - 5f > -0.01f));
- }
- //Place the ramp
- static void PlaceRamp(bool left, GameObject row,Vector3 pos,Pool pool)
- {
- var piecespawnPositon = pos;
- const float border = 5f;
- GameObject part;
- if (left)
- {
- piecespawnPositon.x = border;
- part = pool.MakePoolItem(piecespawnPositon, Quaternion.identity);
- if (part == null) return;
- part.transform.GetChild(0).GetComponent<RampScript>().OnRight = false;
- part.transform.Rotate(0, 180, 0);
- }
- else
- {
- piecespawnPositon.x = -border;
- part = pool.MakePoolItem(piecespawnPositon, Quaternion.identity);
- if (part == null) return;
- part.transform.GetChild(0).GetComponent<RampScript>().OnRight = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement