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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| package jme3test.guligames_package; import com.jme3.app.SimpleApplication; import com.jme3.asset.TextureKey; import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CollisionShape; import com.jme3.bullet.control.RigidBodyControl; import com.jme3.bullet.util.CollisionShapeFactory; import com.jme3.font.BitmapText; import com.jme3.input.MouseInput;
import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.material.Material;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Sphere; import com.jme3.scene.shape.Sphere.TextureMode; import com.jme3.texture.Texture;
import com.jme3.input.controls.ActionListener;
import com.jme3.light.DirectionalLight; import com.jme3.math.ColorRGBA; import com.jme3.math.Vector3f; import com.jme3.scene.Spatial; import com.jme3.scene.Node;
public class guligames extends SimpleApplication{ private BulletAppState bulletAppState; private RigidBodyControl landscape; private RigidBodyControl guliColli; private Spatial gulicourt; private Spatial guli; public static void main(String args[]) { guligames app = new guligames(); app.start(); } Material ball_mat; private RigidBodyControl ball_phy; private static final Sphere sphere; static { sphere = new Sphere(16, 16, 0.2f, true, false); sphere.setTextureMode(TextureMode.Projected); } @Override public void simpleInitApp() { viewPort.setBackgroundColor(new ColorRGBA(0f, 0f, 0f, 1f)); bulletAppState = new BulletAppState(); stateManager.attach(bulletAppState);
guiNode.detachAllChildren(); guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); BitmapText helloText = new BitmapText(guiFont, false); helloText.setSize(guiFont.getCharSet().getRenderedSize()); helloText.setText("Soccer Game"); helloText.setLocalTranslation(300, helloText.getLineHeight(), 0); guiNode.attachChild(helloText); gulicourt = assetManager.loadModel("Models/guliplace/guliplace.mesh.xml"); gulicourt.scale(4.0f, 4.0f, 4.0f); gulicourt.setLocalTranslation(2.0f, -0.5f, -2.0f); CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) gulicourt); landscape = new RigidBodyControl(sceneShape, 0); gulicourt.addControl(landscape); bulletAppState.getPhysicsSpace().add(landscape); rootNode.attachChild(gulicourt); guli = assetManager.loadModel("Models/gelung2/gelung2.mesh.xml"); guli.scale(4.0f, 4.0f, 4.0f); guli.setLocalTranslation(1.5f, 0.1f, -0.1f); CollisionShape keeperShape = CollisionShapeFactory.createMeshShape((Node) guli); guliColli = new RigidBodyControl(keeperShape, 0); guli.addControl(guliColli); bulletAppState.getPhysicsSpace().add(guliColli); rootNode.attachChild(guli); DirectionalLight sun = new DirectionalLight(); sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f)); rootNode.addLight(sun); cam.setLocation(new Vector3f(2f, 6f, 8f)); cam.lookAt(new Vector3f(1, 1, 0), Vector3f.UNIT_Y); inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT)); inputManager.addListener(actionListener, "shoot"); initMaterials(); initCrossHairs(); } private ActionListener actionListener = new ActionListener() { public void onAction(String name, boolean keyPressed, float tpf) { if (name.equals("shoot") && !keyPressed) { makeCannonBall(); } } }; public void initMaterials() { ball_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); TextureKey key2 = new TextureKey("Models/guli/yellow.jpg"); key2.setGenerateMips(true); Texture tex2 = assetManager.loadTexture(key2); ball_mat.setTexture("ColorMap", tex2); } public void makeCannonBall() { Geometry ball_geo = new Geometry("cannon ball", sphere); ball_geo.setMaterial(ball_mat); rootNode.attachChild(ball_geo); ball_geo.setLocalTranslation(cam.getLocation()); ball_phy = new RigidBodyControl(1f); ball_geo.addControl(ball_phy); bulletAppState.getPhysicsSpace().add(ball_phy); ball_phy.setLinearVelocity(cam.getDirection().mult(18)); } protected void initCrossHairs() { guiNode.detachAllChildren(); guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt"); BitmapText ch = new BitmapText(guiFont, false); ch.setSize(guiFont.getCharSet().getRenderedSize() * 2); ch.setText("+"); ch.setLocalTranslation( settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, settings.getHeight() / 2 + ch.getLineHeight() / 2, 0); guiNode.attachChild(ch); } }
|