|
| 1 | +$(document).ready(function(){ |
| 2 | + new WOW().init(); |
| 3 | + var game = { |
| 4 | + init: function(vehicle){ |
| 5 | + this.vehicle=vehicle; |
| 6 | + this.setIntervalIdList=[]; |
| 7 | + this.monsterList=[]; |
| 8 | + this.monsterIdTracker=0; |
| 9 | + this.monsterMoveIntervalIds={}; |
| 10 | + this.monsterHeight=50; |
| 11 | + this.monsterWidth=50; |
| 12 | + this.minionHeight=50; |
| 13 | + this.minionWidth=50; |
| 14 | + //$(this.vehicle).css('left','20px'); |
| 15 | + //$(this.vehicle).css('top','20px'); |
| 16 | + this.args = { |
| 17 | + right: {angle: "360deg", position: "left", increment: true}, |
| 18 | + down: {angle: "90deg", position: "top", increment: true}, |
| 19 | + left: {angle: "360deg", position: "left", increment: false}, |
| 20 | + up: {angle: "90deg", position: "top", increment: false} |
| 21 | + }; |
| 22 | + |
| 23 | + }, |
| 24 | + move: function(direction) { |
| 25 | + var thisArgs = this.args[direction]; |
| 26 | + this.carStop(); |
| 27 | + // $(this.vehicle).css('transform','rotateZ(' + thisArgs.angle + ')'); |
| 28 | + this.vehicleSetIntervalId= setInterval(frame,10); |
| 29 | + this.setIntervalIdList.push(this.vehicleSetIntervalId); |
| 30 | + var pos = parseInt($(this.vehicle).css(thisArgs.position)); |
| 31 | + var vehicle = this.vehicle; |
| 32 | + function frame(){ |
| 33 | + if (thisArgs.increment){ |
| 34 | + pos++; |
| 35 | + }else{ |
| 36 | + pos--; |
| 37 | + } |
| 38 | + // Check if the object has hit the monsters or the border |
| 39 | + $('.monster').each(function()){ |
| 40 | + monster=$(this); |
| 41 | + |
| 42 | + |
| 43 | + } |
| 44 | + $(vehicle).css(thisArgs.position,pos + 'px'); |
| 45 | + } |
| 46 | + }, |
| 47 | + carStop: function(){ |
| 48 | + if (this.vehicleSetIntervalId){ |
| 49 | + clearInterval(this.vehicleSetIntervalId); |
| 50 | + } |
| 51 | + }, |
| 52 | + right: function(){this.move("right")}, |
| 53 | + left: function(){this.move("left")}, |
| 54 | + up: function(){this.move("up")}, |
| 55 | + down: function(){this.move("down")}, |
| 56 | + stop: function(){ |
| 57 | + this.setIntervalIdList.forEach(function(id){ |
| 58 | + clearInterval(id); |
| 59 | + }); |
| 60 | + }, |
| 61 | + startMonsterCreation: function(){ |
| 62 | + dropMonsterFunctionId=setInterval(dropMonster,5000); |
| 63 | + this.setIntervalIdList.push(dropMonsterFunctionId); |
| 64 | + //this.monsterIdTracker++; |
| 65 | + var gameObj=this; |
| 66 | + function dropMonster(){ |
| 67 | + console.log('monster is going to come now'); |
| 68 | + // Create a monster |
| 69 | + var monster=$("<div/>"); |
| 70 | + monster.addClass('monster wow tada'); |
| 71 | + gameObj.monsterIdTracker++; |
| 72 | + var monsterId='monster'+gameObj.monsterIdTracker; |
| 73 | + monster.attr('id',monsterId) |
| 74 | + monster.appendTo($('#playarea')); |
| 75 | + gameObj.monsterList.push(monster); |
| 76 | + |
| 77 | + monsterMovementSetIntervalId=setInterval(makeMonsterMove,1000); |
| 78 | + gameObj.setIntervalIdList.push(monsterMovementSetIntervalId); |
| 79 | + //var gameObj=this; |
| 80 | + function makeMonsterMove(){ |
| 81 | + // Make it move randomly |
| 82 | + randomDirection=Math.ceil(Math.random()*4); |
| 83 | + switch(randomDirection){ |
| 84 | + case 1: |
| 85 | + gameObj.moveMonster("right",monster); |
| 86 | + break; |
| 87 | + case 2: |
| 88 | + gameObj.moveMonster("left",monster); |
| 89 | + break; |
| 90 | + case 3: |
| 91 | + gameObj.moveMonster("up",monster); |
| 92 | + break; |
| 93 | + case 4: |
| 94 | + gameObj.moveMonster("down",monster); |
| 95 | + break; |
| 96 | + |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }, |
| 101 | + moveMonster: function(direction,monster){ |
| 102 | + var thisArgs = this.args[direction]; |
| 103 | + monsterId=monster.attr('id'); |
| 104 | + this.monsterStop(monsterId); |
| 105 | + // monster.css('transform','rotateZ(' + thisArgs.angle + ')'); |
| 106 | + this.monsterMoveIntervalIds[monsterId]=setInterval(frame,10) |
| 107 | + this.setIntervalIdList.push(this.monsterMoveIntervalIds[monsterId]); |
| 108 | + var pos = parseInt(monster.css(thisArgs.position)); |
| 109 | + function frame(){ |
| 110 | + if (thisArgs.increment){ |
| 111 | + pos++; |
| 112 | + }else{ |
| 113 | + pos--; |
| 114 | + } |
| 115 | + monster.css(thisArgs.position,pos + 'px'); |
| 116 | + } |
| 117 | + |
| 118 | + }, |
| 119 | + monsterStop: function(monsterId){ |
| 120 | + if (this.monsterMoveIntervalIds[monsterId]){ |
| 121 | + clearInterval(this.monsterMoveIntervalIds[monsterId]); |
| 122 | + } |
| 123 | + }, |
| 124 | + start: function(){ |
| 125 | + this.startMonsterCreation(); |
| 126 | + this.checkBorderClash(); |
| 127 | + }, |
| 128 | + pause: function(){ |
| 129 | + |
| 130 | + }, |
| 131 | + checkBorderClash:function(){ |
| 132 | + // Check if any of the monster has touched the border every 10 ms |
| 133 | + this.checkBorderClashIntervalId=setInterval(monsterCheck,10); |
| 134 | + this.setIntervalIdList.push(this.checkBorderClashIntervalId); |
| 135 | + var gameObj=this; |
| 136 | + // Grab all monsters |
| 137 | + function monsterCheck(){ |
| 138 | + if ($('.monster').length == 0){ |
| 139 | + return; |
| 140 | + } |
| 141 | + $('.monster').each(function(){ |
| 142 | + // Get the top position of monster |
| 143 | + monster=$(this); |
| 144 | + positionTop=parseInt(monster.css('top')); |
| 145 | + positionLeft=parseInt(monster.css('left')); |
| 146 | + playAreaHeight=parseInt($('#playarea').css('height')); |
| 147 | + playAreaWidth=parseInt($('#playarea').css('width')); |
| 148 | + if (positionTop<=0 || positionLeft<=0 || positionLeft >= playAreaWidth |
| 149 | + || (positionTop+50)>=playAreaHeight){ |
| 150 | + gameObj.monsterStop(monster.attr('id')); |
| 151 | + monster.addClass('shake'); |
| 152 | + monster.attr("wow-duration","1s"); |
| 153 | + monster.fadeIn(1200); |
| 154 | + monster.remove(); |
| 155 | + } |
| 156 | + }); |
| 157 | + } |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + |
| 162 | +var car = $('#car'); |
| 163 | +game.init(car); |
| 164 | +// Register mouse clicks |
| 165 | +$('#start').click(function(){ |
| 166 | + game.start(); |
| 167 | +}); |
| 168 | +$('#stop').click(function(){ |
| 169 | + game.stop(); |
| 170 | +}); |
| 171 | +// Handle Key press event |
| 172 | +$(document).keydown(function(e){ |
| 173 | + switch(e.keyCode){ |
| 174 | + // Left arrow |
| 175 | + case 37: |
| 176 | + game.left(); |
| 177 | + e.preventDefault(); |
| 178 | + break; |
| 179 | + // Up Arrow |
| 180 | + case 38: |
| 181 | + game.up(); |
| 182 | + e.preventDefault(); |
| 183 | + break; |
| 184 | + // Right Arrow |
| 185 | + case 39: |
| 186 | + game.right(); |
| 187 | + e.preventDefault(); |
| 188 | + break; |
| 189 | + // Down Arrow |
| 190 | + case 40: game.down(); |
| 191 | + e.preventDefault(); |
| 192 | + break; |
| 193 | + case 13: |
| 194 | + game.stop(); |
| 195 | + e.preventDefault(); |
| 196 | + break; |
| 197 | + } |
| 198 | +}); |
| 199 | + |
| 200 | +}); |
0 commit comments