飞碟回收工厂类 DiskFactory.cs: 这个类主要是用来实现飞碟的资源管理,如生成飞碟、自动回收飞碟等,使用 List 数组存储飞碟对象,方便管理。具体实现如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiskFactory : System.Object {
private List<GameObject> used = new List<GameObject>();
private List<GameObject> free = new List<GameObject>();
private List<Color> c = new List<Color>();
private int count =0;
private int flag;
private static DiskFactory _instance;
public voidGetDisk(int ruler){
GameObject disk;if(free.Count ==0){
disk = GameObject.Instantiate(Resources.Load<GameObject>("prefabs/disk"));
disk.name ="disk"+ count;
count++;
used.Add(disk);}else{
disk = free[0];
disk.SetActive(true);
used.Add(disk);
free.RemoveAt(0);}
flag = UnityEngine.Random.Range(0,5);
disk.GetComponent<Renderer>().material.color = c[(ruler + flag)%9];
disk.transform.localScale =getSize(ruler);
disk.transform.position =getPosition(ruler + flag);
DiskData data = disk.GetComponent(typeof(DiskData)) as DiskData;
data.speed = ruler +1;
data.target =getTarget(ruler + flag);}
public intGetCount(){return used.Count;}
private Vector3 getTarget(int ruler){if(ruler %2==0){return new Vector3(100,10+ ruler,60);}return new Vector3(-100,10+ruler,60);}
private Vector3 getPosition(int ruler){if(ruler %2==0){return new Vector3(-10,0,-6);}else{return new Vector3(10,0,-6);}}
private Vector3 getSize(int r){if(r ==0){return new Vector3(5,0.1f,5);}elseif(r ==1){return new Vector3(4,0.1f,4);}elseif(r ==2){return new Vector3(3,0.1f,3);}elseif(r ==3){return new Vector3(2,0.1f,2);}else{return new Vector3(1,0.1f,1);}}
public static DiskFactory GetInstance(){if(_instance == null){
_instance = new DiskFactory();
_instance.c.Add(Color.black);
_instance.c.Add(Color.blue);
_instance.c.Add(Color.cyan);
_instance.c.Add(Color.gray);
_instance.c.Add(Color.green);
_instance.c.Add(Color.magenta);
_instance.c.Add(Color.red);
_instance.c.Add(Color.white);
_instance.c.Add(Color.yellow);}return _instance;}
public voidFreeDisk(GameObject disk){
disk.SetActive(false);
free.Add(disk);
used.Remove(disk);}// Use this for initializationvoid Start (){}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiskData : MonoBehaviour {
public Vector3 target;
public float speed;
public diskAction action;// Use this for initializationvoid Start (){
action = SSDirector.getInstance().currentSceneController as diskAction;}// Update is called once per framevoid 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 = Camera.main;//cam.GetComponent<Camera> ();
Ray ray = ca.ScreenPointToRay(Input.mousePosition);//Return the ray's hit
RaycastHit hit;if(Physics.Raycast(ray, out hit)){print(hit.transform.gameObject.name);if(hit.collider.gameObject.tag.Contains("Finish")){//plane tag
Debug.Log("hit "+ hit.collider.gameObject.name +"!");}//消失
action.Boomshakalaka(this.gameObject);}}
this.transform.position = Vector3.MoveTowards(this.transform.position, target, speed * Time.deltaTime *2);if(this.transform.position == target){
action.Drop(this.gameObject);}}}
回合控制类 RoundController.cs: 这个类主要负责飞碟回合的控制。具体实现如下:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RoundController : MonoBehaviour, IsceneController,diskAction {
public ScoreRecorder scoreRecorder = new ScoreRecorder();
private int trial =1;
private int round =0;
bool game_over = false;
bool next_round = false;
private DiskFactory df = DiskFactory.GetInstance();
private int score;voidAwake(){
SSDirector director = SSDirector.getInstance();
director.setFPS(60);
director.currentSceneController = this;
director.currentSceneController.LoadResources();}
public voidLoadResources(){
df.GetDisk(round);}
public voidBoomshakalaka(GameObject disk){
df.FreeDisk(disk);if(df.GetCount()==0){
next_round = true;
scoreRecorder.AddScore(round +1);
trial =1;}}
public voidDrop(GameObject disk){
trial++;if(trial >10){
game_over = true;
df.FreeDisk(disk);}else{
df.FreeDisk(disk);
df.GetDisk(round);}}// Use this for initializationvoid Start (){}// Update is called once per framevoid Update (){
score = scoreRecorder.getScore();}voidOnGUI(){
GUI.Box(new Rect(Screen.width /2-75,10,150,55),"Round "+(round +1)+"\nYour score: "+ score +"\nYour trial left: "+(11-trial));if(next_round){
GUI.Window(0, new Rect(Screen.width /2- Screen.width /12, Screen.height /2- Screen.height /12, Screen.width /6, Screen.height /6), next_round_window,"Success !");}if(game_over){
GUI.Window(0, new Rect(Screen.width /2- Screen.width /12, Screen.height /2- Screen.height /12, Screen.width /6, Screen.height /6), game_over_window,"Game Orver!");}}voidnext_round_window(int id){if(GUI.Button(new Rect(Screen.width /24, Screen.height /24+5, Screen.width /12, Screen.height /12),"Next")){NextRound();}}
private voidNextRound(){
round++;
next_round = false;
df.GetDisk(round);}voidgame_over_window(int id){if(GUI.Button(new Rect(Screen.width /24, Screen.height /24+5, Screen.width /12, Screen.height /12),"Restart")){
Debug.Log("Restart");Restart();}}
private voidRestart(){
trial =1;
round =0;
game_over = false;
next_round = false;
scoreRecorder.Reset();
df.GetDisk(round);}}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScoreRecorder : System.Object
{
private int score =0;
public voidAddScore(int s){
score = score + s;}
public intgetScore(){return score;}
public voidReset(){
score =0;}}
导演类 SSDirector.cs: 具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSDirector : System.Object
{// singlton instance
private static SSDirector _instance;
public IsceneController currentSceneController { get; set;}
public bool running { get; set;}//get instance anytime anywhere!
public static SSDirector getInstance(){if(_instance == null){
_instance = new SSDirector();}return _instance;}
public intgetFPS(){return Application.targetFrameRate;}
public voidsetFPS(int fps){
Application.targetFrameRate = fps;}}
用户交互类 Interface.cs: 这个类主要用于用户交互。具体实现如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Direction { Left, Right, Unfixed};
public interface IsceneController
{voidLoadResources();}
public interface diskAction
{voidBoomshakalaka(GameObject disk);voidDrop(GameObject disk);}