151 lines
3.5 KiB
JavaScript
151 lines
3.5 KiB
JavaScript
let images = {};
|
|
let simulator = [];
|
|
let maxSpeed = 1;
|
|
let backgroundImage;
|
|
|
|
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, type){
|
|
let entity = {
|
|
x: x,
|
|
y: y,
|
|
ax: 0.0,
|
|
ay: 0.0,
|
|
vx: vx,
|
|
vy: vy,
|
|
rot: 0,
|
|
mass: getMass(type),
|
|
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;
|
|
}
|
|
|
|
function onHit(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, entity.y, 0);
|
|
rotate((Math.atan2(entity.vx, entity.vy) - Math.atan2(-1,0)) * 180/Math.PI);
|
|
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);
|
|
|
|
//Load Entities
|
|
let entityCount = 50;
|
|
for(let i = 0; i <= entityCount; i+=1){
|
|
createEntity(
|
|
Math.floor(Math.random() * windowWidth),
|
|
Math.floor(Math.random() * windowHeight),
|
|
(Math.random() * maxSpeed * 2)-maxSpeed,
|
|
(Math.random() * maxSpeed * 2)-maxSpeed,
|
|
getRandomType(),
|
|
);
|
|
}
|
|
}
|
|
|
|
function mouseClicked(){
|
|
console.log(mouseX, mouseY);
|
|
simulator.forEach((entity) =>{
|
|
|
|
})
|
|
}
|
|
|
|
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 > windowWidth){
|
|
entity.x = 0;
|
|
}
|
|
if (entity.x < 0){
|
|
entity.x = windowWidth;
|
|
}
|
|
if (entity.y > windowHeight){
|
|
entity.y = 0;
|
|
}
|
|
if (entity.y < 0){
|
|
entity.y = windowHeight;
|
|
}
|
|
}
|
|
//Draw Entity
|
|
drawEntity(entity);
|
|
}
|
|
} |