From 68fd220e8fef7dde91d4cf2f9f7c1e764446d111 Mon Sep 17 00:00:00 2001 From: s-chenrob Date: Thu, 5 Apr 2018 10:03:47 -0700 Subject: [PATCH] Particles --- src/game/Particle.java | 40 +++++++++++++++++++++++++++++++++++++++ src/game/RhythmState.java | 24 ++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/game/Particle.java diff --git a/src/game/Particle.java b/src/game/Particle.java new file mode 100644 index 0000000..995f926 --- /dev/null +++ b/src/game/Particle.java @@ -0,0 +1,40 @@ +package game; + +import org.newdawn.slick.Color; + +/** + * We assume that all particles can be rendered as a circle + color. + * + * @author s-chenrob + * + */ +public class Particle extends HitObject { + protected int tick; + + public Particle(int x, int y, float radius, float duration, boolean clicked) { + super(x, y, radius, duration, clicked); + + tick = 0; + } + + public void update(int delta) { + tick += delta / 2; + } + + public int getRadius() { + return tick / 2; + } + + public Color getColor() { + return new Color(0, 255, 0, 155 - tick); + } + + /** + * Return true if this particle should be disposed. + * + * @return + */ + public boolean isDead() { + return tick > 150; + } +} diff --git a/src/game/RhythmState.java b/src/game/RhythmState.java index 9556a08..19afff6 100644 --- a/src/game/RhythmState.java +++ b/src/game/RhythmState.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.nio.file.Paths; +import java.util.HashSet; import java.util.LinkedList; import java.util.concurrent.CopyOnWriteArrayList; @@ -61,6 +62,7 @@ public class RhythmState extends DefaultGameState { private long starttime = System.currentTimeMillis(); // initial time private long songtime = 0; // time since beginning of the song, in ms + private HashSet particles = new HashSet<>(); private CopyOnWriteArrayList hitobjects = new CopyOnWriteArrayList<>(); private LinkedList beatmap = new LinkedList<>(); private int beatmapindex = 0; @@ -140,8 +142,17 @@ public void render(GameContainer gc, StateBasedGame arg1, Graphics g) g.setColor(new Color(0, 0, 0, overlayopacity)); g.fill(new Rectangle(0, 0, gc.getWidth(), gc.getHeight())); - // this for loop draws all the hit objects + // this for loop draws all the particles OsuPixels osupixelconverter = new OsuPixels(); + for (Particle particle : particles) { + Vector2f center = osupixelconverter.osuPixeltoXY(gc, + new Vector2f(particle.x, particle.y)); + + g.setColor(particle.getColor()); + g.fill(new Circle(center.x, center.y, particle.getRadius())); + } + + // this for loop draws all the hit objects float scalefactor = osupixelconverter.getScaleFactor(gc); for (int index = 0; index < hitobjects.size(); index++) { HitObject hitobject = hitobjects.get(index); @@ -222,6 +233,16 @@ public void update(GameContainer gc, StateBasedGame sbg, int delta) beatmapindex++; } + HashSet toDelete = new HashSet<>(); + for (Particle particle : particles) { + particle.update(delta); + if (particle.isDead()) { + toDelete.add(particle); + } + } + for (Particle p : toDelete) { + particles.remove(p); + } for (HitObject hitobject : hitobjects) { int index = hitobjects.indexOf(hitobject); @@ -274,6 +295,7 @@ public void click() { perfection += 1.0 - Math.abs(hitobject.duration) / LENIENCE_TIME; combo++; // increases combo } + particles.add(new Particle(hitobject.x, hitobject.y, hitobject.radius, hitobject.duration, false)); break; // breaks out of loop so that only one hit object is clicked at // once }