Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // minimalistic 3d game engine in javascript
- // canvas element
- var canvas = document.createElement("canvas");
- var ctx = canvas.getContext("2d");
- canvas.width = 600;
- canvas.height = 600;
- document.body.appendChild(canvas);
- // camera
- var camera = new Camera();
- // scene
- var scene = new Scene();
- // cube
- function cube(x, y, z, size, color) {
- var cube = new GameObject();
- cube.model = models.cube;
- cube.position = new Vector3(x, y, z);
- cube.scale = new Vector3(size, size, size);
- cube.color = color;
- scene.add(cube);
- }
- // sphere
- function sphere(x, y, z, size, color) {
- var sphere = new GameObject();
- sphere.model = models.sphere;
- sphere.position = new Vector3(x, y, z);
- sphere.scale = new Vector3(size, size, size);
- sphere.color = color;
- scene.add(sphere);
- }
- // pyramid
- function pyramid(x, y, z, size, color) {
- var pyramid = new GameObject();
- pyramid.model = models.pyramid;
- pyramid.position = new Vector3(x, y, z);
- pyramid.scale = new Vector3(size, size, size);
- pyramid.color = color;
- scene.add(pyramid);
- }
- // cylinder
- function cylinder(x, y, z, size, color) {
- var cylinder = new GameObject();
- cylinder.model = models.cylinder;
- cylinder.position = new Vector3(x, y, z);
- cylinder.scale = new Vector3(size, size, size);
- cylinder.color = color;
- scene.add(cylinder);
- }
- // cone
- function cone(x, y, z, size, color) {
- var cone = new GameObject();
- cone.model = models.cone;
- cone.position = new Vector3(x, y, z);
- cone.scale = new Vector3(size, size, size);
- cone.color = color;
- scene.add(cone);
- }
- // torus
- function torus(x, y, z, size, color) {
- var torus = new GameObject();
- torus.model = models.torus;
- torus.position = new Vector3(x, y, z);
- torus.scale = new Vector3(size, size, size);
- torus.color = color;
- scene.add(torus);
- }
- // terrain
- function terrain(x, y, z, size, color) {
- var terrain = new GameObject();
- terrain.model = models.terrain;
- terrain.position = new Vector3(x, y, z);
- terrain.scale = new Vector3(size, size, size);
- terrain.color = color;
- scene.add(terrain);
- }
- // light
- function light(x, y, z, color) {
- var light = new Light();
- light.position = new Vector3(x, y, z);
- light.color = color;
- scene.add(light);
- }
- // directional light
- function directional_light(x, y, z, color) {
- var directional_light = new DirectionalLight();
- directional_light.position = new Vector3(x, y, z);
- directional_light.color = color;
- scene.add(directional_light);
- }
- // spot light
- function spot_light(x, y, z, color) {
- var spot_light = new SpotLight();
- spot_light.position = new Vector3(x, y, z);
- spot_light.color = color;
- scene.add(spot_light);
- }
- // point light
- function point_light(x, y, z, color) {
- var point_light = new PointLight();
- point_light.position = new Vector3(x, y, z);
- point_light.color = color;
- scene.add(point_light);
- }
- // ambient light
- function ambient_light(color) {
- var ambient_light = new AmbientLight();
- ambient_light.color = color;
- scene.add(ambient_light);
- }
- // fog
- function fog(color, density) {
- var fog = new Fog();
- fog.color = color;
- fog.density = density;
- scene.add(fog);
- }
- // particle
- function particle(x, y, z, size, color) {
- var particle = new Particle();
- particle.position = new Vector3(x, y, z);
- particle.scale = new Vector3(size, size, size);
- particle.color = color;
- scene.add(particle);
- }
- // image
- function image(x, y, width, height, image) {
- var image = new Image();
- image.position = new Vector3(x, y, 0);
- image.scale = new Vector3(width, height, 0);
- image.image = image;
- scene.add(image);
- }
- // audio
- function audio(x, y, z, audio) {
- var audio = new Audio();
- audio.position = new Vector3(x, y, z);
- audio.audio = audio;
- scene.add(audio);
- }
- // load 3d models
- var models = {};
- var model_names = ["cube", "sphere", "pyramid", "cylinder", "cone", "torus", "terrain", "particle"];
- var model_count = 0;
- for (var i = 0; i < model_names.length; i++) {
- var model = new Model();
- model.load(model_names[i], function() {
- model_count++;
- if (model_count == model_names.length) {
- init();
- }
- });
- models[model_names[i]] = model;
- }
- // button
- function button(x, y, width, height, color, text, onclick) {
- var button = new Button();
- button.position = new Vector3(x, y, 0);
- button.scale = new Vector3(width, height, 0);
- button.color = color;
- button.text = text;
- button.onclick = onclick;
- scene.add(button);
- }
- // text
- function text(x, y, size, color, text) {
- var text = new Text();
- text.position = new Vector3(x, y, 0);
- text.scale = new Vector3(size, size, 0);
- text.color = color;
- text.text = text;
- scene.add(text);
- }
- // input
- function input(x, y, width, height, color, text, onchange) {
- var input = new Input();
- input.position = new Vector3(x, y, 0);
- input.scale = new Vector3(width, height, 0);
- input.text = text;
- input.onchange = onchange;
- scene.add(input);
- }
- // slider
- function slider(x, y, width, height, color, min, max, value, onchange) {
- var slider = new Slider();
- slider.position = new Vector3(x, y, 0);
- slider.scale = new Vector3(width, height, 0);
- slider.color = color;
- slider.min = min;
- slider.max = max;
- slider.value = value;
- slider.onchange = onchange;
- scene.add(slider);
- }
- // checkbox
- function checkbox(x, y, width, height, color, checked, onchange) {
- var checkbox = new Checkbox();
- checkbox.position = new Vector3(x, y, 0);
- checkbox.scale = new Vector3(width, height, 0);
- checkbox.checked = checked;
- checkbox.onchange = onchange;
- scene.add(checkbox);
- }
- // radio
- function radio(x, y, width, height, color, checked, onchange) {
- var radio = new Radio();
- radio.position = new Vector3(x, y, 0);
- radio.scale = new Vector3(width, height, 0);
- radio.checked = checked;
- radio.onchange = onchange;
- scene.add(radio);
- }
- // dropdown
- function dropdown(x, y, width, height, color, options, onchange) {
- var dropdown = new Dropdown();
- dropdown.position = new Vector3(x, y, 0);
- dropdown.scale = new Vector3(width, height, 0);
- dropdown.options = options;
- dropdown.onchange = onchange;
- scene.add(dropdown);
- }
- // progressbar
- function progressbar(x, y, width, height, color, value) {
- var progressbar = new Progressbar();
- progressbar.position = new Vector3(x, y, 0);
- progressbar.scale = new Vector3(width, height, 0);
- progressbar.color = color;
- progressbar.value = value;
- scene.add(progressbar);
- }
- // canvas
- function canvas(x, y, width, height, color) {
- var canvas = new Canvas();
- canvas.position = new Vector3(x, y, 0);
- canvas.scale = new Vector3(width, height, 0);
- canvas.color = color;
- scene.add(canvas);
- }
- // window
- function window(x, y, width, height, color, text) {
- var window = new Window();
- window.position = new Vector3(x, y, 0);
- window.scale = new Vector3(width, height, 0);
- window.color = color;
- window.text = text;
- scene.add(window);
- }
- // panel
- function panel(x, y, width, height, color) {
- var panel = new Panel();
- panel.position = new Vector3(x, y, 0);
- panel.scale = new Vector3(width, height, 0);
- panel.color = color;
- scene.add(panel);
- }
- // scrollbar
- function scrollbar(x, y, width, height, color, vertical, value, onchange) {
- var scrollbar = new Scrollbar();
- scrollbar.position = new Vector3(x, y, 0);
- scrollbar.scale = new Vector3(width, height, 0);
- scrollbar.color = color;
- scrollbar.vertical = vertical;
- scrollbar.value = value;
- scrollbar.onchange = onchange;
- scene.add(scrollbar);
- }
- // tab
- function tab(x, y, width, height, color, tabs) {
- var tab = new Tab();
- tab.position = new Vector3(x, y, 0);
- tab.scale = new Vector3(width, height, 0);
- tab.color = color;
- tab.tabs = tabs;
- scene.add(tab);
- }
- // file load 3D models
- function load_model(name, callback) {
- var model = new Model();
- model.load(name, callback);
- models[name] = model;
- }
- // file load image
- function load_image(name, callback) {
- var image = new Image();
- image.load(name, callback);
- images[name] = image;
- }
- // file load audio
- function load_audio(name, callback) {
- var audio = new Audio();
- audio.load(name, callback);
- audios[name] = audio;
- }
- // file load font
- function load_font(name, callback) {
- var font = new Font();
- font.load(name, callback);
- fonts[name] = font;
- }
- // file load images
- var images = {};
- var image_names = [];
- var image_count = 0;
- for (var i = 0; i < image_names.length; i++) {
- var image = new Image();
- image.load(image_names[i], function() {
- image_count++;
- if (image_count == image_names.length) {
- init();
- }
- });
- images[image_names[i]] = image;
- }
- // bind arrow keys to move player (and camera)
- function bind_arrow_keys() {
- document.addEventListener("keydown", function(e) {
- if (e.keyCode == 37) {
- player.position.x -= 1;
- }
- if (e.keyCode == 38) {
- player.position.y -= 1;
- }
- if (e.keyCode == 39) {
- player.position.x += 1;
- }
- if (e.keyCode == 40) {
- player.position.y += 1;
- }
- });
- }
- // bind mouse click to shoot
- function bind_mouse_click() {
- document.addEventListener("click", function(e) {
- var bullet = new GameObject();
- bullet.model = models.cube;
- bullet.position = new Vector3(player.position.x, player.position.y, player.position.z);
- bullet.velocity = new Vector3(e.clientX - canvas.width / 2, e.clientY - canvas.height / 2, 0);
- scene.add(bullet);
- });
- }
- // bind mouse move to look around
- function bind_mouse_move() {
- document.addEventListener("mousemove", function(e) {
- camera.rotation.x = e.clientY / canvas.height * Math.PI;
- camera.rotation.y = e.clientX / canvas.width * Math.PI;
- });
- }
- // bind mouse wheel to zoom
- function bind_mouse_wheel() {
- document.addEventListener("wheel", function(e) {
- camera.position.z += e.deltaY / 100;
- });
- }
- // bind spacebar to jump (or swim)
- function bind_spacebar() {
- document.addEventListener("keydown", function(e) {
- if (e.keyCode == 32) {
- player.velocity.y = 1;
- }
- });
- }
- // bind escape to pause
- function bind_escape() {
- document.addEventListener("keydown", function(e) {
- if (e.keyCode == 27) {
- paused = !paused;
- }
- });
- }
- // bind enter for action (or start)
- function bind_enter() {
- document.addEventListener("keydown", function(e) {
- if (e.keyCode == 13) {
- started = true;
- }
- });
- }
- // bind right mouse click for secondary action
- function bind_right_mouse_click() {
- document.addEventListener("contextmenu", function(e) {
- e.preventDefault();
- });
- document.addEventListener("mousedown", function(e) {
- if (e.button == 2) {
- secondary_action = true;
- }
- });
- document.addEventListener("mouseup", function(e) {
- if (e.button == 2) {
- secondary_action = false;
- }
- });
- }
- // display current score
- function display_score() {
- var score = new Text();
- score.position = new Vector3(0, 0, 0);
- score.scale = new Vector3(0.1, 0.1, 0.1);
- score.color = new Color(1, 1, 1);
- score.text = "Score: " + player.score;
- scene.add(score);
- }
- // display health
- function display_health() {
- var health = new Text();
- health.position = new Vector3(0, 0.1, 0);
- health.scale = new Vector3(0.1, 0.1, 0.1);
- health.color = new Color(1, 1, 1);
- health.text = "Health: " + player.health;
- scene.add(health);
- }
- // display ammo
- function display_ammo() {
- var ammo = new Text();
- ammo.position = new Vector3(0, 0.2, 0);
- ammo.scale = new Vector3(0.1, 0.1, 0.1);
- ammo.text = "Ammo: " + player.ammo;
- scene.add(ammo);
- }
- // display time
- function display_time() {
- var time = new Text();
- time.position = new Vector3(0, 0.3, 0);
- time.scale = new Vector3(0.1, 0.1, 0.1);
- time.color = new Color(1, 1, 1);
- time.text = "Time: " + player.time;
- scene.add(time);
- }
- // display money
- function display_money() {
- var money = new Text();
- money.position = new Vector3(0, 0.5, 0);
- money.text = "Money: " + player.money;
- scene.add(money);
- }
- // display fps
- function display_fps() {
- var fps = new Text();
- fps.position = new Vector3(0, 0.4, 0);
- fps.scale = new Vector3(0.1, 0.1, 0.1);
- fps.color = new Color(1, 1, 1);
- fps.text = "FPS: " + player.fps;
- scene.add(fps);
- }
- // display (mini) map
- function display_map() {
- var map = new Image();
- map.position = new Vector3(0, 0, 0);
- map.scale = new Vector3(1, 1, 0);
- map.image = images.map;
- scene.add(map);
- }
- // display inventory
- function display_inventory() {
- var inventory = new Image();
- inventory.position = new Vector3(0, 0, 0);
- inventory.scale = new Vector3(1, 1, 0);
- inventory.image = images.inventory;
- scene.add(inventory);
- }
- // display pause menu
- function display_pause_menu() {
- var pause_menu = new Image();
- pause_menu.position = new Vector3(0, 0, 0);
- pause_menu.scale = new Vector3(1, 1, 0);
- pause_menu.image = images.pause_menu;
- scene.add(pause_menu);
- }
- // display settings menu
- function display_settings_menu() {
- var settings_menu = new Image();
- settings_menu.position = new Vector3(0, 0, 0);
- settings_menu.scale = new Vector3(1, 1, 0);
- settings_menu.image = images.settings_menu;
- scene.add(settings_menu);
- }
- // throw weapon
- function throw_weapon() {
- var weapon = new GameObject();
- weapon.model = models.cube;
- weapon.position = new Vector3(player.position.x, player.position.y, player.position.z);
- weapon.velocity = velocity.set
- scene.add(weapon);
- }
- // shoot weapon
- function shoot_weapon() {
- var bullet = new GameObject();
- bullet.model = models.cube;
- bullet.position = new Vector3(player.position.x, player.position.y, player.position.z);
- weapon.velocity = velocity.set
- scene.add(bullet);
- }
- // explosion
- function explosion(x, y, z) {
- var explosion = new Particle();
- explosion.position = new Vector3(x, y, z);
- explosion.scale = new Vector3(0.1, 0.1, 0.1);
- explosion.velocity = new Vector3(Math.random() * 2 - 1, Math.random() * 2 - 1, 0);
- scene.add(explosion);
- }
- // spawn enemy
- function spawn_enemy() {
- var enemy = new GameObject();
- enemy.model = models.cube;
- enemy.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
- scene.add(enemy);
- }
- // spawn item
- function spawn_item() {
- var item = new GameObject();
- item.model = models.cube;
- item.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
- scene.add(item);
- }
- // spawn powerup
- function spawn_powerup() {
- var powerup = new GameObject();
- powerup.model = models.cube;
- powerup.position = new Vector3(Math.random() * 10 - 5, Math.random() * 10 - 5, 0);
- scene.add(powerup);
- }
- // display level
- function display_level() {
- var level = new Text();
- level.position = new Vector3(0, 0.6, 0);
- level.text = "Level: " + player.level;
- scene.add(level);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement