Пример #1
0
 protected override void Execute()
 {
     parent.isPregnant = false;
     if (parent.childPart == null)
     {
         child                    = Ego.AddGameObject(Object.Instantiate <GameObject>(parent.snakePrefab)).GetComponent <SnakePartComponent>();
         child.snakePrefab        = parent.snakePrefab;
         child.transform.position = position;
         child.transform.rotation = parent.transform.rotation;
         child.transform.parent   = parent.container;
         child.container          = parent.container;
         parent.childPart         = child;
         //child.parentPart = parent;
         createdNew = true;
     }
     else
     {
         createdNew = false;
         if (!parent.childPart.isPregnant)
         {
             parent.childPart.isPregnant = true;
             var commandEvent = new CommandEvent(new PregnancyCommand(parent.childPart, position, true), 1);
             EgoEvents <CommandEvent> .AddEvent(commandEvent);
         }
     }
 }
Пример #2
0
    private KeyCode GetForbiddenKey()
    {
        SnakeFilter FirstSnake = Snakes[0];

        SnakePartComponent SnakeHead      = FirstSnake.SnakeComponent.BodyParts[0];
        SnakePartComponent SnakeFirstBody = FirstSnake.SnakeComponent.BodyParts[1];

        Vector3 FirstBodyForward = SnakeFirstBody.transform.up * -1;

        if (Mathf.RoundToInt(FirstBodyForward.z) == 1)
        {
            return(KeyCode.UpArrow);
        }
        else if (Mathf.RoundToInt(FirstBodyForward.z) == -1)
        {
            return(KeyCode.DownArrow);
        }
        else if (Mathf.RoundToInt(FirstBodyForward.x) == 1)
        {
            return(KeyCode.RightArrow);
        }
        else
        {
            return(KeyCode.LeftArrow);
        }
    }
    private void MoveSnake(SnakeComponent snake)
    {
        bool TooBig = false;

        for (int i = snake.BodyParts.Count - 1; i >= 0; i--)
        {
            SnakePartComponent IteratingPart = snake.BodyParts[i];
            if (snake.Increase && i > 1)
            {
                //Do Nothing
            }
            else if (snake.Increase && i == 1)
            {
                GameObject AddedPart = GameObject.Instantiate <GameObject>(snake.BodyPrefab);
                AddedPart.name = AddedPart.name.Replace("(Clone)", "");
                AddedPart.transform.SetParent(snake.transform);
                AddedPart.transform.position = IteratingPart.transform.position;
                AddedPart.transform.up       = IteratingPart.transform.up;

                snake.BodyParts.Insert(1, AddedPart.GetComponent <SnakePartComponent>());
                snake.Increase = false;
                snake.Size++;
                i++;

                if (snake.Size >= GameSettings.Settings.MaxSnakeSize)
                {
                    TooBig = true;
                }
            }
            else
            {
                if (i > 0)
                {
                    int NextPartIndex           = i - 1;
                    SnakePartComponent NextPart = snake.BodyParts[NextPartIndex];
                    Vector3            Forward  = NextPart.transform.position - IteratingPart.transform.position;
                    IteratingPart.transform.position += Forward;
                }
                else
                {
                    IteratingPart.transform.position += IteratingPart.transform.forward;
                }
            }
        }

        NormalizePartsRotation(snake);

        if (CheckForCollisionWithSelf(snake) || CheckForBoundsCollision(snake))
        {//EndGame
            GameStateSystem.EndGame();
        }
        else if (TooBig)
        {
            GameStateSystem.EndGame(true);
        }
    }
    private SnakeComponent GetSnakeAt(Vector3 position)
    {
        Vector3 CheckPosition = new Vector3(Mathf.RoundToInt(position.x), 1, Mathf.RoundToInt(position.z));

        for (int SnakeIndex = 0; SnakeIndex < Snakes.Length; SnakeIndex++)
        {
            for (int PartIndex = 0; PartIndex < Snakes[SnakeIndex].SnakeComponent.BodyParts.Count; PartIndex++)
            {
                SnakePartComponent CheckPart    = Snakes[SnakeIndex].SnakeComponent.BodyParts[PartIndex];
                Vector3            PartPosition = new Vector3(Mathf.RoundToInt(CheckPart.transform.position.x), Mathf.RoundToInt(CheckPart.transform.position.y), Mathf.RoundToInt(CheckPart.transform.position.z));
                if (PartPosition.Equals(CheckPosition))
                {
                    return(Snakes[SnakeIndex].SnakeComponent);
                }
            }
        }

        return(null);
    }
    private bool CheckForCollisionWithSelf(SnakeComponent snake)
    {
        List <Vector3> PartPositions = new List <Vector3>();

        for (int i = 0; i < snake.BodyParts.Count; i++)
        {
            SnakePartComponent CheckPart    = snake.BodyParts[i];
            Vector3            PartPosition = new Vector3(Mathf.RoundToInt(CheckPart.transform.position.x), Mathf.RoundToInt(CheckPart.transform.position.y), Mathf.RoundToInt(CheckPart.transform.position.z));
            if (!PartPositions.Contains(PartPosition))
            {
                PartPositions.Add(PartPosition);
            }
            else
            {
                return(true);
            }
        }
        return(false);
    }
Пример #6
0
    protected override void OnUpdate()
    {
        if (Snakes.Length == 0)
        {
            return;
        }

        ForbiddenKey = GetForbiddenKey();

        if (ForbiddenKey != KeyCode.UpArrow && Input.GetKeyDown(KeyCode.UpArrow))
        {
            for (int i = 0; i < Snakes.Length; i++)
            {
                SnakePartComponent SnakeHead = Snakes[i].SnakeComponent.BodyParts[0];
                SnakeHead.transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
        else if (ForbiddenKey != KeyCode.RightArrow && Input.GetKeyDown(KeyCode.RightArrow))
        {
            for (int i = 0; i < Snakes.Length; i++)
            {
                SnakePartComponent SnakeHead = Snakes[i].SnakeComponent.BodyParts[0];
                SnakeHead.transform.rotation = Quaternion.Euler(0, 90, 0);
            }
        }
        else if (ForbiddenKey != KeyCode.DownArrow && Input.GetKeyDown(KeyCode.DownArrow))
        {
            for (int i = 0; i < Snakes.Length; i++)
            {
                SnakePartComponent SnakeHead = Snakes[i].SnakeComponent.BodyParts[0];
                SnakeHead.transform.rotation = Quaternion.Euler(0, 180, 0);
            }
        }
        else if (ForbiddenKey != KeyCode.LeftArrow && Input.GetKeyDown(KeyCode.LeftArrow))
        {
            for (int i = 0; i < Snakes.Length; i++)
            {
                SnakePartComponent SnakeHead = Snakes[i].SnakeComponent.BodyParts[0];
                SnakeHead.transform.rotation = Quaternion.Euler(0, 270, 0);
            }
        }
    }
    public bool IsPositionWithinSnake(Vector3 position)
    {
        List <Vector3> PartPositions = new List <Vector3>();

        for (int SnakeIndex = 0; SnakeIndex < Snakes.Length; SnakeIndex++)
        {
            for (int PartIndex = 0; PartIndex < Snakes[SnakeIndex].SnakeComponent.BodyParts.Count; PartIndex++)
            {
                SnakePartComponent CheckPart    = Snakes[SnakeIndex].SnakeComponent.BodyParts[PartIndex];
                Vector3            PartPosition = new Vector3(Mathf.RoundToInt(CheckPart.transform.position.x), Mathf.RoundToInt(CheckPart.transform.position.y), Mathf.RoundToInt(CheckPart.transform.position.z));
                if (!PartPositions.Contains(PartPosition))
                {
                    PartPositions.Add(PartPosition);
                }
            }
        }

        Vector3 CheckPosition = new Vector3(Mathf.RoundToInt(position.x), 1, Mathf.RoundToInt(position.z));

        return(PartPositions.Contains(CheckPosition));
    }
Пример #8
0
 public PregnancyCommand(SnakePartComponent parent, Vector3 position, bool setPregnant)
 {
     this.parent      = parent;
     this.position    = position;
     this.setPregnant = setPregnant;
 }
Пример #9
0
 public BirthCommand(SnakePartComponent parent, Vector3 position)
 {
     this.parent   = parent;
     this.position = position;
 }
Пример #10
0
 public PregnancyEvent(SnakePartComponent newParent)
 {
     this.newParent = newParent;
 }