예제 #1
0
        public void Draw(SpriteBatch spriteBatch, int x, int y)
        {
            Color color = IsIdling ? Color.Green : Color.Orange;

            spriteBatch.Draw(Texture,
                             new Rectangle(x, y, World.BlockSize, World.BlockSize),
                             new Rectangle(0, 0, Texture.Width, Texture.Height),
                             color,
                             0.0f,
                             new Vector2(0),
                             SpriteEffects.None,
                             DrawLayer.Pawn
                             );

            if (carrying != null)
            {
                Point drawCarrying = new Point(x, y);
                int   offset       = World.BlockSize / 4;
                if (direction == Facing.North)
                {
                    drawCarrying.Y -= offset;
                }
                else if (direction == Facing.East)
                {
                    drawCarrying.X += offset;
                }
                else if (direction == Facing.South)
                {
                    drawCarrying.Y += offset;
                }
                else if (direction == Facing.West)
                {
                    drawCarrying.X -= offset;
                }
                carrying.Draw(spriteBatch, drawCarrying.X, drawCarrying.Y, DrawLayer.Pawn - 0.02f);
            }

            if (IsSelected)
            {
                ShapeManager.DrawCircle(spriteBatch, new Point(x + World.BlockSize / 2, y + World.BlockSize / 2), World.BlockSize / 2, Color.Red);

                if (pathfinder != null && pathfinder.Path != null)
                {
                    Cell previous = nextCell;

                    foreach (Cell next in pathfinder.Path)
                    {
                        ShapeManager.DrawLine(spriteBatch,
                                              new Point(previous.X * World.BlockSize + World.BlockSize / 2, previous.Y * World.BlockSize + World.BlockSize / 2),
                                              new Point(next.X * World.BlockSize + World.BlockSize / 2, next.Y * World.BlockSize + World.BlockSize / 2),
                                              Color.Magenta
                                              );
                        previous = next;
                    }
                }
            }
        }
예제 #2
0
    public IEnumerator Knife(GameObject unit, Vector2 target)
    {
        Vector2      origin    = unit.transform.position;
        Vector2      direction = target - (Vector2)unit.transform.position;
        RaycastHit2D hit       = Physics2D.Raycast(origin, direction, Globals.KNIFE_RANGE, ~LayerMask.GetMask("Player"));

        if (hit.collider != null)
        {
            UnitStatus targetHit = hit.collider.GetComponent <UnitStatus>();
            //AutoMover targetMover = hit.collider.GetComponent<AutoMover>();
            //UnitStatus attacker = unit.GetComponent<UnitStatus>();
            if (targetHit != null /*&& targetMover.GetAwareness() != AutoMover.State.Alert*/)
            {
                SoundManager.instance.Play(SoundManager.Sound.Knife);
                targetHit.DamageHealth(3);
                // There is a risk of being hurt attempting to knife an enemy
                //if(attacker && Random.Range(0, 11) == 0)
                //    attacker.DamageHealth(1);
            }

            // May want to make amount of noise random
            GameObject tempNoise = Instantiate(Globals.NOISE, unit.transform.position, Quaternion.identity);
            tempNoise.GetComponent <Noise>().Initialize(unit.CompareTag("Player"), Globals.KNIFE_VOLUME, Noise.Source.Knife);

            // Create and format line
            GameObject shotLine = ShapeManager.DrawLine(origin, hit.point, Globals.BRIGHT_RED, unit.transform);
            shotLine.transform.parent = unit.transform;
            LineRenderer shotRenderer = shotLine.GetComponent <LineRenderer>();
            shotRenderer.startWidth = shotRenderer.endWidth = 0.07f;

            shotLine.AddComponent <AutoVanish>().timeToLive = 0.1f;
        }

        yield return(new WaitForSeconds(0.25f));

        Sidebar.instance.FinishAction();
    }
예제 #3
0
    // Single gunshot, usable by both player and enemy
    public static void Gun(GameObject unit, Vector2 target)
    {
        bool attackerIsPlayer = unit.GetComponent <PlayerMover>();

        // Prevent attacker from hitting themselves
        BoxCollider2D unitCollider = unit.GetComponent <BoxCollider2D>();

        unitCollider.enabled = false;
        int instanceId = unitCollider.GetInstanceID();

        Vector2 shotOrigin = unit.transform.position;
        //(Vector2)unit.transform.position + unit.GetComponent<GridMover>().GetRotator().FrontOffset();

        // 3 Focus -> 10 deg Error, 2 Focus -> 20 deg Error
        // 1 Focus -> 30 deg Error, 0 Focus -> Can't attack
        float marginOfError = attackerIsPlayer ? 13 : 15; //+ (10 * (3 - unit.GetComponent<FieldUnit>().party[0].focus)); // min 10 err, max 30 / formerly memberIndex

        // Calculate if there's a clear line of sight
        Vector2 direction = target - (Vector2)unit.transform.position;
        // RaycastHit2D hit = Physics2D.Raycast(shotOrigin, direction/*, 9, mask*/);

        float angle = Vector2.Angle(unit.GetComponent <Rotator>().FrontOffset(), direction);

        // arg1 of Angle used to be unit.GetComponent<GridMover>().GetRotator().FrontOffset()

        // Generate an actual shot
        angle    += Random.Range(0, marginOfError) * (Random.Range(0, 2) == 0 ? 1 : -1); // add margin of error
        direction = Quaternion.AngleAxis(angle, Vector3.forward) * direction;            // This flattens the shot somehow
        RaycastHit2D hit = Physics2D.Raycast(shotOrigin, direction, 255, ~LayerMask.GetMask(LayerMask.LayerToName(unit.layer)));

        SoundManager.instance.Play(SoundManager.Sound.Gun, instanceId);
        // Debug.Log("layer: " + LayerMask.LayerToName(unit.layer));

        if (hit.collider != null)
        {
            // Make sure to check WHAT is being hit... Player? Wall? Friendly fire?

            // source.PlayOneShot(gunshot); // play sound
            // Camera.main.GetComponent<Jerk>().Shake(1); // Replace this with something better

            UnitStatus targetHit = hit.collider.GetComponent <UnitStatus>();
            if (targetHit)
            {
                targetHit.DamageHealth(); // formerly memberIndex
                AutoMover autoMover = hit.collider.GetComponent <AutoMover>();
                if (autoMover)
                {
                    autoMover.VisualToPosition(Grapher.RoundedVector(unit.transform.position));
                }
            }

            GameObject tempNoise = Instantiate(Globals.NOISE, unit.transform.position, Quaternion.identity);
            tempNoise.GetComponent <Noise>().Initialize(attackerIsPlayer, Globals.GUN_VOLUME, Noise.Source.Gun);

            // Create and format line
            GameObject shotLine = ShapeManager.DrawLine(shotOrigin, hit.point, Globals.BRIGHT_WHITE, unit.transform);
            shotLine.transform.parent = unit.transform;
            LineRenderer shotRenderer = shotLine.GetComponent <LineRenderer>();
            shotRenderer.startWidth = shotRenderer.endWidth = 0.07f;

            shotLine.AddComponent <AutoVanish>().timeToLive = 0.1f;
        }

        unit.GetComponent <BoxCollider2D>().enabled = true;
    }