patternjavaModerate
Dangerworld game
Viewed 0 times
dangerworldgamestackoverflow
Problem
You can download the Dangerworld game in pre-alpha version and review the code. The particular issues I have are
```
package adventure;
import com.jme3.system.AppSettings;
import java.io.File;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.BlenderKey;
import com.jme3.asset.plugins.HttpZipLocator;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.MaterialList;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.ogre.OgreMeshKey;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.MouseInput;
public class PyramidLevel extends SimpleApplication implements ActionListener,
AnimEventListener {
private Node gameLevel;
private static boolean useHttp = false;
private BulletAppState bulletAppState;
private AnimChannel channel;
private AnimControl control;
// character
private BetterCharacterControl goblinControl;
private BetterCharacterControl ninjaControl;
private Bette
- making the character walk over steps instead of sliding
- NPC AI how to make the enemy walk along a wall
```
package adventure;
import com.jme3.system.AppSettings;
import java.io.File;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.BlenderKey;
import com.jme3.asset.plugins.HttpZipLocator;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.MaterialList;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.plugins.ogre.OgreMeshKey;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.MouseInput;
public class PyramidLevel extends SimpleApplication implements ActionListener,
AnimEventListener {
private Node gameLevel;
private static boolean useHttp = false;
private BulletAppState bulletAppState;
private AnimChannel channel;
private AnimControl control;
// character
private BetterCharacterControl goblinControl;
private BetterCharacterControl ninjaControl;
private Bette
Solution
Overall, your code looks quite good. There is something however which you can simplify a lot.
Consider these two code-parts:
All this follows the pattern
which can be simplified using the Ternary operator to
Using this operator, we can write:
However, since we set
Besides this, my only suggestion would be to split up methods when they become too long, there are some of your methods that are on the edge of what I would call "too long" so this can be something to think about.
Consider these two code-parts:
if (!ninjaControl.isOnGround()) {
airTime = airTime + tpf;
} else {
airTime = 0;
}
if (binding.equals("CharLeft")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("CharRight")) {
...All this follows the pattern
if (condition) { value = a; } else { value = b; }which can be simplified using the Ternary operator to
value = (condition ? a : b);Using this operator, we can write:
airTime = (!ninjaControl.isOnGround() ? airTime + tpf : 0);
if (binding.equals("CharLeft")) {
left = (value ? true : false);
} else if (binding.equals("CharRight")) {
...However, since we set
left to true if value is true and to false if value is false, we are essentially setting it to the same value as value. So this series of if-else statements can be shortened to:if (binding.equals("CharLeft")) {
left = value;
} else if (binding.equals("CharRight")) {
right = value;
} else if (binding.equals("CharUp")) {
up = value;
} else if (binding.equals("CharDown")) {
down = value;
}Besides this, my only suggestion would be to split up methods when they become too long, there are some of your methods that are on the edge of what I would call "too long" so this can be something to think about.
Code Snippets
if (!ninjaControl.isOnGround()) {
airTime = airTime + tpf;
} else {
airTime = 0;
}
if (binding.equals("CharLeft")) {
if (value) {
left = true;
} else {
left = false;
}
} else if (binding.equals("CharRight")) {
...if (condition) { value = a; } else { value = b; }value = (condition ? a : b);airTime = (!ninjaControl.isOnGround() ? airTime + tpf : 0);
if (binding.equals("CharLeft")) {
left = (value ? true : false);
} else if (binding.equals("CharRight")) {
...if (binding.equals("CharLeft")) {
left = value;
} else if (binding.equals("CharRight")) {
right = value;
} else if (binding.equals("CharUp")) {
up = value;
} else if (binding.equals("CharDown")) {
down = value;
}Context
StackExchange Code Review Q#26729, answer score: 11
Revisions (0)
No revisions yet.