2015-09-05 05:36:25 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
|
|
|
*
|
|
|
|
* opsu! 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.
|
|
|
|
*
|
|
|
|
* opsu! 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 opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu.ui;
|
|
|
|
|
|
|
|
import itdelatrisu.opsu.GameImage;
|
|
|
|
import itdelatrisu.opsu.Utils;
|
2016-12-23 06:40:36 +01:00
|
|
|
import itdelatrisu.opsu.objects.curves.Vec2f;
|
2015-09-05 05:36:25 +02:00
|
|
|
import itdelatrisu.opsu.ui.animations.AnimatedValue;
|
|
|
|
import itdelatrisu.opsu.ui.animations.AnimationEquation;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import org.newdawn.slick.Image;
|
|
|
|
|
|
|
|
/**
|
2016-12-23 10:03:32 +01:00
|
|
|
* Star stream.
|
2015-09-05 05:36:25 +02:00
|
|
|
*/
|
|
|
|
public class StarStream {
|
2016-12-23 06:40:36 +01:00
|
|
|
/** The origin of the star stream. */
|
2016-12-23 07:29:49 +01:00
|
|
|
private final Vec2f position;
|
2016-12-23 06:40:36 +01:00
|
|
|
|
|
|
|
/** The direction of the star stream. */
|
2016-12-23 07:29:49 +01:00
|
|
|
private final Vec2f direction;
|
|
|
|
|
|
|
|
/** The maximum number of stars to draw at once. */
|
|
|
|
private final int maxStars;
|
2016-12-23 06:40:36 +01:00
|
|
|
|
|
|
|
/** The spread of the stars' starting position. */
|
2016-12-23 07:29:49 +01:00
|
|
|
private float positionSpread = 0f;
|
2016-12-23 06:40:36 +01:00
|
|
|
|
|
|
|
/** The spread of the stars' direction. */
|
2016-12-23 07:29:49 +01:00
|
|
|
private float directionSpread = 0f;
|
|
|
|
|
|
|
|
/** The base (mean) duration for which stars are shown, in ms. */
|
|
|
|
private int durationBase = 1300;
|
|
|
|
|
|
|
|
/** The spread of the stars' duration, in ms. */
|
|
|
|
private int durationSpread = 300;
|
2015-09-05 05:36:25 +02:00
|
|
|
|
2016-12-23 22:53:12 +01:00
|
|
|
/** The base (mean) scale at which stars are drawn. */
|
|
|
|
private float scaleBase = 1f;
|
|
|
|
|
|
|
|
/** The spread of the stars' scale.*/
|
|
|
|
private float scaleSpread = 0f;
|
|
|
|
|
2015-09-05 05:36:25 +02:00
|
|
|
/** The star image. */
|
|
|
|
private final Image starImg;
|
|
|
|
|
|
|
|
/** The current list of stars. */
|
|
|
|
private final List<Star> stars;
|
|
|
|
|
|
|
|
/** Random number generator instance. */
|
|
|
|
private final Random random;
|
|
|
|
|
|
|
|
/** Contains data for a single star. */
|
|
|
|
private class Star {
|
2016-12-23 06:40:36 +01:00
|
|
|
/** The star position offset. */
|
|
|
|
private final Vec2f offset;
|
|
|
|
|
|
|
|
/** The star direction vector. */
|
|
|
|
private final Vec2f dir;
|
|
|
|
|
|
|
|
/** The star image rotation angle. */
|
|
|
|
private final int angle;
|
|
|
|
|
2016-12-23 22:53:12 +01:00
|
|
|
/** The star image scale. */
|
|
|
|
private final float scale;
|
|
|
|
|
2015-09-05 05:36:25 +02:00
|
|
|
/** The star animation progress. */
|
|
|
|
private final AnimatedValue animatedValue;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a star with the given properties.
|
2016-12-23 06:40:36 +01:00
|
|
|
* @param offset the position offset vector
|
|
|
|
* @param direction the direction vector
|
|
|
|
* @param angle the image rotation angle
|
2016-12-23 22:53:12 +01:00
|
|
|
* @param scale the image scale
|
2015-09-05 05:36:25 +02:00
|
|
|
* @param duration the time, in milliseconds, to show the star
|
|
|
|
* @param eqn the animation equation to use
|
|
|
|
*/
|
2016-12-23 22:53:12 +01:00
|
|
|
public Star(Vec2f offset, Vec2f direction, int angle, float scale, int duration, AnimationEquation eqn) {
|
2016-12-23 06:40:36 +01:00
|
|
|
this.offset = offset;
|
|
|
|
this.dir = direction;
|
2015-09-05 05:36:25 +02:00
|
|
|
this.angle = angle;
|
2016-12-23 22:53:12 +01:00
|
|
|
this.scale = scale;
|
2016-12-23 06:40:36 +01:00
|
|
|
this.animatedValue = new AnimatedValue(duration, 0f, 1f, eqn);
|
2015-09-05 05:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the star.
|
|
|
|
*/
|
|
|
|
public void draw() {
|
|
|
|
float t = animatedValue.getValue();
|
2015-09-05 16:32:26 +02:00
|
|
|
starImg.setImageColor(1f, 1f, 1f, Math.min((1 - t) * 5f, 1f));
|
|
|
|
starImg.drawEmbedded(
|
2016-12-23 06:40:36 +01:00
|
|
|
offset.x + t * dir.x, offset.y + t * dir.y,
|
2016-12-23 22:53:12 +01:00
|
|
|
starImg.getWidth() * scale, starImg.getHeight() * scale, angle
|
2016-12-23 06:40:36 +01:00
|
|
|
);
|
2015-09-05 05:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the animation by a delta interval.
|
|
|
|
* @param delta the delta interval since the last call
|
|
|
|
* @return true if an update was applied, false if the animation was not updated
|
|
|
|
*/
|
|
|
|
public boolean update(int delta) { return animatedValue.update(delta); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the star stream.
|
2016-12-23 06:40:36 +01:00
|
|
|
* @param x the x position
|
|
|
|
* @param y the y position
|
|
|
|
* @param dirX the x-axis direction
|
|
|
|
* @param dirY the y-axis direction
|
2016-12-23 07:29:49 +01:00
|
|
|
* @param k the maximum number of stars to draw at once (excluding bursts)
|
2015-09-05 05:36:25 +02:00
|
|
|
*/
|
2016-12-23 07:29:49 +01:00
|
|
|
public StarStream(float x, float y, float dirX, float dirY, int k) {
|
2016-12-23 06:40:36 +01:00
|
|
|
this.position = new Vec2f(x, y);
|
|
|
|
this.direction = new Vec2f(dirX, dirY);
|
2016-12-23 07:29:49 +01:00
|
|
|
this.maxStars = k;
|
2015-09-05 05:36:25 +02:00
|
|
|
this.starImg = GameImage.STAR2.getImage().copy();
|
2016-12-23 07:29:49 +01:00
|
|
|
this.stars = new ArrayList<Star>(k);
|
2015-09-05 05:36:25 +02:00
|
|
|
this.random = new Random();
|
|
|
|
}
|
|
|
|
|
2016-12-23 07:29:49 +01:00
|
|
|
/**
|
|
|
|
* Set the direction spread of this star stream.
|
|
|
|
* @param spread the spread of the stars' starting position
|
|
|
|
*/
|
|
|
|
public void setPositionSpread(float spread) { this.positionSpread = spread; }
|
|
|
|
|
2016-12-23 22:53:12 +01:00
|
|
|
/**
|
|
|
|
* Sets the direction of this star stream.
|
|
|
|
* @param dirX the new x-axis direction
|
|
|
|
* @param dirY the new y-axis direction
|
|
|
|
*/
|
|
|
|
public void setDirection(float dirX, float dirY) { direction.set(dirX, dirY); }
|
|
|
|
|
2016-12-23 07:29:49 +01:00
|
|
|
/**
|
|
|
|
* Set the direction spread of this star stream.
|
|
|
|
* @param spread the spread of the stars' direction
|
|
|
|
*/
|
|
|
|
public void setDirectionSpread(float spread) { this.directionSpread = spread; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the duration base and spread of this star stream.
|
|
|
|
* @param base the base (mean) duration for which stars are shown, in ms
|
|
|
|
* @param spread the spread of the stars' duration, in ms
|
|
|
|
*/
|
|
|
|
public void setDurationSpread(int base, int spread) {
|
|
|
|
this.durationBase = base;
|
|
|
|
this.durationSpread = spread;
|
|
|
|
}
|
|
|
|
|
2016-12-23 22:53:12 +01:00
|
|
|
/**
|
|
|
|
* Sets the scale base and spread of this star stream.
|
|
|
|
* @param base the base (mean) scale at which stars are drawn
|
|
|
|
* @param spread the spread of the stars' scale
|
|
|
|
*/
|
|
|
|
public void setScaleSpread(float base, float spread) {
|
|
|
|
this.scaleBase = base;
|
|
|
|
this.scaleSpread = spread;
|
|
|
|
}
|
|
|
|
|
2015-09-05 05:36:25 +02:00
|
|
|
/**
|
|
|
|
* Draws the star stream.
|
|
|
|
*/
|
|
|
|
public void draw() {
|
2015-09-05 16:32:26 +02:00
|
|
|
if (stars.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
starImg.startUse();
|
2015-09-05 05:36:25 +02:00
|
|
|
for (Star star : stars)
|
|
|
|
star.draw();
|
2015-09-05 16:32:26 +02:00
|
|
|
starImg.endUse();
|
2015-09-05 05:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the stars in the stream by a delta interval.
|
|
|
|
* @param delta the delta interval since the last call
|
|
|
|
*/
|
2016-12-23 07:29:49 +01:00
|
|
|
public void update(int delta) {
|
2015-09-05 05:36:25 +02:00
|
|
|
// update current stars
|
|
|
|
Iterator<Star> iter = stars.iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Star star = iter.next();
|
|
|
|
if (!star.update(delta))
|
|
|
|
iter.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new stars
|
2016-12-23 07:29:49 +01:00
|
|
|
for (int i = stars.size(); i < maxStars; i++) {
|
|
|
|
if (Math.random() < ((i < maxStars / 4) ? 0.25 : 0.66))
|
|
|
|
break; // stagger spawning new stars
|
2016-12-23 06:40:36 +01:00
|
|
|
|
2016-12-23 07:29:49 +01:00
|
|
|
stars.add(createStar());
|
2015-09-05 05:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 06:40:36 +01:00
|
|
|
/**
|
|
|
|
* Creates a new star with randomized properties.
|
|
|
|
*/
|
|
|
|
private Star createStar() {
|
|
|
|
float distanceRatio = Utils.clamp((float) getGaussian(0.65, 0.25), 0.2f, 0.925f);
|
|
|
|
Vec2f offset = position.cpy().add(direction.cpy().nor().normalize().scale((float) getGaussian(0, positionSpread)));
|
|
|
|
Vec2f dir = direction.cpy().scale(distanceRatio).add((float) getGaussian(0, directionSpread), (float) getGaussian(0, directionSpread));
|
|
|
|
int angle = (int) getGaussian(0, 22.5);
|
2016-12-23 22:53:12 +01:00
|
|
|
float scale = (float) getGaussian(scaleBase, scaleSpread);
|
2016-12-23 10:03:32 +01:00
|
|
|
int duration = Math.max(0, (int) (distanceRatio * getGaussian(durationBase, durationSpread)));
|
2016-12-23 06:40:36 +01:00
|
|
|
AnimationEquation eqn = random.nextBoolean() ? AnimationEquation.IN_OUT_QUAD : AnimationEquation.OUT_QUAD;
|
|
|
|
|
2016-12-23 22:53:12 +01:00
|
|
|
return new Star(offset, dir, angle, scale, duration, eqn);
|
2016-12-23 06:40:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a burst of stars instantly.
|
|
|
|
* @param count the number of stars to create
|
|
|
|
*/
|
|
|
|
public void burst(int count) {
|
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
stars.add(createStar());
|
|
|
|
}
|
|
|
|
|
2015-09-05 05:36:25 +02:00
|
|
|
/**
|
|
|
|
* Clears the stars currently in the stream.
|
|
|
|
*/
|
|
|
|
public void clear() { stars.clear(); }
|
|
|
|
|
2016-12-23 10:03:32 +01:00
|
|
|
/**
|
|
|
|
* Returns whether there are any stars currently in this stream.
|
|
|
|
*/
|
|
|
|
public boolean isEmpty() { return stars.isEmpty(); }
|
|
|
|
|
2015-09-05 05:36:25 +02:00
|
|
|
/**
|
|
|
|
* Returns the next pseudorandom, Gaussian ("normally") distributed {@code double} value
|
|
|
|
* with the given mean and standard deviation.
|
|
|
|
* @param mean the mean
|
|
|
|
* @param stdDev the standard deviation
|
|
|
|
*/
|
|
|
|
private double getGaussian(double mean, double stdDev) {
|
|
|
|
return mean + random.nextGaussian() * stdDev;
|
|
|
|
}
|
|
|
|
}
|