示例#1
0
 void OnTriggerEnter(Collider target)
 {
     if (target.tag == "food")
     {
         target.gameObject.SetActive(false);
         Food.instance.MoveFood(target.gameObject.name);
         script_ref.AddBody();
     }
     if (target.tag == "wall" || target.tag == "body")
     {
         Debug.Log("gameover");
         script_ref.StopMove();
         Invoke("GameOver", 0.5f);
     }
 }
示例#2
0
        //
        public void Update(GameTime gameTime)
        {
            // Always rotate the blob
            _blob.Rotate(-4);
            // Move delay logic based on how big the gid is, how much score you have, divide it with what speed tha player has chosen
            // You move faster when the grid area expands, you move slower when the score gets higher
            moveDelay = (1000 / (float)Math.Sqrt(_grid.Length) + (float)Math.Sqrt(Score)) / options.Speed;

            // If game is active, then...
            if (GameActive)
            {
                // Determine what controls that shall be used for the head
                if (!options.ControlsHold)
                {
                    // Checks for input and then locks it, until the keys are up
                    if (input.LeftGame && !keyPressed)
                    {
                        leftKey    = true;
                        keyPressed = true;
                    }
                    // Checks for input and then locks it, until the keys are up
                    else if (input.RightGame && !keyPressed)
                    {
                        rightKey   = true;
                        keyPressed = true;
                    }
                    // Checks for input and then locks it, until the keys are up
                    if (!input.LeftGame && !input.RightGame)
                    {
                        keyPressed = false;
                    }
                }

                // When it's time to move the body
                if (moveTimer <= 0)
                {
                    // Looks at what controls that shall be used for the head
                    if (options.ControlsHold)
                    {
                        _snake.ControlHeadDirection(input.LeftGame, input.RightGame);
                    }
                    else
                    {
                        // LeftKey and RightKey are bools from the if-statement at line 96
                        _snake.ControlHeadDirection(leftKey, rightKey);
                    }
                    // Move the Tail and the rest of the body
                    _snake.MoveBody();
                    // Move the head based on what direction the head has (determined by the if(options.ControlsHold) statements)
                    _snake.MoveHead(_snake.SnakeHead.Direction);

                    // If the snake head collides with a blob
                    if (_snake.CheckCollisionBlob(_blob.X, _blob.Y))
                    {
                        // Spawn a new blob, but if the blob spawn inside the tail, body or head, redo the spawning
                        // I've found out that this doesn't always work...
                        foreach (Body bodyPart in _snake.SnakeBodyParts)
                        {
                            do
                            {
                                _blob.Spawn();
                            } while (_blob.X == _snake.SnakeHead.X && _blob.Y == _snake.SnakeHead.Y ||
                                     _blob.X == _snake.SnakeTail.X && _blob.Y == _snake.SnakeTail.Y ||
                                     _blob.X == bodyPart.X && _blob.Y == bodyPart.Y);
                        }
                        // Add a score point
                        Score++;
                        // Add a body
                        _snake.AddBody();
                        // Make the camera move up (top view) for 4 movements
                        blobCameraTimer = moveDelay * 4.1f;
                        // Make the camera top view bools true
                        eatBlob       = true;
                        eatBlobReturn = true;

                        // Checks if the score is more or equal than half of the grid area
                        if (Score >= (_grid.Length * _grid.Length) / 2)
                        {
                            // If it is, add 2 more in grid length (expand the area with one grid module on each side)
                            _grid.Length += 2;
                            // Update the grid models
                            _grid.Generate(_grid.Length);
                            // Update the edge models
                            _grid.GenerateEdge(_grid.Length);
                        }
                    }

                    // If snake collide with edge of the map or collide with it's own body
                    if (_snake.CheckCollisionEdge(_grid.EdgeDistanceFromOrigin(_grid.Length)) || _snake.CheckCollisionBody())
                    {
                        // Make the game over
                        GameOver = true;
                        // Game shall not update anymore
                        GameActive = false;
                        // Return, this is because to block the models from updating to their next position, like say if you collide with edge, then the player would see a snake head outside the border
                        return;
                    }
                    // Update all visual models to their new position
                    _snake.UpdateMovement();
                    // Reset the key presses for options.ControlsHold at line 96
                    leftKey  = false;
                    rightKey = false;

                    // If cameraTopView timer is less than move delay (In other words, when the timer is on it's last move out of four) and Return is true, make it false and eatBlob true
                    if (blobCameraTimer < moveDelay && eatBlobReturn)
                    {
                        eatBlobReturn = false;
                        eatBlob       = true;
                    }

                    // If eatBlob is true (or as stated in line 191, when Return is true and last round of four),
                    // then make the camera have springiness (for making a smooth movement when camera change position from third to top or vice versa)
                    // Also make the camera go smooth when the snake turns
                    if (_snake.SnakeHead.Turn == 0 || _snake.SnakeHead.Turn == 2 || eatBlob)
                    {
                        ((ChaseCamera)_camera).Springiness = 0.15f;
                        eatBlob = false;
                    }
                    else
                    {
                        // If snake is going straight and have not eaten blob, the camera is instant
                        ((ChaseCamera)_camera).Springiness = 1f;
                    }

                    // At end, reset the timer to it's delay (see line 90)
                    moveTimer = moveDelay;
                }
                else
                {
                    // If timer is not under zero, make the timer go down until it gets to zero
                    moveTimer -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                }
                // Always make the TopViewTimer subtract with elapsed time
                blobCameraTimer -= (float)gameTime.ElapsedGameTime.TotalMilliseconds;

                // If there is time left for TopView
                if (blobCameraTimer > 0)
                {
                    // Make the camera rise 8 times the height of the normal height times a scale (based on when the grid was 13)
                    ((ChaseCamera)_camera).PositionOffset = new Vector3(0, 600 * 8 * ((float)_grid.Length / 13), 800);
                }
                else
                {
                    // Make the camera have normal height
                    ((ChaseCamera)_camera).PositionOffset = new Vector3(0, 600, 800);
                }
                // Make the camera focus a little bit above the snakeHead
                ((ChaseCamera)_camera).TargetOffset = new Vector3(0, 200, 0);
                // Make the camera move accordingly by snake head's movement
                ((ChaseCamera)_camera).Move(_snake.SnakeHead.CustomModel.Position, _snake.SnakeHead.CustomModel.Rotation);
            }
            // If the game is not active
            else if (!GameActive)
            {
                // Make the camera have a slow movement
                ((ChaseCamera)_camera).Springiness = 0.05f;

                // A scale (based on when the grid was 13)
                float scale = (float)_grid.Length / 13;
                // Rotation change updates and changes, well, rotation, at the speed of 180 divided by 1500
                _rotationChange += new Vector3(0, (float)Math.PI / 1500, 0);
                // Zoom out from the play area (based on scale)
                ((ChaseCamera)_camera).PositionOffset = new Vector3(0, 2000 * scale, 4000 * scale);
                // Focus the camera on the left side of the map
                ((ChaseCamera)_camera).TargetOffset = new Vector3(-1000 * scale, 0, 0);
                // Move the camera based on center position and rotation from snakeHead and rotationChange (snakeHead is in there so that the camera will always zoom back from the snake, and not making a 180)
                ((ChaseCamera)_camera).Move(Vector3.Zero, _snake.SnakeHead.CustomModel.Rotation + _rotationChange);
            }
        }