Skip to content

Commit a16675a

Browse files
committed
Added a tiny game / bootseq framework
1 parent 146680b commit a16675a

File tree

8 files changed

+235
-70
lines changed

8 files changed

+235
-70
lines changed

Assets/BootSequence.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
[DefaultExecutionOrder(-1001)]
6+
public class BootSequence : MonoBehaviour
7+
{
8+
Game m_Game;
9+
10+
void Awake()
11+
{
12+
DontDestroyOnLoad(gameObject);
13+
m_Game = new Game();
14+
m_Game.Init();
15+
}
16+
17+
void OnDestroy()
18+
{
19+
m_Game.Shutdown();
20+
}
21+
22+
void Update() { m_Game.Update(); }
23+
void LateUpdate() { m_Game.LateUpdate(); }
24+
void FixedUpdate() { m_Game.FixedUpdate(); }
25+
}
26+

Assets/BootSequence.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/DebugOverlay/Console.cs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55

6-
public class Console
6+
public class Console : IGameSystem
77
{
8-
int m_Width = 80;
9-
int m_Height = 25;
8+
int m_Width;
9+
int m_Height;
1010
int m_NumLines;
1111
int m_LastLine;
1212
int m_LastColumn;
@@ -50,12 +50,22 @@ void CmdHelp(string[] args)
5050
}
5151
}
5252

53+
public void Init()
54+
{
55+
Init(80, 25);
56+
}
57+
5358
public void Init(int width, int height)
5459
{
5560
Resize(width, height);
5661
Clear();
5762
}
5863

64+
public void Shutdown()
65+
{
66+
67+
}
68+
5969
public delegate void CommandDelegate(string[] args);
6070
Dictionary<string, CommandDelegate> m_Commands = new Dictionary<string, CommandDelegate>();
6171
Dictionary<string, string> m_CommandDescriptions = new Dictionary<string, string>();
@@ -73,9 +83,6 @@ public void AddCommand(string name, CommandDelegate callback, string description
7383

7484
public void Resize(int width, int height)
7585
{
76-
var oldWidth = m_Width;
77-
var oldHeight = m_Height;
78-
7986
m_Width = width;
8087
m_Height = height;
8188
m_NumLines = m_ConsoleBuffer.Length / m_Width;
@@ -137,6 +144,8 @@ public void TickUpdate()
137144
m_CursorPos = m_InputFieldLength;
138145
else if (Input.GetKeyDown(KeyCode.Backspace))
139146
Backspace();
147+
else if (Input.GetKeyDown(KeyCode.Tab))
148+
TabComplete();
140149
else if (Input.GetKeyDown(KeyCode.Return))
141150
{
142151
ExecuteCommand(new string(m_InputFieldBuffer, 0, m_InputFieldLength));
@@ -219,7 +228,8 @@ public void TickLateUpdate()
219228
if (c != '\0')
220229
DebugOverlay.instance.AddQuad(i - horizontalScroll, m_Height - 1 + yoffset, 1, 1, c, m_TextColor);
221230
}
222-
DebugOverlay.instance.AddQuad(m_CursorPos - horizontalScroll, m_Height - 1 + yoffset, 1, 1, '\0', m_CursorCol);
231+
if(m_ConsoleState == ConsoleState.Open)
232+
DebugOverlay.instance.AddQuad(m_CursorPos - horizontalScroll, m_Height - 1 + yoffset, 1, 1, '\0', m_CursorCol);
223233
}
224234

225235
void NewLine()
@@ -310,4 +320,55 @@ void Backspace()
310320
m_CursorPos--;
311321
m_InputFieldLength--;
312322
}
323+
324+
void TabComplete()
325+
{
326+
string prefix = new string(m_InputFieldBuffer, 0, m_CursorPos);
327+
328+
// Look for possible tab completions
329+
List<string> matches = new List<string>();
330+
331+
foreach (var c in m_Commands)
332+
{
333+
var name = c.Key;
334+
if (!name.StartsWith(prefix, true, null))
335+
continue;
336+
matches.Add(name);
337+
}
338+
339+
if (matches.Count == 0)
340+
return;
341+
342+
// Look for longest common prefix
343+
int lcp = matches[0].Length;
344+
for (var i = 0; i < matches.Count - 1; i++)
345+
{
346+
lcp = Mathf.Min(lcp, CommonPrefix(matches[i], matches[i + 1]));
347+
}
348+
var bestMatch = matches[0].Substring(prefix.Length, lcp - prefix.Length);
349+
foreach (var c in bestMatch)
350+
Type(c);
351+
if (matches.Count > 1)
352+
{
353+
// write list of possible completions
354+
for (var i = 0; i < matches.Count; i++)
355+
Write(" {0}\n",matches[i]);
356+
}
357+
358+
if(matches.Count == 1)
359+
Type(' ');
360+
}
361+
362+
// Returns length of largest common prefix of two strings
363+
static int CommonPrefix(string a, string b)
364+
{
365+
int minl = Mathf.Min(a.Length, b.Length);
366+
for (int i = 1; i <= minl; i++)
367+
{
368+
if (!a.StartsWith(b.Substring(0, i), true, null))
369+
return i - 1;
370+
}
371+
return minl;
372+
}
373+
313374
}

Assets/DebugOverlay/DebugOverlay.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ public class DebugOverlay : MonoBehaviour
2525
public static int Width { get { return instance.width; } }
2626
public static int Height { get { return instance.height; } }
2727

28-
public void Init()
28+
void Awake()
2929
{
3030
m_LineMaterial = new Material(Shader.Find("Instanced/LineShaderProc"));
31-
instance = this;
3231
}
3332

33+
public void Init()
34+
{
35+
instance = this;
36+
}
3437

3538
public void Shutdown()
3639
{

Assets/Demo/Game.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using System;
5+
#if UNITY_EDITOR
6+
using UnityEditor;
7+
#endif
8+
9+
interface IGameSystem
10+
{
11+
void Init();
12+
void Shutdown();
13+
void TickUpdate();
14+
}
15+
16+
public class Game
17+
{
18+
private static Game _instance;
19+
20+
static public Console console { get { return _instance.m_Console; } }
21+
static public DebugOverlay debugOverlay { get { return _instance.m_DebugOverlay; } }
22+
23+
DebugOverlay m_DebugOverlay;
24+
Console m_Console;
25+
Stats m_Stats;
26+
27+
public void Init()
28+
{
29+
Debug.Assert(_instance == null);
30+
_instance = this;
31+
32+
m_DebugOverlay = GameObject.FindObjectOfType<DebugOverlay>();
33+
m_DebugOverlay.Init();
34+
35+
m_Console = new Console();
36+
m_Console.Init(DebugOverlay.Width, DebugOverlay.Height);
37+
m_Console.AddCommand("quit", CmdQuit, "Quit game");
38+
39+
m_Stats = new Stats();
40+
m_Stats.Init();
41+
42+
Game.console.Write("Game initialized\n");
43+
}
44+
45+
public void Shutdown()
46+
{
47+
Debug.Assert(_instance == this);
48+
49+
m_DebugOverlay.Shutdown();
50+
m_DebugOverlay = null;
51+
m_Console.Shutdown();
52+
m_Console = null;
53+
m_Stats.Shutdown();
54+
m_Stats = null;
55+
56+
_instance = null;
57+
}
58+
59+
void CmdQuit(string[] args)
60+
{
61+
#if UNITY_EDITOR
62+
EditorApplication.isPlaying = false;
63+
#else
64+
Application.Quit();
65+
#endif
66+
}
67+
68+
public void Update()
69+
{
70+
m_Stats.TickUpdate();
71+
m_Console.TickUpdate();
72+
}
73+
74+
75+
public void LateUpdate()
76+
{
77+
m_Console.TickLateUpdate();
78+
m_DebugOverlay.TickLateUpdate();
79+
}
80+
81+
public void FixedUpdate()
82+
{
83+
}
84+
}

0 commit comments

Comments
 (0)