Tweaks to player debris creation

Reduced explosion direction variation and max angular velocity so it's
more apparent that the lines were from the original ship.

Removed subtraction then adding back of player position from getLineModels

Formatting + comments.
This commit is contained in:
Matt Low 2020-01-29 12:48:44 +04:00
parent 0d323a69bc
commit d140959fdc

View File

@ -68,30 +68,28 @@ public class EntityFactory {
private static LineModel[] getLineModels(Model playerModel) {
float[] vertices = playerModel.getVertices();
Vector2 position = playerModel.getPosition();
LineModel[] models = new LineModel[(vertices.length / 2)];
for (int i = 0, n = vertices.length; i < n; i += 2) {
int bIndex = i + 2 == n? 0 : i + 2;
for (int aIndex = 0, n = vertices.length; aIndex < n; aIndex += 2) {
int bIndex = (aIndex + 2 == n) ? 0 : (aIndex + 2);
// tmpA, tmpB = points relative to 0
tmpA.set(vertices[i] - position.x, vertices[i + 1] - position.y);
tmpB.set(vertices[bIndex] - position.x, vertices[bIndex + 1] - position.y);
tmpA.set(vertices[aIndex], vertices[aIndex + 1]);
tmpB.set(vertices[bIndex], vertices[bIndex + 1]);
tmp.set(tmpB).sub(tmpA).scl(0.5f); // Cut line segment in half
tmp2.set(tmpA).add(tmp); // Find center of line segment
tmp.set(tmpB).sub(tmpA).scl(0.5f);
tmp2.set(tmpA).add(tmp); // tmp2 = center of line segment
tmpA.sub(tmp2); // make tmpA, tmpB relative to line center
tmpB.sub(tmp2);
LineModel model = new LineModel(Color.WHITE);
LineModel model = new LineModel(playerModel.getColor());
model.setVertices(new float[]{
tmpA.x, tmpA.y,
tmpB.x, tmpB.y,
});
model.setPosition(tmp2.add(position)); // Offset position to new line center
model.setPosition(tmp2); // Position = line center
models[i / 2] = model;
models[aIndex / 2] = model;
}
return models;
}
@ -100,7 +98,7 @@ public class EntityFactory {
Vector2 playerVelocity = player.getComponent(VelocityComponent.class).velocity;
PositionComponent playerPosition = player.getComponent(PositionComponent.class);
LineModel[] models = getLineModels(player.getComponent(ModelComponent.class).model);
Vector2 explosionCenter = tmp.set(playerPosition.position).sub(Utils.setUnitVectorAngle(tmp2, playerPosition.rotation).scl(10));
Vector2 explosionCenter = tmp.set(playerPosition.position).sub(Utils.setUnitVectorAngle(tmp2, playerPosition.rotation).scl(5));
Entity[] entities = new Entity[models.length];
for (int i = 0, n = models.length; i < n; i++) {
@ -108,13 +106,17 @@ public class EntityFactory {
model.model = models[i];
PositionComponent position = new PositionComponent();
position.position = model.model.getPosition();
position.position = new Vector2(model.model.getPosition());
position.rotation = 90;
VelocityComponent velocity = new VelocityComponent();
velocity.velocity = new Vector2(models[i].getPosition()).sub(explosionCenter).nor().rotate(rand.nextFloat(-45,45)).scl(rand.nextFloat(75,100));
velocity.velocity.add(tmp2.set(playerVelocity).scl(0.75f));
velocity.angularVelocity = rand.nextFloat(-360, 360);
velocity.velocity = new Vector2(models[i].getPosition())
.sub(explosionCenter)
.nor() // Direction from explosion center to center of piece
.rotate(rand.nextFloat(-15, 15)) // Slightly alter the direction each piece flies off in
.scl(rand.nextFloat(75, 100)) // Give each piece a slightly different speed
.add(tmp2.set(playerVelocity).scl(0.75f)); // Maintain 75% of the player's velocity at impact
velocity.angularVelocity = rand.nextFloat(-60, 60); // Make each piece spin at a different rate
Entity entity = createEntity(engine);
entity.addComponent(position);