Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a minimalist JavaScript standalone 3d game engine.
- // Game specific functions
- function Player(x,y,z){
- this.x = x;
- this.y = y;
- this.z = z;
- this.up = true;
- }
- Player.prototype.update = function(){
- // update the position of the player
- if (this.up){
- this.y += this.velocityY;
- if (this.y < 0) {
- this.up = false;
- }
- }
- else{
- this.y -= this.velocityY;
- if (this.y > 0) {
- this.up = true;
- }
- }
- // update the player's position
- this.x += this.velocityX;
- this.z += this.velocityZ;
- // check if the player collided with a wall
- if (this.x < 0) {
- this.x = 0;
- this.velocityX = -this.velocityX;
- }
- else if (this.x > window.innerWidth - 1) {
- this.x = window.innerWidth - 1;
- this.velocityX = -this.velocityX;
- }
- if (this.z < 0) {
- this.z = 0;
- this.velocityZ = -this.velocityZ;
- }
- else if (this.z > window.innerHeight - 1) {
- this.z = window.innerHeight - 1;
- this.velocityZ = -this.velocityZ;
- }
- // update the player's rotation
- this.rotation += this.rotationSpeed;
- }
- // draw the player
- Player.prototype.draw = function(){
- // set the position of the player
- gl.glTranslated(this.x, this.y, this.z);
- // set the rotation of the player
- gl.glRotated(this.rotation, 0, 0, 1);
- // draw the player
- gl.glBegin(gl.GL_QUADS);
- gl.glColor3f(1.0, 0.0, 0.0);
- gl.glVertex3f(0.0, 0.0, 0.0);
- gl.glVertex3f(0.0, this.y, 0.0);
- gl.glVertex3f(0.0, this.y, this.z);
- gl.glVertex3f(0.0, 0.0, this.z);
- gl.glVertex3f(this.x, 0.0, this.z);
- gl.glEnd();
- }
- function Wall(x,y,z){
- this.x = x;
- this.y = y;
- this.z = z;
- this.velocityX = -0.2;
- this.velocityY = -0.2;
- this.velocityZ = 0.2;
- this.rotationSpeed = 1.0;
- }
- Wall.prototype.update = function(){
- // update the position of the wall
- if (this.x < 0) {
- this.x = 0;
- this.velocityX = -this.velocityX;
- }
- else if (this.x > window.innerWidth - 1) {
- this.x = window.innerWidth - 1;
- this.velocityX = -this.velocityX;
- }
- if (this.y < 0) {
- this.y = 0;
- this.velocityY = -this.velocityY;
- }
- else if (this.y > window.innerHeight - 1) {
- this.y = window.innerHeight - 1;
- this.velocityY = -this.velocityY;
- }
- if (this.z < 0) {
- this.z = 0;
- this.velocityZ = -this.velocityZ;
- }
- else if (this.z > window.innerHeight - 1) {
- this.z = window.innerHeight - 1;
- this.velocityZ = -this.velocityZ;
- }
- // update the wall's rotation
- this.rotation += this.rotationSpeed;
- }
- Wall.prototype.draw = function(){
- // set the position of the wall
- gl.glTranslated(this.x, this.y, this.z);
- // set the rotation of the wall
- gl.glRotated(this.rotation, 0, 0, 1);
- // draw the wall
- gl.glBegin(gl.GL_QUADS);
- gl.glColor3f(0.0, 0.0, 0.0);
- gl.glVertex3f(0.0, 0.0, 0.0);
- gl.glVertex3f(0.0, this.y, 0.0);
- gl.glVertex3f(0.0, this.y, this.z);
- gl.glVertex3f(0.0, 0.0, this.z);
- gl.glEnd();
- }
- // the main game loop
- function main() {
- // initialize all variables
- window.requestAnimationFrame(main);
- // set the resolution of the window
- gl.glViewport(0, 0, window.innerWidth, window.innerHeight);
- // set the projection matrix
- gl.glMatrixMode(gl.GL_PROJECTION);
- gl.glLoadIdentity();
- gl.glOrtho(0.0, window.innerWidth, 0.0, window.innerHeight, 0.1, 100.0);
- // set the model view matrix
- gl.glMatrixMode(gl.GL_MODELVIEW);
- gl.glLoadIdentity();
- // set the player's position
- Player.prototype.x = window.innerWidth/2;
- Player.prototype.y = window.innerHeight/2;
- Player.prototype.z = 0.0;
- // set the wall's position
- Wall.prototype.x = window.innerWidth/2;
- Wall.prototype.y = window.innerHeight/2;
- Wall.prototype.z = window.innerHeight/2;
- // start the game loop
- loop();
- }
- // main game loop
- function loop() {
- // set the game time
- requestAnimationFrame(loop);
- // update the player's position
- Player.prototype.update();
- // update the wall's position
- Wall.prototype.update();
- // clear the screen and set the color to black
- gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
- gl.glColor3f(0.0, 0.0, 0.0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement