3D游戏HW6

3D游戏HW6

改进飞碟(Hit UFO)游戏:

游戏内容要求:
按 adapter模式 设计图修改飞碟游戏,使它同时支持物理运动与运动学(变换)运动。

1. 按 adapter模式 设计图修改飞碟游戏

UFOAction.cs中的接口实现了UFO动作的适配器接口。
这个接口再由UFOActiondo和UFOActionKin分别实现动力学和运动学运动。

//UFOAction.cs
public interface UFOAction 
{
    // Start is called before the first frame update
    void Start();
    void SetSpeed(int speed);
    void SetIfRun(bool signal);
    GameObject GetPlayer();
    void SetPlayer(GameObject g);
    void Update();
}

UFOActionKin与UFOActiondo主要区别在Start()和Update()的改变。

//UFOActionKin.cs
public class UFOActionKin : ScriptableObject,UFOAction
{
    public Director director;
    public GameObject player;
    Vector3 start;
    Vector3 end;
    public int speed = 3;
    public bool ifRun = true;
    public void SetSpeed(int speed)
    {
        this.speed = speed;
    }
    public void SetIfRun(bool signal)
    {
        this.ifRun = signal;
    }
    public GameObject GetPlayer()
    {
        return this.player;
    }
    public void SetPlayer(GameObject g)
    {
        this.player = g;
    }
    public void Start()
    {
        director = Director.GetInstance();
        start = new Vector3(Random.Range(-20f, 20f), Random.Range(-20f, 20f), 0);
        if (start.x < 10 && start.x > -10)
            start.x *= 4;
        if (start.y < 10 && start.y > -10)
            start.y *= 4;
        end = new Vector3(-start.x, -start.y, 0);
        player.transform.position = start;
        SetColor();
        Rigidbody rigid = player.GetComponent<Rigidbody>();
        if(rigid != null)
        {
            Destroy(rigid);
        }
    }

    public void Update()
    {
        if (ifRun)
        {
            player.transform.position = Vector3.MoveTowards(player.transform.position,
                end, speed * Time.deltaTime);
            if (player.transform.position == end)
            {
                this.director.currentController.ufofactory.Not_hit(this.player);
            }
        }
    }

    public void SetColor()
    {
        int color = Random.Range(1, 4);
        switch (color)
        {
            case 1:
                player.GetComponent<MeshRenderer>().material.color = Color.red;
                foreach (Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
                }
                break;
            case 2:
                player.GetComponent<MeshRenderer>().material.color = Color.green;
                foreach (Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
                }
                break;
            case 3:
                player.GetComponent<MeshRenderer>().material.color = Color.grey;
                foreach (Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
                }
                break;
            default:
                break;
        }
    }
}
UFOActiondo.cs
public class UFOActiondo : ScriptableObject, UFOAction
{
    public Director director;
    public GameObject player;
    Vector3 start;
    Vector3 end;
    public int speed = 3;
    public bool ifRun = true;
    int framecount = 0;
    public void SetSpeed(int speed)
    {
        this.speed = speed;
    }
    public void SetIfRun(bool signal)
    {
        this.ifRun = signal;
    }
    public GameObject GetPlayer()
    {
        return this.player;

    }
    public void SetPlayer(GameObject g)
    {
        this.player = g;
    }
    // Start is called before the first frame update
    public void Start()
    {
        director = Director.GetInstance();
        if (player.GetComponent<Rigidbody>() == null)
            player.AddComponent<Rigidbody>();
        start = new Vector3(Random.Range(-20f, 20f), Random.Range(-20f, 20f), 0);
        if (start.x < 10 && start.x > -10)
            start.x *= 4;
        if (start.y < 10 && start.y > -10)
            start.y *= 4;
        end = new Vector3(0, 0, 20);
        player.transform.position = start;
        SetColor();
        Rigidbody rigid = player.GetComponent<Rigidbody>();
        rigid.velocity = (end - start) * speed * Random.Range(0.0f, 0.1f);
        rigid.useGravity = false;
    }

    // Update is called once per frame
    public void Update()
    {
        framecount++;
        if (framecount > 300)
            this.director.currentController.ufofactory.Not_hit(this.player);
        Rigidbody rigid = player.GetComponent<Rigidbody>();
        if (ifRun == false)
        {
            rigid.velocity = Vector3.zero;
            framecount = 0;
        }
        if (player.transform.position.x < -100 || player.transform.position.x > 100)
        {
            rigid.velocity = Vector3.zero;
            this.director.currentController.ufofactory.Not_hit(this.player);
        }
    }

    public void SetColor()
    {
        int color = Random.Range(1, 4);
        switch (color)
        {
            case 1:
                player.GetComponent<MeshRenderer>().material.color = Color.red;
                foreach(Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.red;
                }
                break;
            case 2:
                player.GetComponent<MeshRenderer>().material.color = Color.green;
                foreach (Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
                }
                break;
            case 3:
                player.GetComponent<MeshRenderer>().material.color = Color.grey;
                foreach (Transform child in player.transform)
                {
                    child.gameObject.GetComponent<MeshRenderer>().material.color = Color.grey;
                }
                break;
            default:
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值