BSActors.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyrightD
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. namespace OpenSim.Region.PhysicsModule.BulletS
  31. {
  32. public class BSActorCollection
  33. {
  34. private Dictionary<string, BSActor> m_actors;
  35. public BSActorCollection()
  36. {
  37. m_actors = new Dictionary<string, BSActor>();
  38. }
  39. public void Add(string name, BSActor actor)
  40. {
  41. lock (m_actors)
  42. {
  43. if (!m_actors.ContainsKey(name))
  44. {
  45. m_actors[name] = actor;
  46. }
  47. }
  48. }
  49. public bool RemoveAndRelease(string name)
  50. {
  51. bool ret = false;
  52. lock (m_actors)
  53. {
  54. if (m_actors.ContainsKey(name))
  55. {
  56. BSActor beingRemoved = m_actors[name];
  57. m_actors.Remove(name);
  58. beingRemoved.Dispose();
  59. ret = true;
  60. }
  61. }
  62. return ret;
  63. }
  64. public void Clear()
  65. {
  66. lock (m_actors)
  67. {
  68. ForEachActor(a => a.Dispose());
  69. m_actors.Clear();
  70. }
  71. }
  72. public void Dispose()
  73. {
  74. Clear();
  75. }
  76. public bool HasActor(string name)
  77. {
  78. return m_actors.ContainsKey(name);
  79. }
  80. public bool TryGetActor(string actorName, out BSActor theActor)
  81. {
  82. return m_actors.TryGetValue(actorName, out theActor);
  83. }
  84. public void ForEachActor(Action<BSActor> act)
  85. {
  86. lock (m_actors)
  87. {
  88. foreach (KeyValuePair<string, BSActor> kvp in m_actors)
  89. act(kvp.Value);
  90. }
  91. }
  92. public void Enable(bool enabl)
  93. {
  94. ForEachActor(a => a.SetEnabled(enabl));
  95. }
  96. public void Refresh()
  97. {
  98. ForEachActor(a => a.Refresh());
  99. }
  100. public void RemoveDependencies()
  101. {
  102. ForEachActor(a => a.RemoveDependencies());
  103. }
  104. }
  105. // =============================================================================
  106. /// <summary>
  107. /// Each physical object can have 'actors' who are pushing the object around.
  108. /// This can be used for hover, locking axis, making vehicles, etc.
  109. /// Each physical object can have multiple actors acting on it.
  110. ///
  111. /// An actor usually registers itself with physics scene events (pre-step action)
  112. /// and modifies the parameters on the host physical object.
  113. /// </summary>
  114. public abstract class BSActor
  115. {
  116. protected BSScene m_physicsScene { get; private set; }
  117. protected BSPhysObject m_controllingPrim { get; private set; }
  118. public virtual bool Enabled { get; set; }
  119. public string ActorName { get; private set; }
  120. public BSActor(BSScene physicsScene, BSPhysObject pObj, string actorName)
  121. {
  122. m_physicsScene = physicsScene;
  123. m_controllingPrim = pObj;
  124. ActorName = actorName;
  125. Enabled = true;
  126. }
  127. // Return 'true' if activily updating the prim
  128. public virtual bool isActive
  129. {
  130. get { return Enabled; }
  131. }
  132. // Turn the actor on an off. Only used by ActorCollection to set all enabled/disabled.
  133. // Anyone else should assign true/false to 'Enabled'.
  134. public void SetEnabled(bool setEnabled)
  135. {
  136. Enabled = setEnabled;
  137. }
  138. // Release any connections and resources used by the actor.
  139. public abstract void Dispose();
  140. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  141. public abstract void Refresh();
  142. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  143. // Register a prestep action to restore physical requirements before the next simulation step.
  144. public abstract void RemoveDependencies();
  145. }
  146. }