Advertisement
Guest User

Untitled

a guest
May 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. var $ = {
  2. W: 3400,
  3. H: 3400,
  4. cllFldSz: 25,
  5. kick: 0,
  6. pSz :function(){
  7. var size = getRandomInt(11,25);
  8. chance(150,function(){
  9. size = getRandomInt(30,60);
  10. })
  11. chance(450,function(){
  12. size = getRandomInt(90,200);
  13. })
  14. chance(1000,function(){
  15. size = getRandomInt(400,550);
  16. })
  17. return size;
  18. },
  19. particles:400,
  20. drawGrid:0
  21. }
  22. function Realm(){
  23. var world = this;
  24. world.W = $.W;
  25. world.H = $.H;
  26. world.objects = [];
  27. world.canvasEl = document.createElement("canvas");
  28. world.canvasEl.width = world.W;
  29. world.canvasEl.height = world.H;
  30. var ctx = world.canvasEl.getContext("2d");
  31. //world.collFieldSize = 25;
  32. world.optKeys = 0;
  33.  
  34. var CollisionGrid = {
  35. fieldSize : $.cllFldSz,
  36. grid : []
  37. }
  38. var CollisionField = function(){
  39. this.objects = {};
  40. this.optKey = null;
  41. }
  42. CollisionField.prototype.add = function(obj){
  43. this.objects[obj.optKey] = obj;
  44. }
  45. CollisionField.prototype.remove = function(obj){
  46. delete this.objects[obj.optKey];
  47. }
  48. CollisionGrid.grid = (function(){
  49. var grid = [];
  50. var collField;
  51. var cllFldSz = CollisionGrid.fieldSize;
  52. for(var j,i = 0; i < world.W; i += cllFldSz){
  53. grid[i/cllFldSz] = [];
  54. for( j = 0; j < world.H; j+= cllFldSz){
  55. collField = new CollisionField();
  56. grid[i/cllFldSz][j/cllFldSz] = collField;
  57. collField.optKey = world.optKey();
  58. }
  59. }
  60. return grid;
  61. })();
  62.  
  63. function render(){
  64. ctx.fillStyle = "#000000";
  65. ctx.fillRect(0,0,world.W,world.H);
  66. var cllFldSz = CollisionGrid.fieldSize;
  67. var collGridW = world.W/cllFldSz;
  68. var collGridH = world.H/cllFldSz;
  69. var hasObject = false;
  70. var drawGrid = $.drawGrid;
  71. if(drawGrid){
  72. for(var propLen,j,i = 0; i < collGridW; i += 1){
  73. for( j = 0; j < collGridH; j += 1){
  74. hasObject = false;
  75. propLen = Object.keys( CollisionGrid.grid[i][j].objects ).length;
  76. if ( propLen > 0 ){
  77. hasObject = true;
  78. }
  79. if( hasObject ){
  80. ctx.strokeStyle = "#88ff88";
  81. ctx.strokeRect(i*cllFldSz+1,j*cllFldSz+1,cllFldSz-2,cllFldSz-2);
  82. }else{
  83. ctx.strokeStyle = "#880000";
  84. ctx.strokeRect(i*cllFldSz+1,j*cllFldSz+1,cllFldSz-2,cllFldSz-2);
  85. }
  86. }
  87. }
  88. }
  89.  
  90. ctx.strokeStyle = "#ffffff";
  91. ctx.beginPath();
  92. world.objects.forEach(function(obj){
  93. var theCircle = function(x,y){
  94. ctx.moveTo(obj.x + x + obj.size, obj.y + y);
  95. ctx.arc( obj.x + x, obj.y + y, obj.size, 0, Math.PI * 2 );
  96. }
  97. theCircle(0,0);
  98. if(obj.x+obj.size >= world.W){
  99. theCircle(-world.W,0);
  100. }
  101. if(obj.y+obj.size >= world.H){
  102. theCircle(0,-world.H);
  103. }
  104. if(obj.x-obj.size < 0){
  105. theCircle(world.W,0);
  106. }
  107. if(obj.y-obj.size < 0){
  108. theCircle(0,world.H);
  109. }
  110. if((obj.x+obj.size >= world.W)&&(obj.y+obj.size >= world.H)){
  111. theCircle(-world.W,-world.H);
  112. }
  113. if((obj.y+obj.size >= world.H) && (obj.x-obj.size < 0)){
  114. theCircle(world.W,-world.H);
  115. }
  116. if((obj.x+obj.size >= world.W)&&(obj.y-obj.size < 0)){
  117. theCircle(-world.W,world.H);
  118. }
  119. if((obj.y-obj.size < 0)&& (obj.y-obj.size < 0)){
  120. theCircle(world.W,world.H);
  121. }
  122. });
  123. ctx.stroke();
  124. }
  125.  
  126.  
  127.  
  128. O.prototype.move = function(){
  129. this.shiftPos( this.xSpeed, this.ySpeed);
  130. var W = world.W;
  131. var H = world.H;
  132. if (this.x >= W){
  133. this.x -= W;
  134. }
  135. if (this.x < 0){
  136. this.x += W;
  137. }
  138. if (this.y >= H){
  139. this.y -= H;
  140. }
  141. if (this.y < 0){
  142. this.y += H;
  143. }
  144. this.collisionFieldAssociate();
  145. }
  146.  
  147. world.collionFieldsToCheck = [];
  148. O.prototype.collisionFieldAssociate = function(){
  149. var associatingObj = this;
  150. for(var myCollField in this.collisionFieldAssociations){
  151. this.collisionFieldAssociations[myCollField].remove(this);
  152. this.collisionFieldAssociations[myCollField] = null;
  153. delete this.collisionFieldAssociations[myCollField];
  154. };
  155. var cllFldSz = CollisionGrid.fieldSize;
  156. var hLaps = (this.size*2)/cllFldSz;
  157. var vLaps = (this.size*2)/cllFldSz;
  158. var x = this.x;
  159. var y = this.y;
  160. var cW = Math.floor(world.W/cllFldSz);
  161. var cH = Math.floor(world.H/cllFldSz);
  162. var size = this.size;
  163. var xOff = Math.floor( (x - size)/cllFldSz );
  164. if (xOff != Math.floor( (x + size)/cllFldSz) ){
  165. hLaps += 1;
  166. }
  167. var yOff = Math.floor( ( y - size)/cllFldSz );
  168. if (yOff != Math.floor(( y + size)/cllFldSz) ){
  169. vLaps += 1;
  170. }
  171. var collField,ixOff,jyOff;
  172. for(var j,i = 0; i < hLaps; i += 1){
  173. for( j = 0; j < vLaps; j += 1){
  174. ixOff = i+xOff;
  175. while (ixOff>=cW){
  176. ixOff -= cW;
  177. }
  178. while (ixOff<0){
  179. ixOff += cW;
  180. }
  181. jyOff = j+yOff;
  182. while (jyOff>=cH){
  183. jyOff -= cH;
  184. }
  185. while (jyOff<0){
  186. jyOff += cH;
  187. }
  188. collField = CollisionGrid.grid[ixOff][jyOff];
  189. collField.add(this);
  190. this.collisionFieldAssociations[collField.optKey]=collField;
  191. world.collionFieldsToCheck.push(collField);
  192. }
  193. }
  194. }
  195.  
  196. setInterval(function(){
  197. world.tick();
  198. render();
  199. },50);
  200. }
  201. Realm.prototype.runSelf=function(){
  202. console.log('Did it work?');
  203. }
  204. Realm.prototype.optKey = function(){
  205. this.optKeys+=1
  206. return this.optKeys;
  207. }
  208. Realm.prototype.tick = function(){
  209. this.objects.forEach(function(obj){
  210. obj.move();
  211. });
  212. var collisionsAccountedFor = [];
  213. var world = this;
  214. this.collionFieldsToCheck.forEach(function(collField){
  215. var keys = Object.keys( collField.objects );
  216. if( keys.length > 1 ){
  217. var obj1, obj2,accountingSig,XX,YY,lapDone;
  218. for(var d,j,i=0; i < keys.length; i+=1){
  219. for(j = i+1; j < keys.length; j+=1){
  220. obj1 = collField.objects[ keys[i] ];
  221. obj2 = collField.objects[ keys[j] ];
  222.  
  223. if(obj1.optKey<obj2.optKey){accountingSig = obj1.optKey+"_"+obj2.optKey; }else{accountingSig = obj2.optKey+"_"+obj1.optKey;}
  224. if(!collisionsAccountedFor[accountingSig]){
  225. for(XX=-1;XX<=1;XX+=1){
  226. lapDone = false;
  227. for(YY=-1;YY<=1;YY+=1){
  228. d = Math.sqrt( Math.pow(obj1.x - obj2.x + (XX*world.W),2) + Math.pow(obj1.y - obj2.y + (YY*world.H),2) );
  229. if( d <= (obj1.size+obj2.size) ){
  230. influence1 = obj2.size/obj1.size/10;
  231. influence2 = obj1.size/obj2.size/10;
  232. obj1.shiftXSpeed ( ( (obj1.x+ XX*world.W) -obj2.x )/d*(influence1) );
  233. obj1.shiftYSpeed ( ( (obj1.y+ YY*world.H) -obj2.y )/d*(influence1) );
  234. obj2.shiftXSpeed ( (obj2.x- (obj1.x + XX*world.W) )/d*(influence2) );
  235. obj2.shiftYSpeed ( (obj2.y- (obj1.y + YY*world.H) )/d*(influence2) );
  236. collisionsAccountedFor[accountingSig] = true;
  237. lapDone = true;
  238. break;
  239. }
  240. }
  241. if (lapDone) break;
  242. }
  243.  
  244. }
  245. }
  246. }
  247. }
  248. });
  249. //console.clear();
  250. this.collionFieldsToCheck = [];
  251. }
  252. Realm.prototype.manifest = function(obj,x,y){
  253. var o = new obj();
  254. o.setPos(x,y);
  255. o.optKey = this.optKey();
  256. this.objects.push(o);
  257. o.collisionFieldAssociate();
  258. return o;
  259. }
  260.  
  261.  
  262. var O = function(x,y){
  263. this.x = x;
  264. this.y = y;
  265.  
  266. this.size = $.pSz();
  267.  
  268. this.collisionFieldAssociations = [];
  269. this.optKey = null;
  270. this.direction = 0;
  271. this.speed = 0;
  272. this.xSpeed = 0;
  273. this.ySpeed = 0;
  274. }
  275. O.prototype.setPos = function(x,y){
  276. this.x = x;
  277. this.y = y;
  278. }
  279. O.prototype.shiftPos = function(x,y){
  280. this.x += x;
  281. this.y += y;
  282. }
  283. O.prototype.setDirection = function(direction){
  284. this.direction = direction;
  285. /*###TODO: recalc other vars*/
  286. }
  287. O.prototype.setSpeed = function(speed){
  288. this.speed = speed;
  289. /*###TODO: recalc other vars*/
  290. }
  291. O.prototype.setXSpeed = function(speed){
  292. this.xSpeed = speed;
  293. /*###TODO: recalc other vars*/
  294. }
  295. O.prototype.setYSpeed = function(speed){
  296. this.ySpeed = speed;
  297. /*###TODO: recalc other vars*/
  298. }
  299. O.prototype.shiftXSpeed = function(speed){
  300. this.xSpeed += speed;
  301. /*###TODO: recalc other vars*/
  302. }
  303. O.prototype.shiftYSpeed = function(speed){
  304. this.ySpeed += speed;
  305. /*###TODO: recalc other vars*/
  306. }
  307.  
  308. function chance(oneIn,func){
  309. if(getRandomInt(1,oneIn)==1){
  310. func();
  311. }
  312. }
  313. function getRandomInt(min, max){
  314. min = Math.ceil(min);
  315. max = Math.floor(max);
  316. return Math.floor(Math.random() * (max - min)) + min;
  317. }function grt(){return getRandomInt(-$.W/2,$.W/2)+$.W/2; }
  318.  
  319. (function(){
  320. var world = new Realm();
  321. var body = document.getElementsByTagName("body")[0];
  322. body.appendChild(world.canvasEl);
  323.  
  324. var currObj;
  325. var speed = $.kick;
  326. for(var i = 0; i < $.particles; i += 1){
  327. currObj = world.manifest(O,grt(),grt());
  328. currObj.setXSpeed( ( Math.random()-.5 ) * speed );
  329. currObj.setYSpeed( ( Math.random()-.5 ) * speed );
  330. }
  331.  
  332. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement