Initial Commit

This commit is contained in:
Tyrel Souza 2015-08-27 01:09:43 -04:00
commit 5762e435dd
45 changed files with 479 additions and 0 deletions

25
.gitignore vendored Normal file
View File

@ -0,0 +1,25 @@
# Created by https://www.gitignore.io/api/unity
### Unity ###
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
# Autogenerated VS/MD solution and project files
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
# Unity3D generated meta files
*.pidb.meta
# Unity3D Generated File On Crash Reports
sysinfo.txt

BIN
Assets/Game.unity Normal file

Binary file not shown.

8
Assets/Game.unity.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9e67a3a591b256644a1daa40ebf32a2b
timeCreated: 1440645242
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

9
Assets/Resources.meta Normal file
View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2e575c5b12b669b40b861bc5ff56ad2c
folderAsset: yes
timeCreated: 1440639305
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 37cbe527af0aed6438ce40c2beae8260
timeCreated: 1440641514
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 12900458aad356b48ab4ffd644ec05f1
folderAsset: yes
timeCreated: 1440644678
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b66f9e7d5a4f43c4689ae1f1c814c68f
timeCreated: 1440644770
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 349f863a023bc5a438ae6df82cbc6ae9
timeCreated: 1440644693
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b8b897b2d7978e74ab8aefdd126fdd95
timeCreated: 1440644701
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8a5ef4b1cfd15bf46a56074b8608133f
timeCreated: 1440644755
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e3c8801d956f2034b8466dd0d921ff3e
timeCreated: 1440644761
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a54a669325f5641488ffbff0e4c9d872
timeCreated: 1440644688
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 64e4a72fb823c2a439a046c6af121783
timeCreated: 1440644724
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 17093aed8ebf63d448e23e6cc2dd1612
folderAsset: yes
timeCreated: 1440639314
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a0f118e7f2851f9419cb0bcd452b4424
timeCreated: 1440639401
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 3db396677d4ecb84cb133b123fe24518
folderAsset: yes
timeCreated: 1440639318
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,202 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Board : MonoBehaviour {
public List<Gem> gems = new List<Gem>();
public int GridWidth;
public int GridHeight;
public GameObject gemPrefab;
public Gem lastGem;
// Animation
public Vector3 gem1Start, gem1End, gem2Start, gem2End;
public bool isSwapping = false;
public Gem gem1, gem2;
public float startTime;
public float swapRate = 0.5f;
public int AmountToMatch = 3;
public bool isMatched = false;
int topLine = 6;
// Use this for initialization
void Start () {
for (int h = 0; h < GridHeight; h++) {
for (int w = 0; w < GridWidth; w++){
GameObject g = Instantiate(gemPrefab, new Vector3(h,w,0), Quaternion.identity) as GameObject;
g.transform.parent = gameObject.transform;
gems.Add(g.GetComponent<Gem>());
}
}
}
// Update is called once per frame
void Update () {
if (isMatched) {
for (int i =0; i<gems.Count; i++) {
if (gems [i].isMatched) {
// Move it back up top, and change the color.
gems [i].CreateGem ();
gems [i].transform.position = new Vector3 (gems [i].transform.position.x,
gems [i].transform.position.y + topLine,
gems [i].transform.position.z);
}
}
isMatched = false;
} else if (isSwapping) {
MoveGem (gem1, gem1End, gem1Start);
MoveNegGem (gem2, gem2End, gem2Start);
if (Vector3.Distance (gem1.transform.position, gem1End) < 0.1f ||
Vector3.Distance (gem2.transform.position, gem2End) < 0.1f) {
gem1.transform.position = gem1End;
gem2.transform.position = gem2End;
lastGem = null;
gem1.ToggleSelector ();
gem2.ToggleSelector ();
isSwapping = false;
TogglePhysics (false);
CheckMatch ();
}
} else if (!BoardInMotion()) {
for(int i=0;i<gems.Count;i++){
CheckForNearbyMatches(gems[i]);
}
}
}
public bool BoardInMotion(){
for (int i=0; i<gems.Count; i++) {
if(gems[i].transform.localPosition.y > topLine) {
return true;
}
if (gems[i].GetComponent<Rigidbody>().velocity.y > 0.1f){
return true;
}
}
return false;
}
public void CheckForNearbyMatches (Gem g){
List<Gem> gemList = new List<Gem> ();
ConstructMatchList (g.color, g, g.XCoord, g.YCoord, ref gemList);
FixMatchList (g, gemList);
}
public void ConstructMatchList(string color, Gem gem, int XCoord, int YCoord, ref List<Gem> MatchList){
if (gem == null || gem.color != color || MatchList.Contains(gem)){
return;
}
MatchList.Add (gem);
if (XCoord == gem.XCoord || YCoord == gem.YCoord){
foreach(Gem g in gem.Neighbors){
ConstructMatchList(color, g, XCoord, YCoord, ref MatchList);
}
}
}
public void CheckMatch(){
List<Gem> gem1List = new List<Gem> ();
List<Gem> gem2List = new List<Gem> ();
ConstructMatchList (gem1.color, gem1, gem1.XCoord, gem1.YCoord, ref gem1List);
FixMatchList (gem1, gem1List);
ConstructMatchList (gem2.color, gem2, gem2.XCoord, gem2.YCoord, ref gem2List);
FixMatchList (gem2, gem2List);
}
public void FixMatchList(Gem gem, List<Gem> ListToFix){
List<Gem> rows = new List<Gem> ();
List<Gem> columns = new List<Gem> ();
for (int i=0; i <ListToFix.Count; i++) {
if (gem.XCoord == ListToFix[i].XCoord){
rows.Add(ListToFix[i]);
}
if (gem.YCoord == ListToFix[i].YCoord){
columns.Add(ListToFix[i]);
}
}
if (rows.Count >= AmountToMatch) {
isMatched = true;
for (int i=0;i<rows.Count;i++){
rows[i].isMatched=true;
}
}
if (columns.Count >= AmountToMatch) {
isMatched = true;
for (int i=0;i<columns.Count;i++){
columns[i].isMatched=true;
}
}
}
public void MoveGem(Gem gemToMove, Vector3 toPos, Vector3 fromPos){
Vector3 center = (fromPos + toPos) * 0.5f;
center -= new Vector3 (0, 0, 0.1f);
Vector3 riseRelCenter = fromPos - center;
Vector3 setRelCenter = toPos - center;
float fracComplete = (Time.time - startTime) / swapRate;
gemToMove.transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
gemToMove.transform.position += center;
}
public void MoveNegGem(Gem gemToMove, Vector3 toPos, Vector3 fromPos){
Vector3 center = (fromPos + toPos) * 0.5f;
center -= new Vector3 (0, 0, -0.1f);
Vector3 riseRelCenter = fromPos - center;
Vector3 setRelCenter = toPos - center;
float fracComplete = (Time.time - startTime) / swapRate;
gemToMove.transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
gemToMove.transform.position += center;
}
public void SwapGems(Gem currentGem){
if (lastGem == null) {
lastGem = currentGem;
} else if (lastGem == currentGem) {
lastGem = null;
} else {
if(lastGem.IsNeighborWith(currentGem)){
gem1Start = lastGem.transform.position;
gem1End = currentGem.transform.position;
gem2Start = currentGem.transform.position;
gem2End = lastGem.transform.position;
startTime = Time.time;
TogglePhysics(true);
gem1 = lastGem;
gem2 = currentGem;
isSwapping = true;
} else {
lastGem.ToggleSelector();
lastGem = currentGem;
}
}
}
public void TogglePhysics(bool isOn){
for(int i = 0; i<gems.Count; i++){
gems[i].GetComponent<Rigidbody>().isKinematic = isOn;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 673a5430d64e7ce41aa8817b3db6ab98
timeCreated: 1440639417
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,20 @@
using UnityEngine;
using System.Collections;
public class Feeler : MonoBehaviour {
public Gem owner;
void OnTriggerEnter(Collider c){
if (c.tag == "Gem") {
owner.AddNeighbor (c.GetComponent<Gem> ());
}
}
void OnTriggerExit(Collider c){
if (c.tag == "Gem") {
owner.RemoveNeighbor (c.GetComponent<Gem> ());
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: fbdd79d083fff1249b04e9ca1b097ea5
timeCreated: 1440640472
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,78 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gem : MonoBehaviour {
public List<Gem> Neighbors = new List<Gem>();
string[] gemMats = {"Red", "Green", "Blue", "Orange", "Black", "Purple", "Yellow"};
public string color = "";
public GameObject gemSphere;
public GameObject selector;
public bool isSelected;
public bool isMatched;
public int XCoord {
get {
return Mathf.RoundToInt(transform.localPosition.x);
}
}
public int YCoord {
get {
return Mathf.RoundToInt(transform.localPosition.y);
}
}
// Use this for initialization
void Start () {
CreateGem ();
}
// Update is called once per frame
void Update () {
}
void OnMouseDown(){
if (!GameObject.Find ("Board").GetComponent<Board> ().isSwapping) {
ToggleSelector ();
GameObject.Find ("Board").GetComponent<Board> ().SwapGems (this);
}
}
public void ToggleSelector(){
isSelected = !isSelected;
selector.SetActive (isSelected);
}
public bool IsNeighborWith(Gem g){
if (Neighbors.Contains (g)) {
return true;
}
return false;
}
public void AddNeighbor(Gem g){
if(!Neighbors.Contains (g)){
Neighbors.Add (g);
}
}
public void RemoveNeighbor(Gem g){
Neighbors.Remove (g);
}
public void CreateGem(){
color = gemMats [Random.Range (0, gemMats.Length)];
Material m = Resources.Load ("Materials/" + color) as Material;
gemSphere.GetComponent<Renderer> ().material = m;
isMatched = false;
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 7ac8858139702bc48b6175229805ab0c
timeCreated: 1440645702
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,2 @@
m_EditorVersion: 5.1.2f1
m_StandardAssetsVersion: 0

Binary file not shown.

Binary file not shown.

Binary file not shown.