Skip to content

Commit

Permalink
Merge branch 'render'
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed May 7, 2024
2 parents 98fba96 + 3990bba commit 56e90f1
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
public class PropertyModel extends DefaultTableModel {

private String[] columnNames = {"Name", "Value"};
private transient final List<PropertyPair> list;
private final transient List<PropertyPair> list;

public PropertyModel() {
list = new ArrayList<>();
Expand Down
10 changes: 5 additions & 5 deletions demo/src/main/java/io/github/jmecn/tiled/demo/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ private void initPhysics(PhysicsState physicsState, MapRenderer mapRenderer) {
Vector2f pos = new Vector2f((float) obj.getX(), (float) obj.getY());
for (MapObject collision : tile.getCollisions().getObjects()) {

boolean isSensor = Boolean.TRUE.equals(collision.getProperties().get("is_sensor"));
String sensorBehavior = (String) collision.getProperties().get("sensor_behavior");
boolean isSensor = collision.getProperty("is_sensor", false, Boolean.class);
String sensorBehavior = collision.getProperty("sensor_behavior", String.class);
Body body = createObjectBody(physicsState, pos, size, collision, isSensor);
if (isSensor) {
SensorControl control = new SensorControl(body, sensorBehavior);
Expand All @@ -152,13 +152,13 @@ private void initPhysics(PhysicsState physicsState, MapRenderer mapRenderer) {
}
}
} else {
String bodyType = (String) obj.getProperties().get("body_type");
String bodyType = obj.getProperty("body_type", String.class);
if (bodyType == null) {
// not a physics object
continue;
}
boolean isSensor = Boolean.TRUE.equals(obj.getProperties().get("is_sensor"));
String sensorBehavior = (String) obj.getProperties().get("sensor_behavior");
boolean isSensor = obj.getProperty("is_sensor", false, Boolean.class);
String sensorBehavior = obj.getProperty("sensor_behavior", String.class);
Vector2f size = new Vector2f((float) obj.getWidth(), (float) obj.getHeight());
Vector2f pos = new Vector2f((float) obj.getX(), (float) obj.getY());
logger.info("Create object {} body at: {}, {}", obj.getName(), pos, size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import io.github.jmecn.tiled.renderer.MaterialConst;
import org.jbox2d.callbacks.ContactImpulse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import io.github.jmecn.tiled.demo.control.BodyControl;
import io.github.jmecn.tiled.renderer.factory.SpriteFactory;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.Shape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Spatial;
import io.github.jmecn.tiled.animation.AnimatedTileControl;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.github.jmecn.tiled.demo.Const.ANIM_IDLE_DOWN;
import static io.github.jmecn.tiled.demo.Const.VELOCITY;

/**
Expand All @@ -26,8 +22,6 @@
*/
public class PlayerState extends BaseAppState implements ActionListener {

static Logger logger = LoggerFactory.getLogger(PlayerState.class);

private static final String MOVE_LEFT = "MOVE_LEFT";
private static final String MOVE_RIGHT = "MOVE_RIGHT";
private static final String MOVE_FORWARD = "MOVE_FORWARD";
Expand All @@ -42,11 +36,11 @@ public class PlayerState extends BaseAppState implements ActionListener {

// playerInput
private final Vector2f playerInput = new Vector2f(0f, 0f);
private final float inputSensitive = 5f;
private float inputSensitive = 5f;

// velocity
private final Vector2f velocity = new Vector2f(0f, 0f);
private final float moveSpeed = 16f * 5;
private float moveSpeed = 16f * 5;

private Body body;
private Spatial player;
Expand Down Expand Up @@ -92,6 +86,8 @@ public void onAction(String name, boolean isPressed, float tpf) {
case MOVE_BACKWARD:
backward = isPressed;
break;
default:
break;
}

}
Expand All @@ -110,14 +106,6 @@ public void update(float tpf) {
player.setUserData(VELOCITY, velocity);
}

private void setAnimation(String anim) {
logger.info("Change animation to {}", anim);
// nothing
AnimatedTileControl control = player.getControl(AnimatedTileControl.class);
if (control != null) {
control.setAnim(anim);
}
}
public void updateInput(float tpf) {
float step = tpf * inputSensitive;
if (!left && !right) {
Expand Down Expand Up @@ -173,6 +161,13 @@ public void setBody(Body body) {

public void setPlayer(Spatial player) {
this.player = player;
setAnimation(ANIM_IDLE_DOWN);
}

public void setInputSensitive(float sensitive) {
this.inputSensitive = sensitive;
}

public void setMoveSpeed(float speed) {
this.moveSpeed = speed;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public final class TiledConst {
private TiledConst() {
}

public static final String TILED_J3MD = "Shader/Tiled.j3md";
public static final String EMPTY = "";
public static final String TMX_EXTENSION = "tmx";// tiled map xml file
public static final String TSX_EXTENSION = "tsx";// tileset xml file
Expand Down
68 changes: 67 additions & 1 deletion tmx-loader/src/main/java/io/github/jmecn/tiled/core/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public class Base {

protected Properties properties;
protected Properties properties = new Properties();

public boolean hasProperties() {
return properties != null && !properties.isEmpty();
Expand Down Expand Up @@ -49,4 +49,70 @@ public Properties getProperties() {
public void setProperties(Properties properties) {
this.properties = properties;
}

/**
* @param key property name
* @return true if and only if the property exists
*/
public boolean containsKey(String key) {
return properties.containsKey(key);
}


/** @param key property name
* @return the value for that property if it exists, otherwise, null */
public Object getProperty(String key) {
return properties.get(key);
}

/** Returns the object for the given key, casting it to clazz.
* @param key the key of the object
* @param clazz the class of the object
* @return the object or null if the object is not in the map
* @throws ClassCastException if the object with the given key is not of type clazz */
public <T> T getProperty(String key, Class<T> clazz) {
return (T)getProperty(key);
}

/**
* Returns the object for the given key, casting it to clazz.
* @param key the key of the object
* @param defaultValue the default value
* @param clazz the class of the object
* @return the object or the defaultValue if the object is not in the map
* @throws ClassCastException if the object with the given key is not of type clazz
*/
public <T> T getProperty(String key, T defaultValue, Class<T> clazz) {
Object object = getProperty(key);
return object == null ? defaultValue : (T)object;
}

/**
* @param key property name
* @param value value to be inserted or modified (if it already existed)
*/
public void putProperty(String key, Object value) {
properties.put(key, value);
}

/**
* @param properties set of properties to be added
*/
public void putAll(Properties properties) {
this.properties.putAll(properties);
}

/**
* @param key property name to be removed
*/
public void removeProperty(String key) {
properties.remove(key);
}

/**
* Removes all properties
*/
public void clearProperties() {
properties.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,79 +44,79 @@
* @author yanmaoyuan
*
*/
public class Triangulation {
public final class Triangulation {

private Triangulation() {}

private static final float EPSILON = 0.0000000001f;

public static float area(final List<Vector2f> contour) {

int n = contour.size();

float A = 0.0f;
float a = 0.0f;

for (int p = n - 1, q = 0; q < n; p = q++) {
Vector2f vp = contour.get(p);
Vector2f vq = contour.get(q);
A += vp.determinant(vq);
a += vp.determinant(vq);
}
return A * 0.5f;
return a * 0.5f;
}

/*
* InsideTriangle decides if a point P is inside of the triangle defined by
* A, B, C.
* InsideTriangle decides if a point p is inside the triangle defined by pA, pB, pC.
*/
public static boolean InsideTriangle(Vector2f A, Vector2f B, Vector2f C, Vector2f P)

{
Vector2f a = C.subtract(B);
Vector2f b = A.subtract(C);
Vector2f c = B.subtract(A);
public static boolean insideTriangle(Vector2f pA, Vector2f pB, Vector2f pC, Vector2f p) {
Vector2f a = pC.subtract(pB);
Vector2f b = pA.subtract(pC);
Vector2f c = pB.subtract(pA);

Vector2f ap = P.subtract(A);
Vector2f bp = P.subtract(B);
Vector2f cp = P.subtract(C);
Vector2f ap = p.subtract(pA);
Vector2f bp = p.subtract(pB);
Vector2f cp = p.subtract(pC);

return ((a.determinant(bp) >= 0.0f) && (b.determinant(cp) >= 0.0f) && (c.determinant(ap) >= 0.0f));
}

public static boolean Snip(final List<Vector2f> contour, int u, int v,
int w, int n, int[] V) {
Vector2f A = contour.get(V[u]);
Vector2f B = contour.get(V[v]);
Vector2f C = contour.get(V[w]);
public static boolean snip(final List<Vector2f> contour, int u, int v, int w, int n, int[] vertex) {
Vector2f pA = contour.get(vertex[u]);
Vector2f pB = contour.get(vertex[v]);
Vector2f pC = contour.get(vertex[w]);

if (EPSILON > (((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x))))
if (EPSILON > (((pB.x - pA.x) * (pC.y - pA.y)) - ((pB.y - pA.y) * (pC.x - pA.x)))) {
return false;
}

for (int p = 0; p < n; p++) {
if ((p == u) || (p == v) || (p == w))
if ((p == u) || (p == v) || (p == w)) {
continue;
Vector2f P = contour.get(V[p]);
if (InsideTriangle(A, B, C, P))
}
if (insideTriangle(pA, pB, pC, contour.get(vertex[p]))) {
return false;
}
}

return true;
}

public static boolean Process(final List<Vector2f> contour,
List<Integer> index) {
public static boolean process(final List<Vector2f> contour, List<Integer> index) {
/* allocate and initialize list of Vertices in polygon */

int n = contour.size();
if (n < 3)
return false;

int[] V = new int[n];
int[] vertex = new int[n];

/* we want a counter-clockwise polygon in V */
/* we want a counter-clockwise polygon in vertex */

if (0.0f < area(contour))
for (int v = 0; v < n; v++)
V[v] = v;
vertex[v] = v;
else
for (int v = 0; v < n; v++)
V[v] = (n - 1) - v;
vertex[v] = (n - 1) - v;

int nv = n;

Expand All @@ -141,13 +141,13 @@ public static boolean Process(final List<Vector2f> contour,
if (nv <= w)
w = 0; /* next */

if (Snip(contour, u, v, w, nv, V)) {
if (snip(contour, u, v, w, nv, vertex)) {
int a, b, c, s, t;

/* true names of the vertices */
a = V[u];
b = V[v];
c = V[w];
a = vertex[u];
b = vertex[v];
c = vertex[w];

/* output Triangle */
index.add(a);
Expand All @@ -156,10 +156,10 @@ public static boolean Process(final List<Vector2f> contour,

/* remove v from remaining polygon */
for (s = v, t = v + 1; t < nv; s++, t++)
V[s] = V[t];
vertex[s] = vertex[t];
nv--;

/* resest error detection counter */
/* reset error detection counter */
count = 2 * nv;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public AnimatedTileControl(Tile tile) {
}

public void setAnim(String name) {
Animation anim = tile.getAnimation(name);
if (anim != null) {
if (this.anim != anim) {
this.anim = anim;
Animation animation = tile.getAnimation(name);
if (animation != null) {
if (this.anim != animation) {
this.anim = animation;
resetAnimation();
}
} else {
Expand All @@ -45,10 +45,10 @@ public void setAnim(String name) {
}

public void setAnim(int index) {
Animation anim = tile.getAnimations().get(index);
if (anim != null) {
if (this.anim != anim) {
this.anim = anim;
Animation animation = tile.getAnimations().get(index);
if (animation != null) {
if (this.anim != animation) {
this.anim = animation;
resetAnimation();
}
} else {
Expand Down
Loading

0 comments on commit 56e90f1

Please sign in to comment.