Advertisement
vmars316

~paddle-main-WIP.js

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