entity spawning added

This commit is contained in:
Harrison Deng 2017-07-12 23:53:53 -05:00
parent 44551bdea3
commit 9d8d34662d
12 changed files with 138 additions and 36 deletions

BIN
android/assets/music/Beat_8.mp3 Executable file

Binary file not shown.

View File

@ -197,6 +197,7 @@ public class AudioAnalyzer {
int lastID = 0; int lastID = 0;
float bassBeats = 0; float bassBeats = 0;
float umBeats = 0; float umBeats = 0;
avgBPS = -1f;
for (int i = 0; i < umPrunned.size-1 && work; i++) { for (int i = 0; i < umPrunned.size-1 && work; i++) {
bassPeaks.add((bassPrunned.get(i) > bassPrunned.get(i+1) ? bassPrunned.get(i) : 0)); bassPeaks.add((bassPrunned.get(i) > bassPrunned.get(i+1) ? bassPrunned.get(i) : 0));
if (bassPeaks.get(i) > bassMaxValue) { if (bassPeaks.get(i) > bassMaxValue) {
@ -215,7 +216,6 @@ public class AudioAnalyzer {
overlappedPeaks.add(0); overlappedPeaks.add(0);
} }
avgBPS = -1f;
if (avgBPS != -1) { if (avgBPS != -1) {
if (bassPeaks.get(i) == 0) { if (bassPeaks.get(i) == 0) {
@ -235,12 +235,14 @@ public class AudioAnalyzer {
umAvg += umPeaks.get(i); umAvg += umPeaks.get(i);
umBeats++; umBeats++;
} }
} }
//then we minus one from the beats so it actually works out //then we minus one from the beats so it actually works out
avgBPS -= umPrunned.size-lastID; avgBPS -= umPrunned.size-lastID;
avgBPS *= secondsPerWindow; avgBPS *= secondsPerWindow;
avgBPS /= bassBeats; avgBPS /= bassBeats;
Gdx.app.debug("Audio Analyzer", "Avg BPS: " + avgBPS);
bassAvg /= bassBeats; bassAvg /= bassBeats;
umBeats /= umBeats; umBeats /= umBeats;

View File

@ -34,7 +34,7 @@ public class WavAudioData implements AudioData {
@Override @Override
public void readIndexUpdate() { public void readIndexUpdate() {
readIndex = (int) (playbackMusic.getPosition() * decoder.getSampleRate() / readWindowSize); readIndex = (int) (playbackMusic.getPosition() * decoder.getSampleRate() / (float)readWindowSize);
} }
@Override @Override

View File

@ -4,9 +4,9 @@ import zero1hd.polyjet.entity.Entities;
public class EntitySpawnInfo { public class EntitySpawnInfo {
private Entities entityType; private Entities entityType;
private double[] parameters; private float[] parameters;
public EntitySpawnInfo(Entities entityType, double... parameters) { public EntitySpawnInfo(Entities entityType, float... parameters) {
this.entityType = entityType; this.entityType = entityType;
this.parameters = parameters; this.parameters = parameters;
} }
@ -19,4 +19,8 @@ public class EntitySpawnInfo {
public String toString() { public String toString() {
return entityType.name() + ": " + parameters.length; return entityType.name() + ": " + parameters.length;
} }
public float[] getParameters() {
return parameters;
}
} }

View File

@ -1,5 +1,6 @@
package zero1hd.polyjet.audio.map; package zero1hd.polyjet.audio.map;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music; import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
@ -7,19 +8,17 @@ import zero1hd.polyjet.audio.AudioData;
import zero1hd.polyjet.entity.Entities; import zero1hd.polyjet.entity.Entities;
public class GamePlayMap { public class GamePlayMap {
private Music playableClip; private AudioData playableClip;
private float playTime; private float playTime;
private Array<EntitySpawnInfo> spawnList; private Array<EntitySpawnInfo> spawnList;
private boolean building; private boolean building;
private int index; private int index;
/** /**
* GamePlayMap is what the game area will use to generate entities and judge current audio data * GamePlayMap is what the game area will use to generate entities and judge current audio data
* @param audioData audio data * @param audioData audio data
*/ */
public GamePlayMap(AudioData audioData) { public GamePlayMap(AudioData audioData) {
playableClip = audioData.getPlaybackMusic(); this.playableClip = audioData;
playTime = audioData.getDuration();
spawnList = new Array<>(); spawnList = new Array<>();
} }
@ -28,7 +27,7 @@ public class GamePlayMap {
* @return * @return
*/ */
public Music getPlayableClip() { public Music getPlayableClip() {
return playableClip; return playableClip.getPlaybackMusic();
} }
/** /**
@ -60,7 +59,7 @@ public class GamePlayMap {
* @param entityType what type of entity to spawn * @param entityType what type of entity to spawn
* @param parameters the arguments for the entity. It is important to have the same amount of parameters the entity requires to spawn * @param parameters the arguments for the entity. It is important to have the same amount of parameters the entity requires to spawn
*/ */
public void addToMap(Entities entityType, double... parameters) { public void addToMap(Entities entityType, float... parameters) {
if (building && entityType != null && parameters != null) { if (building && entityType != null && parameters != null) {
spawnList.add(new EntitySpawnInfo(entityType, parameters)); spawnList.add(new EntitySpawnInfo(entityType, parameters));
} }
@ -80,10 +79,11 @@ public class GamePlayMap {
* retrieve next entity in list. * retrieve next entity in list.
* @return * @return
*/ */
public EntitySpawnInfo nextEntity() { public EntitySpawnInfo nextEntity(boolean indexUpdate) {
if (!building) { if (!building) {
if (indexUpdate) {
index++; index++;
}
EntitySpawnInfo spawnInfo = spawnList.get(index); EntitySpawnInfo spawnInfo = spawnList.get(index);
return spawnInfo; return spawnInfo;
} else { } else {
@ -91,6 +91,16 @@ public class GamePlayMap {
} }
} }
public EntitySpawnInfo safeNextEntity() {
playableClip.readIndexUpdate();
if (index != playableClip.getReadIndex()) {
index = playableClip.getReadIndex();
return nextEntity(false);
} else {
return null;
}
}
/** /**
* set retrieve entity index position. * set retrieve entity index position.
* @param index * @param index

View File

@ -60,17 +60,21 @@ public class RhythmMapAlgorithm implements Runnable {
); );
} }
if (bassPeaks.get(index) != 0) { if (bassPeaks.get(index) != 0) {
// float xSpawnLocation = (rand.nextFloat()*(Polyjet.GAME_AREA_WIDTH-2))+1;
// map.addToMap(Entities.PELLET,
// xSpawnLocation,
// Polyjet.GAME_AREA_HEIGHT-0.25f,
// 180*rand.nextFloat()+90,
// speedMod*(1f/avgBPS));
map.addToMap(Entities.BAR, map.addToMap(Entities.BAR,
MathUtils.round(rand.nextFloat()*Polyjet.GAME_AREA_WIDTH), MathUtils.round(3),
((Polyjet.GAME_AREA_HEIGHT-bassPeaks.get(index)*Polyjet.GAME_AREA_HEIGHT)/avgBPS)*speedMod); (1.5f/avgBPS)*speedMod);
} else { } else {
if (UMPeaks.get(index) != 0) { if (UMPeaks.get(index) != 0) {
float xSpawnLocation = (rand.nextFloat()*(Polyjet.GAME_AREA_WIDTH-2))+1; // map.addToMap(Entities.BAR,
map.addToMap(Entities.PELLET, // MathUtils.round(3),
xSpawnLocation, // (1.5f/avgBPS)*speedMod);
Polyjet.GAME_AREA_HEIGHT-0.25f,
180+180*rand.nextFloat(),
speedMod*(Polyjet.GAME_AREA_HEIGHT/3)/avgBPS);
} }
} }

View File

@ -1,10 +1,15 @@
package zero1hd.polyjet.entity; package zero1hd.polyjet.entity;
import java.util.Arrays;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool; import com.badlogic.gdx.utils.Pool;
import zero1hd.polyjet.audio.map.EntitySpawnInfo;
import zero1hd.polyjet.entity.ally.Laser; import zero1hd.polyjet.entity.ally.Laser;
import zero1hd.polyjet.entity.enemies.Bar; import zero1hd.polyjet.entity.enemies.Bar;
import zero1hd.polyjet.entity.enemies.Flake; import zero1hd.polyjet.entity.enemies.Flake;
@ -106,6 +111,61 @@ public class EntityController {
} }
} }
public Entity spawnEntity(Stage stage, EntitySpawnInfo entitySpawnInfo) {
if (entitySpawnInfo != null) {
float[] param = entitySpawnInfo.getParameters();
Gdx.app.debug("Spawning Entity", entitySpawnInfo.toString() + " parameters: " + Arrays.toString(param));
switch (entitySpawnInfo.getEntityType()) {
case VOID_CIRCLE:
VoidCircle voidCircle = voidCirclePool.obtain();
voidCircle.init(param[0], param[1], param[2], param[3], param[4]);
activeEnemies.add(voidCircle);
stage.addActor(voidCircle);
return voidCircle;
case LASER:
Laser laser = laserPool.obtain();
laser.init(param[0], param[1], param[2]);
activeAllies.add(laser);
stage.addActor(laser);
return laser;
case PELLET:
Pellet pellet = pelletPool.obtain();
pellet.init(param[0], param[1], param[2], param[3]);
activeEnemies.add(pellet);
stage.addActor(pellet);
return pellet;
case SHARD:
Shard shard = shardPool.obtain();
shard.init(param[0], param[1], param[2], param[3], (int) param[4]);
activeEnemies.add(shard);
stage.addActor(shard);
return shard;
case BAR:
Bar bar = barPool.obtain();
bar.init(param[0], param[1]);
activeEnemies.add(bar);
stage.addActor(bar);
return bar;
case FLAKE:
Flake flake = flakePool.obtain();
Shard[] shards = new Shard[(int) param[0]];
for (int i = 0; i < shards.length; i++) {
shards[i] = (Shard) retrieveEntity(Entities.SHARD);
shards[i].init(param[6], param[7], param[8], param[9], (int) param[10]);
stage.addActor(shards[i]);
}
flake.init(param[1], param[2], param[3], param[4], param[5], shards);
activeEnemies.add(flake);
stage.addActor(flake);
return flake;
default:
return null;
}
} else {
return null;
}
}
public void free(Entity entity) { public void free(Entity entity) {
if (entity.getEntityType().isEnemy()) { if (entity.getEntityType().isEnemy()) {
activeEnemies.removeValue(entity, true); activeEnemies.removeValue(entity, true);

View File

@ -94,10 +94,10 @@ public class AnalyzePage extends Page implements MiniListener {
speedModifier.addListener(new ChangeListener() { speedModifier.addListener(new ChangeListener() {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
speedModifierTitle.setText("Speed Modifier: " + speedModifier.getValue()); speedModifierTitle.setText("Speed Modifier: x" + speedModifier.getValue());
} }
}); });
speedModifierTitle = new Label("Speed Modifier: " + speedModifier.getValue(), skin, "sub-font", skin.getColor("default")); speedModifierTitle = new Label("Speed Modifier: x" + speedModifier.getValue(), skin, "sub-font", skin.getColor("default"));
difficultyTable.add(speedModifierTitle); difficultyTable.add(speedModifierTitle);
difficultyTable.row(); difficultyTable.row();
difficultyTable.add(speedModifier).fillX(); difficultyTable.add(speedModifier).fillX();
@ -110,10 +110,10 @@ public class AnalyzePage extends Page implements MiniListener {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
healthModifierTitle.setText("Health modifier: " + healthModifier.getValue()); healthModifierTitle.setText("Health modifier: +" + healthModifier.getValue());
} }
}); });
healthModifierTitle = new Label("Health modifier: " + healthModifier.getValue(), skin, "sub-font", skin.getColor("default")); healthModifierTitle = new Label("Health modifier: +" + healthModifier.getValue(), skin, "sub-font", skin.getColor("default"));
difficultyTable.add(healthModifierTitle); difficultyTable.add(healthModifierTitle);
difficultyTable.row(); difficultyTable.row();
difficultyTable.add(healthModifier).fillX().spaceBottom(15f); difficultyTable.add(healthModifier).fillX().spaceBottom(15f);
@ -143,7 +143,7 @@ public class AnalyzePage extends Page implements MiniListener {
beginTable = new Table(skin); beginTable = new Table(skin);
beginTable.pad(15f); beginTable.pad(15f);
beginButton = new TextButton("Begin!", skin); beginButton = new TextButton("Begin", skin);
beginTable.add(beginButton); beginTable.add(beginButton);
beginTable.setBackground(skin.getDrawable("large-pane")); beginTable.setBackground(skin.getDrawable("large-pane"));
beginTable.pack(); beginTable.pack();

View File

@ -42,7 +42,9 @@ public class CreativeStage extends Stage implements MiniListener {
RhythmMapAlgorithm mapGen; RhythmMapAlgorithm mapGen;
Window toolbox; Window toolbox;
GamePlayArea gpa;
public CreativeStage(final Polyjet core, final MainMenu mainMenu, final GamePlayArea gpa) { public CreativeStage(final Polyjet core, final MainMenu mainMenu, final GamePlayArea gpa) {
this.gpa = gpa;
musicSelector = new MusicSelector("Select Audio File", core.getDefaultSkin(), core.getPrefs().getString("music dir"), "default"); musicSelector = new MusicSelector("Select Audio File", core.getDefaultSkin(), core.getPrefs().getString("music dir"), "default");
musicSelector.miniSender.addListener(this); musicSelector.miniSender.addListener(this);
musicSelector.refresh(); musicSelector.refresh();
@ -247,15 +249,20 @@ public class CreativeStage extends Stage implements MiniListener {
analyzer.runThresholdCleaning(diffWindow.getSensitivityVal()); analyzer.runThresholdCleaning(diffWindow.getSensitivityVal());
break; break;
case MUSIC_DATA_CLEANED: case MUSIC_DATA_CLEANED:
if (analyzer != null && analyzer.isFinalized()) {
mapGen = new RhythmMapAlgorithm(analyzer, diffWindow.getSpeedModifier(), diffWindow.getHealthModifier()); mapGen = new RhythmMapAlgorithm(analyzer, diffWindow.getSpeedModifier(), diffWindow.getHealthModifier());
mapGen.getSender().addListener(this);
Thread mapGenThread = new Thread(mapGen); Thread mapGenThread = new Thread(mapGen);
mapGenThread.start(); mapGenThread.start();
}
break; break;
case MAP_GENERATED: case MAP_GENERATED:
musicPlayBackControls.setAudiofile(musicSelector.getSelectedMusic()); Gdx.app.debug("creative", "successfully generated map.");
volumeWindow.setMusic(musicPlayBackControls.getAudiofile()); musicPlayBackControls.setAudiofile(analyzer.getAudioData());
beatViewer.setMusic(musicPlayBackControls.getAudiofile(), analyzer); volumeWindow.setMusic(analyzer.getAudioData());
graphViewer.setData(analyzer.getBassPeaks(), analyzer.getUMPeaks(), musicPlayBackControls.getAudiofile()); beatViewer.setMusic(analyzer.getAudioData(), analyzer);
graphViewer.setData(analyzer.getBassPeaks(), analyzer.getUMPeaks(), analyzer.getAudioData());
gpa.setAudioMap(mapGen.getMap());
break; break;
default: default:
break; break;

View File

@ -98,6 +98,8 @@ public class GamePlayArea extends Stage {
//TODO batch draw background //TODO batch draw background
getBatch().begin(); getBatch().begin();
if (bgShader != null) { if (bgShader != null) {
getBatch().setShader(bgShader); getBatch().setShader(bgShader);
bgShader.setUniformf("resolution", background.getWidth(), background.getHeight()); bgShader.setUniformf("resolution", background.getWidth(), background.getHeight());
@ -117,7 +119,9 @@ public class GamePlayArea extends Stage {
if (audioMap != null && time < audioMap.getPlayTime()) { if (audioMap != null && time < audioMap.getPlayTime()) {
time += delta; time += delta;
} }
if (audioMap != null) {
ec.spawnEntity(this, audioMap.safeNextEntity());
}
collisionDetector.collisionCheck(); collisionDetector.collisionCheck();
ec.deathClean(); ec.deathClean();

View File

@ -9,6 +9,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window; import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import zero1hd.polyjet.audio.Audio; import zero1hd.polyjet.audio.Audio;
@ -50,7 +51,16 @@ public class MusicSelector extends Window {
miniSender.send(MiniEvents.MUSIC_SELECTED); miniSender.send(MiniEvents.MUSIC_SELECTED);
} }
}); });
add(confirmButton).right(); add(confirmButton);
TextButton regenMap = new TextButton("regen map", skin);
regenMap.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
miniSender.send(MiniEvents.MUSIC_DATA_CLEANED);
}
});
add(regenMap);
row(); row();

View File

@ -69,5 +69,6 @@ public class VolumeWindow extends Window {
public void setMusic(AudioData music) { public void setMusic(AudioData music) {
this.music = music; this.music = music;
music.getPlaybackMusic().setVolume(prefs.getFloat("music vol")/100f);
} }
} }