forked from Habrador/Unity-Programming-Patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVM.cs
More file actions
111 lines (86 loc) · 3.23 KB
/
Copy pathVM.cs
File metadata and controls
111 lines (86 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BytecodePattern
{
public class VM
{
private GameController gameController;
//Will store values for later use in the switch statement
private Stack<int> stackMachine = new Stack<int>();
//The max size of the stack
private const int MAX_STACK = 128;
public VM(GameController gameController)
{
this.gameController = gameController;
}
public void Interpret(int[] bytecode)
{
stackMachine.Clear();
//Read and execute the instructions
for (int i = 0; i < bytecode.Length; i++)
{
//Convert from int to enum
Instruction instruction = (Instruction)bytecode[i];
switch (instruction)
{
case Instruction.INST_SET_HEALTH:
{
//Important to pop amount before wizard because we push wizard before amount onto the stack
int amount = Pop();
int wizard = Pop();
gameController.SetHealth(wizard, amount);
break;
}
case Instruction.INST_LITERAL:
{
////Important that this i++ is not inside bytecode[i++] or it will not jump to next i
//i++;
//int value = bytecode[i];
//Push(value);
//this can be a oneliner
//in this case bytecode will use i+1 bytecode element
Push(bytecode[++i]);
break;
}
case Instruction.INST_GET_HEALTH:
{
int wizard = Pop();
Push(gameController.GetHealth(wizard));
break;
}
case Instruction.INST_ADD:
{
int b = Pop();
int a = Pop();
Push(a + b);
break;
}
default:
{
Debug.Log($"The VM couldn't find the instruction {instruction} :(");
break;
}
}
}
}
//Stack methods
private int Pop()
{
if (stackMachine.Count == 0)
{
Debug.LogError("The stack is empty :(");
}
return stackMachine.Pop();
}
private void Push(int number)
{
//Check for stack overflow, which is useful because someone might make a mod that tries to break your game
if (stackMachine.Count + 1 > MAX_STACK)
{
Debug.LogError("Stack overflow is not just a place where you copy and paste code!");
}
stackMachine.Push(number);
}
}
}