Ejemplo n.º 1
0
    public void updateGrid(Tetrimo tetrimo)
    {
        for (int y = 0; y < height; ++y)
        {
            for (int x = 0; x < width; ++x)
            {
                Debug.Log(x + "," + y);
                if (grid[x, y] != null)
                {
                    if (grid[x, y].parent == tetrimo.transform)
                    {
                        grid[x, y] = null;
                    }
                }
            }
        }


        foreach (Transform minimo in tetrimo.transform)
        {
            Vector2 pos = tetrimo.round(minimo.position);
            //Debug.Log((int)pos.x+","+(int)pos.y);
            grid[(int)pos.x, (int)pos.y] = minimo;
        }
    }
Ejemplo n.º 2
0
    public void SpawnPiece()
    {
        Vector3 spawnPosition = new Vector3(transform.position.x * -1f * GameState.Instance.Direction.x,
                                            transform.position.y * -1f * GameState.Instance.Direction.y,
                                            0f);
        Tetrimo spawnedPiece = _nextPiece;

        spawnedPiece.transform.position   = spawnPosition;
        spawnedPiece.transform.rotation   = transform.rotation;
        spawnedPiece.transform.parent     = _playArea.transform;
        spawnedPiece.transform.localScale = Vector3.one;
        spawnedPiece.Enable();

        if (GameState.Instance.PlayArea.CheckInterception(spawnedPiece.Parts))
        {
            GameState.Instance.GameOver();
            return;
        }

        GameState.Instance.AddPiece(spawnedPiece);

        _currentPiece            = spawnedPiece;
        _currentPiece.OnStopped += HandleCurrentPieceStopped;



        _pieceID++;
        PrepareNextPierce();
    }
Ejemplo n.º 3
0
 private void setupTetrimo(ref Tetrimo t)
 {
     t.TetrimoPartPrefab      = Resources.Load("TetrimoPartPrefab") as GameObject;
     t.TetrimoPrefab          = Resources.Load("TetrimoPrefab") as GameObject;
     t.TetrimoExplosionPrefab = Resources.Load("TetrimoExplosionPrefab") as GameObject;
     t.FourLinesLabelPrefab   = Resources.Load("FourLinesLabelPrefab") as GameObject;
 }
Ejemplo n.º 4
0
    private void PrepareNextPierce()
    {
        int pieceIdx = Mathf.RoundToInt(Random.Range(0, tetrimosPrefabs.Count));

        _nextPiece = Instantiate(tetrimosPrefabs[pieceIdx], nextPieceUIRoot, false);

        _nextPiece.name = _pieceID.ToString() + "_" + tetrimosPrefabs[pieceIdx].name;
        _nextPiece.Disable();
    }
    public void CheckIntegrity()
    {
        HashSet <int> verticalPiecesIDs = new HashSet <int>();

        //check if they have holes
        foreach (TetrimoPart part in Parts)
        {
            verticalPiecesIDs.Add(PlayArea.PositionToGrid(part.transform.position).y);
        }

        List <int> verticalPiecesIDsList = verticalPiecesIDs.ToList();

        verticalPiecesIDsList.Sort();

        bool doSplit = false;

        List <TetrimoPart> newPieceParts = new List <TetrimoPart>();

        for (int i = 1; i < verticalPiecesIDsList.Count; i++)
        {
            if (verticalPiecesIDsList[i] != verticalPiecesIDsList[i - 1] + 1)
            {
                doSplit = true;
            }

            if (doSplit)
            {
                List <TetrimoPart> toSwap = Parts.Where(part => GameState.Instance.PlayArea.PositionToGrid(part.transform.position).y == verticalPiecesIDsList[i]).ToList();
                toSwap.ForEach(part => Parts.Remove(part));
                newPieceParts.AddRange(toSwap);
            }
        }

        if (!doSplit)
        {
            return;
        }

        GameObject newGOPiece        = new GameObject(name + "_part2");
        Transform  newTransformPiece = newGOPiece.GetComponent <Transform>();

        newTransformPiece.position = transform.position;
        Tetrimo newPiece = newGOPiece.AddComponent <Tetrimo>();

        newPiece.baseColor = baseColor;
        newPiece.Parts     = newPieceParts;
        newPiece.config    = config;
        newPiece._state    = State.Stopped;
        newPieceParts.ForEach(part => part.transform.parent = newTransformPiece);
        newTransformPiece.parent = transform.parent;

        GameState.Instance.InstancedTetrimos.Add(newPiece);

        name += "_part1";
    }
Ejemplo n.º 6
0
    private void HandleCurrentPieceStopped(Tetrimo tetrimo)
    {
        _currentPiece.OnStopped -= HandleCurrentPieceStopped;
        _currentPiece            = null;

        if (GameState.Instance.CurrentStage == GameState.Stage.Playing)
        {
#if UNITY_EDITOR
            if (GameState.Instance.Config.AutomaticSpawn)
#endif
            {
                SpawnPiece();
            }
        }
    }
    void ActivateAndCreateNewPreview() {
        GameObject newGameObject = (GameObject)Instantiate(TetrimoPrefab, Vector3.zero, Quaternion.identity);
        Tetrimo newFallingTetrimo       = newGameObject.GetComponent<Tetrimo>();
        newFallingTetrimo.RotationIndex = NextTetrimo.RotationIndex;
        newFallingTetrimo.ShapeIndex    = NextTetrimo.ShapeIndex;
        newFallingTetrimo.State         = TetrimoState.Spawning;

        foreach (Transform child in newFallingTetrimo.transform) {
            Destroy(child.gameObject);
        }
        Destroy(NextTetrimo.gameObject);

        newGameObject = (GameObject)Instantiate(TetrimoPrefab, Vector3.zero, Quaternion.identity);
        Tetrimo newPreviewTetrimo = newGameObject.GetComponent<Tetrimo>();
        newPreviewTetrimo.State = TetrimoState.Preview;
    }
Ejemplo n.º 8
0
    public GameObject CreateTetrimo(string tetrimoName, int[,] tetrimoLayout, int layoutIndex)
    {
        GameObject tetrimo = new GameObject(TetrimoGOName);

        for (int i = tetrimoLayout.GetLowerBound(0); i <= tetrimoLayout.GetUpperBound(0); i++)
        {
            for (int j = tetrimoLayout.GetLowerBound(1); j <= tetrimoLayout.GetUpperBound(1); j++)
            {
                if (tetrimoLayout[i, j] == 1)
                {
                    drawBaseBlock(i, j, tetrimo.transform);
                }
            }
        }

        Tetrimo t = tetrimo.AddComponent <Tetrimo>();

        t.BlockPositions = TetrimoLayoutDictionary[tetrimoName];
        t.CurrentIndex   = layoutIndex;
        t.ApplyColour();
        return(tetrimo);
    }
    void Start () {
        switch (State) {
        case TetrimoState.Spawning: // when the start of the game is made, you automatically start a spawning tetrimo 
            NextFall = FallingCooldown;
            AgentAction();

            // Create shape and translate it at the center top of the game field.
            // REMARK: Do not use translate here! Instantiate create a copy of the current object.
            CreateShape();
            transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0); //you create a shape with the 3D position of the field size to the right/2, etc.

            //3D position that is transformed to the top 

            // Check if the player has lost the game
            foreach (Transform child in transform) {
                if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null) { //if the field matrx position of y and x is not equal to the null, meaning that 
                	//there is something at these positions, then that means that the game has ended 
                    AgentAction();
                    Application.LoadLevel("GameOver");
                }
            }
            name = "Tetrimo#" + (TetrimoCount-1);
            State = TetrimoState.Falling;
            break;

        case TetrimoState.Preview:
            RotationIndex = Random.Range(0, Shapes.GetLength(1));
            ShapeIndex    = Random.Range(0, Shapes.GetLength(0));
            CreateShape();
            transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y);

            NextTetrimo = this;

            TetrimoCount++;
            name = "Tetrimo#" + TetrimoCount;
            break;
        }
	}
Ejemplo n.º 10
0
    void Start()
    {
        switch (State)
        {
        case TetrimoState.Spawning:
            NextFall = FallingCooldown;

            // Create shape and translate it at the center top of the game field.
            // REMARK: Do not use translate here! Instantiate create a copy of the current object.
            CreateShape();
            transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0);

            // Check if the player has lost the game
            foreach (Transform child in transform)
            {
                if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null)
                {
                    Application.LoadLevel("GameOver");
                }
            }
            name  = "Tetrimo#" + (TetrimoCount - 1);
            State = TetrimoState.Falling;
            break;

        case TetrimoState.Preview:
            RotationIndex = Random.Range(0, Shapes.GetLength(1));
            ShapeIndex    = Random.Range(0, Shapes.GetLength(0));
            CreateShape();
            transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y);

            NextTetrimo = this;

            TetrimoCount++;
            name = "Tetrimo#" + TetrimoCount;
            break;
        }
    }
Ejemplo n.º 11
0
    void Start () {
        switch (State) {
        case TetrimoState.Spawning:
            NextFall = FallingCooldown;

            // Create shape and translate it at the center top of the game field.
            // REMARK: Do not use translate here! Instantiate create a copy of the current object.
            CreateShape();
            transform.position = new Vector3((int)(FieldSize.Right / 2), FieldSize.Top - 1, 0);

            // Check if the player has lost the game
            foreach (Transform child in transform) {
                if (FieldMatrix[(int)child.position.y, (int)child.position.x] != null) {
                    Application.LoadLevel("GameOver");
                }
            }
            name = "Tetrimo#" + (TetrimoCount-1);
            State = TetrimoState.Falling;
            break;

        case TetrimoState.Preview:
            RotationIndex = Random.Range(0, Shapes.GetLength(1));
            ShapeIndex    = Random.Range(0, Shapes.GetLength(0));
            CreateShape();
            transform.position = new Vector3(PreviewHUD.x, PreviewHUD.y);

            NextTetrimo = this;

            TetrimoCount++;
            name = "Tetrimo#" + TetrimoCount;
            break;
        }
	}
Ejemplo n.º 12
0
 private void OnPieceStopped(Tetrimo tetrimo)
 {
     tetrimo.OnStopped -= OnPieceStopped;
     _fallingIsDirty    = true;
 }
Ejemplo n.º 13
0
 public void PlayStopped(Tetrimo t)
 {
     Debug.Assert(Camera.main != null, "Camera.main != null");
     AudioSource.PlayClipAtPoint(clips[(int)Clips.Stopped], Camera.main.transform.position);
 }
Ejemplo n.º 14
0
 public void PlayHorizontalMove(Tetrimo t)
 {
     Debug.Assert(Camera.main != null, "Camera.main != null");
     AudioSource.PlayClipAtPoint(clips[(int)Clips.Move], Camera.main.transform.position);
 }