-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPhysicsShapeCache.java
More file actions
executable file
·106 lines (91 loc) · 3.47 KB
/
PhysicsShapeCache.java
File metadata and controls
executable file
·106 lines (91 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.codeandweb.physicseditor;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.SerializationException;
import com.badlogic.gdx.utils.XmlReader;
import java.io.IOException;
public class PhysicsShapeCache implements Disposable {
private BodyDefNode bodyDefNode;
/**
* Creates a new instance of ShapeCache and loads the passed PhysicsEditor XML file
* containing the body + fixture definitions.
*
* @param file Handle of the XML file
* @throws SerializationException if XML data file cannot be loaded or parsed
*/
public PhysicsShapeCache(FileHandle file) throws SerializationException {
try {
XmlReader reader = new XmlReader();
XmlReader.Element rootNode = reader.parse(file);
bodyDefNode = new BodyDefNode(rootNode);
} catch (SerializationException e) {
e.printStackTrace();
throw new SerializationException("failed to load physics shapes XML", e);
}
}
/**
* Creates a new instance of ShapeCache and loads the passed PhysicsEditor XML file
* containing the body + fixture definitions.
*
* @param internalPath Internal file path of the XML file
*/
public PhysicsShapeCache(String internalPath) throws SerializationException {
this(Gdx.files.internal(internalPath));
}
/**
* Creates a Box2D body, using the fixture definitions loaded from file.
*
* @param name The name of the body exactly as it appears in the XML file.
* @param world The Box2D world to use to create the body.
* @param bodyDef The body's attributes. The body attributes loaded from XML are ignored
* @param scaleX Scale for the fixture widths.
* @param scaleY Scale for the fixture heights, ignored for circles.
* @return A Box2D body.
*/
public Body createBody(String name, World world, BodyDef bodyDef, float scaleX, float scaleY) {
BodyNode bodyNode = bodyDefNode.bodies.get(name);
return bodyNode == null ? null : bodyNode.createBody(world, bodyDef, scaleX, scaleY);
}
/**
* Creates a Box2D body, using the body + fixture definitions loaded from file.
*
* @param name The name of the body exactly as it appears in the XML file.
* @param world The Box2D world to use to create the body.
* @param scaleX Scale for the fixture widths.
* @param scaleY Scale for the fixture heights, ignored for circles.
* @return A Box2D body.
*/
public Body createBody(String name, World world, float scaleX, float scaleY) {
BodyNode bodyNode = bodyDefNode.bodies.get(name);
return bodyNode == null ? null : bodyNode.createBody(world, scaleX, scaleY);
}
/**
* Checks if the supplied body name is present in the XML file.
*
* @param name The name of the body.
* @return Whether or not the name is present.
*/
public boolean contains(String name) {
return bodyDefNode.bodies.get(name) != null;
}
/**
* Gets the pixels-per-meter ratio.
*
* @return PTM
*/
public float getPTM() {
return bodyDefNode.metadata.ptmRatio;
}
/**
* Releases the Shape objects which have been created when loading the XML file.
* To avoid memory leaks you have to call this method if the ShapeCache instance is no longer needed.
*/
public void dispose() {
for (BodyNode body : bodyDefNode.bodies.values())
body.dispose();
bodyDefNode.bodies.clear();
}
}