private void Start()
    {
        AllObjects = new List <SimpleGameObject>();

        SpatialHashingInstance = new SpatialHashingClass(FieldSize, Columns);

        ThreadsDictionary = new Dictionary <int, List <SimpleGameObject> >();

        SetPlayerData();

        CalculateCameraFrustrumCorners();

        TryLoadingAsteroidsDataFromFile();

        for (int x = 0; x < FieldSize; x++)
        {
            for (int y = 0; y < FieldSize; y++)
            {
                var asteroidLocalPosition = new Vector2((x * 3), (y * 3));

                if (!(Vector2.Distance(asteroidLocalPosition, new Vector2(Player.localPosition.x, Player.localPosition.y)) > 1.0f))
                {
                    continue;
                }
                if (!CheckIfPositionIsNotInFrustrum(asteroidLocalPosition))
                {
                    var objInst            = Instantiate(Asteroid, new Vector3(asteroidLocalPosition.x, asteroidLocalPosition.y, 0), Quaternion.identity, GridContainer.transform);
                    var asteroidController = objInst.GetComponent <AsteroidController>();
                    asteroidController.Controller = this;

                    SetAsteroidDirectionAndSpeedIfSaveDoesNotExist(x, y);

                    asteroidController.AsteroidSimpleGameObject = new SimpleGameObject(objInst.transform.position, objInst.transform.position, new Vector2(AsteroidsData.AsteroidXDirection[x, y], AsteroidsData.AsteroidYDirection[x, y]), 0.9f, AsteroidsData.AsteroidSpeed[x, y], SimpleGameObjectTypeEnum.Asteroid, asteroidController);

                    AllObjects.Add(asteroidController.AsteroidSimpleGameObject);

                    SpatialHashingInstance.Insert(asteroidController.AsteroidSimpleGameObject, asteroidController.AsteroidSimpleGameObject);
                }
                else
                {
                    SpawnPointsOutsidePlayerFrustrum.Add(asteroidLocalPosition);

                    SetAsteroidDirectionAndSpeedIfSaveDoesNotExist(x, y);

                    var newSimpleObject = new SimpleGameObject(asteroidLocalPosition, asteroidLocalPosition, new Vector2(AsteroidsData.AsteroidXDirection[x, y], AsteroidsData.AsteroidYDirection[x, y]), 0.9f, AsteroidsData.AsteroidSpeed[x, y], SimpleGameObjectTypeEnum.Asteroid, null);

                    AllObjects.Add(newSimpleObject);                                 //Commenting these two lines will make the app spawn only those asteroids that are visible in player frustrum (only then collision works as it should)
                    SpatialHashingInstance.Insert(newSimpleObject, newSimpleObject); //Commenting these two lines will make the app spawn only those asteroids that are visible in player frustrum (only then collision works as it should)
                }
            }
        }

        SetThreadsDictionary();

        Finished = true;

        Player.GetComponent <PlayerController>().IsAlive = true;

        SaveAsteroidsIfNoSaveFound();
    }
    private void DefineAndStartCollisionBehaviour(SimpleGameObject simpleObject, SimpleGameObject nearestSimpleObject)
    {
        switch (simpleObject.SimpleObjectController.GetType())
        {
        case 0:      //Asteroid
            if (nearestSimpleObject.Type == SimpleGameObjectTypeEnum.Asteroid || nearestSimpleObject.Type == SimpleGameObjectTypeEnum.Bullet)
            {
                simpleObject.SimpleObjectController.Collided();
            }
            break;

        case 1:      //Player
            if (nearestSimpleObject.Type == SimpleGameObjectTypeEnum.Asteroid)
            {
                simpleObject.SimpleObjectController.Collided();
            }
            break;

        case 2:      //Bullet
            if (nearestSimpleObject.Type == SimpleGameObjectTypeEnum.Asteroid)
            {
                simpleObject.SimpleObjectController.Collided();
            }
            break;

        default:
            break;
        }
    }
    private GameObject CreatePaddle(SimpleGameObject paddleVO, GameObject prefab)
    {
        GameObject paddle = Instantiate <GameObject>(prefab);

        paddle.transform.position = new Vector3(paddleVO.x, paddle.transform.position.y, 2);
        return(paddle);
    }
 public GameLogic()
 {
     ball    = new SimpleGameObject();
     paddle1 = new SimpleGameObject();
     paddle2 = new SimpleGameObject();
     Restart();
     hasStarted = false;
 }
 private void DealWithIt(SimpleGameObject newPosList, SimpleGameObject nearest)
 {
     ExecuteOnMainThread.Enqueue(() =>
     {
         StartCoroutine(DealWithAsteroidThatCollided(newPosList));
         StartCoroutine(DealWithAsteroidThatCollided(nearest));
     });
 }
 public void UpdatePaddleModel(GameLogic gameModel)
 {
     if (playerNumber == 1)
     {
         this.paddleModel = gameModel.paddle1;
     }
     else
     {
         this.paddleModel = gameModel.paddle2;
     }
 }
 public void SetPaddleByPlayerNumber(short playerNumber, SimpleGameObject paddle)
 {
     if (playerNumber == 1)
     {
         this.paddle1 = paddle;
     }
     else
     {
         this.paddle2 = paddle;
     }
 }
    private IEnumerator DealWithAsteroidThatCollided(SimpleGameObject asteroid)
    {
        yield return(Delay);

        if (asteroid != null)
        {
            asteroid.OldPosition = SpawnPointsOutsidePlayerFrustrum[UnityEngine.Random.Range(0, SpawnPointsOutsidePlayerFrustrum.Count)];
            asteroid.NewPosition = asteroid.OldPosition;

            asteroid.HasCollided = false;
        }
    }
 /// <summary>
 /// Updates the opponent AI when playing Single Player
 /// </summary>
 private void UpdateAI()
 {
     if (gameLogic != null)
     {
         SimpleGameObject ballObj    = gameLogic.ball;
         SimpleGameObject paddle2Obj = gameLogic.paddle2;
         if (ballObj.x > paddle2Obj.x)
         {
             gameLogic.MovePaddle2(true);
         }
         else if (ballObj.x < paddle2Obj.x)
         {
             gameLogic.MovePaddle2(false);
         }
     }
 }
 public void SetGameModel(String gameId, GameLogic gameModel, short playerNumber)
 {
     if (playerNumber == 1)
     {
         this.paddleModel        = gameModel.paddle1;
         this.movePaddleFunction = gameModel.MovePaddle1;
     }
     else
     {
         this.paddleModel        = gameModel.paddle2;
         this.movePaddleFunction = gameModel.MovePaddle2;
     }
     this.playerNumber     = playerNumber;
     this.gameId           = gameId;
     this.gameModel        = gameModel;
     this.lowerBoundGridX  = gameModel.lowerBoundGridX;
     this.higherBoundGridX = gameModel.higherBoundGridX;
 }
 public void Restart()
 {
     arrayAnglesCurrentIndex = 0;
     hasStarted   = true;
     gameOver     = false;
     paddle1.x    = higherBoundGridX / 2;
     paddle1.y    = 0;
     ball.x       = higherBoundGridX / 2;
     ball.y       = 0;
     paddle2.x    = higherBoundGridX / 2;
     paddle2.y    = higherBoundGridY;
     currentTick  = 0;
     bounceAngle  = FIXED_ANGLE;
     lastBounce   = new SimpleGameObject();
     lastBounce.x = ball.x;
     lastBounce.y = ball.y;
     up           = true;
     right        = true;
     isGoalPause  = false;
 }
    public void Copy(GameLogic gameLogic)
    {
        ball       = gameLogic.ball;
        lastBounce = gameLogic.lastBounce;
        paddle1    = gameLogic.paddle1;
        paddle2    = gameLogic.paddle2;

        hasStarted       = gameLogic.hasStarted;
        gameOver         = gameLogic.gameOver;
        player1Connected = gameLogic.player1Connected;
        player2Connected = gameLogic.player2Connected;

        winner = gameLogic.winner;

        speed                   = gameLogic.speed;
        currentTick             = gameLogic.currentTick;
        bounceAngle             = gameLogic.bounceAngle;
        paused                  = gameLogic.paused;
        arrayAngles             = gameLogic.arrayAngles;
        arrayAnglesCurrentIndex = gameLogic.arrayAnglesCurrentIndex;
        sizeArrayAngles         = gameLogic.sizeArrayAngles;
    }