Example #1
0
    void dig()
    {
        Vector2 shoulderPos = foregroundShoulder.position;

        if (!toolManager.rightOri)
        {
            shoulderPos = backgroundShoulder.position;
        }
        float      dist;
        GameObject obj = MouseOver(shoulderPos, out dist);

        if (obj == null || obj.transform.GetComponent <Collider2D>() == null)
        {
            return;
        }
        // Vector2 nearestPoint = obj.transform.GetComponent<Collider2D>().ClosestPoint(shoulderPos);


        if (dist <= reach)
        {
            asteroid ast = obj.GetComponent <asteroid>();
            if (ast != null)
            {
                if (hit != null)
                {
                    hit.Play();
                }
                pickaxeHit.transform.position = cam.ScreenToWorldPoint(Input.mousePosition);
                pickaxeHit.Play();
                ast.damage(strenth);
            }
        }
        //StartCoroutine("animatePick");
    }
Example #2
0
 void Explode()
 {
     Collider2D[] objectsInRange = Physics2D.OverlapCircleAll(gameObject.transform.position, explosionRadius);
     foreach (Collider2D col in objectsInRange)
     {
         asteroid rock   = col.GetComponent <asteroid>();
         Player   player = col.GetComponent <Player>();
         if (rock != null)
         {
             rock.ApplyDamage(explosionDamage);
         }
         if (player != null)
         {
             player.ApplyDamage(explosionDamage);
         }
     }
     PlayExplosion();
     Destroy(gameObject);
 }
    /// <summary>
    /// Sent when an incoming collider makes contact with this object's
    /// collider (2D physics only).
    /// </summary>
    /// <param name="other">The Collision2D data associated with this collision.</param>
    void OnCollisionEnter2D(Collision2D other)
    {
        asteroid astro = other.gameObject.GetComponent <asteroid>();

        if (astro != null)
        {
            astro.damage(damage);
        }
        if (layerMask == (layerMask | (1 << other.gameObject.layer)))
        {
            particles.Play();
            sprite.enabled     = false;
            col.enabled        = false;
            rb.velocity        = Vector2.zero;
            rb.angularVelocity = 0;
        }

        //Destroy(gameObject);
    }
Example #4
0
            public bool lineOfSight(asteroid to, List <asteroid> asteroids)
            {
                int dx = (to.x - this.x).Abs();
                int dy = (to.y - this.y).Abs();

                foreach (asteroid a in asteroids)
                {
                    if (a != this && a != to)
                    {
                        int dxa = a.x - this.x;
                        int dya = a.y - this.y;
                        if (dxa.Abs() <= dx && dya.Abs() <= dy && sameAngle(to, a))
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
Example #5
0
 public void laserStuff()
 {
     if (Input.GetButton("Fire1"))
     {
         laser.SetActive(true);
         RaycastHit2D hit = Physics2D.Raycast(laserOrigin.position, toolDir, 100, layerMask);
         if (hit.collider != null)
         {
             foreach (LineRenderer l in laserBeams)
             {
                 l.SetPosition(1, l.transform.InverseTransformPoint(hit.point));
                 end.position = hit.point;
                 end.up       = hit.normal;
                 end.gameObject.SetActive(true);
             }
             asteroid astro = hit.collider.GetComponent <asteroid>();
             if (astro != null)
             {
                 Vector2 pos  = astro.transform.position;
                 bool    boom = astro.damage(damagePerSecond * Time.deltaTime);
                 if (boom)
                 {
                     GameObject.Instantiate(explosion, pos, Quaternion.Euler(0, 0, Random.Range(0, 360)));
                     float shaky = Mathf.Clamp(-hit.distance * 0.1f + 1, 0.1f, 1f) * 0.7f;
                     shake.Shake(0.2f, shaky);
                 }
             }
         }
         else
         {
             foreach (LineRenderer l in laserBeams)
             {
                 l.SetPosition(1, Vector2.right * 100f);
                 end.gameObject.SetActive(false);
             }
         }
     }
     else
     {
         laser.SetActive(false);
     }
 }
Example #6
0
 public bool sameAngle(asteroid a, asteroid b)
 {
     if (sign(a.x - this.x) != sign(b.x - this.x))
     {
         return(false);
     }
     if (sign(a.y - this.y) != sign(b.y - this.y))
     {
         return(false);
     }
     if (a.x == this.x && b.x == this.x)
     {
         return(true);
     }
     if (a.y == this.y && b.y == this.y)
     {
         return(true);
     }
     return((a.x - this.x) * (b.y - this.y) == (b.x - this.x) * (a.y - this.y));
 }
Example #7
0
        private static void LoadElements()
        {
            asteroids = new asteroid[20];
            stars     = new SpaceElement[60];

            Random k = new Random();

            for (int i = 0; i < asteroids.Length; i++)
            {
                int[] kk = new int[4];
                kk[0] = k.Next(Width); kk[1] = k.Next(Height); //position
                kk[2] = k.Next(5, 20);                         //size
                kk[3] = k.Next(1, 5);                          //speed
                double ang = k.Next();
                asteroids[i] = new asteroid(new Point(kk[0], kk[1]), new Size(kk[2], kk[2]), ang, (byte)kk[3]);
            }
            for (int i = 0; i < stars.Length; i++)
            {
                stars[i] = new SpaceElement(new Point(k.Next(Width), k.Next(Height)));
            }
        }
Example #8
0
        public static async Task Run()
        {
            var inputs = await File.ReadAllLinesAsync("inputs\\10.txt");

            List <asteroid> asteroids = new List <asteroid> ();

            for (int y = 0; y < inputs.Length; y++)
            {
                for (int x = 0; x < inputs [y].Length; x++)
                {
                    if ((inputs [y]) [x].ToString() == "#")
                    {
                        asteroids.Add(new asteroid()
                        {
                            x = x,
                            y = y
                        });
                    }
                }
            }

            asteroids.ForEach(e => e.countVisible(asteroids));

            var best = asteroids.First(e => e.visible.Count == asteroids.Max(e => e.visible.Count));

            Console.WriteLine("Part 1: " + asteroids.Max(e => e.visible.Count).ToString());

            int goal = 200;

            foreach (asteroid a in best.visible)
            {
                a.angle = -Math.Atan2(a.x - best.x, a.y - best.y);
            }
            best.visible.Sort((a, b) => a.angle.CompareTo(b.angle));
            asteroid target = best.visible [goal - 1];

            Console.WriteLine("Part 2: " + (target.x * 100 + target.y).ToString());
        }
Example #9
0
        private static void LoadElements()
        {
            asteroids = new asteroid[10];
            stars     = new Star[60];

            Random k = new Random();

            for (int i = 0; i < asteroids.Length; i++)
            {
                int[] kk = new int[4];
                kk[0] = k.Next(Width); kk[1] = k.Next(Height); //position
                //kk[0] = 380; kk[1] = 290;
                kk[2] = k.Next(20, 40);                        //size
                kk[3] = k.Next(5, 10);                         //speed

                asteroids[i] = new asteroid(new Point(kk[0], kk[1]), new Size(kk[2], kk[2]), (byte)kk[3]);
            }
            for (int i = 0; i < stars.Length; i++)
            {
                stars[i] = new Star(new Point(k.Next(Width), k.Next(Height)));
            }
            //Bullet _bullet=new Bullet()
        }
Example #10
0
    void LateUpdate()
    {
        changeOldPos = true;
        Debug.DrawRay(shoulder.position, Vector2.up, Color.yellow);
        Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
        // toolManager.focus doMath();

        Vector2 handpos = HandPos(mousePos);

        //rbHead.MovePosition(rbHead.position+(Vector2.right*0.1f));
        if (Input.GetButton("Fire1"))
        {
            normalSound.UnPause();
            toolManager.focus.position = GetCorrectedHandPos(handpos);
            drilling = true;
            drillHead.SetFloat("speed", drillSpeed);
            targetExtend = Mathf.Clamp(Vector2.Distance(shoulder.position, mousePos) - (r + R), 0, extendable);
            //head.localPosition = Vector2.right*(defaultExtend+targetExtend);
        }
        else
        {
            normalSound.Pause();
            toolManager.focus.position = handpos;
            drilling     = false;
            targetExtend = 0;
            drillHead.SetFloat("speed", 0);
        }
        Collider2D[] asteroids;
        touching = GetTouching(out asteroids);
        bool  h    = GetTouchingHinten();
        float plus = (Mathf.Sign(targetExtend - currentExtend) * Time.deltaTime) / (extendSpeed * extendable);
        bool  ok   = true;

        if (touching && !changeOldPos && switchWait == 0)
        {
            float lenny = (transform.InverseTransformPoint(oldPosition).x - (toolLength + currentExtend + (plus * extendable)));
            if (lenny < 0)
            {
                currentExtend      = Mathf.Clamp(currentExtend + (lenny - ((plus * extendable))), 0, extendable);
                head.localPosition = Vector2.right * (defaultExtend + currentExtend);
                rod.localScale     = Vector2.one + Vector2.up * (-0.9f + currentExtend);
                ok = false;
            }
        }
        if (currentExtend != targetExtend && ok)
        {
            float f = currentExtend / extendable;
            if (drilling)
            {
                if ((plus > 0 && touching) || (plus < 0 && h))
                {
                    ok = false;
                }
            }
            if (ok)
            {
                currentExtend = Mathf.Lerp(0, extendable, Mathf.Clamp01(f + plus));
                if ((plus > 0)?(currentExtend > targetExtend):(currentExtend < targetExtend))
                {
                    currentExtend = targetExtend;
                }
            }

            head.localPosition = Vector2.right * (defaultExtend + currentExtend);
            rod.localScale     = Vector2.one + Vector2.up * (-0.9f + currentExtend);
        }
        if (touching && drilling)
        {
            foreach (ParticleSystem pS in particles)
            {
                pS.Play();
                hitSound.UnPause();
            }

            foreach (Collider2D col in asteroids)
            {
                if (col != null)
                {
                    asteroid astro = col.GetComponent <asteroid>();
                    if (astro != null)
                    {
                        astro.damage(damagePerSecond * Time.deltaTime);
                    }
                }
            }
        }
        else
        {
            hitSound.Pause();
            foreach (ParticleSystem pS in particles)
            {
                pS.Stop();
            }
        }
        if (switchWait == 0 && changeOldPos)
        {
            oldPosition = transform.TransformPoint(Vector2.right * (toolLength + currentExtend));
        }

        if (switchWait > 0)
        {
            switchWait--;
        }
    }