source: 2014/24/EemeliK/Zombieland/Jypeli/Physics2DDotNet/Detectors/BroadPhaseCollisionDetector.cs @ 5974

Revision 5974, 3.0 KB checked in by empaheik, 4 years ago (diff)
Line 
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26#if UseDouble
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32using System.Collections.Generic;
33
34namespace Physics2DDotNet.Detectors
35{
36#if !(WINDOWS_PHONE || XBOX)
37    [Serializable]
38#endif
39    public abstract class BroadPhaseCollisionDetector
40    {
41        protected static void SetTag(Body body, object tag)
42        {
43            if (body == null) { throw new ArgumentNullException("body"); }
44            body.DetectorTag = tag;
45        }
46
47        PhysicsEngine engine;
48        public PhysicsEngine Engine
49        {
50            get { return engine; }
51        }
52        protected List<Body> Bodies
53        {
54            get
55            {
56                return engine.bodies;
57            }
58        }
59
60        public abstract void Detect(TimeStep step);
61        internal void OnAddedInternal(PhysicsEngine engine)
62        {
63            if (this.engine != null)
64            {
65                throw new InvalidOperationException("A broadphsed Detector cannot be added to more then one engine.");
66            }
67            this.engine = engine;
68            OnAdded();
69            this.AddBodyRange(engine.bodies);
70        }
71        internal void OnRemovedInternal()
72        {
73            engine = null;
74            Clear();
75            OnRemoved();
76        }
77        protected virtual void OnAdded() { }
78        protected virtual void OnRemoved() { }
79
80        protected internal virtual void AddBodyRange(List<Body> collection) { }
81        protected internal virtual void RemoveExpiredBodies() { }
82
83        protected internal virtual void Clear() { }
84        protected void OnCollision(TimeStep step, Body first, Body second)
85        {
86            this.engine.HandleCollision(step, first, second);
87        }
88    }
89
90}
Note: See TracBrowser for help on using the repository browser.