与游戏世界交互作业

一、编写一个简单的鼠标打飞碟(Hit UFO)游戏

  • 游戏内容要求:
    1. 游戏有 n 个 round,每个 round 都包括10 次 trial;
    2. 每个 trial 的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由该 round 的 ruler 控制;
    3. 每个 trial 的飞碟有随机性,总体难度随 round 上升;
    4. 鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定。
  • 游戏的要求:
    • 使用带缓存的工厂模式管理不同飞碟的生产与回收,该工厂必须是场景单实例的!具体实现见参考资源 Singleton 模板类
    • 近可能使用前面 MVC 结构实现人机交互与游戏模型分离

 

首先,我们看到游戏的第一个要求是要我们使用一个带缓存的工厂模式管理不同飞碟的生产与回收,要实现这一目的,我们需要添加一个飞碟工厂。通过实现这个飞碟工厂,我们能够有效地利用已经构造好的游戏对象实现资源的重利用,以减少资源开销。具体实现如下:

我们再工厂中添加一个usde列表和free列表,分别表示已使用和未使用的飞碟对象。当free列表为空时,我们就需要创建新的对象。然后,我们对飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数等变量进行设置,并将获得的飞碟加入使用队列,回收的飞碟加入初始化队列。

using System.Collections.Generic;
using UnityEngine;

public class DiskFactory : MonoBehaviour {
    public List<GameObject> used = new List<GameObject>();
    public List<GameObject> free = new List<GameObject>();

    // Use this for initialization
    void Start () { }

    public void GenDisk()
    {
        GameObject disk;
        if(free.Count == 0)
        {
            disk = Instantiate<GameObject>(Resources.Load<GameObject>("Prefabs/Disk"), Vector3.zero, Quaternion.identity);
        }
        else
        {
            disk = free[0];
            free.RemoveAt(0);
        }
        float x = Random.Range(-10.0f, 10.0f);
        disk.transform.position = new Vector3(x, 0, 0);
        disk.transform.Rotate(new Vector3(x < 0? -x*9 : x*9, 0, 0));
        float r = Random.Range(0f, 1f);
        float g = Random.Range(0f, 1f);
        float b = Random.Range(0f, 1f);
        Color color = new Color(r, g, b);
        disk.transform.GetComponent<Renderer>().material.color = color;
        used.Add(disk);
    }
    public void RecycleDisk(GameObject obj)
    {
        obj.transform.position = Vector3.zero;
        free.Add(obj);
    }
}

接下来是场景控制器。场景控制器由游戏对象生成,使用Singleton为每一个MonoBehavior子类创建一个对象实例,并通过调用动作管理器,实现每一个对象的相应动作,如飞盘的飞行、爆炸等。

public class FirstSceneController : MonoBehaviour, IUserAction, ISceneController{
    public CCActionManager actionManager;
    public GameObject disk;
    protected DiskFactory df;
    public int flag = 0;
    private float interval = 3;
    public int score = 0;
    public static int times = 0;

    private void Awake()
    {
        SSDirector director = SSDirector.getInstance();
        director.setFPS(60);
        director.currentSceneController = this;
        this.gameObject.AddComponent<DiskFactory>();
        this.gameObject.AddComponent<CCActionManager>();
        this.gameObject.AddComponent<UserGUI>();
        df = Singleton<DiskFactory>.Instance;
        //director.currentSceneController.GenGameObjects();
    }
    private void Start()
    {
    }
    public void GenGameObjects ()
    {
    }
    public void Restart()
    {
        SceneManager.LoadScene("1");
    }
    public void Pause ()
    {
        actionManager.Pause();
    }
    public void Update()
    {
        if (times < 30 && flag == 0)
        {
            if (interval <= 0)
            {
                interval = Random.Range(3, 5);
                times++;
                df.GenDisk();
            }
            interval -= Time.deltaTime;
        }
    }
}

那么,被场景控制器调用的动作管理器是如何实现的呢?动作管理器这里就是设置一个动作列表,从而可以实现对多个飞碟的同时的飞行的控制。然后,我们调用userClickAction,可以实现通过鼠标点击控制对象的行为,实现飞碟一被点击就会爆炸的效果。

public class CCActionManager : SSActionManager, ISSActionCallback {
    public FirstSceneController sceneController;
    public List<CCMoveToAction> seq = new List<CCMoveToAction>();
    public UserClickAction userClickAction;
    public DiskFactory disks;
    
    protected new void Start()
    {
        sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
        sceneController.actionManager = this;
        disks = Singleton<DiskFactory>.Instance;
    }
    protected new void Update()
    {
        if(disks.used.Count > 0)
        {
            GameObject disk = disks.used[0];
            float x = Random.Range(-10, 10);
            CCMoveToAction moveToAction = CCMoveToAction.GetSSAction(new Vector3(x, 12, 0), 3 * (Mathf.CeilToInt(FirstSceneController.times / 10) + 1) * Time.deltaTime);
            seq.Add(moveToAction);
            this.RunAction(disk, moveToAction, this);
            disks.used.RemoveAt(0);
        }
        if (Input.GetMouseButtonDown(0) && sceneController.flag == 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitGameObject;
            if (Physics.Raycast(ray, out hitGameObject))
            {
                GameObject gameObject = hitGameObject.collider.gameObject;
                if (gameObject.tag == "disk")
                {
                    foreach(var k in seq)
                    {
                        if (k.gameObject == gameObject)
                            k.transform.position = k.target;
                    }
                    userClickAction = UserClickAction.GetSSAction();
                    this.RunAction(gameObject, userClickAction, this);
                }/*
                else if (gameObject.transform.parent.name == "boat" && sceneController.boatCapacity < 2 && (moveToAction == null || !moveToAction.enable))
                {
                    moveToAction = CCMoveToAction.GetSSAction(-gameObject.transform.parent.transform.position, 10*Time.deltaTime);
                    this.RunAction(gameObject.transform.parent.gameObject, moveToAction, this);
                }*/
            }
        }
        base.Update();
    }
    public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
        disks.RecycleDisk(source.gameObject);
        seq.Remove(source as CCMoveToAction);
        source.destory = true;
        if (FirstSceneController.times >= 30)
            sceneController.flag = 1;
    }
    public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
    }
    public void Pause()
    {
        if(sceneController.flag == 0)
        {
            foreach (var k in seq)
            {
                k.enable = false;
            }
            sceneController.flag = 2;
        }
        else if(sceneController.flag == 2)
        {
            foreach (var k in seq)
            {
                k.enable = true;
            }
            sceneController.flag = 0;
        }
    }
}

最后是UserGUI的实现,每当用户成功点击一个飞碟使其爆炸,在分数一栏其得分就加一;每当用户漏点一个飞碟,在生命一栏其生命就减一。点击start开始游戏,若游戏失败,点击restart可重新开始游戏。

if (game_start)
        {
            //用户射击
            if (Input.GetButtonDown("Fire1"))
            {
                Vector3 pos = Input.mousePosition;
                action.Hit(pos);
            }

            GUI.Label(new Rect(5, 5, 200, 50), "Score:", text_style);
            GUI.Label(new Rect(100, 5, 200, 50), action.GetScore().ToString(), score_style);

            GUI.Label(new Rect(Screen.width - 280, 5, 50, 50), "Life:", text_style);
            //显示当前血量
            for (int i = 0; i < life; i++)
            {
                GUI.Label(new Rect(Screen.width - 200 + 30 * i, 5, 50, 50), "★", bold_style);
            }
            for (int i = life; i < 6; i++)
            {
                GUI.Label(new Rect(Screen.width - 200 + 30 * i, 5, 50, 50), "★", wait_style);
            }

            if (action.GetCoolTimes() >= 0 && action.GetRound() == 1) {
                        //    GUI.Label(new Rect(Screen.width / 2 - 100, 60, 200, 50), "", wait_style);
                    if (action.GetCoolTimes() == 0) {
                        GUI.Label(new Rect(Screen.width / 2 - 200, 150, 200, 50), "ROUND 1", time_style);
                    }else
                        GUI.Label(new Rect(Screen.width / 2 - 30, 150, 200, 50), action.GetCoolTimes().ToString(), time_style);


            }
            if (action.GetRound() == 2 && action.GetCoolTimes2() > 0) {
                    GUI.Label(new Rect(Screen.width / 2 - 200, 150, 200, 50), "ROUND 2", time_style);

            }
            if (action.GetRound() == 3 && action.GetCoolTimes3() > 0) {
                    GUI.Label(new Rect(Screen.width / 2 - 200, 150, 200, 50), "ROUND 3", time_style);

            }


            //游戏结束
            if (life == 0)
            {
                high_score = high_score > action.GetScore() ? high_score : action.GetScore();
                GUI.Label(new Rect(Screen.width / 2 - 100, 110, 100, 100), "Game Over!", over_style);
                GUI.Label(new Rect(Screen.width / 2 - 80, 200, 50, 50), "Highest Score:", text_style);
                GUI.Label(new Rect(Screen.width / 2 + 50, 200, 50, 50), high_score.ToString(), text_style);

                if (GUI.Button(new Rect(Screen.width / 2 - 60, 300, 100, 50), "Restart"))
                {
                    life = 6;
                    action.ReStart();
                    return;
                }
                action.GameOver();
            }
        }
        else
        {
            GUI.Label(new Rect(Screen.width / 2 - 110, 110, 100, 100), "鼠标打飞碟", over_style);
       
            if (GUI.Button(new Rect(Screen.width / 2 - 60, 200, 100, 50), "Start"))
            {
                game_start = true;
                
                action.BeginGame();
            }
        }
    }
    public void ReduceBlood()
    {
        if(life > 0)
            life--;
    }

游戏场景如下:

 

二 、编写一个简单地自定义Component

我们可以编写一个旋转脚本,使得UFO可以进行转动。在旋转脚本中,我们设计不同的速度与转轴,并实现预制:

public class Rotate : MonoBehaviour
{

    public float v;
    public float ang1, ang2;

    void Update()
    {
        Vector3 axis = new Vector3(0, ang1, ang2);
        this.transform.RotateAround(new Vector3(0, 0, 0), axis, v * Time.deltaTime);
        this.transform.Rotate(Vector3.up * 100 * Time.deltaTime);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值