Advertisement
here2share

// a minimalist JavaScript standalone 3d game engine.

Jul 6th, 2022
980
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a minimalist JavaScript standalone 3d game engine.
  2.  
  3. // Game specific functions
  4.  
  5. function Player(x,y,z){
  6.     this.x = x;
  7.     this.y = y;
  8.     this.z = z;
  9.     this.up = true;
  10. }
  11.  
  12. Player.prototype.update = function(){
  13.     // update the position of the player
  14.     if (this.up){
  15.         this.y += this.velocityY;
  16.         if (this.y < 0) {
  17.             this.up = false;
  18.         }
  19.     }
  20.     else{
  21.         this.y -= this.velocityY;
  22.         if (this.y > 0) {
  23.             this.up = true;
  24.         }
  25.     }
  26.  
  27.     // update the player's position
  28.     this.x += this.velocityX;
  29.     this.z += this.velocityZ;
  30.  
  31.     // check if the player collided with a wall
  32.     if (this.x < 0) {
  33.         this.x = 0;
  34.         this.velocityX = -this.velocityX;
  35.     }
  36.     else if (this.x > window.innerWidth - 1) {
  37.         this.x = window.innerWidth - 1;
  38.         this.velocityX = -this.velocityX;
  39.     }
  40.  
  41.     if (this.z < 0) {
  42.         this.z = 0;
  43.         this.velocityZ = -this.velocityZ;
  44.     }
  45.     else if (this.z > window.innerHeight - 1) {
  46.         this.z = window.innerHeight - 1;
  47.         this.velocityZ = -this.velocityZ;
  48.     }
  49.  
  50.     // update the player's rotation
  51.     this.rotation += this.rotationSpeed;
  52. }
  53.  
  54. // draw the player
  55. Player.prototype.draw = function(){
  56.     // set the position of the player
  57.     gl.glTranslated(this.x, this.y, this.z);
  58.  
  59.     // set the rotation of the player
  60.     gl.glRotated(this.rotation, 0, 0, 1);
  61.  
  62.     // draw the player
  63.     gl.glBegin(gl.GL_QUADS);
  64.         gl.glColor3f(1.0, 0.0, 0.0);
  65.         gl.glVertex3f(0.0, 0.0, 0.0);
  66.         gl.glVertex3f(0.0, this.y, 0.0);
  67.         gl.glVertex3f(0.0, this.y, this.z);
  68.         gl.glVertex3f(0.0, 0.0, this.z);
  69.         gl.glVertex3f(this.x, 0.0, this.z);
  70.     gl.glEnd();
  71. }
  72.  
  73. function Wall(x,y,z){
  74.     this.x = x;
  75.     this.y = y;
  76.     this.z = z;
  77.     this.velocityX = -0.2;
  78.     this.velocityY = -0.2;
  79.     this.velocityZ = 0.2;
  80.     this.rotationSpeed = 1.0;
  81. }
  82.  
  83. Wall.prototype.update = function(){
  84.     // update the position of the wall
  85.     if (this.x < 0) {
  86.         this.x = 0;
  87.         this.velocityX = -this.velocityX;
  88.     }
  89.     else if (this.x > window.innerWidth - 1) {
  90.         this.x = window.innerWidth - 1;
  91.         this.velocityX = -this.velocityX;
  92.     }
  93.  
  94.     if (this.y < 0) {
  95.         this.y = 0;
  96.         this.velocityY = -this.velocityY;
  97.     }
  98.     else if (this.y > window.innerHeight - 1) {
  99.         this.y = window.innerHeight - 1;
  100.         this.velocityY = -this.velocityY;
  101.     }
  102.  
  103.     if (this.z < 0) {
  104.         this.z = 0;
  105.         this.velocityZ = -this.velocityZ;
  106.     }
  107.     else if (this.z > window.innerHeight - 1) {
  108.         this.z = window.innerHeight - 1;
  109.         this.velocityZ = -this.velocityZ;
  110.     }
  111.  
  112.     // update the wall's rotation
  113.     this.rotation += this.rotationSpeed;
  114. }
  115.  
  116. Wall.prototype.draw = function(){
  117.     // set the position of the wall
  118.     gl.glTranslated(this.x, this.y, this.z);
  119.  
  120.     // set the rotation of the wall
  121.     gl.glRotated(this.rotation, 0, 0, 1);
  122.  
  123.     // draw the wall
  124.     gl.glBegin(gl.GL_QUADS);
  125.         gl.glColor3f(0.0, 0.0, 0.0);
  126.         gl.glVertex3f(0.0, 0.0, 0.0);
  127.         gl.glVertex3f(0.0, this.y, 0.0);
  128.         gl.glVertex3f(0.0, this.y, this.z);
  129.         gl.glVertex3f(0.0, 0.0, this.z);
  130.     gl.glEnd();
  131. }
  132.  
  133. // the main game loop
  134. function main() {
  135.     // initialize all variables
  136.     window.requestAnimationFrame(main);
  137.  
  138.     // set the resolution of the window
  139.     gl.glViewport(0, 0, window.innerWidth, window.innerHeight);
  140.  
  141.     // set the projection matrix
  142.     gl.glMatrixMode(gl.GL_PROJECTION);
  143.     gl.glLoadIdentity();
  144.     gl.glOrtho(0.0, window.innerWidth, 0.0, window.innerHeight, 0.1, 100.0);
  145.  
  146.     // set the model view matrix
  147.     gl.glMatrixMode(gl.GL_MODELVIEW);
  148.     gl.glLoadIdentity();
  149.  
  150.     // set the player's position
  151.     Player.prototype.x = window.innerWidth/2;
  152.     Player.prototype.y = window.innerHeight/2;
  153.     Player.prototype.z = 0.0;
  154.  
  155.     // set the wall's position
  156.     Wall.prototype.x = window.innerWidth/2;
  157.     Wall.prototype.y = window.innerHeight/2;
  158.     Wall.prototype.z = window.innerHeight/2;
  159.  
  160.     // start the game loop
  161.     loop();
  162. }
  163.  
  164. // main game loop
  165. function loop() {
  166.     // set the game time
  167.     requestAnimationFrame(loop);
  168.  
  169.     // update the player's position
  170.     Player.prototype.update();
  171.  
  172.     // update the wall's position
  173.     Wall.prototype.update();
  174.  
  175.     // clear the screen and set the color to black
  176.     gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
  177.     gl.glColor3f(0.0, 0.0, 0.0);
Advertisement
RAW Paste Data Copied
Advertisement