源战役客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

199 行
4.3 KiB

  1. CoVector2 = CoVector2 or BaseClass()
  2. local CoVector2 = CoVector2
  3. function CoVector2:__init(x, y)
  4. self.x = x
  5. self.y = y
  6. -- local now_time = Status.NowTime
  7. -- if CoVector2.cache_time == nil or CoVector2.cache_time < now_time then
  8. -- -- if CoVector2.call_num == 1 then
  9. -- print("创建Vector2个数", CoVector2.call_num, now_time)
  10. -- -- print(xxxxx+3)
  11. -- -- end
  12. -- CoVector2.call_num = 0
  13. -- CoVector2.cache_time = now_time
  14. -- end
  15. -- CoVector2.call_num = CoVector2.call_num + 1
  16. -- if CoVector2.call_num >1 then
  17. -- -- print("创建Vector2个数", CoVector2.call_num, now_time)
  18. -- print(xxxxx+3)
  19. -- end
  20. local meta_table = getmetatable(self)
  21. meta_table.__add = function (v1, v2)
  22. return CoVector2.New(v1.x + v2.x, v1.y + v2.y)
  23. end
  24. meta_table.__sub = function (v1, v2)
  25. return CoVector2.New(v1.x - v2.x, v1.y - v2.y)
  26. end
  27. meta_table.__mul = function (v1, v2)
  28. local f = tonumber(v2)
  29. if f ~= nil then
  30. return CoVector2.New(v1.x * f, v1.y * f)
  31. else
  32. -- print("~+~+~+ co.Vector2 相乘2", v1.x, v1.y, v2.x, v2.y)
  33. return CoVector2.New(v1.x * v2.x, v1.y * v2.y)
  34. end
  35. end
  36. meta_table.__div = function (v1, v2)
  37. local f = tonumber(v2)
  38. if f ~= nil then
  39. return CoVector2.New(v1.x / f, v1.y / f)
  40. else
  41. return CoVector2.New(v1.x / v2.x, v1.y / v2.y)
  42. end
  43. end
  44. meta_table.__tostring = function (v1)
  45. return string.format("co.Vector2(%.4f, %.4f)", v1.x, v1.y)
  46. end
  47. end
  48. function CoVector2:__defineVar()
  49. return {
  50. _class_type = self,
  51. --_cid = self._id,
  52. _iid = _in_obj_ins_id,
  53. x = 0,
  54. y = 0
  55. }
  56. end
  57. function CoVector2:__delete()
  58. -- body
  59. end
  60. function CoVector2:copyVector2(v)
  61. self.x, self.y = v.x, v.y
  62. end
  63. function CoVector2:setXY(x, y)
  64. self.x, self.y = x, y
  65. end
  66. function CoVector2:Mul(x, y)
  67. y = y or x
  68. if x and y then
  69. self.x, self.y = self.x * x, self.y * y
  70. end
  71. end
  72. function CoVector2:length()
  73. return math.sqrt(self.x * self.x + self.y * self.y)
  74. end
  75. function CoVector2:squaredLength()
  76. return self.x * self.x + self.y * self.y
  77. end
  78. --把对象存进缓存队列
  79. function CoVector2:DeleteV()
  80. local free_num = co.free_vector2_num
  81. if co.free_vector2_num < 100 then
  82. co.free_vector2_num = free_num + 1
  83. co.free_vector2_list[free_num+1] = self
  84. end
  85. end
  86. function CoVector2:normalise()
  87. local l = math.sqrt(self.x * self.x + self.y * self.y)
  88. if l >= 1e-6 then
  89. local f = 1 / l
  90. self.x = self.x * f
  91. self.y = self.y * f
  92. end
  93. return l
  94. end
  95. function CoVector2:distance(v2)
  96. local dx = self.x - v2.x
  97. local dy = self.y - v2.y
  98. return math.sqrt(dx * dx + dy * dy)
  99. end
  100. function CoVector2:squaredDistance(v2)
  101. local dx = self.x - v2.x
  102. local dy = self.y - v2.y
  103. return dx * dx + dy * dy
  104. end
  105. function CoVector2:dotProduct(v2)
  106. return self.x * v2.x + self.y * v2.y
  107. end
  108. function CoVector2:crossProduct(v2)
  109. return self.x * v2.y - self.y * v2.x
  110. end
  111. function CoVector2:angleBetween(v2)
  112. local lenProduct = self:length() * v2:length()
  113. if lenProduct < 1e-6 then
  114. lenProduct = 1e-6
  115. end
  116. local f = self:dotProduct(v2) / lenProduct
  117. if f < -1 then
  118. f = -1
  119. elseif f > 1 then
  120. f = 1
  121. end
  122. return math.acos(f)
  123. end
  124. --返回值顺时针方向为正角度
  125. function CoVector2:angleTo(v2)
  126. local angle = self:angleBetween(v2)
  127. if self:crossProduct(v2) < 0 then
  128. angle = math.pi * 2 - angle
  129. end
  130. return angle
  131. end
  132. function CoVector2:toGameVector2()
  133. if self.game_vector2 == nil then
  134. self.game_vector2 = Game.Vector2(self.x, self.y)
  135. else
  136. self.game_vector2.x = self.x
  137. self.game_vector2.y = self.y
  138. end
  139. return self.game_vector2
  140. end
  141. --沿着dir方向移动dist距离
  142. function CoVector2:Move(dir, dist)
  143. self.x = self.x + dir.x * dist
  144. self.y = self.y + dir.y * dist
  145. end
  146. --把V1向量按照“X轴正方向转到V2方向”的方式旋转
  147. function CoVector2:rotateFromXAxisTo(v2, proj_modify)
  148. local r1 = co.XAix:angleTo(self)
  149. local r2 = co.XAix:angleTo(v2)
  150. local r = r1 + r2
  151. local l = self:length()
  152. local cos_r = math.cos(r)
  153. local sin_r = math.sin(r)
  154. --在转到90度的时候要缩短一些,以产生透视效果
  155. local p = proj_modify and (cos_r * 0.2929 + 0.7071) or 1
  156. self.x, self.y = cos_r * l, sin_r * l * p
  157. return self
  158. end
  159. --radian为正数时顺时针旋转
  160. function CoVector2:rotate(radian )
  161. local r = co.XAix:angleTo(self)
  162. r = r + radian
  163. local l = self:length()
  164. local cos_r = math.cos(r)
  165. local sin_r = math.sin(r)
  166. self.x, self.y = cos_r * l, sin_r * l
  167. return self
  168. end