Really, it got me asking so many questions to myself, and ended up finding out about C# versions and Unity’s C# version and Timers and anonymous functions (I think it’s not exagerated to say that typescript is the C# equivalent of Javascript)… it was an awesome experience!
Here’s my code, btw:
using System;
using UnityEngine;
public class Hacker : MonoBehaviour
{
// Game state
private int level = 0;
private enum Screen {
MainMenu,
Password,
Win
};
private string passwordToCheck;
private string[,] passwords = {
{ "unity", "human", "balto", "match", "poker" },
{ "detergent", "enchiladas", "confetti", "chocolate", "biscuits" },
{ "marshmallow", "modified", "constitution", "smurfette", "intelligence" }
};
Screen currentScreen;
// Use this for initialization
private void Start()
{
ShowMainMenu("Welcome, Player!");
}
// Update is called once per frame
private void Update()
{
}
// Show the main menu
private void ShowMainMenu(string greeting)
{
currentScreen = Screen.MainMenu;
Terminal.ClearScreen();
Terminal.WriteLine(greeting);
Terminal.WriteLine("You've been given a challenge. Prove \nyour worth by finding the hidden \nsecret for one of these places. Select a place to accept the challenge:\n");
Terminal.WriteLine("Press 1 for your neighbour's PC");
Terminal.WriteLine("Press 2 for the Supermarket Backend");
Terminal.WriteLine("Press 3 for the Presidential Computer");
Terminal.WriteLine("\nEnter your selection:");
}
private void OnUserInput(string input)
{
if (!string.IsNullOrEmpty(input)) {
if (input == "menu") {
ShowMainMenu("Welcome, Player!");
return;
}
switch (currentScreen) {
case Screen.MainMenu:
RunMainMenu(input);
break;
case Screen.Password:
ValidatePasswordInput(input);
break;
case Screen.Win:
ShowMainMenu("Welcome, Player!");
break;
}
}
else {
print("Empty input");
}
}
private void RunMainMenu(string input)
{
/// Easter eggs
switch (input) {
case "ranger":
Terminal.WriteLine("Go go Power Rangers!");
return;
case "furry":
Terminal.WriteLine("Please don't yiff me!");
return;
}
/// Actual validation
string[] options = { "1", "2", "3" };
if (Array.IndexOf(options, input) >= 0) {
int.TryParse(input, out level);
StartGame();
}
else {
Terminal.WriteLine("Wrong choice, Player!");
}
}
private void StartGame()
{
Terminal.ClearScreen();
string[] phrases = {
"Let's hack your neighbour!",
"Let's hack the market!",
"Let's hack the system!"
};
currentScreen = Screen.Password;
passwordToCheck = passwords[level - 1, 0]; /// get the first, for now
Terminal.WriteLine(phrases[level - 1]);
Terminal.WriteLine("Please enter your password:");
}
private void ValidatePasswordInput(string input)
{
if (input == passwordToCheck) {
ShowEndScreen();
}
else {
Terminal.WriteLine("Wrong password! Try again:");
}
}
private void ShowEndScreen()
{
currentScreen = Screen.Win;
Terminal.WriteLine("Password correct! Logging in...");
Terminal.WriteLine("..........");
Terminal.WriteLine("Logged in! Congratulations! \nType anything to start over.");
}
}