鼠标打飞碟(Hit UFO)游戏改进版

本文详细介绍了一款基于Unity的打飞碟游戏设计与实现,采用Adapter模式支持物理与运动学运动,通过工厂模式创建不同类型的飞碟,利用场景单例进行游戏管理,实现了鼠标拾取多个物体的功能。
部署运行你感兴趣的模型镜像

鼠标打飞碟(Hit UFO)游戏改进版

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

跟之前的简单打飞碟相比,这次的多了个Adapter类和plane,plane是用来体现飞碟的物理运动的。
在这里插入图片描述

动作管理器统一接口

public interface SceneController
{
    void load();
    void game(int Round);
    int Choose(float[] probe);
}

飞碟随机运动

using UnityEngine;
using System.Collections;

public class move1 : MonoBehaviour {
    public float speedX = 5.0F;
    public float speedY = 5.0F;
    public float speedZ = 5.0F;
    // Use this for initialization
    float rand1 = Random.Range(-1.0f, 1.0f);
    float rand2 = Random.Range(-1.0f, 1.0f);
    float rand3 = Random.Range(-1.0f, 1.0f);

    float randx;
    float randy;
    


    void Start () {
        randx = Random.Range(-1.0f, 1.0f);
        randy = Random.Range(-1.0f, 1.0f);
        //Debug.Log(randx);
        //Debug.Log(randy);
        if (randx>0)
            randx = 1.0f;
        else
            randx = -1.0f;

        if (randy >0)
            randy = 1.0f;
        else
            randy = -1.0f;
    }
	
	// Update is called once per frame
	void Update () {
        
        
        float translationY = 1 * speedY*rand1*randx;
        
        float translationX = 1 * speedX*rand2*randy;
        
        float translationZ = 1 * speedZ;
        translationY *= Time.deltaTime;
        translationX *= Time.deltaTime;
        //transform.Translate(0, translationY, 0);
        //transform.Translate(translationX, 0, 0);
        transform.Translate(translationX, translationY, translationZ);
        
    }
    

}

工厂

using UnityEngine;
using Adapter;

namespace SimpleFactory
{
    #region 产品

    public interface UFO
    {
        GameObject Version(int round);
    }
    
    public class UFO1 : UFO
    {
        
        float x = Random.Range(0.0f, 5.0f);
        public virtual GameObject Version(int round)
        {
            GameObject UFO1;
            Vector3 UFO1Pos = new Vector3(x, 0, 0);
            UFO1 = Object.Instantiate(Resources.Load("UFO1", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;
            UFO1.name = "UFO1";
            UFO1.AddComponent<PickupObject>();
            //UFO1.AddComponent<Rigidbody>();
            move1 test = UFO1.AddComponent<move1>();
            test.speedX *=  (float)round;
            test.speedY *=   (float)round;
            test.speedZ = 0.0f ;
            return UFO1;
        }
     
    }
    
    public class UFO2 : UFO
    {
        float x = Random.Range(0.0f, 5.0f);
        public virtual GameObject Version(int round)
        {
            GameObject UFO1;
            Vector3 UFO1Pos = new Vector3(x, 0, 0);
            UFO1 = Object.Instantiate(Resources.Load("UFO2", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;
            UFO1.name = "UFO2";
            UFO1.AddComponent<PickupObject>();
            //UFO1.AddComponent<Rigidbody>();
            move1 test = UFO1.AddComponent<move1>();
            test.speedX *= (float)round;
            test.speedY *= (float)round;
            test.speedZ = 0.0f;
            return UFO1;
     
        }
        public virtual void Add_Component_Rigidbody(GameObject ufo)
        {
            ufo.AddComponent<Rigidbody>();
        }
    }

    public class UFO3 : UFO
    {
        float x = Random.Range(0.0f, 5.0f);
        public virtual GameObject Version(int round)
        {
            GameObject UFO1;
            Vector3 UFO1Pos = new Vector3(x, 0, 0);
            UFO1 = Object.Instantiate(Resources.Load("UFO3", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;
            UFO1.name = "UFO3";
            UFO1.AddComponent<PickupObject>();
            //UFO1.AddComponent<Rigidbody>();
            move1 test = UFO1.AddComponent<move1>();
            test.speedX *= (float)round;
            test.speedY *=  (float)round;
            test.speedZ = 0.0f;
            return UFO1;

        }
    }

    public class UFO4 : UFO
    {
        float x = Random.Range(0.0f, 5.0f);
        public virtual GameObject Version(int round)
        {
            GameObject UFO1;
            Vector3 UFO1Pos = new Vector3(x, 0, 0);
            UFO1 = Object.Instantiate(Resources.Load("UFO4", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;
            UFO1.name = "UFO4";
            UFO1.AddComponent<PickupObject>();
            //UFO1.AddComponent<Rigidbody>();
            move1 test = UFO1.AddComponent<move1>();
            test.speedX *= (float)round;
            test.speedY *= (float)round;
            test.speedZ = 0.0f;
            return UFO1;
     
        }
    }


    public class UFO5 : UFO
    {
        float x = Random.Range(0.0f, 5.0f);
        public virtual GameObject Version(int round)
        {
            GameObject UFO1;
            Vector3 UFO1Pos = new Vector3(x, 0, 0);
            UFO1 = Object.Instantiate(Resources.Load("UFO5", typeof(GameObject)), UFO1Pos, Quaternion.identity) as GameObject;
            UFO1.name = "UFO5";
            UFO1.AddComponent<PickupObject>();
            //UFO1.AddComponent<Rigidbody>();
            move1 test = UFO1.AddComponent<move1>();
            test.speedX *= (float)round;
            test.speedY *= (float)round;
            test.speedZ = 0.0f;
            return UFO1;

        }
    }
    #endregion

    #region 工厂
    public interface IFactory
    {
        UFO Create();
    }

    public class UFO1Factory : IFactory
    {
        public virtual UFO Create()
        {
            
            return new UFO1();
        }
    }

    public class UFO2Factory : IFactory
    {
        public virtual UFO Create()
        {
            return new UFO2();
        }
    }

    public class UFO3Factory : IFactory
    {
        public virtual UFO Create()
        {
            
            return new UFO3();
        }
    }

    public class UFO4Factory : IFactory
    {
        public virtual UFO Create()
        {
            return new UFO4();
        }
    }

    public class UFO5Factory : IFactory
    {
        public virtual UFO Create()
        {
            return new UFO5();
        }
    }

    #endregion
}
}

场景单实例

using UnityEngine;
using System.Collections;

public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
    protected static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = (T)FindObjectOfType(typeof(T));
                if (instance == null)
                {
                    Debug.LogError("An instance of " + typeof(T) +
                    " is needed in the scene, but there is none.");
                }
            }
            return instance;
        }
    }
}

场景单实例的使用很简单,你仅需要将 MonoBehaviour 子类对象挂载任何一个游戏对象上即可。

然后在任意位置使用代码 Singleton <>.Instance 获得该对象。

光标拾取多个物体程序

public class PickupMultiObjects : MonoBehaviour {

	public GameObject cam;

	// Update is called once per frame
	void Update () {
		if (Input.GetButtonDown("Fire1")) {
			Debug.Log ("Fired Pressed");
			Debug.Log (Input.mousePosition);

			Vector3 mp = Input.mousePosition; //get Screen Position

			//create ray, origin is camera, and direction to mousepoint
			Camera ca;
			if (cam != null ) ca = cam.GetComponent<Camera> (); 
			else ca = Camera.main;

			Ray ray = ca.ScreenPointToRay(Input.mousePosition);

			//Return the ray's hits
			RaycastHit[] hits = Physics.RaycastAll (ray);

			foreach (RaycastHit hit in hits) {
				print (hit.transform.gameObject.name);
				if (hit.collider.gameObject.tag.Contains("Finish")) { //plane tag
					Debug.Log ("hit " + hit.collider.gameObject.name +"!" ); 
				}
				Destroy (hit.transform.gameObject);
			}
		}		
	}
}

Adater类():

using UnityEngine;
using System.Collections;
using SimpleFactory;

namespace Adapter
{
    public class adapter
    {
        public void Add_Component_Rigidbody(SimpleFactory.UFO ufo,int i)
        {
            ufo.Version(i).AddComponent<Rigidbody>();

        }
    }
}

成品图:
在这里插入图片描述
在这里插入图片描述

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

【EI复现】基于深度强化学习的微能源网能量管理与优化策略研究(Python代码实现)内容概要:本文围绕“基于深度强化学习的微能源网能量管理与优化策略”展开研究,重点利用深度Q网络(DQN)等深度强化学习算法对微能源网中的能量调度进行建模与优化,旨在应对可再生能源出力波动、负荷变化及运行成本等问题。文中结合Python代码实现,构建了包含光伏、储能、负荷等元素的微能源网模型,通过强化学习智能体动态决策能量分配策略,实现经济性、稳定性和能效的多重优化目标,并可能与其他优化算法进行对比分析以验证有效性。研究属于电力系统与人工智能交叉领域,具有较强的工程应用背景和学术参考价值。; 适合人群:具备一定Python编程基础和机器学习基础知识,从事电力系统、能源互联网、智能优化等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习如何将深度强化学习应用于微能源网的能量管理;②掌握DQN等算法在实际能源系统调度中的建模与实现方法;③为相关课题研究或项目开发提供代码参考和技术思路。; 阅读建议:建议读者结合提供的Python代码进行实践操作,理解环境建模、状态空间、动作空间及奖励函数的设计逻辑,同时可扩展学习其他强化学习算法在能源系统中的应用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值