|
|
@@ -1,5 +1,6 @@
|
|
|
import * as three from "three-js";
|
|
|
import { actor } from "./actor.js";
|
|
|
+import { factor } from "./factor.js";
|
|
|
|
|
|
class render_engine {
|
|
|
#renderer;
|
|
|
@@ -15,7 +16,7 @@ class render_engine {
|
|
|
throw new TypeError("Canvas must be an HTMLCanvasElement.");
|
|
|
}
|
|
|
|
|
|
- if (!(context instanceof WebGLRenderingContext)) {
|
|
|
+ if (!(context instanceof WebGL2RenderingContext)) {
|
|
|
throw new TypeError("Context must be WebGL RenderingContext.");
|
|
|
}
|
|
|
|
|
|
@@ -23,11 +24,10 @@ class render_engine {
|
|
|
throw new TypeError("Player must be an actor.");
|
|
|
}
|
|
|
|
|
|
- this.#actors = {};
|
|
|
+ this.#actors = new Array();
|
|
|
this.#player = player;
|
|
|
this.#canvas = canvas;
|
|
|
this.#scene = new three.Scene();
|
|
|
- this.#scene.background = new three.Color(0x101010);
|
|
|
this.#camera = new three.PerspectiveCamera(75, 1, 0.1, 1000);
|
|
|
|
|
|
this.#renderer = new three.WebGLRenderer({
|
|
|
@@ -47,6 +47,26 @@ class render_engine {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ get player() {
|
|
|
+ return this.#player;
|
|
|
+ }
|
|
|
+
|
|
|
+ get canvas() {
|
|
|
+ return this.#canvas;
|
|
|
+ }
|
|
|
+
|
|
|
+ set background(target) {
|
|
|
+ if (typeof(target) !== "number") {
|
|
|
+ throw new TypeError("Background must be an number.");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.#scene.background = target;
|
|
|
+ }
|
|
|
+
|
|
|
+ get background() {
|
|
|
+ return this.#scene.background;
|
|
|
+ }
|
|
|
+
|
|
|
#resize_canvas() {
|
|
|
this.#canvas.width = window.innerWidth;
|
|
|
this.#canvas.height = window.innerHeight;
|
|
|
@@ -61,7 +81,6 @@ class render_engine {
|
|
|
|
|
|
run() {
|
|
|
this.#looped = true;
|
|
|
- this.#init();
|
|
|
this.#loop();
|
|
|
}
|
|
|
|
|
|
@@ -88,24 +107,13 @@ class render_engine {
|
|
|
setTimeout(() => { this.#loop(); }, new_frame);
|
|
|
}
|
|
|
|
|
|
- #init() {
|
|
|
- const geometry = new three.BoxGeometry(1, 1, 1);
|
|
|
- const material = new three.MeshStandardMaterial({ color: 0x009000 });
|
|
|
- this.#actors.cube = new three.Mesh(geometry, material);
|
|
|
- this.#actors.cube.position.x = 10;
|
|
|
- this.#actors.cube.position.y = 0;
|
|
|
- this.#actors.cube.position.z = 10;
|
|
|
-
|
|
|
- const light = new three.HemisphereLight(0x707070);
|
|
|
- light.position.x = 0;
|
|
|
- light.position.y = 10;
|
|
|
- light.position.z = 0;
|
|
|
-
|
|
|
- this.#scene.add(this.#actors.cube);
|
|
|
- this.#scene.add(light);
|
|
|
+ add_factor(target) {
|
|
|
+ if (!(target instanceof factor)) {
|
|
|
+ throw new TypeError("New factor must be in factor class.");
|
|
|
+ }
|
|
|
|
|
|
- this.#camera.position.y = 0;
|
|
|
- this.#player.rotate = 270;
|
|
|
+ this.#actors.push(target);
|
|
|
+ this.#scene.add(target.mesh);
|
|
|
}
|
|
|
|
|
|
#sync_camera() {
|
|
|
@@ -120,8 +128,10 @@ class render_engine {
|
|
|
|
|
|
#render() {
|
|
|
this.#sync_camera();
|
|
|
- this.#actors.cube.rotation.x += 0.01;
|
|
|
- this.#actors.cube.rotation.y += 0.01;
|
|
|
+
|
|
|
+ this.#actors.forEach(actor => {
|
|
|
+ actor.loop();
|
|
|
+ });
|
|
|
|
|
|
this.#renderer.render(this.#scene, this.#camera);
|
|
|
}
|