Unity3D Quest MiniGame

用Unity3d 实现一个简易的任务系统,任务都所有状态都在npc身上。 任务物品收集状态是绑定在摄像机上的,二者通过 GameObject.Find 找到对方,然后通过pulic function 进行赋值。

由于MonoDevelop 对中文支持性很差,必须为Unidecode编码才能显示,否则就是乱码,因此为了方便,对话全部用了英文。

npc 身上的代码, 应为不支持中文,所以没怎么写注释。
基本流程为 去 摄像机上取出当前物品的完成状态,去本地化文件中取出任务存档。 然后根据任务状态来显示对应的对话框。
[cc lang=”c#” nowrpa=”false”]
using UnityEngine;
using System.Collections;

public class npc : MonoBehaviour {

public const int TASK_STATE_DEFAULT = 0;
public const int TASK_STATE_OVER = 1;
public const int TASK_STATE_OPEN = 2;
public const int TASK_STATE_GAME = 3;
public const int TASK_STATE_COMPLETE = 4;
public const int TASK_STATE_FINISH = 5;

public int gameState = 0;

private Story script = null;
private Rect windowRect = new Rect(Screen.width – 200 >> 1,0,200,100);
// Use this for initialization
void Start () {
script = GameObject.Find(“Main Camera”).GetComponent();
gameState = PlayerPrefs.GetInt(“gameState”, TASK_STATE_DEFAULT);
}

void OnMouseDown()
{
switch(gameState)
{
case TASK_STATE_OVER:
gameState = TASK_STATE_OPEN;
break;
case TASK_STATE_COMPLETE:
gameState = TASK_STATE_FINISH;
break;
}
}

void OnMouseOver()
{
if(gameState == TASK_STATE_DEFAULT)
{
gameState = TASK_STATE_OVER;
}
}

void OnGUI()
{
switch(gameState)
{
case TASK_STATE_OVER:
GUILayout.Box(“Click Npc to accept a quest”);
break;
case TASK_STATE_OPEN:
windowRect = GUILayout.Window(0, windowRect, AddWindow, “Accpet Quest”);
break;
case TASK_STATE_GAME:
GUILayout.Box(“The num of pic you has got is : ” + script.items);
break;
case TASK_STATE_COMPLETE:
GUILayout.Box(“You have finish your quest”);
break;
case TASK_STATE_FINISH:
GUILayout.Box(“Quest Complete!”);
if(GUILayout.Button(“ReStart”))
{
gameState = TASK_STATE_DEFAULT;
script.items = 0;
PlayerPrefs.SetInt(“itemCount”, script.items);
PlayerPrefs.SetInt(“gameState”, gameState);
}
break;
}
}

void AddWindow(int windowID)
{
GUILayout.Label(“please find 10 pics in this game”);
GUILayout.BeginHorizontal();
if(GUILayout.Button(“Accpet Quest”))
{
gameState = TASK_STATE_GAME;
}

if(GUILayout.Button(“Refuse Quest”))
{
gameState = TASK_STATE_DEFAULT;
}

GUILayout.EndHorizontal();
}

// Update is called once per frame
void Update () {

}
}
[/cc]

然后是摄像机上的脚本
流程为,以玩家鼠标位置发射一道激光到游戏场景中,然后判断激光的目标是否为任务道具, 如果是,道具数量++, 当道具数量为10个的时候,改变npc身上的任务状态为完成。

这里有个偷懒的地方,由于任务物品是手动摆放,而不是动态创建的,因此重置任务后,任务物品不会再次创建。
[cc lang=”c#” nowrpa=”false”]
using UnityEngine;
using System.Collections;

public class Story : MonoBehaviour {

public int items;

private npc my_npc;

// Use this for initialization
void Start () {
items = PlayerPrefs.GetInt(“itemCount”, 0);
my_npc = GameObject.Find(“npc”).GetComponent();
Debug.Log(my_npc);
}

// Update is called once per frame
void Update () {
if(my_npc.gameState == npc.TASK_STATE_GAME)
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
GameObject obj = hit.collider.gameObject;
if(obj.name == “stone”)
{
items ++;
if(items >= 10)
{
my_npc.gameState = npc.TASK_STATE_COMPLETE;
}

Destroy(obj);
PlayerPrefs.SetInt(“itemCount”, items);
PlayerPrefs.SetInt(“gameState”, my_npc.gameState);
}
}
}
}
}
}
[/cc]

点击这个猫咪的方块,就可以收集任务道具了。

任务完成后,回到npc处,会提示任务完成,看情况可以重置任务。

hava fun!

发表评论

电子邮件地址不会被公开。 必填项已用*标注