Advertisement
Jodyone

services.js

May 12th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * service.js
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 8
  6.  *
  7.  * Implements a shuttle service.
  8.  */
  9.  
  10. // default height
  11. var HEIGHT = 0.8;
  12.  
  13. // default latitude
  14. var LATITUDE = 42.3745615030193;
  15.  
  16. // default longitude
  17. var LONGITUDE = -71.11803936751632;
  18.  
  19. // default heading
  20. var HEADING = 1.757197490907891;
  21.  
  22. // default number of seats
  23. var SEATS = 10;
  24.  
  25. // default velocity
  26. var VELOCITY = 50;
  27.  
  28. // global reference to shuttle's marker on 2D map
  29. var bus = null;
  30.  
  31. // global reference to 3D Earth
  32. var earth = null;
  33.  
  34. // global reference to 2D map
  35. var map = null;
  36.  
  37. // global reference to shuttle
  38. var shuttle = null;
  39.  
  40. // global reference to points
  41. var points = 0;
  42.  
  43.  
  44.  
  45. // load version 1 of the Google Earth API
  46. google.load("earth", "1");
  47.  
  48. // load version 3 of the Google Maps API
  49. google.load("maps", "3", {other_params: "sensor=false"});
  50.  
  51. // once the window has loaded
  52. $(window).load(function() {
  53.  
  54.     // listen for keydown anywhere in body
  55.     $(document.body).keydown(function(event) {
  56.         return keystroke(event, true);
  57.     });
  58.  
  59.     // listen for keyup anywhere in body
  60.     $(document.body).keyup(function(event) {
  61.         return keystroke(event, false);
  62.     });
  63.  
  64.     // listen for click on Drop Off button
  65.     $("#dropoff").click(function(event) {
  66.         dropoff();
  67.     });
  68.  
  69.     // listen for click on Pick Up button
  70.     $("#pickup").click(function(event) {
  71.         pickup();
  72.     });
  73.  
  74.     // load application
  75.     load();
  76. });
  77.  
  78. // unload application
  79. $(window).unload(function() {
  80.     unload();
  81. });
  82.  
  83. /**
  84.  * Renders seating chart.
  85.  */
  86. function chart()
  87. {
  88.  
  89.     var html = "<ol start='0'>";
  90.     for (var j = 0; j < shuttle.seats.length; j++)
  91.     {
  92.            
  93.         if (shuttle.seats[j] == null)
  94.         {
  95.            
  96.             html += "<li>Empty Seat</li>";
  97.         }
  98.         else
  99.         {
  100.            
  101.            
  102.             html += "<li>" + shuttle.seats[j].html + "</li>";
  103.         }
  104.     }
  105.     html += "</ol>";
  106.     $("#chart").html(html);
  107.    
  108. }
  109. /**
  110.  * dropdown menu for teleport
  111.  */
  112.  function teleport()
  113. {
  114.      // loop to store HOUSES array in house
  115.      for(var k = 0; k < HOUSES.length; k++)
  116.          {
  117.              var house = HOUSES[k];
  118.              console.log(house);
  119.          }
  120.      
  121.           var html = "<ol start='0'>";
  122.          for (var l = 0; l < HOUSES.length; l++)
  123.          {
  124.            
  125.             html += "<select><option value='"+house+"'>"+house+"</option>";
  126.    
  127.          }
  128.          html += "</ol>";
  129.          $("#teleport").html(html);
  130.  
  131.        
  132.  
  133. }
  134.  
  135. /**
  136.  * Drops off passengers if their stop is nearby.
  137.  */
  138. function dropoff()
  139. {
  140.      
  141.         for (var j = 0; j < shuttle.seats.length; j++)
  142.         {
  143.            if (shuttle.seats[j] != null)
  144.            {
  145.                 var house = shuttle.seats[j].x;
  146.            
  147.                 var d_from_h = shuttle.distance(HOUSES[house].lat,HOUSES[house].lng);
  148.              
  149.                 if ( d_from_h < 30.0)
  150.                 {
  151.                    
  152.                     shuttle.seats[j] = null;  
  153.                     points++;
  154.                    $("#announcements").html("Driver has " + points + " points!" );
  155.                 }
  156.             }
  157.    
  158.         }
  159.         if (points == 101)
  160.         {
  161.             $("#announcements").html("ALL PASSENGERS ARE DROPPED OFF");
  162.         }
  163.  
  164.     chart();
  165. }
  166.  
  167. /**
  168.  * Called if Google Earth fails to load.
  169.  */
  170. function failureCB(errorCode)
  171. {
  172.     // report error unless plugin simply isn't installed
  173.     if (errorCode != ERR_CREATE_PLUGIN)
  174.     {
  175.         alert(errorCode);
  176.     }
  177. }
  178.  
  179. /**
  180.  * Handler for Earth's frameend event.
  181.  */
  182. function frameend()
  183. {
  184.     shuttle.update();
  185. }
  186.  
  187. /**
  188.  * Called once Google Earth has loaded.
  189.  */
  190. function initCB(instance)
  191. {
  192.     // retain reference to GEPlugin instance
  193.     earth = instance;
  194.  
  195.     // specify the speed at which the camera moves
  196.     earth.getOptions().setFlyToSpeed(100);
  197.  
  198.     // show buildings
  199.     earth.getLayerRoot().enableLayerById(earth.LAYER_BUILDINGS, true);
  200.  
  201.     // disable terrain (so that Earth is flat)
  202.     earth.getLayerRoot().enableLayerById(earth.LAYER_TERRAIN, false);
  203.  
  204.     // prevent mouse navigation in the plugin
  205.     earth.getOptions().setMouseNavigationEnabled(false);
  206.  
  207.     // instantiate shuttle
  208.     shuttle = new Shuttle({
  209.         heading: HEADING,
  210.         height: HEIGHT,
  211.         latitude: LATITUDE,
  212.         longitude: LONGITUDE,
  213.         planet: earth,
  214.         seats: SEATS,
  215.         velocity: VELOCITY
  216.     });
  217.  
  218.     // synchronize camera with Earth
  219.     google.earth.addEventListener(earth, "frameend", frameend);
  220.  
  221.     // synchronize map with Earth
  222.     google.earth.addEventListener(earth.getView(), "viewchange", viewchange);
  223.  
  224.     // update shuttle's camera
  225.     shuttle.updateCamera();
  226.  
  227.     // show Earth
  228.     earth.getWindow().setVisibility(true);
  229.  
  230.     // render seating chart
  231.     chart();
  232.  
  233.     // populate Earth with passengers and houses
  234.     populate();
  235. }
  236.  
  237. /**
  238.  * Handles keystrokes.
  239.  */
  240. function keystroke(event, state)
  241. {
  242.     // ensure we have event
  243.     if (!event)
  244.     {
  245.         event = window.event;
  246.     }
  247.  
  248.     // left arrow
  249.     if (event.keyCode == 37)
  250.     {
  251.         shuttle.states.turningLeftward = state;
  252.         return false;
  253.     }
  254.  
  255.     // up arrow
  256.     else if (event.keyCode == 38)
  257.     {
  258.         shuttle.states.tiltingUpward = state;
  259.         return false;
  260.     }
  261.  
  262.     // right arrow
  263.     else if (event.keyCode == 39)
  264.     {
  265.         shuttle.states.turningRightward = state;
  266.         return false;
  267.     }
  268.  
  269.     // down arrow
  270.     else if (event.keyCode == 40)
  271.     {
  272.         shuttle.states.tiltingDownward = state;
  273.         return false;
  274.     }
  275.  
  276.     // A, a
  277.     else if (event.keyCode == 65 || event.keyCode == 97)
  278.     {
  279.         shuttle.states.slidingLeftward = state;
  280.         return false;
  281.     }
  282.  
  283.     // D, d
  284.     else if (event.keyCode == 68 || event.keyCode == 100)
  285.     {
  286.         shuttle.states.slidingRightward = state;
  287.         return false;
  288.     }
  289.  
  290.     // S, s
  291.     else if (event.keyCode == 83 || event.keyCode == 115)
  292.     {
  293.         shuttle.states.movingBackward = state;    
  294.         return false;
  295.     }
  296.  
  297.     // W, w
  298.     else if (event.keyCode == 87 || event.keyCode == 119)
  299.     {
  300.         shuttle.states.movingForward = state;    
  301.         return false;
  302.     }
  303.     // Q increase velocity
  304.       else if (event.keyCode == 81 )
  305.     {
  306.          
  307.         shuttle.velocity = 200;
  308.         return false;
  309.     }
  310.         else if (event.keyCode == 69 )
  311.     {
  312.          
  313.         shuttle.velocity = 50;
  314.         return false;
  315.     }
  316.          else if (event.keyCode == 72 )
  317.     {
  318.           teleport();
  319.           return false;
  320.     }
  321.  
  322.     return true;
  323. }
  324.  
  325. /**
  326.  * Loads application.
  327.  */
  328. function load()
  329. {
  330.     // embed 2D map in DOM
  331.     var latlng = new google.maps.LatLng(LATITUDE, LONGITUDE);
  332.     map = new google.maps.Map($("#map").get(0), {
  333.         center: latlng,
  334.         disableDefaultUI: true,
  335.         mapTypeId: google.maps.MapTypeId.ROADMAP,
  336.         scrollwheel: false,
  337.         zoom: 17,
  338.         zoomControl: true
  339.     });
  340.  
  341.     // prepare shuttle's icon for map
  342.     bus = new google.maps.Marker({
  343.         icon: "https://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/bus.png",
  344.         map: map,
  345.         title: "you are here"
  346.     });
  347.  
  348.     // embed 3D Earth in DOM
  349.     google.earth.createInstance("earth", initCB, failureCB);
  350. }
  351.  
  352. /**
  353.  * Picks up nearby passengers.
  354.  */
  355. function pickup()
  356. {
  357.    var features = earth.getFeatures();
  358.  
  359.    for (var i = 0; i < PASSENGERS.length; i++)
  360.    {
  361.        if (PASSENGERS[i].placemark != null)
  362.        {
  363.            var lat = PASSENGERS[i].placemark.getGeometry().getLatitude();
  364.            var lng = PASSENGERS[i].placemark.getGeometry().getLongitude();
  365.            var dist = shuttle.distance(lat,lng);
  366.          
  367.            if (dist < 15.0 )
  368.            {  
  369.              
  370.                 if (!HOUSES[PASSENGERS[i].house])
  371.                 {
  372.                      $("#announcements").html("NO FRESHMAN");
  373.                      break;
  374.                 }
  375.                 else
  376.                 {
  377.                    
  378.                     for(var j = 0; j < shuttle.seats.length; j++)
  379.                     {
  380.                        
  381.                        if (shuttle.seats[j] == null)
  382.                        {
  383.                            
  384.                          shuttle.seats[j] = {html:PASSENGERS[i].name + ' at ' + PASSENGERS[i].house,
  385.                                               x:PASSENGERS[i].house};
  386.                            features.removeChild(PASSENGERS[i].placemark);  
  387.                            PASSENGERS[i].marker.setMap(null);
  388.                            break;
  389.  
  390.                        
  391.                         }
  392.                         if (shuttle.seats[j] != null)
  393.                         {
  394.                              $("#announcements").html("NO SEATS AVAILABLE");
  395.                         }  
  396.  
  397.                }
  398.                }
  399.                    
  400.              
  401.            }
  402.            
  403.            else
  404.            {
  405.                $("#announcements").html("no passengers in range");
  406.            }  
  407.         }  
  408.    }
  409.    chart();
  410.      
  411. }
  412.  
  413. /**
  414.  * Populates Earth with passengers and houses.
  415.  */
  416. function populate()
  417. {
  418.     // mark houses
  419.     for (var house in HOUSES)
  420.     {
  421.         // plant house on map
  422.         new google.maps.Marker({
  423.             icon: "https://google-maps-icons.googlecode.com/files/home.png",
  424.             map: map,
  425.             position: new google.maps.LatLng(HOUSES[house].lat, HOUSES[house].lng),
  426.             title: house
  427.         });
  428.     }
  429.  
  430.     // get current URL, sans any filename
  431.     var url = window.location.href.substring(0, (window.location.href.lastIndexOf("/")) + 1);
  432.  
  433.     // scatter passengers
  434.     for (var i = 0; i < PASSENGERS.length; i++)
  435.     {
  436.         // pick a random building
  437.         var building = BUILDINGS[Math.floor(Math.random() * BUILDINGS.length)];
  438.  
  439.         // prepare placemark
  440.         var placemark = earth.createPlacemark("");
  441.         placemark.setName(PASSENGERS[i].name + " to " + PASSENGERS[i].house);
  442.  
  443.         // prepare icon
  444.         var icon = earth.createIcon("");
  445.         icon.setHref(url + "/img/" + PASSENGERS[i].username + ".jpg");
  446.  
  447.         // prepare style
  448.         var style = earth.createStyle("");
  449.         style.getIconStyle().setIcon(icon);
  450.         style.getIconStyle().setScale(4.0);
  451.  
  452.         // prepare stylemap
  453.         var styleMap = earth.createStyleMap("");
  454.         styleMap.setNormalStyle(style);
  455.         styleMap.setHighlightStyle(style);
  456.  
  457.         // associate stylemap with placemark
  458.         placemark.setStyleSelector(styleMap);
  459.  
  460.         // prepare point
  461.         var point = earth.createPoint("");
  462.         point.setAltitudeMode(earth.ALTITUDE_RELATIVE_TO_GROUND);
  463.         point.setLatitude(building.lat);
  464.         point.setLongitude(building.lng);
  465.         point.setAltitude(0.0);
  466.  
  467.         // associate placemark with point
  468.         placemark.setGeometry(point);
  469.  
  470.         // add placemark to Earth
  471.         earth.getFeatures().appendChild(placemark);
  472.  
  473.         // add marker to map
  474.         var marker = new google.maps.Marker({
  475.             icon: "https://maps.gstatic.com/intl/en_us/mapfiles/ms/micons/man.png",
  476.             map: map,
  477.             position: new google.maps.LatLng(building.lat, building.lng),
  478.             title: PASSENGERS[i].name + " at " + building.name
  479.         });
  480.  
  481.         // TODO: remember passenger's placemark and marker for pick-up's sake
  482.         PASSENGERS[i].placemark = placemark;
  483.         PASSENGERS[i].marker = marker;
  484.  
  485.     }
  486.  
  487. }
  488.  
  489. /**
  490.  * Handler for Earth's viewchange event.
  491.  */
  492. function viewchange()
  493. {
  494.     // keep map centered on shuttle's marker
  495.     var latlng = new google.maps.LatLng(shuttle.position.latitude, shuttle.position.longitude);
  496.     map.setCenter(latlng);
  497.     bus.setPosition(latlng);
  498. }
  499.  
  500. /**
  501.  * Unloads Earth.
  502.  */
  503. function unload()
  504. {
  505.     google.earth.removeEventListener(earth.getView(), "viewchange", viewchange);
  506.     google.earth.removeEventListener(earth, "frameend", frameend);
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement