Advertisement
Cookie042

oop/event stuffs

Jul 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.        
  10.         var player1 = new Player("player 1");
  11.         var player2 = new Player("player 2");
  12.        
  13.         var game = new Game1();
  14.  
  15.         var running = true;
  16.        
  17.         while (running)
  18.         {
  19.             game.Update();
  20.             game.Draw();
  21.             Thread.Sleep(100);
  22.         }
  23.     }
  24. }
  25.  
  26. public class Game1
  27. {
  28.     public delegate void UpdateDelegate();
  29.     public delegate void DrawDelegate();
  30.  
  31.     public static event UpdateDelegate UpdateHandler;
  32.     public static event DrawDelegate DrawHandler;
  33.  
  34.     public void Update()
  35.     {
  36.         //the ? is a null check
  37.         UpdateHandler?.Invoke();
  38.     }
  39.  
  40.     public void Draw()
  41.     {
  42.         DrawHandler?.Invoke();
  43.        
  44.     }
  45.    
  46. }
  47.  
  48. public abstract class Entity
  49. {
  50.     // static fields
  51.     public static List<Entity> entities = new List<Entity>();
  52.  
  53.     //static constructor, run before anything is instantiated
  54.     static Entity()
  55.     {
  56.         //adding the update methods to the game events
  57.         Game1.UpdateHandler += UpdateAll;
  58.         Game1.DrawHandler += DrawAll;
  59.     }
  60.    
  61.     // static methods
  62.     public static void UpdateAll() // called from game1 event invocation
  63.     {
  64.         foreach (Entity entity in entities) entity.Update();
  65.     }
  66.  
  67.     public static void DrawAll() // called from game1 event invocation
  68.     {
  69.         foreach (Entity entity in entities) entity.Draw();
  70.     }
  71.  
  72.    
  73.    
  74.     // instance fields
  75.  
  76.     public string name;
  77.     //public Vector3 position;
  78.     //...
  79.  
  80.     //constructor
  81.     public Entity()
  82.     {
  83.        
  84.         Console.WriteLine("Entity " + name + " added to entities list");
  85.         entities.Add(this);
  86.     }
  87.  
  88.     //abstract intsance methods, these must be inherited
  89.     public abstract void Update();
  90.     public abstract void Draw();
  91. }
  92.  
  93. public class Player : Entity
  94. {
  95.  
  96.     public Player(string name)
  97.     {
  98.         //base class is also run to add it to the entities list in the base class
  99.         Console.WriteLine("new player:" + name);
  100.         this.name = name;
  101.     }
  102.    
  103.     public override void Update()
  104.     {
  105.         Console.WriteLine("update:" + name);
  106.         //this is called via the Entity UpdateAll() static method
  107.     }
  108.  
  109.     public override void Draw()
  110.     {
  111.         Console.WriteLine("draw: " + name);
  112.         //this is called via the Entity UpdateAll() static method
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement