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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
package jme3test.helloworld;
import com.jme3.app.SimpleApplication;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
/** Sample 4 - how to trigger repeating actions from the main update loop.
* In this example, we make the player character rotate. */
public class HelloLoop extends SimpleApplication {
final static long timeElapsedMax = 20;
protected Geometry objectBlue;
protected Geometry objectGreen;
protected Integer isGrow = 1;
protected Integer objectGrowNumberCurrent = 0;
protected Integer objectScaleLimitMax = 50;
protected BitmapText labelText;
protected Integer loopNumberFromBegin = 0;
protected long timeApplicationCurrent;
protected long timeApplicationCheckPoint;
protected float objectGrowPercent = 2;
protected float coefGrown;
protected float coefReduce;
protected String textTitle;
public static void main(String[] args) {
HelloLoop app = new HelloLoop();
app.start();
}
@Override
public void simpleInitApp() {
//Création du cube blue
Box b = new Box(Vector3f.ZERO, 1, 1, 1);
objectBlue = new Geometry("blue cube", b);
Material mat = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
objectBlue.setMaterial(mat);
objectBlue.move(new Vector3f(-2, 0, 0));
rootNode.attachChild(objectBlue);
//Création du cube vert
Box b2 = new Box(Vector3f.ZERO, 1, 1, 1);
objectGreen = new Geometry("green cube2", b2);
Material mat2 = new Material(assetManager,
"Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Green);
objectGreen.setMaterial(mat2);
objectGreen.move(new Vector3f(2, 0, 0));
rootNode.attachChild(objectGreen);
// Display a line of text with a default font
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
labelText = new BitmapText(guiFont, false);
labelText.setSize(guiFont.getCharSet().getRenderedSize());
labelText.setText("Hello World");
labelText.setLocalTranslation(300, labelText.getLineHeight(), 0);
guiNode.attachChild(labelText);
//Initialisation des coefficients de redimensionnement
coefGrown = 1 + (objectGrowPercent / 100);
coefReduce = 1 / coefGrown;
//Initialisation de la mesure du temps:
timeApplicationCheckPoint = System.currentTimeMillis();
}
/* This is the update loop */
@Override
public void simpleUpdate(float tpf) {
//Récupération du temps système
timeApplicationCurrent = System.currentTimeMillis();
//Détermination du temps écoulé depuis la dernière boucle
long timeElapsed = timeApplicationCurrent - timeApplicationCheckPoint;
//Si le temps écoulé est suffisant, réalisation des tâches suivantes
if (timeElapsed >= HelloLoop.timeElapsedMax) {
//Incrémentaton du compteur de boucle
loopNumberFromBegin += 1;
textTitle = "Boucle n°" + loopNumberFromBegin.toString();
//Doit-on agrandir ou diminuer le cube ?
switch (isGrow) {
//Agrandissement du cube
case 1:
objectBlue.scale(coefGrown);
labelText.setText(textTitle + " - " + "Agrandissement");
break;
//Réduction du cube
case -1:
objectBlue.scale(coefReduce);
labelText.setText(textTitle + " - " + "Réduction");
break;
}
//Incrémentation du nombe de fois qu'on vient de réaliser
//la même opréation
objectGrowNumberCurrent += 1;
//A-t-on réalisé suffisament de fois la même opréation ?
if (objectGrowNumberCurrent == objectScaleLimitMax) {
//On inverse le sens (agrandissement <-> réduction
isGrow *= -1;
//On réinitialise le compteur d'opération
objectGrowNumberCurrent = 0;
}
//Enregistrement du temps écoulé
timeApplicationCheckPoint = timeApplicationCurrent;
}
objectBlue.rotate(2 * tpf, 0, 2 * tpf);
objectGreen.rotate(-2 * tpf, 0, -2 * tpf);
}
} |
Partager