示例#1
0
        private void RotateTowardPosition(AIShipComp nc, Vector3 targetPosition)
        {
            var pc         = nc.GetComponent <PositionComp>().transform;
            var moveVector = targetPosition - pc.position;

            pc.rotation =
                ZenMath.QuaternionUtil.SlerpLookAtTarget(pc, moveVector,
                                                         pc.GetComponent <ShipComp>().CurrentRotationSpeed *
                                                         Time.deltaTime);
        }
        //public GameObject[] targets { get; private set; }

        public ShipContext(Entity inEntity)
            : base(inEntity)
        {
            positionComp = entity.GetComponent <PositionComp>();
            transform    = positionComp.transform;
            targetComp   = entity.GetComponent <TargetComp>();
            scannerComp  = entity.GetComponent <ScannerComp>();
            commandComp  = entity.GetComponent <CommandComp>();
            rbComp       = entity.GetComponent <RigidbodyComp>()?.rigidbody;
            AiShipComp   = entity.GetComponent <AIShipComp>();
        }
示例#3
0
        private void MoveTowardPosition(AIShipComp nc, Vector3 moveToPosition)
        {
            //ZenGizmosDebug.Instance.targetPosition = moveToPosition;
            var pc = nc.GetComponent <PositionComp>().transform;
            var sc = nc.GetComponent <ShipComp>();

            pc.rotation =
                //ZenMath.QuaternionUtil.SlerpLookAtTarget(pc, moveVector, 5f /*Rotation speed*/* Time.deltaTime);
                ZenMath.QuaternionUtil.SlerpLookAtTarget(pc, moveToPosition, 0.5f /*Rotation speed*/ * Time.deltaTime);

            //float faceAngle = Vector3.Dot(pc.forward, moveVector.normalized);
            Debug.DrawLine(pc.position, moveToPosition, Color.yellow);
            //if (faceAngle > 0.75) // only accelerate if pointing in the right general direction
            //{
            //    nc.GetComponent<RigidbodyComp>().rigidbody.AddForce(
            //                                                    pc.forward *
            //                                                    nc.GetComponent<ShipComp>().CurrentAcceleration  * 60f *
            //                                                    Time.deltaTime);
            //}
            var rb = nc.GetComponent <RigidbodyComp>().rigidbody;
            //calc force
            Vector3 force = CalculateForce(
                pc.position,
                rb.velocity,
                moveToPosition,
                Vector3.zero);                            // #TODO: Change this zero to a next waypoint direction velocity
            //rb.AddForce(force, ForceMode.VelocityChange);
            Vector3 v             = (force / rb.mass) * Time.deltaTime * sc.CurrentAcceleration / 60f;
            Vector3 totalVelocity = rb.velocity + v;
            Vector3 limitedV      = Vector3.ClampMagnitude(totalVelocity, sc.CurrentMaxSpeed);

            rb.velocity = limitedV;

            //float maxspeed = nc.GetComponent<ShipComp>().CurrentMaxSpeed;
            //getting close to target
            if ((pc.position - moveToPosition).sqrMagnitude < 50.1f)
            {
                //Debug.Log("Close to target");
                nc.Navigation.HasReachedTarget = true;
            }
            else
            {
                ZenLogger.LogGame($"Travel distance: {(pc.position - moveToPosition).sqrMagnitude}");
            }
        }
示例#4
0
        private void UpdateOrbit(AIShipComp nc)
        {
            Vector3 moveto = nc.GetComponent <TargetComp>().target.position + nc.Navigation.TargetPositionOffset;

            MoveTowardPosition(nc, moveto);
        }
示例#5
0
 private void UpdateApproach(AIShipComp nc)
 {
     MoveTowardPosition(nc, nc.GetComponent <TargetComp>().target.position);
 }
示例#6
0
 private void UpdateAttacking(AIShipComp nc)
 {
     RotateTowardPosition(nc, nc.GetComponent <TargetComp>().target.position);
 }