コード例 #1
0
ファイル: Enemy.cs プロジェクト: 2school4cool/LudumDare2019
        // Calculate where the enemy should move based on player position and nearby enemies
        private void CalculateMovement()
        {
            var selfPosition   = transform.position;
            var selfVector     = new Vector2(selfPosition.x, selfPosition.y);
            var playerPosition = _player.transform.position;
            var targetVector   = new Vector2(playerPosition.x, playerPosition.y) - selfVector;

            targetVector.Normalize();
            targetVector *= DefaultSpeed * Time.fixedDeltaTime;

            // Distance self from other enemies
            var enemies = GameObject.FindGameObjectsWithTag("Enemy");
            var force   = Vector2.zero;

            foreach (var enemy in enemies)
            {
                if (enemy == this.gameObject)
                {
                    continue;
                }

                var position    = enemy.transform.position;
                var enemyVector = new Vector2(position.x, position.y);
                var deltaVector = selfVector - enemyVector;

                if (deltaVector.magnitude > 10f)
                {
                    continue;
                }

                // Avoid 0 issues by adding random offset
                if (Math.Abs(deltaVector.magnitude) < 0.001)
                {
                    enemy.transform.position += new Vector3(
                        UnityEngine.Random.Range(1, 2) / 1000f,
                        UnityEngine.Random.Range(1, 2) / 1000f,
                        0);
                    deltaVector = selfVector - enemyVector;
                }
                force += 0.1f / (deltaVector.magnitude * deltaVector.magnitude) * deltaVector;
            }

            targetVector += force;

            var topSpeed = TopSpeed;
            var speed    = Math.Min(targetVector.magnitude, topSpeed);

            targetVector.Normalize();
            targetVector.Scale(new Vector2(speed, speed));

            if (float.IsNaN(targetVector.x) || float.IsNaN(targetVector.y))
            {
                return;
            }

            transform.position += new Vector3(targetVector.x, targetVector.y, 0);
        }
コード例 #2
0
    void FixedUpdate()
    {
        if (movable)
        {
            offset = transform.Find("Hitbox Center").transform.position - transform.position;
        }

        // Get the scale of the transform
        // Get the direction the hit box is facing and multiply it component-wise with the scale
        // Multiply the size and scale so that if the parent is scaled up or down, the hitbox scales with it.
        Vector2 scale        = m_DamagerTransform.lossyScale;
        Vector2 facingOffset = Vector2.Scale(offset, scale);

        scaledSize = Vector2.Scale(size, scale);

        // From the hitbox rectangle, get one corner and the corner opposite it for the OverlapArea function
        // Point A is the position of the transform, moved left or right by facingOffset (this is the center of the hitbox
        // then moved to the corned by subtracting half the length of the scaledSize in the x and y directions.
        // Point B is just Point A plus the size in the x and y direction of the scaledSize vector.
        pointA = (Vector2)m_DamagerTransform.position + facingOffset - scaledSize * 0.5F;
        pointB = pointA + scaledSize;

        // pointADebug.transform.position = pointA;
        // pointBDebug.transform.position = pointB;

        // print("A: " + pointA + " B: " + pointB);

        if (!canDamage)
        {
            return;
        }

        int hitCount = Physics2D.OverlapArea(pointA, pointB, m_AttackContactFilter, m_AttackOverlapResults);

        // print(hitCount);

        for (int i = 0; i < hitCount; ++i)
        {
            Damageable damageable = m_AttackOverlapResults[i].GetComponent <Damageable>();

            if (damageable && canDamage)
            {
                // OnDamageableEvent.Invoke(this, damageable);
                damageable.OnTakeDamage.Invoke(this, damageable);
                canDamage = false;
                if (!indefiniteBackoff)
                {
                    StartCoroutine(BackoffCallback());
                }
            }
            else
            {
                // OnNonDamageableEvent.Invoke(this);
            }
        }
    }
コード例 #3
0
    static bool Vector2_Scale__Vector2(JSVCall vc, int argc)
    {
        int len = argc;

        if (len == 1)
        {
            UnityEngine.Vector2 arg0    = (UnityEngine.Vector2)JSApi.getVector2S((int)JSApi.GetType.Arg);
            UnityEngine.Vector2 argThis = (UnityEngine.Vector2)vc.csObj;        argThis.Scale(arg0);
            JSMgr.changeJSObj(vc.jsObjID, argThis);
        }

        return(true);
    }
コード例 #4
0
        void IDragHandler.OnDrag([NotNull] PointerEventData eventData)
        {
            if (targetTransform == null)
            {
                return;
            }

            float scale = 1f / ModuleListPanel.CanvasScale;
            var   size  = targetTransform.sizeDelta + Vector2.Scale(eventData.delta, new Vector2(scale, -scale));

            targetTransform.sizeDelta = Vector2.Max(size, minSize);
            ScaleChanged?.Invoke();
            //Debug.Log(targetTransform.sizeDelta);
        }
コード例 #5
0
 static public int Scale(IntPtr l)
 {
     try{
         UnityEngine.Vector2 self = (UnityEngine.Vector2)checkSelf(l);
         UnityEngine.Vector2 a1;
         checkType(l, 2, out a1);
         self.Scale(a1);
         setBack(l, self);
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
コード例 #6
0
 static int Scale(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         UnityEngine.Vector2 obj  = (UnityEngine.Vector2)ToLua.CheckObject(L, 1, typeof(UnityEngine.Vector2));
         UnityEngine.Vector2 arg0 = (UnityEngine.Vector2)ToLua.CheckObject(L, 2, typeof(UnityEngine.Vector2));
         obj.Scale(arg0);
         ToLua.SetBack(L, 1, obj);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #7
0
    // Update is called once per frame
    void Update()
    {
        bool willmove = !lookOnRightClick || Input.GetMouseButton(1);

        if (willmove)
        {
            Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
            mouseDelta = Vector2.Scale(mouseDelta, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
            // the interpolated float result between the two float values
            smoothV.x  = Mathf.Lerp(smoothV.x, mouseDelta.x, 1f / smoothing);
            smoothV.y  = Mathf.Lerp(smoothV.y, mouseDelta.y, 1f / smoothing);
            mouseLook += smoothV;

            transform.localRotation           = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
            character.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, character.transform.up);
        }
    }
コード例 #8
0
        /**
         * The coordinate point set must be length 4
         * <p>
         * Notice that C# uses pass by value; use; x.v = PopulateLineSegment(x.v);
         */
        public static void PopulateLineSegment(Vector3[] points, Vector2 start, Vector2 end, float startWidth, float endWidth)
        {
            var q = end - start;
              var x = new Vector3(q [1], -q [0], 0);
              var w1 = startWidth;
              var w2 = endWidth;

              /* Some kind of weird issue prevents unity from rendering tiny width meshes.
               * This is minimum size of a half-strip-width that seems to render ok. */
              var minStripWidth = 0.01;

              /* Start point */
              x.Normalize();
              var y = new Vector2(x[0], x[1]);
              y.Scale(new Vector3(w1 / 2.0f, w1 / 2.0f, 0f));
              if (y.magnitude < minStripWidth) {
            y = new Vector2(x[0], x[1]);
            y[0] = (float) ((double) y[0] * minStripWidth);
            y[1] = (float) ((double) y[1] * minStripWidth);
              }
              points[0] = new Vector3(start [0] + y[0], start [1] + y[1], 0);
              points[1] = new Vector3(start [0] - y[0], start [1] - y[1], 0);

              /* End point */
              y = new Vector2(x[0], x[1]);
              y.Scale(new Vector3(w2 / 2.0f, w2 / 2.0f, 0f));
              if (y.magnitude < minStripWidth) {
            y = new Vector2(x[0], x[1]);
            y[0] = (float) ((double) y[0] * minStripWidth);
            y[1] = (float) ((double) y[1] * minStripWidth);
              }
              points[2] = new Vector3(end [0] + y[0], end[1] + y[1], 0);
              points[3] = new Vector3(end [0] - y[0], end[1] - y[1], 0);
        }
コード例 #9
0
        private void UpdateLine()
        {
            _size = size;

            float totalDistance = 0;
            Vector3 lastPosition = Vector3.zero;

            List<Vector3> vertices = new List<Vector3>();
            List<Vector2> uvs = new List<Vector2>();
            List<Vector3> normals = new List<Vector3>();
            List<int> triangles = new List<int>();

            List<Vector3> positions = new List<Vector3>();

            for (int i = 0; i < coords.Length; i++)
            {
                // Get world position by coordinates
                Vector3 position = OnlineMapsTileSetControl.instance.GetWorldPosition(coords[i]);
                positions.Add(position);

                if (i != 0)
                {
                    // Calculate angle between coordinates.
                    float a = OnlineMapsUtils.Angle2DRad(lastPosition, position, 90);

                    // Calculate offset
                    Vector3 off = new Vector3(Mathf.Cos(a) * size, 0, Mathf.Sin(a) * size);

                    // Init verticles, normals and triangles.
                    int vCount = vertices.Count;

                    vertices.Add(lastPosition + off);
                    vertices.Add(lastPosition - off);
                    vertices.Add(position + off);
                    vertices.Add(position - off);

                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);
                    normals.Add(Vector3.up);

                    triangles.Add(vCount);
                    triangles.Add(vCount + 3);
                    triangles.Add(vCount + 1);
                    triangles.Add(vCount);
                    triangles.Add(vCount + 2);
                    triangles.Add(vCount + 3);

                    totalDistance += (lastPosition - position).magnitude;
                }

                lastPosition = position;
            }

            float tDistance = 0;

            for (int i = 1; i < positions.Count; i++)
            {
                float distance = (positions[i - 1] - positions[i]).magnitude;

                // Updates UV
                uvs.Add(new Vector2(tDistance / totalDistance, 0));
                uvs.Add(new Vector2(tDistance / totalDistance, 1));

                tDistance += distance;

                uvs.Add(new Vector2(tDistance / totalDistance, 0));
                uvs.Add(new Vector2(tDistance / totalDistance, 1));
            }

            // Update mesh
            mesh.vertices = vertices.ToArray();
            mesh.normals = normals.ToArray();
            mesh.uv = uvs.ToArray();
            mesh.triangles = triangles.ToArray();

            // Scale texture
            Vector2 scale = new Vector2(totalDistance / size, 1);
            scale.Scale(uvScale);
            meshRenderer.material.mainTextureScale = scale;
        }
コード例 #10
0
        public static void TestScaleBy()
        {
            const float floatScale = 4.8f;
            const float floatStart = 1.2f;
            float floatVal = floatStart;
            Ref<float> floatRef = new Ref<float>(
            () => floatVal,
            t => floatVal = t
            );

            const double doubleScale = 3.2;
            const double doubleStart = 9.2;
            double doubleVal = doubleStart;
            Ref<double> doubleRef = new Ref<double>(
            () => doubleVal,
            t => doubleVal = t
            );

            Vector2 vec2Scale = new Vector2(9.5f, 2.0f);
            Vector2 vec2Start = new Vector2(4.0f, 5.0f);
            Vector2 vec2Val = vec2Start;
            Ref<Vector2> vec2Ref = new Ref<Vector2>(
            () => vec2Val,
            t =>  vec2Val = t
            );

            Vector3 vec3Scale = new Vector3(4.0f, 19.0f, 2.0f);
            Vector3 vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
            Vector3 vec3Val = vec3Start;
            Ref<Vector3> vec3Ref = new Ref<Vector3>(
            () =>  vec3Val,
            t => vec3Val = t
            );

            Vector4 vec4Scale = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
            Vector4 vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
            Vector4 vec4Val = vec4Start;
            Ref<Vector4> vec4Ref = new Ref<Vector4>(
            () => vec4Val,
            t => vec4Val = t
            );

            CommandQueue queue = new CommandQueue();
            queue.Enqueue(
            Commands.Repeat(2,
                Commands.Sequence(
                    Commands.Parallel(
                        Commands.ScaleBy(floatRef, floatScale, 1.0),
                        Commands.ScaleBy(doubleRef, doubleScale, 1.0),
                        Commands.ScaleBy(vec2Ref, vec2Scale, 1.0),
                        Commands.ScaleBy(vec3Ref, vec3Scale, 1.0),
                        Commands.ScaleBy(vec4Ref, vec4Scale, 1.0)
                    ),
                    Commands.WaitForFrames(1)
                )
            )
            );

            queue.Update(0.2f);

            Vector2 vec2ExpectedScale = vec2Scale;
            Vector3 vec3ExpectedScale = vec3Scale;
            Vector4 vec4ExpectedScale = vec4Scale;
            vec2ExpectedScale.Scale(new Vector2(0.2f, 0.2f));
            vec3ExpectedScale.Scale(new Vector3(0.2f, 0.2f, 0.2f));
            vec4ExpectedScale.Scale(new Vector4(0.2f, 0.2f, 0.2f, 0.2f));
            vec2ExpectedScale += new Vector2(0.8f, 0.8f);
            vec3ExpectedScale += new Vector3(0.8f, 0.8f, 0.8f);
            vec4ExpectedScale += new Vector4(0.8f, 0.8f, 0.8f, 0.8f);
            vec2ExpectedScale.Scale(vec2Start);
            vec3ExpectedScale.Scale(vec3Start);
            vec4ExpectedScale.Scale(vec4Start);
            AreEqual(floatVal, floatStart * (0.8f + floatScale * 0.2f), 0.001f);
            AreEqual(doubleVal, doubleStart * (0.8 + doubleScale * 0.2), 0.001f);
            AreEqual(vec2Val, vec2ExpectedScale, 0.001f);
            AreEqual(vec3Val, vec3ExpectedScale, 0.001f);
            AreEqual(vec4Val, vec4ExpectedScale, 0.001f);

            queue.Update(0.8);
            vec2ExpectedScale = vec2Scale;
            vec3ExpectedScale = vec3Scale;
            vec4ExpectedScale = vec4Scale;
            vec2ExpectedScale.Scale(vec2Start);
            vec3ExpectedScale.Scale(vec3Start);
            vec4ExpectedScale.Scale(vec4Start);
            AreEqual(floatVal,  floatStart * floatScale, 0.001f);
            AreEqual(doubleVal,  doubleStart * doubleScale, 0.001f);
            AreEqual(vec2Val, vec2ExpectedScale, 0.001f);
            AreEqual(vec3Val, vec3ExpectedScale, 0.001f);
            AreEqual(vec4Val, vec4ExpectedScale, 0.001f);

            floatVal = floatStart;
            doubleVal = doubleStart;
            vec2Val = vec2Start;
            vec3Val = vec3Start;
            vec4Val = vec4Start;
            queue.Update(0.0);
            queue.Update(0.5);
            vec2ExpectedScale = vec2Scale;
            vec3ExpectedScale = vec3Scale;
            vec4ExpectedScale = vec4Scale;
            vec2ExpectedScale.Scale(new Vector2(0.5f, 0.5f));
            vec3ExpectedScale.Scale(new Vector3(0.5f, 0.5f, 0.5f));
            vec4ExpectedScale.Scale(new Vector4(0.5f, 0.5f, 0.5f, 0.5f));
            vec2ExpectedScale += new Vector2(0.5f, 0.5f);
            vec3ExpectedScale += new Vector3(0.5f, 0.5f, 0.5f);
            vec4ExpectedScale += new Vector4(0.5f, 0.5f, 0.5f, 0.5f);
            vec2ExpectedScale.Scale(vec2Start);
            vec3ExpectedScale.Scale(vec3Start);
            vec4ExpectedScale.Scale(vec4Start);
            AreEqual(floatVal, floatStart * (0.5f + floatScale * 0.5f), 0.001f);
            AreEqual(doubleVal, doubleStart * (0.5 + doubleScale * 0.5), 0.001f);
            AreEqual(vec2Val, vec2ExpectedScale, 0.001f);
            AreEqual(vec3Val, vec3ExpectedScale, 0.001f);
            AreEqual(vec4Val, vec4ExpectedScale, 0.001f);
        }
コード例 #11
0
 public static Vector2 Multiply(this UnityEngine.Vector2 me, UnityEngine.Vector2 other)
 {
     me.Scale(other);
     return(me);
 }
コード例 #12
0
 public static Vector2 Divide(this UnityEngine.Vector2 me, UnityEngine.Vector2 other)
 {
     me.Scale(new UnityEngine.Vector2(1 / other.x, 1 / other.y));
     return(me);
 }