Facebook
From Ronalds Mazītis, 3 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 114
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.   <head>
  4.     <meta charset="UTF-8" />
  5.     <meta
  6.       name="viewport"
  7.       content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
  8.     />
  9.     <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  10.     <title>3D ONLINE GAME 2</title>
  11.     <link rel="stylesheet" href="/css/examples.css?ver=1.0.0" />
  12.     <script src="/js/examples.js?ver=1.1.1"></script>
  13.     <script src="/lib/phaser.min.js?ver=3.52.0"></script>
  14.     <script src="/lib/enable3d/enable3d.phaserExtension.0.25.0.min.js"></script>
  15.               <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  16.  
  17.    
  18.  
  19.   </head>
  20.  
  21.   <body>
  22.    <!--- <div id="info-text">Use WASD, SPACE and your Mouse.<br />Try to play it on your mobile device :)</div> -->
  23.    
  24.  
  25.   <center>
  26.    <h1>
  27.           <?php  
  28.    include_once 'db.php';
  29.  
  30. $sessionid = $_SESSION['user_id'];
  31.  
  32. echo $user_data['username'];
  33.  
  34. if(empty($sessionid)){
  35.             header('Location: index.php');
  36.  
  37. //echo $sessionid;
  38.  
  39. }
  40.  
  41.  
  42. //skripts kurš pie katras čaraktera kustību apstājas updeito tabulas sadaļu *location*
  43. //skripts kurš katru lietotāju kura datetime sakrīt ar intervālu pēdējās 15 min, zīmē ekrānā
  44.  
  45.  
  46.  
  47.  
  48.  
  49.    ?>
  50.  
  51.    </h1>  <a href="logout.php">Logout.</a>
  52.    </center>
  53.    
  54.     <script>
  55.       const {
  56.         enable3d,
  57.         Scene3D,
  58.         Canvas,
  59.         ThirdDimension,
  60.         THREE,
  61.         JoyStick,
  62.         ExtendedObject3D,
  63.         ThirdPersonControls,
  64.         PointerLock,
  65.         PointerDrag
  66.       } = ENABLE3D
  67.  
  68.       /**
  69.        * Is touch device?
  70.        */
  71.       const isTouchDevice = 'ontouchstart' in window
  72.  
  73.       class MainScene extends Scene3D {
  74.         constructor() {
  75.           super({ key: 'MainScene' })
  76.         }
  77.  
  78.         init() {
  79.           this.accessThirdDimension({ maxSubSteps: 10, fixedTimeStep: 1 / 120 })
  80.           this.third.renderer.outputEncoding = THREE.LinearEncoding
  81.           this.canJump = true
  82.           this.move = false
  83.  
  84.           this.moveTop = 0
  85.           this.moveRight = 0
  86.         }
  87.  
  88.         async create() {
  89.           const { lights } = await this.third.warpSpeed('-ground', '-orbitControls')
  90.  
  91.           const { hemisphereLight, ambientLight, directionalLight } = lights
  92.           const intensity = 0.65
  93.           hemisphereLight.intensity = intensity
  94.           ambientLight.intensity = intensity
  95.           directionalLight.intensity = intensity
  96.  
  97.           //this.third.physics.add.box({ y: 10, x: 35 }, { lambert: { color: 'red' } })
  98.  
  99.           // this.third.physics.debug.enable()
  100.  
  101.           /**
  102.            * Medieval Fantasy Book by Pixel (https://sketchfab.com/stefan.lengyel1)
  103.            * https://sketchfab.com/3d-models/medieval-fantasy-book-06d5a80a04fc4c5ab552759e9a97d91a
  104.            * Attribution 4.0 International (CC BY 4.0)
  105.            
  106.            E4E717
  107.            */
  108.           this.third.load.gltf('/assets/glb/book6.glb').then(object => {
  109.             const scene = object.scenes[0]
  110.  
  111.             const book = new ExtendedObject3D()
  112.             book.name = 'scene'
  113.             book.add(scene)
  114.             this.third.add.existing(book)
  115.  
  116.             // add animations
  117.             // sadly only the flags animations works
  118.             object.animations.forEach((anim, i) => {
  119.               book.mixer = this.third.animationMixers.create(book)
  120.               // overwrite the action to be an array of actions
  121.               book.action = []
  122.               book.action[i] = book.mixer.clipAction(anim)
  123.               book.action[i].play()
  124.             })
  125.  
  126.             book.traverse(child => {
  127.               if (child.isMesh) {
  128.                 child.castShadow = child.receiveShadow = false
  129.                 child.material.metalness = 0
  130.                 child.material.roughness = 1
  131.  
  132.                 if (/mesh/i.test(child.name)) {
  133.                   this.third.physics.add.existing(child, {
  134.                     shape: 'concave',
  135.                     mass: 0,
  136.                     collisionFlags: 1,
  137.                     autoCenter: false
  138.                   })
  139.                   child.body.setAngularFactor(0, 0, 0)
  140.                   child.body.setLinearFactor(0, 0, 0)
  141.                 }
  142.               }
  143.             })
  144.           })
  145.          
  146.          
  147.  
  148.          
  149.          
  150.          
  151.  
  152.  
  153.           var skins = [];
  154.           $.ajax({
  155.               type: "GET",
  156.               async: false,
  157.               url: 'loadskins.php',
  158.               data: {},
  159.               success: function (data) {
  160.                   skins = data.split(",").map(String);
  161.               }
  162.           });
  163.  
  164.                        
  165.                          
  166.                          
  167.   //console.log(vertibasx);
  168.  
  169.           for (var i = 0; i < skins.length - 1; i++) {
  170.               let skin = skins[i];  
  171.              
  172.               const lokacijumasivs = [];
  173.                          
  174.                         //// let objstr = obj[i].toString().split("");
  175.                          //let objstr = obj[i];
  176.                          
  177.  
  178.  
  179.  
  180.                          ////console.log(objstr);
  181.                           //console.log("x", vertibasx[i]);
  182.                           //console.log("y", vertibasy[i]);
  183.                           //console.log("z", vertibasz[i]);
  184.  
  185.  
  186.  
  187.  
  188. var obj = [];
  189.                       $.ajax({
  190.                           type: "GET",
  191.                           async: false,
  192.                           url: 'getlocation.php',
  193.                           data: {},
  194.                           success: function (data) {
  195.                                                 obj = data.split("*").map(String);
  196.  
  197.                           }
  198.  
  199.                       });
  200.            
  201.  
  202.  
  203.                          console.log("obj", obj);
  204.                          
  205.  const vertibasx = obj.slice(0,1);
  206.                          const vertibasy = obj.slice(1,2);
  207.                          const vertibasz = obj.slice(2,3);
  208.                          
  209.  
  210.                           console.log(vertibasx);
  211.                           console.log(vertibasy);
  212.                           console.log(vertibasz);
  213.  
  214.           /**
  215.            * box_man.glb by Jan Bláha
  216.            * https://github.com/swift502/Sketchbook
  217.            * CC-0 license 2018
  218.            */
  219.            var object = await this.third.load.gltf(skin)
  220.           ///this.third.load.gltf('/assets/glb/box_man.glb').then(object => {
  221.             const man = object.scene.children[0]
  222.  
  223.             this.man = new ExtendedObject3D()
  224.             this.man.name = 'man'
  225.             this.man.rotateY(Math.PI + 0.1) // a hack
  226.             this.man.add(man)
  227.             this.man.rotation.set(0, Math.PI * 1.5, 0)
  228.             //console.log("uzstada", vertibasx, vertibasy, vertibasx);
  229.            
  230.             console.log("iteracija", i);
  231.                         //if(vertibasx !== null && vertibasy !== null && vertibasz !== null){
  232.                         //let verx = vertibasx[i];
  233.                         //let very = vertibasy[i];
  234.                         //let verz = vertibasz[i];
  235.                        
  236.                        
  237. //console.log("shitais?", verx);
  238. //console.log("2shitais?", very);
  239. //console.log("3shitais?", verz);
  240.  
  241.  
  242.                                         if(vertibasx && vertibasy && vertibasz){
  243.                                     this.man.position.set(vertibasx, vertibasy, vertibasz)
  244.                                         } else {
  245.                                                                                     this.man.position.set(754.2014, 1000, -205.63472)
  246.                                         }
  247.                
  248.                 //} else {
  249.                 //let verx = 754.2014;
  250.                 //let very = 1000;
  251.                 //let verz = -205.63472;
  252.                
  253.                             //this.man.position.set(verx, very, verz)
  254.  
  255.                 //}
  256.             //this.man.position.set(59, 0, 79)
  257.  
  258.             // add shadow
  259.             this.man.traverse(child => {
  260.               if (child.isMesh) {
  261.                 child.castShadow = child.receiveShadow = true
  262.                 // https://discourse.threejs.org/t/cant-export-material-from-blender-gltf/12258
  263.                 child.material.roughness = 1
  264.                 child.material.metalness = 0
  265.               }
  266.             })
  267.  
  268.             /**
  269.              * Animations
  270.              */
  271.             this.third.animationMixers.add(this.man.animation.mixer)
  272.             object.animations.forEach(animation => {
  273.               if (animation.name) {
  274.                 this.man.animation.add(animation.name, animation)
  275.               }
  276.             })
  277.             this.man.animation.play('idle')
  278.  
  279.             /**
  280.              * Add the player to the scene with a body
  281.              */
  282.             this.third.add.existing(this.man)
  283.             this.third.physics.add.existing(this.man, {
  284.               shape: 'sphere',
  285.               radius: 0.25,
  286.               width: 0.5,
  287.               offset: { y: -0.25 }
  288.             })
  289.             this.man.body.setFriction(0.8)
  290.             this.man.body.setAngularFactor(0, 0, 0)
  291.  
  292.             // https://docs.panda3d.org/1.10/python/programming/physics/bullet/ccd
  293.             this.man.body.setCcdMotionThreshold(1e-7)
  294.             this.man.body.setCcdSweptSphereRadius(0.25)
  295.  
  296.             /**
  297.              * Add 3rd Person Controls
  298.              */
  299.             this.controls = new ThirdPersonControls(this.third.camera, this.man, {
  300.               offset: new THREE.Vector3(0, 1, 0),
  301.               targetRadius: 3
  302.             })
  303.             // set initial view to 90 deg theta
  304.             this.controls.theta = 90
  305.  
  306.             /**
  307.              * Add Pointer Lock and Pointer Drag
  308.              */
  309.             if (!isTouchDevice) {
  310.               let pl = new PointerLock(this.game.canvas)
  311.               let pd = new PointerDrag(this.game.canvas)
  312.               pd.onMove(delta => {
  313.                 if (pl.isLocked()) {
  314.                   this.moveTop = -delta.y
  315.                   this.moveRight = delta.x
  316.                 }
  317.               })
  318.             }
  319.          // })
  320.  
  321.           /**
  322.            * Add Keys
  323.            */
  324.           this.keys = {
  325.             a: this.input.keyboard.addKey('a'),
  326.             w: this.input.keyboard.addKey('w'),
  327.             d: this.input.keyboard.addKey('d'),
  328.             s: this.input.keyboard.addKey('s'),
  329.             space: this.input.keyboard.addKey(32)
  330.           }
  331.  
  332.           /**
  333.            * Add joystick
  334.            */
  335.           if (isTouchDevice) {
  336.             const joystick = new JoyStick()
  337.             const axis = joystick.add.axis({
  338.               styles: { left: 35, bottom: 35, size: 100 }
  339.             })
  340.             axis.onMove(event => {
  341.               /**
  342.                * Update Camera
  343.                */
  344.               const { top, right } = event
  345.               this.moveTop = top * 3
  346.               this.moveRight = right * 3
  347.             })
  348.             const buttonA = joystick.add.button({
  349.               letter: 'A',
  350.               styles: { right: 35, bottom: 110, size: 80 }
  351.             })
  352.             buttonA.onClick(() => this.jump())
  353.             const buttonB = joystick.add.button({
  354.               letter: 'B',
  355.               styles: { right: 110, bottom: 35, size: 80 }
  356.             })
  357.             buttonB.onClick(() => (this.move = true))
  358.             buttonB.onRelease(() => (this.move = false))
  359.           }
  360.         }
  361.  
  362. }
  363.         jump() {
  364.           if (!this.man || !this.canJump) return
  365.           this.canJump = false
  366.           this.man.animation.play('jump_running', 500, false)
  367.           this.time.addEvent({
  368.             delay: 650,
  369.             callback: () => {
  370.               this.canJump = true
  371.               this.man.animation.play('idle')
  372.             }
  373.           })
  374.           this.man.body.applyForceY(6)
  375.         }
  376.  
  377.         update(time, delta) {
  378.           if (this.man && this.man.body) {
  379.             /**
  380.              * Update Controls
  381.              */
  382.             this.controls.update(this.moveRight * 2, -this.moveTop * 2)
  383.             if (!isTouchDevice) this.moveRight = this.moveTop = 0
  384.             /**
  385.              * Player Turn
  386.              */
  387.  
  388.                      
  389.                      
  390.            
  391.        
  392.                 //console.log("globals mainigais speed", speed);
  393.                
  394.                
  395.             const v3 = new THREE.Vector3()
  396.  
  397.             const rotation = this.third.camera.getWorldDirection(v3)
  398.            
  399.  
  400.             var rotati = [];
  401.                       $.ajax({
  402.                           type: "GET",
  403.                           async: false,
  404.                           url: 'getrotation.php',
  405.                           data: {},
  406.                           success: function (data) {
  407.                                                 rotati = data.split("*").map(String);
  408.  
  409.                           }
  410.  
  411.                       });
  412.                  
  413.  
  414.                  
  415.                  
  416.             const theta = Math.atan2(rotation.x, rotation.z)
  417.             const rotationMan = this.man.getWorldDirection(v3)
  418.             for (var i = 0; i < rotati.length - 1; i++) {
  419.               let rotatine = rotati[i];  
  420.                                                                 console.log(rotatine);
  421.  
  422.                                 if(rotatine){
  423.             const thetaMan = Math.atan2(rotationMan.x, rotationMan.z)
  424.               $.post(
  425.                           "postrotation.php", {
  426.                               rotation: thetaMan
  427.                           },
  428.                           function (data) {
  429.                                                                 //console.log("rotacija", thetaMan);
  430.                               //console.log("izsaukta", v3);
  431.                           }
  432.                       );
  433.                      
  434.                       this.man.body.setAngularVelocityY(0)
  435.  
  436.             const l = Math.abs(theta - thetaMan)
  437.                
  438.             let rotationSpeed = isTouchDevice ? 2 : 4
  439.             let d = Math.PI / 24
  440.  
  441.             if (l > d) {
  442.               if (l > Math.PI - d) rotationSpeed *= -1
  443.               if (theta < thetaMan) rotationSpeed *= -1
  444.               this.man.body.setAngularVelocityY(rotationSpeed)
  445.             }
  446.            
  447.            
  448.                          } else {
  449.                          const thetaMan = rotatine;
  450.                          
  451.                            $.post(
  452.                           "postrotation.php", {
  453.                               rotation: thetaMan
  454.                           },
  455.                           function (data) {
  456.                                                                 //console.log("rotacija", thetaMan);
  457.                               //console.log("izsaukta", v3);
  458.                           }
  459.                       );
  460.                          
  461.                
  462.            
  463.                                  
  464.                      
  465.                      
  466.             this.man.body.setAngularVelocityY(0)
  467.  
  468.             const l = Math.abs(theta - thetaMan)
  469.                
  470.             let rotationSpeed = isTouchDevice ? 2 : 4
  471.             let d = Math.PI / 24
  472.  
  473.             if (l > d) {
  474.               if (l > Math.PI - d) rotationSpeed *= -1
  475.               if (theta < thetaMan) rotationSpeed *= -1
  476.               this.man.body.setAngularVelocityY(rotationSpeed)
  477.             }
  478.            
  479.                 }
  480.                          }
  481.  
  482.  
  483.  
  484.             /**
  485.              * Player Move
  486.              */
  487.             if (this.keys.w.isDown || this.move) {
  488.               if (this.man.animation.current === 'idle' && this.canJump) this.man.animation.play('run')
  489.                                           $.post(
  490.                           "postanimation.php", {
  491.                               animation: this.man.animation.current
  492.                           },
  493.                           function (data) {
  494.                                                                 //console.log("viens", x);
  495.                               //console.log("izsaukta", v3);
  496.                           }
  497.                       );
  498.                      
  499.                      
  500.  const speed = [];
  501.               var getspeed = [];
  502.                       $.ajax({
  503.                           type: "GET",
  504.                           async: false,
  505.                           url: 'getspeed.php',
  506.                           data: {},
  507.                           success: function (data) {
  508.                                                 getspeed = data.split("*").map(String);
  509.                                                                         //console.log("getsp", getspeed);
  510.                           }
  511.  
  512.                       });
  513.                                                  
  514.               for (var i = 0; i < getspeed.length - 1; i++) {
  515.                                 let getsp = getspeed[i];  
  516.                                 //console.log("speed", getsp);
  517.                       speed.push(getsp);
  518.                 }
  519.                
  520.                                                                                                 //console.log("speed.length", speed);
  521.                
  522.                
  523.             const v3 = new THREE.Vector3()
  524.  
  525.            
  526.  
  527.             var rotati = [];
  528.                       $.ajax({
  529.                           type: "GET",
  530.                           async: false,
  531.                           url: 'getrotation.php',
  532.                           data: {},
  533.                           success: function (data) {
  534.                                                 rotati = data.split("*").map(String);
  535.  
  536.                           }
  537.  
  538.                       });
  539.                         console.log("rotāciju masīvs", rotati);
  540.                        
  541.                        
  542.                        
  543.                        
  544.                        
  545.                 if(rotati){
  546.                
  547.                 for (var i = 0; i < rotati.length; i++) {
  548.                
  549.                                 const rotation = rotati[i];
  550.                                
  551.                                 //
  552.  
  553.                 }      
  554.                
  555.                 } else {
  556.                              const rotation = this.third.camera.getWorldDirection(v3)
  557.  
  558.                 }
  559.                
  560.                
  561.                 console.log("rotation", rotation);
  562.                
  563.                
  564.                
  565.                
  566.                          
  567.             const theta = Math.atan2(rotation.x, rotation.z)
  568.             const rotationMan = this.man.getWorldDirection(v3)
  569.  
  570.  
  571.  
  572. //console.log("globals mainigais speed", speed);
  573. //console.log("theta", theta);
  574.  
  575.                 for (var i = 0; i < speed.length; i++) {
  576.                
  577.                
  578.                
  579.                                         console.log("xT", Math.sin(theta));
  580.  
  581.                         let speedi = parseInt(speed[i]);
  582.                 //console.log("spee",typeof(parseInt(speedi)));
  583.                
  584.                         console.log("speed array2", speedi);
  585.                                         console.log("xT2", Math.sin(theta));
  586.  
  587.               const x = Math.sin(theta) * 4,
  588.                 y = this.man.body.velocity.y,
  589.                 z = Math.cos(theta) * 4
  590.                
  591.                 console.log("y mainas randomā", y);
  592.  
  593.                 var obj2 = [];
  594.                       $.ajax({
  595.                           type: "GET",
  596.                           async: false,
  597.                           url: 'getlocation.php',
  598.                           data: {},
  599.                           success: function (data) {
  600.                                                 obj2 = data.split("*").map(String);
  601.  
  602.                           }
  603.  
  604.                       });
  605.            
  606.  
  607.  
  608.                          console.log("obj2", obj2);
  609.                          
  610.                          let vertibasx = obj2.slice(0,1);
  611.                          let vertibasy = obj2.slice(1,2);
  612.                          let vertibasz = obj2.slice(2,3);
  613.                
  614.                
  615.          
  616.                               let xv = parseInt(vertibasx[i]) + x;
  617.  
  618.                          
  619.                              
  620.  
  621.                          
  622.                               let zv = parseInt(vertibasz[i]) + z;
  623.  
  624.                          
  625.  
  626.                
  627.                 //nepareizā lokācija
  628.                
  629.                 if(xv){
  630.            $.post(
  631.                           "postlocation.php", {
  632.                               x: xv,
  633.                               y: 0,
  634.                              z: zv,
  635.                              
  636.                           },
  637.                           function (data) {
  638.                                                                 //console.log("viens", x);
  639.                               //console.log("izsaukta", v3);
  640.                           }
  641.                       );
  642.                                  
  643.                                  
  644.               this.man.body.setVelocity(x, y, z)
  645.                   //}
  646.           }
  647.   }
  648.                  
  649.                  
  650.                  
  651.             } else {
  652.                                
  653.                                             const v3 = new THREE.Vector3()
  654.  
  655.                         const rotation = this.third.camera.getWorldDirection(v3)
  656.             const theta = Math.atan2(rotation.x, rotation.z)
  657.             const rotationMan = this.man.getWorldDirection(v3)
  658.  
  659.  
  660.  
  661.                                  const x = Math.sin(theta) * 4,
  662.                 y = this.man.body.velocity.y,
  663.                 z = Math.cos(theta) * 4
  664.                
  665.                                
  666.                                
  667.                                 var obj2 = [];
  668.                       $.ajax({
  669.                           type: "GET",
  670.                           async: false,
  671.                           url: 'getlocation.php',
  672.                           data: {},
  673.                           success: function (data) {
  674.                                                 obj2 = data.split("*").map(String);
  675.  
  676.                           }
  677.  
  678.                       });
  679.            
  680.  
  681.  
  682.                          console.log("obj2", obj2);
  683.                          
  684.                          let vertibasx = obj2.slice(0,1);
  685.                          let vertibasy = obj2.slice(1,2);
  686.                          let vertibasz = obj2.slice(2,3);
  687.                
  688.                
  689.          
  690.                               let xv = parseInt(vertibasx[i]) + x;
  691.  
  692.                          
  693.                              
  694.  
  695.                          
  696.                               let zv = parseInt(vertibasz[i]) + z;
  697.  
  698.                          
  699.  
  700.                
  701.                 //nepareizā lokācija
  702.                
  703.                 if(xv){
  704.            $.post(
  705.                           "postlocation.php", {
  706.                               x: xv,
  707.                               y: 0,
  708.                              z: zv,
  709.                              
  710.                           },
  711.                           function (data) {
  712.                                                                 //console.log("viens", x);
  713.                               //console.log("izsaukta", v3);
  714.                           }
  715.                       );
  716.                                   }
  717.                                
  718.                                
  719.                                
  720.                                
  721.                                
  722.                                
  723.                                
  724.                                 //šeit jābūt kodam kas updeitos koordinātes ja nespiež nevienu pogu bet papriekšu custom rotācija
  725.                                
  726.                                
  727.               if (this.man.animation.current === 'run' && this.canJump) this.man.animation.play('idle')
  728.                                                   $.post(
  729.                           "postanimation.php", {
  730.                               animation: this.man.animation.current
  731.                           },
  732.                           function (data) {
  733.                                                                 //console.log("viens", x);
  734.                               //console.log("izsaukta", v3);
  735.                           }
  736.                       );
  737.             }
  738.  
  739.             /**
  740.              * Player Jump
  741.              */
  742.             if (this.keys.space.isDown && this.canJump) {
  743.               this.jump()
  744.             }
  745.           }
  746.         }
  747.       }
  748.  
  749.       const config = {
  750.         type: Phaser.WEBGL,
  751.         transparent: true,
  752.         scale: {
  753.           mode: Phaser.Scale.FIT,
  754.           autoCenter: Phaser.Scale.CENTER_BOTH,
  755.           width: window.innerWidth * Math.max(1, window.devicePixelRatio / 2),
  756.           height: window.innerHeight * Math.max(1, window.devicePixelRatio / 2)
  757.         },
  758.         scene: [MainScene],
  759.         ...Canvas({ antialias: false })
  760.       }
  761.  
  762.       window.addEventListener('load', () => {
  763.         enable3d(() => new Phaser.Game(config)).withPhysics('/lib/ammo/kripken')
  764.       })
  765.     </script>
  766.   </body>
  767. </html>
  768.  
  769.