Compare commits
1 Commits
dev
...
sharding_a
Author | SHA1 | Date | |
---|---|---|---|
7a94db1383 |
@ -251,4 +251,90 @@ public class EntityFactory {
|
|||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static PolygonModel[] getPolygonShards(PolygonModel model, int shardCount) {
|
||||||
|
PolygonModel[] shards = new PolygonModel[shardCount];
|
||||||
|
Vector2 position = tmp.set(model.getPosition());
|
||||||
|
float[] vertices = model.getVertices();
|
||||||
|
|
||||||
|
int verticesPerShard = vertices.length / (shardCount * 2);
|
||||||
|
int remaining = vertices.length % (shardCount * 2);
|
||||||
|
|
||||||
|
int vertice = 0;
|
||||||
|
for (int i = 0; i < shardCount; i++) {
|
||||||
|
if (i + 1 == shardCount && remaining > 0) {
|
||||||
|
// Put the remaining vertices onto the last shard
|
||||||
|
verticesPerShard += remaining / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
float[] shardVertices = new float[(verticesPerShard + 1) * 2];
|
||||||
|
shardVertices[0] = 0;
|
||||||
|
shardVertices[1] = 0;
|
||||||
|
float minX = shardVertices[0], maxX = shardVertices[0];
|
||||||
|
float minY = shardVertices[1], maxY = shardVertices[1];
|
||||||
|
for (int j = 2, n = shardVertices.length; j < n; j += 2) {
|
||||||
|
float x = shardVertices[j] = vertices[vertice++] - position.x;
|
||||||
|
float y = shardVertices[j + 1] = vertices[vertice++] - position.y;
|
||||||
|
minX = x < minX ? x : minX;
|
||||||
|
maxX = x > maxX ? x : maxX;
|
||||||
|
minY = y < minY ? y : minY;
|
||||||
|
maxY = y > maxY ? y : maxY;
|
||||||
|
}
|
||||||
|
|
||||||
|
float centerX = minX + ((maxX - minX) / 2);
|
||||||
|
float centerY = minY + ((maxY - minY) / 2);
|
||||||
|
|
||||||
|
for (int j = 0, n = shardVertices.length; j < n; j += 2) {
|
||||||
|
shardVertices[j] -= centerX;
|
||||||
|
shardVertices[j + 1] -= centerY;
|
||||||
|
}
|
||||||
|
|
||||||
|
PolygonModel shard = new PolygonModel(model.getColor());
|
||||||
|
shard.setVertices(shardVertices);
|
||||||
|
shard.setPosition(tmp2.set(position).add(centerX, centerY));
|
||||||
|
shards[i] = shard;
|
||||||
|
}
|
||||||
|
|
||||||
|
return shards;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Entity[] splitAsteroidIntoShards(Engine engine, Entity asteroid, int shardCount) {
|
||||||
|
Entity[] entities = new Entity[shardCount];
|
||||||
|
PolygonModel asteroidModel = (PolygonModel) asteroid.getComponent(ModelComponent.class).model;
|
||||||
|
PolygonModel[] shards = getPolygonShards(asteroidModel, shardCount);
|
||||||
|
Vector2 asteroidPosition = asteroidModel.getPosition();
|
||||||
|
Vector2 asteroidVelocity = asteroid.getComponent(VelocityComponent.class).velocity;
|
||||||
|
int generation = asteroid.getComponent(AsteroidComponent.class).generation;
|
||||||
|
|
||||||
|
for (int i = 0; i < entities.length; i++) {
|
||||||
|
ModelComponent model = new ModelComponent();
|
||||||
|
model.model = shards[i];
|
||||||
|
|
||||||
|
PositionComponent position = new PositionComponent();
|
||||||
|
position.position = new Vector2(model.model.getPosition());
|
||||||
|
position.rotation = 90;
|
||||||
|
|
||||||
|
VelocityComponent velocity = new VelocityComponent();
|
||||||
|
velocity.velocity = new Vector2(shards[i].getPosition())
|
||||||
|
.sub(asteroidPosition)
|
||||||
|
.nor()
|
||||||
|
.rotate(rand.nextFloat(-15, 15)) // Slightly alter the direction each piece flies off in
|
||||||
|
.scl(asteroidVelocity.len() * 1f) // Set speed to asteroid's original velocity
|
||||||
|
.add(tmp2.set(asteroidVelocity).scl(0.25f));
|
||||||
|
velocity.angularVelocity = rand.nextFloat(-30, 30);
|
||||||
|
|
||||||
|
AsteroidComponent asteroidComponent = new AsteroidComponent();
|
||||||
|
asteroidComponent.generation = generation + 1;
|
||||||
|
|
||||||
|
Entity entity = createEntity(engine);
|
||||||
|
entity.addComponent(model);
|
||||||
|
entity.addComponent(position);
|
||||||
|
entity.addComponent(velocity);
|
||||||
|
entity.addComponent(asteroidComponent);
|
||||||
|
entity.addComponent(COLLIDER);
|
||||||
|
entities[i] = entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user