Skip to content

Instantly share code, notes, and snippets.

@LMichelle
Last active March 19, 2018 14:16
Show Gist options
  • Save LMichelle/294656c62ff384f84a4fd0d0b073a364 to your computer and use it in GitHub Desktop.
Save LMichelle/294656c62ff384f84a4fd0d0b073a364 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BustAMove {
public class Grid : MonoBehaviour {
public List<GameObject> allBubblesList = new List<GameObject>();
public List<GameObject> nodesList = new List<GameObject>();
public Vector2 startPos;
public GameObject nodePrefab;
public GameObject bubbleToList;
public bool pushGridOnce = true;
public bool gotBubbleCount = false;
public int gridHeight;
public int gridWidth;
public int maxCountForPushGrid;
private int gridHeightCalculated;
private int gridWidthCalculated;
private bool once = true;
public int points;
// Arrow
private ArrowControl arrowScript;
// ------------------------------------------------------------------------------------------ UNITY FUNCTIONS
void Start() {
GridStartPos();
CreateGrid();
foreach (GameObject node in GameObject.FindGameObjectsWithTag("Node")) {
nodesList.Add(node);
}
pushGridOnce = true;
maxCountForPushGrid = GameObject.FindGameObjectWithTag("Player").GetComponent<ArrowControl>().maxCount;
}
private void Update() {
if (GameObject.FindGameObjectWithTag("Player").GetComponent<ArrowControl>().counter == maxCountForPushGrid && pushGridOnce) {
PushGrid();
pushGridOnce = false;
}
if (GameObject.FindGameObjectWithTag("Player").GetComponent<ArrowControl>().counter < maxCountForPushGrid) {
pushGridOnce = true;
}
foreach (GameObject node in GameObject.FindGameObjectsWithTag("Node")) {
List<GameObject> bubbleListPerNode = node.GetComponent<NodeChecker>().bubblesAroundList;
foreach (GameObject bubble in bubbleListPerNode) {
if (!allBubblesList.Contains(bubble)) {
allBubblesList.Add(bubble);
}
}
}
for (int i = allBubblesList.Count - 1; i > -1; i--) {
if (allBubblesList[i] == null) {
allBubblesList.RemoveAt(i);
}
}
if (allBubblesList.Count > 0 && once) {
gotBubbleCount = true;
once = false;
}
}
// ------------------------------------------------------------------------------------------ FUNCTIONS
// Set Starting position to count from where the Grid objects transform is.
//Then calculate what the actual position is to where the grid goes.
void GridStartPos() {
startPos = this.transform.position;
gridWidthCalculated = (int)startPos.x + gridWidth;
gridHeightCalculated = (int)startPos.y + gridHeight;
}
// Creates a grid filled with nodes.
void CreateGrid() {
for (int x = (int)startPos.x; x < gridWidthCalculated; x++) {
for (int y = (int)startPos.y; y < gridHeightCalculated; y++) {
// Every second row has one bubble less due to the offset.
if (y % 2 == 0 && x == gridWidthCalculated - 1) {
} else {
NewPlace(x, y);
}
}
}
}
// create a new node at the right position
void NewPlace(int x, int y) {
GameObject node = Instantiate(nodePrefab) as GameObject;
Vector2 gridPos = new Vector2(x, y);
gridPos = SetPos(gridPos);
gridPos.x += 0.5f;
gridPos.y += 0.5f;
node.transform.position = gridPos;
node.transform.parent = this.transform;
node.name = "Place " + node.transform.localPosition.x + " " + node.transform.localPosition.y;
}
// If the row is even, then make sure there is an offset.
Vector2 SetPos(Vector2 pos) {
if ((int)pos.y % 2 == 0) {
pos.x += 0.5f;
}
return pos;
}
void PushGrid() {
// Push grid down after shooting ... bubbles
foreach (GameObject node in nodesList) {
node.transform.position = new Vector2(node.transform.position.x, node.transform.position.y - 1);
}
// also change all bubble colors
foreach (GameObject bub in allBubblesList) {
bub.GetComponent<BubbleColor>().DiscoBal();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment