Skip to content

Commit 3423e52

Browse files
committed
Added support for physics listeners. Necessary for the real solution
and I'll use it to temporarily send upates the 'naive' way.
1 parent fb43b17 commit 3423e52

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* $Id$
3+
*
4+
* Copyright (c) 2016, Simsilica, LLC
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. Neither the name of the copyright holder nor the names of its
20+
* contributors may be used to endorse or promote products derived
21+
* from this software without specific prior written permission.
22+
*
23+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34+
* OF THE POSSIBILITY OF SUCH DAMAGE.
35+
*/
36+
37+
package example.sim;
38+
39+
import com.simsilica.sim.SimTime;
40+
41+
/**
42+
*
43+
*
44+
* @author Paul Speed
45+
*/
46+
public interface PhysicsListener {
47+
48+
public void beginFrame( SimTime time );
49+
50+
public void updateBody( Body body );
51+
52+
public void endFrame( SimTime time );
53+
54+
}

sim-eth-basic/src/main/java/example/sim/SimplePhysics.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,24 @@ public class SimplePhysics extends AbstractGameSystem {
5858

5959
private ConcurrentLinkedQueue<Body> toAdd = new ConcurrentLinkedQueue<>();
6060
private ConcurrentLinkedQueue<Body> toRemove = new ConcurrentLinkedQueue<>();
61+
62+
private SafeArrayList<PhysicsListener> listeners = new SafeArrayList<>(PhysicsListener.class);
6163

6264
public SimplePhysics() {
6365
}
66+
67+
/**
68+
* Adds a listener that will be notified about physics related updates.
69+
* This is not a thread safe method call so must be called during setup
70+
* or from the physics/simulation thread.
71+
*/
72+
public void addPhysicsListener( PhysicsListener l ) {
73+
listeners.add(l);
74+
}
75+
76+
public void removePhysicsListener( PhysicsListener l ) {
77+
listeners.remove(l);
78+
}
6479

6580
public int createBody() {
6681
return createBody(null);
@@ -106,6 +121,10 @@ private void updateBodyList() {
106121

107122
@Override
108123
public void update( SimTime time ) {
124+
125+
for( PhysicsListener l : listeners.getArray() ) {
126+
l.beginFrame(time);
127+
}
109128

110129
updateBodyList();
111130

@@ -123,7 +142,16 @@ public void update( SimTime time ) {
123142
b.integrate(tpf);
124143
}
125144

126-
// Publish the results
145+
// Publish the results
146+
for( PhysicsListener l : listeners.getArray() ) {
147+
for( Body b : bodies.getArray() ) {
148+
l.updateBody(b);
149+
}
150+
}
151+
152+
for( PhysicsListener l : listeners.getArray() ) {
153+
l.endFrame(time);
154+
}
127155
}
128156

129157
}

0 commit comments

Comments
 (0)