private void PreCreateMoles()
    {
        /* To ensure all moles are on the screen are visible I create them from the bottom up depending on the grid
         * So I first calculate the bottom middle position as a starting point
         */
        Vector3 botMid = new Vector3((_spawnLimitBotLeft.x + _spawnLimitTopRight.x) / 2, _spawnLimitBotLeft.y, _spawnLimitBotLeft.z);

        /* Then I calculate the maximum size of the squares to fill the field
         */
        float screenWorldWidth   = _spawnLimitTopRight.x - _spawnLimitBotLeft.x;
        float screenWorldHeight  = _spawnLimitTopRight.z - _spawnLimitBotLeft.z;
        float possibleMoleWidth  = screenWorldWidth / _gridSize.x;
        float possibleMoleHeight = screenWorldHeight / _gridSize.y;
        float moleSize           = possibleMoleWidth < possibleMoleHeight ? possibleMoleWidth : possibleMoleHeight;

        /* Using these values I can precreate objects so I dont have to recalculate this at runtime
         */
        for (int x = 0; x < _gridSize.x; x++)
        {
            for (int y = 0; y < _gridSize.y; y++)
            {
                Vector3    position = new Vector3(botMid.x - moleSize * _gridSize.x / 2 + moleSize / 2 + x * moleSize, _spawnLimitBotLeft.y, _spawnLimitBotLeft.z + moleSize / 2 + y * moleSize);
                MoleEntity newMole  = Instantiate(_molePrefab, position, Quaternion.identity, transform);
                newMole.Setup(moleSize);
                _inactiveMoles.Add(newMole);
            }
        }
    }
示例#2
0
    private void CheckPositionForMole(Vector3 position)
    {
        Ray ray = _mainCamera.ScreenPointToRay(position);

        if (Physics.Raycast(ray, out RaycastHit raycastHit, 1000, _moleLayerMask))
        {
            MoleEntity moleController = raycastHit.collider.transform.GetComponentInParent <MoleEntity>();
            MoleHit(moleController);
        }
    public MoleEntity SpawnNewMole()
    {
        if (_inactiveMoles.Count == 0)
        {
            return(null);
        }

        MoleEntity MoleToSpawn = _inactiveMoles[Random.Range(0, _inactiveMoles.Count - 1)];

        ActivateMole(MoleToSpawn);
        return(MoleToSpawn);
    }
 public void ActivateMole(MoleEntity moleToActivate)
 {
     _inactiveMoles.Remove(moleToActivate);
     _activeMoles.Add(moleToActivate);
     moleToActivate.SetBehaviour(GetRandomMoleBehaviour());
 }
 public void DisableMole(MoleEntity moleToDisable)
 {
     _activeMoles.Remove(moleToDisable);
     _inactiveMoles.Add(moleToDisable);
     moleToDisable.SetInactive();
 }