Last active
December 11, 2015 06:58
-
-
Save Pretz/4562537 to your computer and use it in GitHub Desktop.
samples for a blog post on LibGDX programming targeting OUYA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cursor { | |
public static float DEFAULT_VELOCITY = 500f; | |
private final Vector2 position; | |
public final Vector2 velocity; | |
public Cursor() { | |
position = new Vector2(0, 0); | |
velocity = new Vector2(0, 0); | |
} | |
public void updatePosition(float timeDelta) { | |
position.add(velocity.tmp().mul(timeDelta)); | |
} | |
public Vector2 getPosition() { | |
return position; | |
} | |
public void setPosition(float x, float y) { | |
this.position.set(x, y); | |
} | |
} | |
public class GameWorld { | |
private Vector2 cursorVec = new Vector2(0, 0); | |
private final Cursor cursor = new Cursor(); | |
private void processInput(float timeDelta) { | |
cursor.velocity.y = 0; | |
cursor.velocity.x = 0; | |
for (Controller controller : Controllers.getControllers()) { | |
float axisY = 0; | |
float axisX = 0; | |
if (Ouya.ID.equals(controller.getName())) { | |
axisY = controller.getAxis(Ouya.AXIS_LEFT_Y); | |
axisX = controller.getAxis(Ouya.AXIS_LEFT_X); | |
} | |
if (Math.abs(axisY) > 0.2) { | |
cursor.velocity.y = -axisY * Cursor.DEFAULT_VELOCITY; | |
} | |
if (Math.abs(axisX) > 0.2) { | |
cursor.velocity.x = axisX * Cursor.DEFAULT_VELOCITY; | |
} | |
} | |
} | |
public void update(float timeDelta) { | |
processInput(timeDelta); | |
cursor.updatePosition(timeDelta); | |
level.update(timeDelta); | |
} | |
@Override | |
public boolean mouseMoved(int screenX, int screenY) { | |
cursorVec.set(screenX, screenY, 0); | |
renderer.getCamera().unproject(cursorVec); | |
cursor.setPosition(cursorVec.x, cursorVec.y); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void processInput(float timeDelta) { | |
cursor.velocity.y = 0; | |
cursor.velocity.x = 0; | |
for (Controller controller : Controllers.getControllers()) { | |
float axisY = 0; | |
float axisX = 0; | |
if (Ouya.ID.equals(controller.getName())) { | |
axisY = controller.getAxis(Ouya.AXIS_LEFT_Y); | |
axisX = controller.getAxis(Ouya.AXIS_LEFT_X); | |
} | |
if (Math.abs(axisY) > 0.2) { | |
cursor.velocity.y = -axisY * Cursor.DEFAULT_VELOCITY; | |
} | |
if (Math.abs(axisX) > 0.2) { | |
cursor.velocity.x = axisX * Cursor.DEFAULT_VELOCITY; | |
} | |
} | |
} | |
public void update(float timeDelta) { | |
processInput(timeDelta); | |
cursor.position.add(cursor.velocity.tmp().mul(timeDelta)); | |
level.update(timeDelta); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void drawCursor(Cursor cursor) { | |
Vector2 position = cursor.getPosition(); | |
spriteBatch.setColor(white); | |
spriteBatch.draw(cursorTexture, position.x, position.y - cursorHeight, cursorWidth, cursorHeight); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment