鼠标打飞碟(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>();
}
}
}
成品图: