Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.destinationsol.menu.background;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
Expand Down Expand Up @@ -64,11 +65,11 @@ public MenuBackgroundAsteroidManager(DisplayDimensions displayDimensions, World
}
}

public void update() {
public void update(float delta) {
retainedBackgroundAsteroids.clear();

for (MenuBackgroundObject backgroundObject : backgroundAsteroids) {
backgroundObject.update();
backgroundObject.update(delta);

boolean isInWidth = Math.abs(backgroundObject.getPosition().x) < MenuBackgroundManager.VIEWPORT_HEIGHT * displayDimensions.getRatio() * 0.8f;
boolean isInHeight = Math.abs(backgroundObject.getPosition().y) < MenuBackgroundManager.VIEWPORT_HEIGHT * 0.8f;
Expand Down Expand Up @@ -123,7 +124,7 @@ public MenuBackgroundObject buildAsteroid() {
Body body = asteroidMeshLoader.getBodyAndSprite(world, texture, size, BodyDef.BodyType.DynamicBody, position, angle, new ArrayList<>(), 10f, DrawableLevel.BODIES);
body.setLinearVelocity(velocity);
body.setAngularVelocity(angularVelocity);
MenuBackgroundObject asteroid = new MenuBackgroundObject(texture, size, tint, position, velocity, asteroidMeshLoader.getOrigin(texture.name, size).cpy(), angle, body);
MenuBackgroundObject asteroid = new MenuBackgroundObject(new Animation<>(Float.MAX_VALUE, texture), size, tint, position, velocity, asteroidMeshLoader.getOrigin(texture.name, size).cpy(), angle, body);
body.setUserData(asteroid);

return asteroid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public MenuBackgroundManager(DisplayDimensions displayDimensions) {
backgroundCamera = new OrthographicCamera(VIEWPORT_HEIGHT * displayDimensions.getRatio(), -VIEWPORT_HEIGHT);
}

public void update() {
asteroidManager.update();
shipManager.update();
public void update(float delta) {
asteroidManager.update(delta);
shipManager.update(delta);
world.step(Const.REAL_TIME_STEP, 6, 2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.destinationsol.menu.background;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
Expand All @@ -27,7 +28,7 @@
* Contains common data holders required for all background objects.
*/
public class MenuBackgroundObject {
TextureAtlas.AtlasRegion texture;
Animation<TextureAtlas.AtlasRegion> frames;

float scale;

Expand All @@ -41,15 +42,18 @@ public class MenuBackgroundObject {

Body body;

public MenuBackgroundObject(TextureAtlas.AtlasRegion texture, float scale, Color tint, Vector2 position, Vector2 velocity, Vector2 origin, float angle, Body body) {
this.texture = texture;
float animationTime;

public MenuBackgroundObject(Animation<TextureAtlas.AtlasRegion> frames, float scale, Color tint, Vector2 position, Vector2 velocity, Vector2 origin, float angle, Body body) {
this.frames = frames;
this.scale = scale;
this.tint = tint;
this.position = position;
this.velocity = velocity;
this.origin = origin;
this.angle = angle;
this.body = body;
this.animationTime = 0.0f;
}

public void setParamsFromBody() {
Expand All @@ -58,12 +62,13 @@ public void setParamsFromBody() {
angle = body.getAngle() * MathUtils.radDeg;
}

public void update() {
public void update(float delta) {
this.animationTime += delta;
setParamsFromBody();
}

public void draw(UiDrawer drawer) {
drawer.draw(texture, scale, scale, origin.x, origin.y, position.x, position.y, angle, tint);
drawer.draw(frames.getKeyFrame(animationTime, true), scale, scale, origin.x, origin.y, position.x, position.y, angle, tint);
}

public Vector2 getPosition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.destinationsol.menu.background;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
Expand Down Expand Up @@ -76,6 +77,7 @@ public MenuBackgroundShipManager(DisplayDimensions displayDimensions, World worl


public void addShip(String urnString) {
Animation<TextureAtlas.AtlasRegion> frames = Assets.getAnimation(urnString);
TextureAtlas.AtlasRegion texture = Assets.getAtlasRegion(urnString);
JSONObject rootNode = Validator.getValidatedJSON(urnString, "engine:schemaHullConfig");

Expand All @@ -89,12 +91,12 @@ public void addShip(String urnString) {
Body body = shipMeshLoader.getBodyAndSprite(world, texture, scale, BodyDef.BodyType.DynamicBody, position, angle, new ArrayList<>(), Float.MAX_VALUE, DrawableLevel.BODIES);
body.setLinearVelocity(velocity);
Vector2 origin = shipMeshLoader.getOrigin(texture.name, scale);
MenuBackgroundObject ship = new MenuBackgroundObject(texture, scale, SolColor.WHITE, position, velocity, origin.cpy(), angle, body);
MenuBackgroundObject ship = new MenuBackgroundObject(frames, scale, SolColor.WHITE, position, velocity, origin.cpy(), angle, body);
backgroundShips.add(ship);
}

public void update() {
backgroundShips.forEach(ship -> ship.update());
public void update(float deltaTime) {
backgroundShips.forEach(ship -> ship.update(deltaTime));
backgroundShips.removeIf(ship -> ship.getPosition().y >= 3f);
if (backgroundShips.isEmpty()) {
//Spawn a random ship
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public void update(float delta) {
textColor.setAlpha(Math.clamp(0.0f, 1.0f, alpha));
textSkin.getDefaultStyleFor(creditsText.getFamily()).setTextColor(textColor);

solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void update(float delta) {
}
}

solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void onAdded() {
@Override
public void update(float delta) {
super.update(delta);
solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onAdded() {
@Override
public void update(float delta) {
super.update(delta);
solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.destinationsol.ui.nui.screens.mainMenu;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import org.destinationsol.SolApplication;
import org.destinationsol.assets.Assets;
import org.destinationsol.assets.json.Json;
Expand All @@ -25,23 +27,23 @@
import org.destinationsol.ui.nui.NUIManager;
import org.destinationsol.ui.nui.NUIScreenLayer;
import org.destinationsol.ui.nui.widgets.KeyActivatedButton;
import org.destinationsol.ui.nui.widgets.UIAnimatedImage;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.gestalt.assets.ResourceUrn;
import org.terasology.gestalt.module.Module;
import org.terasology.gestalt.naming.Name;
import org.terasology.joml.geom.Rectanglei;
import org.terasology.nui.Canvas;
import org.terasology.nui.UITextureRegion;
import org.terasology.nui.backends.libgdx.GDXInputUtil;
import org.terasology.nui.widgets.UIButton;
import org.terasology.nui.widgets.UIImage;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class NewShipScreen extends NUIScreenLayer {
Expand All @@ -50,7 +52,7 @@ public class NewShipScreen extends NUIScreenLayer {
private final ModuleManager moduleManager;
private int playerSpawnConfigIndex = 0;
private List<String> playerSpawnConfigNames = new ArrayList<>();
private List<UITextureRegion> playerSpawnConfigTextures = new ArrayList<>();
private List<ShipSpriteData> playerSpawnConfigTextures = new ArrayList<>();
private WorldConfig worldConfig;

@Inject
Expand Down Expand Up @@ -82,7 +84,14 @@ public void initialise() {
for (String spawnConfigName : playerSpawnConfigs.keySet()) {
JSONObject playerSpawnConfig = playerSpawnConfigs.getJSONObject(spawnConfigName);
try {
playerSpawnConfigTextures.add(Assets.getDSTexture(playerSpawnConfig.getString("hull")).getUiTexture());
String shipHullName = playerSpawnConfig.getString("hull");
Animation<TextureAtlas.AtlasRegion> shipAnimation = Assets.getAnimation(shipHullName);
UITextureRegion shipAnimationTextureRegion = Assets.getDSTexture(shipHullName).getUiTexture();
List<Rectanglei> frames = new ArrayList<>();
for (TextureAtlas.AtlasRegion frame : shipAnimation.getKeyFrames()) {
frames.add(new Rectanglei(frame.getRegionX(), frame.getRegionY() - frame.getRegionHeight(), frame.getRegionX() + frame.getRegionWidth(), frame.getRegionY()));
}
playerSpawnConfigTextures.add(new ShipSpriteData(shipAnimationTextureRegion, shipAnimation.getFrameDuration(), frames));
} catch (RuntimeException e) {
logger.error("Failed to load ship texture!", e);
// Null values will not render any texture.
Expand All @@ -91,15 +100,19 @@ public void initialise() {
}
}

UIImage shipPreviewImage = find("shipPreviewImage", UIImage.class);
shipPreviewImage.setImage(playerSpawnConfigTextures.get(playerSpawnConfigIndex));
UIAnimatedImage shipPreviewImage = find("shipPreviewImage", UIAnimatedImage.class);
shipPreviewImage.setSpritesheet(playerSpawnConfigTextures.get(playerSpawnConfigIndex).texture);
shipPreviewImage.setFrameDuration(playerSpawnConfigTextures.get(playerSpawnConfigIndex).frameDuration);
shipPreviewImage.setFrames(playerSpawnConfigTextures.get(playerSpawnConfigIndex).frames);

UIButton startingShipButton = find("startingShipButton", UIButton.class);
startingShipButton.setText("Starting Ship: " + playerSpawnConfigNames.get(playerSpawnConfigIndex));
startingShipButton.subscribe(button -> {
playerSpawnConfigIndex = (playerSpawnConfigIndex + 1) % playerSpawnConfigNames.size();
((UIButton)button).setText("Starting Ship: " + playerSpawnConfigNames.get(playerSpawnConfigIndex));
shipPreviewImage.setImage(playerSpawnConfigTextures.get(playerSpawnConfigIndex));
shipPreviewImage.setSpritesheet(playerSpawnConfigTextures.get(playerSpawnConfigIndex).texture);
shipPreviewImage.setFrameDuration(playerSpawnConfigTextures.get(playerSpawnConfigIndex).frameDuration);
shipPreviewImage.setFrames(playerSpawnConfigTextures.get(playerSpawnConfigIndex).frames);
});

UIButton modulesButton = find("modulesButton", UIButton.class);
Expand Down Expand Up @@ -150,7 +163,7 @@ public void onAdded() {
@Override
public void update(float delta) {
super.update(delta);
solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand All @@ -166,4 +179,16 @@ public void onDraw(Canvas canvas) {
protected boolean escapeCloses() {
return false;
}

private static class ShipSpriteData {
public final float frameDuration;
public final List<Rectanglei> frames;
public final UITextureRegion texture;

public ShipSpriteData(UITextureRegion texture, float frameDuration, List<Rectanglei> frames) {
this.texture = texture;
this.frameDuration = frameDuration;
this.frames = frames;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void onAdded() {
@Override
public void update(float delta) {
super.update(delta);
solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void initialise() {
@Override
public void update(float delta) {
super.update(delta);
solApplication.getMenuBackgroundManager().update();
solApplication.getMenuBackgroundManager().update(delta);
}

@Override
Expand Down
Loading