void Start() { forces = GetComponent <ForceAffector>(); BoxCollider2D windBox = GetComponent <BoxCollider2D>(); //windRect = new Rect(); //windRect.center = windBox.bounds.center; //windRect.size = windBox.bounds.size; float left = transform.position.x - (GetComponent <BoxCollider2D>().size.x / 2); float top = transform.position.y + (GetComponent <BoxCollider2D>().size.y / 2); float width = GetComponent <BoxCollider2D>().size.x; float height = GetComponent <BoxCollider2D>().size.y; windRect = new Rect(left, top, width, height); windAreaOffset = new Vector2(gameObject.GetComponent <BoxCollider2D>().offset.x, gameObject.GetComponent <BoxCollider2D>().offset.y); // Reposition windRect's x and y with windAreaOffset windRect.x += windAreaOffset.x; windRect.y += windAreaOffset.y; unitWidthQuarter = (spriteWidth / 32) / 4; unitHeightQuarter = (spriteHeight / 32) / 4; if (poolSize <= 0) { poolSize = Mathf.RoundToInt((windRect.width * windRect.height) / 150); poolSize = poolSize == 0 ? 1 : poolSize; } }
private void HandleForceProperties(GameObject gameObject, IDictionary <string, string> customProperties) { ForceAffector force = gameObject.AddComponent <ForceAffector>(); // X Force if (customProperties.ContainsKey("force:x")) { float val = -1; try { val = (float)System.Convert.ToDouble(customProperties["force:x"]); } catch (System.FormatException) { val = -1; Debug.LogError("force:x property formatted improperly"); } force.force.x = val; } // Y Force if (customProperties.ContainsKey("force:y")) { float val = -1; try { val = (float)System.Convert.ToDouble(customProperties["force:y"]); } catch (System.FormatException) { val = -1; Debug.LogError("force:y property formatted improperly"); } force.force.y = val; } }
void Update() { layers = WeatherManager.getWeatherLayer(WeatherManager.Instance.checkCurrentWeather(gameObject)) | 1 << LayerMask.NameToLayer("Default"); //LayerMask. //WeatherManager.WeatherType w = (WeatherManager.WeatherType)WeatherManager.Instance.checkCurrentWeather(gameObject); //string weatherString = ""; //switch (w) //{ // case WeatherManager.WeatherType.sun: // weatherString = "Sun"; // break; // case WeatherManager.WeatherType.snow: // weatherString = "Snow"; // break; // case WeatherManager.WeatherType.rain: // weatherString = "Rain"; // break; //} //layers = LayerMask.GetMask("Default", weatherString); Vector3 pos = transform.position; Vector3 inc = Vector2.one * 0.01f; Collider2D collider = GetComponent <Collider2D>(); Vector2 topRight = collider.bounds.center - collider.bounds.extents; //pos - GetComponent<Collider2D>().bounds.extents - inc; Vector2 botLeft = collider.bounds.center + collider.bounds.extents; //pos + GetComponent<Collider2D>().bounds.extents + inc; Collider2D[] colliders = Physics2D.OverlapAreaAll(topRight, botLeft, layers); // Forces of highest priority will be summed Force forces = new Force(); forces.priority = -1; // Highest priority will be chosen FrictionAffector friction = null; GravityAffector gravity = null; foreach (Collider2D c in colliders) { ForceAffector foa = c.GetComponent <ForceAffector>(); if (foa != null) { if (forces.priority == foa.priority) { forces.force += foa.force; } else if (foa.priority > forces.priority) { forces.priority = foa.priority; forces.force = foa.force; } } FrictionAffector fra = c.GetComponent <FrictionAffector>(); if (fra != null) { if (friction == null || fra.priority > friction.priority) { friction = fra; } } GravityAffector ga = c.GetComponent <GravityAffector>(); if (ga != null) { if (gravity == null || ga.priority > gravity.priority) { gravity = ga; } } } // Force externalForce = Vector3.zero; if (forces.priority > -1) { externalForce = forces.force; } // Friction if (GetComponent <Controller2D>().collisions.below) { moveSpeed = moveSpeedDefault; sprintMult = sprintMultDefault; groundDrag = groundDragDefault; airDrag = airDragDefault; if (friction != null) { if (friction.groundDrag >= 0) { groundDrag = friction.groundDrag; } if (friction.airDrag >= 0) { airDrag = friction.airDrag; } if (friction.maxMoveSpeed >= 0) { moveSpeed = friction.maxMoveSpeed; } if (friction.sprintMult >= 1) { sprintMult = friction.sprintMult; } } } // Air Drag //if (airDragAffect != null && airDragAffect.drag >= 0) // airDrag = airDragAffect.drag; // Gravity jumpHeight = jumpHeightDefault; timeToApex = timeToApexDefault; if (gravity != null) { jumpHeight = gravity.jumpHeight; timeToApex = gravity.timeToApex; } }
// Check if a particle is inside any collider on a weather layer that's currently in affect private void handleForces(WeatherManager.WeatherType t, ref ParticleSystem.Particle p, out bool inForceAccector, out bool inGravityAffector) { inForceAccector = false; inGravityAffector = false; Force externalForce = new Force(); externalForce.priority = -1; Collider2D[] col = Physics2D.OverlapPointAll(p.position, WeatherManager.getWeatherLayer(t)); foreach (Collider2D c in col) { ForceAffector foa = c.GetComponent <ForceAffector>(); if (foa != null) { inForceAccector = true; if (externalForce.priority == foa.priority) { externalForce.force += foa.force; p.velocity = externalForce.force; } else if (foa.priority > externalForce.priority) { externalForce.priority = foa.priority; externalForce.force = foa.force; } } GravityAffector ga = c.GetComponent <GravityAffector>(); if (ga != null) { inGravityAffector = true; jumpHeight = ga.jumpHeight; timeToJumpApex = ga.timeToApex; } else { jumpHeight = pal.jumpHeightDefault; timeToJumpApex = pal.timeToApexDefault; } } if (jumpHeight == 0 && timeToJumpApex == 0) { jumpHeight = pal.jumpHeightDefault; timeToJumpApex = pal.timeToApexDefault; } float gravityMod = 1; switch (t) { case WeatherManager.WeatherType.sun: gravityMod = dandyFall; break; case WeatherManager.WeatherType.rain: gravityMod = rainFall; break; case WeatherManager.WeatherType.snow: gravityMod = snowFall; break; } Vector3 velocity = p.velocity; gravity = -(2 * jumpHeight) / Mathf.Pow(timeToJumpApex, 2) * gravityMod; velocity.x = externalForce.force.x; // Reduce or increase gravity depending on the magnitude and direction of the external force float forceDiff = gravity + externalForce.force.y; float m = 0.05f; float forceMod = forceDiff > 0 ? Mathf.Pow(2, m * -forceDiff) : -Mathf.Pow(2, m * forceDiff) + 2; velocity.y = gravity * forceMod; p.velocity = velocity; }