BObject.ts 7.8 KB

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