// Driving to the end point void Driving() { if (Condition.IsDriving) { if (Condition.IsCrossWalkDetected && Condition.IsGreenLightOn) { Condition.IsDriving = false; Condition.IsWaiting = true; State = CarStates.kCarState_Waiting; } else { // Test if (!Condition.IsOtherCarNear) { PathfindingMovement(); } //RB.MovePosition(transform.position + (transform.forward * Time.deltaTime * Speed)); } } else { Condition.IsSearching = true; State = CarStates.kCarState_Searching; } }
// Start is called before the first frame update void Start() { //Patrón singleton. if (GameController.instance.CarControl == null) { GameController.instance.CarControl = this; } else { Destroy(gameObject); } CarState = CarStates.Stopped; //El auto empieza detenido. //Se obtienen valores desde los parámetros del juego. maxDurability = GameController.instance.Parameters.GetMaxCarDurability(); durability = maxDurability; maxSpeed = GameController.instance.Parameters.GetMaxCarSpeed(); acceleration = GameController.instance.Parameters.GetCarAcceleration(); brakeForce = GameController.instance.Parameters.GetCarBrakeForce(); steeringSpeed = GameController.instance.Parameters.GetCarSteeringSpeed(); powerEnabled = GameController.instance.Parameters.GetCarPowerEnabled(); powerDuration = GameController.instance.Parameters.GetCarPowerDuration(); powerCooldown = GameController.instance.Parameters.GetCarPowerCooldown(); powerReady = true; //El poder especial comienza listo para usarse. shield.GetComponent <Renderer>().material.renderQueue++; //El material del escudo se debe renderizar por sobre el auto cuando usa los materiales translúcidos. }
public void GrabSuccessors(CarState grabber, int onLap) { foreach (var cs in GrabbingBag) { if (grabber.UsedRequiredTypesOfTyres) { if (!cs.UsedRequiredTypesOfTyres) { continue; } if (cs.TyreSet.IsBrandNew) { cs.OnLap = onLap; grabber.AddSuccessor(cs); CarStates.Add(cs); } if (cs.TyreSet.State == grabber.TyreSet.State + 1 && cs.TyreSet.IsTheSameTyreType(grabber.TyreSet)) { cs.OnLap = onLap; grabber.AddSuccessor(cs); CarStates.Add(cs); } } else { if (cs.UsedRequiredTypesOfTyres) { if (cs.TyreSet.IsBrandNew && !cs.TyreSet.IsTheSameTyreType(grabber.TyreSet)) { cs.OnLap = onLap; grabber.AddSuccessor(cs); CarStates.Add(cs); } } else { if (!cs.TyreSet.IsTheSameTyreType(grabber.TyreSet)) { continue; } if (cs.TyreSet.IsBrandNew || cs.TyreSet.State == grabber.TyreSet.State + 1) { cs.OnLap = onLap; grabber.AddSuccessor(cs); CarStates.Add(cs); } } } } }
/// <summary> /// создается новая машина произвольного цвета. Ее состояние - ожидание въезда /// направление RouteTo задается произвольно /// /// </summary> public Car(Cell Location) { RouteTo = (Routes)(_rand.Next(1, 5)); RouteFrom = Location.Route; this.Location = Location; GoalState = CarStates.MoveToRing; //цвета машин ColorCar = _rand.Next(0, 4); }
// Searching for a new direction void Searching() { if (Condition.IsSearching) { GetNewPath(); GetNextLocationStep(); Condition.IsSearching = false; } else { Condition.IsDriving = true; State = CarStates.kCarState_Driving; } }
void SetCarState() { int temp = _sphereColliders.Count(c => c.isTouchingSurface); numWheelsSurface = temp; isAllWheelsSurface = numWheelsSurface >= 3; // All wheels are touching the ground if (isAllWheelsSurface) { carState = CarStates.AllWheelsSurface; } // Some wheels are touching the ground, but not the body if (!isAllWheelsSurface && !isBodySurface) { carState = CarStates.SomeWheelsSurface; } // We are lying on our side if (isBodySurface && !isAllWheelsSurface) { carState = CarStates.BodySideGround; } // All wheels on the ground if (isAllWheelsSurface && Vector3.Dot(Vector3.up, transform.up) > 0.95f) { carState = CarStates.AllWheelsGround; } // He is dead Jimmy! if (isBodySurface && Vector3.Dot(Vector3.up, transform.up) < -0.95f) { carState = CarStates.BodyGroundDead; } // In the air if (!isBodySurface && numWheelsSurface == 0) { carState = CarStates.Air; } isCanDrive = carState == CarStates.AllWheelsSurface || carState == CarStates.AllWheelsGround; }
// Waiting traffic light void Waiting() { if (Condition.IsWaiting) { if (Condition.CrossWalk.GetComponent <CrossWalkBehaviour>().GetCrossWalkStates == CrossWalkBehaviour.CrossWalkStates.kCrossWalkStates_RedLight) { Condition.IsWaiting = false; } else { transform.position = Condition.Position; } } else { Condition.IsGreenLightOn = false; Condition.IsDriving = true; State = CarStates.kCarState_Driving; } }
public void CreateCarStateTree() { for (int i = 1; i <= BeginningCarState.Car.Race.NumberOfLaps; i++) { List <CarState> removableCarStates = new List <CarState>(); removableCarStates.AddRange(CarStates); foreach (var cs in removableCarStates) { GrabSuccessors(cs, i); CarStates.Remove(cs); AllCarStates.Add(cs); NumberOfNodes++; } CreateGrabbingBag(); } Leaves.AddRange(CarStates); NumberOfNodes += CarStates.Count; AllCarStates.AddRange(Leaves); }
void Init() { State = CarStates.kCarState_Searching; Condition.IsSearching = true; Condition.IsDriving = false; Condition.IsGreenLightOn = false; Condition.IsCrossWalkDetected = false; Condition.IsReachedPoint = false; Condition.IsWaiting = false; Condition.IsOtherCarNear = false; Condition.IsNearObjective = false; Condition.DistanceFromObjective = 0.0f; Condition.Position = Vector3.zero; Condition.CrossWalk = null; // RB = GetComponent<Rigidbody>(); IsInitialized = true; }
public void AddCarState(CarState carState) { CarStates.Add(carState); CalculateTime(); }
public void SetCarState(CarStates newCarState) { carCurrentState = newCarState; HandleSounds(); }
public RacePlan(List <CarState> carStates) { CarStates.AddRange(carStates); TotalTime = new TimeSpan(); CalculateTime(); }
//Esta función destruye el auto, terminando la carrera antes de que se acabe el tiempo. Como comentarios se agregan posibles efectos visuales de este estado. private void DestroyCar() { CarState = CarStates.Destroyed; //car explosion //activate fire }
public static string Message(this CarStates value) { return(carStateDictionary[value]); }
//Función que aturde al auto, haciendo que baje su velocidad y rote sobre su eje por un tiempo determinado. private void StunCar() { CarState = CarStates.Stunned; carBody.GetComponent <Renderer>().materials = carMaterialsTransparent; //Como feedback visual, al entrar en este estado, el auto se pondrá de color transparente. }
//Función que detiene al auto. public void StopCar() { CarState = CarStates.Stopping; carBody.GetComponent <Renderer>().materials = carMaterialsOpaque; }
//Función que hace que el auto empiece a acelerar. public void StartCar() { CarState = CarStates.Accelerating; }
private void CarFSM() { //Finite State Machine que determina el comportamiento del auto según su estado. switch (CarState) { //Cuando está en estado "Stopping", se le aplica una fuerza negativa al auto. Una vez que su velocidad es 0, cambia a estado "Stopped". case CarStates.Stopping: if (rigidBody.velocity.z > 0) { rigidBody.AddForce(new Vector3(0, 0, -brakeForce), ForceMode.Acceleration); } else { CarState = CarStates.Stopped; } break; //Cuando está en estado "Stopped", se asegura que la velocidad del auto sea 0. case CarStates.Stopped: rigidBody.velocity = new Vector3(0, 0, 0); break; //Cuando el estado es "Accelerating", se aplica una aceleración positiva al auto, hasta llegar a su velocidad máxima. case CarStates.Accelerating: rigidBody.AddForce(new Vector3(0, 0, acceleration), ForceMode.Acceleration); Speed = rigidBody.velocity.z; if (rigidBody.velocity.z >= maxSpeed) { CarState = CarStates.Running; //Cuando el auto llega a su velocidad máxima, cambia su estado a "Running". } Steer(); //Mientras está en este estado, el auto puede doblar. break; //Cuando está en estado "Running" se asegura que su velocidad sea la velocidad máxima. case CarStates.Running: rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0, maxSpeed); Steer(); //En este estado el auto puede doblar. break; //Cuando está en este estado, el modelo del auto gira cada frame, se aplica una aceleración negativa hasta llegar a velocidad máxima dividida en el factor de stun. case CarStates.Stunned: carBody.transform.Rotate(new Vector3(0, 2, 0)); if (rigidBody.velocity.z > maxSpeed / stunFactor) { rigidBody.AddForce(new Vector3(0, 0, -brakeForce), ForceMode.Acceleration); } else { rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0, maxSpeed / stunFactor); } if (stunTimer >= stunDuration) //Además se activa el timer de stun, que al llegar a la duración, el auto vuelve a estado "Accelerating" y vuelve a sus materiales opacos. { carBody.GetComponent <Renderer>().materials = carMaterialsOpaque; CarState = CarStates.Accelerating; stunTimer = 0; } stunTimer += Time.deltaTime; break; //En este estado, el auto comienza a frenar y girar. al llegar a velocidad 0, se mantiene detenido. case CarStates.Destroyed: if (rigidBody.velocity.z > 0) { carBody.transform.Rotate(new Vector3(0, 1, 0)); rigidBody.AddForce(new Vector3(0, 0, -brakeForce), ForceMode.Acceleration); } else { rigidBody.velocity = new Vector3(0, 0, 0); } break; } }
/// <summary> /// логика движения машины /// </summary> public void TryMoveForward() { if (GoalState == CarStates.MoveToRing) { if (Location.RoadNextCell != null) { MoveNext(Location.RoadNextCell); } else if (Location.EntryOrDepartNext != null) { MoveNext(Location.EntryOrDepartNext); } if (Location.TypePosition == PosTypes.Ring) { GoalState = CarStates.EntryRing; } } else if (GoalState == CarStates.EntryRing) { if (Location.LineNumber == Envirmnt.Inst.Cross.LinesRing || Location.LineNumber >= NeedRingLineNumber) { GoalState = CarStates.MoveToDepart; } else { MoveNext(Location.EntryOrDepartNext); } } else if (GoalState == CarStates.MoveToDepart) { MoveNext(Location.RingNextCell); if (Location.TypeFunc == FuncTypes.Depart) { if (Location.X == 6 && Location.Y == 19) { } if (Location.Route == RouteTo) { GoalState = CarStates.DepartRing; } } } else if (GoalState == CarStates.DepartRing) { MoveNext(Location.EntryOrDepartNext); if (Location.TypePosition == PosTypes.Road) { GoalState = CarStates.DepartMap; } } else if (GoalState == CarStates.DepartMap) { if (Location.RoadNextCell != null) { MoveNext(Location.RoadNextCell); } else { //уничтожение машины Location.Car = null; Envirmnt.Inst.Cars.Remove(this); if (OnCarDestroy != null) { OnCarDestroy(this); } } } }