OT源代码的分析,OrtHello 迟早攻破你 (七)第2个例子,碰撞和简单运动

来源:互联网 发布:决战平安京邀请码淘宝 编辑:程序博客网 时间:2024/05/21 12:01

首先记住在main的updata里面,OT的初始化不是放在start中的,而是在update中的,具体做法是:

void Update () {
        if (!OT.isValid) return;

        if (!initialized)
            Initialize();
    }

-,-简单有效orz

再看看CExample2 到底做了什么,首先传入了两个OTsprite:

 public OTSprite blockPrototype; // prototype to create blocks
 public OTSprite starPrototype;  // prototype to create stars

然后就是初始化函数,你会发现update里面什么判断都没有

CreateObjects()
    {
        GameObject  blocks = GameObject.Find("Blocks");  // 这是在场景中的一个空的物体,最后当成边界用
        if (blocks!=null)
        {
            int c = (Screen.width - 20) / 85;  由屏幕大小计算横向可以摆多少个
            int s = (Screen.width - (c * 85)) / 2;  计算间隔
           
            for (int x = 0; x < c; x++)  // 创建横向顶部的方块
            {
                //关键部分,由blockPrototype来创建,属性就会喝blockPrototype一摸一样
                OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
                b.position = new Vector2(s + 50 + (x * 85)- (Screen.width/2), 20 - (Screen.height/2));
                b.name = "bottom" + x;
                // 链接到父物体
                b.transform.parent = blocks.transform;
            }


            for (int x = 0; x < c; x++)//同理 创建横向底部的方块
            {
                OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
                b.position = new Vector2(s + 50 + (x * 85) - (Screen.width / 2), Screen.height - 20 - (Screen.height / 2));
                b.name = "top" + x;
                b.transform.parent = blocks.transform;
            }

            c = (Screen.height - 50) / 85;
            s = (Screen.height - (c * 85)) / 2;


            for (int x = 0; x < c; x++)
            {
                OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
                b.rotation = 90; // 旋转了一下
                b.position = new Vector2(20 - (Screen.width / 2), (Screen.height/2) - 40 - s - (x*85) );
                b.name = "left" + x;
                b.transform.parent = blocks.transform;
            }

            for (int x = 0; x < c; x++)
            {
                OTSprite b = (Instantiate(blockPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
                b.rotation = 90;
                b.position = new Vector2((Screen.width / 2)-20, (Screen.height / 2) - 40 - s - (x * 85));
                b.name = "right" + x;
                b.transform.parent = blocks.transform;
            }
        }

        GameObject stars = GameObject.Find("Stars"); //找到空物体星星
        if (stars != null)
        {
            int c = 50;   // 创建的星星数量
            for (int x = 0; x < c; x++)
            {
                // Create a new star
                OTSprite s = (Instantiate(starPrototype.gameObject) as GameObject).GetComponent<OTSprite>();
                s.position =
                    new Vector2(
                        -1 * (Screen.width / 2) + 50 + Random.value * (Screen.width - 100),
                        (Screen.height / 2) - 40 - Random.value * (Screen.height - 80));
                s.name = "star" + x;
                s.depth = 100 + x; //星星的深度设置  不同的深度才能显示层次感
                s.transform.parent = stars.transform;
            }
        }
    }

可以看到其实初始化也没做什么事,判断部分都是由碰撞器去做了


好吧,下面看看CBlock2 是如何附加碰撞器上去的

public class CBlock2 : MonoBehaviour {
    OTSprite sprite;                // This block's sprite class
    bool colorFade = false;         //是否渐变
    float fadeTime = 0;             // 渐变的计时器
    float fadeSpeed = 0.25f;         // 渐变速度
    
    Color startColor =
        new Color(0.3f, 0.3f, 0.3f); // 砖块颜色

    void Start () {
        sprite = GetComponent<OTSprite>();
        // Set this sprite's collision delegate
        // HINT : We could use sprite.InitCallBacks(this) as well.
        // but because delegates are the C# way we will use this technique


        sprite.onCollision = OnCollision;
        sprite.tintColor = startColor;


        // Register this material with Orthello so we can re-use it later
        OT.RegisterMaterial("Block-start", new Material(sprite.material));
      
        for (int i=0; i<10; i++)    // Generate Highlight materials and store them
        {
            var m = new Material(sprite.material);
            m.SetColor("_EmisColor", Color.Lerp(Color.white,startColor,0.1f * i));
            OT.RegisterMaterial("Block-tint"+i, m);            
        }
    }

    void Update()
    {
        if (colorFade)
        {
            int    fadeIndex  =  (int)(Mathf.Floor((fadeTime / fadeSpeed) * 10));
            sprite.material = OT.LookupMaterial("Block-tint" + fadeIndex);

            fadeTime += Time.deltaTime;
            if (fadeTime >= fadeSpeed)
            {
                colorFade = false;

                if (OT.LookupMaterial("Block-start") == null)
                    print("Block-start material not found!");

                sprite.material = OT.LookupMaterial("Block-start");  // Set block's color to start color
            }
        }
    }

    public void OnCollision(OTObject owner)
    {
        colorFade = true;          // 碰撞后调用
        fadeTime = 0;           //再次设碰撞时间
    }
}

接着看最后的星星的部分

public class CStar2 : MonoBehaviour {

    OTSprite sprite;                    // 链接的sprite
    Vector2 speed =   new Vector2(150, 150);        // 每秒移动的速度
    float rotation = 90;                // 每秒转动的角度

    Color startColor =  new Color(0.5f, 1f, 0.5f);    //  开始的颜色   一开始是绿色的
    Color stayColor =   new Color(1f, 1f, 1f);    // 重叠时的颜色    两个星星重叠后颜色就变黑

    void Start () {
        sprite = GetComponent<OTSprite>();
        // Set this sprite's stay/exit/collision delegates
        sprite.onStay = OnStay;
        sprite.onExit = OnExit;
        sprite.onCollision = OnCollision;

      
        //给一个初始的随机速度
        speed = new Vector2(150 + 150 * Random.value, 150 + 150 * Random.value);
        sprite.tintColor = startColor;

        // 注册星星的材质以便后面使用到它
        OT.RegisterMaterial("Star-start", new Material(sprite.material));  //创建一个开始的材质材质
        var m = new Material(sprite.material);
        m.SetColor("_EmisColor", stayColor);
        OT.RegisterMaterial("Star-stay", m);// 创建一个静态的材质
    }
   
    void Update () {
        sprite.position += speed * Time.deltaTime;  // 注意这里还是乘以  Time.deltaTime  表示每秒多少个
        sprite.rotation  += (rotation * Time.deltaTime);
    }

    // OnStay delegate当这个星星撞到另一个星星时调用
    // !IMPORTANT - This sprite's collidable setting has to be true otherwide
    // collision delegates will not be called
    public void OnStay(OTObject owner)
    {
        //检查我们是否碰到另一个星星  并且 调整颜色如果我们没了
        if (owner.collisionObject.name.IndexOf("star") == 0)
            sprite.material = OT.LookupMaterial("Star-stay");  // 关键步骤  lookupMaterial
    }


    // OnExit delegate 离开星星时调用
    public void OnExit(OTObject owner)
    {
        if (owner.collisionObject.name.IndexOf("star") == 0)
            sprite.material = OT.LookupMaterial("Star-start");
    }


    // OnCollision 委托是碰到了边界时候的处理方法
    // HINT - OnEnter and OnCollision delegates 确实同时发生, 唯一不同的是命名不同
    public void OnCollision(OTObject owner)
    {
        //检查是否碰到上边界
        if (owner.collisionObject.name.IndexOf("top") == 0 && speed.y > 0)
        {
            speed = new Vector2(speed.x, speed.y * -1);
            if ((speed.x < 0 && rotation > 0) || (speed.x > 0 && rotation < 0))
                rotation *= -1;
        }
        else
            //检查是否碰到下边界
            if (owner.collisionObject.name.IndexOf("bottom") == 0 && speed.y < 0)
            {
                speed = new Vector2(speed.x, speed.y * -1);
                if ((speed.x < 0 && rotation < 0) || (speed.x > 0 && rotation > 0))
                    rotation *= -1;
            }
            else
                //检查是否碰到左边界
                if (owner.collisionObject.name.IndexOf("left") == 0 && speed.x < 0)
                {
                    speed = new Vector2(speed.x * -1, speed.y);
                    if ((speed.y < 0 && rotation > 0) || (speed.y > 0 && rotation < 0))
                        rotation *= -1;
                }
                else
                    //检查是否碰到右边界
                    if (owner.collisionObject.name.IndexOf("right") == 0 && speed.x > 0)
                    {
                        speed = new Vector2(speed.x * -1, speed.y);
                        if ((speed.y < 0 && rotation < 0) || (speed.y > 0 && rotation > 0))
                            rotation *= -1;
                    }
    }

}



原创粉丝点击