2
0

Session.cs 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. //-----------------------------------------------------------------------------
  2. // Session.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Xml.Linq;
  12. using System.Xml.Serialization;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using RolePlaying.Data;
  17. namespace RolePlaying
  18. {
  19. class Session
  20. {
  21. internal const int BACK_BUFFER_WIDTH = 1280;
  22. internal const int BACK_BUFFER_HEIGHT = 720;
  23. /// <summary>
  24. /// The single Session instance that can be active at a time.
  25. /// </summary>
  26. private static Session singleton;
  27. internal static Session Singleton => singleton;
  28. /// <summary>
  29. /// The party that is playing the game right now.
  30. /// </summary>
  31. private Party party;
  32. /// <summary>
  33. /// The party that is playing the game right now.
  34. /// </summary>
  35. public static Party Party
  36. {
  37. get { return (singleton == null ? null : singleton.party); }
  38. }
  39. static GameStartDescription gameStartDesc;
  40. /// <summary>
  41. /// Change the current map, arriving at the given portal if any.
  42. /// </summary>
  43. /// <param name="contentName">The asset name of the new map.</param>
  44. /// <param name="originalPortal">The portal from the previous map.</param>
  45. public static void ChangeMap(string contentName, Portal originalPortal)
  46. {
  47. // make sure the content name is valid
  48. string mapContentName = contentName;
  49. if (!mapContentName.StartsWith("Maps" + Path.DirectorySeparatorChar.ToString()))
  50. {
  51. mapContentName = Path.Combine("Maps", mapContentName);
  52. }
  53. // check for trivial movement - typically intra-map portals
  54. if ((TileEngine.Map != null) && (TileEngine.Map.AssetName == mapContentName))
  55. {
  56. TileEngine.SetMap(TileEngine.Map, originalPortal == null ? null :
  57. TileEngine.Map.FindPortal(originalPortal.DestinationMapPortalName));
  58. }
  59. // load the map
  60. ContentManager contentManager = singleton.screenManager.Game.Content;
  61. //TODO Map map = content.Load<Map>(mapContentName).Clone() as Map;
  62. var map = Map.Load(mapContentName, contentManager);
  63. // modify the map based on the world changes (removed chests, etc.).
  64. singleton.ModifyMap(map);
  65. // start playing the music for the new map
  66. AudioManager.PlayMusic(map.MusicCueName);
  67. // set the new map into the tile engine
  68. TileEngine.SetMap(map, originalPortal == null ? null :
  69. map.FindPortal(originalPortal.DestinationMapPortalName));
  70. }
  71. /// <summary>
  72. /// Perform any actions associated withe the given tile.
  73. /// </summary>
  74. /// <param name="mapPosition">The tile to check.</param>
  75. /// <returns>True if anything was encountered, false otherwise.</returns>
  76. public static bool EncounterTile(Point mapPosition)
  77. {
  78. // look for fixed-combats from the quest
  79. if ((singleton.quest != null) &&
  80. ((singleton.quest.Stage == Quest.QuestStage.InProgress) ||
  81. (singleton.quest.Stage == Quest.QuestStage.RequirementsMet)))
  82. {
  83. MapEntry<FixedCombat> fixedCombatEntry =
  84. singleton.quest.FixedCombatEntries.Find(
  85. delegate (WorldEntry<FixedCombat> worldEntry)
  86. {
  87. return
  88. TileEngine.Map.AssetName.EndsWith(
  89. worldEntry.MapContentName) &&
  90. worldEntry.MapPosition == mapPosition;
  91. });
  92. if (fixedCombatEntry != null)
  93. {
  94. Session.EncounterFixedCombat(fixedCombatEntry);
  95. return true;
  96. }
  97. }
  98. // look for fixed-combats from the map
  99. MapEntry<FixedCombat> fixedCombatMapEntry =
  100. TileEngine.Map.FixedCombatEntries.Find(
  101. delegate (MapEntry<FixedCombat> mapEntry)
  102. {
  103. return mapEntry.MapPosition == mapPosition;
  104. });
  105. if (fixedCombatMapEntry != null)
  106. {
  107. Session.EncounterFixedCombat(fixedCombatMapEntry);
  108. return true;
  109. }
  110. // look for chests from the current quest
  111. if (singleton.quest != null)
  112. {
  113. MapEntry<Chest> chestEntry = singleton.quest.ChestEntries.Find(
  114. delegate (WorldEntry<Chest> worldEntry)
  115. {
  116. return
  117. TileEngine.Map.AssetName.EndsWith(
  118. worldEntry.MapContentName) &&
  119. worldEntry.MapPosition == mapPosition;
  120. });
  121. if (chestEntry != null)
  122. {
  123. Session.EncounterChest(chestEntry);
  124. return true;
  125. }
  126. }
  127. // look for chests from the map
  128. MapEntry<Chest> chestMapEntry =
  129. TileEngine.Map.ChestEntries.Find(delegate (MapEntry<Chest> mapEntry)
  130. {
  131. return mapEntry.MapPosition == mapPosition;
  132. });
  133. if (chestMapEntry != null)
  134. {
  135. Session.EncounterChest(chestMapEntry);
  136. return true;
  137. }
  138. // look for player NPCs from the map
  139. MapEntry<Player> playerNpcEntry =
  140. TileEngine.Map.PlayerNpcEntries.Find(delegate (MapEntry<Player> mapEntry)
  141. {
  142. return mapEntry.MapPosition == mapPosition;
  143. });
  144. if (playerNpcEntry != null)
  145. {
  146. Session.EncounterPlayerNpc(playerNpcEntry);
  147. return true;
  148. }
  149. // look for quest NPCs from the map
  150. MapEntry<QuestNpc> questNpcEntry =
  151. TileEngine.Map.QuestNpcEntries.Find(delegate (MapEntry<QuestNpc> mapEntry)
  152. {
  153. return mapEntry.MapPosition == mapPosition;
  154. });
  155. if (questNpcEntry != null)
  156. {
  157. Session.EncounterQuestNpc(questNpcEntry);
  158. return true;
  159. }
  160. // look for portals from the map
  161. MapEntry<Portal> portalEntry =
  162. TileEngine.Map.PortalEntries.Find(delegate (MapEntry<Portal> mapEntry)
  163. {
  164. return mapEntry.MapPosition == mapPosition;
  165. });
  166. if (portalEntry != null)
  167. {
  168. Session.EncounterPortal(portalEntry);
  169. return true;
  170. }
  171. // look for inns from the map
  172. MapEntry<Inn> innEntry =
  173. TileEngine.Map.InnEntries.Find(delegate (MapEntry<Inn> mapEntry)
  174. {
  175. return mapEntry.MapPosition == mapPosition;
  176. });
  177. if (innEntry != null)
  178. {
  179. Session.EncounterInn(innEntry);
  180. return true;
  181. }
  182. // look for stores from the map
  183. MapEntry<Store> storeEntry =
  184. TileEngine.Map.StoreEntries.Find(delegate (MapEntry<Store> mapEntry)
  185. {
  186. return mapEntry.MapPosition == mapPosition;
  187. });
  188. if (storeEntry != null)
  189. {
  190. Session.EncounterStore(storeEntry);
  191. return true;
  192. }
  193. // nothing encountered
  194. return false;
  195. }
  196. /// <summary>
  197. /// Performs the actions associated with encountering a FixedCombat entry.
  198. /// </summary>
  199. public static void EncounterFixedCombat(MapEntry<FixedCombat> fixedCombatEntry)
  200. {
  201. // check the parameter
  202. if ((fixedCombatEntry == null) || (fixedCombatEntry.Content == null))
  203. {
  204. throw new ArgumentNullException("fixedCombatEntry");
  205. }
  206. if (!CombatEngine.IsActive)
  207. {
  208. // start combat
  209. CombatEngine.StartNewCombat(fixedCombatEntry);
  210. }
  211. }
  212. /// <summary>
  213. /// Performs the actions associated with encountering a Chest entry.
  214. /// </summary>
  215. public static void EncounterChest(MapEntry<Chest> chestEntry)
  216. {
  217. // check the parameter
  218. if ((chestEntry == null) || (chestEntry.Content == null))
  219. {
  220. throw new ArgumentNullException("chestEntry");
  221. }
  222. // add the chest screen
  223. singleton.screenManager.AddScreen(new ChestScreen(chestEntry));
  224. }
  225. /// <summary>
  226. /// Performs the actions associated with encountering a player-NPC entry.
  227. /// </summary>
  228. public static void EncounterPlayerNpc(MapEntry<Player> playerEntry)
  229. {
  230. // check the parameter
  231. if ((playerEntry == null) || (playerEntry.Content == null))
  232. {
  233. throw new ArgumentNullException("playerEntry");
  234. }
  235. // add the player-NPC screen
  236. singleton.screenManager.AddScreen(new PlayerNpcScreen(playerEntry));
  237. }
  238. /// <summary>
  239. /// Performs the actions associated with encountering a QuestNpc entry.
  240. /// </summary>
  241. public static void EncounterQuestNpc(MapEntry<QuestNpc> questNpcEntry)
  242. {
  243. // check the parameter
  244. if ((questNpcEntry == null) || (questNpcEntry.Content == null))
  245. {
  246. throw new ArgumentNullException("questNpcEntry");
  247. }
  248. // add the quest-NPC screen
  249. singleton.screenManager.AddScreen(new QuestNpcScreen(questNpcEntry));
  250. }
  251. /// <summary>
  252. /// Performs the actions associated with encountering an Inn entry.
  253. /// </summary>
  254. public static void EncounterInn(MapEntry<Inn> innEntry)
  255. {
  256. // check the parameter
  257. if ((innEntry == null) || (innEntry.Content == null))
  258. {
  259. throw new ArgumentNullException("innEntry");
  260. }
  261. // add the inn screen
  262. singleton.screenManager.AddScreen(new InnScreen(innEntry.Content));
  263. }
  264. /// <summary>
  265. /// Performs the actions associated with encountering a Store entry.
  266. /// </summary>
  267. public static void EncounterStore(MapEntry<Store> storeEntry)
  268. {
  269. // check the parameter
  270. if ((storeEntry == null) || (storeEntry.Content == null))
  271. {
  272. throw new ArgumentNullException("storeEntry");
  273. }
  274. // add the store screen
  275. singleton.screenManager.AddScreen(new StoreScreen(storeEntry.Content));
  276. }
  277. /// <summary>
  278. /// Performs the actions associated with encountering a Portal entry.
  279. /// </summary>
  280. public static void EncounterPortal(MapEntry<Portal> portalEntry)
  281. {
  282. // check the parameter
  283. if ((portalEntry == null) || (portalEntry.Content == null))
  284. {
  285. throw new ArgumentNullException("portalEntry");
  286. }
  287. // change to the new map
  288. ChangeMap(portalEntry.Content.DestinationMapContentName,
  289. portalEntry.Content);
  290. }
  291. /// <summary>
  292. /// Check if a random combat should start. If so, start combat immediately.
  293. /// </summary>
  294. /// <returns>True if combat was started, false otherwise.</returns>
  295. public static bool CheckForRandomCombat(RandomCombat randomCombat)
  296. {
  297. // check the parameter
  298. if ((randomCombat == null) || (randomCombat.CombatProbability <= 0))
  299. {
  300. return false;
  301. }
  302. // check to see if combat has already started
  303. if (CombatEngine.IsActive)
  304. {
  305. return false;
  306. }
  307. // check to see if the random combat starts
  308. int randomCombatCheck = random.Next(100);
  309. if (randomCombatCheck < randomCombat.CombatProbability)
  310. {
  311. // start combat immediately
  312. CombatEngine.StartNewCombat(randomCombat);
  313. return true;
  314. }
  315. // combat not started
  316. return false;
  317. }
  318. /// <summary>
  319. /// The main quest line for this session.
  320. /// </summary>
  321. private QuestLine questLine;
  322. /// <summary>
  323. /// The main quest line for this session.
  324. /// </summary>
  325. public static QuestLine QuestLine
  326. {
  327. get { return (singleton == null ? null : singleton.questLine); }
  328. }
  329. /// <summary>
  330. /// If true, the main quest line for this session is complete.
  331. /// </summary>
  332. public static bool IsQuestLineComplete
  333. {
  334. get
  335. {
  336. if ((singleton == null) || (singleton.questLine == null) ||
  337. (singleton.questLine.QuestContentNames == null))
  338. {
  339. return false;
  340. }
  341. return singleton.currentQuestIndex >=
  342. singleton.questLine.QuestContentNames.Count;
  343. }
  344. }
  345. /// <summary>
  346. /// The current quest in this session.
  347. /// </summary>
  348. private Quest quest;
  349. /// <summary>
  350. /// The current quest in this session.
  351. /// </summary>
  352. public static Quest Quest
  353. {
  354. get { return (singleton == null ? null : singleton.quest); }
  355. }
  356. /// <summary>
  357. /// The index of the current quest into the quest line.
  358. /// </summary>
  359. private int currentQuestIndex = 0;
  360. /// <summary>
  361. /// The index of the current quest into the quest line.
  362. /// </summary>
  363. public static int CurrentQuestIndex
  364. {
  365. get { return (singleton == null ? -1 : singleton.currentQuestIndex); }
  366. }
  367. /// <summary>
  368. /// Update the current quest and quest line for this session.
  369. /// </summary>
  370. public void UpdateQuest()
  371. {
  372. // check the singleton's state to see if we should care about quests
  373. if ((party == null) || (questLine == null))
  374. {
  375. return;
  376. }
  377. // if we don't have a quest, then take the next one from the list
  378. if ((quest == null) && (questLine.Quests.Count > 0) &&
  379. !Session.IsQuestLineComplete)
  380. {
  381. quest = questLine.Quests[currentQuestIndex];
  382. quest.Stage = Quest.QuestStage.NotStarted;
  383. // clear the monster-kill record
  384. party.MonsterKills.Clear();
  385. // clear the modified-quest lists
  386. modifiedQuestChests.Clear();
  387. removedQuestChests.Clear();
  388. removedQuestFixedCombats.Clear();
  389. }
  390. // handle quest-stage transitions
  391. if ((quest != null) && !Session.IsQuestLineComplete)
  392. {
  393. switch (quest.Stage)
  394. {
  395. case Quest.QuestStage.NotStarted:
  396. // start the new quest
  397. quest.Stage = Quest.QuestStage.InProgress;
  398. if (!quest.AreRequirementsMet)
  399. {
  400. // show the announcement of the quest and the requirements
  401. ScreenManager.AddScreen(new QuestLogScreen(quest));
  402. }
  403. break;
  404. case Quest.QuestStage.InProgress:
  405. // update monster requirements
  406. foreach (QuestRequirement<Monster> monsterRequirement in
  407. quest.MonsterRequirements)
  408. {
  409. monsterRequirement.CompletedCount = 0;
  410. Monster monster = monsterRequirement.Content;
  411. if (party.MonsterKills.ContainsKey(monster.AssetName))
  412. {
  413. monsterRequirement.CompletedCount =
  414. party.MonsterKills[monster.AssetName];
  415. }
  416. }
  417. // update gear requirements
  418. foreach (QuestRequirement<Gear> gearRequirement in
  419. quest.GearRequirements)
  420. {
  421. gearRequirement.CompletedCount = 0;
  422. foreach (ContentEntry<Gear> entry in party.Inventory)
  423. {
  424. if (entry.Content == gearRequirement.Content)
  425. {
  426. gearRequirement.CompletedCount += entry.Count;
  427. }
  428. }
  429. }
  430. // check to see if the requirements have been met
  431. if (quest.AreRequirementsMet)
  432. {
  433. // immediately remove the gear
  434. foreach (QuestRequirement<Gear> gearRequirement in
  435. quest.GearRequirements)
  436. {
  437. Gear gear = gearRequirement.Content;
  438. party.RemoveFromInventory(gear,
  439. gearRequirement.Count);
  440. }
  441. // check to see if there is a destination
  442. if (String.IsNullOrEmpty(
  443. quest.DestinationMapContentName))
  444. {
  445. // complete the quest
  446. quest.Stage = Quest.QuestStage.Completed;
  447. // show the completion dialogue
  448. if (!String.IsNullOrEmpty(quest.CompletionMessage))
  449. {
  450. DialogueScreen dialogueScreen = new DialogueScreen();
  451. dialogueScreen.TitleText = "Quest Complete";
  452. dialogueScreen.BackText = String.Empty;
  453. dialogueScreen.DialogueText =
  454. quest.CompletionMessage;
  455. ScreenManager.AddScreen(dialogueScreen);
  456. }
  457. }
  458. else
  459. {
  460. quest.Stage = Quest.QuestStage.RequirementsMet;
  461. // remind the player about the destination
  462. screenManager.AddScreen(new QuestLogScreen(quest));
  463. }
  464. }
  465. break;
  466. case Quest.QuestStage.RequirementsMet:
  467. break;
  468. case Quest.QuestStage.Completed:
  469. // show the quest rewards screen
  470. RewardsScreen rewardsScreen =
  471. new RewardsScreen(RewardsScreen.RewardScreenMode.Quest,
  472. Quest.ExperienceReward, Quest.GoldReward, Quest.GearRewards);
  473. screenManager.AddScreen(rewardsScreen);
  474. // advance to the next quest
  475. currentQuestIndex++;
  476. quest = null;
  477. break;
  478. }
  479. }
  480. }
  481. /// <summary>
  482. /// The chests removed from the map asset by player actions.
  483. /// </summary>
  484. private List<WorldEntry<Chest>> removedMapChests =
  485. new List<WorldEntry<Chest>>();
  486. /// <summary>
  487. /// The chests removed from the current quest asset by player actions.
  488. /// </summary>
  489. private List<WorldEntry<Chest>> removedQuestChests =
  490. new List<WorldEntry<Chest>>();
  491. /// <summary>
  492. /// Remove the given chest entry from the current map or quest.
  493. /// </summary>
  494. public static void RemoveChest(MapEntry<Chest> mapEntry)
  495. {
  496. // check the parameter
  497. if (mapEntry == null)
  498. {
  499. return;
  500. }
  501. // check the map for the item first
  502. if (TileEngine.Map != null)
  503. {
  504. int removedEntries = TileEngine.Map.ChestEntries.RemoveAll(
  505. delegate (MapEntry<Chest> entry)
  506. {
  507. return ((entry.ContentName == mapEntry.ContentName) &&
  508. (entry.MapPosition == mapEntry.MapPosition));
  509. });
  510. if (removedEntries > 0)
  511. {
  512. WorldEntry<Chest> worldEntry = new WorldEntry<Chest>();
  513. worldEntry.Content = mapEntry.Content;
  514. worldEntry.ContentName = mapEntry.ContentName;
  515. worldEntry.Count = mapEntry.Count;
  516. worldEntry.Direction = mapEntry.Direction;
  517. worldEntry.MapContentName = TileEngine.Map.AssetName;
  518. worldEntry.MapPosition = mapEntry.MapPosition;
  519. singleton.removedMapChests.Add(worldEntry);
  520. return;
  521. }
  522. }
  523. // look for the map entry within the quest
  524. if (singleton.quest != null)
  525. {
  526. int removedEntries = singleton.quest.ChestEntries.RemoveAll(
  527. delegate (WorldEntry<Chest> entry)
  528. {
  529. return ((entry.ContentName == mapEntry.ContentName) &&
  530. (entry.MapPosition == mapEntry.MapPosition) &&
  531. TileEngine.Map.AssetName.EndsWith(entry.MapContentName));
  532. });
  533. if (removedEntries > 0)
  534. {
  535. WorldEntry<Chest> worldEntry = new WorldEntry<Chest>();
  536. worldEntry.Content = mapEntry.Content;
  537. worldEntry.ContentName = mapEntry.ContentName;
  538. worldEntry.Count = mapEntry.Count;
  539. worldEntry.Direction = mapEntry.Direction;
  540. worldEntry.MapContentName = TileEngine.Map.AssetName;
  541. worldEntry.MapPosition = mapEntry.MapPosition;
  542. singleton.removedQuestChests.Add(worldEntry);
  543. return;
  544. }
  545. }
  546. }
  547. /// <summary>
  548. /// The fixed-combats removed from the map asset by player actions.
  549. /// </summary>
  550. private List<WorldEntry<FixedCombat>> removedMapFixedCombats =
  551. new List<WorldEntry<FixedCombat>>();
  552. /// <summary>
  553. /// The fixed-combats removed from the current quest asset by player actions.
  554. /// </summary>
  555. private List<WorldEntry<FixedCombat>> removedQuestFixedCombats =
  556. new List<WorldEntry<FixedCombat>>();
  557. /// <summary>
  558. /// Remove the given fixed-combat entry from the current map or quest.
  559. /// </summary>
  560. public static void RemoveFixedCombat(MapEntry<FixedCombat> mapEntry)
  561. {
  562. // check the parameter
  563. if (mapEntry == null)
  564. {
  565. return;
  566. }
  567. // check the map for the item first
  568. if (TileEngine.Map != null)
  569. {
  570. int removedEntries = TileEngine.Map.FixedCombatEntries.RemoveAll(
  571. delegate (MapEntry<FixedCombat> entry)
  572. {
  573. return ((entry.ContentName == mapEntry.ContentName) &&
  574. (entry.MapPosition == mapEntry.MapPosition));
  575. });
  576. if (removedEntries > 0)
  577. {
  578. WorldEntry<FixedCombat> worldEntry = new WorldEntry<FixedCombat>();
  579. worldEntry.Content = mapEntry.Content;
  580. worldEntry.ContentName = mapEntry.ContentName;
  581. worldEntry.Count = mapEntry.Count;
  582. worldEntry.Direction = mapEntry.Direction;
  583. worldEntry.MapContentName = TileEngine.Map.AssetName;
  584. worldEntry.MapPosition = mapEntry.MapPosition;
  585. singleton.removedMapFixedCombats.Add(worldEntry);
  586. return;
  587. }
  588. }
  589. // look for the map entry within the quest
  590. if (singleton.quest != null)
  591. {
  592. int removedEntries = singleton.quest.FixedCombatEntries.RemoveAll(
  593. delegate (WorldEntry<FixedCombat> entry)
  594. {
  595. return ((entry.ContentName == mapEntry.ContentName) &&
  596. (entry.MapPosition == mapEntry.MapPosition) &&
  597. TileEngine.Map.AssetName.EndsWith(entry.MapContentName));
  598. });
  599. if (removedEntries > 0)
  600. {
  601. WorldEntry<FixedCombat> worldEntry = new WorldEntry<FixedCombat>();
  602. worldEntry.Content = mapEntry.Content;
  603. worldEntry.ContentName = mapEntry.ContentName;
  604. worldEntry.Count = mapEntry.Count;
  605. worldEntry.Direction = mapEntry.Direction;
  606. worldEntry.MapContentName = TileEngine.Map.AssetName;
  607. worldEntry.MapPosition = mapEntry.MapPosition;
  608. singleton.removedQuestFixedCombats.Add(worldEntry);
  609. return;
  610. }
  611. }
  612. }
  613. /// <summary>
  614. /// The player NPCs removed from the map asset by player actions.
  615. /// </summary>
  616. private List<WorldEntry<Player>> removedMapPlayerNpcs =
  617. new List<WorldEntry<Player>>();
  618. /// <summary>
  619. /// Remove the given player NPC entry from the current map or quest.
  620. /// </summary>
  621. public static void RemovePlayerNpc(MapEntry<Player> mapEntry)
  622. {
  623. // check the parameter
  624. if (mapEntry == null)
  625. {
  626. return;
  627. }
  628. // check the map for the item
  629. if (TileEngine.Map != null)
  630. {
  631. int removedEntries = TileEngine.Map.PlayerNpcEntries.RemoveAll(
  632. delegate (MapEntry<Player> entry)
  633. {
  634. return ((entry.ContentName == mapEntry.ContentName) &&
  635. (entry.MapPosition == mapEntry.MapPosition));
  636. });
  637. if (removedEntries > 0)
  638. {
  639. WorldEntry<Player> worldEntry = new WorldEntry<Player>();
  640. worldEntry.Content = mapEntry.Content;
  641. worldEntry.ContentName = mapEntry.ContentName;
  642. worldEntry.Count = mapEntry.Count;
  643. worldEntry.Direction = mapEntry.Direction;
  644. worldEntry.MapContentName = TileEngine.Map.AssetName;
  645. worldEntry.MapPosition = mapEntry.MapPosition;
  646. singleton.removedMapPlayerNpcs.Add(worldEntry);
  647. return;
  648. }
  649. }
  650. // quests don't have a list of player NPCs
  651. }
  652. /// <summary>
  653. /// The chests that have been modified, but not emptied, by player action.
  654. /// </summary>
  655. private List<ModifiedChestEntry> modifiedMapChests =
  656. new List<ModifiedChestEntry>();
  657. /// <summary>
  658. /// The chests belonging to the current quest that have been modified,
  659. /// but not emptied, by player action.
  660. /// </summary>
  661. private List<ModifiedChestEntry> modifiedQuestChests =
  662. new List<ModifiedChestEntry>();
  663. /// <summary>
  664. /// Stores the entry for a chest on the current map or quest that has been
  665. /// modified but not emptied.
  666. /// </summary>
  667. public static void StoreModifiedChest(MapEntry<Chest> mapEntry)
  668. {
  669. // check the parameter
  670. if ((mapEntry == null) || (mapEntry.Content == null))
  671. {
  672. throw new ArgumentNullException("mapEntry");
  673. }
  674. Predicate<ModifiedChestEntry> checkModifiedChests =
  675. delegate (ModifiedChestEntry entry)
  676. {
  677. return
  678. (TileEngine.Map.AssetName.EndsWith(
  679. entry.WorldEntry.MapContentName) &&
  680. (entry.WorldEntry.ContentName == mapEntry.ContentName) &&
  681. (entry.WorldEntry.MapPosition == mapEntry.MapPosition));
  682. };
  683. // check the map for the item first
  684. if ((TileEngine.Map != null) && TileEngine.Map.ChestEntries.Exists(
  685. delegate (MapEntry<Chest> entry)
  686. {
  687. return ((entry.ContentName == mapEntry.ContentName) &&
  688. (entry.MapPosition == mapEntry.MapPosition));
  689. }))
  690. {
  691. singleton.modifiedMapChests.RemoveAll(checkModifiedChests);
  692. ModifiedChestEntry modifiedChestEntry = new ModifiedChestEntry();
  693. modifiedChestEntry.WorldEntry.Content = mapEntry.Content;
  694. modifiedChestEntry.WorldEntry.ContentName = mapEntry.ContentName;
  695. modifiedChestEntry.WorldEntry.Count = mapEntry.Count;
  696. modifiedChestEntry.WorldEntry.Direction = mapEntry.Direction;
  697. modifiedChestEntry.WorldEntry.MapContentName =
  698. TileEngine.Map.AssetName;
  699. modifiedChestEntry.WorldEntry.MapPosition = mapEntry.MapPosition;
  700. Chest chest = mapEntry.Content;
  701. modifiedChestEntry.ChestEntries.AddRange(chest.Entries);
  702. modifiedChestEntry.Gold = chest.Gold;
  703. singleton.modifiedMapChests.Add(modifiedChestEntry);
  704. return;
  705. }
  706. // look for the map entry within the quest
  707. if ((singleton.quest != null) && singleton.quest.ChestEntries.Exists(
  708. delegate (WorldEntry<Chest> entry)
  709. {
  710. return ((entry.ContentName == mapEntry.ContentName) &&
  711. (entry.MapPosition == mapEntry.MapPosition) &&
  712. TileEngine.Map.AssetName.EndsWith(entry.MapContentName));
  713. }))
  714. {
  715. singleton.modifiedQuestChests.RemoveAll(checkModifiedChests);
  716. ModifiedChestEntry modifiedChestEntry = new ModifiedChestEntry();
  717. modifiedChestEntry.WorldEntry.Content = mapEntry.Content;
  718. modifiedChestEntry.WorldEntry.ContentName = mapEntry.ContentName;
  719. modifiedChestEntry.WorldEntry.Count = mapEntry.Count;
  720. modifiedChestEntry.WorldEntry.Direction = mapEntry.Direction;
  721. modifiedChestEntry.WorldEntry.MapContentName = TileEngine.Map.AssetName;
  722. modifiedChestEntry.WorldEntry.MapPosition = mapEntry.MapPosition;
  723. Chest chest = mapEntry.Content;
  724. modifiedChestEntry.ChestEntries.AddRange(chest.Entries);
  725. modifiedChestEntry.Gold = chest.Gold;
  726. singleton.modifiedQuestChests.Add(modifiedChestEntry);
  727. return;
  728. }
  729. }
  730. /// <summary>
  731. /// Remove the specified content from the map, typically from an earlier session.
  732. /// </summary>
  733. private void ModifyMap(Map map)
  734. {
  735. // check the parameter
  736. if (map == null)
  737. {
  738. throw new ArgumentNullException("map");
  739. }
  740. // remove all chests that were emptied already
  741. if ((removedMapChests != null) && (removedMapChests.Count > 0))
  742. {
  743. // check each removed-content entry
  744. map.ChestEntries.RemoveAll(delegate (MapEntry<Chest> mapEntry)
  745. {
  746. return (removedMapChests.Exists(
  747. delegate (WorldEntry<Chest> removedEntry)
  748. {
  749. return
  750. (map.AssetName.EndsWith(removedEntry.MapContentName) &&
  751. (removedEntry.ContentName == mapEntry.ContentName) &&
  752. (removedEntry.MapPosition == mapEntry.MapPosition));
  753. }));
  754. });
  755. }
  756. // remove all fixed-combats that were defeated already
  757. if ((removedMapFixedCombats != null) && (removedMapFixedCombats.Count > 0))
  758. {
  759. // check each removed-content entry
  760. map.FixedCombatEntries.RemoveAll(delegate (MapEntry<FixedCombat> mapEntry)
  761. {
  762. return (removedMapFixedCombats.Exists(
  763. delegate (WorldEntry<FixedCombat> removedEntry)
  764. {
  765. return
  766. (map.AssetName.EndsWith(removedEntry.MapContentName) &&
  767. (removedEntry.ContentName == mapEntry.ContentName) &&
  768. (removedEntry.MapPosition == mapEntry.MapPosition));
  769. }));
  770. });
  771. }
  772. // remove the player NPCs that have already joined the party
  773. if ((removedMapPlayerNpcs != null) && (removedMapPlayerNpcs.Count > 0))
  774. {
  775. // check each removed-content entry
  776. map.PlayerNpcEntries.RemoveAll(delegate (MapEntry<Player> mapEntry)
  777. {
  778. return (removedMapPlayerNpcs.Exists(
  779. delegate (WorldEntry<Player> removedEntry)
  780. {
  781. return
  782. (map.AssetName.EndsWith(removedEntry.MapContentName) &&
  783. (removedEntry.ContentName == mapEntry.ContentName) &&
  784. (removedEntry.MapPosition == mapEntry.MapPosition));
  785. }));
  786. });
  787. }
  788. // replace the chest entries of modified chests - they are already clones
  789. if ((modifiedMapChests != null) && (modifiedMapChests.Count > 0))
  790. {
  791. foreach (MapEntry<Chest> entry in map.ChestEntries)
  792. {
  793. ModifiedChestEntry modifiedEntry = modifiedMapChests.Find(
  794. delegate (ModifiedChestEntry modifiedTestEntry)
  795. {
  796. return
  797. (map.AssetName.EndsWith(
  798. modifiedTestEntry.WorldEntry.MapContentName) &&
  799. (modifiedTestEntry.WorldEntry.ContentName ==
  800. entry.ContentName) &&
  801. (modifiedTestEntry.WorldEntry.MapPosition ==
  802. entry.MapPosition));
  803. });
  804. // if the chest has been modified, apply the changes
  805. if (modifiedEntry != null)
  806. {
  807. ModifyChest(entry.Content, modifiedEntry);
  808. }
  809. }
  810. }
  811. }
  812. /// <summary>
  813. /// Remove the specified content from the map, typically from an earlier session.
  814. /// </summary>
  815. private void ModifyQuest(Quest quest)
  816. {
  817. // check the parameter
  818. if (quest == null)
  819. {
  820. throw new ArgumentNullException("quest");
  821. }
  822. // remove all chests that were emptied arleady
  823. if ((removedQuestChests != null) && (removedQuestChests.Count > 0))
  824. {
  825. // check each removed-content entry
  826. quest.ChestEntries.RemoveAll(
  827. delegate (WorldEntry<Chest> worldEntry)
  828. {
  829. return (removedQuestChests.Exists(
  830. delegate (WorldEntry<Chest> removedEntry)
  831. {
  832. return
  833. (removedEntry.MapContentName.EndsWith(
  834. worldEntry.MapContentName) &&
  835. (removedEntry.ContentName ==
  836. worldEntry.ContentName) &&
  837. (removedEntry.MapPosition ==
  838. worldEntry.MapPosition));
  839. }));
  840. });
  841. }
  842. // remove all of the fixed-combats that have already been defeated
  843. if ((removedQuestFixedCombats != null) &&
  844. (removedQuestFixedCombats.Count > 0))
  845. {
  846. // check each removed-content entry
  847. quest.FixedCombatEntries.RemoveAll(
  848. delegate (WorldEntry<FixedCombat> worldEntry)
  849. {
  850. return (removedQuestFixedCombats.Exists(
  851. delegate (WorldEntry<FixedCombat> removedEntry)
  852. {
  853. return
  854. (removedEntry.MapContentName.EndsWith(
  855. worldEntry.MapContentName) &&
  856. (removedEntry.ContentName ==
  857. worldEntry.ContentName) &&
  858. (removedEntry.MapPosition ==
  859. worldEntry.MapPosition));
  860. }));
  861. });
  862. }
  863. // replace the chest entries of modified chests - they are already clones
  864. if ((modifiedQuestChests != null) && (modifiedQuestChests.Count > 0))
  865. {
  866. foreach (WorldEntry<Chest> entry in quest.ChestEntries)
  867. {
  868. ModifiedChestEntry modifiedEntry = modifiedQuestChests.Find(
  869. delegate (ModifiedChestEntry modifiedTestEntry)
  870. {
  871. return
  872. ((modifiedTestEntry.WorldEntry.MapContentName ==
  873. entry.MapContentName) &&
  874. (modifiedTestEntry.WorldEntry.ContentName ==
  875. entry.ContentName) &&
  876. (modifiedTestEntry.WorldEntry.MapPosition ==
  877. entry.MapPosition));
  878. });
  879. // if the chest has been modified, apply the changes
  880. if (modifiedEntry != null)
  881. {
  882. ModifyChest(entry.Content, modifiedEntry);
  883. }
  884. }
  885. }
  886. }
  887. /// <summary>
  888. /// Modify a Chest object based on the data in a ModifiedChestEntry object.
  889. /// </summary>
  890. private void ModifyChest(Chest chest, ModifiedChestEntry modifiedChestEntry)
  891. {
  892. // check the parameters
  893. if ((chest == null) || (modifiedChestEntry == null))
  894. {
  895. return;
  896. }
  897. // set the new gold amount
  898. chest.Gold = modifiedChestEntry.Gold;
  899. // remove all contents not found in the modified version
  900. chest.Entries.RemoveAll(delegate (ContentEntry<Gear> contentEntry)
  901. {
  902. return !modifiedChestEntry.ChestEntries.Exists(
  903. delegate (ContentEntry<Gear> modifiedTestEntry)
  904. {
  905. return (contentEntry.ContentName ==
  906. modifiedTestEntry.ContentName);
  907. });
  908. });
  909. // set the new counts on the remaining content items
  910. foreach (ContentEntry<Gear> contentEntry in chest.Entries)
  911. {
  912. ContentEntry<Gear> modifiedGearEntry =
  913. modifiedChestEntry.ChestEntries.Find(
  914. delegate (ContentEntry<Gear> modifiedTestEntry)
  915. {
  916. return (contentEntry.ContentName ==
  917. modifiedTestEntry.ContentName);
  918. });
  919. if (modifiedGearEntry != null)
  920. {
  921. contentEntry.Count = modifiedGearEntry.Count;
  922. }
  923. }
  924. }
  925. /// <summary>
  926. /// The ScreenManager used to manage all UI in the game.
  927. /// </summary>
  928. private ScreenManager screenManager;
  929. /// <summary>
  930. /// The ScreenManager used to manage all UI in the game.
  931. /// </summary>
  932. public static ScreenManager ScreenManager
  933. {
  934. get { return (singleton == null ? null : singleton.screenManager); }
  935. }
  936. /// <summary>
  937. /// The GameplayScreen object that created this session.
  938. /// </summary>
  939. private GameplayScreen gameplayScreen;
  940. /// <summary>
  941. /// The heads-up-display menu shown on the map and combat screens.
  942. /// </summary>
  943. private Hud hud;
  944. /// <summary>
  945. /// The heads-up-display menu shown on the map and combat screens.
  946. /// </summary>
  947. public static Hud Hud
  948. {
  949. get { return (singleton == null ? null : singleton.hud); }
  950. }
  951. /// <summary>
  952. /// Returns true if there is an active session.
  953. /// </summary>
  954. public static bool IsActive
  955. {
  956. get { return singleton != null; }
  957. }
  958. /// <summary>
  959. /// Private constructor of a Session object.
  960. /// </summary>
  961. /// <remarks>
  962. /// The lack of public constructors forces the singleton model.
  963. /// </remarks>
  964. private Session(ScreenManager screenManager, GameplayScreen gameplayScreen)
  965. {
  966. // check the parameter
  967. if (screenManager == null)
  968. {
  969. throw new ArgumentNullException("screenManager");
  970. }
  971. if (gameplayScreen == null)
  972. {
  973. throw new ArgumentNullException("gameplayScreen");
  974. }
  975. // assign the parameter
  976. this.screenManager = screenManager;
  977. this.gameplayScreen = gameplayScreen;
  978. // create the HUD interface
  979. this.hud = new Hud(screenManager);
  980. this.hud.LoadContent();
  981. }
  982. /// <summary>
  983. /// Update the session for this frame.
  984. /// </summary>
  985. /// <remarks>This should only be called if there are no menus in use.</remarks>
  986. public static void Update(GameTime gameTime)
  987. {
  988. // check the singleton
  989. if (singleton == null)
  990. {
  991. return;
  992. }
  993. if (CombatEngine.IsActive)
  994. {
  995. CombatEngine.Update(gameTime);
  996. }
  997. else
  998. {
  999. singleton.UpdateQuest();
  1000. TileEngine.Update(gameTime);
  1001. }
  1002. }
  1003. /// <summary>
  1004. /// Draws the session environment to the screen
  1005. /// </summary>
  1006. public static void Draw(GameTime gameTime)
  1007. {
  1008. SpriteBatch spriteBatch = singleton.screenManager.SpriteBatch;
  1009. if (CombatEngine.IsActive)
  1010. {
  1011. // draw the combat background
  1012. if (TileEngine.Map.CombatTexture != null)
  1013. {
  1014. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  1015. spriteBatch.Draw(TileEngine.Map.CombatTexture, Vector2.Zero,
  1016. Color.White);
  1017. spriteBatch.End();
  1018. }
  1019. // draw the combat itself
  1020. spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, ScreenManager.GlobalTransformation);
  1021. CombatEngine.Draw(gameTime);
  1022. spriteBatch.End();
  1023. }
  1024. else
  1025. {
  1026. singleton.DrawNonCombat(gameTime);
  1027. }
  1028. singleton.hud.Draw();
  1029. #if DEBUG
  1030. // draw the debug information
  1031. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  1032. spriteBatch.DrawString(Fonts.DebugFont, "Map: " + TileEngine.Map.AssetName, new Vector2(10, 10), Color.Yellow);
  1033. spriteBatch.End();
  1034. #endif
  1035. }
  1036. /// <summary>
  1037. /// Draws everything related to the non-combat part of the screen
  1038. /// </summary>
  1039. /// <param name="gameTime">Provides a snapshot of timing values</param>
  1040. private void DrawNonCombat(GameTime gameTime)
  1041. {
  1042. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  1043. // draw the background
  1044. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  1045. if (TileEngine.Map.Texture != null)
  1046. {
  1047. // draw the ground layer
  1048. TileEngine.DrawLayers(spriteBatch, true, true, false);
  1049. // draw the character shadows
  1050. DrawShadows(spriteBatch);
  1051. }
  1052. spriteBatch.End();
  1053. // Sort the object layer and player according to depth
  1054. spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, ScreenManager.GlobalTransformation);
  1055. float elapsedSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds;
  1056. // draw the party leader
  1057. {
  1058. Player player = party.Players[0];
  1059. Vector2 position = TileEngine.PartyLeaderPosition.ScreenPosition;
  1060. player.Direction = TileEngine.PartyLeaderPosition.Direction;
  1061. player.ResetAnimation(TileEngine.PartyLeaderPosition.IsMoving);
  1062. switch (player.State)
  1063. {
  1064. case Character.CharacterState.Idle:
  1065. if (player.MapSprite != null)
  1066. {
  1067. player.MapSprite.UpdateAnimation(elapsedSeconds);
  1068. player.MapSprite.Draw(spriteBatch, position,
  1069. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1070. }
  1071. break;
  1072. case Character.CharacterState.Walking:
  1073. if (player.WalkingSprite != null)
  1074. {
  1075. player.WalkingSprite.UpdateAnimation(elapsedSeconds);
  1076. player.WalkingSprite.Draw(spriteBatch, position,
  1077. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1078. }
  1079. else if (player.MapSprite != null)
  1080. {
  1081. player.MapSprite.UpdateAnimation(elapsedSeconds);
  1082. player.MapSprite.Draw(spriteBatch, position,
  1083. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1084. }
  1085. break;
  1086. }
  1087. }
  1088. // draw the player NPCs
  1089. foreach (MapEntry<Player> playerEntry in TileEngine.Map.PlayerNpcEntries)
  1090. {
  1091. if (playerEntry.Content == null)
  1092. {
  1093. continue;
  1094. }
  1095. Vector2 position =
  1096. TileEngine.GetScreenPosition(playerEntry.MapPosition);
  1097. playerEntry.Content.ResetAnimation(false);
  1098. switch (playerEntry.Content.State)
  1099. {
  1100. case Character.CharacterState.Idle:
  1101. if (playerEntry.Content.MapSprite != null)
  1102. {
  1103. playerEntry.Content.MapSprite.UpdateAnimation(
  1104. elapsedSeconds);
  1105. playerEntry.Content.MapSprite.Draw(spriteBatch, position,
  1106. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1107. }
  1108. break;
  1109. case Character.CharacterState.Walking:
  1110. if (playerEntry.Content.WalkingSprite != null)
  1111. {
  1112. playerEntry.Content.WalkingSprite.UpdateAnimation(
  1113. elapsedSeconds);
  1114. playerEntry.Content.WalkingSprite.Draw(spriteBatch, position,
  1115. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1116. }
  1117. else if (playerEntry.Content.MapSprite != null)
  1118. {
  1119. playerEntry.Content.MapSprite.UpdateAnimation(
  1120. elapsedSeconds);
  1121. playerEntry.Content.MapSprite.Draw(spriteBatch, position,
  1122. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1123. }
  1124. break;
  1125. }
  1126. }
  1127. // draw the quest NPCs
  1128. foreach (MapEntry<QuestNpc> questNpcEntry in TileEngine.Map.QuestNpcEntries)
  1129. {
  1130. if (questNpcEntry.Content == null)
  1131. {
  1132. continue;
  1133. }
  1134. Vector2 position =
  1135. TileEngine.GetScreenPosition(questNpcEntry.MapPosition);
  1136. questNpcEntry.Content.ResetAnimation(false);
  1137. switch (questNpcEntry.Content.State)
  1138. {
  1139. case Character.CharacterState.Idle:
  1140. if (questNpcEntry.Content.MapSprite != null)
  1141. {
  1142. questNpcEntry.Content.MapSprite.UpdateAnimation(
  1143. elapsedSeconds);
  1144. questNpcEntry.Content.MapSprite.Draw(spriteBatch, position,
  1145. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1146. }
  1147. break;
  1148. case Character.CharacterState.Walking:
  1149. if (questNpcEntry.Content.WalkingSprite != null)
  1150. {
  1151. questNpcEntry.Content.WalkingSprite.UpdateAnimation(
  1152. elapsedSeconds);
  1153. questNpcEntry.Content.WalkingSprite.Draw(spriteBatch,
  1154. position,
  1155. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1156. }
  1157. else if (questNpcEntry.Content.MapSprite != null)
  1158. {
  1159. questNpcEntry.Content.MapSprite.UpdateAnimation(
  1160. elapsedSeconds);
  1161. questNpcEntry.Content.MapSprite.Draw(spriteBatch, position,
  1162. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1163. }
  1164. break;
  1165. }
  1166. }
  1167. // draw the fixed-combat monsters NPCs from the TileEngine.Map
  1168. // -- since there may be many of the same FixedCombat object
  1169. // on the TileEngine.Map, but their animations are handled differently
  1170. foreach (MapEntry<FixedCombat> fixedCombatEntry in
  1171. TileEngine.Map.FixedCombatEntries)
  1172. {
  1173. if ((fixedCombatEntry.Content == null) ||
  1174. (fixedCombatEntry.Content.Entries.Count <= 0))
  1175. {
  1176. continue;
  1177. }
  1178. Vector2 position =
  1179. TileEngine.GetScreenPosition(fixedCombatEntry.MapPosition);
  1180. fixedCombatEntry.MapSprite.UpdateAnimation(elapsedSeconds);
  1181. fixedCombatEntry.MapSprite.Draw(spriteBatch, position,
  1182. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1183. }
  1184. // draw the fixed-combat monsters NPCs from the current quest
  1185. // -- since there may be many of the same FixedCombat object
  1186. // on the TileEngine.Map, their animations are handled differently
  1187. if ((quest != null) && ((quest.Stage == Quest.QuestStage.InProgress) ||
  1188. (quest.Stage == Quest.QuestStage.RequirementsMet)))
  1189. {
  1190. foreach (WorldEntry<FixedCombat> fixedCombatEntry
  1191. in quest.FixedCombatEntries)
  1192. {
  1193. if ((fixedCombatEntry.Content == null) ||
  1194. (fixedCombatEntry.Content.Entries.Count <= 0) ||
  1195. !TileEngine.Map.AssetName.EndsWith(
  1196. fixedCombatEntry.MapContentName))
  1197. {
  1198. continue;
  1199. }
  1200. Vector2 position =
  1201. TileEngine.GetScreenPosition(fixedCombatEntry.MapPosition);
  1202. fixedCombatEntry.MapSprite.UpdateAnimation(elapsedSeconds);
  1203. fixedCombatEntry.MapSprite.Draw(spriteBatch, position,
  1204. 1f - position.Y / (float)TileEngine.Viewport.Height);
  1205. }
  1206. }
  1207. // draw the chests from the TileEngine.Map
  1208. foreach (MapEntry<Chest> chestEntry in TileEngine.Map.ChestEntries)
  1209. {
  1210. if (chestEntry.Content == null)
  1211. {
  1212. continue;
  1213. }
  1214. Vector2 position = TileEngine.GetScreenPosition(chestEntry.MapPosition);
  1215. spriteBatch.Draw(chestEntry.Content.Texture,
  1216. position, null, Color.White, 0f, Vector2.Zero, 1f,
  1217. SpriteEffects.None,
  1218. MathHelper.Clamp(1f - position.Y /
  1219. (float)TileEngine.Viewport.Height, 0f, 1f));
  1220. }
  1221. // draw the chests from the current quest
  1222. if ((quest != null) && ((quest.Stage == Quest.QuestStage.InProgress) ||
  1223. (quest.Stage == Quest.QuestStage.RequirementsMet)))
  1224. {
  1225. foreach (WorldEntry<Chest> chestEntry in quest.ChestEntries)
  1226. {
  1227. if ((chestEntry.Content == null) ||
  1228. !TileEngine.Map.AssetName.EndsWith(chestEntry.MapContentName))
  1229. {
  1230. continue;
  1231. }
  1232. Vector2 position =
  1233. TileEngine.GetScreenPosition(chestEntry.MapPosition);
  1234. spriteBatch.Draw(chestEntry.Content.Texture,
  1235. position, null, Color.White, 0f, Vector2.Zero, 1f,
  1236. SpriteEffects.None,
  1237. MathHelper.Clamp(1f - position.Y /
  1238. (float)TileEngine.Viewport.Height, 0f, 1f));
  1239. }
  1240. }
  1241. spriteBatch.End();
  1242. // draw the foreground
  1243. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  1244. if (TileEngine.Map.Texture != null)
  1245. {
  1246. TileEngine.DrawLayers(spriteBatch, false, false, true);
  1247. }
  1248. spriteBatch.End();
  1249. }
  1250. /// <summary>
  1251. /// Draw the shadows that appear under all characters.
  1252. /// </summary>
  1253. private void DrawShadows(SpriteBatch spriteBatch)
  1254. {
  1255. var shadowXOffset = 20;
  1256. var shadowYOffset = 20;
  1257. // draw the shadow of the party leader
  1258. Player player = party.Players[0];
  1259. if (player.ShadowTexture != null)
  1260. {
  1261. var position = TileEngine.PartyLeaderPosition.ScreenPosition;
  1262. position.X -= shadowXOffset;
  1263. position.Y -= shadowYOffset;
  1264. spriteBatch.Draw(player.ShadowTexture,
  1265. position, null, Color.White, 0f,
  1266. new Vector2(
  1267. (player.ShadowTexture.Width - TileEngine.Map.TileSize.X) / 2,
  1268. (player.ShadowTexture.Height - TileEngine.Map.TileSize.Y) / 2 -
  1269. player.ShadowTexture.Height / 6),
  1270. 1f, SpriteEffects.None, 1f);
  1271. }
  1272. // draw the player NPCs' shadows
  1273. foreach (MapEntry<Player> playerEntry in TileEngine.Map.PlayerNpcEntries)
  1274. {
  1275. if (playerEntry.Content == null)
  1276. {
  1277. continue;
  1278. }
  1279. if (playerEntry.Content.ShadowTexture != null)
  1280. {
  1281. Vector2 position =
  1282. TileEngine.GetScreenPosition(playerEntry.MapPosition);
  1283. position.X -= shadowXOffset;
  1284. position.Y -= shadowYOffset;
  1285. spriteBatch.Draw(playerEntry.Content.ShadowTexture, position,
  1286. null, Color.White, 0f,
  1287. new Vector2(
  1288. (playerEntry.Content.ShadowTexture.Width -
  1289. TileEngine.Map.TileSize.X) / 2,
  1290. (playerEntry.Content.ShadowTexture.Height -
  1291. TileEngine.Map.TileSize.Y) / 2 -
  1292. playerEntry.Content.ShadowTexture.Height / 6),
  1293. 1f, SpriteEffects.None, 1f);
  1294. }
  1295. }
  1296. // draw the quest NPCs' shadows
  1297. foreach (MapEntry<QuestNpc> questNpcEntry in TileEngine.Map.QuestNpcEntries)
  1298. {
  1299. if (questNpcEntry.Content == null)
  1300. {
  1301. continue;
  1302. }
  1303. if (questNpcEntry.Content.ShadowTexture != null)
  1304. {
  1305. Vector2 position =
  1306. TileEngine.GetScreenPosition(questNpcEntry.MapPosition);
  1307. position.X -= shadowXOffset;
  1308. position.Y -= shadowYOffset;
  1309. spriteBatch.Draw(questNpcEntry.Content.ShadowTexture, position,
  1310. null, Color.White, 0f,
  1311. new Vector2(
  1312. (questNpcEntry.Content.ShadowTexture.Width -
  1313. TileEngine.Map.TileSize.X) / 2,
  1314. (questNpcEntry.Content.ShadowTexture.Height -
  1315. TileEngine.Map.TileSize.Y) / 2 -
  1316. questNpcEntry.Content.ShadowTexture.Height / 6),
  1317. 1f, SpriteEffects.None, 1f);
  1318. }
  1319. }
  1320. // draw the fixed-combat monsters NPCs' shadows
  1321. foreach (MapEntry<FixedCombat> fixedCombatEntry in
  1322. TileEngine.Map.FixedCombatEntries)
  1323. {
  1324. if ((fixedCombatEntry.Content == null) ||
  1325. (fixedCombatEntry.Content.Entries.Count <= 0))
  1326. {
  1327. continue;
  1328. }
  1329. Monster monster = fixedCombatEntry.Content.Entries[0].Content;
  1330. if (monster.ShadowTexture != null)
  1331. {
  1332. Vector2 position =
  1333. TileEngine.GetScreenPosition(fixedCombatEntry.MapPosition);
  1334. position.X -= shadowXOffset;
  1335. position.Y -= shadowYOffset;
  1336. spriteBatch.Draw(monster.ShadowTexture, position,
  1337. null, Color.White, 0f,
  1338. new Vector2(
  1339. (monster.ShadowTexture.Width - TileEngine.Map.TileSize.X) / 2,
  1340. (monster.ShadowTexture.Height - TileEngine.Map.TileSize.Y) / 2 -
  1341. monster.ShadowTexture.Height / 6),
  1342. 1f, SpriteEffects.None, 1f);
  1343. }
  1344. }
  1345. }
  1346. /// <summary>
  1347. /// Start a new session based on the data provided.
  1348. /// </summary>
  1349. public static void StartNewSession(GameStartDescription gameStartDescription,
  1350. ScreenManager screenManager, GameplayScreen gameplayScreen)
  1351. {
  1352. // check the parameters
  1353. if (gameStartDescription == null)
  1354. {
  1355. throw new ArgumentNullException("gameStartDescripton");
  1356. }
  1357. if (screenManager == null)
  1358. {
  1359. throw new ArgumentNullException("screenManager");
  1360. }
  1361. if (gameplayScreen == null)
  1362. {
  1363. throw new ArgumentNullException("gameplayScreen");
  1364. }
  1365. gameStartDesc = gameStartDescription;
  1366. // end any existing session
  1367. EndSession();
  1368. // create a new singleton
  1369. singleton = new Session(screenManager, gameplayScreen);
  1370. // set up the initial map
  1371. ChangeMap(gameStartDescription.MapContentName, null);
  1372. // set up the initial party
  1373. ContentManager contentManager = singleton.screenManager.Game.Content;
  1374. singleton.party = new Party(gameStartDescription, contentManager);
  1375. }
  1376. /// <summary>
  1377. /// End the current session.
  1378. /// </summary>
  1379. public static void EndSession()
  1380. {
  1381. // exit the gameplay screen
  1382. // -- store the gameplay session, for re-entrance
  1383. if (singleton != null)
  1384. {
  1385. GameplayScreen gameplayScreen = singleton.gameplayScreen;
  1386. singleton.gameplayScreen = null;
  1387. // pop the music
  1388. AudioManager.PopMusic();
  1389. // clear the singleton
  1390. singleton = null;
  1391. if (gameplayScreen != null)
  1392. {
  1393. gameplayScreen.ExitScreen();
  1394. }
  1395. }
  1396. }
  1397. /// <summary>
  1398. /// Start a new session, using the data in the given save game.
  1399. /// </summary>
  1400. /// <param name="saveGameDescription">The description of the save game.</param>
  1401. /// <param name="screenManager">The ScreenManager for the new session.</param>
  1402. public static void LoadSession(SaveGameDescription saveGameDescription,
  1403. ScreenManager screenManager, GameplayScreen gameplayScreen)
  1404. {
  1405. // check the parameters
  1406. if (saveGameDescription == null)
  1407. {
  1408. throw new ArgumentNullException("saveGameDescription");
  1409. }
  1410. if (screenManager == null)
  1411. {
  1412. throw new ArgumentNullException("screenManager");
  1413. }
  1414. if (gameplayScreen == null)
  1415. {
  1416. throw new ArgumentNullException("gameplayScreen");
  1417. }
  1418. // end any existing session
  1419. EndSession();
  1420. // create the new session
  1421. singleton = new Session(screenManager, gameplayScreen);
  1422. // get the storage device and load the session
  1423. // TODO: Re-enable when Storage API returns in future MonoGame version
  1424. /*
  1425. GetStorageDevice(
  1426. delegate(StorageDevice storageDevice)
  1427. {
  1428. LoadSessionResult(storageDevice, saveGameDescription);
  1429. });
  1430. */
  1431. }
  1432. /// <summary>
  1433. /// Receives the storage device and starts a new session,
  1434. /// using the data in the given save game.
  1435. /// </summary>
  1436. /// <remarks>The new session is created in LoadSessionResult.</remarks>
  1437. /// <param name="storageDevice">The chosen storage device.</param>
  1438. /// <param name="saveGameDescription">The description of the save game.</param>
  1439. // TODO: Re-enable when Storage API returns in future MonoGame version
  1440. /*
  1441. public static void LoadSessionResult(StorageDevice storageDevice,
  1442. SaveGameDescription saveGameDescription)
  1443. {
  1444. // check the parameters
  1445. if (saveGameDescription == null)
  1446. {
  1447. throw new ArgumentNullException("saveGameDescription");
  1448. }
  1449. // check the parameter
  1450. if ((storageDevice == null) || !storageDevice.IsConnected)
  1451. {
  1452. return;
  1453. }
  1454. // open the container
  1455. using (StorageContainer storageContainer = OpenContainer(storageDevice))
  1456. {
  1457. using (Stream stream =
  1458. storageContainer.OpenFile(saveGameDescription.FileName, FileMode.Open))
  1459. {
  1460. using (XmlReader xmlReader = XmlReader.Create(stream))
  1461. {
  1462. // <rolePlayingGameData>
  1463. xmlReader.ReadStartElement("rolePlayingGameSaveData");
  1464. // read the map information
  1465. xmlReader.ReadStartElement("mapData");
  1466. string mapAssetName =
  1467. xmlReader.ReadElementString("mapContentName");
  1468. PlayerPosition playerPosition = new XmlSerializer(
  1469. typeof(PlayerPosition)).Deserialize(xmlReader)
  1470. as PlayerPosition;
  1471. singleton.removedMapChests = new XmlSerializer(
  1472. typeof(List<WorldEntry<Chest>>)).Deserialize(xmlReader)
  1473. as List<WorldEntry<Chest>>;
  1474. singleton.removedMapFixedCombats = new XmlSerializer(
  1475. typeof(List<WorldEntry<FixedCombat>>)).Deserialize(xmlReader)
  1476. as List<WorldEntry<FixedCombat>>;
  1477. singleton.removedMapPlayerNpcs = new XmlSerializer(
  1478. typeof(List<WorldEntry<Player>>)).Deserialize(xmlReader)
  1479. as List<WorldEntry<Player>>;
  1480. singleton.modifiedMapChests = new XmlSerializer(
  1481. typeof(List<ModifiedChestEntry>)).Deserialize(xmlReader)
  1482. as List<ModifiedChestEntry>;
  1483. ChangeMap(mapAssetName, null);
  1484. TileEngine.PartyLeaderPosition = playerPosition;
  1485. xmlReader.ReadEndElement();
  1486. // read the quest information
  1487. ContentManager content = Session.ScreenManager.Game.Content;
  1488. xmlReader.ReadStartElement("questData");
  1489. singleton.questLine = content.Load<QuestLine>(
  1490. xmlReader.ReadElementString("questLineContentName")).Clone()
  1491. as QuestLine;
  1492. singleton.currentQuestIndex = Convert.ToInt32(
  1493. xmlReader.ReadElementString("currentQuestIndex"));
  1494. for (int i = 0; i < singleton.currentQuestIndex; i++)
  1495. {
  1496. singleton.questLine.Quests[i].Stage =
  1497. Quest.QuestStage.Completed;
  1498. }
  1499. singleton.removedQuestChests = new XmlSerializer(
  1500. typeof(List<WorldEntry<Chest>>)).Deserialize(xmlReader)
  1501. as List<WorldEntry<Chest>>;
  1502. singleton.removedQuestFixedCombats = new XmlSerializer(
  1503. typeof(List<WorldEntry<FixedCombat>>)).Deserialize(xmlReader)
  1504. as List<WorldEntry<FixedCombat>>;
  1505. singleton.modifiedQuestChests = new XmlSerializer(
  1506. typeof(List<ModifiedChestEntry>)).Deserialize(xmlReader)
  1507. as List<ModifiedChestEntry>;
  1508. Quest.QuestStage questStage = (Quest.QuestStage)Enum.Parse(
  1509. typeof(Quest.QuestStage),
  1510. xmlReader.ReadElementString("currentQuestStage"), true);
  1511. if ((singleton.questLine != null) && !IsQuestLineComplete)
  1512. {
  1513. singleton.quest =
  1514. singleton.questLine.Quests[CurrentQuestIndex];
  1515. singleton.ModifyQuest(singleton.quest);
  1516. singleton.quest.Stage = questStage;
  1517. }
  1518. xmlReader.ReadEndElement();
  1519. // read the party data
  1520. singleton.party = new Party(new XmlSerializer(
  1521. typeof(PartySaveData)).Deserialize(xmlReader)
  1522. as PartySaveData, content);
  1523. // </rolePlayingGameSaveData>
  1524. xmlReader.ReadEndElement();
  1525. }
  1526. }
  1527. }
  1528. }
  1529. */
  1530. /// <summary>
  1531. /// Save the current state of the session.
  1532. /// </summary>
  1533. /// <param name="overwriteDescription">
  1534. /// The description of the save game to over-write, if any.
  1535. /// </param>
  1536. public static void SaveSession(SaveGameDescription overwriteDescription)
  1537. {
  1538. // TODO: Re-enable when Storage API returns in future MonoGame version
  1539. /*
  1540. // retrieve the storage device, asynchronously
  1541. GetStorageDevice(delegate(StorageDevice storageDevice)
  1542. {
  1543. SaveSessionResult(storageDevice, overwriteDescription);
  1544. });
  1545. */
  1546. }
  1547. internal void StartMainQuestLine()
  1548. {
  1549. // load the quest line
  1550. /* var loadedQuestLine = content.Load<QuestLine>(
  1551. Path.Combine("Quests", "QuestLines", gameStartDescription.QuestLineContentName)).Clone() as QuestLine;*/
  1552. var questLine = QuestLine.Load(Path.Combine("Quests", "QuestLines", gameStartDesc.QuestLineContentName), singleton.screenManager.Game.Content);
  1553. if (singleton.questLine != questLine)
  1554. {
  1555. // set the quest line
  1556. singleton.questLine = questLine;
  1557. }
  1558. }
  1559. /// <summary>
  1560. /// Save the current state of the session, with the given storage device.
  1561. /// </summary>
  1562. /// <param name="storageDevice">The chosen storage device.</param>
  1563. /// <param name="overwriteDescription">
  1564. /// The description of the save game to over-write, if any.
  1565. /// </param>
  1566. // TODO: Re-enable when Storage API returns in future MonoGame version
  1567. /*
  1568. private static void SaveSessionResult(StorageDevice storageDevice,
  1569. SaveGameDescription overwriteDescription)
  1570. {
  1571. // check the parameter
  1572. if ((storageDevice == null) || !storageDevice.IsConnected)
  1573. {
  1574. return;
  1575. }
  1576. // open the container
  1577. using (StorageContainer storageContainer =
  1578. OpenContainer(storageDevice))
  1579. {
  1580. string filename;
  1581. string descriptionFilename;
  1582. // get the filenames
  1583. if (overwriteDescription == null)
  1584. {
  1585. int saveGameIndex = 0;
  1586. string testFilename;
  1587. do
  1588. {
  1589. saveGameIndex++;
  1590. testFilename = "SaveGame" + saveGameIndex.ToString() + ".xml";
  1591. }
  1592. while (storageContainer.FileExists(testFilename));
  1593. filename = testFilename;
  1594. descriptionFilename = "SaveGameDescription" +
  1595. saveGameIndex.ToString() + ".xml";
  1596. }
  1597. else
  1598. {
  1599. filename = overwriteDescription.FileName;
  1600. descriptionFilename = "SaveGameDescription" +
  1601. Path.GetFileNameWithoutExtension(
  1602. overwriteDescription.FileName).Substring(8) + ".xml";
  1603. }
  1604. using (Stream stream = storageContainer.OpenFile(filename, FileMode.Create))
  1605. {
  1606. using (XmlWriter xmlWriter = XmlWriter.Create(stream))
  1607. {
  1608. // <rolePlayingGameData>
  1609. xmlWriter.WriteStartElement("rolePlayingGameSaveData");
  1610. // write the map information
  1611. xmlWriter.WriteStartElement("mapData");
  1612. xmlWriter.WriteElementString("mapContentName",
  1613. TileEngine.Map.AssetName);
  1614. new XmlSerializer(typeof(PlayerPosition)).Serialize(
  1615. xmlWriter, TileEngine.PartyLeaderPosition);
  1616. new XmlSerializer(typeof(List<WorldEntry<Chest>>)).Serialize(
  1617. xmlWriter, singleton.removedMapChests);
  1618. new XmlSerializer(
  1619. typeof(List<WorldEntry<FixedCombat>>)).Serialize(
  1620. xmlWriter, singleton.removedMapFixedCombats);
  1621. new XmlSerializer(typeof(List<WorldEntry<Player>>)).Serialize(
  1622. xmlWriter, singleton.removedMapPlayerNpcs);
  1623. new XmlSerializer(typeof(List<ModifiedChestEntry>)).Serialize(
  1624. xmlWriter, singleton.modifiedMapChests);
  1625. xmlWriter.WriteEndElement();
  1626. // write the quest information
  1627. xmlWriter.WriteStartElement("questData");
  1628. xmlWriter.WriteElementString("questLineContentName",
  1629. singleton.questLine.AssetName);
  1630. xmlWriter.WriteElementString("currentQuestIndex",
  1631. singleton.currentQuestIndex.ToString());
  1632. new XmlSerializer(typeof(List<WorldEntry<Chest>>)).Serialize(
  1633. xmlWriter, singleton.removedQuestChests);
  1634. new XmlSerializer(
  1635. typeof(List<WorldEntry<FixedCombat>>)).Serialize(
  1636. xmlWriter, singleton.removedQuestFixedCombats);
  1637. new XmlSerializer(typeof(List<ModifiedChestEntry>)).Serialize(
  1638. xmlWriter, singleton.modifiedQuestChests);
  1639. xmlWriter.WriteElementString("currentQuestStage",
  1640. IsQuestLineComplete ?
  1641. Quest.QuestStage.NotStarted.ToString() :
  1642. singleton.quest.Stage.ToString());
  1643. xmlWriter.WriteEndElement();
  1644. // write the party data
  1645. new XmlSerializer(typeof(PartySaveData)).Serialize(xmlWriter,
  1646. new PartySaveData(singleton.party));
  1647. // </rolePlayingGameSaveData>
  1648. xmlWriter.WriteEndElement();
  1649. }
  1650. }
  1651. // create the save game description
  1652. SaveGameDescription description = new SaveGameDescription();
  1653. description.FileName = Path.GetFileName(filename);
  1654. description.ChapterName = IsQuestLineComplete ? "Quest Line Complete" :
  1655. Quest.Name;
  1656. description.Description = DateTime.Now.ToString();
  1657. using (Stream stream =
  1658. storageContainer.OpenFile(descriptionFilename, FileMode.Create))
  1659. {
  1660. new XmlSerializer(typeof(SaveGameDescription)).Serialize(stream,
  1661. description);
  1662. }
  1663. }
  1664. }
  1665. */
  1666. /// <summary>
  1667. /// Delete the save game specified by the description.
  1668. /// </summary>
  1669. /// <param name="saveGameDescription">The description of the save game.</param>
  1670. // TODO: Re-enable when Storage API returns in future MonoGame version
  1671. /*
  1672. public static void DeleteSaveGame(SaveGameDescription saveGameDescription)
  1673. {
  1674. // check the parameters
  1675. if (saveGameDescription == null)
  1676. {
  1677. throw new ArgumentNullException("saveGameDescription");
  1678. }
  1679. // get the storage device and load the session
  1680. GetStorageDevice(
  1681. delegate(StorageDevice storageDevice)
  1682. {
  1683. DeleteSaveGameResult(storageDevice, saveGameDescription);
  1684. });
  1685. }
  1686. */
  1687. /// <summary>
  1688. /// Delete the save game specified by the description.
  1689. /// </summary>
  1690. /// <param name="storageDevice">The chosen storage device.</param>
  1691. /// <param name="saveGameDescription">The description of the save game.</param>
  1692. // TODO: Re-enable when Storage API returns in future MonoGame version
  1693. /*
  1694. public static void DeleteSaveGameResult(StorageDevice storageDevice,
  1695. SaveGameDescription saveGameDescription)
  1696. {
  1697. // check the parameters
  1698. if (saveGameDescription == null)
  1699. {
  1700. throw new ArgumentNullException("saveGameDescription");
  1701. }
  1702. // check the parameter
  1703. if ((storageDevice == null) || !storageDevice.IsConnected)
  1704. {
  1705. return;
  1706. }
  1707. // open the container
  1708. using (StorageContainer storageContainer =
  1709. OpenContainer(storageDevice))
  1710. {
  1711. storageContainer.DeleteFile(saveGameDescription.FileName);
  1712. storageContainer.DeleteFile("SaveGameDescription" +
  1713. Path.GetFileNameWithoutExtension(
  1714. saveGameDescription.FileName).Substring(8) + ".xml");
  1715. }
  1716. // refresh the save game descriptions
  1717. Session.RefreshSaveGameDescriptions();
  1718. }
  1719. */
  1720. /// <summary>
  1721. /// Save game descriptions for the current set of save games.
  1722. /// </summary>
  1723. private static List<SaveGameDescription> saveGameDescriptions = null;
  1724. /// <summary>
  1725. /// Save game descriptions for the current set of save games.
  1726. /// </summary>
  1727. public static List<SaveGameDescription> SaveGameDescriptions
  1728. {
  1729. get { return saveGameDescriptions; }
  1730. }
  1731. /// <summary>
  1732. /// The maximum number of save-game descriptions that the list may hold.
  1733. /// </summary>
  1734. public const int MaximumSaveGameDescriptions = 5;
  1735. /// <summary>
  1736. /// XML serializer for SaveGameDescription objects.
  1737. /// </summary>
  1738. private static XmlSerializer saveGameDescriptionSerializer =
  1739. new XmlSerializer(typeof(SaveGameDescription));
  1740. /// <summary>
  1741. /// Refresh the list of save-game descriptions.
  1742. /// </summary>
  1743. // TODO: Re-enable when Storage API returns in future MonoGame version
  1744. /*
  1745. public static void RefreshSaveGameDescriptions()
  1746. {
  1747. // clear the list
  1748. saveGameDescriptions = null;
  1749. // retrieve the storage device, asynchronously
  1750. GetStorageDevice(RefreshSaveGameDescriptionsResult);
  1751. }
  1752. */
  1753. /// <summary>
  1754. /// Asynchronous storage-device callback for
  1755. /// refreshing the save-game descriptions.
  1756. /// </summary>
  1757. // TODO: Re-enable when Storage API returns in future MonoGame version
  1758. /*
  1759. private static void RefreshSaveGameDescriptionsResult(
  1760. StorageDevice storageDevice)
  1761. {
  1762. // check the parameter
  1763. if ((storageDevice == null) || !storageDevice.IsConnected)
  1764. {
  1765. return;
  1766. }
  1767. // open the container
  1768. using (StorageContainer storageContainer =
  1769. OpenContainer(storageDevice))
  1770. {
  1771. saveGameDescriptions = new List<SaveGameDescription>();
  1772. // get the description list
  1773. string[] filenames =
  1774. storageContainer.GetFileNames("SaveGameDescription*.xml");
  1775. // add each entry to the list
  1776. foreach (string filename in filenames)
  1777. {
  1778. SaveGameDescription saveGameDescription;
  1779. // check the size of the list
  1780. if (saveGameDescriptions.Count >= MaximumSaveGameDescriptions)
  1781. {
  1782. break;
  1783. }
  1784. // open the file stream
  1785. using (Stream fileStream = storageContainer.OpenFile(filename, FileMode.Open))
  1786. {
  1787. // deserialize the object
  1788. saveGameDescription =
  1789. saveGameDescriptionSerializer.Deserialize(fileStream)
  1790. as SaveGameDescription;
  1791. // if it's valid, add it to the list
  1792. if (saveGameDescription != null)
  1793. {
  1794. saveGameDescriptions.Add(saveGameDescription);
  1795. }
  1796. }
  1797. }
  1798. }
  1799. }
  1800. */
  1801. /// <summary>
  1802. /// The stored StorageDevice object.
  1803. /// </summary>
  1804. // TODO: Re-enable when Storage API returns in future MonoGame version
  1805. /*
  1806. private static StorageDevice storageDevice;
  1807. */
  1808. /// <summary>
  1809. /// The container name used for save games.
  1810. /// </summary>
  1811. // TODO: Re-enable when Storage API returns in future MonoGame version
  1812. /*
  1813. public static string SaveGameContainerName = "RolePlayingGame";
  1814. */
  1815. /// <summary>
  1816. /// A delegate for receiving StorageDevice objects.
  1817. /// </summary>
  1818. // TODO: Re-enable when Storage API returns in future MonoGame version
  1819. /*
  1820. public delegate void StorageDeviceDelegate(StorageDevice storageDevice);
  1821. */
  1822. /// <summary>
  1823. /// Asynchronously retrieve a storage device.
  1824. /// </summary>
  1825. /// <param name="retrievalDelegate">
  1826. /// The delegate called when the device is available.
  1827. /// </param>
  1828. /// <remarks>
  1829. /// If there is a suitable cached storage device,
  1830. /// the delegate may be called directly by this function.
  1831. /// </remarks>
  1832. // TODO: Re-enable when Storage API returns in future MonoGame version
  1833. /*
  1834. public static void GetStorageDevice(StorageDeviceDelegate retrievalDelegate)
  1835. {
  1836. // check the parameter
  1837. if (retrievalDelegate == null)
  1838. {
  1839. throw new ArgumentNullException("retrievalDelegate");
  1840. }
  1841. // check the stored storage device
  1842. if ((storageDevice != null) && storageDevice.IsConnected)
  1843. {
  1844. retrievalDelegate(storageDevice);
  1845. return;
  1846. }
  1847. // the storage device must be retrieved
  1848. if (!Guide.IsVisible)
  1849. {
  1850. // Reset the device
  1851. storageDevice = null;
  1852. StorageDevice.BeginShowSelector(GetStorageDeviceResult, retrievalDelegate);
  1853. }
  1854. }
  1855. */
  1856. /// <summary>
  1857. /// Asynchronous callback to the guide's BeginShowStorageDeviceSelector call.
  1858. /// </summary>
  1859. /// <param name="result">The IAsyncResult object with the device.</param>
  1860. // TODO: Re-enable when Storage API returns in future MonoGame version
  1861. /*
  1862. private static void GetStorageDeviceResult(IAsyncResult result)
  1863. {
  1864. // check the parameter
  1865. if ((result == null) || !result.IsCompleted)
  1866. {
  1867. return;
  1868. }
  1869. // retrieve and store the storage device
  1870. storageDevice = StorageDevice.EndShowSelector(result);
  1871. // check the new storage device
  1872. if ((storageDevice != null) && storageDevice.IsConnected)
  1873. {
  1874. // it passes; call the stored delegate
  1875. StorageDeviceDelegate func = result.AsyncState as StorageDeviceDelegate;
  1876. if (func != null)
  1877. {
  1878. func(storageDevice);
  1879. }
  1880. }
  1881. }
  1882. */
  1883. /// <summary>
  1884. /// Synchronously opens storage container
  1885. /// </summary>
  1886. // TODO: Re-enable when Storage API returns in future MonoGame version
  1887. /*
  1888. private static StorageContainer OpenContainer(StorageDevice storageDevice)
  1889. {
  1890. IAsyncResult result =
  1891. storageDevice.BeginOpenContainer(Session.SaveGameContainerName, null, null);
  1892. // Wait for the WaitHandle to become signaled.
  1893. result.AsyncWaitHandle.WaitOne();
  1894. StorageContainer container = storageDevice.EndOpenContainer(result);
  1895. // Close the wait handle.
  1896. result.AsyncWaitHandle.Close();
  1897. return container;
  1898. }
  1899. */
  1900. /// <summary>
  1901. /// The random-number generator used with game events.
  1902. /// </summary>
  1903. private static Random random = new Random();
  1904. /// <summary>
  1905. /// The random-number generator used with game events.
  1906. /// </summary>
  1907. public static Random Random
  1908. {
  1909. get { return random; }
  1910. }
  1911. }
  1912. }