How would I make the following code respawn an already existing entity (that is disabled) and reenable the clone while spawning in the random area between 1000 1000?
This is my demo code.
var EntityCreator = pc.createScript('entityCreator');
EntityCreator.attributes.add('material', {
type: 'asset',
assetType: 'material'
});
EntityCreator.attributes.add('boxDimensions', {
type: 'number',
default: 10
});
EntityCreator.attributes.add('lifetime', {
type: 'number',
default: 5
});
EntityCreator.attributes.add('maxCubes', {
type: 'number',
default: 10
});
// initialize code called once per entity
EntityCreator.prototype.initialize = function() {
this.entities = [];
};
// update code called every frame
EntityCreator.prototype.update = function(dt) {
// Spawn new cubes if there are less than maxCubes
while (this.entities.length < this.maxCubes) {
this.spawnCube();
}
// Loop through Entities and delete them when their time is up
for (i = 0; i < this.entities.length; i++) {
this.entities[i].timer -= dt;
if (this.entities[i].timer < 0) {
// entity.destroy() deletes all components and removes Entity from the hierarchy
this.entities[i].entity.destroy();
// Remove from the local list
this.entities.splice(i, 1);
}
}
};
EntityCreator.prototype.spawnCube = function () {
var entity = new pc.Entity();
// Add a new Model Component and add it to the Entity.
entity.addComponent("render", {
type: 'box'
});
// set material
entity.render.material = this.material.resource;
// Move to a random position
entity.setLocalPosition(
pc.math.random(-this.boxDimensions, this.boxDimensions),
pc.math.random(-this.boxDimensions, this.boxDimensions),
pc.math.random(-this.boxDimensions, this.boxDimensions)
);
// Add to the Hierarchy
this.app.root.addChild(entity);
// Store in a list for some random duration before deleting
this.entities.push({
entity: entity,
timer: pc.math.random(0, this.lifetime)
});
};