Skip to content

Commit

Permalink
Update A Simple Game (#3)
Browse files Browse the repository at this point in the history
* Implemented the latest version of the tutorial.

* Added a preload filter to prevent loading the large MP3 file at startup.
  • Loading branch information
raeleus authored Sep 4, 2024
1 parent cbf853a commit 699d13c
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 59 deletions.
File renamed without changes.
Binary file removed a-simple-game/assets/drop.wav
Binary file not shown.
122 changes: 66 additions & 56 deletions a-simple-game/core/src/main/java/com/badlogic/drop/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
Expand All @@ -11,57 +11,47 @@
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class Main implements ApplicationListener {
Texture backgroundTexture;
Texture bucketTexture;
Texture dropTexture;
Texture splashTexture;
Sprite bucketSprite;
Sound dropSound;
Music music;
SpriteBatch spriteBatch;
FitViewport viewport;
ScreenViewport screenViewport;
Sprite bucketSprite;
Vector2 touchPos;
Array<Sprite> dropSprites;
float dropTimer;
Rectangle bucketRectangle;
Rectangle dropRectangle;
Vector3 touchPos;
Sound dropSound;
Music music;
public boolean clickedSplash;
private long lastDropTime;
Texture splashTexture;
ScreenViewport screenViewport;
public Preloader preloader;

@Override
public void create() {
backgroundTexture = new Texture("background.png");
bucketTexture = new Texture("bucket.png");
dropTexture = new Texture("drop.png");
splashTexture = new Texture("splash.png");

viewport = new FitViewport(8, 5);
screenViewport = new ScreenViewport();
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
spriteBatch = new SpriteBatch();

viewport = new FitViewport(8, 5);
bucketSprite = new Sprite(bucketTexture);
bucketSprite.setSize(1, 1);

touchPos = new Vector3();

touchPos = new Vector2();
dropSprites = new Array<>();

bucketRectangle = new Rectangle();
dropRectangle = new Rectangle();

dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
music.setLooping(true);
music.setVolume(.5f);
lastDropTime = TimeUtils.millis();
splashTexture = new Texture("splash.png");
screenViewport = new ScreenViewport();
}

@Override
Expand All @@ -73,55 +63,47 @@ public void resize(int width, int height) {
@Override
public void render() {
if (!clickedSplash) splashRender();
else gameRender();
else {
input();
logic();
draw();
}
}

private void gameRender() {
float worldWidth = viewport.getWorldWidth();
float worldHeight = viewport.getWorldHeight();

//input
private void input() {
float speed = 4f;
float delta = Gdx.graphics.getDeltaTime();

if (Gdx.input.isKeyPressed(Keys.LEFT)) {
bucketSprite.setX(bucketSprite.getX() - 4f * delta);
} else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
bucketSprite.setX(bucketSprite.getX() + 4f * delta);
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
bucketSprite.translateX(speed * delta);
} else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
bucketSprite.translateX(-speed * delta);
}

if (Gdx.input.isTouched()) {
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 1);
touchPos.set(Gdx.input.getX(), Gdx.input.getY());
viewport.unproject(touchPos);
bucketSprite.setX(touchPos.x);
bucketSprite.setCenterX(touchPos.x);
}
}

//logic
private void logic() {
float worldWidth = viewport.getWorldWidth();
float worldHeight = viewport.getWorldHeight();
float bucketWidth = bucketSprite.getWidth();
float bucketHeight = bucketSprite.getHeight();
if (bucketSprite.getX() < 0) bucketSprite.setX(0);
else if (bucketSprite.getX() > worldWidth - bucketWidth) bucketSprite.setX(worldWidth - bucketWidth);

long time = TimeUtils.millis();
if (time - lastDropTime > 1000) {
lastDropTime = time;
float dropWidth = 1;
float dropHeight = 1;

Sprite dropSprite = new Sprite(dropTexture);
dropSprite.setSize(dropWidth, dropHeight);
dropSprite.setX(MathUtils.random(0f, worldWidth - dropWidth));
dropSprite.setY(worldHeight);
dropSprites.add(dropSprite);
}

bucketSprite.setX(MathUtils.clamp(bucketSprite.getX(), 0, worldWidth - bucketWidth));

float delta = Gdx.graphics.getDeltaTime();
bucketRectangle.set(bucketSprite.getX(), bucketSprite.getY(), bucketWidth, bucketHeight);

for (int i = dropSprites.size - 1; i >= 0; i--) {
Sprite dropSprite = dropSprites.get(i);
float dropWidth = dropSprite.getWidth();
float dropHeight = dropSprite.getHeight();

dropSprite.setY(dropSprite.getY() - 2f * delta);
dropSprite.translateY(-2f * delta);
dropRectangle.set(dropSprite.getX(), dropSprite.getY(), dropWidth, dropHeight);

if (dropSprite.getY() < -dropHeight) dropSprites.removeIndex(i);
Expand All @@ -131,12 +113,22 @@ else if (bucketRectangle.overlaps(dropRectangle)) {
}
}

//rendering
dropTimer += delta;
if (dropTimer > 1f) {
dropTimer = 0;
createDroplet();
}
}

private void draw() {
ScreenUtils.clear(Color.BLACK);
viewport.apply();
spriteBatch.setProjectionMatrix(viewport.getCamera().combined);
spriteBatch.begin();

float worldWidth = viewport.getWorldWidth();
float worldHeight = viewport.getWorldHeight();

spriteBatch.draw(backgroundTexture, 0, 0, worldWidth, worldHeight);
bucketSprite.draw(spriteBatch);

Expand All @@ -151,7 +143,12 @@ private void splashRender() {
if (Gdx.input.isTouched()) {
clickedSplash = true;

music.play();
preloader.preloadBundle("delayed-loading", bundle -> {
music = Gdx.audio.newMusic(Gdx.files.internal("delayed-loading/music.mp3"));
music.setLooping(true);
music.setVolume(.5f);
music.play();
});
}

float worldWidth = viewport.getWorldWidth();
Expand All @@ -174,6 +171,19 @@ private void splashRender() {
spriteBatch.end();
}

private void createDroplet() {
float dropWidth = 1;
float dropHeight = 1;
float worldWidth = viewport.getWorldWidth();
float worldHeight = viewport.getWorldHeight();

Sprite dropSprite = new Sprite(dropTexture);
dropSprite.setSize(dropWidth, dropHeight);
dropSprite.setX(MathUtils.random(0f, worldWidth - dropWidth));
dropSprite.setY(worldHeight);
dropSprites.add(dropSprite);
}

@Override
public void pause() {

Expand Down
22 changes: 22 additions & 0 deletions a-simple-game/core/src/main/java/com/badlogic/drop/Preloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.badlogic.drop;

public interface Preloader {

/**Request that the named bundle be preloaded.
*
* @param bundle the name of the bundle
* @param callback to be called upon preload completion
*/
public void preloadBundle(String bundle, Callback callback);

/** Allows the Preloader to communicate back to the caller. */
public interface Callback {

/**Called when the named bundle has been preloaded.
*
* @param bundle the name of the bundle
*/
void onBundlePreloaded(String bundle);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

<!-- You usually won't need to make changes to the rest of this. -->
<set-configuration-property name="gdx.assetpath" value="../assets" />
<set-configuration-property name="gdx.assetfilterclass" value="com.badlogic.drop.gwt.DropAssetFilter"/>
<set-configuration-property name="xsiframe.failIfScriptTag" value="FALSE"/>
<!-- These two lines reduce the work GWT has to do during compilation and also shrink output size. -->
<set-property name="user.agent" value="gecko1_8, safari"/>
<collapse-property name="user.agent" values="*" />
<!-- Remove the "user.agent" lines above if you encounter issues with Safari or other Gecko browsers. -->
</module>
</module>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.badlogic.drop.gwt;

import com.badlogic.gdx.backends.gwt.preloader.DefaultAssetFilter;

public class DropAssetFilter extends DefaultAssetFilter {
@Override
public String getBundleName(String file) {
file = file.replace('\\', '/');
if (file.contains("delayed-loading/")) return "delayed-loading";
return super.getBundleName(file);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public GwtApplicationConfiguration getConfig () {

@Override
public ApplicationListener createApplicationListener () {
return new Main();
Main main = new Main();
main.preloader = new GwtPreloader(this);
return main;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.badlogic.drop.gwt;

import com.badlogic.drop.Preloader;
import com.badlogic.gdx.backends.gwt.GwtApplication;
import com.badlogic.gdx.backends.gwt.preloader.Preloader.PreloaderCallback;
import com.badlogic.gdx.backends.gwt.preloader.Preloader.PreloaderState;

public class GwtPreloader implements Preloader {

GwtApplication application;

public GwtPreloader(GwtApplication application) {
this.application = application;
}

@Override
public void preloadBundle (final String bundle, final Callback callback) {
application.getPreloader().preload(bundle + ".txt", new PreloaderCallback() {
@Override
public void update (PreloaderState state) {
if (state.hasEnded()) {
callback.onBundlePreloaded(bundle);
}
}
@Override
public void error (String file) {
}
});
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public static void main(String[] args) {
}

private static Lwjgl3Application createApplication() {
return new Lwjgl3Application(new Main(), getDefaultConfiguration());
Main main = new Main();
main.preloader = new NullPreloader();
return new Lwjgl3Application(main, getDefaultConfiguration());
}

private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.badlogic.drop.lwjgl3;

import com.badlogic.drop.Preloader;

public class NullPreloader implements Preloader {

@Override
public void preloadBundle (final String bundle, final Callback callback) {
callback.onBundlePreloaded(bundle);
}

}

0 comments on commit 699d13c

Please sign in to comment.