Board logo

標題: Vector+Sprite [打印本頁]

作者: ray    時間: 2013-1-12 10:09     標題: Vector+Sprite

  1. //Class Vector begin
  2. //類別宣告
  3. function Vector(inc)
  4. {       
  5.         if(inc == 0)
  6.         {
  7.                 inc = 100;
  8.         }
  9.        
  10.         //屬性宣告,定義
  11.         this.data = new Array(inc);
  12.         this.increment = inc;
  13.         this.size = 0;
  14.        
  15.         //方法宣告
  16.         this.getCapacity = Vector_getCapacity;
  17.         this.getSize = getSize;
  18.         this.isEmpty = Vector_isEmpty;
  19.         this.getLastElement = getLastElement;
  20.         this.getFirstElement = getFirstElement;
  21.         this.getElementAt = Vector_getElementAt;
  22.         this.addElement = addElement;
  23.         this.insertElementAt = insertElementAt;
  24.         this.removeElementAt = removeElementAt;
  25.         this.removeAllElements = removeAllElements;
  26.         this.indexOf = indexOf;
  27.         this.contains = contains
  28.         this.resize = resize;
  29.         this.toString = toString;
  30.         this.sort = sort;
  31.         this.trimToSize = trimToSize;
  32.         this.clone = clone;
  33.         this.overwriteElementAt;
  34. }

  35. // getCapacity() -- returns the number of elements the vector can hold
  36. function Vector_getCapacity()
  37. {
  38.         return this.data.length;
  39. }

  40. // getSize() -- returns the current size of the vector
  41. function getSize()
  42. {
  43.         return this.size;
  44. }

  45. // isEmpty() -- checks to see if the Vector has any elements
  46. function Vector_isEmpty()
  47. {
  48.         return this.getSize() == 0;
  49. }

  50. // getLastElement() -- returns the last element
  51. function getLastElement()
  52. {
  53.         if (this.data[this.getSize() - 1] != null)
  54.         {
  55.                 return this.data[this.getSize() - 1];
  56.         }
  57. }

  58. // getFirstElement() -- returns the first element
  59. function getFirstElement()
  60. {
  61.         if (this.data[0] != null)
  62.         {
  63.                 return this.data[0];
  64.         }
  65. }

  66. // getElementAt() -- returns an element at a specified index
  67. function Vector_getElementAt(i)
  68. {
  69.         try
  70.         {
  71.                 return this.data[i];
  72.         }
  73.         catch (e)
  74.         {
  75.                 return "Exception " + e + " occured when accessing " + i;       
  76.         }       
  77. }

  78. // addElement() -- adds a element at the end of the Vector
  79. function addElement(obj)
  80. {
  81.         if(this.getSize() == this.data.length)
  82.         {
  83.                 this.resize();
  84.         }
  85.         this.data[this.size++] = obj;
  86. }

  87. // insertElementAt() -- inserts an element at a given position
  88. function insertElementAt(obj, index)
  89. {
  90.         try
  91.         {
  92.                 if (this.size == this.capacity)
  93.                 {
  94.                         this.resize();
  95.                 }
  96.                
  97.                 for (var i=this.getSize(); i > index; i--)
  98.                 {
  99.                         this.data[i] = this.data[i-1];
  100.                 }
  101.                 this.data[index] = obj;
  102.                 this.size++;
  103.         }
  104.         catch (e)
  105.         {
  106.                 return "Invalid index " + i;
  107.         }
  108. }

  109. // removeElementAt() -- removes an element at a specific index
  110. function removeElementAt(index)
  111. {
  112.         try
  113.         {
  114.                 var element = this.data[index];
  115.                
  116.                 for(var i=index; i<(this.getSize()-1); i++)
  117.                 {
  118.                         this.data[i] = this.data[i+1];
  119.                 }
  120.                
  121.                 this.data[getSize()-1] = null;
  122.                 this.size--;
  123.                 return element;
  124.         }
  125.         catch(e)
  126.         {
  127.                 return "Invalid index " + index;
  128.         }
  129. }

  130. // removeAllElements() -- removes all elements in the Vector
  131. function removeAllElements()
  132. {
  133.         this.size = 0;
  134.        
  135.         for (var i=0; i<this.data.length; i++)
  136.         {
  137.                 this.data[i] = null;
  138.         }
  139. }

  140. // indexOf() -- returns the index of a searched element
  141. function indexOf(obj)
  142. {
  143.         for (var i=0; i<this.getSize(); i++)
  144.         {
  145.                 if (this.data[i] == obj)
  146.                 {
  147.                         return i;
  148.                 }
  149.         }
  150.         return -1;
  151. }

  152. // contains() -- returns true if the element is in the Vector, otherwise false
  153. function contains(obj)
  154. {
  155.         for (var i=0; i<this.getSize(); i++)
  156.         {
  157.                 if (this.data[i] == obj)
  158.                 {
  159.                         return true;
  160.                 }
  161.         }
  162.         return false;
  163. }

  164. // resize() -- increases the size of the Vector
  165. function resize()
  166. {
  167.         newData = new Array(this.data.length + this.increment);
  168.        
  169.         for        (var i=0; i< this.data.length; i++)
  170.         {
  171.                 newData[i] = this.data[i];
  172.         }
  173.        
  174.         this.data = newData;
  175. }


  176. // trimToSize() -- trims the vector down to it's size
  177. function trimToSize()
  178. {
  179.         var temp = new Array(this.getSize());
  180.        
  181.         for (var i = 0; i < this.getSize(); i++)
  182.         {
  183.                 temp[i] = this.data[i];
  184.         }
  185.         this.size = temp.length - 1;
  186.         this.data = temp;
  187. }

  188. // sort() - sorts the collection based on a field name - f
  189. function sort(f)
  190. {
  191.         var i, j;
  192.         var currentValue;
  193.         var currentObj;
  194.         var compareObj;
  195.         var compareValue;
  196.        
  197.         for(i=1; i<this.getSize();i++)
  198.         {
  199.                 currentObj = this.data[i];
  200.                 currentValue = currentObj[f];
  201.                
  202.                 j= i-1;
  203.                 compareObj = this.data[j];
  204.                 compareValue = compareObj[f];
  205.                
  206.                 while(j >=0 && compareValue > currentValue)
  207.                 {
  208.                         this.data[j+1] = this.data[j];
  209.                         j--;
  210.                         if (j >=0)
  211.                         {
  212.                                 compareObj = this.data[j];
  213.                                 compareValue = compareObj[f];
  214.                         }                               
  215.                 }       
  216.                 this.data[j+1] = currentObj;
  217.         }
  218. }

  219. // clone() -- copies the contents of a Vector to another Vector returning the new Vector.
  220. function clone()
  221. {
  222.         var newVector = new Vector(this.size);
  223.        
  224.         for (var i=0; i<this.size; i++)
  225.         {
  226.                 newVector.addElement(this.data[i]);
  227.         }
  228.        
  229.         return newVector;
  230. }

  231. // toString() -- returns a string rep. of the Vector
  232. function toString()
  233. {
  234.         var str = "Vector Object properties:\n" +
  235.                   "Increment: " + this.increment + "\n" +
  236.                   "Size: " + this.size + "\n" +
  237.                   "Elements:\n";
  238.        
  239.         for (var i=0; i<getSize(); i++)
  240.         {
  241.                 for (var prop in this.data[i])
  242.                 {
  243.                         var obj = this.data[i];
  244.                         str += "\tObject." + prop + " = " + obj[prop] + "\n";
  245.                 }
  246.         }
  247.         return str;       
  248. }

  249. // overwriteElementAt() - overwrites the element with an object at the specific index.
  250. function overwriteElementAt(obj, index)
  251. {
  252.         this.data[index] = obj;
  253. }
  254. //Class Vector end

  255. //Class Sprite begin
  256. //類別宣告
  257. function Sprite(name,pic,w,h,x,y,sw,sh)
  258. {
  259.         //屬性宣告,定義
  260.         this.name = name;
  261.         this.w = w;
  262.         this.h = h;
  263.         this.sw = sw;
  264.         this.sh = sh;
  265.         this.x = x;
  266.         this.y = y;
  267.         this.speed = 1;
  268.        
  269.         //方法宣告
  270.     this.show = Sprite_show;
  271.     this.move = Sprite_move;
  272.     this.shift = Sprite_shift;
  273.     this.setPic = Sprite_setPic;
  274.     this.collision = SPrite_collision;
  275.   
  276.           //建構子程序
  277.           this.pic = new Image();
  278.         this.pic.src = pic;
  279. }
  280. //方法定義

  281. function Sprite_show(idx,idy)
  282. {
  283.         ctx.drawImage(this.pic,idx*this.w,idy*this.h,this.w,this.h,this.x,this.y,this.sw,this.sh);
  284. }

  285. function Sprite_move(x,y)
  286. {
  287.         this.x = x;
  288.         this.y = y;
  289. }

  290. function Sprite_shift(dx,dy)
  291. {
  292.         this.move(this.x+dx*this.speed,this.y+dy*this.speed);
  293. }

  294. function Sprite_setPic(newPic)
  295. {
  296.         this.pic.src = newPic;
  297. }

  298. function SPrite_collision(obj)
  299. {
  300.         var rate = 0.5;
  301.         if(this.x+(this.sw*rate) > obj.x && this.x < obj.x+(obj.sw*rate) && this.y+(this.sh*rate) > obj.y && this.y < obj.y+(obj.sh*rate))
  302.                 return true;
  303.         else
  304.                 return false;
  305. }
  306. //Class Sprite end
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2