using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class Operation { public int idx, val, op; // 0: ChangeValue // 1: OpenDoor // 2: CloseDoor // 3: Delay // 4: AddValue } public class Initalize : MonoBehaviour { public GameObject[] Boxes; public DoorAnimation[] Door; public NewTextModify[] Label; public GameObject AllBoxes; public TextMeshProUGUI CodeInput; public int IndexChange = -1, ActionChange = -1, TimeStop = 0, Accumulator = 0; public bool ActionExisted = false, ActionMoving = false; Queue AllOperation = new Queue(); private bool GonnaCompile = true; private int FakeAccumulator = 0; private int[] FakeBoxes = new int[100]; // Start is called before the first frame update void Start() { for(int i = 0; i < 100; ++i) { Boxes[i] = AllBoxes.gameObject.transform.GetChild(i).gameObject; Label[i] = Boxes[i].GetComponent(); Door[i] = Boxes[i].gameObject.transform.GetChild(0).GetChild(0).GetComponent(); Label[i].Init(i); } UpdateValue(0, 10); UpdateValue(1, 2); } public Operation Cope(int idx, int val, int op) { var Op = new Operation(); Op.idx = idx; Op.val = val; Op.op = op; return Op; } // Put value from Box into Accumulator void Op5(int idx) { AllOperation.Enqueue(Cope(idx, 0, 1)); AllOperation.Enqueue(Cope(100, Label[idx].Val, 0)); AllOperation.Enqueue(Cope(0, 100, 3)); AllOperation.Enqueue(Cope(idx, 0, 2)); FakeBoxes[idx] = FakeAccumulator; } // Put value from Accumulator into Box void Op3(int idx) { // Code to animate number to fly up from Accumulator AllOperation.Enqueue(Cope(idx, 0, 1)); AllOperation.Enqueue(Cope(idx, Accumulator, 0)); AllOperation.Enqueue(Cope(0, 100, 3)); AllOperation.Enqueue(Cope(idx, 0, 2)); FakeAccumulator = FakeBoxes[idx]; } // Get value from Box and add into Accumulator void Op1(int idx) { AllOperation.Enqueue(Cope(idx, 0, 1)); AllOperation.Enqueue(Cope(100, FakeBoxes[idx], 4)); AllOperation.Enqueue(Cope(0, 100, 3)); AllOperation.Enqueue(Cope(idx, 0, 2)); FakeAccumulator += FakeBoxes[idx]; } // Get value from Box and subtract from Accumulator void Op2(int idx) { AllOperation.Enqueue(Cope(idx, 0, 1)); AllOperation.Enqueue(Cope(100, -FakeBoxes[idx], 4)); AllOperation.Enqueue(Cope(0, 100, 3)); AllOperation.Enqueue(Cope(idx, 0, 2)); FakeAccumulator -= FakeBoxes[idx]; } // Just update value of a box from nowhere void UpdateValue(int idx, int value) { AllOperation.Enqueue(Cope(idx, 0, 1)); AllOperation.Enqueue(Cope(idx, value, 0)); AllOperation.Enqueue(Cope(0, 100, 3)); AllOperation.Enqueue(Cope(idx, 0, 2)); FakeBoxes[idx] = value; } void CompileError() { } void Compile() { if(!GonnaCompile) return; string InputText = CodeInput.text; if(InputText.Length <= -1 || InputText[0] != '#') return; int[] EncodeOperation = new int[100]; int InputLength = InputText.Length, Counter = 0; for(int i = 2; i < InputLength - 2; i += 4) { EncodeOperation[Counter] = Convert.ToInt32(InputText.Substring(i, 3)); Counter += 1; } GonnaCompile = false; int ptr = 0; while(true) { int OpType = EncodeOperation[ptr] / 100; int OpValue = EncodeOperation[ptr] % 100; if(OpType == 0) { break; } else if(OpType == 9) { continue; } else if(OpType == 1) { Op1(OpValue); } else if(OpType == 2) { Op2(OpValue); } else if(OpType == 3) { Op3(OpValue); } else if(OpType == 5) { Op5(OpValue); } else if(OpType == 6) { ptr = OpValue; continue; } else if(OpType == 7 && FakeAccumulator == 0) { ptr = OpValue; continue; } else if(OpType == 8 && FakeAccumulator >= 0) { ptr = OpValue; continue; } ptr += 1; } } // Update is called once per frame void Update() { Compile(); if(TimeStop > 0) { TimeStop -= 1; return; } if(ActionExisted) { if(!Door[IndexChange].DoorIsMoving) { if(ActionChange == 1) Door[IndexChange].OpenDoor(); else Door[IndexChange].CloseDoor(); } if(Door[IndexChange].TimeRotation == 900) { ActionExisted = false; IndexChange = -1; ActionChange = -1; } } else { if(AllOperation.Count == 0) return; var Current = AllOperation.Peek(); AllOperation.Dequeue(); if(Current.op == 0) { if(Current.idx == 100) { Accumulator = Current.val; return; } Label[Current.idx].ChangeValue(Current.val); } else if(Current.op == 4) { if(Current.idx == 100) { Accumulator += Current.val; return; } } else if(Current.op == 3) { TimeStop = Current.val; } else { ActionExisted = true; IndexChange = Current.idx; ActionChange = Current.op; } } } }