public void OnInputMove(InputAction.CallbackContext context)
        {
            if (null == node)
            {
                return;
            }

            // Don't use Node2D Velocity (it is constant). Use rigidbody velocity, which decays
            // This results in more fluid movement
            node.VelocityType = Node2D.MoveType.None;

            //Debug.Log("Input Move: " + context.ToString());

            var angleAxisLimiter = new AngleAxisLimiter2D(axisLimit);
            var rigidbody        = node.GetComponent <Rigidbody2D>();

            var axisVector = context.ReadValue <Vector2>();
            var angle      = AngleUtils.Vector2ToDegreeAngle(axisVector);
            var distance   = AngleUtils.Distance(Vector2.zero, axisVector);

            if (distance <= deadZone)
            {
                rigidbody.velocity = Vector2.zero;
                return;
            }

            var factor             = (distance - deadZone) / (1.0f - deadZone);
            var interpolatedFactor = interpolate.Transform(factor);
            var moveSpeed          = minMoveSpeed + ((maxMoveSpeed - minMoveSpeed) * interpolatedFactor);

            var limitedAngle = angleAxisLimiter.LimitAngle(angle);
            var velocity     = AngleUtils.DegreeAngleToVector2(limitedAngle, moveSpeed);

            rigidbody.velocity = velocity;
        }
Beispiel #2
0
        /// <summary>
        /// Translate a degree angle to a map direction
        /// </summary>
        public static MapDirection DegreeAngleToMapDirection(float degreeAngle, MapDirectionAxisLimit axisLimit)
        {
            var axisLimitNumber = 4;

            switch (axisLimit)
            {
            case MapDirectionAxisLimit.EightWay:
                axisLimitNumber = 8;
                break;
            }

            var angleAxisLimiter = new AngleAxisLimiter2D(axisLimitNumber);
            var limitedAngle     = angleAxisLimiter.LimitAngle(degreeAngle);

            var result = MapDirection.North;

            switch (Mathf.RoundToInt(limitedAngle))
            {
            case 0:
            case 360:
                return(MapDirection.North);

            case 45:
                return(MapDirection.Northeast);

            case 90:
                return(MapDirection.East);

            case 135:
                return(MapDirection.Southeast);

            case 180:
                return(MapDirection.South);

            case 225:
                return(MapDirection.Southwest);

            case 270:
                return(MapDirection.West);

            case 315:
                return(MapDirection.Northwest);
            }

            return(result);
        }
Beispiel #3
0
        public void TestAngleAxisLimiter_4Way()
        {
            var limiter = new AngleAxisLimiter2D(4);

            Assert.AreEqual(0, limiter.LimitAngle(0));
            Assert.AreEqual(0, limiter.LimitAngle(44));
            Assert.AreEqual(0, limiter.LimitAngle(-44));

            Assert.AreEqual(90, limiter.LimitAngle(46));
            Assert.AreEqual(90, limiter.LimitAngle(90));
            Assert.AreEqual(90, limiter.LimitAngle(134));

            Assert.AreEqual(180, limiter.LimitAngle(136));
            Assert.AreEqual(180, limiter.LimitAngle(180));
            Assert.AreEqual(180, limiter.LimitAngle(214));

            Assert.AreEqual(270, limiter.LimitAngle(226));
            Assert.AreEqual(270, limiter.LimitAngle(270));
            Assert.AreEqual(270, limiter.LimitAngle(314));

            Assert.AreEqual(360, limiter.LimitAngle(316));
            Assert.AreEqual(360, limiter.LimitAngle(360));
        }