Skip to content

Instantly share code, notes, and snippets.

@ThomasAunvik
Created August 9, 2017 20:08
Show Gist options
  • Save ThomasAunvik/ce27b9882d8416482486f9b15ce128ec to your computer and use it in GitHub Desktop.
Save ThomasAunvik/ce27b9882d8416482486f9b15ce128ec to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Computer : MonoBehaviour {
/// <summary>
/// The GameObject of Text
/// </summary>
public GameObject TextObject;
/// <summary>
/// The current computer
/// </summary>
public GameObject currentComputer;
public PCMenu ComputerMenu;
/// <summary>
/// The computer settings of the computer.
/// </summary>
protected ComputerSettings computerSettings;
// Use this for initialization
void Start () {
ResetComputer();
}
/// <summary>
/// The start of the cool-down, Time + Cool-down = startedCooldown
/// </summary>
private float startedCooldown = 0;
// Update is called once per frame
void Update () {
if(Input.anyKeyDown && Time.time > startedCooldown) {
KeyTapped();
startedCooldown = Time.time + computerSettings.tapCooldown;
}
}
/// <summary>
/// Will keep the number of key taps that have been pressed.
/// </summary>
private int keyTaps = 0, currentXNames = 0, currentYNames = 1;
/// <summary>
/// Will launch a method that will write the word "Code" to the computer screen.
/// </summary>
void KeyTapped() {
if(!ComputerMenu.isMenuOpen) { //TODO add ifInMenu equals false.
if(!(currentYNames >= computerSettings.yNames && currentXNames == computerSettings.xNames)) {
WriteText(!(keyTaps < currentYNames * computerSettings.xNames));
} else if(Input.GetKeyDown(KeyCode.Return)) CompleteCode();
}
}
/// <summary>
/// Writes the text "Code" either in a new line or not
/// </summary>
/// <param name="newLine">"Code" should be placed on a new line?</param>
void WriteText(bool newLine) {
string currentText = TextObject.GetComponent<TextMesh>().text;
if(!newLine) {
currentText = currentText + "Code";
currentXNames++;
} else {
currentText = currentText + "\nCode";
currentYNames++;
currentXNames = 1;
}
keyTaps++;
TextObject.GetComponent<TextMesh>().text = currentText;
}
/// <summary>
/// Writes the text "Code"
/// </summary>
void WriteText() { WriteText(false); }
/// <summary>
/// After completing a Code Block you will receive points
/// </summary>
void CompleteCode() {
ResetComputer();
// TODO create a point system
}
/// <summary>
/// Resets the current computer to its default state, and resets the progress of key-taps
/// </summary>
void ResetComputer() {
GetComponent<SpriteRenderer>().sprite = currentComputer.GetComponent<SpriteRenderer>().sprite;
computerSettings = currentComputer.GetComponent<ComputerSettings>();
TextObject.GetComponent<TextMesh>().text = "";
gameObject.transform.localPosition = computerSettings.computerPosition;
gameObject.transform.localScale = computerSettings.computerSize;
TextObject.transform.localPosition = computerSettings.textPosition;
TextObject.transform.localScale = computerSettings.textSize;
keyTaps = 0; currentXNames = 0; currentYNames = 1;
}
public void ChangeComputer(GameObject newComputer) {
currentComputer = newComputer;
ResetComputer();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment