190 lines
5.0 KiB
JavaScript
190 lines
5.0 KiB
JavaScript
let images = {};
|
|
let simulator = [];
|
|
let maxSpeed = 1;
|
|
let backgroundImage;
|
|
let worldScale;
|
|
let LEFT = [-1,0];
|
|
|
|
const entityTypes = {
|
|
METERORITE_SMALL: "meteorite_small",
|
|
METERORITE_LARGE: "meteorite_large",
|
|
COMET: "comet",
|
|
};
|
|
|
|
let Settings = {
|
|
windowWrap: true,
|
|
physics: true,
|
|
}
|
|
|
|
function getMass(type){
|
|
switch (type){
|
|
case entityTypes.COMET: return 1400.0;
|
|
case entityTypes.METERORITE_SMALL: return 500.0;
|
|
case entityTypes.METERORITE_LARGE: return 1000.0;
|
|
}
|
|
}
|
|
|
|
function createEntity(x, y, vx, vy, ax, ay, rot, rotv, type){
|
|
let entity = {
|
|
x: x,
|
|
y: y,
|
|
ax: ax,
|
|
ay: ay,
|
|
vx: vx,
|
|
vy: vy,
|
|
rot: rot,
|
|
rotv: rotv,
|
|
mass: getMass(type),
|
|
forcex: 0.0,
|
|
forcey: 0.0,
|
|
type: type,
|
|
}
|
|
simulator.push(entity);
|
|
}
|
|
|
|
|
|
function getRandomType(){
|
|
let rand = Math.random();
|
|
if (rand > 0.955){
|
|
console.log("Comet made");
|
|
return entityTypes.COMET;
|
|
}if(rand > 0.4){
|
|
return entityTypes.METERORITE_SMALL;
|
|
}else{
|
|
return entityTypes.METERORITE_LARGE;
|
|
}
|
|
}
|
|
|
|
function physicsUpdate(entity){
|
|
entity.vx += entity.ax;
|
|
entity.vy += entity.ay;
|
|
entity.x += entity.vx;
|
|
entity.y += entity.vy;
|
|
entity.rot += entity.rotv;
|
|
|
|
}
|
|
|
|
function onHit(entity1, entity2){
|
|
if(entity1.type == entityTypes.METERORITE_LARGE){
|
|
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{
|
|
Explode(entity1);
|
|
}
|
|
|
|
if(entity2.type == entityTypes.METERORITE_LARGE){
|
|
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(entity1.type == entityTypes.COMET){
|
|
|
|
}else{
|
|
Explode(entity2);
|
|
}
|
|
|
|
}
|
|
|
|
function Explode(entity){
|
|
|
|
}
|
|
|
|
function drawBackground(refImage){
|
|
let w = windowWidth / refImage.width;
|
|
let h = windowHeight / refImage.height;
|
|
for (let iy = 0; iy <= h; iy++){
|
|
for (let ix = 0; ix <= w; ix++){
|
|
image(refImage, refImage.width * ix, refImage.height * iy);
|
|
}
|
|
}
|
|
}
|
|
|
|
function drawEntity(entity){
|
|
push();
|
|
imageMode(CENTER);
|
|
translate(entity.x*(worldScale.value()/100), entity.y*(worldScale.value()/100), 0);
|
|
if (entity.type == entityTypes.COMET){
|
|
rotate((Math.atan2(entity.vx, entity.vy) - Math.atan2(-1,0)) * 180/Math.PI);
|
|
|
|
}else{
|
|
rotate(entity.rot);
|
|
}
|
|
image(images[entity.type], 0,0);
|
|
pop();
|
|
}
|
|
|
|
function preload(){
|
|
images[entityTypes.METERORITE_SMALL] = loadImage("/media/Art/meteorite_small_1.png");
|
|
images[entityTypes.METERORITE_LARGE] = loadImage("/media/Art/Meteorite_Large_1_100.png");
|
|
images[entityTypes.COMET] = loadImage("/media/Art/comet.png");
|
|
//images["ship"] = loadImage("/media/Art/ship.png"); #Make Ship asset for game portion
|
|
images["background"] = loadImage("/media/Art/EmptySpace.png");
|
|
}
|
|
|
|
function setup(){
|
|
let canvas = createCanvas(windowWidth, windowHeight);
|
|
background(255);
|
|
describe("A space physics simulator");
|
|
|
|
canvas.position(0,0);
|
|
worldScale = createSlider();
|
|
//Load Entities
|
|
let entityCount = 50;
|
|
let rotationSpeed = 100.0;
|
|
for(let i = 0; i <= entityCount; i+=1){
|
|
createEntity(
|
|
Math.floor(Math.random() * windowWidth), //X Location
|
|
Math.floor(Math.random() * windowHeight), //Y Location
|
|
(Math.random() * maxSpeed * 2)-maxSpeed, //X Velocity
|
|
(Math.random() * maxSpeed * 2)-maxSpeed, //Y Velocity
|
|
0.0, //X Acceleration
|
|
0.0, //Y Acceleration
|
|
0.0, //Current Rotation
|
|
(Math.random() * 2 *(Math.PI / rotationSpeed))-(Math.PI / rotationSpeed), //Rotation Velocity
|
|
getRandomType(),
|
|
);
|
|
}
|
|
}
|
|
|
|
function mouseClicked(){
|
|
console.log(mouseX, mouseY);
|
|
simulator.forEach((entity) =>{
|
|
|
|
})
|
|
console.log(worldScale.value());
|
|
}
|
|
|
|
function draw(){
|
|
//background(images["background"]);
|
|
//Draw Tiled Background
|
|
drawBackground(images["background"]);
|
|
|
|
for(let e = 1; e < simulator.length; e++){
|
|
let entity = simulator[e]
|
|
|
|
//Physics Update
|
|
if (Settings.physics){
|
|
physicsUpdate(entity)
|
|
}
|
|
|
|
//Window Wrap
|
|
if (Settings.windowWrap){
|
|
if (entity.x * (worldScale.value() /100) > windowWidth){
|
|
entity.x = 0;
|
|
}
|
|
if (entity.x < 0){
|
|
entity.x = windowWidth / (worldScale.value() /100);
|
|
}
|
|
if (entity.y * (worldScale.value() /100) > windowHeight){
|
|
entity.y = 0;
|
|
}
|
|
if (entity.y < 0){
|
|
entity.y = windowHeight / (worldScale.value() /100);
|
|
}
|
|
}
|
|
//Draw Entity
|
|
drawEntity(entity);
|
|
}
|
|
}
|
|
|
|
function windowResized() {
|
|
resizeCanvas(windowWidth, windowHeight);
|
|
} |