SameGame engine made with Unity and C#
Talking about SameGame game, C#, Game development and Unity3D.
Learn cross platform HTML5 game development
Check my Gumroad page for commented source code, games and books.
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<Vector2> 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<tileScript> ().value = tileColor;
tile.GetComponent<tileScript> ().coordinate = new Vector2 (j, i);
tile.GetComponent<Renderer> ().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<Vector2> ();
floodFill (hitCollider.GetComponent<tileScript> ().coordinate, hitCollider.GetComponent<tileScript> ().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<tileScript> ().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<tileScript> ().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.