BSActors.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.Physics.BulletSPlugin
  31. {
  32. public class BSActorCollection
  33. {
  34. private BSScene m_physicsScene { get; set; }
  35. private Dictionary<string, BSActor> m_actors;
  36. public BSActorCollection(BSScene physicsScene)
  37. {
  38. m_physicsScene = physicsScene;
  39. m_actors = new Dictionary<string, BSActor>();
  40. }
  41. public void Add(string name, BSActor actor)
  42. {
  43. lock (m_actors)
  44. {
  45. if (!m_actors.ContainsKey(name))
  46. {
  47. m_actors[name] = actor;
  48. }
  49. }
  50. }
  51. public bool RemoveAndRelease(string name)
  52. {
  53. bool ret = false;
  54. lock (m_actors)
  55. {
  56. if (m_actors.ContainsKey(name))
  57. {
  58. BSActor beingRemoved = m_actors[name];
  59. m_actors.Remove(name);
  60. beingRemoved.Dispose();
  61. ret = true;
  62. }
  63. }
  64. return ret;
  65. }
  66. public void Clear()
  67. {
  68. lock (m_actors)
  69. {
  70. ForEachActor(a => a.Dispose());
  71. m_actors.Clear();
  72. }
  73. }
  74. public void Dispose()
  75. {
  76. Clear();
  77. }
  78. public bool HasActor(string name)
  79. {
  80. return m_actors.ContainsKey(name);
  81. }
  82. public bool TryGetActor(string actorName, out BSActor theActor)
  83. {
  84. return m_actors.TryGetValue(actorName, out theActor);
  85. }
  86. public void ForEachActor(Action<BSActor> act)
  87. {
  88. lock (m_actors)
  89. {
  90. foreach (KeyValuePair<string, BSActor> kvp in m_actors)
  91. act(kvp.Value);
  92. }
  93. }
  94. public void Enable(bool enabl)
  95. {
  96. ForEachActor(a => a.SetEnabled(enabl));
  97. }
  98. public void Refresh()
  99. {
  100. ForEachActor(a => a.Refresh());
  101. }
  102. public void RemoveDependencies()
  103. {
  104. ForEachActor(a => a.RemoveDependencies());
  105. }
  106. }
  107. // =============================================================================
  108. /// <summary>
  109. /// Each physical object can have 'actors' who are pushing the object around.
  110. /// This can be used for hover, locking axis, making vehicles, etc.
  111. /// Each physical object can have multiple actors acting on it.
  112. ///
  113. /// An actor usually registers itself with physics scene events (pre-step action)
  114. /// and modifies the parameters on the host physical object.
  115. /// </summary>
  116. public abstract class BSActor
  117. {
  118. protected BSScene m_physicsScene { get; private set; }
  119. protected BSPhysObject m_controllingPrim { get; private set; }
  120. public virtual bool Enabled { get; set; }
  121. public string ActorName { get; private set; }
  122. public BSActor(BSScene physicsScene, BSPhysObject pObj, string actorName)
  123. {
  124. m_physicsScene = physicsScene;
  125. m_controllingPrim = pObj;
  126. ActorName = actorName;
  127. Enabled = true;
  128. }
  129. // Return 'true' if activily updating the prim
  130. public virtual bool isActive
  131. {
  132. get { return Enabled; }
  133. }
  134. // Turn the actor on an off. Only used by ActorCollection to set all enabled/disabled.
  135. // Anyone else should assign true/false to 'Enabled'.
  136. public void SetEnabled(bool setEnabled)
  137. {
  138. Enabled = setEnabled;
  139. }
  140. // Release any connections and resources used by the actor.
  141. public abstract void Dispose();
  142. // Called when physical parameters (properties set in Bullet) need to be re-applied.
  143. public abstract void Refresh();
  144. // The object's physical representation is being rebuilt so pick up any physical dependencies (constraints, ...).
  145. // Register a prestep action to restore physical requirements before the next simulation step.
  146. public abstract void RemoveDependencies();
  147. }
  148. }