public LifeStoneInfo CreateLifeStoneInfo(LifeStoneInfo lifeStoneInfo) { Vector2Int size = lifeStoneInfo.getSize(); Vector2Int newSize; string fill = lifeStoneInfo.getFill(); string newFill = ""; Vector2Int maxPoint = new Vector2Int(-1, -1); Vector2Int minPoint = new Vector2Int(size.x + 1, size.y + 1); for (int j = 0; j < size.y; j++) { for (int i = 0; i < size.x; i++) { if (fill[j * size.x + i] != ' ') { maxPoint.x = Mathf.Max(i, maxPoint.x); maxPoint.y = Mathf.Max(j, maxPoint.y); minPoint.x = Mathf.Min(i, minPoint.x); minPoint.y = Mathf.Min(j, minPoint.y); } } } newSize = maxPoint - minPoint + Vector2Int.one; for (int j = minPoint.y; j <= maxPoint.y; j++) { newFill += fill.Substring(j * size.x + minPoint.x, newSize.x); } return(new LifeStoneInfo(newSize, newFill)); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.D)) { LifeStoneInfo temp = CreateLifeStoneShape(3, 0); /*for(int i = temp.lifeStonePos.Length / temp.size.x - 1; i >= 0; i--) * { * string oneLine = ""; * for(int j = 0; j < temp.size.x; j++) * { * oneLine += temp.lifeStonePos[i * (int)temp.size.x + j] + ""; * } * Debug.Log(oneLine); * }*/ GetLifeStone(temp); /*CreateLifeStone(new Vector2Int(2, 0), LifeStoneType.Normal); * CreateLifeStone(new Vector2Int(1, 1), LifeStoneType.Normal); * CreateLifeStone(new Vector2Int(2, 1), LifeStoneType.Normal); * * * * GetLifeStone(new LifeStoneInfo("1011", new Vector2Int(2, 2)));*/ } }
public DroppedItem CreateItem(Vector2 pos, int lifeStoneAmount, int goldRate) { float droppedLifeStoneOffset = 0.33f; DroppedItem temp = Instantiate(droppedItem, pos, Quaternion.identity); LifeStoneInfo lifeStoneInfo = LifeStoneManager.Instance.CreateLifeStoneShape(lifeStoneAmount, goldRate); temp.lifeStoneInfo = lifeStoneInfo; for (int y = 0; y < lifeStoneInfo.size.y; y++) { for (int x = 0; x < lifeStoneInfo.size.x; x++) { if ((LifeStoneType)int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString()) != LifeStoneType.NULL) { Instantiate(droppedLifeStone, new Vector2(x, y) * droppedLifeStoneOffset, Quaternion.identity, temp.transform); } } } temp.GetComponent <BoxCollider2D>().size = (Vector2)lifeStoneInfo.size * droppedLifeStoneOffset; temp.GetComponent <BoxCollider2D>().offset = (lifeStoneInfo.size - new Vector2(1, 1)) / 2 * droppedLifeStoneOffset; temp.transform.Find("GroundCollider").GetComponent <BoxCollider2D>().size = (Vector2)lifeStoneInfo.size * droppedLifeStoneOffset; temp.transform.Find("GroundCollider").GetComponent <BoxCollider2D>().offset = (lifeStoneInfo.size - new Vector2(1, 1)) / 2 * droppedLifeStoneOffset; temp.isWeapon = false; return(temp); }
public void Init(LifeStoneInfo _info, Vector3 pos) { info = _info; rb2D = GetComponent <Rigidbody2D>(); bc2D = GetComponent <BoxCollider2D>(); unitSize = unitSprite.GetComponent <SpriteRenderer>().bounds.size.x; Vector2Int inSize = info.getSize(); string inFill = info.getFill(); unitObj = new GameObject[inSize.x * inSize.y]; highObj = new GameObject[inSize.x * inSize.y]; transform.position = pos - new Vector3(inSize.x * unitSize / 2f, 0, 0); for (int i = 0; i < inSize.x * inSize.y; i++) { if (inFill[i] != ' ') { unitObj[i] = Instantiate(unitSprite, transform); unitObj[i].transform.localPosition = new Vector3((i % inSize.x) * unitSize, ((int)(i / inSize.x)) * unitSize, 0); unitObj[i].GetComponent <SpriteRenderer>().sprite = sprites[inFill[i] - 'A']; highObj[i] = Instantiate(highlightSprite, transform); highObj[i].transform.localPosition = unitObj[i].transform.localPosition; highObj[i].SetActive(false); } } bc2D.offset = new Vector2(unitSize * inSize.x / 2f, unitSize * inSize.y / 2f); bc2D.size = new Vector2(unitSize * inSize.x, unitSize * inSize.y); }
public GameObject InstantiateDroppedLifeStone(LifeStoneInfo info, Vector3 pos, float popoutStrength) { GameObject tmpObj = Instantiate(droppedLifeStonePrefab); tmpObj.transform.SetParent(MapManager.currentRoom.roomInGame.transform); tmpObj.GetComponent <DroppedLifeStone>().Init(info, pos); PopoutGenerator(tmpObj, popoutStrength); return(tmpObj); }
/// <summary> /// push LifeStone in LifeStoneFrame /// </summary> /// <param name="pushInfo"></param> public bool PushLifeStone(LifeStoneInfo pushInfo) { System.Random rnd = new System.Random(); Vector2Int pSize = pushInfo.getSize(); string pFill = pushInfo.getFill(); int[] minRow = new int[] { lifeStoneRowNum, lifeStoneRowNum, lifeStoneRowNum }; int selectedCol = 0, selectedRow = lifeStoneRowNum; ArrayList selColCand = new ArrayList(); { int i, j, pi, pj; for (i = 0; i <= 3 - pSize.x; i++) { for (j = lifeStoneRowNum - 1; j >= 0; j--) { for (pi = 0; pi < pSize.x; pi++) { for (pj = 0; pj < pSize.y; pj++) { if (pFill[pj * pSize.x + pi] != ' ' && lifeStoneArray[j + pj, i + pi] != 0) { break; } } if (pj != pSize.y) { break; } } if (pi != pSize.x) { break; } minRow[i] = j; } } } for (int i = 0; i <= 3 - pSize.x; i++) { if (minRow[i] < selectedRow) { selectedRow = minRow[i]; } } if (selectedRow == lifeStoneRowNum) { GameManager.Instance.DisplayText("생명석 자리가 없습니다!"); return(false); } for (int i = 0; i <= 3 - pSize.x; i++) { if (minRow[i] == selectedRow) { selColCand.Add(i); } } selectedCol = (int)selColCand[rnd.Next(selColCand.Count)]; float vibration = pushInfo.getAmount() * vibrationVariable * lifeStoneSize; int cutRow = pSize.y; for (int pj = 0; pj < pSize.y; pj++) { if (cutRow == pSize.y && selectedRow + pj >= lifeStoneRowNum) { cutRow = pj; //break; } for (int pi = 0; pi < pSize.x; pi++) { if (pFill[pj * pSize.x + pi] != ' ') { int xtmp = selectedCol + pi, ytmp = selectedRow + pj; GameObject tmpObj; tmpObj = Instantiate(lifeUnitPrefab, stoneSuper.transform); if (pj < cutRow) { lifeStoneArray[ytmp, xtmp] = pFill[pj * pSize.x + pi] - 'A' + 1; lifeStoneUnit[ytmp, xtmp] = tmpObj; } tmpObj.GetComponent <LifeUnitInFrame>().Init( pFill[pj * pSize.x + pi] - 'A' + 1, lifeStoneSize, new Vector2Int(xtmp, ytmp), new Vector2Int(xtmp, lifeStoneRowNum + pj), new Vector2(frameBorder * lifeStoneSize, frameBorder * lifeStoneSize), true, vibration); vibration = 0; } } } stoneCut = false; if (cutRow < pSize.y) { StartCoroutine(CutCoroutine(cutRow, pSize, pFill)); } return(true); }
public void GetLifeStone(LifeStoneInfo lifeStoneInfo) { //Check possible && lowest position of new life stone int minY = rowSize; List <Vector2Int> minPosCands = new List <Vector2Int>(); for (int i = 0; i < 4 - lifeStoneInfo.size.x; i++) { //Make initial matrix without new life stone LifeStoneType[,] lifeStoneDownTest = new LifeStoneType[rowSize + lifeStoneInfo.size.y, columnSize]; for (int y = 0; y < rowSize; y++) { for (int x = 0; x < columnSize; x++) { lifeStoneDownTest[y, x] = lifeStoneGrid[y, x] != null ? lifeStoneGrid[y, x].type : LifeStoneType.NULL; } } //Push new life stone to the top of the initial matrix for (int y = 0; y < lifeStoneInfo.size.y; y++) { for (int x = 0; x < lifeStoneInfo.size.x; x++) { lifeStoneDownTest[y + rowSize, x + i] = (LifeStoneType)int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString()); } } //Find the lowest position of the new life stone with current x offset bool moveDown = true; for (int j = rowSize - 1; moveDown && j >= 0; j--) { for (int y = 0; moveDown && y < lifeStoneInfo.size.y; y++) { for (int x = 0; moveDown && x < lifeStoneInfo.size.x; x++) { if ((LifeStoneType)int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString()) != LifeStoneType.NULL) { //Debug.Log("j : " + j + " i : " + i + " x : " + (x + i) + " y : " + (y + j) + " " + lifeStoneDownTest[y + j, x + i]); if (lifeStoneDownTest[y + j, x + i] != LifeStoneType.NULL) { //Debug.Log("j : " + j + " i : " + i + " x : " + (x + i) + " y : " + (y + j) + " False"); //Mark lowest position moveDown = false; if (minY > j + 1) { minY = j + 1; minPosCands.Clear(); //Debug.Log(new Vector2Int(i, j + 1) + "accept"); minPosCands.Add(new Vector2Int(i, j + 1)); } else if (minY == j + 1) { //Debug.Log(new Vector2Int(i, j + 1) + "accept"); minPosCands.Add(new Vector2Int(i, j + 1)); } break; } lifeStoneDownTest[y + j, x + i] = lifeStoneDownTest[y + j + 1, x + i]; lifeStoneDownTest[y + j + 1, x + i] = LifeStoneType.NULL; } } } } //If the lowest pos is floor, mark floor if (moveDown) { if (minY > 0) { minY = 0; minPosCands.Clear(); } //Debug.Log(new Vector2Int(i, 0) + "accept floor"); minPosCands.Add(new Vector2Int(i, 0)); } } //If current height is not enough to take the whole new life stone if (minY + lifeStoneInfo.size.y - 1 >= rowSize) { Debug.Log("Height exceeded"); int cutSize = rowSize - minY; string remainingLifeStoneInfo = ""; for (int y = cutSize; y < lifeStoneInfo.size.y; y++) { for (int x = 0; x < lifeStoneInfo.size.x; x++) { remainingLifeStoneInfo += int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString()); } } //Need to work later //Make cut off life stone to be eaten Debug.Log(remainingLifeStoneInfo); lifeStoneInfo.size.y = cutSize; } //Randomize the lowest pos of the new life stone int randomizer = Random.Range(0, minPosCands.Count); for (int y = 0; y < lifeStoneInfo.size.y; y++) { for (int x = 0; x < lifeStoneInfo.size.x; x++) { if ((LifeStoneType)int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString()) != LifeStoneType.NULL) { Vector2Int newPos = new Vector2Int(x + minPosCands[randomizer].x, y + minPosCands[randomizer].y); CreateLifeStone(y + rowSize, newPos, (LifeStoneType)int.Parse(lifeStoneInfo.lifeStonePos[y * lifeStoneInfo.size.x + x].ToString())); PlayerController.Instance.hp++; } } } }