fantasy/painting/factory.js

32 lines
734 B
JavaScript

import Rectangle from "./rectangle.js";
import Circle from "./circle.js";
import Triangle from "./triangle.js";
const options = {
rectangle: Rectangle,
circle : Circle,
triangle : Triangle
};
let collection = [
Circle, Rectangle, Triangle
];
export default {
createRandomShape: function() {
let Shape = collection[Math.floor(Math.random() * collection.length)];
return Shape.fromOptions(Shape.randomOptions());
},
setCollection(obj) {
collection = [];
let keys = Object.keys(obj);
for (let i = 0; i < keys.length; ++i) {
let key = keys[i];
if (obj[key]) {
collection.push(options[key]);
}
}
}
};