From f9eceda7d78d611c8b0e4527f80d44dd309a155d Mon Sep 17 00:00:00 2001 From: s-xuch Date: Thu, 5 Apr 2018 16:30:04 -0700 Subject: [PATCH] Mocked(?) unit test --- src/game/GameScreen.java | 22 ++++++++++++++++++++++ src/game/OsuPixels.java | 20 +++++++++++++------- src/game/RhythmState.java | 16 ++++++++-------- src/game/Screen.java | 7 +++++++ src/test/MockScreen.java | 24 ++++++++++++++++++++++++ src/test/OsuPixelsTest.java | 22 +++++++++------------- 6 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 src/game/GameScreen.java create mode 100644 src/game/Screen.java create mode 100644 src/test/MockScreen.java diff --git a/src/game/GameScreen.java b/src/game/GameScreen.java new file mode 100644 index 0000000..5deedcf --- /dev/null +++ b/src/game/GameScreen.java @@ -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(); + } + +} diff --git a/src/game/OsuPixels.java b/src/game/OsuPixels.java index 70c6066..29637fa 100644 --- a/src/game/OsuPixels.java +++ b/src/game/OsuPixels.java @@ -1,6 +1,5 @@ package game; -import org.newdawn.slick.GameContainer; import org.newdawn.slick.geom.Vector2f; /** @@ -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. @@ -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; @@ -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, @@ -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; diff --git a/src/game/RhythmState.java b/src/game/RhythmState.java index d19efe4..e0cf2ce 100644 --- a/src/game/RhythmState.java +++ b/src/game/RhythmState.java @@ -144,9 +144,9 @@ 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()); @@ -154,10 +154,10 @@ public void render(GameContainer gc, StateBasedGame arg1, Graphics g) } // 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 @@ -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 @@ -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 diff --git a/src/game/Screen.java b/src/game/Screen.java new file mode 100644 index 0000000..464189d --- /dev/null +++ b/src/game/Screen.java @@ -0,0 +1,7 @@ +package game; + +public interface Screen { + int getWidth(); + + int getHeight(); +} diff --git a/src/test/MockScreen.java b/src/test/MockScreen.java new file mode 100644 index 0000000..1d9ec45 --- /dev/null +++ b/src/test/MockScreen.java @@ -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; + } + +} diff --git a/src/test/OsuPixelsTest.java b/src/test/OsuPixelsTest.java index a848a2c..82ffdc2 100644 --- a/src/test/OsuPixelsTest.java +++ b/src/test/OsuPixelsTest.java @@ -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. *

@@ -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; } }