diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index 5bccb23..97a4957 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -44,7 +44,7 @@ public class EntityFactory { ModelComponent model = new ModelComponent(); model.model = new PolygonModel(Color.WHITE); - model.model.setVertices(new float[] { + model.model.setVertices(new float[]{ 0f, 4f, // tip -2.5f, -4f, // bottom left -1f, -2.5f, // indent @@ -54,7 +54,7 @@ public class EntityFactory { model.model.setScale(5); AccelerationComponent accel = new AccelerationComponent(); - accel.acceleration = new Vector2(0,1f); + accel.acceleration = new Vector2(0, 1f); Entity player = createEntity(engine); player.addComponent(position); @@ -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); - model.setVertices(new float[] { + 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); @@ -142,7 +144,7 @@ public class EntityFactory { ModelComponent model = new ModelComponent(); model.model = new PolygonModel(Color.YELLOW); - model.model.setVertices(new float[] { + model.model.setVertices(new float[]{ 1f, 0f, -1f, 0f, -1f, -4f,