private void OnAircraftChangePosition(Vector2 pos) { if (pos.x > -_screenSize.x / 2 && pos.x < _screenSize.x / 2 && pos.y > -_screenSize.y / 2 && pos.y < _screenSize.y / 2) { if (_aircraftOLdPosition != null) // can figure out aircraft velosity and so on { if (_rocketController.IsHitting(pos)) { OnHitting(); } else { //Debug.LogWarning(" pos x = " + pos.x + " y = " + pos.y + " _rocketPos " + _rocketController.Position.x + " " + _rocketController.Position.y); Vector2 LeaserTargetPosition = FigureOutLeaserTarget(pos, new Vector2(_aircraftOLdPosition.Value.x, _aircraftOLdPosition.Value.y), _rocketController.Position); _rocketController.OnLeaserTargetPositionChange(LeaserTargetPosition); _aircraftOLdPosition = pos; MainThreadRunner.AddTask(() => _leadingTarget.transform.gameObject.SetActive(true)); } } else { _aircraftOLdPosition = pos; } } else { OnLeftScreen(); } }
public void Fire(Transform obj) { _isFired = true; if (_leaserTargetPosition != null) { _currentPosition = new Vector3(0, 0, 0); Vector2 vectorRocket = _leaserTargetPosition.Value - _currentPosition; vectorRocket.Normalize(); _currentPosition = vectorRocket * Constants.RocketVelocity * Constants.PulseRepetitionInterval / 1000 * Constants.TimeScale; _timeOfPreviosPosition = System.DateTime.Now; // for renew rocket position on OnLeaserTargetPositionChange float distanceToTarget = Mathf.Sqrt(Mathf.Pow((_rocketView.transform.localPosition.x - _leaserTargetPosition.Value.x), 2) + Mathf.Pow((_rocketView.transform.localPosition.y - _leaserTargetPosition.Value.y), 2)); float timeToTarget = distanceToTarget / Constants.RocketVelocity; //Debug.LogWarning("distanceToTarget= " + distanceToTarget + " timeToTarget " + timeToTarget); MainThreadRunner.AddTask(() => { _rocketView.transform.localPosition = new Vector3(0, 0, 0); HOTween.Kill(_rocketView.transform); Vector3 velocity = new Vector3(_leaserTargetPosition.Value.x, _leaserTargetPosition.Value.y, 0) - _rocketView.transform.position; _rocketView.transform.rotation = Quaternion.LookRotation(velocity, Vector3.forward); _rocketView.gameObject.SetActive(true); HOTween.To(_rocketView.transform, timeToTarget / Constants.TimeScale, new TweenParms().Prop("localPosition", new Vector3(_leaserTargetPosition.Value.x, _leaserTargetPosition.Value.y, 0)).Ease(EaseType.Linear)); // HOTween.To(_rocketView.transform, Constants.PulseRepetitionInterval / 1000 , //new TweenParms().Prop("localPosition", new Vector3(_currentPosition.x, _currentPosition.y, 0)).Ease(EaseType.Linear)); }); } }
private void Runnable() { if (!_isRunnig) { try { _isRunnig = true; Vector2 position = _trajectory.GenerateCoord(); MainThreadRunner.AddTask(() => { HOTween.Kill(_aircraftView.transform); var velocity = new Vector3(position.x, position.y, 0) - _aircraftView.transform.localPosition; float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); _aircraftView.transform.rotation = q; //Debug.LogWarning("rotation" + _aircraftView.transform.rotation.x + " " + _aircraftView.transform.rotation.y + " " + _aircraftView.transform.rotation.z); HOTween.To(_aircraftView.transform, Constants.PulseRepetitionInterval / 1000.0f, new TweenParms().Prop("localPosition", new Vector3(position.x, position.y, 0)).Ease(EaseType.Linear)); }); if (ChangePosition != null) { ChangePosition(position); } _isRunnig = false; } catch (Exception e) { Debug.LogWarning(" AircraftController Runnable" + e.ToString()); } } }
public void Stop() { _isFired = false; _currentPosition = Vector2.zero; MainThreadRunner.AddTask(() => { HOTween.Kill(_rocketView.transform); _rocketView.transform.localPosition = new Vector3(0, 0, 0); _rocketView.gameObject.SetActive(false); }); }
public void Stop() { _scheduledTask.Stop(); _isRunnig = false; MainThreadRunner.AddTask(() => { _aircraftView.gameObject.SetActive(false); HOTween.Kill(_aircraftView.transform); }); }
public void Run() { _trajectory = _trajectoryFactory.CreateTrajectory(); _currentPoint = _trajectory.StartPoint; MainThreadRunner.AddTask(() => { _aircraftView.gameObject.SetActive(true); _aircraftView.transform.localPosition = new Vector3(_currentPoint.x, _currentPoint.y, 0); } ); _scheduledTask.Start(); }
private void OnHitting() { Debug.LogWarning("Aircraft was hitted"); _aircraftOLdPosition = null; _aircraftController.Stop(); _rocketController.Stop(); MainThreadRunner.AddTask(() => { _airDefense.FireButton.interactable = true; _leadingTarget.transform.gameObject.SetActive(false); }); _aircraftController.Run(); }
private Vector2 FigureOutLeaserTarget(Vector2 aircraftPos, Vector2 aircraftOldPos, Vector2 rocketPos) { float koef = (Constants.PulseRepetitionInterval / 1000.0f) * Constants.TimeScale; float aircraftVelocity = Mathf.Sqrt(Mathf.Pow(((aircraftOldPos.x - aircraftPos.x) / (koef)), 2) + Mathf.Pow(((aircraftOldPos.y - aircraftPos.y) / (koef)), 2)); Vector2 vectorFromRocket = aircraftPos - rocketPos; Vector2 vectorAircraft = aircraftPos - aircraftOldPos; float angle = -AngleBetweenVector(vectorAircraft, vectorFromRocket); float a = aircraftVelocity / Constants.RocketVelocity; float timeToGetTarget; float distanceForTarget = Mathf.Sqrt(Mathf.Pow(((aircraftPos.x - rocketPos.x)), 2) + Mathf.Pow(((aircraftPos.y - rocketPos.y)), 2)); timeToGetTarget = distanceForTarget / ( Constants.RocketVelocity * Mathf.Sqrt(1 - a * a * Mathf.Sin(angle * Mathf.PI / 180) * Mathf.Sin(angle * Mathf.PI / 180)) - aircraftVelocity * Mathf.Cos(angle * Mathf.PI / 180) ); // Debug.LogWarning("_____________timeToGetTarget = " + timeToGetTarget + " distance " + distanceForTarget); Vector2 delta = new Vector2 ( timeToGetTarget * aircraftVelocity * Mathf.Cos(angle * Mathf.PI / 180), timeToGetTarget * aircraftVelocity * Mathf.Sin(angle * Mathf.PI / 180) ); vectorAircraft.Normalize(); var vectorFromAircraftToLeaserTarget = vectorAircraft * timeToGetTarget * aircraftVelocity; MainThreadRunner.AddTask(() => _leadingTarget.transform.localPosition = new Vector3(aircraftPos.x + vectorFromAircraftToLeaserTarget.x, aircraftPos.y + vectorFromAircraftToLeaserTarget.y, 0)); // Debug.LogWarning("_____________ vectorFromAircraftToLeaserTarget. = " + vectorFromAircraftToLeaserTarget.x + " y " + vectorFromAircraftToLeaserTarget.y); return(new Vector2(aircraftPos.x + vectorFromAircraftToLeaserTarget.x, aircraftPos.y + vectorFromAircraftToLeaserTarget.y)); }
public void OnLeaserTargetPositionChange(Vector2 pos) { _leaserTargetPosition = pos; if (_isFired) { // figure out the current rocket position TimeSpan span = (System.DateTime.Now - _timeOfPreviosPosition); int differInMs = (int)span.TotalMilliseconds; //Debug.LogWarning(" delta time= " + i); Vector2 vectorRocket = _leaserTargetPosition.Value - _currentPosition; vectorRocket.Normalize(); _currentPosition += vectorRocket * Constants.RocketVelocity * differInMs / 1000 * Constants.TimeScale; _timeOfPreviosPosition = System.DateTime.Now; MainThreadRunner.AddTask(() => { HOTween.Kill(_rocketView.transform); //https://answers.unity.com/questions/585035/lookat-2d-equivalent-.html var velocity = new Vector3(_leaserTargetPosition.Value.x, _leaserTargetPosition.Value.y, 0) - _rocketView.transform.localPosition; float angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg; Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward); _rocketView.transform.localRotation = q; float distanceToTarget = Mathf.Sqrt(Mathf.Pow((_rocketView.transform.localPosition.x - _leaserTargetPosition.Value.x), 2) + Mathf.Pow((_rocketView.transform.localPosition.y - _leaserTargetPosition.Value.y), 2)); float timeToTarget = distanceToTarget / Constants.RocketVelocity; //Debug.LogWarning("distanceToTarget= " + distanceToTarget + " timeToTarget " + timeToTarget); HOTween.To(_rocketView.transform, timeToTarget / Constants.TimeScale, new TweenParms().Prop("localPosition", new Vector3(_leaserTargetPosition.Value.x, _leaserTargetPosition.Value.y, 0)).Ease(EaseType.Linear)); // HOTween.To(_rocketView.transform, Constants.PulseRepetitionInterval / 1000 , //new TweenParms().Prop("localPosition", new Vector3(_currentPosition.x, _currentPosition.y, 0)).Ease(EaseType.Linear)); }); } }