Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using mrogue.Components;
  3. using mrogue.Interface;
  4. using mrogue.Service;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Linq;
  9. using mrogue.World;
  10.  
  11. namespace mrogue
  12. {
  13. /// <summary>
  14. /// Root orchestrator for an object in the game world.
  15. /// Contains a collection of Components that define the Actor's behavior
  16. /// </summary>
  17. public class Actor : Entity
  18. {
  19. private static NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  20.  
  21. public Point Position { get; set; }
  22. public int Elevation { get; set; }
  23. public Map Map { get; set; }
  24.  
  25. private Dictionary<Type, Component> Components { get; set; }
  26.  
  27. public MovementComponent Movement {
  28. get => GetComponent<MovementComponent>();
  29. }
  30. public CombatComponent Combat
  31. {
  32. get => GetComponent<CombatComponent>();
  33. }
  34. public PlayerComponent Player
  35. {
  36. get => GetComponent<PlayerComponent>();
  37. }
  38. public ItemComponent Item
  39. {
  40. get => GetComponent<ItemComponent>();
  41. }
  42. public InventoryComponent Inventory
  43. {
  44. get => GetComponent<InventoryComponent>();
  45. }
  46.  
  47. public Actor()
  48. {
  49. Components = new Dictionary<Type, Component>();
  50. }
  51. public Actor(params Component[] components)
  52. {
  53. Components = new Dictionary<Type, Component>();
  54. foreach (Component c in components)
  55. {
  56. c.Actor = this;
  57. Components.Add(c.GetType(), c);
  58.  
  59. if(c is ItemComponent itemComponent)
  60. {
  61. this.PrintableName = itemComponent.Item.PrintableName;
  62. this.Description = itemComponent.Item.Description;
  63. this.Glyph = itemComponent.Item.Glyph;
  64. }
  65. }
  66. }
  67.  
  68. public T GetComponent<T>() where T : Component
  69. {
  70. var key = typeof(T);
  71. if (Components.ContainsKey(key))
  72. {
  73. return Components[key] as T;
  74. }
  75. else
  76. {
  77. return null;
  78. }
  79. }
  80. public void SetComponent<T>(T component) where T : Component
  81. {
  82. Components[typeof(T)] = component;
  83. component.Actor = this;
  84. }
  85. public bool HasComponent<T>() where T : Component
  86. {
  87. return Components.ContainsKey(typeof(T));
  88. }
  89.  
  90. public void OnTick()
  91. {
  92. foreach(var component in Components.Values)
  93. {
  94. component.OnTick();
  95. }
  96. }
  97.  
  98. /// <summary>
  99. /// Sets the actor's position and recalculates the game state
  100. /// </summary>
  101. /// <param name="position"></param>
  102. public ActionResult SetPosition(Point position)
  103. {
  104. log.Info("Setting actor postion {0}", position);
  105.  
  106. var oldPosition = Position;
  107. var oldElevation = Elevation;
  108.  
  109. this.Position = position;
  110. this.Elevation = Map[Position].Elevation;
  111.  
  112. //TODO - Maybe may a rules system or something?
  113.  
  114. //TODO - Update changed tiles if we ever have a changed tiles list
  115. //TODO - Update FoV map
  116. //TODO - Update pathfinding
  117. //TODO - Trigger step event
  118. //TODO - Handle status changes (slugish, agile)
  119. //TODO - Handle falling in a pit
  120. //TODO - If is player, update global FoV
  121. //TODO - Handle light recalculation
  122.  
  123. return ActionResult.Success;
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement