■
Spawner2.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawner2 : MonoBehaviour {
public GameObject groups;
// Use this for initialization
void Start () {
// Spawn initial Group
spawnNext();
}
// Update is called once per frame
void Update () {
}
public void spawnNext() {
// Random Index
// Spawn Group at current Position
Instantiate(groups[0],
transform.position,
Quaternion.identity);
}
}
Grid2.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gridx2 : MonoBehaviour {
// The Grid itself
public static int w = 6;
public static int para_w = 10;
public static int h = 14;
public static Transform[,] grid = new Transform[w+para_w, h];
public static int[,] hako= new int [w+para_w, h];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public static Vector2 roundVec2(Vector2 v) {
return new Vector2(Mathf.Round(v.x),
Mathf.Round(v.y));
}
public static bool insideBorder(Vector2 pos) {
return ((int)pos.x >= 0+para_w &&
(int)pos.x < w+para_w &&
(int)pos.y >= 0);
}
public static void deleteRow(int y) {
for (int x = 0+para_w; x < w+para_w; ++x) {
Destroy(grid[x, y].gameObject);
grid[x, y] = null;
hako [x, y] = 0;
}
}
public static void decreaseRow(int y) {
for (int x = 0+para_w; x < w+para_w; ++x) {
if (grid[x, y] != null) {
// Move one towards bottom
grid[x, y-1] = grid[x, y];
hako [x, y - 1] = hako [x, y];
grid[x, y] = null;
hako [x, y] = 0;
// Update Block position
grid[x, y-1].position += new Vector3(0, -1, 0);
}
}
}
public static void decreaseRowsAbove(int y) {
for (int i = y; i < h; ++i)
decreaseRow(i);
}
public static bool isRowFull(int y) {
for (int x = 0+para_w; x < w+para_w; ++x)
if (grid[x, y] == null)
return false;
return true;
}
public static void deleteFullRows() {
for (int y = 0; y < h; ++y) {
if (isRowFull(y)) {
deleteRow(y);
decreaseRowsAbove(y+1);
--y;
}
}
}
}
Group2.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Group2 : MonoBehaviour {
float lastFall = 0;
// Use this for initialization
public GameObject groups;
GameObject iti,ni,nex_iti,nex_ni,nex_iti2,nex_ni2;
int iti_tg,ni_tg;
float osi=0,sw_osi=0,omi=0,sw_omi=0,ohi=0,sw_ohi=0;
int otosu_ans,otosu_watasi=0;
float mi_ro=0.1f,hi_ro=0.1f,mi_yaji=9,hi_yaji=9;
void Start () {
sptex.randNext ();
iti= Instantiate(groups[sptex.j_one[0]],transform.position,Quaternion.identity)as GameObject;
iti.transform.parent = this.transform;
nex_iti=Instantiate(groups[sptex.j_one[1]],transform.position,Quaternion.identity)as GameObject;
nex_iti.transform.position = new Vector3(6.5f, 9, 0);
nex_iti2=Instantiate(groups[sptex.j_one[2]],transform.position,Quaternion.identity)as GameObject;
nex_iti2.transform.position = new Vector3(7f, 7.4f, 0);
nex_iti2.transform.localScale = new Vector3(0.7f, 0.7f, 0);
// Debug.Log (i);
switch(sptex.j_one[0]){
case 0:
iti_tg = 1;
break;
case 1:
iti_tg = 2;
break;
case 2:
iti_tg = 3;
break;
case 3:
iti_tg = 4;
break;
default:
Debug.Log ("unko");
break;
}
ni= Instantiate(groups[sptex.j_two[0]],transform.position,Quaternion.identity)as GameObject;
ni.transform.parent = this.transform;
ni.transform.position += new Vector3(0, 1, 0);
nex_ni=Instantiate(groups[sptex.j_two[1]],transform.position,Quaternion.identity)as GameObject;
nex_ni.transform.position = new Vector3(6.5f, 10, 0);
nex_ni2=Instantiate(groups[sptex.j_two[2]],transform.position,Quaternion.identity)as GameObject;
nex_ni2.transform.position = new Vector3(7f, 8, 0);
nex_ni2.transform.localScale = new Vector3(0.7f, 0.7f, 0);
// Debug.Log (i);
switch(sptex.j_two[0]){
case 0:
ni_tg = 1;
break;
case 1:
ni_tg = 2;
break;
case 2:
ni_tg = 3;
break;
case 3:
ni_tg = 4;
break;
default:
Debug.Log ("unko");
break;
}
if (!isValidGridPos()) {
Debug.Log("1");
Debug.Log("GAME OVER");
Debug.Log("2");
Destroy(gameObject);
}
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown (KeyCode.RightArrow)) {
mi_yaji = 0;
}
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
hi_yaji = 0;
}
// Move Left
if (Input.GetKeyDown (KeyCode.LeftArrow)||(sw_ohi==1)&&(ohi>hi_ro)) {
// Modify position
transform.position += new Vector3(-1, 0, 0);
// See if valid
if (isValidGridPos ()) {
// It's valid. Update grid.
updateGrid ();
} else {
// It's not valid. revert.
transform.position += new Vector3 (1, 0, 0);
}
ohi = 0;
if (hi_yaji == 0) {
hi_ro = 0.1f;
hi_yaji=1;
// Debug.Log("a");
} else {
hi_ro = 0.01f;
// Debug.Log("b");
}
}
// Move Right
if (Input.GetKeyDown (KeyCode.RightArrow)||(sw_omi==1)&&(omi>mi_ro)){
// Modify position
transform.position += new Vector3(1, 0, 0);
// See if valid
if (isValidGridPos ()) {
// It's valid. Update grid.
updateGrid ();
} else {
// It's not valid. revert.
transform.position += new Vector3 (-1, 0, 0);
}
omi = 0;
if (mi_yaji == 0) {
mi_ro = 0.1f;
mi_yaji=1;
} else {
mi_ro = 0.01f;
}
}
// Rotate
if (Input.GetKeyDown(KeyCode.X)) {
transform.Rotate(0, 0, -90);
// See if valid
if (isValidGridPos())
// It's valid. Update grid.
updateGrid();
else
// It's not valid. revert.
// transform.Rotate(0, 0, 90);
transform.position += new Vector3 (1, 0, 0);
if(isValidGridPos()){
}else{
transform.position += new Vector3 (-2, 0, 0);
if (isValidGridPos ()) {
} else {
transform.position += new Vector3 (1, 0, 0);
transform.Rotate(0, 0, -90);
if (isValidGridPos ()==false) {
transform.position += new Vector3 (0, 1, 0);
}
}
}
updateGrid();
}
if (Input.GetKeyDown(KeyCode.Z)) {
transform.Rotate(0, 0, 90);
// See if valid
if (isValidGridPos())
// It's valid. Update grid.
updateGrid();
else
// It's not valid. revert.
// transform.Rotate(0, 0, -90);
transform.position += new Vector3 (-1, 0, 0);
if(isValidGridPos()){
}else{
transform.position += new Vector3 (2, 0, 0);
if (isValidGridPos ()) {
} else {
transform.position += new Vector3 (-1, 0, 0);
transform.Rotate(0, 0, 90);
if (isValidGridPos ()==false) {
transform.position += new Vector3 (0, 1, 0);
}
}
}
updateGrid();
}
// Move Downwards and Fall
if (Input.GetKeyDown(KeyCode.DownArrow)||(sw_osi==1)&&(osi>0.05f)||Time.time - lastFall >= 1.5) {
// Modify position
transform.position += new Vector3(0, -1, 0);
// See if valid
if (isValidGridPos()) {
// It's valid. Update grid.
updateGrid();
} else {
// It's not valid. revert.
transform.position += new Vector3(0, 1, 0);
// Clear filled horizontal lines
//Gridx.deleteFullRows();
StartCoroutine(dmethod());
enabled = false;
}
osi = 0;
lastFall = Time.time;
}
if (Input.GetKey (KeyCode.DownArrow)) {
sw_osi = 1;
osi += Time.deltaTime;
}else{
sw_osi = 0;
osi = 0;
}
if (Input.GetKey (KeyCode.RightArrow)) {
sw_omi = 1;
omi += Time.deltaTime;
}else{
sw_omi = 0;
omi = 0;
mi_yaji = 0;
mi_ro = 1;
}
if (Input.GetKey (KeyCode.LeftArrow)){
sw_ohi = 1;
ohi += Time.deltaTime;
}else{
sw_ohi = 0;
ohi = 0;
hi_yaji = 0;
hi_ro = 1;
}
}
bool isValidGridPos() {
foreach (Transform child in transform) {
Vector2 v = Gridx2.roundVec2(child.position);
// Not inside Border?
if (!Gridx2.insideBorder(v))
return false;
// Block in grid cell (and not part of same group)?
if (Gridx2.grid[(int)v.x, (int)v.y] != null &&
Gridx2.grid[(int)v.x, (int)v.y].parent != transform)
return false;
}
return true;
}
void updateGrid() {
// Remove old children from grid
for (int y = 0; y < Gridx2.h; ++y) {
for (int x = 0+Gridx2.para_w; x < Gridx2.w+Gridx2.para_w; ++x) {
if (Gridx2.grid [x, y] != null) {
if (Gridx2.grid [x, y].parent == transform) {
Gridx2.grid [x, y] = null;
Gridx2.hako [x, y] = 0;
}
}
}
}
// Add new children to grid
foreach (Transform child in transform) {
Vector2 v = Gridx2.roundVec2(child.position);
Gridx2.grid[(int)v.x, (int)v.y] = child;
if (child.name == iti.name) {
switch (iti_tg) {
case 1:
Gridx2.hako [(int)v.x, (int)v.y] = 1;
break;
case 2:
Gridx2.hako [(int)v.x, (int)v.y] = 2;
break;
case 3:
Gridx2.hako [(int)v.x, (int)v.y] = 3;
break;
case 4:
Gridx2.hako [(int)v.x, (int)v.y] = 4;
break;
}
} else if (child.name == ni.name) {
switch (ni_tg) {
case 1:
Gridx2.hako [(int)v.x, (int)v.y] = 1;
break;
case 2:
Gridx2.hako [(int)v.x, (int)v.y] = 2;
break;
case 3:
Gridx2.hako [(int)v.x, (int)v.y] = 3;
break;
case 4:
Gridx2.hako [(int)v.x, (int)v.y] = 4;
break;
}
} else {
Debug.Log ("unko");
}
}
}
public static void renketu(int x,int y,ref int n){
int c=Gridx2.hako[x,y]; /// 自分の色
if (c == 0) {
return;
}
Gridx2.hako[x,y]=9;
n++;
if(x+1
if(y+1
if(x-1>=0 && Gridx2.hako[x-1,y]==c) renketu(x-1,y,ref n);
if(y-1>=0 && Gridx2.hako[x,y-1]==c) renketu(x,y-1,ref n);
Gridx2.hako[x,y]=c;
}
public static void pain(int x,int y){
int c=Gridx2.hako[x,y]; /// 自分の色
if (c == 0) {
return;
}
Gridx2.hako[x,y]=9;
if(x+1
if(y+1
if(x-1>=0 && Gridx2.hako[x-1,y]==c) pain(x-1,y);
if(y-1>=0 && Gridx2.hako[x,y-1]==c) pain(x,y-1);
Gridx2.hako[x,y]=0;
Destroy(Gridx2.grid[x, y].gameObject);
}
private IEnumerator dmethod(){
otosu_ans = 0;
yield return StartCoroutine("otosu");
/*if (otosu_ans == 1) {
yield return new WaitForSeconds (0.3f);
};*/
int oo,st = 0;
for (int i = 0; i <= 10; i++) {
// this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y-0.1f+0.02f*i, this.transform.position.z);
// this.transform.localScale = new Vector3 (1.0f, 0.8f+0.02f*i, 1.0f);
yield return new WaitForSeconds (0.01f);
}
yield return new WaitForSeconds (0.3f);
do{
otosu_watasi=0;
for (int y = 0; y < Gridx2.h; ++y) {
for (int x = 0; x < Gridx2.w+Gridx2.para_w; ++x) {
if (Gridx2.hako[x,y] != 0) {
oo = 0;
renketu (x, y, ref oo);
if (oo >= 4) {
if(otosu_watasi==1){
}else if(st==0){
yield return new WaitForSeconds (0.4f);
otosu_watasi=1;
}else{
yield return new WaitForSeconds (1.2f);
otosu_watasi=1;
}
pain (x, y);
}
}
}
}
st++;
yield return StartCoroutine("otosu");
if(st>10){
break;
}
}while(otosu_ans == 1);
Destroy (nex_iti);
Destroy (nex_ni);
Destroy (nex_iti2);
Destroy (nex_ni2);
// Spawn next Group
Debug.Log ("yo");
FindObjectOfType
().spawnNext(); Debug.Log ("yo2");
}
private IEnumerator otosu() {
int iy;
otosu_ans = 0;
for (int x = 0; x < Gridx2.w+Gridx2.para_w; ++x) {
for (int y = 0; y < Gridx2.h; ++y) {
if (Gridx2.hako [x, y] == 0){
for (iy = y; iy < Gridx2.h && Gridx2.hako[x, iy]==0; iy++);
if (iy == Gridx2.h) {
break;
}
otosu_ans = 1;
while (Gridx2.hako [x, y] == 0) {
for (iy = y; iy < Gridx2.h - 1; iy++) {
if (Gridx2.grid [x, iy + 1] != null) {
Gridx2.grid [x, iy + 1].position += new Vector3 (0, -1, 0);
if (otosu_watasi == 0) {
yield return new WaitForSeconds (0.05f);
} else {
yield return new WaitForSeconds (0.01f);
}
}
Gridx2.grid [x, iy] = Gridx2.grid [x, iy + 1];
Gridx2.hako [x, iy] = Gridx2.hako [x, iy + 1];
Gridx2.grid [x, iy + 1] = null;
Gridx2.hako [x, iy + 1] = 0;
}
}
}
}
}
}
}
sptex.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sptex : MonoBehaviour {
public static int j_kazu=4;
public static int j_one= new int [3];
public static int j_two= new int [3];
public static int sw_jun= new int [1];
public static int itare= new int [1];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
static public void randNext() {
UnityEngine.Random.InitState(itare[0]);
int i;
if (sw_jun[0] == 0) {
i = Random.Range (0, j_kazu);
j_one [0] = i;
i = Random.Range (0, j_kazu);
j_two [0] = i;
i = Random.Range (0, j_kazu);
j_one [1] = i;
i = Random.Range (0, j_kazu);
j_two [1] = i;
i = Random.Range (0, j_kazu);
j_one [2] = i;
i = Random.Range (0, j_kazu);
j_two [2] = i;
} else {
i = Random.Range (0, j_kazu);
j_one[0]=j_one[1];
j_one[1]=j_one[2];
j_one[2]=i;
i = Random.Range (0, j_kazu);
j_two[0]=j_two[1];
j_two[1]=j_two[2];
j_two[2]=i;
}
sw_jun [0] = 1;
itare[0]++;
}
}