00001 //BSD License 00002 //DXGfx® - http://www.eteractions.com 00003 //Copyright (c) 2005 00004 //by Guillaume Randon 00005 //Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00006 //and associated documentation files (the "Software"), to deal in the Software without restriction, 00007 //including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 //and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 00009 //subject to the following conditions: 00010 //The above copyright notice and this permission notice shall be included in all copies or substantial 00011 //portions of the Software. 00012 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 00013 //INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 00014 //AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 //DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 using System; 00018 using System.Collections.Generic; 00019 using System.Text; 00020 using Microsoft.DirectX; 00021 00022 namespace Game 00023 { 00024 public delegate void ActionCompletedHandler(Action thisisfinished); 00025 00026 public class Action 00027 { 00028 protected IWorldEntity actor; 00029 00030 public IWorldEntity complement = null; 00031 00032 public bool iamdone; 00033 00034 public event ActionCompletedHandler completed; 00035 00036 public Action() 00037 { 00038 iamdone = false; 00039 } 00040 00041 public Action(IWorldEntity theActor) : base() 00042 { 00043 SetActor(theActor); 00044 } 00045 00046 public virtual void SetActor(IWorldEntity theActor) 00047 { 00048 actor = theActor; 00049 } 00050 00051 public virtual bool CheckFeasibility(WorldEntity selectedEntity) 00052 { 00053 return false; 00054 } 00055 00056 public virtual void Update(double appTime, float elapsedTime) 00057 { 00058 if (iamdone) 00059 { 00060 IAmDone(); 00061 } 00062 } 00063 00064 public virtual void IAmDone() 00065 { 00066 iamdone = true; 00067 if (completed != null) 00068 { 00069 completed(this); 00070 } 00071 } 00072 00073 public virtual bool Similar(Action action) 00074 { 00075 return false; 00076 } 00077 00078 public static bool Similar(Action ac1, Action ac2) 00079 { 00080 return ac1.Similar(ac2); 00081 } 00082 } 00083 00084 public interface ICanMove : IWorldEntity 00085 { 00086 void Move(Vector3 dir, float elapsedTime); 00087 } 00088 00089 public class Moving : Action 00090 { 00091 public WorldLocation destination; 00092 00093 public Moving(ICanMove theActor) 00094 : base(theActor) 00095 { 00096 } 00097 00098 public override void SetActor(IWorldEntity theActor) 00099 { 00100 ICanMove movingActor = theActor as ICanMove; 00101 00102 if (movingActor == null) 00103 { 00104 throw new ArgumentException("The actor can not move (doesn't implement ICanMove) which is not accepted for this type of action!"); 00105 } 00106 else 00107 { 00108 base.SetActor(theActor); 00109 } 00110 } 00111 00112 public override bool CheckFeasibility(WorldEntity selectedEntity) 00113 { 00114 return true; 00115 } 00116 00117 public override void Update(double appTime, float elapsedTime) 00118 { 00119 base.Update(appTime, elapsedTime); 00120 00121 Vector3 dir = Vector3.Subtract(destination.SceneObject.Position, actor.SceneObject.Position); 00122 00123 if (dir.Length() > destination.radius) 00124 { 00125 ((ICanMove)actor).Move(Vector3.Normalize(dir), elapsedTime); 00126 } 00127 else 00128 { 00129 IAmDone(); 00130 } 00131 } 00132 00133 public override bool Similar(Action action) 00134 { 00135 bool ret = false; 00136 Moving mov = action as Moving; 00137 if (mov!=null) 00138 { 00139 if (destination.Distance(mov.destination) < 5.0f) 00140 ret = true; 00141 } 00142 return ret; 00143 } 00144 } 00145 00146 public class Sleeping : Action 00147 { 00148 public Sleeping(IWorldEntity theActor) 00149 : base(theActor) 00150 { 00151 } 00152 00153 public override bool Similar(Action action) 00154 { 00155 // Any action should override this. This is needed to check if a completed action 00156 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00157 return false; 00158 } 00159 } 00160 00161 public class Eating : Action 00162 { 00163 public Eating(IWorldEntity theActor) 00164 : base(theActor) 00165 { 00166 } 00167 00168 public override void Update(double appTime, float elapsedTime) 00169 { 00170 IAmDone(); 00171 } 00172 public override bool Similar(Action action) 00173 { 00174 return (complement == action.complement); 00175 } 00176 } 00177 00178 public class MovingAround : Action 00179 { 00180 public MovingAround(IWorldEntity theActor) 00181 : base(theActor) 00182 { 00183 } 00184 00185 public override bool Similar(Action action) 00186 { 00187 // Any action should override this. This is needed to check if a completed action 00188 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00189 return false; 00190 } 00191 } 00192 00193 public class LookingForFood : Action 00194 { 00195 public LookingForFood(IWorldEntity theActor) 00196 : base(theActor) 00197 { 00198 } 00199 00200 public override bool Similar(Action action) 00201 { 00202 // Any action should override this. This is needed to check if a completed action 00203 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00204 return false; 00205 } 00206 } 00207 00208 public class Drinking : Action 00209 { 00210 public Drinking(IWorldEntity theActor) 00211 : base(theActor) 00212 { 00213 } 00214 00215 public override bool Similar(Action action) 00216 { 00217 // Any action should override this. This is needed to check if a completed action 00218 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00219 return false; 00220 } 00221 } 00222 00223 public class Fleeing : Action 00224 { 00225 public Fleeing(IWorldEntity theActor) 00226 : base(theActor) 00227 { 00228 } 00229 00230 public override bool Similar(Action action) 00231 { 00232 // Any action should override this. This is needed to check if a completed action 00233 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00234 return false; 00235 } 00236 } 00237 00238 public class Talking : Action 00239 { 00240 public Talking(IWorldEntity theActor) 00241 : base(theActor) 00242 { 00243 } 00244 00245 public override bool Similar(Action action) 00246 { 00247 // Any action should override this. This is needed to check if a completed action 00248 // was equivalent to the action embedded in a step that some needed to perform to finish a mission. 00249 return false; 00250 } 00251 } 00252 }
1.5.8