Advertisement
vmars316

BenghaziGame

Feb 22nd, 2016
93
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 throwerLeftX = 0;
  6.     var throwerTopY = 0;
  7.     // imports
  8.     var CommandEnum = com.dgsprb.quick.CommandEnum;
  9.     var Quick = com.dgsprb.quick.Quick;
  10.     var GameObject = com.dgsprb.quick.GameObject;
  11.     var Rect = com.dgsprb.quick.Rect;
  12.     var ImageFactory = com.dgsprb.quick.ImageFactory;
  13.     var Scene = com.dgsprb.quick.Scene;
  14.     var Text = com.dgsprb.quick.Text;  
  15.  
  16.     // static
  17.     function main() {
  18.         Quick.setAutoScale(false);
  19.         Quick.setName("Lies&Cowpies");
  20.         Quick.init(function () { return new FirstScene() });
  21.     }
  22.  
  23.     var Background = (function () {
  24.  
  25.         function Background() {
  26.             GameObject.call(this);
  27.             this.setColor("Green");
  28.             this.setWidth(Quick.getWidth());
  29.             this.setHeight(Quick.getHeight());
  30.         };
  31.         Background.prototype = Object.create(GameObject.prototype);
  32.  
  33.         return Background;
  34.     })();
  35.  
  36.     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
  37.     function Ball() { // Ball class constructor method - what happens when "new Ball()" is issued
  38.         GameObject.call(this); // call the constructor from the superclass - this is done automatically in Java
  39.         this.setImageId("cowpieSprite"); // setImageId is a method inherited from Sprite - correct!
  40.         this.pointer = Quick.getPointer(); // pointer is a member property of Ball
  41.         // 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!
  42.         this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Ball
  43. //        this.setEssential(); // a method of GameObject, sets the Ball object as essential to its Scene,  
  44.         // I don't what it means to be essential to its Scene - it means that if this object expires,  
  45.         // the scene will expire too.
  46.         this.setSolid(); // a method of GameObject , so Ball can collide with other solid objects - correct!
  47. //      this.setCenterX((Quick.getWidth() / 2) - 20);
  48.         this.setBottom(Quick.getHeight() - this.getHeight());
  49.         this.setTop(500);
  50.         this.controller = Quick.getController();
  51.         this.pointer = Quick.getPointer();
  52.  
  53.     };
  54.     Ball.prototype = Object.create(GameObject.prototype); // this says the Ball class inherits from GameObject - reads just like "extends GameObject" in Java
  55.     // 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
  56.     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
  57.         var collision = this.getCollision(gameObject); // a method of Ball Rect to get direction at collision - right!
  58. //      this.setSize(64, 64);
  59. //        this.bounceFrom(collision); // a method of Ball Rect to bounce away from the current direction - right!
  60.         if (gameObject.hasTag("lies02")) { // a method of GameObject , "returns true if object contains the given tag" - perfect!
  61.             Quick.play("pingSound"); // calls a static method from Quick class
  62.         }
  63.     };  // endof  Ball.prototype.onCollision  
  64.         // override
  65. /*      Ball.prototype.offBoundary = function (gameObject) {
  66.         var isItTrue = 'true';
  67.            this.expire();
  68.            if(this.getExpired() == isItTrue) {alert("Yes, Ball.prototype.offBoundary IS Expired"); };
  69.            if(this.getExpired() != isItTrue) {alert("No, Ball.prototype.offBoundary IS NOT Expired"); };
  70.         };
  71. */     
  72.                 return Ball; // finally publishes the class to the outer scope
  73. })();
  74. //
  75.  
  76. //
  77.     var Truth = (function () {
  78.         function Truth() {
  79.             GameObject.call(this);
  80. //          this.setImageId("truth02Sprite");
  81.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  82.             this.setEssential();
  83.             this.setSolid();
  84.             this.setLeft(0);
  85.             this.setTop(100);
  86.             this.setSpeedX(2);
  87.         };
  88.         Truth.prototype = Object.create(GameObject.prototype);
  89.  
  90.         Truth.prototype.offBoundary = function (gameObject) {
  91.             this.bounceX(); // for the horizontal axis
  92.             this.setImage(ImageFactory.mirror(this.getImage()));
  93. //            alert("Truth.prototype.offBoundary");
  94.         };
  95.         return Truth;
  96.  
  97.     })();
  98. // 
  99.     var Lies = (function () {
  100.  
  101.         function Lies() {
  102.             GameObject.call(this);
  103. //          this.setImageId("lies02Sprite");
  104.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  105.             this.setEssential();
  106.             this.setSolid();
  107.             this.setLeft(0);
  108.             this.setTop(120);
  109.             this.setSpeedX(4);
  110.         };
  111.         Lies.prototype = Object.create(GameObject.prototype);
  112.  
  113.         Lies.prototype.offBoundary = function (gameObject) {
  114.             this.bounceX(); // for the horizontal axis
  115.             this.setImage(ImageFactory.mirror(this.getImage()));
  116. //            alert("Lies.prototype.offBoundary");
  117.         };
  118.         return Lies;
  119.  
  120.     })();  
  121. //
  122.     var FirstScene = (function () {
  123.  
  124.         function FirstScene() {
  125.             Scene.call(this);
  126.             this.add(new Background());
  127.             var ball = new Ball();
  128. //          this.add(ball);
  129.             var thrower = new Thrower();
  130.             this.add(thrower);
  131.             var truth02 = new Truth();
  132.             this.add(truth02);
  133.             truth02.addTag("truth02");
  134.             truth02.setImageId("truth02Sprite");
  135.             truth02.setLeft(0);
  136.             truth02.setTop(120);
  137.             truth02.setSpeedX(2);          
  138.             var lies02 = new Lies();
  139.             this.add(lies02);
  140.             lies02.addTag("lies02");
  141.             lies02.setImageId("lies02Sprite");
  142.             lies02.setLeft(0)    //  (-64);
  143.             lies02.setTop(120);
  144.             lies02.setSpeedX(4);           
  145. //          Quick.play("pongSound");
  146.         };
  147.         FirstScene.prototype = Object.create(Scene.prototype);
  148.  
  149.         // override
  150.         FirstScene.prototype.getNext = function () {
  151.             return new FirstScene();
  152.         };
  153.  
  154.         return FirstScene;
  155.  
  156.     })();
  157.  
  158.     var Thrower = (function () {
  159.         function Thrower() {
  160.             GameObject.call(this);
  161.             this.addTag("thrower");
  162.             this.controller = Quick.getController();
  163.             this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  164.             this.pointer = Quick.getPointer();
  165.             this.setImageId("throwerSprite");
  166.             this.setSolid();
  167.             this.setEssential();           
  168.             this.setCenterX(Quick.getWidth() / 2);
  169.             this.setBottom(Quick.getHeight() - this.getHeight());
  170. //          this.setX(400);
  171.             this.setTop(500);    
  172.         };
  173.         Thrower.prototype = Object.create(GameObject.prototype);
  174.        
  175.         Thrower.prototype.fire = function () {
  176.             var ball = new Ball(); // create a brand new ball to be thrown
  177.             var scene = this.getScene(); // ask for the scene the avatar is
  178.             scene.add(ball); // add the ball to the same scene I am
  179.             ball.setX(this.getX()); // within your Thrower update method       
  180. //          ball.setSpeedX(0);
  181.             ball.setSpeedY(-9);
  182. //          alert('ball.getX = ' + ball.getX() + '  this.getX = ' + this.getX());
  183.         };
  184.        
  185.         Thrower.prototype.update = function () {
  186.             if (this.controller.keyDown(CommandEnum.LEFT) && this.getLeft() > 0) {
  187.                 this.moveX(-8);
  188.                 }
  189.             if (this.controller.keyDown(CommandEnum.RIGHT) && this.getRight() < Quick.getWidth()) {
  190.                 this.moveX(8);
  191.                 }              
  192.             if (this.controller.keyDown(CommandEnum.UP) || this.controller.keyDown(CommandEnum.A))
  193.                { // added UP to A (space bar, fire button) because it is a more traditional fire key
  194. //                this.ball.setX(this.getX()); // within your Thrower update method    
  195.                 this.fire(); // call the method we defined above
  196. //              alert('this.getX = ' + this.getX());
  197.                 }
  198.                 }
  199.         Thrower.prototype.offBoundary = function (gameObject) {
  200.             this.setCenterX(Quick.getWidth() / 2);
  201.               };               
  202.        
  203.         return Thrower;
  204.     })();
  205.  
  206.     main();
  207.  
  208. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement