private void NormalizePartsRotation(SnakeComponent snake)
 {
     for (int i = 1; i < snake.BodyParts.Count; i++)
     {
         snake.BodyParts[i].transform.up = snake.BodyParts[i - 1].transform.position - snake.BodyParts[i].transform.position;
     }
 }
    void OnSpawnTail(SpawnEvent data)
    {
        Debug.Log("Spawn tail");
        SnakeComponent      snake   = _world.GetComponent <SnakeComponent> (_snakeEntity);
        GameObjectComponent snakeGO = _world.GetComponent <GameObjectComponent> (_snakeEntity);
        GameObject          tail    = GameObject.Instantiate(_tailPrefab, snakeGO.Transform.localPosition, Quaternion.identity);

        snake.Tail.Insert(0, tail.transform);
    }
    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 bool CheckForBoundsCollision(SnakeComponent snake)
 {
     try
     {
         return(snake.BodyParts.FirstOrDefault(x => Mathf.Abs(x.transform.position.x) >= GameSettings.Settings.Bounds || Mathf.Abs(x.transform.position.z) >= GameSettings.Settings.Bounds) != null);
     }
     catch (System.Exception ex)
     {
         Debug.LogError("Could not get GameSettings from project. Error : " + ex.Message + System.Environment.NewLine + ex.StackTrace);
     }
     return(false);
 }
    void SpawnSnake()
    {
        Debug.Log("Spawn snake");
        GameObject headPrefab = Resources.Load <GameObject> ("SnakeHead");
        GameObject head       = GameObject.Instantiate(headPrefab, Vector3.zero, Quaternion.identity);

        _snakeEntity = _world.CreateEntity();
        SnakeComponent sc = _world.AddComponent <SnakeComponent> (_snakeEntity);

        sc.Length = 0;
        sc.Tail   = new List <Transform> ();
        _world.AddComponent <ControlComponent> (_snakeEntity);
        _world.AddComponent <GameObjectComponent> (_snakeEntity).Init(head);
        _world.AddComponent <ColliderComponent> (_snakeEntity).Collider = head.GetComponent <Collider2D> ();
    }
    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);
    }
Beispiel #7
0
    void IEcsUpdateSystem.Update()
    {
        _snakeCollider = _world.GetComponent <ColliderComponent> (_snakeObjects.Entities[0], _colliderComponentId);
        SnakeComponent snakeComponent = _world.GetComponent <SnakeComponent> (_snakeObjects.Entities[0], _snakeComponentId);

        foreach (var entity in _collisionObjects.Entities)
        {
            var eCollider = _world.GetComponent <ColliderComponent> (entity, _colliderComponentId);
            if (eCollider.Collider.bounds.Intersects(_snakeCollider.Collider.bounds))
            {
                snakeComponent.Length++;
                _world.PublishEvent <int> (snakeComponent.Length);
                var dData = new DestroyEvent();
                dData.Index = entity;
                _world.PublishEvent <DestroyEvent> (dData);
                var sData = new SpawnEvent();
                sData.Length = snakeComponent.Length;
                _world.PublishEvent <SpawnEvent> (sData);
            }
        }
    }
Beispiel #8
0
        //Factory method to add more to the snake.
        public void Grow()
        {
            bool prevCoordAvailable = false;
            int  newPieceX          = 0;
            int  newPieceY          = 0;

            foreach (int[] coordinate in this.PreviousTailCoords)
            {
                //we are checking for food in previous coordinates because of the unlikely event food is spawned in one of those
                //coordinates after the snake eats a food (a 0.03% chance mind you but not out of the question, it's still many orders
                //of magnitude more likely than Dream's disputed speedruns)
                if (AnyPieceAtCoords(coordinate[0], coordinate[1]) || Program.Logic.IsFoodAtLocation(coordinate[0], coordinate[1]))
                {
                    continue;
                }
                else
                {
                    newPieceX          = coordinate[0];
                    newPieceY          = coordinate[1];
                    prevCoordAvailable = true;
                    //this.Pieces.Add(new SnakeBody(coordinate[0], coordinate[1]));
                }
            }

            if (!prevCoordAvailable)
            {
                //first, attempt to infer the orientation of the last and second to last piece and continue on from that.
                SnakeComponent lastPiece    = this.Pieces[this.Pieces.Count - 1];
                SnakeComponent sToLastPiece = this.Pieces[this.Pieces.Count - 2];
                SnakeHead      head         = (SnakeHead)this.Pieces[0];
                //we can assume it will not be diagonal as the algorithm never tries to go diagonally elsewhere
                if (lastPiece.X == sToLastPiece.X)
                {
                    //they are on the same X
                    newPieceX = lastPiece.X;
                    newPieceY = lastPiece.Y - (lastPiece.Y - sToLastPiece.Y);
                }
                else
                {
                    newPieceX = lastPiece.X - (lastPiece.X - sToLastPiece.X);
                    newPieceY = lastPiece.Y;
                }

                if (this.AnyPieceAtCoords(newPieceX, newPieceY))
                {
                    //failing that, go by the facing of the head.
                    int facingInt = (int)head.Facing;
                    int yChange   = facingInt < 2 ? (facingInt == 0 ? 1 : -1) : 0;
                    int xChange   = facingInt > 1 ? (facingInt - 2 == 0 ? -1 : 1) : 0;

                    if (!this.AnyPieceAtCoords(lastPiece.X + xChange, lastPiece.Y + yChange))
                    {
                        newPieceX = lastPiece.X + xChange;
                        newPieceY = lastPiece.Y + yChange;
                    }
                    else
                    {
                        //as a last resort, go for the first available position going clockwise
                        //if nothing else works, idk lol I'll think of something
                        int[][] relCoordsToTry = new int[4][] { new int[2] {
                                                                    0, 1
                                                                }, new int[2] {
                                                                    1, 0
                                                                }, new int[2] {
                                                                    0, -1
                                                                }, new int[2] {
                                                                    -1, 0
                                                                } };
                        int[] validCoords = null;

                        foreach (int[] toTry in relCoordsToTry)
                        {
                            if (!this.AnyPieceAtCoords(lastPiece.X + toTry[0], lastPiece.Y + toTry[1]))
                            {
                                validCoords = toTry;
                                break;
                            }
                        }

                        if (validCoords == null)
                        {
                            //the thing I'll think of
                        }
                        else
                        {
                            newPieceX = validCoords[0];
                            newPieceY = validCoords[1];
                        }
                    }
                }
            }

            this.Pieces.Add(new SnakeBody(newPieceX, newPieceY));
        }
    public void FeedSnake(Vector3 eatPosition)
    {
        SnakeComponent EatingSnake = GetSnakeAt(eatPosition);

        EatingSnake.Increase = true;
    }