还是上次说的那个网络游戏,定下来是一个休闲的FPS游戏。和CS差不多,8到16个玩家在游戏里面分成2组对战射击。现在要实现初始化场景的工作。要呈现一个三维物体一般两个元素是少不了的,一是这个物体的骨架,也就是模型,二就是这个骨架上填充的纹理。
  我们知道,这样的一个游戏不可能只有一张地图,而且地图的数量肯定是会一直增加的。如果游戏在初始化场景的时候需要根据不同的地图分别加载模型和纹理对象,那么势必就会使得场景的扩充变得很不方便。由此,我们引入Abstract Factory,抽象工厂生产的都是实际类型的接口(或者抽象类型),如果加了新的场景可以确保不需要修改加载场景的那部分代码。
  using System; 
  using System.Reflection; 
  namespace AbstractFactoryExample 
  { 
  class Program 
  { 
  static void Main(string[] args) 
  { 
  Patrix patrix = new Patrix(); 
  patrix.LoadScene("HalfPaper"); 
  patrix.LoadScene("Matrix"); 
  } 
  } 
  class Patrix 
  { 
  private PatrixSceneFactory GetGameScene(string gameSceneName) 
  { 
  return (PatrixSceneFactory)Assembly.Load("AbstractFactoryExample").CreateInstance("AbstractFactoryExample." + gameSceneName); 
  } 
  public void LoadScene(string gameSceneName) 
  { 
  PatrixSceneFactory psf = GetGameScene(gameSceneName); 
  Texture texture = psf.CreateTexture(); 
  Model model = psf.CreateModel(); 
  model.FillTexture(texture); 
  } 
  } 
  abstract class PatrixSceneFactory 
  { 
  public abstract Model CreateModel(); 
  public abstract Texture CreateTexture(); 
  } 
  abstract class Model 
  { 
  public abstract void FillTexture(Texture texture); 
  } 
  abstract class Texture 
  { 
  } 
  class HalfPaper : PatrixSceneFactory 
  { 
  public override Model CreateModel() 
  { 
  return new HalfPaperModel(); 
  } 
  public override Texture CreateTexture() 
  { 
  return new HalfPaperTexture(); 
  } 
  } 
  class HalfPaperModel : Model 
  { 
  public HalfPaperModel() 
  { 
  Console.WriteLine("HalfPaper Model Created"); 
  } 
  public override void FillTexture(Texture texture) 
  { 
  Console.WriteLine("HalfPaper Model is filled Texture"); 
  } 
  } 
  class HalfPaperTexture : Texture 
  { 
  public HalfPaperTexture() 
  { 
  Console.WriteLine("HalfPaper Texture Created"); 
  } 
  } 
  class Matrix : PatrixSceneFactory 
  { 
  public override Model CreateModel() 
  { 
  return new MatrixModel(); 
  } 
  public override Texture CreateTexture() 
  { 
  return new MatrixTexture(); 
  } 
  } 
  class MatrixModel : Model 
  { 
  public MatrixModel() 
  { 
  Console.WriteLine("Matrix Model Created"); 
  } 
  public override void FillTexture(Texture texture) 
  { 
  Console.WriteLine("Matrix Model is filled Texture"); 
  } 
  } 
  class MatrixTexture : Texture 
  { 
  public MatrixTexture() 
  { 
  Console.WriteLine("Matrix Texture Created"); 
  } 
  } 
  }
赞
If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
 
 
QQ:154298438
QQ:417480759