码迷,mamicode.com
首页 > 其他好文 > 详细

gof-组件模式

时间:2019-08-25 21:44:56      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:组件   graphic   art   逻辑   场景   pre   on()   send   pac   

应用场景:类关联的域比较多,功能过多 你懒通过多重继承实现功能细分时

体验:逻辑更有层次感,功能迭代更加便捷,运维难度-1

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

namespace ComponentPatter_original
{

    public class World {
        public void resolveCollision(int x,int y,int z) { }
    }

    public class Graphic {
        public void draw(int x,int y) { }
    }

    public static class InputInfo {
        public static int GetJoyDirection() {
            return 0;
        }
    }


    public class Bjron {
        private static readonly int WALK_ACCCELERATION = 1;
        private int x, y, velocity;
        private Sprite stand, walkLeft, walkRight;
        public Bjron():this(0,0,0) {}
        public Bjron(int x, int y, int velocity) {
            this.x = x;
            this.y = y;
            this.velocity = velocity;
        }
        public void Update(World world, Graphic graphics) {
            switch (InputInfo.GetJoyDirection()) {
                case 1: x += WALK_ACCCELERATION; break;
                case 2: y += WALK_ACCCELERATION; break;
            }


            this.velocity += WALK_ACCCELERATION;
            world.resolveCollision(x,y,velocity);
            graphics.draw(x,y);
        }
    }
}

namespace ComponentPatter_Improve {
    public class World
    {
        public void resolveCollision(int x, int y, int z) { }
    }

    public class Graphic
    {
        public void draw(int x, int y) { }
    }

    public static class InputInfo
    {
        public static int GetJoyDirection()
        {
            return 0;
        }
    }

    //将处理输入操作的部分独立出来
    public class InputComponent {
        public void Update(Bjron bjron) {
            switch (InputInfo.GetJoyDirection())
            {
                case 1: bjron.x += WALK_ACCCELERATION; break;
                case 2: bjron.y += WALK_ACCCELERATION; break;
            }
        }
        private readonly int WALK_ACCCELERATION = 1;
    }

    public class PhysicsComponent {
        private int velocity;
        public void Update(Bjron bjorn, World world) {
            world.resolveCollision(bjorn.x, bjorn.y, velocity);
        }
    }

    public class GraphyicsComponent {
        public float color_r, color_g, color_b;
        public void Update(Bjron bjorn, Graphic gph)
        {
            gph.draw(bjorn.x, bjorn.y);
        }
    }

    public class Bjron
    {
        private static readonly int WALK_ACCCELERATION = 1;
        public int x, y, velocity;
        private Sprite stand, walkLeft, walkRight;
        
        public Bjron() : this(0, 0, 0) { }
        public Bjron(int x, int y, int velocity)
        {
            this.x = x;
            this.y = y;
            this.velocity = velocity;
        }

        private InputComponent input;
        private PhysicsComponent physic;
        private GraphyicsComponent graphic;
        public void Update(World world, Graphic graphics)
        {
            input.Update(this);
            physic.Update(this,world);
            graphic.Update(this,graphics);
        }
    }
}


namespace ComponentPatter_Final
{
    //组件与组件之前通讯的两种方式 1,把组件包丢给组件使用 这样的话耦合度比较高 逻辑处理起来比较散乱 看团队喜好了...
    //另一种如下 创建一个中介者的接口用于 把功能包装成命令 在中介中枢集中处理消息 个人感觉这样写起来比较明了 这里不写了.... 用第一种
    public abstract class IComponent_Sample{
        public virtual void SendMessage(string msg) {

        }
    }
    public class World
    {
        public void resolveCollision(int x, int y, int z) { }
    }

    public class Graphic
    {
        public void draw(int x, int y) { }
    }

    public static class InputInfo
    {
        public static int GetJoyDirection()
        {
            return 0;
        }
    }

    //将功能细分 逐个处理
    public class InputComponent:IComponent_Sample
    {
        public virtual void Update(GmaeObj bjron)
        {
            switch (InputInfo.GetJoyDirection())
            {
                case 1: bjron.x += WALK_ACCCELERATION; break;
                case 2: bjron.y += WALK_ACCCELERATION; break;
            }
        }

        private readonly int WALK_ACCCELERATION = 1;
    }

    public class InputComponent_demo1:InputComponent {
        public override void Update(GmaeObj gameObject) {
            base.Update(gameObject);
            //迭代或添加功能
        }
    }
    public class PhysicsComponent
    {
        private int velocity;
        public virtual void Update(GmaeObj gameObject, World world)
        {
            world.resolveCollision(gameObject.x, gameObject.y, velocity);
        }
        public virtual void AddVelocity() {}
    }

    public class GraphyicsComponent
    {
        public float color_r, color_g, color_b;
        public virtual void Update(GmaeObj gameObject, Graphic gph)
        {
            gph.draw(gameObject.x, gameObject.y);
            gameObject.physic.AddVelocity();
        }
    }

    public class GmaeObj
    {
        private static readonly int WALK_ACCCELERATION = 1;
        public int x, y, velocity;
        private Sprite stand, walkLeft, walkRight;

        public GmaeObj() : this(0, 0, 0) { }
        public GmaeObj(int x, int y, int velocity)
        {
            this.x = x;
            this.y = y;
            this.velocity = velocity;
        }
        //用来更换组建
        public GmaeObj(InputComponent ic,PhysicsComponent pc, GraphyicsComponent gc):this(){
            this.input = ic;
            this.physic = pc;
            this.graphic = gc;

        }

        //组件向外界暴漏用来做功能迭代和组件交互
        public InputComponent input;
        public PhysicsComponent physic;
        public GraphyicsComponent graphic;
        public void Update(World world, Graphic graphics)
        {
            input.Update(this);
            physic.Update(this, world);
            graphic.Update(this, graphics);
        }
    }

    public class StartGame {
        void Start() {
            var game = new GmaeObj();
            //更新组件
            game.input = new InputComponent_demo1();

        }
    }
}

 

gof-组件模式

标签:组件   graphic   art   逻辑   场景   pre   on()   send   pac   

原文地址:https://www.cnblogs.com/chenggg/p/11409445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!