I am in the process of working out a way to handle entities in my game. I decided to go with a Factory class that handles this stuff.
It will be responsible for updating and deleting entites from the world, from drops, bullets, particles etc etc.
Problem being, it seems to freeze my game when doing large operations. I have a Grenade class that you can throw, it explodes and creates around 45 Shrapnel objects around it (box2d) and accelerates them away from the explosion point.
This does 3 things, it first loops through the Explosives array of Shrapnel, fires them all in there direction.
It adds these shrapnels objects to another array in the Factory class which updates them, then removes them if needed
The renderer, well it renderes the graphics.
This means there is pretty much 3 loops running, only 1 of which handles any graphics. Should I move my logic onto a seperate thread? I have the thread setup and ready to go but is there something else I can do?
Here is some code for you to gander over:
Player presses G, it calls this method and creates a small rectangle which acts as a grenade
/**
* Throw this explosive in the direction of the thrower
*
* @param postion
* the starting position
* @param angle
* the angle to be thrown, used to calculate the trajectory
* @param power
* power of the throw
*/
public void throwExplosive(Vector2 postion, float angle, float power) {
createBody(BodyType.DynamicBody, postion.x, postion.y, angle);
createPolyFixture(0.10f, 0.10f, 0.10f, 0.10f, 0.15f, false);
createTime = TimeUtils.nanoTime();
body.applyLinearImpulse(power * MathUtils.cos(angle),
power * MathUtils.sin(angle), 0, 0, true);
body.setUserData(this);
isThrown = true;
}
Then this grenade gets added to an array for updating
/**
* Update this explosive object, keeps detonation timers in check
*/
public void update() {
if (isThrown) {
if (TimeUtils.nanoTime() - createTime > fragTime) {
detonate(body.getPosition());
isThrown = false;
}
}
}
Then this is called
/**
* Detonate this explosive and send shrapnel flying out
*/
public void detonate(Vector2 position) {
for (Shrapnel shrapnel : this.shrapnel) {
shrapnel.fire(position);
}
}
Which basically does this
/**
* Fire this shrapnel in its assigned direction, call this to create an
* explosion type effect
*/
public void fire(Vector2 position) {
createBody(BodyType.DynamicBody, position.x, position.y, angle);
createPolyFixture(0.1f, 0.1f, 0.75f, 0, 0.2f, true);
createTime = TimeUtils.nanoTime();
body.applyLinearImpulse(speed * MathUtils.cos(angle),
speed * MathUtils.sin(angle), 0, 0, true);
Level.factory.projectiles.add(this);
body.setUserData(this);
}
If you are wondering when the shrapnel gets added to the array, it happens in the constructor of whatever explosive class is being called, currently I just have a Grenade class:
public Grenade() {
fragTime = MathUtility.secondToNano(3);
float angle = 0;
for (int count = 0; count < 45; count++) {
Shrapnel shrapnel = new Shrapnel(this, true, position, angle,
MathUtility.secondToNano(5), 0.05f, 3);
this.shrapnel.add(shrapnel);
angle += 8 * MathUtils.degRad;
}
}