示例#1
0
    public Egg CreateEgg()
    {
        GameObject eggPrefab = this.config.EggPrefab;
        Egg        egg       = SerpentUtils.Instantiate <Egg>(eggPrefab, this.transform);

        return(egg);
    }
示例#2
0
    private void CreateObject()
    {
        GameObject o = SerpentUtils.Instantiate(this.prefab, this.transform);

        DontDestroyOnLoad(o);
        o.SetActive(false);
        this.objects.Add(o);
    }
示例#3
0
    private Snake CreateSnake(SnakeConfig config, int length)
    {
        Snake snake = SerpentUtils.Instantiate <Snake>(this.snakePrefab, this.mazeController.transform);

        snake.SetUp(this.mazeController, config, length);
        snake.SnakeSegmentsChanged += this.NumSnakeSegmentsChanged;
        snake.SnakeSegmentEaten    += this.SnakeSegmentEaten;
        return(snake);
    }
示例#4
0
    private void CreateFrog()
    {
        this.frog = SerpentUtils.Instantiate <Frog>(this.frogPrefab, this.mazeController.transform);
        if (this.frog == null)
        {
            return;
        }

        this.frog.CreatureDied += FrogDied;
        this.frog.SetUp(this, this.mazeController);

        // Randomize frog placement.
        int x = 0;
        int y = 0;

        if (UnityEngine.Random.Range(0, 2) == 0)
        {
            // top or bottom edge
            if (UnityEngine.Random.Range(0, 2) == 0)
            {
                y = this.mazeController.Maze.Height - 1;
            }

            // avoid spawning right in the corners
            x = 1 + UnityEngine.Random.Range(0, this.mazeController.Maze.Width - 1);
        }
        else
        {
            // left or right edge
            if (UnityEngine.Random.Range(0, 2) == 0)
            {
                x = this.mazeController.Maze.Width - 1;
            }

            // avoid spawning right in the corners
            y = 1 + UnityEngine.Random.Range(0, this.mazeController.Maze.Height - 1);
        }

        PlaceCreature(this.frog, x, y, Direction.N);
    }
示例#5
0
    public void AddSegment()
    {
        // Don't add a segment if the snake will become bigger than should be possible.
        if (this.Length == SerpentConsts.MaxSnakeLength)
        {
            return;
        }

        // This method should add a new segment at the end of the snake.  It can be in the same position
        // as the last segment and will appear when the now next-to-last segment moves away from
        // its current position.
        SnakeSegment newSegment = null;

        if (this.head == null)
        {
            this.head        = SerpentUtils.Instantiate <SnakeHead>(this.config.HeadPrefab, this.transform);
            newSegment       = this.head;
            newSegment.Snake = this;
        }
        else
        {
            SnakeBody newBodySegment = Managers.SnakeBodyCache.GetObject <SnakeBody>();
            newSegment = newBodySegment;

            newBodySegment.Reset();
            newBodySegment.SetParent(this);
            newBodySegment.Snake = this;
            newBodySegment.SetSpriteDepth(this.head.GetSpriteDepth());

            // If a tail already exists for this snake (it's not just a head) then that segment's sprite name will need
            // to be changed
            string[] bodySpriteNames = this.config.BodySpriteNames;
            // Subtract one from length for the head, and one to switch to 0-based array index.
            int bodySpriteIndex = (this.length - 2) % bodySpriteNames.Length;

            SnakeSegment tail = this.Tail;

            /*
             * if (tail != null)
             * {
             *      tail.SetSpriteName(bodySpriteNames[bodySpriteIndex]);
             * }
             *
             * // Now set the sprite name for the new segment.
             * if (this.config.TailSpriteName != null && this.config.TailSpriteName.Length > 0)
             * {
             *      newBodySegment.SetSpriteName(this.config.TailSpriteName);
             * }
             * else
             * {
             *      bodySpriteIndex = (bodySpriteIndex + 1) % bodySpriteNames.Length;
             *      newBodySegment.SetSpriteName(bodySpriteNames[bodySpriteIndex]);
             * }
             */


            bodySpriteIndex = (bodySpriteIndex + 1) % bodySpriteNames.Length;
            newBodySegment.SetSpriteName(bodySpriteNames[bodySpriteIndex]);


            SnakeSegment previousSegment = tail;
            if (previousSegment == null)
            {
                previousSegment = this.head;
            }

            previousSegment.NextSegment = newBodySegment;

            // NOTE, assuming that the segments are the same width and height here.
            SnakeBody lastBodySegment = previousSegment as SnakeBody;
            // Subtract 1 from distance to get the segments to overlap just a bit.
            float distance = previousSegment.Height * 0.5f + newBodySegment.Height * 0.5f - 1.0f;
            if (lastBodySegment != null)
            {
                distance += lastBodySegment.DistanceFromHead;
            }
            newBodySegment.DistanceFromHead = distance;

            if (lastBodySegment != null)
            {
                lastBodySegment.MoveEgg(newBodySegment);
            }
        }

        newSegment.Visible = this.visible;
        this.length++;

        if (this.config.Player)
        {
            int   firstColourIndex = (this.Length - 1) % (SerpentConsts.PlayerSegmentColours.Length);
            Color firstColor       = SerpentConsts.PlayerSegmentColours[firstColourIndex];

            int   secondColourIndex = (firstColourIndex + 1) % (SerpentConsts.PlayerSegmentColours.Length);
            Color secondColor       = SerpentConsts.PlayerSegmentColours[secondColourIndex];

            newSegment.SetGradatedColour(firstColor, secondColor);
        }
        else
        {
            newSegment.Colour = this.colour;
        }

        if (this.SnakeSegmentsChanged != null)
        {
            this.SnakeSegmentsChanged(this);
        }
        UpdateSpeed();
        UpdateValues();
    }