diff options
Diffstat (limited to 'Space Invaders/GameEngine')
-rw-r--r-- | Space Invaders/GameEngine/GameEngine.cs | 167 | ||||
-rw-r--r-- | Space Invaders/GameEngine/Graphics.cs | 65 |
2 files changed, 232 insertions, 0 deletions
diff --git a/Space Invaders/GameEngine/GameEngine.cs b/Space Invaders/GameEngine/GameEngine.cs new file mode 100644 index 0000000..6c5c5b0 --- /dev/null +++ b/Space Invaders/GameEngine/GameEngine.cs @@ -0,0 +1,167 @@ +using System; +using System.Collections.Generic; +using System.Threading; + +namespace GameEngine { + + abstract class Game { + + virtual public int ConsoleHeight { get; set; } + virtual public int ConsoleWidth { get; set; } + + public List<GameObject> GameObjects = new List<GameObject>(); + public Thread _listnerThread = new Thread(Input.Listner); + + private Graphics _graphics; + private bool _running; + + abstract public void Setup(); + + virtual public void Start() { + // Do initialization + _graphics = new Graphics(ConsoleHeight, ConsoleWidth); + + // Run start thing in all GameObjects + for (int i = 0; i < GameObjects.Count; i++) { + GameObjects[i].Start(); + } + + // Start loop and other threads + _running = true; // This is a notifier to every thread, that will help us close the app + _listnerThread.Start(); + GameLoop(); + } + + private void GameLoop() { + while (_running) { + + // Run the Update method in each GameObject in the scene + for (int i = 0; i < GameObjects.Count; i++) { + GameObjects[i].Update(); + } + + // Scale every GameObject + for (int i = 0; i < GameObjects.Count; i++) { + GameObjects[i].ScaleSprite(); + } + + // Last thing to do each time. Render the frame and display it! + Frame nextFrame = new Frame(); + nextFrame.Buffer = RenderGameObjects(); // Make buffer, remove dead objects + _graphics.DrawFrame(nextFrame); + + // Run lateUpdate on each gameObject + for (int i = 0; i < GameObjects.Count; i++) { + GameObjects[i].LateUpdate(nextFrame); + } + } + } + + private char[,] RenderGameObjects() { + // This will make a buffer of all gameObjects on Screen + char[,] buffer = new char[ConsoleHeight, ConsoleWidth]; + + for (int i = 0; i < GameObjects.Count; i++) { + GameObject gameObject = GameObjects[i]; + char[,] sprite = gameObject.Sprite; + + if (gameObject.isDead) { + GameObjects.RemoveAt(i); + continue; + } + if (gameObject.Sprite == null) { continue; } + + for (int y = 0; y < sprite.GetLength(0); y++) { + for (int x = 0; x < sprite.GetLength(1); x++) { + try { + buffer[gameObject.yPos + y, gameObject.xPos + x] = sprite[y, x]; + } catch { } + } + } + } + + return buffer; + } + + } + + static class Input { + + static List<ConsoleKey> KeyList = new List<ConsoleKey>(); + + static public void Listner() { + while (true) { + if (Console.KeyAvailable) { + KeyList.Add(Console.ReadKey().Key); + } + } + } + + static public bool KeyPressed(ConsoleKey keyCode) { + for (int i = 0; i < KeyList.Count; i++) { + if (KeyList[i] == keyCode) { + KeyList.RemoveAt(i); + return true; + } + } + return false; + } + + } + + abstract class GameObject { + public Game Parent; + virtual public bool isDead { get; set; } + virtual public String Tag { get; set; } + virtual public int Scale { get; set; } + virtual public int xPos { get; set; } + virtual public int yPos { get; set; } + virtual public Char[,] Sprite { get; set; } + + private Char[,] RenderedSprite; + + virtual public void Start() { + // This method is called once before the game actually starts + } + + virtual public void Update() { + // This method is called right before the frame is rendered + } + + virtual public void LateUpdate(Frame thisFrame) { + // This method is called after the frame is rendered + } + + public void ScaleSprite() { + // This is called after the Update method, and scales the sprite if it has changed + if (RenderedSprite == Sprite) { return; } + + RenderedSprite = new Char[Sprite.GetLength(0) * Scale, Sprite.GetLength(1) * Scale * 2]; + + for (int y = 0; y < Sprite.GetLength(0); y++) { + for (int x = 0; x < Sprite.GetLength(1); x++) { + + for (int y1 = 0; y1 < Scale; y1++) { + for (int x1 = 0; x1 < (Scale * 2); x1++) { // The *2 is because a char is twice as high as wide + + try { + int xOffset = x1 + ((Scale + Scale) * x) - x; + int yOffset = y1 + (Scale * y) - y; + int xPos = xOffset + x; + int yPos = yOffset + y; + char cChar = Sprite[y, x]; + RenderedSprite[yPos, xPos] = cChar; + } catch { } + + } + } + + + } + } + Sprite = RenderedSprite; + } + + } + +} diff --git a/Space Invaders/GameEngine/Graphics.cs b/Space Invaders/GameEngine/Graphics.cs new file mode 100644 index 0000000..1c15199 --- /dev/null +++ b/Space Invaders/GameEngine/Graphics.cs @@ -0,0 +1,65 @@ +using System; +using System.Diagnostics; + +namespace GameEngine { + + class Graphics { + /* This class is made for drawing frame by frame in the console. + * It is designed to be used from a while loop, where the draw-frame method is called at the end of the loop */ + + // IMPORTANT! You might have to change the console font size, depending on how many rows and columns you want to have! + + public int Rows { get; } // This should not be editable + public int Columns { get; } // Neither should this + public int FrameNum; // How many frames have been drawn + + private Char[,] _consoleBuffer; // This stores how the screen looked last frame + private Stopwatch _stopWatch; // This is used to calculate time between frames + + public Graphics(int columns, int rows) { + Rows = rows; + Columns = columns; + _consoleBuffer = new Char[Columns, Rows]; + + Console.CursorVisible = false; + Console.WindowHeight = Columns; + Console.WindowWidth = Rows; + Console.SetBufferSize(rows, columns); + + _stopWatch = Stopwatch.StartNew(); + FrameNum = 0; + } + + public void DrawFrame(Frame newFrame) { + /* Method that draws all changes from last frame to the screen */ + FrameNum++; + //Console.Clear(); + //Console.SetCursorPosition(0, 0); + for (int y = 0; y < _consoleBuffer.GetLength(0); y++) { + for (int x = 0; x < _consoleBuffer.GetLength(1); x++) { + + if (newFrame.Buffer[y, x] != _consoleBuffer[y, x]) { + _consoleBuffer[y, x] = newFrame.Buffer[y, x]; + Console.SetCursorPosition(x, y); + Console.Write(newFrame.Buffer[y, x]); + } + + } + } + + _stopWatch.Reset(); + } + + public long DeltaTime() { + /* Method that returns time since last frame */ + return _stopWatch.ElapsedMilliseconds; + } + + + } + + class Frame { + public char[,] Buffer; + } + +} |