Hello everybody!
I am looking for little help with my project.
I have little problem with moving tiles algorithm in my version of 2048 game.
It is very pre alpha code, I have a lot of work waiting for me in this project.
My game is storing every tile in GameObject table.
I could get every tile by position [x,y] translated to index.
Every tile have their own script with declared value.
How I could make this moving tile algorithm in proper way?
I found few source codes but I have problem with using it in my project.
I am sorry for polish variable names in project - I added few comments in English.
I think the idea of my code should be clear.
//here is my edited code of engineScript. - thanks Robert for comment!
using UnityEngine;
using System.Collections;
public class engineScript : MonoBehaviour {
public static int cols = 4; //maxX
public static int rows = 4; //maxY
public static int minValue = 2, maxValue = 4;
private int score = 0;
private int currentTilesAmount;
public Transform[,] tiles = new Transform[cols,rows];
public GameObject tilePrefab;
public float offset = 0.25f;
// Use this for initialization
void Start () {
newTile();
newTile();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.LeftArrow)){
tilesLeft();
newTile();
}
else
if (Input.GetKeyDown(KeyCode.RightArrow)){
tilesRight();
newTile();
}
else
if (Input.GetKeyDown(KeyCode.DownArrow)){
tilesDown();
newTile();
}
else
if (Input.GetKeyDown(KeyCode.UpArrow)){
tilesUp();
newTile();
}
}
void tilesLeft(){
}
void tilesRight(){
}
void tilesDown(){
}
void tilesUp(){
}
//generowanie losowej wartości startowej tiles - działa ok
//generate random value for new tile
int newTileValue(){
int value;
// find out if we are generating a tile with the lowest or highest value
float highOrLowChance = Random.Range(0f, 0.99f);
if (highOrLowChance <= 0.75f) {
value = minValue;
} else {
value = maxValue;
}
return value;
}
//policz ile jest wolnych pozycji - działa OK
//count of free positions in main table
int freePositionAmount(){
int freeSpace = 0, lockedSpace = 0;
foreach(Transform t in tiles){
if (t == null)
freeSpace++;
else
lockedSpace++;
}
currentTilesAmount = lockedSpace;
//number of free space
Debug.Log("freePositionAmount: ilość wolnych miejsc w tablicy: " + freeSpace); //free space amount in table
return freeSpace;
}
//losowanie wolnej pozycji w tablicy - działa OK
//looking for free random position in table
void randomFreePositon(out int posX, out int posY){
//temporary variable for freePositionAmount
int freeSpaceAmount = freePositionAmount();
//tablica przechowująca wolne pozycje [x,y]
//table with free position indexes [x,y]
int[,] freePositions = new int[freeSpaceAmount,2];
if(freeSpaceAmount > 0){
int iterator = 0;
for (int x = 0 ; x < cols ; x++)
for (int y = 0; y < rows; y++){
if (tiles[x,y] == null){
freePositions[iterator,0] = x; //free posX
freePositions[iterator,1] = y; //free posY
iterator++;
}
}
//wybór randomowej pozycji w tablicy z wolnymi miejscami
//select random free position in main table
int random = Random.Range(0, freePositions.GetLength(0)-1);
posX = freePositions[random,0];
posY = freePositions[random,1];
}
else
{
Debug.Log("randomFreePosition: brak wolnych miejsc w tablicy!"); // there are no free space in table
throw new UnityException("randomFreePosition: brak wolnych miejsc w tablicy!"); // there are no free space in table
}
}
//tworzenie nowego obiektu tiles i dodanie do głównej tablicy kostek - działa OK
//creating new tile and adding the object into main table
void newTile(){
//losowanie wolnej pozycji...
//looking for free position...
int x, y;
randomFreePositon(out x, out y);
//instantiate new tile at [x,y] position with offset
GameObject tmp_tile = (GameObject) Instantiate(tilePrefab,new Vector3(x+(x*offset)-2,y + (y*offset)-2,0),Quaternion.identity);
//add new tile to table
if(tmp_tile){
//przypisz losową wartość początkową 2 lub 4
//random value for new tile
tmp_tile.GetComponent<cubeScript>().value = newTileValue();
//dodanie tiles do tablicy
//add tile to table
tiles[x,y] = tmp_tile.transform;
Debug.Log("newTile: kostka została dodana do głównej tablicy!"); // new tile was succesfully added into main table
}
}
}