spawned Stars (not tested), refactored some stuff

This commit is contained in:
Raybz@Raybz 2013-06-21 14:58:13 +02:00
parent 0d2ec02510
commit b8fa020781
11 changed files with 122 additions and 115 deletions

View File

@ -23,6 +23,7 @@ import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.util.Random;
import org.wyrez.shootingstars.data.AudioDataManager;
import org.wyrez.shootingstars.helper.ColorHelper;
/**
*
@ -53,7 +54,7 @@ public class GroundGlowControl extends BaseControl {
if (audioDataManager.isFlash() && glowTimer <= 0f) {
if (random.nextBoolean()) {
glowTimer = GLOW_DURATION;
color = calcColor(audioDataManager.getFlashData());
color = ColorHelper.calcColor(audioDataManager.getFlashData());
}
}
@ -70,28 +71,6 @@ public class GroundGlowControl extends BaseControl {
}
}
private ColorRGBA calcColor(float value) {
int r, g, b;
if (value < 0.25f) {
r = 0;
g = Math.round(255 * value / 0.25f);
b = 255;
} else if (value < 0.5f) {
r = 0;
g = 255;
b = Math.round(255 * ((0.25f - (value - 0.25f)) / 0.25f));
} else if (value < 0.75f) {
g = 255;
r = Math.round(255 * (value - 0.5f) / 0.25f);
b = 0;
} else {
g = Math.round(255 * ((0.25f - (value - 0.75f)) / 0.25f));
r = 255;
b = 0;
}
return new ColorRGBA((float) r / 255f, (float) g / 255f, (float) b / 255f, 1f);
}
public Control cloneForSpatial(Spatial spatial) {
GroundGlowControl control = new GroundGlowControl(audioDataManager);
control.setSpatial(spatial);

View File

@ -16,6 +16,7 @@
*/
package org.wyrez.shootingstars.controls;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
@ -26,21 +27,21 @@ import org.wyrez.shootingstars.helper.UserDataKeys;
* @author Darth Affe
*/
public class PlayerCamControl extends BaseControl {
private Camera camera;
public PlayerCamControl(Camera camera) {
this.camera = camera;
}
@Override
public void update(float tpf) {
if (spatial.getUserData(UserDataKeys.RUNNING)) {
camera.setLocation(spatial.getLocalTranslation().add(10f,10f,10f));
camera.setLocation(spatial.getLocalTranslation());
camera.setRotation(spatial.getLocalRotation());
}
}
public Control cloneForSpatial(Spatial spatial) {
PlayerCamControl control = new PlayerCamControl(camera);
control.setSpatial(spatial);

View File

@ -17,8 +17,10 @@
package org.wyrez.shootingstars.controls;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import org.wyrez.shootingstars.helper.ColorHelper;
import org.wyrez.shootingstars.helper.UserDataKeys;
/**
@ -27,35 +29,43 @@ import org.wyrez.shootingstars.helper.UserDataKeys;
*/
public class StarPointControl extends BaseControl {
private int points = 100; //Todo Change StartPoint at Spawn
private static final float MAX_POINTS = 100f;
private static final float HIT_TIMER = 10f;
/**/
private float points;
private Spatial player;
private ColorRGBA starColor;
public StarPointControl(Spatial player) {
private ColorRGBA color;
private float timer;
public StarPointControl(Spatial player, float value) {
this.player = player;
this.points = (int) Math.round(MAX_POINTS * value);
this.timer = HIT_TIMER;
this.color = ColorHelper.calcColor(value / MAX_POINTS);
((Geometry) spatial).getMaterial().setColor("Color", color);
}
@Override
protected void controlUpdate(float tpf) {
if (spatial.getUserData(UserDataKeys.HITTED)) {
int pointsToAdd = Integer.parseInt(player.getUserData(UserDataKeys.POINTS).toString()) + points;
player.setUserData(UserDataKeys.POINTS, pointsToAdd);
} else {
points -= tpf / 2;
if(points < 75) {
starColor = new ColorRGBA(0.5f, 0.5f, 0.5f, 1f);
//Todo Set color to spatial
player.setUserData(UserDataKeys.POINTS, (Integer) (player.getUserData(UserDataKeys.POINTS))
+ (int) Math.round(points));
} else if (timer <= 0f) {
if (points > 1f) {
points -= 2f * tpf;
color = ColorHelper.calcColor(points / MAX_POINTS);
} else if (points < 1f) {
points = 1f;
color = ColorHelper.calcColor(points / MAX_POINTS);
}
} else {
timer -= tpf;
}
}
public Control cloneForSpatial(Spatial spatial) {
StarPointControl control = new StarPointControl(player);
StarPointControl control = new StarPointControl(player, (float) points);
spatial.addControl(control);
return control;
}
public int getPoints() {
return this.points;
}
}

View File

@ -45,11 +45,13 @@ public class AudioDataManager {
// private float[] movementData;
private float[] flashData;
private float[] groundMoveData;
private float[] spawnData;
private float peakAverageFlash;
private float indexFactor;
private float bpm;
private float currentTime;
private float lastTimeSync;
private int lastSpawnDataPollIndex;
public void AudioDataManager() {
}
@ -118,7 +120,18 @@ public class AudioDataManager {
public void analyseMidBand() {
audioProcessorMidBand.calculate();
audioProcessorMidBand.cutFastPeaks(MIN_PEAK_DIFF);
spawnData = audioProcessorMidBand.getPeaks();
float minValue = audioProcessorMidBand.getPeakAverage() * 0.5f;
float maxValue = audioProcessorHighBand.getPeakAverage() * 2f;
for (int i = 0; i < spawnData.length; i++) {
if (spawnData[i] > maxValue) {
spawnData[i] = maxValue;
} else if (spawnData[i] < minValue) {
spawnData[i] = 0f;
}
}
SampleHelper.normalize(spawnData, 0f, 1f);
audioProcessorMidBand.clean();
audioProcessorMidBand = null;
@ -192,24 +205,32 @@ public class AudioDataManager {
}
public float getGroundMoveData() {
return groundMoveData[calcIndex(flashData.length)];
return groundMoveData[calcIndex(groundMoveData.length)];
}
public float getMovementData() {
return 0.25f;
// System.out.println(currentTime + " * " + indexFactor + " = " + calcIndex() + " --> " + movementData[calcIndex()]);
// return movementData[calcIndex()];
}
public float getSpeedData() {
return speedData[calcIndex(flashData.length)];
return speedData[calcIndex(speedData.length)];
}
public float getMidData() {
return 0f; //TODO implement
}
public float getHighData() {
return 0f; //TODO implement
public float[] getSpawnData() {
int index = calcIndex(spawnData.length);
float[] data;
if (index == lastSpawnDataPollIndex) {
data = null;
} else if (index - 1 != lastSpawnDataPollIndex) {
data = new float[index - lastSpawnDataPollIndex];
for (int i = 0; i < data.length; i++) {
data[i] = spawnData[lastSpawnDataPollIndex + 1 + i];
}
return data;
} else {
data = new float[]{spawnData[index]};
}
lastSpawnDataPollIndex = index;
return data;
}
}

View File

@ -29,7 +29,8 @@ public enum Materials {
UNSHADED("Common/MatDefs/Misc/Unshaded.j3md", null, null, null),
HEX("Common/MatDefs/Light/Lighting.j3md", "Textures/Hex.png", null, null),
WEAPON("Common/MatDefs/Light/Lighting.j3md", null, ColorRGBA.White, ColorRGBA.Cyan),
GLOW("Common/MatDefs/Light/Lighting.j3md", null, ColorRGBA.White, ColorRGBA.Black);
GLOW("Common/MatDefs/Light/Lighting.j3md", null, ColorRGBA.White, ColorRGBA.Black),
STAR("Common/MatDefs/Misc/Unshaded.j3md", null, null, null);
private static AssetManager assetManager;
private String material;
private String texture;

View File

@ -41,14 +41,15 @@ public class GlowingHexPrism extends Node {
Material prismMat = Materials.HEX.create();
prismMat.setColor("Diffuse", ColorRGBA.Black);
prism = new Geometry("Prism " + position, new HexPrism(position, size, height));
prism = new Geometry("Prism " + position, new HexPrism(size, height));
prism.setMaterial(prismMat);
prism.setLocalTranslation(position);
this.attachChild(prism);
Material glowMat = Materials.GLOW.create();
glow = new Geometry("Prism " + position, new HexPrism(position.add(
0f, height, 0f), size * glowPercentage, height * 0.001f));
glow = new Geometry("Prism " + position, new HexPrism(size * glowPercentage, height * 0.001f));
glow.setMaterial(glowMat);
glow.setLocalTranslation(position.add(0f, height, 0f));
this.attachChild(glow);
glow.addControl(new GroundGlowControl(adm));

View File

@ -33,15 +33,15 @@ import org.wyrez.shootingstars.mesh.HexPrism;
* @author Darth Affe
*/
public class Ground extends Node {
public Ground(AudioDataManager adm) {
this(adm, 1000f);
}
public Ground(AudioDataManager adm, float radius) {
this(adm, radius, 0.1f);
}
public Ground(AudioDataManager adm, float radius, float factor) {
super("Ground");
generateHexGrid(adm, radius, factor);
@ -52,11 +52,11 @@ public class Ground extends Node {
private void generateHexGrid(AudioDataManager adm, float borderWidth, float factor) {
float size = borderWidth * factor;
int maxIterations = (int) (borderWidth / size) - 2;
Node prism = new GlowingHexPrism(new Vector3f(0f, -140f, 0f), size, 150f, 0.8f, adm);
prism.addControl(new GroundMoveControl(adm));
this.attachChild(prism);
float xcc = 0f;
float ycc = 0f;
float centerDistance = size * FastMath.sqrt(3f);
@ -65,13 +65,14 @@ public class Ground extends Node {
float yc = ycc - (float) iteration * centerDistance;
float dx = (FastMath.sqrt(3f) * centerDistance / 2f);
float dy = centerDistance / 2f;
for (int directions = 0; directions < 6; directions++) {
for (int steps = 0; steps < iteration; steps++) {
if ((iteration == maxIterations - 1)) {
Geometry hex = new Geometry("Prism_" + xc + " - " + yc,
new HexPrism(new Vector3f(xc, 0f, yc), size, 160f));
new HexPrism(size, 160f));
hex.setMaterial(Materials.HEX.create());
hex.setLocalTranslation(new Vector3f(xc, 0f, yc));
hex.addControl(new GroundBorderGlowControl(adm));
this.attachChild(hex);
} else {
@ -90,7 +91,7 @@ public class Ground extends Node {
}
}
}
private void addLight() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.White);

View File

@ -8,20 +8,19 @@ import tonegod.gui.core.Screen;
*/
public class ScreenHelper {
private static final float X = 1280;
private static final float Y = 720;
private static final float WIDTH = 1280;
private static final float HEIGHT = 720;
private Screen screen;
public ScreenHelper(Screen screen) {
this.screen = screen;
}
public float calcX(float value) {
return screen.getWidth() * ((100/X) * value) / 100;
return screen.getWidth() * ((100f / WIDTH) * value) / 100f;
}
public float calcY(float value) {
return screen.getHeight() * ((100/Y) * value) / 100;
return screen.getHeight() * ((100f / HEIGHT) * value) / 100f;
}
}

View File

@ -45,6 +45,7 @@ public class HexPrism extends Mesh {
*
*/
//TODO remove bottom for better performance
private static final short[] GEOMETRY_INDICES_DATA = {
0, 1, 2,/**/ 2, 3, 0,/**/ 0, 3, 4,/**/ 4, 5, 0,/**/ 0, 5, 6,/**/ 6, 1, 0, // bottom
//0, 1, 6,/**/ 6, 5, 0,/**/ 0, 5, 4,/**/ 4, 3, 0,/**/ 0, 3, 2,/**/ 2, 1, 0, // reverse bottom
@ -62,17 +63,12 @@ public class HexPrism extends Mesh {
0.5f, 0.5f, 0, 0.5f, 0.25f, 0, 0.75f, 0, 1, 0.5f, 0.75f, 1, 0.25f, 1,//bottom
0.5f, 0.5f, 0, 0.5f, 0.25f, 0, 0.75f, 0, 1, 0.5f, 0.75f, 1, 0.25f, 1 //top
};
private Vector3f center;
private float radius;
private float height;
public HexPrism(float radius, float height) {
this(Vector3f.ZERO, radius, height);
}
public HexPrism(Vector3f center, float radius, float height) {
super();
updateGeometry(center, radius, height);
updateGeometry(radius, height);
}
/**
@ -84,11 +80,10 @@ public class HexPrism extends Mesh {
@Override
public HexPrism clone() {
return new HexPrism(center.clone(), radius, height);
return new HexPrism(radius, height);
}
protected final void updateGeometry(Vector3f center, float radius, float height) {
this.center = center;
protected final void updateGeometry(float radius, float height) {
this.radius = radius;
this.height = height;
updateGeometryIndices();
@ -124,13 +119,13 @@ public class HexPrism extends Mesh {
protected Vector3f[] computeVertices() {
Vector3f[] vertices = new Vector3f[14];
//bottom
vertices[0] = center;
vertices[0] = new Vector3f(Vector3f.ZERO);
for (int i = 0; i < 6; i++) {
vertices[i + 1] = new Vector3f(vertices[0].x + radius * FastMath.cos(i * FastMath.PI / 3),
vertices[0].y, (vertices[0].z + radius * FastMath.sin(i * FastMath.PI / 3)));
}
//top
vertices[7] = center.add(0f, height, 0f);
vertices[7] = Vector3f.ZERO.add(0f, height, 0f);
for (int i = 0; i < 6; i++) {
vertices[i + 8] = new Vector3f(vertices[7].x + radius * FastMath.cos(i * FastMath.PI / 3),
vertices[7].y, (vertices[7].z + radius * FastMath.sin(i * FastMath.PI / 3)));

View File

@ -38,18 +38,13 @@ public class Star extends Mesh {
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);
updateGeometry(radius, height, factor);
}
/**
@ -61,11 +56,10 @@ public class Star extends Mesh {
@Override
public Star clone() {
return new Star(center.clone(), radius, height, factor);
return new Star(radius, height, factor);
}
protected final void updateGeometry(Vector3f center, float radius, float height, float factor) {
this.center = center;
protected final void updateGeometry(float radius, float height, float factor) {
this.radius = radius;
this.height = height;
this.factor = factor;
@ -94,17 +88,17 @@ public class Star extends Mesh {
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));;
vertices[0] = new Vector3f(new Vector3f(0f, -(height / 2f), 0f));
vertices[13] = new Vector3f(new Vector3f(0f, (height / 2f), 0f));
//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)));
vertices[i + 1] = new Vector3f(radius * FastMath.cos(i * FastMath.PI / 6),
0f, (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)));
vertices[i + 1] = new Vector3f((radius * factor) * FastMath.cos(i * FastMath.PI / 6),
0f, ((radius * factor) * FastMath.sin(i * FastMath.PI / 6)));
}
return vertices;
}

View File

@ -21,27 +21,18 @@ import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.controls.ActionListener;
import com.jme3.math.Vector3f;
import com.jme3.post.FilterPostProcessor;
import com.jme3.post.filters.BloomFilter;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
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.data.AudioDataManager;
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.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.game.StarManager;
import org.wyrez.shootingstars.gui.GameGUI;
import org.wyrez.shootingstars.gui.listener.GameListener;
import org.wyrez.shootingstars.helper.ScreenHelper;
@ -56,6 +47,10 @@ import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer;
*/
public class GameState extends AbstractAppState implements GameListener, ActionListener {
public static final float FIELD_RADIUS = 1000f;
public static final float FIELD_HEIGHT = 550f;
public static final float FIELD_LOWER_POS = 200f;
/**/
private StateManager stateManager;
private AssetManager assetManager;
private InputManager inputManager;
@ -63,6 +58,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
private Camera camera;
private GameSettings settings;
private AudioDataManager audioDataManager;
private StarManager starManager;
private final MediaPlayerFactory mediaPlayerFactory;
private DirectMediaPlayer mediaPlayer;
private Node rootNode;
@ -77,7 +73,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
InputManager inputManager, Camera camera, AudioDataManager audioDataManager, GameSettings settings, ScreenHelper screenHelper) {
this.rootNode = rootNode;
this.gui = new GameGUI(screen, this, assetManager,screenHelper);
this.gui = new GameGUI(screen, this, assetManager, screenHelper);
this.stateManager = stateManager;
inputManager.addListener(this, new String[]{"ESC"});
@ -86,6 +82,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
this.audioDataManager = audioDataManager;
this.assetManager = assetManager;
this.viewPort = viewPort;
this.starManager = new StarManager(audioDataManager, player);
mediaPlayerFactory = new MediaPlayerFactory("--no-video-title-show", "--quiet");
@ -93,7 +90,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
}
public void loadGround() {
ground = new Ground(audioDataManager);
ground = new Ground(audioDataManager, FIELD_RADIUS);
FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
fpp.addFilter(new BloomFilter(BloomFilter.GlowMode.Objects));
viewPort.addProcessor(fpp);
@ -107,14 +104,14 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
}
public void loadCinema() {
cinema = new Cinema(settings);
cinema = new Cinema(settings, FIELD_RADIUS);
cinema.move(0f, 160f, 0f);
}
public void loadPlayer() {
player = new Player(rootNode, stateManager, assetManager, viewPort, gui,
inputManager, camera, audioDataManager);
gui.setPlayer(player);
}
@ -135,6 +132,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
rootNode.attachChild(cinema);
rootNode.attachChild(ground);
rootNode.attachChild(player);
rootNode.attachChild(starManager);
gui.setWait();
gui.attach();
}
@ -142,6 +140,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
@Override
public void stateDetached(AppStateManager stateManager) {
gui.detach();
rootNode.detachChild(starManager);
rootNode.detachChild(player);
rootNode.detachChild(ground);
rootNode.detachChild(cinema);
@ -159,6 +158,7 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
public void update(float tpf) {
if (isRunning) {
audioDataManager.update(tpf);
starManager.update(tpf);
}
}
@ -168,6 +168,11 @@ public class GameState extends AbstractAppState implements GameListener, ActionL
setRunning(false);
mediaPlayer.release();
mediaPlayerFactory.release();
mediaPlayer = null;
player = null;
ground = null;
cinema = null;
starManager = null;
}
public DirectMediaPlayer getMediaPlayer() {