Guest User

Untitled

a guest
Jun 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using RelayLib;
  5. using GameTypes;
  6. using System.Runtime.CompilerServices;
  7. namespace TingTing
  8. {
  9. public class TingConcrete : Ting { }
  10. public abstract class Ting : RelayObjectTwo
  11. {
  12. public enum ActionEvent
  13. {
  14. START,
  15. STOP,
  16. FIRE,
  17. }
  18.  
  19. public delegate void MyActionListener(Ting pTing, string pActionName, ActionEvent pEvent);
  20. public static readonly string TABLE_NAME = "Ting_Base";
  21. public Logger logger = new Logger();
  22. protected TingRunner _tingRunner;
  23. protected RoomRunner _roomRunner;
  24. #region PRE_TING_CREATION
  25. /// <summary>
  26. ///when a new Ting is created we need to have it created at the correct starting position,
  27. ///otherwise the SetupCells can't resolve their appropriate room, (used functionality in subclass Door).
  28. /// </summary>
  29. private WorldCoordinate _startingPosition = WorldCoordinate.NONE;
  30. private string _startingName = "unnamed";
  31. internal void SetInitCreateValues(string pName, WorldCoordinate pPosition)
  32. {
  33. _startingName = pName;
  34. _startingPosition = pPosition;
  35. }
  36. #endregion
  37. public virtual void Update(float dt) {}
  38.  
  39. #region CELLS
  40.  
  41. ValueEntry<string> CELL_name;
  42. ValueEntry<WorldCoordinate> CELL_position;
  43. ValueEntry<Direction> CELL_direction;
  44. ValueEntry<string> CELL_dialogueLine;
  45. ValueEntry<string> CELL_actionName;
  46. ValueEntry<bool> CELL_actionHasFired;
  47. ValueEntry<float> CELL_actionStartTime;
  48. ValueEntry<float> CELL_actionTriggerTime;
  49. ValueEntry<float> CELL_actionEndTime;
  50. ValueEntry<string> CELL_actionOtherObjectName;
  51. ValueEntry<string> CELL_prefab;
  52. ValueEntry<bool> CELL_isBeingHeld;
  53.  
  54. protected override void SetupCells()
  55. {
  56. CELL_name = EnsureCell("name", _startingName);
  57. CELL_position = EnsureCell("position", _startingPosition);
  58. CELL_direction = EnsureCell("direction", Direction.RIGHT);
  59. CELL_dialogueLine = EnsureCell("dialogueLine", "");
  60. CELL_actionName = EnsureCell("action", "");
  61. CELL_actionHasFired = EnsureCell("actionHasFired", false);
  62. CELL_actionStartTime = EnsureCell("startTime", 0f);
  63. CELL_actionTriggerTime = EnsureCell("triggerTime", 0f);
  64. CELL_actionEndTime = EnsureCell("endTime", 0f);
  65. CELL_actionOtherObjectName = EnsureCell("otherObjectName", "");
  66. CELL_prefab = EnsureCell("prefab", "unspecified");
  67. CELL_isBeingHeld = EnsureCell("isBeingHeld", false);
  68. }
  69.  
  70. #endregion
  71.  
  72. #region ACTIONS
  73.  
  74. public virtual bool CanInteractWith(Ting pTingToInteractWith) { return false; }
  75. public virtual void InteractWith(Ting pTingToInteractWith) { throw new NotImplementedException(); }
  76.  
  77. public void UpdateAction(float pTime)
  78. {
  79. if(actionName != "")
  80. {
  81. if(!actionHasFired && pTime >= actionTriggerTime) {
  82. #if DEBUG
  83. if(actionOtherObject == null) {
  84. logger.Log("Triggering action '" + actionName + "'");
  85. }
  86. else {
  87. logger.Log("Triggering action '" + actionName + "' with other ting '" + actionOtherObject.name + "'");
  88. }
  89. #endif
  90. actionHasFired = true;
  91. if (_dispatchActionFire != null)
  92. _dispatchActionFire(this, actionName, ActionEvent.FIRE);
  93.  
  94. }
  95. if(pTime > actionEndTime) {
  96. #if DEBUG
  97. logger.Log("pTime (" + pTime + ") > actionEndTime (" + actionEndTime + ")");
  98. #endif
  99.  
  100. StopAction();
  101. }
  102. }
  103. }
  104.  
  105. protected void StartAction(string pActionName, Ting pOtherObject, float pLengthUntilTrigger, float pActionLength)
  106. {
  107. #if DEBUG
  108. logger.Log("Starting action '" + pActionName + "'");
  109. #endif
  110. actionName = pActionName;
  111. float worldTime = _tingRunner.worldTime;
  112. actionStartTime = worldTime;
  113. actionEndTime = worldTime + pActionLength;
  114. actionTriggerTime = worldTime + pLengthUntilTrigger;
  115. actionHasFired = false;
  116. actionOtherObject = pOtherObject;
  117. if (_dispatchActionStart != null)
  118. _dispatchActionStart(this, actionName, ActionEvent.START);
  119. }
  120.  
  121. public void StopAction()
  122. {
  123. #if DEBUG
  124. logger.Log("Stopping action '" + actionName + "'");
  125. #endif
  126. actionName = "";
  127. actionOtherObject = null;
  128. if (_dispatchActionStop != null)
  129. _dispatchActionStop(this, actionName, ActionEvent.STOP);
  130. }
  131.  
  132. #endregion
  133.  
  134. #region ACCESSORS
  135.  
  136. public string name {
  137. get {
  138. return CELL_name.data;
  139. }
  140. private set {
  141. CELL_name.data = value;
  142. }
  143. }
  144.  
  145. [ShowInEditor]
  146. public WorldCoordinate position
  147. {
  148. get {
  149. return CELL_position.data;
  150. }
  151. set
  152. {
  153. if (!_roomRunner.HasRoom(value.roomName))
  154. {
  155. throw new WorldCoordinateException("Can't place a ting in a undefined room: " + value.roomName);
  156. }
  157. CELL_position.data = value;
  158. // TODO: Add self to tile node!
  159. }
  160. }
  161.  
  162. public Room room
  163. {
  164. get {
  165. return _roomRunner.GetRoom(CELL_position.data.roomName);
  166. }
  167. }
  168.  
  169. public PointTileNode tile
  170. {
  171. get { return room.GetTile(localPoint); }
  172. }
  173.  
  174. public IntPoint localPoint
  175. {
  176. get { return CELL_position.data.localPosition; }
  177. }
  178.  
  179. public IntPoint worldPoint
  180. {
  181. get { return room.worldPosition + this.localPoint; }
  182. }
  183.  
  184. [ShowInEditor]
  185. public Direction direction {
  186. get {
  187. return CELL_direction.data;
  188. }
  189. set {
  190. CELL_direction.data = value;
  191. }
  192. }
  193.  
  194. [ShowInEditor]
  195. public string dialogueLine {
  196. get {
  197. return CELL_dialogueLine.data;
  198. }
  199. set {
  200. CELL_dialogueLine.data = value;
  201. }
  202. }
  203.  
  204. [EditableInEditor]
  205. public string actionName {
  206. get {
  207. return CELL_actionName.data;
  208. }
  209. set {
  210. CELL_actionName.data = value;
  211. }
  212. }
  213.  
  214. [ShowInEditor]
  215. public bool actionHasFired {
  216. get {
  217. return CELL_actionHasFired.data;
  218. }
  219. set {
  220. CELL_actionHasFired.data = value;
  221. }
  222. }
  223.  
  224. //[ShowInEditor]
  225. public float actionStartTime {
  226. get {
  227. return CELL_actionStartTime.data;
  228. }
  229. set {
  230. CELL_actionStartTime.data = value;
  231. }
  232. }
  233.  
  234. //[ShowInEditor]
  235. public float actionTriggerTime {
  236. get {
  237. return CELL_actionTriggerTime.data;
  238. }
  239. set {
  240. CELL_actionTriggerTime.data = value;
  241. }
  242. }
  243.  
  244. //[ShowInEditor]
  245. public float actionEndTime {
  246. get {
  247. return CELL_actionEndTime.data;
  248. }
  249. set {
  250. CELL_actionEndTime.data = value;
  251. }
  252. }
  253.  
  254. [ShowInEditor]
  255. public string prefab {
  256. get {
  257. return CELL_prefab.data;
  258. }
  259. set {
  260. CELL_prefab.data = value;
  261. }
  262. }
  263.  
  264. [ShowInEditor]
  265. public bool isBeingHeld {
  266. get {
  267. return CELL_isBeingHeld.data;
  268. }
  269. set {
  270. CELL_isBeingHeld.data = value;
  271. }
  272. }
  273.  
  274. public Ting actionOtherObject {
  275. get
  276. {
  277. if(CELL_actionOtherObjectName.data == "")
  278. return null;
  279. else
  280. return _tingRunner.GetTing(CELL_actionOtherObjectName.data);
  281. }
  282. set
  283. {
  284. if (value == null)
  285. CELL_actionOtherObjectName.data = "";
  286. else
  287. CELL_actionOtherObjectName.data = value.name;
  288. }
  289. }
  290.  
  291. internal void SetupBaseRunners(TingRunner pTingRunner, RoomRunner pRoomRunner)
  292. {
  293. _roomRunner = pRoomRunner;
  294. _tingRunner = pTingRunner;
  295. }
  296.  
  297. public virtual bool canBePickedUp {
  298. get {
  299. return false;
  300. }
  301. }
  302.  
  303. public virtual IntPoint[] interactionPoints {
  304. get {
  305. return new IntPoint[] {
  306. localPoint + IntPoint.Down,
  307. localPoint + IntPoint.Up,
  308. localPoint + IntPoint.Left,
  309. localPoint + IntPoint.Right
  310. };
  311. }
  312. }
  313.  
  314. public virtual string tooltipName
  315. {
  316. get {
  317. return "Ting";
  318. }
  319. }
  320.  
  321. public virtual string verbDescription
  322. {
  323. get {
  324. return "Use [NAME]";
  325. }
  326. }
  327.  
  328. protected float worldTime { get { return _tingRunner.worldTime; } }
  329. private event MyActionListener _dispatchActionStart;
  330. private event MyActionListener _dispatchActionStop;
  331. private event MyActionListener _dispatchActionFire;
  332. public void AddActionListener( MyActionListener pActionListener, ActionEvent pEventType)
  333. {
  334. switch (pEventType)
  335. {
  336. case ActionEvent.START: _dispatchActionStart += pActionListener; break;
  337. case ActionEvent.FIRE: _dispatchActionFire += pActionListener; break;
  338. case ActionEvent.STOP: _dispatchActionStop += pActionListener; break;
  339. }
  340. }
  341.  
  342. public void RemoveActionListner(MyActionListener pActionListener, ActionEvent pEventType)
  343. {
  344. switch (pEventType)
  345. {
  346. case ActionEvent.START: _dispatchActionStart -= pActionListener; break;
  347. case ActionEvent.FIRE: _dispatchActionFire -= pActionListener; break;
  348. case ActionEvent.STOP: _dispatchActionStop -= pActionListener; break;
  349. }
  350. }
  351.  
  352. #endregion
  353. }
  354. }
Add Comment
Please, Sign In to add comment