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