This repository has been archived on 2022-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
GeometryDash3D/js/Env.js
2022-11-24 19:45:20 +01:00

112 lines
2.6 KiB
JavaScript

import { Element } from "./Element.js";
import { Quality } from "./utils.js";
export class Env {
constructor() {
this.scene = new THREE.Scene();
this.camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
1,
1000
);
this.camera.position.z = 7;
this.renderer = new THREE.WebGLRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);
// Change the default quality of the rendered scene
this.setQuality(Quality.medium);
// Store all elements in the env
this.elements = [];
// Setup renderer for lights
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Add light source
const light = new THREE.DirectionalLight(THREE.Color.NAMES.white);
// On top : 1, right : 2 and between player (0) and camera (7) : 4
light.position.set(2, 1, 4);
light.castShadow = true;
this.scene.add(light);
// Clock
this.clock = new THREE.Clock();
}
/**
* Get the Canvas element
* @returns domElement
*/
getDomElement = () => this.renderer.domElement;
/**
* Get current scene
* @returns Scene
*/
getScene = () => this.scene;
/**
* Get used camera
* @returns Camera
*/
getCamera = () => this.camera;
/**
* Get current renderer
* @returns Render
*/
getRenderer = () => this.renderer;
/**
* Change the quality of the render
* @param {Quality} quality
* @returns
*/
setQuality = (quality) => {
this.quality = quality;
this.renderer.setSize(
window.innerWidth / quality,
window.innerHeight / quality,
false
);
};
/**
* Add an element to the scene
* @param {Element} element Element
*/
addToScene = (element) => {
this.elements.push(element);
this.scene.add(element.data);
};
/**
* Animate all the elements in the environnement
*/
animate = () => {
this.elements.forEach((element) => {
if (element.animation) {
element.animation();
}
});
};
/**
* Render the current scene, using the camera
* @returns
*/
render = () => this.renderer.render(this.scene, this.camera);
/**
* Update the game logic
*/
update = () => {
this.animate();
this.render();
};
}