M

作者: 胤醚貔貅 | 来源:发表于2017-07-11 20:56 被阅读20次

    1.有限状态机

    usingUnityEngine;

    usingSystem.Collections;

    usingSystem.Collections.Generic;

    ///

    ///状态基类

    ///

    public  abstract  class  State{

    publicFSMManagerfsm;

    publicState(FSMManagerfsm){

    this.fsm=fsm;

    }

    publicabstractvoidOnEnter();//当状态进入

    publicabstractvoidOnUpdate();//状态持续

    publicabstractvoidOnExit();//状态退出

    }

    publicclassFSMManager{

    publicGameObjectm_ctrl;//控制游戏对象

    publicDictionary<string,State>allState;//存放所有状态

    privateStatecurState;//当前状态

    ///

    ///添加状态

    ///

    publicvoidAddState(Statestate){

    stringstateName=state.GetType().Name;

    if(allState==null){

    allState=newDictionary();

    }

    if(!allState.ContainsKey(stateName)){

    allState.Add(stateName,state);

    }else{

    }

    }

    ///

    ///移除状态

    ///

    ///The 1st type parameter.

    publicvoidRemoveState<T>(){

    stringstateName=typeof(T).Name;

    if(allState.ContainsKey(stateName)){

    allState.Remove(stateName);

    }

    }

    ///

    ///切换状态

    ///

    ///The 1st type parameter.

    publicvoidChangeState<T>(){

    stringstateName=typeof(T).Name;

    if(allState.ContainsKey(stateName)){

    if(curState!=null){

    curState.OnExit();

    }

    curState=allState[stateName];

    curState.OnEnter();

    }

    }

    ///

    ///更新状态机

    ///

    publicvoidUpdateFSM(){

    if(curState!=null){

    curState.OnUpdate();

    }

    }

    }

    怪物脚本

    usingUnityEngine;

    usingSystem.Collections;

    publicclassPlayer:MonoBehaviour{

    publicstringname;

    publicfloathp;

    publicfloatmp;

    publicfloatattack;

    publicfloatattackRange;

    publicTransformtarget;//目标

    publicNavMeshAgentm_Agent;

    FSMManagerfsm;

    voidStart(){

    fsm=newFSMManager();

    fsm.m_ctrl=gameObject;

    fsm.AddState(newIdleState(fsm));

    fsm.AddState(newPursuitstate(fsm));

    fsm.ChangeState();

    }

    voidUpdate(){

    fsm.UpdateFSM();

    }

    }

    2.数据库

    usingUnityEngine;

    usingSystem.Collections;

    ///

    ///普通单例模版

    ///

    publicclassSingletonwhereT:new(){

    //单例

    protectedstaticTinstance;

    publicstaticTInstance{

    get{

    if(instance==null){

    instance=newT();

    }

    returninstance;

    }

    }

    }

    usingUnityEngine;

    usingSystem.Collections;

    usingSystem.Collections.Generic;

    usingSystem.IO;

    usingMono.Data.Sqlite;

    publicclassDataBaseTool:Singleton{

    privatestringDataBaseName="GameData.sqlite";

    privateSqliteConnectionconn;

    privateSqliteCommandcommand;

    privatestringdataPath="";

    publicDataBaseTool(){

    if(conn==null){

    dataPath="DataSource="+Application.streamingAssetsPath+"/"+DataBaseName;

    #ifUNITY_ANDRIOD

    dataPath=AndriodPaltformSet();

    #endif

    try{

    conn=newSqliteConnection(dataPath);

    }catch(System.Exceptione){

    Debug.Log("创建数据库链接失败。。。");

    Debug.Log(e.Message);

    }

    }

    }

    //安卓平台数据库设置

    publicstringAndriodPaltformSet(){

    stringpath="URI=file:"+Application.persistentDataPath+"/"+DataBaseName;

    //AndroidAPK中数据库文件的路径

    stringandroidPath="jar:file://"+Application.dataPath+"!/assets/"+DataBaseName;

    //Android沙盒路径

    stringandroidFilePath=Application.persistentDataPath+"/"+DataBaseName;

    //如果Android项目源文件中不存在数据库文件,说明没有加载过,需要加载

    if(!File.Exists(androidFilePath))

    {

    //从APK路径拿到Sqlite数据库文件,下载

    WWWwww=newWWW(androidPath);

    //下载未完成时,保持等待

    while(!www.isDone){}

    //下载完成,IO流写入到沙盒路径

    File.WriteAllBytes(androidFilePath,www.bytes);

    }

    returnpath;

    }

    ///

    ///打开数据库连接

    ///

    publicvoidOpenConnection(){

    if(conn!=null){

    try{

    conn.Open();

    }catch(System.Exceptionex){

    Debug.Log("数据库打开失败");

    Debug.LogError(ex.Message);

    }

    }

    }

    ///

    ///关闭数据库链接

    ///

    publicvoidCloseConnection(){

    if(conn!=null){

    try{

    conn.Close();

    }catch(System.Exceptionex){

    Debug.Log("数据库关闭失败");

    Debug.LogError(ex.Message);

    }

    }

    }

    #region对外接口方法

    ///

    ///获得一行数据的方法

    ///

    ///The row data.

    ///Sql.

    publicDictionaryGetRowData(stringsql){

    OpenConnection();

    command=conn.CreateCommand();

    command.CommandText=sql;

    SqliteDataReaderreader=command.ExecuteReader();

    Dictionarydata=newDictionary();

    if(reader.Read()){

    for(inti=0;i

    data.Add(reader.GetName(i),reader.GetValue(i));

    }

    }

    reader.Close();

    reader=null;

    command.Dispose();

    command=null;

    CloseConnection();

    returndata;

    }

    ///

    ///获得一张表的方法

    ///

    ///The all data.

    ///Sql.

    publicList>GetAllData(stringsql){

    OpenConnection();

    command=conn.CreateCommand();

    command.CommandText=sql;

    SqliteDataReaderreader=command.ExecuteReader();

    List>allData=newList>();

    while(reader.Read()){

    DictionaryrowData=newDictionary();

    for(inti=0;i

    rowData.Add(reader.GetName(i),reader.GetValue(i));

    }

    allData.Add(rowData);

    }

    reader.Close();

    reader=null;

    command.Dispose();

    command=null;

    CloseConnection();

    returnallData;

    }

    #endregion

    }

    3.四元数缓动

    Vector3torwards=(monster_player.attackTarget.transform.position-monster_player.transform.position);

    QuaternionqTorwards=Quaternion.LookRotation(torwards);

    monster_player.transform.rotation=Quaternion.Slerp(monster_player.transform.rotation,qTorwards,Time.deltaTime*5f);

    相关文章

      网友评论

          本文标题:M

          本文链接:https://www.haomeiwen.com/subject/zfzmhxtx.html