Skip to content

Commit 3766ac3

Browse files
committed
press H to spwan a tentaclehamster
1 parent 9d67633 commit 3766ac3

File tree

7 files changed

+45
-38
lines changed

7 files changed

+45
-38
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The sun is from here: https://openclipart.org/detail/1212/sol-de-mayo-bandera-de
1313

1414
It can be controled with the arrow keys, space is the brake.
1515
Physics debug information can be displayed with P and frame rate with I.
16+
To spawn a tentaclehamster press H.
1617
Enter enables demo mode, the bus location will be reset to the road, it will drive automatically and stop at every station. To leave demo mode simply steer the bus in any direction.
1718
By dragging with the mouse you can change the view and with the mousewheel alter view distance.
1819
When you drive near a station passengers will randomly enter or leave the bus, you can also topple trees/stations/signs.

src/busline3d/appstate/BusClientAppState.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ public void initialize(AppStateManager stateManager, Application app) {
8383
busNode = (Node) this.app.getRootNode().getChild("Bus");
8484
clientAppState.observeSpatial("Bus", busNode);
8585

86-
Node hamster = (Node) this.app.getRootNode().getChild("Hamster");
87-
clientAppState.observeSpatial("Hamster", hamster);
88-
8986
clientAppState.setMessageHandler(this);
9087
clientAppState.sendMessage(new SetNameMessage(stationname));
9188
NetworkAssetLocator.setClientAppState(clientAppState);

src/busline3d/appstate/BusServerAppState.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import busline3d.command.AddStationCommand;
44
import busline3d.command.SetNameCommand;
5-
import busline3d.control.HamsterControl;
65
import busline3d.control.PassengerControl;
76
import busline3d.message.NewPassengerMessage;
87
import busline3d.message.PassengerBusMessage;
@@ -15,8 +14,6 @@
1514
import com.jme3.bullet.BulletAppState;
1615
import com.jme3.bullet.control.RigidBodyControl;
1716
import com.jme3.math.FastMath;
18-
import com.jme3.math.Quaternion;
19-
import com.jme3.math.Transform;
2017
import com.jme3.math.Vector3f;
2118
import com.jme3.network.Filters;
2219
import com.jme3.network.HostedConnection;
@@ -78,7 +75,6 @@ public void initialize(AppStateManager stateManager, Application app) {
7875
serverAppState.addMessageListener(this);
7976
}
8077
addDome();
81-
animateHamster();
8278
}
8379

8480
private void addAddStationCommand(Server server, HostedConnection conn, float oldradius) {
@@ -105,19 +101,6 @@ public void addDome() {
105101
bulletAppState.getPhysicsSpace().add(floorDome);
106102
}
107103

108-
private void animateHamster() {
109-
Node hamster = (Node) this.app.getRootNode().getChild("Hamster");
110-
if (!singleplayer) {
111-
Vector3f location = hamster.getLocalTranslation().clone();
112-
Quaternion rotation = hamster.getLocalRotation().clone();
113-
hamster.removeFromParent();
114-
hamster.setLocalTransform(Transform.IDENTITY);
115-
hamster = serverAppState.addObservedSpatial(hamster, location, rotation, "Hamster", "");
116-
}
117-
BulletAppState bulletAppState = this.app.getStateManager().getState(BulletAppState.class);
118-
hamster.addControl(new HamsterControl(bulletAppState, serverAppState));
119-
}
120-
121104
public void connectionAdded(Server server, HostedConnection conn) {
122105
serverAppState.setSuspendMovement(true);
123106
float oldradius = radius;

src/busline3d/appstate/DriveBusAppState.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package busline3d.appstate;
22

33
import busline3d.BusLine3D;
4+
import busline3d.control.HamsterControl;
45
import com.jme3.app.Application;
56
import com.jme3.app.state.AbstractAppState;
67
import com.jme3.app.state.AppStateManager;
@@ -27,6 +28,7 @@
2728
import com.jme3.scene.Node;
2829
import com.jme3.scene.Spatial;
2930
import de.lessvoid.nifty.Nifty;
31+
import java.util.Collections;
3032
import util.UtilFunctions;
3133
import util.appstate.ServerAppState;
3234

@@ -61,6 +63,7 @@ public class DriveBusAppState extends AbstractAppState implements ActionListener
6163
private float stopped;
6264
private HudAppState hudAppState;
6365
private Nifty nifty;
66+
private int hamstercount;
6467

6568
@Override
6669
public void initialize(AppStateManager stateManager, Application app) {
@@ -151,6 +154,7 @@ private void setupKeys(InputManager inputManager) {
151154
inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
152155
inputManager.addMapping("TogglePhysics", new KeyTrigger(KeyInput.KEY_P));
153156
inputManager.addMapping("ToggleInfo", new KeyTrigger(KeyInput.KEY_I));
157+
inputManager.addMapping("Hamster", new KeyTrigger(KeyInput.KEY_H));
154158
inputManager.addListener(this, "Lefts");
155159
inputManager.addListener(this, "Rights");
156160
inputManager.addListener(this, "Ups");
@@ -159,6 +163,7 @@ private void setupKeys(InputManager inputManager) {
159163
inputManager.addListener(this, "Reset");
160164
inputManager.addListener(this, "TogglePhysics");
161165
inputManager.addListener(this, "ToggleInfo");
166+
inputManager.addListener(this, "Hamster");
162167
}
163168

164169
public void onAction(String binding, boolean value, float tpf) {
@@ -215,6 +220,29 @@ public void onAction(String binding, boolean value, float tpf) {
215220
busControl.setKinematic(true);
216221
autopilot = true;
217222
}
223+
} else if (binding.equals("Hamster")) {
224+
if (value) {
225+
Node hamster = (Node) this.app.getAssetManager().loadModel("Models/tentaclehamster/tentaclehamster.j3o");
226+
String name = "ObjectHamster" + hamstercount++;
227+
hamster.setLocalTranslation(busNode.getWorldTranslation().add(busControl.getForwardVector(null).multLocal(20)));
228+
hamster.setLocalRotation(busNode.getWorldRotation().opposite());
229+
hamster.setLocalScale(2);
230+
ServerAppState serverAppState = app.getStateManager().getState(ServerAppState.class);
231+
if (serverAppState == null || serverAppState.getServer() == null) {
232+
hamster.setName(name);
233+
this.app.getRootNode().attachChild(hamster);
234+
} else {
235+
hamster.setName("Hamster");
236+
Vector3f location = hamster.getLocalTranslation().clone();
237+
Quaternion rotation = hamster.getLocalRotation().clone();
238+
hamster.removeFromParent();
239+
hamster.setLocalTransform(Transform.IDENTITY);
240+
hamster = serverAppState.addObservedSpatial(hamster, location, rotation, name, "Models/tentaclehamster/tentaclehamster.j3o");
241+
serverAppState.addObjects(Collections.singletonList((Spatial) hamster));
242+
}
243+
BulletAppState bulletAppState = this.app.getStateManager().getState(BulletAppState.class);
244+
hamster.addControl(new HamsterControl(bulletAppState, serverAppState, name));
245+
}
218246
} else if (autopilot) {
219247
autopilot = false;
220248
busControl.setKinematic(false);

src/busline3d/appstate/WorldAppState.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ public void initialize(AppStateManager stateManager, Application app) {
8585
passengerControl = new PassengerControl(geometries, this, false);
8686
busNode.addControl(passengerControl);
8787

88-
Node hamster = (Node) this.app.getAssetManager().loadModel("Models/tentaclehamster/tentaclehamster.j3o");
89-
hamster.setName("Hamster");
90-
hamster.setLocalTranslation(0, 30, 0);
91-
hamster.setLocalScale(2);
92-
rootNode.attachChild(hamster);
93-
9488
this.app.getStateManager().attach(sunAppState);
9589
}
9690

src/busline3d/command/AnimationCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public boolean execute() {
2828
if (node == null) {
2929
return false;
3030
}
31-
node = (Node) node.getChild(object);
31+
node = (Node) ((Node) node.getChild(0)).getChild(0);
3232
AnimControl anim = node.getControl(AnimControl.class);
3333

3434
AnimChannel channel;

src/busline3d/control/HamsterControl.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package busline3d.control;
22

33
import busline3d.message.AnimationMessage;
4-
import busline3d.message.PassengerBusMessage;
54
import com.jme3.animation.AnimChannel;
65
import com.jme3.animation.AnimControl;
76
import com.jme3.animation.AnimEventListener;
@@ -28,28 +27,31 @@ public class HamsterControl extends AbstractControl implements AnimEventListener
2827
private BulletAppState bulletAppState;
2928
private ServerAppState serverAppState;
3029
private BetterCharacterControl physicsCharacter;
31-
private Vector3f viewDirection = new Vector3f(0, 0, 1);
30+
private Vector3f viewDirection;
3231
private AnimChannel channel;
3332
private Node hamster;
3433
private float brownNoise;
34+
private String name;
3535

36-
public HamsterControl(BulletAppState bulletAppState, ServerAppState serverAppState) {
36+
public HamsterControl(BulletAppState bulletAppState, ServerAppState serverAppState, String name) {
3737
this.bulletAppState = bulletAppState;
3838
this.serverAppState = serverAppState;
39+
this.name = name;
3940
}
4041

4142
@Override
4243
public void setSpatial(Spatial spatial) {
4344
super.setSpatial(spatial);
4445
hamster = (Node) spatial;
46+
viewDirection = hamster.getLocalRotation().mult(new Vector3f(0, 0, 1));
4547
Node hamsternode = (Node) hamster.getChild("Hamster");
4648
if (hamsternode.getChild("Hamster") instanceof Node) {
4749
hamsternode = (Node) hamsternode.getChild("Hamster");
4850
}
4951
AnimControl anim = hamsternode.getControl(AnimControl.class);
5052
anim.addListener(this);
5153
channel = anim.createChannel();
52-
playAnim("walk");
54+
playAnim("walk", 1);
5355
physicsCharacter = hamster.getControl(BetterCharacterControl.class);
5456
if (physicsCharacter == null) {
5557
physicsCharacter = new BetterCharacterControl(7f, 20f, 1000f);
@@ -59,11 +61,12 @@ public void setSpatial(Spatial spatial) {
5961
if (bulletAppState != null) {
6062
bulletAppState.getPhysicsSpace().add(physicsCharacter);
6163
}
64+
physicsCharacter.setViewDirection(viewDirection);
6265
}
6366

6467
@Override
6568
public Control cloneForSpatial(Spatial spatial) {
66-
HamsterControl newcontrol = new HamsterControl(bulletAppState, serverAppState);
69+
HamsterControl newcontrol = new HamsterControl(bulletAppState, serverAppState, name);
6770
newcontrol.setSpatial(spatial);
6871
return newcontrol;
6972
}
@@ -88,21 +91,22 @@ protected void controlRender(RenderManager rm, ViewPort vp) {
8891
public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
8992
if (physicsCharacter.isOnGround() && Math.random() < 0.1) {
9093
physicsCharacter.jump();
91-
playAnim("jump");
92-
channel.setSpeed(0.333f);
94+
playAnim("jump", 0.333f);
95+
9396
} else {
94-
playAnim("walk");
97+
playAnim("walk", 1);
9598
}
9699
}
97100

98101
public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
99102
}
100103

101-
private void playAnim(String animName) {
102-
if (serverAppState != null) {
103-
serverAppState.getServer().broadcast(new AnimationMessage("Hamster", animName, 0.333f).setReliable(true));
104+
private void playAnim(String animName, float speed) {
105+
if (serverAppState != null && serverAppState.getServer() != null) {
106+
serverAppState.getServer().broadcast(new AnimationMessage(name, animName, speed).setReliable(true));
104107
}
105108
channel.setAnim(animName);
109+
channel.setSpeed(speed);
106110
channel.setLoopMode(LoopMode.DontLoop);
107111
}
108112
}

0 commit comments

Comments
 (0)