From 63902a914389ee82d2c266eefb374bfb8bf2cb64 Mon Sep 17 00:00:00 2001 From: "Raybz@Raybz" Date: Fri, 21 Jun 2013 12:48:46 +0200 Subject: [PATCH] added player-node, fixed weaponProjectileControl, added star --- .../controls/PlayerCamControl.java | 2 +- .../controls/WeaponProjectileControl.java | 82 +++++-------- .../org/wyrez/shootingstars/game/Player.java | 89 ++++++++++++++ .../org/wyrez/shootingstars/mesh/Star.java | 111 ++++++++++++++++++ .../wyrez/shootingstars/states/GameState.java | 20 +--- 5 files changed, 234 insertions(+), 70 deletions(-) create mode 100644 ShootingStars/src/org/wyrez/shootingstars/game/Player.java create mode 100644 ShootingStars/src/org/wyrez/shootingstars/mesh/Star.java diff --git a/ShootingStars/src/org/wyrez/shootingstars/controls/PlayerCamControl.java b/ShootingStars/src/org/wyrez/shootingstars/controls/PlayerCamControl.java index 24854a2..eff85e3 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/controls/PlayerCamControl.java +++ b/ShootingStars/src/org/wyrez/shootingstars/controls/PlayerCamControl.java @@ -36,7 +36,7 @@ public class PlayerCamControl extends BaseControl { @Override public void update(float tpf) { if (spatial.getUserData(UserDataKeys.RUNNING)) { - camera.setLocation(spatial.getLocalTranslation()); + camera.setLocation(spatial.getLocalTranslation().add(10f,10f,10f)); camera.setRotation(spatial.getLocalRotation()); } } diff --git a/ShootingStars/src/org/wyrez/shootingstars/controls/WeaponProjectileControl.java b/ShootingStars/src/org/wyrez/shootingstars/controls/WeaponProjectileControl.java index ef03ee9..013737e 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/controls/WeaponProjectileControl.java +++ b/ShootingStars/src/org/wyrez/shootingstars/controls/WeaponProjectileControl.java @@ -16,13 +16,6 @@ */ package org.wyrez.shootingstars.controls; -import com.jme3.asset.AssetManager; -import com.jme3.input.InputManager; -import com.jme3.input.KeyInput; -import com.jme3.input.MouseInput; -import com.jme3.input.controls.ActionListener; -import com.jme3.input.controls.KeyTrigger; -import com.jme3.input.controls.MouseButtonTrigger; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import com.jme3.scene.Node; @@ -34,18 +27,16 @@ import org.wyrez.shootingstars.helper.UserDataKeys; * * @author Snowsun */ -public class WeaponProjectileControl extends BaseControl implements ActionListener { +public class WeaponProjectileControl extends BaseControl { - private AssetManager assetManager; private Node rootNode; - private InputManager inputManager; private Spatial player; + private boolean isVisible; - public WeaponProjectileControl(AssetManager assetManager, Node rootNode, InputManager inputManager) { - this.assetManager = assetManager; + public WeaponProjectileControl(Node rootNode, Spatial player) { this.rootNode = rootNode; - this.inputManager = inputManager; - initMappings(); + this.player = player; + this.isVisible = false; } @Override @@ -56,55 +47,40 @@ public class WeaponProjectileControl extends BaseControl implements ActionListen @Override protected void controlUpdate(float tpf) { if (player.getUserData(UserDataKeys.SHOOTING)) { - shoot(); + if (!isVisible) { + spatial.setCullHint(Spatial.CullHint.Never); + isVisible = true; + } +// shoot(); } else { - rootNode.detachChild(spatial); + if (isVisible) { + spatial.setCullHint(Spatial.CullHint.Always); + isVisible = false; + } } } public Control cloneForSpatial(Spatial spatial) { - WeaponProjectileControl control = new WeaponProjectileControl(assetManager, rootNode, inputManager); + WeaponProjectileControl control = new WeaponProjectileControl(rootNode, player); spatial.addControl(control); return control; } - public void onAction(String name, boolean isPressed, float tpf) { - if (name.equals("PLAYER_Mouse_Left_Click") && isPressed) { - rootNode.attachChild(spatial); - } else if (name.equals("PLAYER_Mouse_Left_Click") && !isPressed) { - rootNode.detachChild(spatial); - } - } - - public void setPlayer(Spatial player) { - this.player = player; - } - - public Spatial getPlayer() { - return this.player; - } - - private void initMappings() { - inputManager.addMapping("PLAYER_Mouse_Left_Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT), - new KeyTrigger(KeyInput.KEY_DOWN)); - inputManager.addListener(this, new String[]{"PLAYER_Mouse_Left_Click"}); - } - /* * Fires a projectile */ - private void shoot() { - spatial.setLocalTranslation(player.getLocalTranslation().add(0, 0, -4)); - - Vector3f vectorDifference = new Vector3f(player.getLocalTranslation().subtract(player.getWorldTranslation())); - spatial.setLocalTranslation(vectorDifference.addLocal(player.getLocalTranslation())); - - Quaternion worldDiff = new Quaternion(player.getLocalRotation().subtract(player.getWorldRotation())); - spatial.setLocalRotation(worldDiff.addLocal(player.getLocalRotation())); - - spatial.move(player.getLocalRotation().getRotationColumn(2).mult(-3.5f)); -// spatial.move(player.getLocalRotation().getRotationColumn(1).mult(0f)); -// spatial.move(player.getLocalRotation().getRotationColumn(0).mult(0)); -// spatial.rotate(0f, FastMath.PI, 0); - } +// private void shoot() { +// spatial.setLocalTranslation(player.getLocalTranslation().add(0, 0, -4)); +// +// Vector3f vectorDifference = new Vector3f(player.getLocalTranslation().subtract(player.getWorldTranslation())); +// spatial.setLocalTranslation(vectorDifference.addLocal(player.getLocalTranslation())); +// +// Quaternion worldDiff = new Quaternion(player.getLocalRotation().subtract(player.getWorldRotation())); +// spatial.setLocalRotation(worldDiff.addLocal(player.getLocalRotation())); +// +// spatial.move(player.getLocalRotation().getRotationColumn(2).mult(3.5f)); +// spatial.move(player.getLocalRotation().getRotationColumn(1).mult(0.1f)); +// spatial.move(player.getLocalRotation().getRotationColumn(0).mult(0.1f)); +//// spatial.rotate(0f, FastMath.PI, 0); +// } } diff --git a/ShootingStars/src/org/wyrez/shootingstars/game/Player.java b/ShootingStars/src/org/wyrez/shootingstars/game/Player.java new file mode 100644 index 0000000..c8c817c --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/game/Player.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.wyrez.shootingstars.game; + +import com.jme3.asset.AssetManager; +import com.jme3.input.InputManager; +import com.jme3.light.AmbientLight; +import com.jme3.math.ColorRGBA; +import com.jme3.renderer.Camera; +import com.jme3.renderer.ViewPort; +import com.jme3.scene.Geometry; +import com.jme3.scene.Node; +import com.jme3.scene.Spatial; +import com.jme3.scene.shape.Box; +import org.wyrez.shootingstars.controls.PlayerCamControl; +import org.wyrez.shootingstars.controls.PlayerMouseControl; +import org.wyrez.shootingstars.controls.PlayerMoveControl; +import org.wyrez.shootingstars.controls.PlayerShootControl; +import org.wyrez.shootingstars.controls.WeaponProjectileControl; +import org.wyrez.shootingstars.data.AudioDataManager; +import org.wyrez.shootingstars.factories.Materials; +import org.wyrez.shootingstars.gui.GameGUI; +import org.wyrez.shootingstars.helper.UserDataKeys; +import org.wyrez.shootingstars.states.StateManager; + +/** + * + * @author Darth Affe + */ +public class Player extends Node { + + private Spatial weapon; + private Spatial projectile; + + public Player(Node rootNode, StateManager stateManager, AssetManager assetManager, + ViewPort viewPort, GameGUI gui, InputManager inputManager, Camera camera, + AudioDataManager audioDataManager) { + super("Player"); + initPlayer(inputManager, camera, audioDataManager, gui); + //TODO implement +// initWeapon(assetManager); +// initProjectile(rootNode); +// addLight(); + } + + private void initPlayer(InputManager inputManager, Camera camera, + AudioDataManager audioDataManager, GameGUI gui) { + this.setUserData(UserDataKeys.RUNNING, false); + this.setUserData(UserDataKeys.SHOOTING, false); + this.addControl(new PlayerMouseControl(inputManager, camera)); + this.addControl(new PlayerMoveControl(audioDataManager)); + this.addControl(new PlayerShootControl(inputManager, gui)); + this.addControl(new PlayerCamControl(camera)); + } + + private void initWeapon(AssetManager assetManager) { + weapon = assetManager.loadModel("Models/LaserGun.j3o"); + weapon.move(10f, 0f, 0f); + this.attachChild(weapon); + } + + private void initProjectile(Node rootNode) { + projectile = new Geometry("weapon", new Box(0.01f, 0.01f, 2f)); + projectile.addControl(new WeaponProjectileControl(rootNode, this)); + projectile.setMaterial(Materials.WEAPON.create()); + + this.attachChild(projectile); + } + + private void addLight() { + AmbientLight ambient = new AmbientLight(); + ambient.setColor(ColorRGBA.White); + this.addLight(ambient); + } +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/mesh/Star.java b/ShootingStars/src/org/wyrez/shootingstars/mesh/Star.java new file mode 100644 index 0000000..2379b2f --- /dev/null +++ b/ShootingStars/src/org/wyrez/shootingstars/mesh/Star.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2013 Darth Affe and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.wyrez.shootingstars.mesh; + +import com.jme3.math.FastMath; +import com.jme3.math.Vector3f; +import com.jme3.scene.Mesh; +import com.jme3.scene.VertexBuffer; +import com.jme3.util.BufferUtils; + +/** + * + * @author Darth Affe + */ +public class Star extends Mesh { + + private static final short[] GEOMETRY_INDICES_DATA = { + 0, 1, 2,/**/ 0, 2, 3,/**/ 0, 3, 4,/**/ 0, 4, 5,/**/ 0, 5, 6,/**/ 0, 6, 7,/**/ + 0, 7, 8,/**/ 0, 8, 9,/**/ 0, 9, 10,/**/ 0, 10, 11,/**/ 0, 11, 12,/**/ 0, 12, 1,/**/ + 13, 2, 1,/**/ 13, 3, 2,/**/ 13, 4, 3,/**/ 13, 5, 4,/**/ 13, 6, 5,/**/ 13, 7, 6,/**/ + 13, 8, 7,/**/ 13, 9, 8,/**/ 13, 10, 9,/**/ 13, 11, 10,/**/ 13, 12, 11,/**/ 13, 1, 12}; + //TODO fix + private static final float[] GEOMETRY_TEXTURE_DATA = { + 0.5f, 0.5f, /**/ 0.5f, 0f,/**/ 0.75f, 0.25f,/**/ 1f, 0f,/**/ 0.75f, 0.5f,/**/ 1f, 1f,/**/ 0.75f, 0.75f, + 1f, 0.5f,/**/ 0.25f, 0.75f,/**/ 0f, 1f,/**/ 0f, 0.5f,/**/ 0f, 0f,/**/ 0.25f, 0.25f,/**/ 0f, 0f + }; + private Vector3f center; + private float radius; + private float height; + private float factor; + + public Star(float radius, float height, float factor) { + this(Vector3f.ZERO, radius, height, factor); + } + + public Star(Vector3f center, float radius, float height, float factor) { + super(); + updateGeometry(center, radius, height, factor); + } + + /** + * Empty constructor for serialization only. Do not use! + */ + public Star() { + super(); + } + + @Override + public Star clone() { + return new Star(center.clone(), radius, height, factor); + } + + protected final void updateGeometry(Vector3f center, float radius, float height, float factor) { + this.center = center; + this.radius = radius; + this.height = height; + this.factor = factor; + updateGeometryIndices(); + updateGeometryVertices(); + updateGeometryTextures(); + } + + protected void updateGeometryIndices() { + if (getBuffer(VertexBuffer.Type.Index) == null) { + setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createShortBuffer(GEOMETRY_INDICES_DATA)); + } + } + + protected void updateGeometryTextures() { + if (getBuffer(VertexBuffer.Type.TexCoord) == null) { + setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(GEOMETRY_TEXTURE_DATA)); + } + } + + protected void updateGeometryVertices() { + Vector3f[] vertices = computeVertices(); + setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices)); + updateBound(); + } + + protected Vector3f[] computeVertices() { + Vector3f[] vertices = new Vector3f[14]; + vertices[0] = new Vector3f(new Vector3f(center.x, center.y - (height / 2f), center.z)); + vertices[13] = new Vector3f(new Vector3f(center.x, center.y + (height / 2f), center.z));; + //outer + for (int i = 0; i < 12; i += 2) { + vertices[i + 1] = new Vector3f(center.x + radius * FastMath.cos(i * FastMath.PI / 6), + center.y, (center.z + radius * FastMath.sin(i * FastMath.PI / 6))); + } + //inner + for (int i = 1; i < 12; i += 2) { + vertices[i + 1] = new Vector3f(center.x + (radius * factor) * FastMath.cos(i * FastMath.PI / 6), + center.y, (center.z + (radius * factor) * FastMath.sin(i * FastMath.PI / 6))); + } + return vertices; + } +} diff --git a/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java b/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java index f6e36d3..231e2be 100644 --- a/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java +++ b/ShootingStars/src/org/wyrez/shootingstars/states/GameState.java @@ -41,6 +41,7 @@ import org.wyrez.shootingstars.factories.Materials; import org.wyrez.shootingstars.game.Cinema; import org.wyrez.shootingstars.game.GameSettings; import org.wyrez.shootingstars.game.Ground; +import org.wyrez.shootingstars.game.Player; import org.wyrez.shootingstars.gui.GameGUI; import org.wyrez.shootingstars.gui.listener.GameListener; import org.wyrez.shootingstars.helper.UserDataKeys; @@ -67,8 +68,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL private GameGUI gui; private Cinema cinema; private Ground ground; - private Spatial player; - private Spatial weapon; + private Node player; private boolean isRunning = false; public GameState(Node rootNode, Screen screen, StateManager stateManager, @@ -111,18 +111,8 @@ public class GameState extends AbstractAppState implements GameListener, ActionL } public void loadPlayer() { - player = new Geometry("player", new Box(Vector3f.ZERO, 1f, 1f, 1f)); //TODO start location? - player.setMaterial(Materials.UNSHADED.create()); - player.setUserData(UserDataKeys.RUNNING, false); - player.setUserData(UserDataKeys.SHOOTING, false); - player.addControl(new PlayerMouseControl(inputManager, camera)); - player.addControl(new PlayerMoveControl(audioDataManager)); - player.addControl(new PlayerShootControl(inputManager, gui)); - player.addControl(new PlayerCamControl(camera)); - - weapon = new Geometry("weapon", new Box(0.01f, 0.01f, 2f)); - weapon.addControl(new WeaponProjectileControl(rootNode, player)); - weapon.setMaterial(Materials.WEAPON.create()); + player = new Player(rootNode, stateManager, assetManager, viewPort, gui, + inputManager, camera, audioDataManager); } private void setRunning(boolean running) { @@ -142,7 +132,6 @@ public class GameState extends AbstractAppState implements GameListener, ActionL rootNode.attachChild(cinema); rootNode.attachChild(ground); rootNode.attachChild(player); - rootNode.attachChild(weapon); gui.setWait(); gui.attach(); } @@ -150,7 +139,6 @@ public class GameState extends AbstractAppState implements GameListener, ActionL @Override public void stateDetached(AppStateManager stateManager) { gui.detach(); - rootNode.detachChild(weapon); rootNode.detachChild(player); rootNode.detachChild(ground); rootNode.detachChild(cinema);