You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.6 KiB
C#

1 year ago
using System;
1 year ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
1 year ago
using TMPro;
1 year ago
public class Operation {
public int idx, val, op;
1 year ago
// 0: ChangeValue
// 1: OpenDoor
// 2: CloseDoor
// 3: Delay
// 4: AddValue
1 year ago
}
public class Initalize : MonoBehaviour
{
public GameObject[] Boxes;
public DoorAnimation[] Door;
1 year ago
public NewTextModify[] Label;
public GameObject AllBoxes;
public int IndexChange = -1, ActionChange = -1, TimeStop = 0, Accumulator = 0;
1 year ago
public bool ActionExisted = false, ActionMoving = false;
Queue<Operation> AllOperation = new Queue<Operation>();
public GameObject TextAreaInstruction, TextAreaInput, CompileButton;
public TextMeshProUGUI Instruction, Input, AnotherDisplayAccumulator;
Queue<int> AllInput = new Queue<int>();
public int Output;
1 year ago
private bool GonnaCompile = true;
private int FakeAccumulator = 0;
private int[] FakeBoxes = new int[100];
1 year ago
// Start is called before the first frame update
void Start()
{
// public string s = DisplayAccumulator.GetComponent<TextMeshProUGUI>();
1 year ago
for(int i = 0; i < 100; ++i) {
Boxes[i] = AllBoxes.gameObject.transform.GetChild(i).gameObject;
Label[i] = Boxes[i].GetComponent<NewTextModify>();
Door[i] = Boxes[i].gameObject.transform.GetChild(0).GetChild(0).GetComponent<DoorAnimation>();
Label[i].Init(i);
}
}
public Operation Cope(int idx, int val, int op) {
var Op = new Operation();
Op.idx = idx; Op.val = val; Op.op = op;
return Op;
}
1 year ago
// Put value from Box into Accumulator
void Op5(int idx) {
AllOperation.Enqueue(Cope(idx, 0, 1));
AllOperation.Enqueue(Cope(100, FakeBoxes[idx], 0));
1 year ago
AllOperation.Enqueue(Cope(0, 100, 3));
AllOperation.Enqueue(Cope(idx, 0, 2));
FakeAccumulator = FakeBoxes[idx];
1 year ago
}
// 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, FakeAccumulator, 0));
1 year ago
AllOperation.Enqueue(Cope(0, 100, 3));
AllOperation.Enqueue(Cope(idx, 0, 2));
FakeBoxes[idx] = FakeAccumulator;
1 year ago
}
// 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];
}
void Op901() {
// Code to summon number from Input Bracket
AllOperation.Enqueue(Cope(100, AllInput.Peek(), 0));
FakeAccumulator = AllInput.Peek(); AllInput.Dequeue();
}
void Op902() {
// Code to summon number from Accumulator
AllOperation.Enqueue(Cope(101, FakeAccumulator, 0));
// Output = FakeAccumulator;
}
1 year ago
// Just update value of a box from nowhere
1 year ago
void UpdateValue(int idx, int value) {
AllOperation.Enqueue(Cope(idx, 0, 1));
AllOperation.Enqueue(Cope(idx, value, 0));
1 year ago
AllOperation.Enqueue(Cope(0, 100, 3));
1 year ago
AllOperation.Enqueue(Cope(idx, 0, 2));
1 year ago
FakeBoxes[idx] = value;
}
void CompileError() {
}
bool CheckDigit(char c) {
return (c >= '0' && c <= '9');
}
public void Compile() {
1 year ago
if(!GonnaCompile) return;
GonnaCompile = false;
string InstructionText = Instruction.text;
string InputText = Input.text;
TextAreaInstruction.gameObject.SetActive(false);
CompileButton.gameObject.SetActive(false);
TextAreaInput.gameObject.SetActive(false);
int InputLength = InputText.Length;
for(int i = 0; i < InputLength; ++i) {
int idx = i, value = 0;
while(idx < InputLength && CheckDigit(InputText[idx])) {
value = value * 10 + (InputText[idx] - '0');
++idx;
}
if(i != idx) {
i = idx - 1;
AllInput.Enqueue(value);
}
}
1 year ago
int[] EncodeOperation = new int[100];
int InstructionLength = InstructionText.Length, Counter = 0;
for(int i = 0; i < InstructionLength - 2; i += 4) {
while(i < InstructionLength && !CheckDigit(InstructionText[i]))
++i;
EncodeOperation[Counter] = Convert.ToInt32(InstructionText.Substring(i, 3));
1 year ago
Counter += 1;
}
1 year ago
int ptr = 0;
while(true) {
int OpType = EncodeOperation[ptr] / 100;
int OpValue = EncodeOperation[ptr] % 100;
if(OpType == 0) {
break;
}
else if(OpType == 9) {
if(OpValue == 1) Op901();
if(OpValue == 2) Op902();
1 year ago
}
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;
}
1 year ago
}
// Update is called once per frame
void Update()
{
// Compile();
1 year ago
if(TimeStop > 0) {
TimeStop -= 1;
return;
}
1 year ago
if(ActionExisted) {
if(!Door[IndexChange].DoorIsMoving) {
if(ActionChange == 1) Door[IndexChange].OpenDoor();
else Door[IndexChange].CloseDoor();
}
if(Door[IndexChange].TimePassed >= 1.0f) {
1 year ago
ActionExisted = false;
IndexChange = -1;
ActionChange = -1;
}
}
else {
if(AllOperation.Count == 0) return;
var Current = AllOperation.Peek(); AllOperation.Dequeue();
if(Current.op == 0) {
1 year ago
if(Current.idx == 100) {
Accumulator = Current.val;
AnotherDisplayAccumulator.text = Accumulator.ToString();
1 year ago
return;
}
else if(Current.idx == 101) {
Output = Current.val;
return;
}
1 year ago
Label[Current.idx].ChangeValue(Current.val);
}
1 year ago
else if(Current.op == 4) {
if(Current.idx == 100) {
Accumulator += Current.val;
AnotherDisplayAccumulator.text = Accumulator.ToString();
1 year ago
return;
}
}
1 year ago
else if(Current.op == 3) {
TimeStop = Current.val;
}
1 year ago
else {
ActionExisted = true;
IndexChange = Current.idx;
ActionChange = Current.op;
}
}
}
void LateUpdate() {
}
1 year ago
}