BObject.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import FSprite from "../object/FSprite";
  2. import CMath from "../../../util/CMath";
  3. import FMap from "../map/FMap";
  4. import { __SkillData } from "../../data/sdata/SManage";
  5. import BEffect from "./BEffect";
  6. /**
  7. * 子弹
  8. */
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class BObject extends cc.Component {
  12. @property({
  13. displayName: '子弹速度'
  14. })
  15. speed: number = 2000;//子弹速度
  16. @property({
  17. displayName: '子弹射程'
  18. })
  19. distance: number = 500;//子弹攻击距离
  20. @property({
  21. type: cc.Prefab,
  22. displayName: '施法特效'
  23. })
  24. mStartEffect: cc.Prefab = null;
  25. @property({
  26. type: cc.Prefab,
  27. displayName: '击中目标特效'
  28. })
  29. mHitEffect: cc.Prefab = null;
  30. @property({
  31. type: cc.Material,
  32. displayName: '击中目标材质'
  33. })
  34. mHitMaterial: cc.Material = null;
  35. @property({
  36. displayName: '材质颜色'
  37. })
  38. mHitColor: cc.Color = new cc.Color();
  39. private mRigidBody: cc.RigidBody;//刚体
  40. private map: FMap;
  41. /**
  42. * 子弹所属角色
  43. */
  44. public sprite: FSprite;
  45. /**
  46. * 子弹附带的技能
  47. */
  48. public _skillData: __SkillData
  49. /**
  50. * 子弹发射的位置
  51. */
  52. private origin: cc.Vec2;
  53. private img: cc.Node;
  54. private shadow: cc.Node;
  55. onLoad() {
  56. this.mRigidBody = this.node.getComponent(cc.RigidBody);
  57. this.img = this.node.children[0];
  58. if (this.node.children.length > 1) {
  59. this.shadow = this.node.children[1];
  60. }
  61. }
  62. public setSprite(sprite: FSprite) {
  63. this.sprite = sprite;
  64. this.map = sprite.map;
  65. }
  66. /**
  67. * 向目标发射
  68. * @param node
  69. */
  70. public fire(node: cc.Node) {
  71. let p1 = this.node.getPosition();
  72. let p2 = node.getPosition();
  73. let p3 = p2.clone();
  74. p3.y += node.height / 2;
  75. this.fireAB(p1, p3);
  76. }
  77. public firePos(p2: cc.Vec2) {
  78. let p1 = this.node.getPosition();
  79. this.fireAB(p1, p2);
  80. }
  81. /**
  82. * 向某方向发射
  83. * @param dir
  84. */
  85. public fireDir(dir) {
  86. this.fireAB(cc.Vec2.ZERO_R, dir);
  87. }
  88. /**
  89. * @param p1 坐标原点
  90. * @param p2 瞄准的坐标
  91. */
  92. public fireAB(p1, p2) {
  93. this.origin = this.node.getPosition();
  94. let angle = CMath.getAngle(p1, p2);
  95. this.img.angle = angle * 180 / Math.PI;
  96. let pos = cc.v2();
  97. pos.x = Math.cos(angle) * this.speed;
  98. pos.y = Math.sin(angle) * this.speed;
  99. this.mRigidBody.applyLinearImpulse(
  100. pos,
  101. this.mRigidBody.getWorldCenter(),
  102. true
  103. );
  104. }
  105. //弧度
  106. public fireAngle(angle) {
  107. this.origin = this.node.getPosition();
  108. this.img.angle = angle * 180 / Math.PI;
  109. let pos = cc.v2();
  110. pos.x = Math.cos(angle) * this.speed;
  111. pos.y = Math.sin(angle) * this.speed;
  112. this.mRigidBody.applyLinearImpulse(
  113. pos,
  114. this.mRigidBody.getWorldCenter(),
  115. true
  116. );
  117. }
  118. //弧度
  119. public fireAngle2(angle) {
  120. this.origin = this.node.getPosition();
  121. // this.node.angle = angle*180/Math.PI;
  122. let pos = cc.v2();
  123. pos.x = Math.cos(angle) * this.speed;
  124. pos.y = Math.sin(angle) * this.speed;
  125. this.mRigidBody.applyLinearImpulse(
  126. pos,
  127. this.mRigidBody.getWorldCenter(),
  128. true
  129. );
  130. }
  131. //角度
  132. public fireAngle1(angle) {
  133. this.origin = this.node.getPosition();
  134. this.img.angle = angle;
  135. let hd = angle * Math.PI / 180
  136. let pos = cc.v2();
  137. pos.x = Math.cos(hd) * this.speed;
  138. pos.y = Math.sin(hd) * this.speed;
  139. this.mRigidBody.applyLinearImpulse(
  140. pos,
  141. this.mRigidBody.getWorldCenter(),
  142. true
  143. );
  144. }
  145. public fireAngleV2(v2: cc.Vec2) {
  146. if (v2.x == 0 && v2.y == 0) {
  147. v2.x = 1;
  148. }
  149. this.origin = this.node.getPosition();
  150. let pos = cc.v2();
  151. pos.x = v2.x * this.speed;
  152. pos.y = v2.y * this.speed;
  153. this.img.angle = Math.atan2(v2.y, v2.x) * 180 / Math.PI;
  154. this.mRigidBody.applyLinearImpulse(
  155. pos,
  156. this.mRigidBody.getWorldCenter(),
  157. true
  158. );
  159. }
  160. update(dt) {
  161. if (this.sprite && this.sprite.isValid && this.sprite.hp > 0) {
  162. let dis = cc.Vec2.distance(this.origin, this.node.getPosition());
  163. if (dis >= this.distance) {
  164. this.node.destroy();
  165. }
  166. } else {
  167. this.node.destroy();
  168. }
  169. }
  170. public __destroy() {
  171. let node = cc.instantiate(this.mHitEffect);
  172. node.addComponent(BEffect)
  173. node.x = this.node.x;
  174. node.y = this.node.y;
  175. node.parent = this.map.node;
  176. this.node.destroy();
  177. }
  178. private isMaterial = false;
  179. public updateMaterial(sprite: FSprite) {
  180. if (this.isMaterial) {
  181. return
  182. }
  183. this.isMaterial = true;
  184. if (this.mHitMaterial) {
  185. sprite.spine.setMaterial(0, this.mHitMaterial)
  186. let _material = sprite.spine.getMaterial(0);
  187. _material.setProperty("u_rate", 1);
  188. _material.setProperty("u_color", this.mHitColor);
  189. sprite.spine.setMaterial(0, _material)
  190. let rate = 1;
  191. sprite.schedule(() => {
  192. rate -= 0.1
  193. if (rate <= 0.3) {
  194. rate = 0.3;
  195. _material.setProperty("u_rate", 1);
  196. sprite.spine.setMaterial(0, _material)
  197. this.isMaterial = false;
  198. } else {
  199. // cc.log('============= rate',rate);
  200. _material.setProperty("u_rate", rate);
  201. sprite.spine.setMaterial(0, _material)
  202. }
  203. }, 0.01, 8)
  204. }
  205. }
  206. smokeEffect(pos: cc.Vec3) {
  207. cc.loader.loadRes("icon/effect_sj/effect/effect_smoke.json", sp.SkeletonData, (err, res)=>{
  208. if(err){
  209. console.log("===load smokeEffect err====", err)
  210. return
  211. }
  212. let node = new cc.Node();
  213. node.name = "smokeEffect";
  214. node.parent = this.map.node;
  215. node.setPosition(pos);
  216. let spine = node.addComponent(sp.Skeleton);
  217. spine.skeletonData = res;
  218. spine.setAnimation(0, "animation", false);
  219. spine.setCompleteListener(()=>{
  220. node.destroy();
  221. })
  222. })
  223. }
  224. onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  225. if (self.node == this.node) {
  226. if (other.tag != 0) {
  227. if (other.tag == 103 || other.tag == 100) { // 箱子
  228. this.smokeEffect(self.node.position);
  229. this.node.destroy();
  230. }else if (other.tag == 104) {//树不用碰撞
  231. }
  232. } else if (other.node.group == 'map') {//撞到地图
  233. this.__destroy();
  234. } else {
  235. if (this.sprite && this.sprite.isValid && this.sprite.hp > 0 &&
  236. this.sprite.node.group != other.node.group) {
  237. if (self.isValid && other.node.isValid) {
  238. let otherNode = other.node as cc.Node;
  239. let target = otherNode.getComponent(FSprite);
  240. if (target.hp > 0) {
  241. if (target != null && target.isActive) {
  242. this.sprite.atkjs(target, this._skillData);
  243. this.__destroy();
  244. this.updateMaterial(target)
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }