• 游戏开发中的设计模式


    单例模式

    实例化单一对象,懒加载

    //单例模式
    class GameManagerSingleton
    {
        private constructor(){}
        private static instance:GameManagerSingleton;
        public static Instance()
        {
            if(!GameManagerSingleton.instance)
            {
                this.instance = new GameManagerSingleton();
            }
            return this.instance;
        }
    
    
        Init(){}
    
    }
    
    GameManagerSingleton.Instance().Init();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    代理模式

    根据不同的代理者,具体化实际方法操作

    //代理模式
    interface ICall
    {
        DealNum(x1:number,x2:number):number;
    }
    
    class NPC1 implements ICall
    {
        DealNum(x1: number, x2: number): number {
            return x1 + x2;
        }
    }
    
    class NPC2 implements ICall
    {
        DealNum(x1: number, x2: number): number {
            return x1 - x2;
        }
    }
    
    class Person
    {
        public delegate : ICall;
        public DealNum(x1,x2) : number
        {
            let retNum = this.delegate.DealNum(x1,x2);
            return retNum;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    观察者模式

    设置观察者后,对象的变更会告知观察者

    
    //观察者模式
    interface IObserver
    {
        nameChanged(newName);
    }
    
    class Test implements IObserver
    {
      //捕捉变化
        nameChanged(newName : string) 
        {
            console.log("NewName:"+newName)
        }
    }
    
    class People
    {
        private name : string;
        /**
         *
         */
        constructor() {
            this.name = ""
        }
        public get Name()
        {
           return this.name;
        }
    
        public set Name(value)
        {
            if(this.name != value)
            {
                this.name = value;
                //发生变化会向观察者发消息
                for(let observer of this.Observers)
                {
                    observer.nameChanged(this.name);
                }
            }
            
        }
        Observers : Array<IObserver> = new Array<IObserver>();
    }
    
    
    
    let People1 = new People()
    let observer1  = new Test()
    //设置观察者
    People1.Observers.push(observer1)
    //改变对象
    People1.Name = "Tom"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    工厂模式

    游戏中构建item群体的常用模式

    enum CarType
    {
        BMW,
        Benz,
        Audi,
    }
    
    
    class Car 
    {
       name : string;
       static CreateCar(type :  CarType)
        {
            switch(type)
            {
                case CarType.BMW:
                    return new BMW();
                case CarType.Benz:
                    return new Benz();
            }
        }
    }
    
    class BMW extends Car
    {
        /**
         *
         */
        constructor() {
            super();
            this.name = "BMW"
        }
    }
    class Benz extends Car
    {
        /**
         *
         */
        constructor() {
            super();
            this.name = "Benz"
        }
    }
    
    Car.CreateCar(CarType.Benz)
    Car.CreateCar(CarType.BMW)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    餐饮美食网页设计(HTML+CSS+JavaScript)
    计算程序运行时间:计算或者不计算sleep()的两种情况perf_counter()和process_time()
    动物园
    js 获取当前域名或Url的参数
    华为数通方向HCIP-DataCom H12-831题库(多选题:221-240)
    Linux编译链接选项静态库--whole-archive,--no-whole-archive
    微信小程序中 web-view 组件渲染外部 h5页面如何使用?
    欠拟合、过拟合现象,及解决办法
    GateWay 常用配置文件
    玩转ChatGPT:Kimi测评(科研写作)
  • 原文地址:https://blog.csdn.net/Mr_PEaSutBUtteR/article/details/133755850