Code Monkey home page Code Monkey logo

2048-unity's People

Contributors

jamiltron avatar lorenalexm avatar nicoco007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

2048-unity's Issues

Remove duplicated code and clean up

We can probably get rid of the int grid and the variable that tracks current tiles and instead use a collection of gameObjects to represent the tiles, and then instead of shifting tiles one grid by one grid we can just raycast from the grid to the edge of the board to determine slides/upgrades.

How to make the Tiles slide slowly?

in your code it moves from one position to another in upgradegrid() method, but things compile so fast it doesnt look like it is moving from 3-2-1-0 i looks like as if it is moving from 3 to 0, is there a way to slow this thing? i tried ienumrator it just hangs the game...anyother way to delay action so it is visible the way the tile is moving.

Tried to make 6x6 based on your script

hey i have been trying to make 2048 in unity, i am new to multidimensional array concept, however i tried few things to make it work in 6x6 grid and my object is not getting recogonized in the gridposition, will you have a look at my code?

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

public GameObject tile;
public GameObject objs;
public int GridX,GridY;
public int[,] Area;
public int currerntTileAmount = 0;
private static float spaceBetweenTiles = 1f;
private static Vector3 horizontalRay = new Vector3(1f, 0f, 0f);
private static Vector3 verticalRay = new Vector3(0f, 1f, 0f);

// Use this for initialization
void Start () {
    Area = new int[GridX,GridY];
    for(int x = 0;x<GridX;x++)
    {
        for(int y = 0;y<GridY;y++)
        {
            GameObject go = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity) as GameObject;
            //go.gameObject.name = "Orb";
            go.transform.parent = this.transform;
            //Area.Add(go);
        }
    }
    gameObject.transform.position = new Vector3(0f,0f,0f);

    generateRandomTiles();
    //generateRandomTiles();
}

// Update is called once per frame
void Update () {
    if(Input.GetKeyDown(KeyCode.LeftArrow))
    {
        MoveTilesLeft();
    }
}

private static Vector2 GridToWorldPoint(int x, int y) {

// return new Vector2(x + horizontalSpacingOffset + borderSpacing * x,
// -y + verticalSpacingOffset - borderSpacing * y);
Debug.Log("Grid Pos:"+x+" "+y);
return new Vector2(x,y);
}

private static Vector2 WorldToGridPoint(float x, float y) {

// return new Vector2((x - horizontalSpacingOffset) / (1 + borderSpacing),
// (y - verticalSpacingOffset) / -(1 + borderSpacing));
Debug.Log(x+" "+y);
return new Vector2(x,y);

}

private void UpdateGrid(GameObject currentTile, Vector2 amountToMove) {
    Transform tileTransform = currentTile.transform;
    Tile tile = currentTile.GetComponent<Tile>(); 
    Vector2 gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
    //Area[tile.x,tile.y] = 0;
    Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = 0;
    //Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = 0;

    Vector2 newPosition = currentTile.transform.position;
    newPosition += amountToMove;
    currentTile.transform.position = newPosition;

    gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
    Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = tile.id;
    //Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = tile.id;
}

void generateRandomTiles()
{
    if(currerntTileAmount> GridX * GridY)
    {
        throw new UnityException("Grids are full");
    }
    int id = Random.Range(1,5);
    Debug.Log("ID: "+id);
    int x = Random.Range(0,GridX);
    Debug.Log("X: "+x);
    int y = Random.Range(0,GridY);
    Debug.Log("Y: "+y);
    bool found = false;
    while(!found)
    {
        if(Area[x,y] == 0)
        {
            found = true;
            Area[x,y] = id;
            Vector2 worldposition = GridToWorldPoint(x,y);
            GameObject obj;
            obj = Instantiate(objs,worldposition,transform.rotation) as GameObject;
            obj.GetComponent<Tile>().x = x;
            obj.GetComponent<Tile>().y = y;
            obj.GetComponent<Tile>().id = id;
            currerntTileAmount++;

        }

        x++;
        if (x >= GridY) {
            y++;
            x = 0;
        }

        if (y >= GridX) {
            y = 0;
        }

    }

}

private bool MoveTilesLeft() {
    bool hasMoved = false;
    for (int x = 1; x < 6; x++) {
        for (int y = 5; y >= 0; y--) {
            if (Area[x, y] == 0) {
                continue;
            }

            GameObject currentTile = GetObjectAtGridPosition(x, y);
            bool stopped = false;

            while (!stopped) {
                // see if the position to the left is open
                RaycastHit2D hit = Physics2D.Raycast (currentTile.transform.position - horizontalRay, -Vector2.right,1f);
                if (hit && hit.collider.gameObject != currentTile) {
                    Tile otherTile = hit.collider.gameObject.GetComponent<Tile>();
                    if (otherTile != null) {
                        Tile thisTile = currentTile.GetComponent<Tile>();
                        //              if (thisTile.power == otherTile.power) {
                        //                UpgradeTile(currentTile, thisTile, hit.collider.gameObject, otherTile);
                        //                hasMoved = true;
                        //              }
                    }
                    stopped = true;
                } else {
                    Debug.Log("Moving");
                    UpdateGrid (currentTile, new Vector2(-spaceBetweenTiles, 0f));
                    hasMoved = true;
                }
            }
        }
    }
    return hasMoved;
}

void OnGUI()
{
    if(GUI.Button(new Rect(Screen.width * 0.8f,Screen.height * 0.1f,Screen.width * 0.1f,Screen.height * 0.1f),"Generate"))
    {
        generateRandomTiles();
    }
}


private GameObject GetObjectAtGridPosition(int x, int y) {
    RaycastHit2D hit = Physics2D.Raycast(GridToWorldPoint(x,y),Vector2.right);

    if (hit) {
        Debug.Log(hit.collider.name);
        Debug.Log(hit.collider.transform.position.x+" "+hit.collider.transform.position.y);
        return hit.collider.gameObject;
    } 
    else {
        //return hit.collider.gameObject;
        throw new UnityException("Unable to find gameObject in grid position (" + x + ", " + y + ")");
    }
}

}

Tiles collapsing

While playing, some tiles collapse. Thus the exception "grid is full" is raised, even though some cells are free.

Example : 16 tiles, some free cells, but exception

2048bug

Is it possible to add support for touch screen devices

Hello,

it is great clone, works perfect on desktop. But is it possible to add support for touch screen devices? I guess the GridManager script has to be modify to include Input.Touch prperties?

Best regards,
Stoil

Update to latest UI

Unity 4.6 included a UI update. The UI for this is pretty horrible as is, so maybe in updating we can make sure that buttons are in consistent locations and look alright.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.