ch05与游戏世界交互——鼠标打飞碟小游戏

本文介绍了一个使用Unity3D开发的鼠标打飞碟小游戏,游戏包含多个round,每个round有不同的trial,飞碟具有随机性。通过工厂模式管理和回收飞碟,运用MVC结构实现交互与模型分离。游戏设计包括UML图,代码结构以导演、飞碟数据、飞碟工厂、判断逻辑、场景控制器等组件为主,详细说明了游戏对象的创建、脚本挂载和交互方式。
摘要由CSDN通过智能技术生成

游戏内容要求:

  • 游戏有 n 个 round,每个 round 都包括10 次 trial。

  • 每个 trial 的飞碟的色彩、大小、发射位置、速度、角度、同时出现的个数都可能不同。它们由该 round 的 ruler 控制。

  • 每个 trial 的飞碟有随机性,总体难度随 round 上升。

  • 鼠标点中得分,得分规则按色彩、大小、速度不同计算,规则可自由设定


游戏的要求:

  • 使用带缓存的工厂模式管理不同飞碟的生产与回收,该工厂必须是场景单实例的!具体实现见参考资源 Singleton 模板类。
  • 近可能使用前面 MVC 结构实现人机交互与游戏模型分离。

游戏效果图
在这里插入图片描述


详细介绍:

一、UML图

  • 结构上参考了课件的描述,采用工厂+单实例+对象池的方法,结合前面学习的MVC方法,将人机交互和游戏模型分离

在这里插入图片描述

  • 代码结构:

在这里插入图片描述


Director: 常规的Director,一个游戏只有一个


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



public class Director : System.Object
{
    //导演,控制场景切换
    private static Director _instance;
    public SceneController currentSceneController { get; set; }

    public bool running { get; set; }

    public static Director getInstance()
    {
        if (_instance == null)
        {
            _instance = new Director();
        }
        return _instance;
    }

    public int getFPS()
    {
        return Application.targetFrameRate;
    }
    //让游戏以指定的帧率执行
    public void setFPS(int fps)
    {
        Application.targetFrameRate = fps;
    }

    public void NextScene()
    {

    }
}

DiskData: 给飞盘添加属性


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



public class Attributes
{
    //飞盘大小
    public float size;
    //飞盘颜色
    public Color color;
    //发射飞盘的位置
    public Vector3 position;
    //飞盘的发射方向
    public Vector3 direction;
    //飞盘速度
    public float speed;
    //飞碟材料
    public Material material;

    //打击飞盘的分数
    public int score;

    //构造函数
    public Attributes(float size, Color color, Vector3 position, Vector3 direction, float speed, Material material)
    {
        this.size = size;
        this.color = color;
        this.material = material;
        this.position = position;
        this.direction = direction;
        this.speed = speed;
        this.score = ((int)(this.speed + 10 - this.size));
    }
}



public class DiskData : MonoBehaviour
{

    Attributes _attributes;

    public Attributes attributes
    {
        get
        {
            return _attributes;
        }
        set
        {
            _attributes = value;
            this.gameObject.GetComponent<Transform>().position = value.position;
            this.gameObject.GetComponent<Transform>().localScale = new Vector3(1, 1, 1) * value.size;
            //this.gameObject.transform.GetChild(0).gameObject.GetComponent<Renderer>().material.color = value.color;
            //this.gameObject.GetComponent<Renderer>().material = value.material;
            this.GetComponentsInChildren<Renderer>()[0].materials[0] = value.material;

            Transform[] father = this.gameObject.GetComponentsInChildren<Transform>();

            foreach (var child in father)
            {
                child.GetComponent<Renderer>().material = value.material;
            }


            this.gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().material = value.material;
            Debug.Log("this.gameObject.GetComponentsInChildren<Renderer>()[0].materials[0]: " + value.material.name + this.gameObject.transform.GetChild(0).GetComponent<MeshRenderer>().material.name);
            //Debug.Log("this.gameObject.GetComponent<Renderer>().
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值