Files
ScienceLab.Density/Assets/Scripts/Lesson_3/HighStrikerController.cs
2026-06-04 00:55:54 +03:00

86 lines
2.5 KiB
C#

using UnityEngine;
public class HighStrikerController : MonoBehaviour
{
public PlanetData[] planets;
public ToyRocketController rocket;
public StrikerLeverController newtonLever;
public StrikerLeverController lengthLever;
public int currentPlanet = 0;
public float maxDistanceY = 0f;
public Transform startRocketPosition;
public Transform endRocketPosition;
void Start()
{
SetupStriker();
}
void Update()
{
}
private void SetupStriker()
{
// Rocket initial setup
rocket.SetRocketMass(Random.Range(10f, 100f));
// Planets initial setup
maxDistanceY = Random.Range(20000f, 40000f);
foreach (PlanetData planet in planets)
{
float planetY = planet.planetFlightPoint.transform.localPosition.y;
float planetDist = Remap(planetY, startRocketPosition.localPosition.y, endRocketPosition.localPosition.y, 0f, maxDistanceY);
planet.SetupPlanet(planetDist);
}
SetupLevers();
}
private void SetupLevers()
{
PlanetData planet = planets[currentPlanet];
if (Random.Range(0, 100) < 50f) // If Newton Lever
{
lengthLever.isBlocked = true;
newtonLever.isBlocked = false;
lengthLever.springValue = (2 * (rocket.rocketMass / 1000f) * 9.81f * planet.planetDistanceM) / Random.Range(lengthLever.maxSpringValue * 0.25f, lengthLever.maxSpringValue * 0.9f);
} else // If Length Lever
{
lengthLever.isBlocked = false;
newtonLever.isBlocked = true;
newtonLever.springValue = (2 * (rocket.rocketMass / 1000f) * 9.81f * planet.planetDistanceM) / Random.Range(lengthLever.maxSpringValue * 0.25f, lengthLever.maxSpringValue * 0.9f);
}
}
public void FlyRocket()
{
float k = newtonLever.springValue;
float x = lengthLever.springValue / 100f;
float m = rocket.rocketMass / 1000f;
float height = (k * x * x) / (2f * m * 9.81f);
Vector3 flyTo = rocket.transform.localPosition;
Debug.Log(height);
flyTo.y = Remap(height, 0f, maxDistanceY / 100f, startRocketPosition.transform.localPosition.y, endRocketPosition.transform.localPosition.y);
rocket.transform.localPosition = flyTo;
}
private float Remap(float v, float inMin, float inMax, float outMin, float outMax)
{
return (v - inMin) / (inMax - inMin) * (outMax - outMin) + outMin;
}
}