Made new entities created use the old locations

This commit is contained in:
2025-10-29 16:39:04 -05:00
parent 0b1a622b99
commit 1202cd0221

View File

@ -24,8 +24,28 @@ function getMass(type){
} }
} }
function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){ function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type, id=0, iframes=Math.floor(Math.random()*100)){
let entity = { if (Settings.emptyEntries.length > 1){
var entryID = Settings.emptyEntries.pop();
if (Settings.devMode) console.warn("Creating new enity from old", entryID);
if (entryID <= simulator.length && entryID >= 0){
entity = simulator[entryID];
entity.x = x;
entity.y = y;
entity.vx = vx;
entity.vy = vy;
entity.ax = ax;
entity.ay = ay;
entity.rot = rot;
entity.rotv = rotv;
entity.iframes = 1000;
entity.show = true;
entity.type = type
}else{
if (Settings.devMode) console.error("Entry ID out of bounds", Settings.emptyEntries.length, id, entryID);
}
}else{
simulator.push({
x: x, x: x,
y: y, y: y,
ax: ax, ax: ax,
@ -38,8 +58,11 @@ function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){
forcex: 0.0, forcex: 0.0,
forcey: 0.0, forcey: 0.0,
type: type, type: type,
show: true,
ueid: id,
iframes: iframes
});
} }
simulator.push(entity);
} }
@ -56,29 +79,47 @@ function getRandomType(){
} }
function physicsUpdate(entity){ function physicsUpdate(entity){
if (entity.show){
entity.vx += entity.ax; entity.vx += entity.ax;
entity.vy += entity.ay; entity.vy += entity.ay;
entity.x += entity.vx; entity.x += entity.vx;
entity.y += entity.vy; entity.y += entity.vy;
entity.rot += entity.rotv; entity.rot += entity.rotv;
if (entity.iframes > 0) entity.iframes -= 1;
if (entity.iframes == 0) simulator.forEach((other_entity) =>{
checkCollision(entity, other_entity);
});
}
} }
function onHit(entity1, entity2){ function checkCollision(entity1, entity2){
if(entity1.type == entityTypes.METERORITE_LARGE){ if (entity1.type == entityTypes.COMET && entity2.type == entityTypes.COMET){
createEntity(entity1.x, entity1.y, entity2.vx-entity1.vx, entity2.vy-entity1.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL);
}if(entity1.type == entityTypes.COMET){
}else{ }else if (entity1.ueid != entity2.ueid
&& entity1.show
&& entity2.show){
var dist = distance(entity1, entity2);
if(dist < 15){
if (Settings.devMode) {
stroke('magenta');
strokeWeight(5);
line(entity1.x,entity1.y, entity2.x, entity2.y);
console.log("Collision at", entity1.x, entity1.y, entity1.iframes, entity2.x, entity2.y, dist);
}
Explode(entity1); Explode(entity1);
Explode(entity2);
}
}
} }
if(entity2.type == entityTypes.METERORITE_LARGE){ function Explode(entity){
createEntity(entity2.x, entity2.y, entity1.vx-entity2.vx, entity1.vy-entity2.vy, 0.0,0.0,entity2.rot-entity1.rot, entity2.rotv-entity1.rotv, entityTypes.METERORITE_SMALL); if(entity.type == entityTypes.METERORITE_SMALL){
}if(entity1.type == entityTypes.COMET){ entity.show = false;
Settings.emptyEntries.push(entity.ueid);
}else{ }if(entity.type == entityTypes.METERORITE_LARGE){
Explode(entity2); entity.type = entityTypes.METERORITE_SMALL
entity.iframes = 1000;
createEntity(entity.x, entity.y, entity.vx + Math.random(), entity.vy + Math.random(), 0.0,0.0,0.0,0.0, entityTypes.METERORITE_SMALL, id=entity.ueid, iframes=1000);
} }
} }
@ -127,9 +168,8 @@ function setup(){
canvas.position(0,0); canvas.position(0,0);
worldScale = createSlider(); worldScale = createSlider();
//Load Entities //Load Entities
let entityCount = 50;
let rotationSpeed = 100.0; for(var i = 0; i <= entityCount; i+=1){
for(let i = 0; i <= entityCount; i+=1){
createEntity( createEntity(
Math.floor(Math.random() * windowWidth), //X Location Math.floor(Math.random() * windowWidth), //X Location
Math.floor(Math.random() * windowHeight), //Y Location Math.floor(Math.random() * windowHeight), //Y Location
@ -140,6 +180,7 @@ function setup(){
0.0, //Current Rotation 0.0, //Current Rotation
(Math.random() * 2 *(Math.PI / rotationSpeed))-(Math.PI / rotationSpeed), //Rotation Velocity (Math.random() * 2 *(Math.PI / rotationSpeed))-(Math.PI / rotationSpeed), //Rotation Velocity
getRandomType(), getRandomType(),
id=i
); );
} }
} }