Код: Выделить всё
package com.thefifthmoon.magicpiano;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class MagicPiano extends ApplicationAdapter {
public static final float MIN_NOTES_DISTANCE = 25;
public static final float WIDTH = 800;
public static final float HEIGHT = 480;
public static final float ROSE_HEIGHT = 66;
public static final float ROSE_WIDTH = 188;
public static final float TEAR_SIZE = 66;
public static final long RAIN_DROP_SPEED = 666;
private static int score;
private static int sNotesNumber = 0;
private OrthographicCamera mCamera;
private SpriteBatch mBatch;
private Texture mBackgroundTexture;
private Texture mDropImage;
private Texture mGothicrose;
private Sound c, c_sharp, d, d_sharp, e, f, f_sharp, g, g_sharp, a, a_sharp, b,
c_up, c_sharp_up, d_up, d_sharp_up, e_up, f_up, f_sharp_up, g_up, g_sharp_up, a_up, a_sharp_up, b_up;
private Rectangle mBucket;
private Vector3 mTouchPos;
private Array<Rectangle> mRaindrops;
private HashMap<Integer, Sound> mSoundHashMap;
private HashMap<Integer, Integer> mKeyPosition;
private MidiInfo mMidiInfo;
private Music mMusic;
private String mMidiFile;
private static String sMusicFile;
private String mImgBackground;
private long mPauseBeforeMp3;
private long mPauseBeforeMidi;
public static int getScore() {return score;}
public static int getNotesNumber() {return sNotesNumber;}
public static String getMusicFile() {return sMusicFile;}
public void setMusicFile(String musicFile) {sMusicFile = musicFile;}
public void setMidiFile(String midiFile) {mMidiFile = midiFile;}
public void setImgBackground(String imgBackground) {mImgBackground = imgBackground;}
public void setPauseBeforeMp3(long pauseBeforeMp3) {this.mPauseBeforeMp3 = pauseBeforeMp3;}
public void setPauseBeforeMidi(long pauseBeforeMidi) {mPauseBeforeMidi = pauseBeforeMidi;}
@Override
public void create() {
score = 0;
mCamera = new OrthographicCamera();
mCamera.setToOrtho(false, WIDTH, HEIGHT);
mBatch = new SpriteBatch();
mTouchPos = new Vector3();
initializeMidiInfo();
initializeTextures();
initializeKeysPositions();
initializeSounds();
playMusic();
mBucket = new Rectangle();
mBucket.x = WIDTH / 2 - ROSE_WIDTH / 2;
mBucket.y = 0;
mBucket.width = ROSE_WIDTH;
mBucket.height = ROSE_HEIGHT;
mRaindrops = new Array<Rectangle>();
spawnTears();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mCamera.update();
mBatch.setProjectionMatrix(mCamera.combined);
mBatch.begin();
mBatch.draw(mBackgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
mBatch.draw(mGothicrose, mBucket.x, mBucket.y);
for (Rectangle raindrop : mRaindrops) {
mBatch.draw(mDropImage, raindrop.x, raindrop.y);
}
mBatch.end();
if (Gdx.input.isTouched()) {
mTouchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
mCamera.unproject(mTouchPos);
mBucket.x = mTouchPos.x - ROSE_WIDTH / 2;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) mBucket.x -= 200 * Gdx.graphics.getDeltaTime();
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
mBucket.x += 200 * Gdx.graphics.getDeltaTime();
if (mBucket.x < 0) mBucket.x = 0;
if (mBucket.x > WIDTH - ROSE_WIDTH) mBucket.x = WIDTH - ROSE_WIDTH;
Iterator<Rectangle> iter = mRaindrops.iterator();
while (iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= RAIN_DROP_SPEED * Gdx.graphics.getDeltaTime();
if (raindrop.y + ROSE_HEIGHT < 0) iter.remove();
if (raindrop.overlaps(mBucket)) {
playNote(raindrop);
iter.remove();
}
}
}
private void playNote(Rectangle r) {
float x = r.x;
float notePosition = x / MIN_NOTES_DISTANCE;
for (java.util.Map.Entry<Integer, Integer> entry : mKeyPosition.entrySet()) {
if (entry.getValue().equals((int) notePosition)) {
mSoundHashMap.get(entry.getKey()).play();
score++;
}
}
}
private void spawnTears() {
final long startTime = System.currentTimeMillis();
new Thread(new Runnable() {
@Override
public void run() {
for (final Map.Entry<Long, Integer> entry : mMidiInfo.getMidiInfo().entrySet()) {
while (System.currentTimeMillis() - startTime < entry.getKey()) {
try {
Thread.sleep(5);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
spawnRaindropAtKey(mKeyPosition.get(entry.getValue()));
}
}
}).start();
}
private void spawnRaindropAtKey(int key) {
Rectangle raindrop = new Rectangle();
raindrop.x = MIN_NOTES_DISTANCE * key;
raindrop.y = HEIGHT;
raindrop.width = TEAR_SIZE;
raindrop.height = TEAR_SIZE;
mRaindrops.add(raindrop);
}
private void initializeMidiInfo() {
try {
mMidiInfo = new MidiInfo(mMidiFile, mPauseBeforeMidi);
sNotesNumber = mMidiInfo.getMidiInfo().size();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void initializeTextures() {
mDropImage = new Texture("Blood_Drop.png");
mGothicrose = new Texture("Gothic-Rose-188-66.png");
mBackgroundTexture = new Texture(mImgBackground);
}
private void initializeKeysPositions() {
mKeyPosition = new HashMap<Integer, Integer>();
mKeyPosition.put(0, 0); // c
mKeyPosition.put(1, 1); // c#
mKeyPosition.put(2, 2); // d
mKeyPosition.put(3, 3); // d#
mKeyPosition.put(4, 4); // e
mKeyPosition.put(5, 6); // f
mKeyPosition.put(6, 7); // f#
mKeyPosition.put(7, 8); // g
mKeyPosition.put(8, 9); // g#
mKeyPosition.put(9, 10); // a
mKeyPosition.put(10, 11); // a#
mKeyPosition.put(11, 12); // b
mKeyPosition.put(12, 14); // etc...
mKeyPosition.put(13, 15);
mKeyPosition.put(14, 16);
mKeyPosition.put(15, 17);
mKeyPosition.put(16, 18);
mKeyPosition.put(17, 20);
mKeyPosition.put(18, 21);
mKeyPosition.put(19, 22);
mKeyPosition.put(20, 23);
mKeyPosition.put(21, 24);
mKeyPosition.put(22, 25);
mKeyPosition.put(23, 26);
}
public void initializeSounds() {
c = Gdx.audio.newSound(Gdx.files.internal("c.mp3"));
c_sharp = Gdx.audio.newSound(Gdx.files.internal("c_sharp.mp3"));
d = Gdx.audio.newSound(Gdx.files.internal("d.mp3"));
d_sharp = Gdx.audio.newSound(Gdx.files.internal("d_sharp.mp3"));
e = Gdx.audio.newSound(Gdx.files.internal("e.mp3"));
f = Gdx.audio.newSound(Gdx.files.internal("f.mp3"));
f_sharp = Gdx.audio.newSound(Gdx.files.internal("f_sharp.mp3"));
g_sharp = Gdx.audio.newSound(Gdx.files.internal("g_sharp.mp3"));
g = Gdx.audio.newSound(Gdx.files.internal("g.mp3"));
a = Gdx.audio.newSound(Gdx.files.internal("a.mp3"));
a_sharp = Gdx.audio.newSound(Gdx.files.internal("a_sharp.mp3"));
b = Gdx.audio.newSound(Gdx.files.internal("b.mp3"));
c_up = Gdx.audio.newSound(Gdx.files.internal("c_up.mp3"));
c_sharp_up = Gdx.audio.newSound(Gdx.files.internal("c_sharp_up.mp3"));
d_up = Gdx.audio.newSound(Gdx.files.internal("d_up.mp3"));
d_sharp_up = Gdx.audio.newSound(Gdx.files.internal("d_sharp_up.mp3"));
e_up = Gdx.audio.newSound(Gdx.files.internal("e_up.mp3"));
f_up = Gdx.audio.newSound(Gdx.files.internal("f_up.mp3"));
f_sharp_up = Gdx.audio.newSound(Gdx.files.internal("f_sharp_up.mp3"));
g_up = Gdx.audio.newSound(Gdx.files.internal("g_up.mp3"));
g_sharp_up = Gdx.audio.newSound(Gdx.files.internal("g_sharp_up.mp3"));
a_up = Gdx.audio.newSound(Gdx.files.internal("a_up.mp3"));
a_sharp_up = Gdx.audio.newSound(Gdx.files.internal("a_sharp_up.mp3"));
b_up = Gdx.audio.newSound(Gdx.files.internal("b_up.mp3"));
mSoundHashMap = new HashMap<Integer, Sound>();
mSoundHashMap.put(0, c);
mSoundHashMap.put(1, c_sharp);
mSoundHashMap.put(2, d);
mSoundHashMap.put(3, d_sharp);
mSoundHashMap.put(4, e);
mSoundHashMap.put(5, f);
mSoundHashMap.put(7, g);
mSoundHashMap.put(6, f_sharp);
mSoundHashMap.put(8, g_sharp);
mSoundHashMap.put(9, a);
mSoundHashMap.put(10, a_sharp);
mSoundHashMap.put(11, b);
mSoundHashMap.put(12, c_up);
mSoundHashMap.put(13, c_sharp_up);
mSoundHashMap.put(14, d_up);
mSoundHashMap.put(15, d_sharp_up);
mSoundHashMap.put(16, e_up);
mSoundHashMap.put(17, f_up);
mSoundHashMap.put(18, f_sharp_up);
mSoundHashMap.put(19, g_up);
mSoundHashMap.put(20, g_sharp_up);
mSoundHashMap.put(21, a_up);
mSoundHashMap.put(22, a_sharp_up);
mSoundHashMap.put(23, b_up);
}
private void playMusic() {
new Thread(new Runnable() {
@Override
public void run() {
mMusic = Gdx.audio.newMusic(Gdx.files.internal(sMusicFile));
try {
Thread.sleep(mPauseBeforeMp3);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
mMusic.play();
}
}).start();
}
@Override
public void dispose() {
super.dispose();
mDropImage.dispose();
mGothicrose.dispose();
mMusic.dispose();
mBatch.dispose();
ArrayList<Sound> soundList = new ArrayList<Sound>(Arrays.asList(c, c_sharp, d, d_sharp, e,
f, f_sharp, g, g_sharp, a, a_sharp, b, c_up, c_sharp_up, d_up, d_sharp_up, e_up,
f_up, f_sharp_up, g_up, g_sharp_up, a_up, a_sharp_up, b_up));
for (Sound sound : soundList) {
sound.dispose();
}
}
}