12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import Two from "two.js";
- import { scale, desginCageRadius, pedestalOffsetX, pedestalOffsetY, cageNum } from './design'
- import bulletImage from '@/assets/RoundaboutImage/2-13.png';
-
- // 子弹的核心逻辑
- export default ({two, center, speed, getGameState}) => {
- const _event = new Two.Events()
-
- // 子弹个数 = 轿厢个数
- const num = cageNum;
- // 子弹宽度 = 轿厢宽度
- const bulletRadius = desginCageRadius;
-
- // 子弹其实位置与底座是一致的
- const x = center.x + pedestalOffsetX * scale;
- const y = center.y + pedestalOffsetY * scale;
-
- // 弹夹
- let clip = [];
- // 在膛子弹
- let current = null;
-
- // 绘制子弹
- function drawBullets () {
- const bulletTexture = two.makeTexture(bulletImage);
-
- const list = Array(num).fill().map((_, inx) => {
- const arc = two.makeCircle(x, y, bulletRadius)
- arc.id = `bullet-${inx}`
- // arc.fill = 'black'
- arc.fill = bulletTexture
- arc.noStroke()
- arc.scale = scale;
- arc.visible = false; // 默认不显示, 即不上膛
-
- return arc
- })
-
- return list;
- }
-
- // 子弹上膛
- function bulletLoad() {
- current = clip.shift()
- if (current) {
- current.visible = true
- }
- }
-
- // 子弹飞行
- function fly() {
- if (!current) return;
- const { top, height } = current.getBoundingClientRect()
- current.position = new Two.Vector(current.position.x, current.position.y + speed)
-
- // 子弹飞行事件
- _event.dispatchEvent('shooting', {
- bullet: current,
- cancel: () => {
- two.removeEventListener('update', fly);
- _event.dispatchEvent('afterShoot');
- },
- })
- }
-
- // 射击
- function shoot() {
- if (getGameState() !== 1) return;
-
- _event.dispatchEvent('beforeShoot')
- two.addEventListener('update', fly)
- }
-
- // 弹夹
- clip = drawBullets()
-
- return {
- on: _event.on.bind(_event),
- bulletLoad,
- shoot,
- }
- }
|