Advertisement
vmars316

BenghaziGame

Mar 5th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.  
  3.     "use strict";
  4.      // http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_function
  5.     var red = 153;
  6.     var green = 45;
  7.     var blue = 45;
  8.     var truth01Trips = 0; // max 6
  9.     var truth02Trips = 0; // max 4
  10.     var truth03Trips = 0; // max 7
  11.     var truth04Trips = 0; // max 5
  12.     var ball, compound, manufacturer, thrower;
  13.     var truth01, truth02, truth03, truth04;
  14.     var lies01, lies02, lies03, lies04;
  15.     var Cursor;
  16.     var totalScore = 0; var oopsScore = 0; var goodHits = 0; var totalShots = 0;
  17.    
  18.     // imports
  19.     var CommandEnum = com.dgsprb.quick.CommandEnum;
  20.     var Quick = com.dgsprb.quick.Quick;
  21.     var GameObject = com.dgsprb.quick.GameObject;
  22.     var Rect = com.dgsprb.quick.Rect;
  23.     var ImageFactory = com.dgsprb.quick.ImageFactory;
  24.     var Scene = com.dgsprb.quick.Scene;
  25. //  var Text = com.dgsprb.quick.Text;  
  26.  
  27.     // static
  28.     function main() {
  29.         Quick.setAutoScale(false);
  30.         Quick.setName("Lies&Cowpies");
  31.         Quick.init(function () { return new FirstScene() });
  32.     }
  33.  
  34.     var Background = (function () {
  35.         function Background() {
  36.             GameObject.call(this);
  37.             this.setColor("rgb(" + red + "," + green + "," + blue + ")");
  38.             this.setWidth(Quick.getWidth());
  39.             this.setHeight(Quick.getHeight());
  40.         };
  41.         Background.prototype = Object.create(GameObject.prototype);
  42.  
  43.         return Background;
  44.     })();
  45.  
  46.     var RestartBtn = (function () { // RestartBtn class namespace
  47.     function RestartBtn() { // RestartBtn class constructor method
  48.         GameObject.call(this); // call the constructor from the superclass
  49.         this.setImageId("restartBtn"); // setImageId, a method inherited from Sprite
  50.         this.pointer = Quick.getPointer(); // pointer is a member property of RestartBtn
  51.         this.setBoundary(new Rect(100, 570, 100, 30)); // a method of Sprite, to set this boundaries
  52.         this.setSolid();
  53.         this.setEssential();  
  54.         this.setBottom(600); this.setLeft(100) ;  this.setTop(572);
  55.         }  
  56.         RestartBtn.prototype = Object.create(GameObject.prototype);
  57.             return RestartBtn; // finally publishes the class to the outer scope
  58. })();  
  59.     var PauseBtn = (function () { // PauseBtn class namespace
  60.     function PauseBtn() { // PauseBtn class constructor method
  61.         GameObject.call(this); // call the constructor from the superclass
  62.         this.setImageId("pauseBtn"); // setImageId, a method inherited from Sprite
  63.         this.pointer = Quick.getPointer(); // pointer is a member property of PauseBtn
  64.         this.setBoundary(new Rect(210, 570, 100, 30)); // a method of Sprite, to set the boundaries of PauseBtn
  65.         this.setSolid();
  66.         this.setEssential();  // a method of GameObject, sets the PauseBtn object as essential to its Scene,  that if this object expires,  the scene will expire too.  
  67.         this.setBottom(600); this.setLeft(210);  this.setTop(572);
  68.         }  
  69.         PauseBtn.prototype = Object.create(GameObject.prototype);
  70.             return PauseBtn; // finally publishes the class to the outer scope
  71. })();  
  72.     var PlayBtn = (function () { // PlayBtn class namespace
  73.     function PlayBtn() { // PlayBtn class constructor method
  74.         GameObject.call(this); // call the constructor from the superclass
  75.         this.setImageId("playBtn"); // setImageId, a method inherited from Sprite
  76.         this.pointer = Quick.getPointer(); // pointer is a member property of PlayBtn
  77.         // getPointer is a static method of Quick
  78.         this.setBoundary(new Rect(320, 570, 100, 30)); // a method of Sprite, to set the boundaries of PlayBtn
  79.         this.setSolid();
  80.         this.setEssential();  // a method of GameObject, sets the PlayBtn object as essential to its Scene,  that if this object expires,  the scene will expire too.  
  81.         this.setBottom(600); this.setLeft(320);  this.setTop(572);
  82.         }  
  83.         PlayBtn.prototype = Object.create(GameObject.prototype);
  84.             return PlayBtn; // finally publishes the class to the outer scope
  85. })();  
  86.     var QuitBtn = (function () { // QuitBtn class namespace
  87.     function QuitBtn() { // QuitBtn class constructor method
  88.         GameObject.call(this); // call the constructor from the superclass
  89.         this.setImageId("quitBtn"); // setImageId, a method inherited from Sprite
  90.         this.pointer = Quick.getPointer(); // pointer is a member property of QuitBtn
  91.         // getPointer is a static method of Quick
  92.         this.setBoundary(new Rect(430, 570, 100, 30)); // a method of Sprite, to set the boundaries of QuitBtn
  93.         this.setSolid();
  94.         this.setEssential();  // a method of GameObject, sets the QuitBtn object as essential to its Scene,  that if this object expires,  the scene will expire too.  
  95.         this.setBottom(600); this.setLeft(430);  this.setTop(572);
  96.         }  
  97.         QuitBtn.prototype = Object.create(GameObject.prototype);
  98.             return QuitBtn; // finally publishes the class to the outer scope
  99. })();  
  100.  
  101.     var Compound = (function () { // Compound class namespace
  102.     function Compound() { // Compound class constructor method
  103.         GameObject.call(this); // call the constructor from the superclass
  104.         this.setImageId("bgCompound"); // setImageId, a method inherited from Sprite
  105.         this.pointer = Quick.getPointer(); // pointer is a member property of Compound
  106.         // getPointer is a static method of Quick
  107.         this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Compound
  108.         this.setEssential();  // a method of GameObject, sets the Compound object as essential to its Scene,  that if this object expires,  the scene will expire too.  
  109.         this.setBottom(Quick.getHeight() - this.getHeight());
  110.         this.setLeft(0);  this.setTop(378);
  111.         }  
  112.         Compound.prototype = Object.create(GameObject.prototype);
  113.             return Compound; // finally publishes the class to the outer scope
  114. })();  
  115.     var Manufacturer = (function () { // Manufacturer class namespace
  116.     function Manufacturer() { // Manufacturer class constructor method
  117.         GameObject.call(this); // call the constructor from the superclass
  118.         this.setImageId("manufacturer"); //setImageId, a method inherited from Sprite
  119.         this.pointer = Quick.getPointer(); // pointer is a member property of Manufacturer
  120.         // getPointer is a static method of Quick
  121.         this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Manufacturer
  122.         this.setEssential();  // a method of GameObject, sets the Manufacturer object as essential to its Scene,  that if this object expires,  the scene will expire too.  
  123.         this.setBottom(Quick.getHeight() - this.getHeight());
  124.         this.setLeft(618);  this.setTop(468);
  125.         }  
  126.         Manufacturer.prototype = Object.create(GameObject.prototype); // this says the Ball class
  127.             return Manufacturer; // finally publishes the class to the outer scope
  128. })();   //
  129.     var Ball = (function () { // Ball class namespace - this reads just like "class Ball" in Java and encompasses the class definition until the last line of this excerpt
  130.     function Ball() { // Ball class constructor method - what happens when "new Ball()" is issued
  131.         GameObject.call(this); // call the constructor from the superclass - this is done automatically in Java
  132.         this.setImageId("cowpieSprite"); // setImageId is a method inherited from Sprite - correct!
  133.         this.pointer = Quick.getPointer(); // pointer is a member property of Ball
  134.         // getPointer is a static method of Quick, I guess a static method is just a method of Quick, which is a static class whose public members are accessible without creating an instance of that class. - you nailed it!
  135.         this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Ball
  136. //        this.setEssential(); // a method of GameObject, sets the Ball object as essential to its Scene, Essential to its Scene -means that if this object expires,  the scene will expire too.
  137.         this.setSolid(); // a method of GameObject , so Ball can collide with other solid objects - correct!
  138.         this.setBottom(Quick.getHeight() - this.getHeight());
  139.         this.setTop(500);
  140. //      this.controller = Quick.getController();
  141.     };
  142.     Ball.prototype = Object.create(GameObject.prototype); // this says the Ball class inherits from GameObject - reads just like "extends GameObject" in Java
  143.     // override - this comment means the following method overrides (rewrites) the method with the same name defined in the super class (in this case, GameObject) and though it's not necessary to put that comment there it can be useful for code readers
  144. //
  145.     Ball.prototype.onCollision = function (gameObject) { // still no instance of Ball here, just class method definition - no instance of this class is created until "new Ball()" is issued
  146.         var collision = this.getCollision(gameObject); // a method of Ball Rect to get direction
  147.         if (gameObject.hasTag("lies02")) {
  148.             lies02.setLeft(0); truth02.setSpeedX(0); lies02.setVisible(false);  
  149.             truth02.setVisible(true); truth02.setSpeedX(4);
  150.             Quick.play("pingSound"); // calls a static method from Quick class
  151.             goodHits ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2));  // each goodHit = 3 points , each oopsHits = -2 points
  152.             this.expire();  
  153.             };
  154.         if (gameObject.hasTag("truth02")) { // "returns true if object contains the given tag"
  155. //          truth02.setVisible(true); truth02.setSpeedX(3);
  156.             Quick.play("pingSound"); // calls a static method from Quick class
  157.             Quick.play("pongSound"); // calls a static method from Quick class
  158.             oopsScore ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2));
  159.             this.expire();
  160.             };
  161.             document.getElementById('totalScore').innerHTML = totalScore;
  162.             document.getElementById('oopsScore').innerHTML = oopsScore;
  163.             document.getElementById('goodHits').innerHTML = goodHits;
  164.             document.getElementById('totalShots').innerHTML = totalShots;
  165.     };  // endof  Ball.prototype.onCollision  
  166.     // override
  167.                 return Ball; // finally publishes the class to the outer scope
  168. })();
  169. /*
  170. Function UpdateScores () {
  171. //           document.getElementById('totalShots').innerHTML = totalScore + oopsScore +goodHits +totalShots ;
  172.             alert('Function UpdateScores');  
  173. };
  174. */
  175.     var Truth = (function () {
  176.         function Truth() {
  177.             GameObject.call(this);
  178.             this.setImageId("truth02Sprite");
  179.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  180. //          this.setEssential();
  181.             this.setSolid();
  182.             this.setLeft(0);
  183.             this.setTop(120);
  184.             this.setSpeedX(4);
  185.         };
  186.         Truth.prototype = Object.create(GameObject.prototype);
  187.  
  188.         Truth.prototype.offBoundary = function (gameObject) {
  189.            if (this.hasTag("truth02")) {
  190.                 truth02Trips += 1; // increment by 1
  191.                 if(truth02Trips > 2) { truth02Trips = 0;
  192.                     truth02.setLeft(0); truth02.setVisible(false); truth02.setSpeedX(0);       
  193.                 };
  194.                 Quick.play("pingSound"); // calls a static method from Quick class
  195.                 if(truth02.getVisible() )
  196.                 { this.bounceX(); // for the horizontal axis
  197.                 this.setImage(ImageFactory.mirror(this.getImage()));
  198.                 };
  199.                 if(!truth02.getVisible() ) {
  200.                 lies02.setLeft(0); lies02.setVisible(true); lies02.setSpeedX(4);
  201.                 };
  202.         };         
  203.         }
  204.         return Truth;
  205.  
  206.     })();
  207. // 
  208.     var Lies = (function () {
  209.         function Lies() {
  210.             GameObject.call(this);
  211.             this.setImageId("lies02Sprite");
  212.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  213. //          this.setEssential();
  214.             this.setSolid();
  215.             this.setLeft(0);
  216.             this.setTop(120);
  217.             this.setSpeedX(4);
  218.         };
  219.         Lies.prototype = Object.create(GameObject.prototype);
  220.         Lies.prototype.offBoundary = function (gameObject) {
  221.            this.bounceX(); // for the horizontal axis
  222.            this.setImage(ImageFactory.mirror(this.getImage()));
  223.         };
  224.         return Lies;
  225.     })();  
  226. //
  227.     var FirstScene = (function () {
  228.         function FirstScene() {
  229.             Scene.call(this);
  230.             this.add(new Background());
  231.             var compound = new Compound();
  232.             this.add(compound);        
  233.             manufacturer = new Manufacturer();
  234.             this.add(manufacturer);    
  235.             var restartBtn = new RestartBtn();
  236.             this.add(restartBtn);
  237.             restartBtn.addTag("restartBtn");
  238.             var pauseBtn = new PauseBtn();
  239.             this.add(pauseBtn);
  240.             pauseBtn.addTag("pauseBtn");
  241.             var playBtn = new PlayBtn();
  242.             this.add(playBtn);
  243.             playBtn.addTag("playBtn");
  244.             var quitBtn = new QuitBtn();
  245.             this.add(quitBtn);
  246.             quitBtn.addTag("quitBtn");         
  247.             manufacturer = new Manufacturer();
  248.             this.add(manufacturer);        
  249.  
  250.             truth02 = new Truth();
  251.             this.add(truth02);
  252.             truth02.addTag("truth02");
  253.             truth02.setImageId("truth02Sprite");
  254.             truth02.setLeft(0);
  255.             truth02.setTop(120);
  256.             truth02.setSpeedX(9);          
  257.             lies02 = new Lies();
  258.             this.add(lies02);
  259.             lies02.addTag("lies02");
  260.             lies02.setImageId("lies02Sprite");
  261.             lies02.setVisible(false);
  262.             lies02.setLeft(0)    //  (-64);
  263.             lies02.setTop(120);
  264.             lies02.setSpeedX(0);
  265. //         
  266.             var ball = new Ball();
  267.             thrower = new Thrower();
  268.             this.add(thrower);
  269.             var cursorPoint = new Cursor();
  270.             this.add(cursorPoint);
  271.             var spark = new Spark();
  272.             };
  273.         FirstScene.prototype = Object.create(Scene.prototype);
  274.  
  275.         // override
  276.         FirstScene.prototype.getNext = function () {
  277.             return new FirstScene();
  278.         };
  279.  
  280.         return FirstScene;
  281.  
  282.     })();
  283. //
  284.     var Cursor = (function () {
  285.         function Cursor() {
  286.             GameObject.call(this);
  287.             this.addTag("pointerSprite");
  288.             this.controller = Quick.getController();
  289.             this.setBoundary(new Rect(0, 600, Quick.getWidth(), 30)); // a method of Sprite,set Boundary
  290.             this.pointer = Quick.getPointer();
  291.             this.setImageId("pointerSprite");
  292.             this.setEssential();           
  293.             this.setBottom(600); this.setLeft(50); this.setTop(570);    
  294.         };
  295.         Cursor.prototype = Object.create(GameObject.prototype);
  296.        
  297.         Cursor.prototype.fire = function () {
  298.             var spark = new Spark(); // create a brand new spark to be thrown
  299.             var scene = this.getScene(); // ask for the scene the avatar is
  300.             scene.add(spark); // add the spark to the same scene I am
  301.             if (spark) {spark.setX(this.getX()) };     
  302.             spark.setSpeedY(1);
  303.         };
  304.        
  305.         Cursor.prototype.update = function () {
  306.                 var mousePos = this.pointer.getPosition();
  307.                 this.setPosition(mousePos);
  308.             if (this.controller.keyPush(CommandEnum.DOWN) || this.controller.keyPush(CommandEnum.A))
  309.                {  
  310.                 this.fire(); // call the method we defined above
  311.                 };
  312.                 this.setCenterY(587);
  313.         };
  314.         Cursor.prototype.offBoundary = function (gameObject) {
  315.             this.setCenterY(587);
  316.         };  
  317.              
  318.         return Cursor;
  319.     })();
  320.  
  321.     var Spark = (function () { // Spark class namespace
  322.     function Spark() { // Spark class constructor method
  323.         GameObject.call(this); // call the constructor from the superclass
  324.         this.setImageId("sparkSprite"); // setImageId,a method inherited from Sprite
  325.         this.pointer = Quick.getPointer(); // pointer is a member property of Spark
  326.         this.setBoundary(new Rect(0, 600, Quick.getWidth(), 30)); // a method of Sprite,set
  327.         this.setSolid(); // a method of GameObject , so Spark can collide with other solid objects    
  328.         this.setBottom(600); this.setLeft(50); this.setTop(565);    
  329.         this.controller = Quick.getController();
  330.     };
  331.     Spark.prototype = Object.create(GameObject.prototype);
  332.     Spark.prototype.onCollision = function (gameObject) {        
  333.     var collision = this.getCollision(gameObject); // get direction at collision
  334.           if (gameObject.hasTag("restartBtn")) { // returns true if object contains given tag
  335.               Quick.play("pongSound"); // calls a static method from Quick class
  336. //            alert('restartBtn clicked');
  337.             };
  338.           if (gameObject.hasTag("pauseBtn")) { // returns true if object contains given tag
  339.               Quick.play("pongSound"); // calls a static method from Quick class
  340.             };
  341.           if (gameObject.hasTag("playBtn")) { // returns true if object contains given tag
  342.               Quick.play("pongSound"); // calls a static method from Quick class
  343.             };
  344.           if (gameObject.hasTag("quitBtn")) { // returns true if object contains given tag
  345.               Quick.play("pongSound"); // calls a static method from Quick class
  346.             };
  347.     };
  348.                 return Spark; // finally publishes the class to the outer scope
  349. })();
  350.  
  351. //
  352.     var Thrower = (function () {
  353.         function Thrower() {
  354.             GameObject.call(this);
  355.             this.addTag("thrower");
  356.             this.controller = Quick.getController();
  357.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  358.             this.pointer = Quick.getPointer();
  359. //          this.setSolid();
  360.             this.setImageId("throwerSprite");
  361.             this.setEssential();           
  362.             this.setCenterX(Quick.getWidth() / 2);
  363.             this.setBottom(Quick.getHeight() - this.getHeight());
  364.             this.setTop(508);    
  365.         };
  366.         Thrower.prototype = Object.create(GameObject.prototype);
  367.        
  368.         Thrower.prototype.fire = function () {
  369.             var ball = new Ball(); // create a brand new ball to be thrown
  370.             var scene = this.getScene(); // ask for the scene the avatar is
  371.             scene.add(ball); // add the ball to the same scene I am
  372.             if (ball) {ball.setX(this.getX()) }; // within your Thrower update method  
  373.             totalShots ++ ;        
  374.                 document.getElementById('totalScore').innerHTML = totalScore;
  375.                 document.getElementById('oopsScore').innerHTML = oopsScore;
  376.                 document.getElementById('goodHits').innerHTML = goodHits;
  377.                 document.getElementById('totalShots').innerHTML = totalShots;
  378.             ball.setSpeedY(-9);
  379.         };
  380.        
  381.         Thrower.prototype.update = function () {
  382.             if (this.controller.keyDown(CommandEnum.LEFT) && this.getLeft() > 0) {
  383.                 this.moveX(-8);
  384.                 }
  385.             if (this.controller.keyDown(CommandEnum.RIGHT) && this.getRight() < Quick.getWidth()) {
  386.                 this.moveX(8);
  387.                 }              
  388.             if (this.controller.keyPush(CommandEnum.UP) || this.controller.keyPush(CommandEnum.A))
  389.                {        // added UP to A (space bar, fire button) because it is a more traditional fire key
  390.                 this.fire(); // call the method we defined above
  391.                 }
  392.  
  393.                 }  
  394.         Thrower.prototype.offBoundary = function (gameObject) {
  395.             this.setCenterX(Quick.getWidth() / 2);
  396.               };               
  397.        
  398.         return Thrower;
  399.     })();
  400.  
  401. //}
  402.  
  403.     main();
  404.  
  405. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement