Ejemplo n.º 1
0
        protected virtual void FixedUpdate()
        {
            var currentPoint = (Vector2)transform.position;
            var vector       = targetPoint - currentPoint;

            if (vector.magnitude <= MinimumDistance)
            {
                ChangeTargetPoint();

                vector = targetPoint - currentPoint;
            }

            // Limit target speed
            if (vector.magnitude > MaximumSpeed)
            {
                vector = vector.normalized * MaximumSpeed;
            }

            // Acceleration
            if (body == null)
            {
                body = GetComponent <Rigidbody2D>();
            }

            var factor = D2dHelper.DampenFactor(Acceleration, Time.deltaTime);

            body.velocity = Vector2.Lerp(body.velocity, vector * SpeedBoost, factor);
        }
Ejemplo n.º 2
0
        private static float Dampen(float current, float target, float dampening, float elapsed, float minStep = 0.0f)
        {
            var factor   = D2dHelper.DampenFactor(dampening, elapsed);
            var maxDelta = Mathf.Abs(target - current) * factor + minStep * elapsed;

            return(Mathf.MoveTowards(current, target, maxDelta));
        }
        protected virtual void Update()
        {
            var localPosition = transform.localPosition;
            var targetX       = (Input.mousePosition.x - Screen.width / 2) * MoveScale;
            var targetY       = (Input.mousePosition.y - Screen.height / 2) * MoveScale;
            var factor        = D2dHelper.DampenFactor(MoveSpeed, Time.deltaTime);

            localPosition.x = Mathf.Lerp(localPosition.x, targetX, factor);
            localPosition.y = Mathf.Lerp(localPosition.y, targetY, factor);

            transform.localPosition = localPosition;

            // Left click?
            if (Input.GetMouseButtonDown(0) == true)
            {
                var mainCamera = Camera.main;

                if (MuzzlePrefab != null)
                {
                    Instantiate(MuzzlePrefab, transform.position, Quaternion.identity);
                }

                if (BulletPrefab != null && mainCamera != null)
                {
                    var position = mainCamera.ScreenToWorldPoint(Input.mousePosition);

                    Instantiate(BulletPrefab, position, Quaternion.identity);
                }
            }
        }
Ejemplo n.º 4
0
        protected virtual void Update()
        {
            // Required key is down?
            if (Input.GetKeyDown(Requires) == true)
            {
                // Main camera exists?
                var mainCamera = Camera.main;

                if (mainCamera != null)
                {
                    // World position of the mouse
                    var position = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);

                    // Read the destructible and alpha at this position
                    var destructible = default(D2dDestructible);
                    var alpha        = default(Color32);

                    if (D2dDestructible.TrySampleAlphaAll(position, ref destructible, ref alpha) == true)
                    {
                        Debug.Log("Read " + destructible + " with alpha: " + alpha);
                    }
                    else
                    {
                        Debug.Log("Read nothing.");
                    }
                }
            }
        }
Ejemplo n.º 5
0
        protected virtual void Update()
        {
            // Touching the screen?
            inputManager.Update(Requires);

            if (inputManager.Fingers.Count > 0)
            {
                // Main camera exists?
                var mainCamera = Camera.main;

                if (mainCamera != null)
                {
                    // World position of the mouse
                    var position = D2dHelper.ScreenToWorldPosition(inputManager.Fingers[0].PositionA, Intercept, mainCamera);

                    // Read the destructible and alpha at this position
                    var destructible = default(D2dDestructible);
                    var alpha        = default(Color32);

                    if (D2dDestructible.TrySampleAlphaAll(position, ref destructible, ref alpha) == true)
                    {
                        Debug.Log("Read " + destructible + " with alpha: " + alpha);
                    }
                    else
                    {
                        Debug.Log("Read nothing.");
                    }
                }
            }
        }
Ejemplo n.º 6
0
        protected virtual void FixedUpdate()
        {
            var newPosition  = transform.position;
            var rayLength    = (newPosition - oldPosition).magnitude;
            var rayDirection = (newPosition - oldPosition).normalized;
            var hit          = Physics2D.Raycast(oldPosition, rayDirection, rayLength, RaycastMask);

            // Update old position to trail behind
            if (rayLength > MaxLength)
            {
                rayLength   = MaxLength;
                oldPosition = newPosition - rayDirection * rayLength;
            }

            transform.localScale = MaxScale * D2dHelper.Divide(rayLength, MaxLength);

            if (hit.collider != null)
            {
                if (string.IsNullOrEmpty(IgnoreTag) == true || hit.collider.tag != IgnoreTag)
                {
                    if (ExplosionPrefab != null)
                    {
                        Instantiate(ExplosionPrefab, hit.point, Quaternion.identity);
                    }

                    Destroy(gameObject);
                }
            }
        }
        protected virtual void Update()
        {
            var factor = D2dHelper.DampenFactor(Dampening, Time.deltaTime);

            currentThrottle = Mathf.Lerp(currentThrottle, Throttle, factor);

            transform.localScale = MaxScale * Random.Range(1.0f - Flicker, 1.0f + Flicker) * currentThrottle;
        }
Ejemplo n.º 8
0
 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         var mainCamera = Camera.main;
         var startPos   = D2dHelper.ScreenToWorldPosition(Input.mousePosition, 0, mainCamera);
         Spawn(startPos);
     }
 }
Ejemplo n.º 9
0
        /// <summary>This will transform the current slicer between the input positions.</summary>
        public void SetTransform(Vector2 positionA, Vector2 positionB)
        {
            var scale = Vector2.Distance(positionB, positionA);
            var angle = D2dHelper.Atan2(positionB - positionA) * Mathf.Rad2Deg;

            // Transform the indicator so it lines up with the slice
            transform.position   = positionA;
            transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
            transform.localScale = new Vector3(Thickness, scale, scale);
        }
Ejemplo n.º 10
0
        protected virtual void Update()
        {
            // Update flipping if the game is playing
            if (Application.isPlaying == true)
            {
                cooldown -= Time.deltaTime;

                // Flip?
                if (cooldown <= 0.0f)
                {
                    FrontShowing = !FrontShowing;

                    ResetCooldown();
                }
            }

            // Get target angle based on flip state
            var targetAngle = FrontShowing == true ? 0.0f : 180.0f;

            // Slowly rotate to the target angle if the game is playing
            if (Application.isPlaying == true)
            {
                var factor = D2dHelper.DampenFactor(FlipSpeed, Time.deltaTime);

                angle = Mathf.Lerp(angle, targetAngle, factor);
            }
            // Instantly rotate if it's not
            else
            {
                angle = targetAngle;
            }

            transform.localRotation = Quaternion.Euler(0.0f, angle, 0.0f);

            // Make the destructible indestructible if it's past 90 degrees
            if (Destructible != null)
            {
                Destructible.Indestructible = targetAngle >= 90.0f;
            }

            // Update movement
            if (Application.isPlaying == true)
            {
                MoveProgress += MoveSpeed * Time.deltaTime;
            }

            var moveDistance = (EndPosition - StartPosition).magnitude;

            if (moveDistance > 0.0f)
            {
                var progress01 = Mathf.PingPong(MoveProgress / moveDistance, 1.0f);

                transform.localPosition = Vector3.Lerp(StartPosition, EndPosition, Mathf.SmoothStep(0.0f, 1.0f, progress01));
            }
        }
        protected virtual void LateUpdate()
        {
            if (Application.isPlaying == true)
            {
                var factor = D2dHelper.DampenFactor(FlashDampening, Time.deltaTime, 0.1f);

                Flash = Mathf.Lerp(Flash, 0.0f, factor);
            }

            cachedCanvasGroup.alpha = Flash > 0.005f ? Flash : 0.0f;
        }
Ejemplo n.º 12
0
        protected virtual void Update()
        {
            // Update input
            inputManager.Update(Requires);

            // Make sure the camera exists
            var camera = D2dHelper.GetCamera(null);

            if (camera != null)
            {
                // Loop through all non-gui fingers
                foreach (var finger in inputManager.Fingers)
                {
                    if (finger.StartedOverGui == false)
                    {
                        // Grab extra finger data and position
                        var link     = D2dInputManager.Link.FindOrCreate(ref links, finger);
                        var position = D2dHelper.ScreenToWorldPosition(finger.PositionA, Intercept, camera);

                        // Create indiactor?
                        if (finger.Down == true)
                        {
                            link.Start = position;

                            if (IndicatorPrefab != null)
                            {
                                link.Visual = Instantiate(IndicatorPrefab);

                                link.Visual.SetActive(true);
                            }
                        }

                        // Update indicator?
                        if (finger.Set == true && link.Visual != null)
                        {
                            var scale = Vector3.Distance(position, link.Start);
                            var angle = D2dHelper.Atan2(position - link.Start) * Mathf.Rad2Deg;

                            link.Visual.transform.position   = link.Start;
                            link.Visual.transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
                            link.Visual.transform.localScale = new Vector3(Thickness, scale, scale);
                        }

                        // Slice scene then clear link?
                        if (finger.Up == true)
                        {
                            D2dSlice.All(Paint, link.Start, position, Thickness, Shape, Color, Layers);

                            link.Clear();
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        protected virtual void Update()
        {
            var targetAngle = Input.GetAxisRaw("Horizontal") * SteerAngleMax;
            var factor      = D2dHelper.DampenFactor(SteerAngleDampening, Time.deltaTime);

            currentAngle = Mathf.Lerp(currentAngle, targetAngle, factor);

            for (var i = 0; i < SteerWheels.Length; i++)
            {
                SteerWheels[i].transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -currentAngle);
            }
        }
        protected virtual void Update()
        {
            // Get the main camera
            var mainCamera = Camera.main;

            // Begin dragging
            if (Input.GetKey(Requires) == true && down == false)
            {
                down = true;
                startMousePosition = Input.mousePosition;
            }

            // End dragging
            if (Input.GetKey(Requires) == false && down == true)
            {
                down = false;

                // Main camera exists?
                if (mainCamera != null)
                {
                    var endMousePosition = Input.mousePosition;
                    var startPos         = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                    var endPos           = D2dHelper.ScreenToWorldPosition(endMousePosition, Intercept, mainCamera);

                    D2dSlice.All(Paint, startPos, endPos, Thickness, Shape, Color, Layers);
                }
            }

            // Update indicator?
            if (down == true && mainCamera != null && IndicatorPrefab != null)
            {
                if (indicatorInstance == null)
                {
                    indicatorInstance = Instantiate(IndicatorPrefab);
                }

                var startPos   = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                var currentPos = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);
                var scale      = Vector3.Distance(currentPos, startPos);
                var angle      = D2dHelper.Atan2(currentPos - startPos) * Mathf.Rad2Deg;

                // Transform the indicator so it lines up with the slice
                indicatorInstance.transform.position   = startPos;
                indicatorInstance.transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
                indicatorInstance.transform.localScale = new Vector3(Thickness, scale, scale);
            }
            // Destroy indicator?
            else if (indicatorInstance != null)
            {
                Destroy(indicatorInstance.gameObject);
            }
        }
        protected virtual void LateUpdate()
        {
            var factor = D2dHelper.DampenFactor(ShakeDampening, Time.deltaTime, 0.1f);

            Shake = Mathf.Lerp(Shake, 0.0f, factor);

            var shakeStrength = Shake * ShakeScale;
            var shakeTime     = Time.time * ShakeSpeed;
            var localPosition = transform.localPosition;

            localPosition.x = Mathf.PerlinNoise(offsetX, shakeTime) * shakeStrength;
            localPosition.y = Mathf.PerlinNoise(offsetY, shakeTime) * shakeStrength;

            transform.localPosition = localPosition;
        }
Ejemplo n.º 16
0
        protected virtual void Update()
        {
            // Update input
            inputManager.Update(KeyCode.None);

            // Make sure the camera exists
            var camera = D2dHelper.GetCamera(null);

            if (camera != null)
            {
                // Loop through all non-gui fingers
                foreach (var finger in inputManager.Fingers)
                {
                    if (finger.StartedOverGui == false)
                    {
                        var localPosition = transform.localPosition;
                        var targetX       = (finger.PositionA.x - Screen.width / 2) * MoveScale;
                        var targetY       = (finger.PositionA.y - Screen.height / 2) * MoveScale;
                        var factor        = D2dHelper.DampenFactor(MoveSpeed, Time.deltaTime);

                        localPosition.x = Mathf.Lerp(localPosition.x, targetX, factor);
                        localPosition.y = Mathf.Lerp(localPosition.y, targetY, factor);

                        transform.localPosition = localPosition;

                        // Fire?
                        if (finger.Up == true)
                        {
                            if (MuzzlePrefab != null)
                            {
                                Instantiate(MuzzlePrefab, transform.position, Quaternion.identity);
                            }

                            if (BulletPrefab != null)
                            {
                                var position = camera.ScreenToWorldPoint(finger.PositionA);

                                Instantiate(BulletPrefab, position, Quaternion.identity);
                            }
                        }

                        // Skip other fingers
                        break;
                    }
                }
            }
        }
Ejemplo n.º 17
0
        protected virtual void FixedUpdate()
        {
            if (Target != null)
            {
                // Get the main camera
                var mainCamera = Camera.main;

                // Begin dragging
                if (Input.GetKey(Requires) == true)
                {
                    var position = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);
                    var factor   = D2dHelper.DampenFactor(Dampening, Time.fixedDeltaTime);

                    Target.velocity += (Vector2)(position - Target.transform.position) * factor;
                }
            }
        }
Ejemplo n.º 18
0
    void Playing_Update()
    {
        // Required key is down?
        if (Input.GetKeyDown(KeyCode.Mouse0) == true)
        {
            // Get screen ray of mouse position
            explosionPosition = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCam);

            var collider = Physics2D.OverlapPoint(explosionPosition);

            if (collider != null)
            {
                var commonBox = collider.GetComponentInParent <CommonBox>();
                if (commonBox != null)
                {
                    commonBox.AddFire(explosionPosition);
                }


                //var destructible = collider.GetComponentInParent<D2dDestructible>();

                //if (destructible != null)
                //{
                //    // Register split event
                //    destructible.OnEndSplit.AddListener(OnEndSplit);

                //    // Split via fracture
                //    D2dQuadFracturer.Fracture(destructible, FractureCount, 0.5f);

                //    // Unregister split event
                //    destructible.OnEndSplit.RemoveListener(OnEndSplit);

                //    // Spawn explosion prefab?
                //    if (ExplosionPrefab != null)
                //    {
                //        var worldRotation = Quaternion.Euler(0.0f, 0.0f, UnityEngine.Random.Range(0.0f, 360.0f)); // Random rotation around Z axis

                //        Instantiate(ExplosionPrefab, explosionPosition, worldRotation);
                //    }
                //}
            }
        }
    }
Ejemplo n.º 19
0
        private float GetAngleAndClampCurrentPos(Vector3 startPos, ref Vector3 currentPos)
        {
            if (startPos != currentPos)
            {
                var distance = Vector3.Distance(currentPos, startPos);

                if (DistanceMin > 0.0f && distance < DistanceMin)
                {
                    distance = DistanceMin;
                }

                if (DistanceMax > 0.0f && distance > DistanceMax)
                {
                    distance = DistanceMax;
                }

                currentPos = startPos + (currentPos - startPos).normalized * distance;
            }

            return(D2dHelper.Atan2(currentPos - startPos) * Mathf.Rad2Deg);
        }
Ejemplo n.º 20
0
        protected virtual void FixedUpdate()
        {
            if (inputManager.Fingers.Count > 0)
            {
                // Make sure the camera exists
                var camera = D2dHelper.GetCamera(null);

                if (camera != null)
                {
                    // Make sure the target exists
                    if (Target != null)
                    {
                        // Grab world position and transition there
                        var center   = inputManager.GetAveragePosition(true);
                        var position = D2dHelper.ScreenToWorldPosition(center, Intercept, camera);
                        var factor   = D2dHelper.DampenFactor(Dampening, Time.fixedDeltaTime);

                        Target.velocity += (Vector2)(position - Target.transform.position) * factor;
                    }
                }
            }
        }
Ejemplo n.º 21
0
        private void SpawnNow()
        {
            // Prefab exists?
            if (Prefab != null)
            {
                // Main camera exists?
                var camera = D2dHelper.GetCamera(Camera);

                if (camera != null)
                {
                    // World position of the mouse
                    var position = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, camera);

                    // Get a random rotation around the Z axis
                    var rotation = Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f));

                    // Spawn prefab here
                    var clone = Instantiate(Prefab, position, rotation);

                    clone.SetActive(true);
                }
            }
        }
Ejemplo n.º 22
0
        protected virtual void Update()
        {
            // Update input
            inputManager.Update(KeyCode.None);

            // Calculate control values from fingers/mouse
            var delta       = inputManager.GetAveragePullScaled(true);
            var targetSteer = Mathf.Clamp(delta.x / 100.0f, -1.0f, 1.0f) * SteerAngleMax;
            var targetDrive = Mathf.Clamp(delta.y / 100.0f, -1.0f, 1.0f) * DriveTorque;

            // Smooth to target values
            var steerFactor = D2dHelper.DampenFactor(SteerAngleDampening, Time.deltaTime);
            var driveFactor = D2dHelper.DampenFactor(DriveDampening, Time.deltaTime);

            currentSteer = Mathf.Lerp(currentSteer, targetSteer, steerFactor);
            currentDrive = Mathf.Lerp(currentDrive, targetDrive, driveFactor);

            // Apply steering
            for (var i = 0; i < SteerWheels.Length; i++)
            {
                SteerWheels[i].transform.localRotation = Quaternion.Euler(0.0f, 0.0f, -currentSteer);
            }
        }
Ejemplo n.º 23
0
        protected virtual void Update()
        {
            if (Input.GetKeyDown(Requires) == true)
            {
                var camera = D2dHelper.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray   = camera.ScreenPointToRay(Input.mousePosition);
                    var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, Layers);

                    if (count > 0)
                    {
                        for (var i = 0; i < count; i++)
                        {
                            var raycastHit2D = raycastHit2Ds[i];

                            if (TryFracture(raycastHit2D) == true)
                            {
                                // Spawn prefab?
                                if (Prefab != null)
                                {
                                    var clone = Instantiate(Prefab, raycastHit2D.point, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));

                                    clone.SetActive(true);
                                }

                                if (Hit == HitType.First)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 24
0
        protected virtual void Update()
        {
            // Update input
            inputManager.Update(Requires);

            // Make sure the camera exists
            var camera = D2dHelper.GetCamera(null);

            if (camera != null)
            {
                // Loop through all non-gui fingers
                foreach (var finger in inputManager.Fingers)
                {
                    if (finger.StartedOverGui == false)
                    {
                        // Grab extra finger data and position
                        var link     = D2dInputManager.Link.FindOrCreate(ref links, finger);
                        var position = D2dHelper.ScreenToWorldPosition(finger.PositionA, Intercept, camera);

                        // Create indiactor?
                        if (finger.Down == true && IndicatorPrefab != null)
                        {
                            link.Visual = Instantiate(IndicatorPrefab);
                            link.Scale  = link.Visual.transform.localScale;

                            link.Visual.SetActive(true);
                        }

                        // Update indicator?
                        if (finger.Set == true && link.Visual != null)
                        {
                            link.Visual.transform.position = position;

                            link.Visual.transform.localScale = Vector3.Scale(link.Scale, new Vector3(Size.x, Size.y, 1.0f));
                        }

                        // Clear indicator then stamp?
                        if (finger.Up == true)
                        {
                            // Stamp everything at this point?
                            if (Hit == HitType.All)
                            {
                                D2dStamp.All(Paint, position, Size, Angle, Shape, Color, Layers);
                            }

                            // Stamp the first thing at this point?
                            if (Hit == HitType.First)
                            {
                                var destructible = default(D2dDestructible);

                                if (D2dDestructible.TrySampleThrough(position, ref destructible) == true)
                                {
                                    destructible.Paint(Paint, D2dStamp.CalculateMatrix(position, Size, Angle), Shape, Color);
                                }
                            }

                            // Destroy indicator
                            link.Clear();
                        }
                    }
                }
            }
        }
        protected virtual void Update()
        {
            // Get the main camera
            var mainCamera = Camera.main;

            // Begin dragging
            if (Input.GetKey(Requires) == true && down == false)
            {
                down = true;
                startMousePosition = Input.mousePosition;
            }

            // End dragging
            if (Input.GetKey(Requires) == false && down == true)
            {
                down = false;

                // Throw prefab?
                if (mainCamera != null && ProjectilePrefab != null)
                {
                    // Calc values
                    var startPos   = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                    var currentPos = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);
                    var angle      = GetAngleAndClampCurrentPos(startPos, ref currentPos) + ProjectileAngle + Random.Range(-ProjectileSpread, ProjectileSpread);

                    // Spawn
                    var projectile = Instantiate(ProjectilePrefab, startPos, Quaternion.Euler(0.0f, 0.0f, -angle));

                    projectile.SetActive(true);

                    // Apply velocity?
                    var rigidbody2D = projectile.GetComponent <Rigidbody2D>();

                    if (rigidbody2D != null)
                    {
                        rigidbody2D.velocity = (currentPos - startPos) * ProjectileSpeed;
                    }
                }
            }

            // Update indicator?
            if (down == true && mainCamera != null && IndicatorPrefab != null)
            {
                if (indicatorInstance == null)
                {
                    indicatorInstance = Instantiate(IndicatorPrefab);

                    indicatorInstance.gameObject.SetActive(true);
                }

                var startPos   = D2dHelper.ScreenToWorldPosition(startMousePosition, Intercept, mainCamera);
                var currentPos = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);
                var angle      = GetAngleAndClampCurrentPos(startPos, ref currentPos);
                var scale      = Vector3.Distance(currentPos, startPos) * Scale;

                // Transform the indicator so it lines up with the slice
                indicatorInstance.transform.position   = startPos;
                indicatorInstance.transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
                indicatorInstance.transform.localScale = new Vector3(scale, scale, scale);
            }
            // Destroy indicator?
            else if (indicatorInstance != null)
            {
                Destroy(indicatorInstance.gameObject);
            }
        }
Ejemplo n.º 26
0
        protected virtual void Update()
        {
            // Update input
            inputManager.Update(Requires);

            // Make sure the camera exists
            var camera = D2dHelper.GetCamera(null);

            if (camera != null)
            {
                // Loop through all non-gui fingers
                foreach (var finger in inputManager.Fingers)
                {
                    if (finger.StartedOverGui == false)
                    {
                        // Grab extra finger data and position
                        var link     = D2dInputManager.Link.FindOrCreate(ref links, finger);
                        var position = D2dHelper.ScreenToWorldPosition(finger.PositionA, Intercept, camera);

                        // Create indiactor?
                        if (finger.Down == true)
                        {
                            link.Start = position;

                            if (IndicatorPrefab != null)
                            {
                                link.Visual = Instantiate(IndicatorPrefab);

                                link.Visual.SetActive(true);
                            }
                        }

                        // Update indicator?
                        if (finger.Set == true && link.Visual != null)
                        {
                            var angle = GetAngleAndClampCurrentPos(link.Start, ref position);
                            var scale = Vector3.Distance(position, link.Start) * Scale;

                            link.Visual.transform.position   = link.Start;
                            link.Visual.transform.rotation   = Quaternion.Euler(0.0f, 0.0f, -angle);
                            link.Visual.transform.localScale = new Vector3(scale, scale, scale);
                        }

                        // Slice scene then clear link?
                        if (finger.Up == true)
                        {
                            var angle = GetAngleAndClampCurrentPos(link.Start, ref position) + ProjectileAngle + Random.Range(-ProjectileSpread, ProjectileSpread);

                            // Spawn
                            var projectile = Instantiate(ProjectilePrefab, link.Start, Quaternion.Euler(0.0f, 0.0f, -angle));

                            projectile.SetActive(true);

                            // Apply velocity?
                            var rigidbody2D = projectile.GetComponent <Rigidbody2D>();

                            if (rigidbody2D != null)
                            {
                                rigidbody2D.velocity = (position - link.Start) * ProjectileSpeed;
                            }

                            link.Clear();
                        }
                    }
                }
            }
        }
Ejemplo n.º 27
0
        protected override void OnInspector()
        {
            var rebuild = false;

            if (Any(t => t.GetComponent <PolygonCollider2D>() != null))
            {
                EditorGUILayout.HelpBox("D2dDestructible isn't compatible with PolygonCollider2D, use D2dPolygonCollider instead.", MessageType.Warning);
            }

            if (Any(t => t.GetComponent <EdgeCollider2D>() != null))
            {
                EditorGUILayout.HelpBox("D2dDestructible isn't compatible with EdgeCollider2D, use D2dEdgeCollider instead.", MessageType.Warning);
            }

            Draw("healSnapshot", "If you want to be able to heal this destructible sprite, then set a snapshot of the healed state here.");

            if (Any(t => t.HealSnapshot != null && t.CanHeal == false))
            {
                EditorGUILayout.HelpBox("This healSnapshot is incompatible with this destructible sprite state.", MessageType.Warning);
            }

            Draw("overrideSharpness", "This allows you to manually control the sharpness of the alpha gradient:\nZero = AlphaSharpness\nPositive = OverrideSharpness\nNegative = AlphaSharpness * -OverrideSharpness");
            Draw("paintMultiplier", "This allows you to control how easily this object can be painted.\n\n1 = Default.\n2 = Twice as much damage.\n0.5 = Half as much damage.");
            Draw("pixels", "This allows you to control how the alphaTex pixels are handled.");
            Draw("indestructible", "This keeps your destructible sprite active, but prevents it from taking visual damage.");

            Separator();

            EditorGUILayout.BeginHorizontal();
            if (Any(t => t.InvalidMaterial))
            {
                if (GUILayout.Button("Change Material") == true)
                {
                    Each(t => { if (t.InvalidMaterial == true)
                                {
                                    t.ChangeMaterial();
                                }
                         });
                }
            }

            if (GUILayout.Button("Optimize") == true)
            {
                DirtyEach(t => t.Optimize());
            }

            if (GUILayout.Button("Trim") == true)
            {
                DirtyEach(t => t.Trim());
            }

            if (GUILayout.Button("Clear") == true)
            {
                DirtyEach(t => t.Clear());
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            if (Any(t => t.GetComponent <D2dCollider>() == null))
            {
                if (GUILayout.Button("+ Polygon Collider") == true)
                {
                    Each(t => t.gameObject.AddComponent <D2dPolygonCollider>());
                }

                if (GUILayout.Button("+ Edge Collider") == true)
                {
                    Each(t => t.gameObject.AddComponent <D2dEdgeCollider>());
                }
            }

            if (Any(t => t.GetComponent <D2dSplitter>() == null) && GUILayout.Button("+ Splitter") == true)
            {
                Each(t => t.gameObject.AddComponent <D2dSplitter>());
            }
            EditorGUILayout.EndHorizontal();

            Separator();

            if (Targets.Length == 1)
            {
                BeginDisabled();
                EditorGUI.ObjectField(D2dHelper.Reserve(), "Alpha Tex", Target.AlphaTex, typeof(Texture2D), true);
                EditorGUILayout.IntField("Alpha Width", Target.AlphaWidth);
                EditorGUILayout.IntField("Alpha Height", Target.AlphaHeight);
                EditorGUILayout.FloatField("Alpha Sharpness", Target.AlphaSharpness);
                EditorGUILayout.IntField("Alpha Count", Target.AlphaCount);
                EditorGUILayout.IntField("Original Alpha Count", Target.OriginalAlphaCount);
                EditorGUI.ProgressBar(D2dHelper.Reserve(), Target.AlphaRatio, "Alpha Ratio");
                EndDisabled();
            }

            if (rebuild == true)
            {
                DirtyEach(t => t.RebuildAlphaTex());
            }
        }
        protected virtual void Update()
        {
            // Main camera exists?
            var mainCamera = Camera.main;

            if (mainCamera != null)
            {
                // World position of the mouse
                var position = D2dHelper.ScreenToWorldPosition(Input.mousePosition, Intercept, mainCamera);

                // Begin dragging
                if (Input.GetKey(Requires) == true && down == false)
                {
                    down = true;
                }

                // End dragging
                if (Input.GetKey(Requires) == false && down == true)
                {
                    down = false;

                    // Stamp everything at this point?
                    if (Hit == HitType.All)
                    {
                        D2dStamp.All(Paint, position, Size, Angle, Shape, Color, Layers);
                    }

                    // Stamp the first thing at this point?
                    if (Hit == HitType.First)
                    {
                        var destructible = default(D2dDestructible);

                        if (D2dDestructible.TrySampleThrough(position, ref destructible) == true)
                        {
                            destructible.Paint(Paint, D2dStamp.CalculateMatrix(position, Size, Angle), Shape, Color);
                        }
                    }
                }

                // Update indicator?
                if (down == true && IndicatorPrefab != null)
                {
                    if (indicatorInstance == null)
                    {
                        indicatorInstance = Instantiate(IndicatorPrefab);

                        indicatorScale = indicatorInstance.transform.localScale;

                        indicatorInstance.SetActive(true);
                    }

                    indicatorInstance.transform.position = position;

                    indicatorInstance.transform.localScale = Vector3.Scale(indicatorScale, new Vector3(Size.x, Size.y, 1.0f));
                }
                // Destroy indicator?
                else if (indicatorInstance != null)
                {
                    Destroy(indicatorInstance.gameObject);
                }
            }
        }