Skip to content

Commit

Permalink
Particles
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-robert committed Apr 5, 2018
1 parent db1fd8d commit 68fd220
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/game/Particle.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 23 additions & 1 deletion src/game/RhythmState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Particle> particles = new HashSet<>();
private CopyOnWriteArrayList<HitObject> hitobjects = new CopyOnWriteArrayList<>();
private LinkedList<Beat> beatmap = new LinkedList<>();
private int beatmapindex = 0;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -222,6 +233,16 @@ public void update(GameContainer gc, StateBasedGame sbg, int delta)
beatmapindex++;
}

HashSet<Particle> 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);

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 68fd220

Please sign in to comment.