Skip to content

Instantly share code, notes, and snippets.

@maxandersen
Last active January 9, 2022 09:22
Show Gist options
  • Save maxandersen/d4e465ab26ae5d85b7090aecf4003dc1 to your computer and use it in GitHub Desktop.
Save maxandersen/d4e465ab26ae5d85b7090aecf4003dc1 to your computer and use it in GitHub Desktop.
Run this with: jbang <gisturl>
//DEPS com.github.almasb:fxgl:17
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.concurrent.AsyncTask;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import com.almasb.fxgl.entity.components.CollidableComponent;
import com.almasb.fxgl.input.UserAction;
import com.almasb.fxgl.physics.BoundingShape;
import com.almasb.fxgl.physics.HitBox;
import com.almasb.fxgl.physics.PhysicsComponent;
import com.almasb.fxgl.physics.box2d.dynamics.BodyType;
import com.almasb.fxgl.physics.box2d.dynamics.FixtureDef;
import com.almasb.fxgl.texture.Texture;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.almasb.fxgl.dsl.FXGL.entityBuilder;
import static com.almasb.fxgl.dsl.FXGL.getAssetLoader;
import static com.almasb.fxgl.dsl.FXGL.getExecutor;
import static com.almasb.fxgl.dsl.FXGL.getGameScene;
import static com.almasb.fxgl.dsl.FXGL.getGameWorld;
import static com.almasb.fxgl.dsl.FXGL.getInput;
import static com.almasb.fxgl.dsl.FXGL.onCollisionBegin;
/**
* @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com)
*/
public class crazybounce extends GameApplication {
private enum TType {
BULLET, WALL
}
List<String> urls = Arrays.asList(
"https://design.jboss.org/weld/logo/images/weld_icon_256x.png",
"https://design.jboss.org/strimzi/logo/final/strimzilogomark_default_256px.png",
"https://design.jboss.org/quarkiverse/logo/final/PNG/quarkiverse_icon_default_256px.png",
"https://design.jboss.org/resteasy/logo/images/resteasy_logo_64x.png",
"https://design.jboss.org/kogito/logo/final/PNG/kogito_icon_rgb_color_default_256px.png",
"https://design.jboss.org/jbosstools/logo/jbosstools_icon64.png",
"https://design.jboss.org/hibernate/logo/final/hibernate_icon_mediumbkg_256px.png",
"https://design.jboss.org/jbossforge/logo/final/jbossforge_icon_256px.png",
"https://design.jboss.org/errai/logo/final/errai_icon_64px.png",
"https://design.jboss.org/drogueiot/logo/final/PNG/drogueiot_fullcolor_icon_256px.png",
"https://design.jboss.org/quarkus/logo/final/PNG/quarkus_icon_rgb_256px_default.png",
"https://design.jboss.org/arquillian/logo/final/arquillian_icon_256px.png",
"https://design.jboss.org/wildfly/logo/final/wildfly_icon_256px.png",
"https://raw.githubusercontent.com/jbangdev/jbang/main/images/jbang_icon.png",
"https://design.jboss.org/ceylon/logo/final/ceylon_icon_256px.png",
"https://design.jboss.org/byteman/logo/final/byteman_icon_256px.png",
"https://design.jboss.org/blip/images/icons/planner_icon_256px.png",
"https://design.jboss.org/debezium/logo/final/black/black_debezium_256px.png",
"https://design.jboss.org/dekorate/logo/final/png/dekorate_icon_rgb_blue_256px.png");
Map<String, Texture> textures = new HashMap<>();
Texture randomTexture() {
// returning copy as otherwise they dissapear when reused
return textures.get(randomTextureUrl()).copy();
}
private String randomTextureUrl() {
return urls.get(FXGL.random(0,urls.size()-1));
}
@Override
protected void initSettings(GameSettings settings) {
settings.setWidth(1280);
settings.setHeight(720);
}
@Override
protected void initInput() {
getInput().addAction(new UserAction("LMB") {
private double x;
private double y;
@Override
protected void onActionBegin() {
x = getInput().getMouseXWorld();
y = getInput().getMouseYWorld();
}
@Override
protected void onActionEnd() {
var endx = getInput().getMouseXWorld();
var endy = getInput().getMouseYWorld();
spawnBullet(x, y, endx - x, endy - y);
}
}, MouseButton.PRIMARY);
}
@Override
protected void initGame() {
getGameScene().setBackgroundColor(Color.LIGHTGRAY);
Map<String, AsyncTask> asyncs = new HashMap<>();
urls.forEach(url -> {
AsyncTask<Integer> async = getExecutor().startAsync(() -> {
System.out.println("Loading " + url);
Texture t = getAssetLoader().loadTexture(new URL(url), 450 / 15.0, 449 / 15.0);
textures.put(url, t);
System.out.println("DONE Loading " + url);
return 0;
});
asyncs.put(url,async);
});
asyncs.values().forEach(AsyncTask::await);
var screenBounds = entityBuilder().buildScreenBoundsAndAttach(50);
screenBounds.addComponent(new CollidableComponent(true));
screenBounds.setType(TType.WALL);
}
@Override
protected void initPhysics() {
onCollisionBegin(TType.WALL, TType.BULLET, (wall, bullet) -> {
var vx = bullet.getComponent(PhysicsComponent.class).getVelocityX();
var vy = bullet.getComponent(PhysicsComponent.class).getVelocityY();
// limit proximity spawns to 10
if (getGameWorld().getEntitiesInRange(bullet.getBoundingBoxComponent().range(100, 100)).size() < 10) {
spawnBullet(bullet.getX() + 30 * -Math.signum(vx), bullet.getY() + 30 * -Math.signum(vy), -Math.signum(vx) * 20, -Math.signum(vy) * 20);
}
});
onCollisionBegin(TType.BULLET, TType.BULLET, (bullet, otherbullet) -> {
var vx = bullet.getComponent(PhysicsComponent.class).getVelocityX();
var vy = bullet.getComponent(PhysicsComponent.class).getVelocityY();
// limit proximity spawns to 10
if (getGameWorld().getEntitiesInRange(bullet.getBoundingBoxComponent().range(100, 100)).size() < 10) {
spawnBullet(bullet.getX() + 30 * -Math.signum(vx), bullet.getY() + 30 * -Math.signum(vy), -Math.signum(vx) * 20, -Math.signum(vy) * 20);
}
});
}
private void spawnBullet(double x, double y, double vx, double vy) {
var physics = new PhysicsComponent();
physics.setFixtureDef(new FixtureDef().density(25.5f).restitution(0.1f));
physics.setBodyType(BodyType.DYNAMIC);
physics.setOnPhysicsInitialized(() -> {
physics.setLinearVelocity(vx * 10, vy * 10);
});
var texture = randomTexture();
entityBuilder()
.at(x, y)
.type(TType.BULLET)
.bbox(new HitBox(BoundingShape.circle(450 / 15.0 / 2.0)))
.view(texture)
//.view(texture(textureName(), 450 / 15.0, 449 / 15.0))
.with(physics)
.with(new ExpireCleanComponent(Duration.seconds(50)).animateOpacity())
.collidable()
.buildAndAttach();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment