jamiltron / 2048-unity Goto Github PK
View Code? Open in Web Editor NEWA 2048 clone written in an afternoon to see how easy it would be to do in Unity
A 2048 clone written in an afternoon to see how easy it would be to do in Unity
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.
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.
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 + ")");
}
}
}
Prefab 1 is missing, the basic tile should be 1 instead of 2.
Just as an example for how one would do it on a bigger game.
The button needs better positioning, styling, etc.
I just hate doing any UI work, so any help would be appreciated :P
This is a difference between this game and the other versions of 2048 - when the player hits a button in an invalid direction the game is not supposed to populate a new tile. This game does, although fixing that should be relatively easy.
I'm having trouble making this swipe on android, help me!
I have only filled 3/4 of the grid and the game has thrown an error.
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
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.
What does it mean?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.