Skip to content

Commit

Permalink
Mocked(?) unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
s-xuch authored and s-xuch committed Apr 5, 2018
1 parent 6e05c9b commit f9eceda
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 28 deletions.
22 changes: 22 additions & 0 deletions src/game/GameScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package game;

import org.newdawn.slick.GameContainer;

public class GameScreen implements Screen {
private GameContainer gc;

public GameScreen(GameContainer gc) {
this.gc = gc;
}

@Override
public int getWidth() {
return gc.getWidth();
}

@Override
public int getHeight() {
return gc.getHeight();
}

}
20 changes: 13 additions & 7 deletions src/game/OsuPixels.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package game;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Vector2f;

/**
Expand All @@ -9,6 +8,13 @@
*
*/
public class OsuPixels {

private final Screen gc;

public OsuPixels(Screen gc) {
this.gc = gc;
}

/**
* Converts a vector containing osupixel coordinates to a vector containing
* display coordinates.
Expand All @@ -22,10 +28,10 @@ public class OsuPixels {
* @return Returns a vector containing the converted display coordinates of the
* osupixel coordinate vector.
*/
public Vector2f osuPixeltoXY(GameContainer gc, Vector2f osupixels) {
public Vector2f osuPixeltoXY(Vector2f osupixels) {
float newx = osupixels.x;
float newy = osupixels.y;
float scalefactor = getScaleFactor(gc);
float scalefactor = getScaleFactor();
// add padding on the sides
newx += 64.0;
newy += 48.0;
Expand Down Expand Up @@ -55,8 +61,8 @@ public Vector2f osuPixeltoXY(GameContainer gc, Vector2f osupixels) {
* @return Returns a vector containing the converted osupixel coordinates of the
* display coordinate vector.
*/
public Vector2f XYtoOsuPixel(GameContainer gc, Vector2f convertedvector) {
float scalefactor = (float) (1.0 / getScaleFactor(gc));
public Vector2f XYtoOsuPixel(Vector2f convertedvector) {
float scalefactor = (float) (1.0 / getScaleFactor());
if ((double) gc.getWidth() / gc.getHeight() >= 640.0 / 480.0) {
convertedvector = new Vector2f(
convertedvector.x - (gc.getWidth() - 640 / scalefactor) / 2,
Expand Down Expand Up @@ -88,9 +94,9 @@ public Vector2f XYtoOsuPixel(GameContainer gc, Vector2f convertedvector) {
* and the height of a 640x480px display. If the screen width to screen
* height ratio is less than that of a 640x480px display, returns the
* float ratio between the screen width and that of a 640x480px display.
*
*
*/
protected float getScaleFactor(GameContainer gc) {
protected float getScaleFactor() {
int screenheight = gc.getHeight();
int screenwidth = gc.getWidth();
float scalefactor;
Expand Down
16 changes: 8 additions & 8 deletions src/game/RhythmState.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,20 +144,20 @@ public void render(GameContainer gc, StateBasedGame arg1, Graphics g)
g.fill(new Rectangle(0, 0, gc.getWidth(), gc.getHeight()));

// this for loop draws all the particles
OsuPixels osupixelconverter = new OsuPixels();
OsuPixels osupixelconverter = new OsuPixels(new GameScreen(gc));
for (Particle particle : particles) {
Vector2f center = osupixelconverter.osuPixeltoXY(gc,
Vector2f center = osupixelconverter.osuPixeltoXY(
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);
float scalefactor = osupixelconverter.getScaleFactor();
for (int index = 0; index < hitobjects.size(); index++) {
HitObject hitobject = hitobjects.get(index);
Vector2f currentcirclepos = osupixelconverter.osuPixeltoXY(gc,
Vector2f currentcirclepos = osupixelconverter.osuPixeltoXY(
new Vector2f(hitobject.x, hitobject.y)); // converts from osupixels to
// real display position
if (index != hitobjects.size() - 1) { // check to make sure hitobject is not
Expand All @@ -166,7 +166,7 @@ public void render(GameContainer gc, StateBasedGame arg1, Graphics g)
g.setColor(Color.white);

// draws a line in between consecutive hit objects
Vector2f nextcirclepos = osupixelconverter.osuPixeltoXY(gc, new Vector2f(
Vector2f nextcirclepos = osupixelconverter.osuPixeltoXY(new Vector2f(
hitobjects.get(index + 1).x, hitobjects.get(index + 1).y)); // converts
// from
// osupixels
Expand Down Expand Up @@ -280,13 +280,13 @@ public boolean isAcceptingInput() {

public void click() {
int x = inp.getMouseX(), y = inp.getMouseY();
OsuPixels osupixelstoxy = new OsuPixels();
float scalefactor = osupixelstoxy.getScaleFactor(gamecontainer);
OsuPixels osupixelstoxy = new OsuPixels(new GameScreen(gamecontainer));
float scalefactor = osupixelstoxy.getScaleFactor();
for (HitObject hitobject : hitobjects) {
// checks if current circle has already been clicked, then checks if click is
// within the circle
if (!hitobject.clicked && new Vector2f(x, y)
.distance(osupixelstoxy.osuPixeltoXY(gamecontainer,
.distance(osupixelstoxy.osuPixeltoXY(
new Vector2f(hitobject.x, hitobject.y))) < innerRadius
* scalefactor) {
// changes hitobject click state
Expand Down
7 changes: 7 additions & 0 deletions src/game/Screen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package game;

public interface Screen {
int getWidth();

int getHeight();
}
24 changes: 24 additions & 0 deletions src/test/MockScreen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package test;

import game.Screen;

public class MockScreen implements Screen {
private int height;
private int width;

public MockScreen(int height, int width) {
this.height = height;
this.width = width;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}

}
22 changes: 9 additions & 13 deletions src/test/OsuPixelsTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package test;

import org.junit.Test;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.geom.Vector2f;

import game.OsuPixels;
import junit.framework.Assert;

public class OsuPixelsTest {

@Test
public void test() {
// TODO: Integrate
}

/**
* Tests if OsuPixels returns the right vectors.
* <p>
Expand All @@ -23,19 +18,20 @@ public void test() {
* The game container, used for osupixel conversion.
* @return Returns whether or not the test was successful.
*/
public boolean testOsuPixels(GameContainer gc) {
OsuPixels osupixelconverter = new OsuPixels();
@Test
public void testOsuPixels() {
OsuPixels osupixelconverter = new OsuPixels(new MockScreen(1024, 568));
for (int x = 0; x <= 512; x++) {
for (int y = 0; y <= 384; y++) {
Vector2f test = new Vector2f(x, y);
test = osupixelconverter.osuPixeltoXY(gc, test);
test = osupixelconverter.XYtoOsuPixel(gc, test);
if ((int) test.x != x || (int) test.y != y) {
return false;
test = osupixelconverter.osuPixeltoXY(test);
test = osupixelconverter.XYtoOsuPixel(test);
if ((int) test.x != x || (int) (test.y + .5) != y) {
Assert.fail(String.format("calculated: %f %f instead of %d %d",
test.x, test.y, x, y));
}
}
}
return true;
}

}

0 comments on commit f9eceda

Please sign in to comment.