Talking about SameGame game, C#, Game development and Unity3D.
Probably you did not notice it, but this month I am really into SameGame engines and after the HTML Phaser version now it comes the Unity version, made with C# trying to keep the code structure as similar as possible to the Phaser version I showed you in the post HTML5 SameGame engine powered by Phaser. This is the working prototype, you should know how to play it: Pick a tile with at least another adjacent tile of the same color to remove them. The code is uncommented but next week you will see a step by step coverage about making this engine with Phaser Vs making this engine with Unity. This is the class used for the tile, had to do it to create custom properties:
using UnityEngine;
using System.Collections;
public class tileScript : MonoBehaviour {
public int value;
public Vector2 coordinate;
}
And this is the main game:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Script : MonoBehaviour {
public GameObject tileObject;
private Color[] colors = new Color[4];
private GameObject[,] tilesArray = new GameObject[8, 8];
private int numRows = 8;
private int numCols = 8;
private List filled;
void Start () {
colors [0] = new Color(1f, 0f, 0f, 1f);
colors [1] = new Color(0f, 1f, 0f, 1f);
colors [2] = new Color(0f, 0f, 1f, 1f);
colors [3] = new Color(1f, 1f, 0f, 1f);
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++){
GameObject tile = Instantiate(tileObject);
float yPos = 3.5f - 1f * i;
float xPos = -3.5f + 1f * j;
tile.transform.position = new Vector2 (xPos, yPos);
int tileColor = Random.Range (0, colors.Length);
tile.GetComponent ().value = tileColor;
tile.GetComponent ().coordinate = new Vector2 (j, i);
tile.GetComponent ().material.color = colors [tileColor];
tilesArray [i,j] = tile;
}
}
}
void Update () {
if (Input.GetButtonDown (“Fire1”)) {
Vector2 firePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D hitCollider = Physics2D.OverlapPoint(firePosition);
if (hitCollider) {
filled = new List ();
floodFill (hitCollider.GetComponent ().coordinate, hitCollider.GetComponent ().value);
if (filled.Count > 1) {
for (int i = 0; i < filled.Count; i++) {
Destroy (tilesArray [(int)filled [i].y, (int)filled [i].x]);
tilesArray [(int)filled [i].y, (int)filled [i].x] = null;
}
fillVerticalHoles ();
fillHorizontalHoles ();
}
}
}
}
void fillVerticalHoles(){
for (int i = numRows - 2; i >= 0; i–) {
for (int j = 0; j < numCols; j++) {
if (tilesArray [i, j] != null) {
int holesBelow = 0;
for (int z = i + 1; z < numRows; z++) {
if (tilesArray [z, j] == null) {
holesBelow++;
}
}
if (holesBelow>0) {
moveTile (i, j, i + holesBelow, j);
}
}
}
}
}
void fillHorizontalHoles(){
for (int i = 0; i < numCols - 1; i++) {
if (tilesInColumn (i) == 0) {
for (int j = i + 1; j < numCols; j++) {
if (tilesInColumn (j) != 0) {
for (int z = 0; z < numRows; z++) {
if (tilesArray [z, j] != null) {
moveTile (z, j, z, i);
}
}
break;
}
}
}
}
}
void moveTile(int fromRow, int fromCol, int toRow, int toCol){
tilesArray [toRow, toCol] = tilesArray [fromRow, fromCol];
tilesArray [toRow, toCol].GetComponent ().coordinate = new Vector2 (toCol, toRow);
tilesArray [toRow, toCol].transform.position = new Vector2(-3.5f + 1f * toCol, 3.5f – 1f * toRow);
tilesArray [fromRow, fromCol] = null;
}
int tilesInColumn(int col){
int result = 0;
for (int i = 0; i < numRows; i++) {
if (tilesArray [i, col] != null) {
result++;
}
}
return result;
}
void floodFill(Vector2 p, int n){
if (p.x < 0 || p.y < 0 || p.x > numCols – 1 || p.y > numRows – 1) {
return;
}
if (tilesArray [(int)p.y, (int)p.x] != null && tilesArray [(int)p.y, (int)p.x].GetComponent ().value == n && !filled.Contains(p)) {
filled.Add (p);
floodFill (new Vector2 (p.x + 1, p.y),n);
floodFill (new Vector2 (p.x – 1, p.y),n);
floodFill (new Vector2 (p.x, p.y + 1),n);
floodFill (new Vector2 (p.x, p.y – 1),n);
}
}
}
Have fun with it, you can also download the entire project. Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.