Пример #1
0
    void Move(EDirecton dir)
    {
        _position = GenerateLevel.FloatToIntCoordinates(new Vector2(Player.transform.position.x, Player.transform.position.z));


        if (DebugPos != null)
        {
            var floatPaintPos = GenerateLevel.IntToFloatCoordinates(_position);

            DebugPos.transform.position = new Vector3(floatPaintPos.x, DebugPos.transform.position.y, floatPaintPos.y);
            if (_map.Access(_position) == 0)
            {
                DebugPos.GetComponent <MeshRenderer>().material.color = Color.blue;
            }
            else
            {
                DebugPos.GetComponent <MeshRenderer>().material.color = Color.red;
            }
        }

        if (_dirMove.magnitude < double.Epsilon)
        {
            switch (dir)
            {
            case EDirecton.UP:
                _dirMove = Vector2.up;
                break;

            case EDirecton.DOWN:
                _dirMove = Vector2.down;
                break;

            case EDirecton.LEFT:
                _dirMove = Vector2.left;
                break;

            case EDirecton.RIGHT:
                _dirMove = Vector2.right;
                break;

            default:
                break;
            }
        }
        if (_dirMove.magnitude >= double.Epsilon)
        {
            Ray r = new Ray();
            r.origin    = Player.transform.position;
            r.direction = new Vector3(_dirMove.x, 0.0f, _dirMove.y);
            RaycastHit hit;
            if (Debug != null)
            {
                Debug.transform.position = Player.transform.position + new Vector3(_dirMove.x, 0.0f, _dirMove.y);
            }
            if (Physics.Raycast(r, out hit) && hit.distance < 0.75f) // wall _dirMove = 0
            {
                _dirMove = Vector2.zero;
                _lastDir = EDirecton.NONE;

                // We realign the position with the grid to avoid accumulating errors
                var newFloatCoords = GenerateLevel.IntToFloatCoordinates(_position);
                Player.transform.position = new Vector3(newFloatCoords.x, Player.transform.position.y, newFloatCoords.y);
            }
            else
            {
                Player.transform.position = Player.transform.position + new Vector3(_dirMove.x, 0.0f, _dirMove.y) * Time.deltaTime * Speed;

                if (_map.Access(_position) == 0)
                {
                    var floatPaintPos = GenerateLevel.IntToFloatCoordinates(_position);
                    var paintGround   = GameObject.Instantiate(PaintGroundPrefab);
                    paintGround.transform.position = new Vector3(floatPaintPos.x, paintGround.transform.position.y, floatPaintPos.y);
                    _map.Set(_position, -1);
                    _toFillCount++;

                    var particle = GameObject.Instantiate(ParticlePrefab);
                    particle.transform.position = new Vector3(floatPaintPos.x, paintGround.transform.position.y, floatPaintPos.y);
                }
            }
        }
    }