⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agentplayer.cs

📁 一个通用的数据库访问层
💻 CS
字号:
using System;
using System.Collections;
using System.Drawing;

namespace YariSoft.Utils
{
	[Serializable]
	public struct AgentOperation
	{
		public enum OperationTypeEnum { Animation, Speak, MoveTo };

		public OperationTypeEnum	OperationType;
		public object				Action;

		#region Constructor/Destructor
		public AgentOperation ( OperationTypeEnum OperationType, object Action )
		{
			this.OperationType	= OperationType;
			this.Action			= Action;
		}
		#endregion
	}

	[Serializable]
	public class AgentMovie
	{
		public ArrayList Operations;
		public string	 Name;

		#region Constructor/Destructor
		public AgentMovie ( string Name )
		{
			this.Operations = new ArrayList();
			this.Name = Name;
		}
		#endregion

		#region Public functions
		public void AddOperation ( AgentOperation Opeartion )
		{
			this.Operations.Add( Opeartion );
		}
		#endregion
	}

	[Serializable]
	public class AgentPlayer
	{
		public enum MovieTypeEnum { Start, Stop, Error, Question, Busy, DefPosition };

		public Hashtable	Movies = null;
		[NonSerialized()]
		public string FileName		= defaultFileName;
		[NonSerialized()]
		public bool   HasChanges	= false;

		#region Local variables
		[NonSerialized()]
		private	Random generator	= null;
		private const string defaultFileName 
									= "Untitled.amv";
		#endregion

		#region Constructor/Destructor
		public AgentPlayer ()
		{
			this.Movies = new Hashtable();
			for(int i = (int)MovieTypeEnum.Start; i <= (int)MovieTypeEnum.Busy; i++ ){
				ArrayList agentMovies = new ArrayList ();
				this.Movies.Add ( ( MovieTypeEnum )i, agentMovies );
			}
			this.generator = new Random();
		}
		#endregion

		#region Public functions
		public void AddMovie ( AgentMovie Movie, MovieTypeEnum MovieType )
		{
			ArrayList agentMovies = ( ArrayList )this.Movies[ MovieType ];
			if( agentMovies != null ){
				agentMovies.Add( Movie );
			}
		}

		public void Play ( MovieTypeEnum MovieType, AgentServerObjects.IAgentCharacter Character )
		{
			if( Character == null ){
				return;
			}

			AgentMovie movie = this.GetMovie( MovieType );
			if( movie != null ){
				this.Play( movie, Character );
			}
		}

		public void Play ( AgentMovie Movie, AgentServerObjects.IAgentCharacter Character )
		{
			if( Movie == null ){
				return;
			}

			try{
				int pdwReqID;
				Character.StopAll( 0xFFFFFFF );
				foreach( AgentOperation operation in Movie.Operations ){
					switch( operation.OperationType ){
						case AgentOperation.OperationTypeEnum.Animation:
							Character.Play( ( string )operation.Action, out pdwReqID );
							break;
						case AgentOperation.OperationTypeEnum.Speak:
							Character.Speak( ( string )operation.Action, null, out pdwReqID );
							break;
						case AgentOperation.OperationTypeEnum.MoveTo:
							Point curPoint = ( Point )operation.Action;
							Character.MoveTo( (short)( curPoint.X ), (short)(curPoint.Y), 1, out pdwReqID);
							break;
					}
				}
			}catch{
			}
		}

		public bool SaveToFile()
		{
			bool showDiaolg = false;
			if( this.FileName == defaultFileName ){
				showDiaolg = true;
			}

			if( YariSoft.Utils.ConfigFile.SaveToFile( ref this.FileName, this, showDiaolg ) ){
				this.HasChanges = false;
			}
			return ! this.HasChanges;
		}

		public bool SaveAsToFile()
		{
			if( YariSoft.Utils.ConfigFile.SaveToFile( ref this.FileName, this, true ) ){
				this.HasChanges = false;
			}
			return ! this.HasChanges;
		}

		public static AgentPlayer LoadFromFile( string FileName )
		{
			AgentPlayer result = ( AgentPlayer )YariSoft.Utils.ConfigFile.LoadFromFile( ref FileName, false );
			if( result == null ){
				result = new AgentPlayer();
			} else {
				result.FileName = FileName;
				result.generator = new Random();
			}
			return result;
		}
		#endregion

		#region Private functions
		private AgentMovie GetMovie ( MovieTypeEnum MovieType )
		{
			ArrayList agentMovies = (ArrayList)this.Movies[ MovieType ];
			if( agentMovies == null ){
				return null;
			}

			if( agentMovies.Count == 0 ){
				return null;
			}
			
			return ( AgentMovie )agentMovies[ this.generator.Next( agentMovies.Count ) ];
		}
		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -