Example #1
0
    void Collect()
    {
        los = false;

        var root = new Vector3(
            enemy.cast.position.x,
            enemy.transform.position.y,
            enemy.transform.position.z
            );

        var dir   = root - transform.position;
        var dir3d = enemy.cast.position - self.cast.position;

        dir.Normalize();
        dir3d.Normalize();

        var win = 0.1f;

        if (Time.time - self.lastMiss < win || Time.time - self.lastHit < win)
        {
            if (self.lastAggr != null)
            {
                lastTarget = self.lastAggr.transform.position;
                lastSelf   = transform.position;
            }
        }

        if (Vector3.Dot(dir, transform.forward) > 0.5f)
        {
            var result = self.Cast(dir3d);
            if (result.HasValue)
            {
                var body = result.Value.transform;
                var dist = result.Value.distance;
                var tag  = body.tag;

                if (tag == "Player")
                {
                    los        = true;
                    lastTarget = body.position;
                    lastSelf   = transform.position;
                    seen       = 0;

                    // Debug.DrawRay(transform.position, dir * dist, Color.green);
                }
                else
                {
                    // Debug.DrawRay(transform.position, dir * dist, Color.red);
                }
            }
        }

        Debug.DrawLine(lastSelf, lastTarget, Color.magenta);

        locked = los ? Vector3.Dot(transform.forward, dir) : 0;
        shot   = locked >= 1 - (bot.spread + 0.001f);
        locked = Mathf.Max(0, locked);

        /*
         * float exposure = self.down ? 0.5f : 0;
         * Vector3[] checks = new Vector3[]
         * {
         *  Vector3.forward,
         *  (new Vector3( 0.5f, 0, 1)).normalized,
         *  (new Vector3(-0.5f, 0, 1)).normalized,
         * };
         *
         * foreach (var check in checks)
         * {
         *  var result = self.Cast(check);
         *  if (result.HasValue)
         *  {
         *      if (result.Value.distance < 2)
         *          continue;
         *  }
         *
         *  exposure += 0.5f / 3;
         * }
         */

        exposure = Mathf.Clamp01(
            1 - Mathf.Abs(cover.NearestCover(transform.position.x)) / (2 * Global.Values.width)
            );

        float disc = Vector3.Distance(lastSelf, transform.position);

        if (lastSelf.y < 0)
        {
            disc = 2 * Global.Values.width;
        }
        discovered = Mathf.Clamp01(1 - disc / (2 * Global.Values.width));

        seen     += Time.deltaTime;
        seenDelay = Mathf.Clamp01(seen / 3);

        if (transform.position.x > Global.Values.width - 1)
        {
            switchSide = -1;
        }
        else if (transform.position.x < -Global.Values.width + 1)
        {
            switchSide = 1;
        }

        var max = self.lastMiss > self.lastHit ?
                  self.lastMiss : self.lastHit;

        underFire = 1 - Mathf.Clamp01((Time.time - max) / 2);
    }