football.vue 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. <template>
  2. <div class="game-container">
  3. <div id="gameCanvas" class="game-canvas"></div>
  4. <canvas ref="canvasRef" :width="clientObj.width" :height="clientObj.height"
  5. style="position:fixed;left: 0; top: 0; z-index: 999;"></canvas>
  6. <!-- 游戏启动界面 -->
  7. <div v-if="currentScene === 'start'" class="gamestart">
  8. <img v-if="currentScene === 'start'" src="/static/images/football/game_start.jpg" class="start_bg" />
  9. <div class="btn rule" @click="showRules = true">查看规则</div>
  10. <div class="btn start" @click="startGame">踢腿开始游戏</div>
  11. <!-- 规则弹窗 -->
  12. <div v-if="showRules" class="ruleshadow">
  13. <div class="rulebox">
  14. <span class="x_rulebox" @click="showRules = false"></span>
  15. <h3>操作方法:</h3>
  16. <p>手指拖动控制人物带球奔跑的方向和速度。</p>
  17. <h3>道具说明:</h3>
  18. <div class="daoju">
  19. <div class="daoju_item">
  20. <span class="daoju_1"></span>
  21. <p>每收集一个,生命值+1;触碰障碍物,生命可再复活。</p>
  22. </div>
  23. <div class="daoju_item">
  24. <span class="daoju_2"></span>
  25. <p>每收集一个,能力值+1;扫除面前一切障碍,加速前进。</p>
  26. </div>
  27. <div class="daoju_item">
  28. <span class="daoju_3"></span>
  29. <p>触碰锥桶,生命值-1;成功绕过一次分数+1。</p>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. <!-- 规则说明 -->
  36. <div v-if="currentScene === 'rule'" class="zhiyin">
  37. <div class="btn start" @click="continueGame">继续游戏</div>
  38. </div>
  39. <!-- 倒计时 -->
  40. <div v-if="currentScene === 'countdown'" class="daoju">
  41. <div class="daojishibox">
  42. <div class="daojishinum">{{ countdownNum }}</div>
  43. </div>
  44. </div>
  45. <!-- 游戏结束 -->
  46. <div v-if="currentScene === 'gameover'" class="gameend">
  47. <div v-if="currentScene === 'gameover'" class="game_fenshu">
  48. <span>{{ score }}</span>分
  49. </div>
  50. <div class="chenghao">恭喜你获得称号<p>{{ getTitle(score) }}</p>
  51. </div>
  52. <!-- <div class="game_ogain" @click="restartGame">返回游戏</div> -->
  53. </div>
  54. </div>
  55. </template>
  56. <script setup name="Football" lang="ts">
  57. import Phaser from 'phaser';
  58. import { onMounted, ref, reactive, onBeforeUnmount, watch } from 'vue';
  59. import { initSpeech, speckText, playMusic, controlMusic, speckCancel, chineseNumber } from '@/utils/speech';
  60. import { useWebSocket } from '@/utils/bodyposeWs';
  61. const { proxy } = getCurrentInstance() as any;
  62. const router = useRouter();
  63. const { bodyposeWs, startDevice, checkBodypose, openBodypose, terminateBodypose, suspendBodypose, resumeBodypose, getBodyposeState, closeWS } = useWebSocket();
  64. const canvasRef = ref(null);
  65. const data = reactive<any>({
  66. bodyposeData: {},//姿态信息
  67. bodyposeState: false,//姿态识别窗口状态
  68. parameter: {},//参数
  69. deviceInfo: {},//设备信息
  70. againNum: 0,//再次启动次数
  71. againTimer: null,//定时状态
  72. wsState: false,//WS状态
  73. clientObj: {},//浏览器对象
  74. boxes: [],//四个点坐标
  75. proportion: null,//人框和屏幕比例
  76. myThrow: 0,//0踢腿 1收腿
  77. myTimer: null,
  78. direction: null,//跑动
  79. });
  80. const { bodyposeData, bodyposeState, parameter, deviceInfo, againNum, againTimer, wsState, clientObj, boxes, proportion, myThrow, myTimer, direction } = toRefs(data);
  81. // 游戏状态管理
  82. const currentScene = ref('start');
  83. const showRules = ref(false);
  84. const countdownNum = ref(3);
  85. const score = ref(0);
  86. const game = ref(null);
  87. const gameConfig = ref(null);
  88. let scoreListener = null; // 存储事件监听器引用
  89. // 游戏常量
  90. const width = document.documentElement.clientWidth;
  91. const height = document.documentElement.clientHeight;
  92. const GAME_WIDTH = width;
  93. const GAME_HEIGHT = height;
  94. // 游戏资源
  95. const gameAssets = {
  96. images: [
  97. { key: 'gameStart', url: 'static/images/football/game_start.jpg' },
  98. { key: 'grass', url: 'static/images/football/Caopi.png' },
  99. { key: 'playerShoot', url: 'static/images/football/QiuyuanShooting.png' },
  100. { key: 'pile', url: 'static/images/football/Pile.png' },
  101. { key: 'jersey', url: 'static/images/football/Jersey.png' },
  102. { key: 'broom', url: 'static/images/football/Broom.png' },
  103. { key: 'goalkeeper', url: 'static/images/football/Goalkeeper.png' },
  104. { key: 'goal', url: 'static/images/football/Goal.png' },
  105. { key: 'ball', url: 'static/images/football/Ball.png' },
  106. { key: 'line', url: 'static/images/football/Line.png' },
  107. { key: 'line2', url: 'static/images/football/Line2.png' },
  108. { key: 'goalBackground', url: 'static/images/football/GoalBackGround.png' },
  109. { key: 'gameOver', url: 'static/images/football/gameover.png' }
  110. ],
  111. spritesheets: [
  112. {
  113. key: 'playerAnim',
  114. url: 'static/images/football/Qiuyuan.png',
  115. frameWidth: 92.5,
  116. frameHeight: 92.5
  117. },
  118. {
  119. key: 'ballAnim',
  120. url: 'static/images/football/Ball.png',
  121. frameWidth: 31,
  122. frameHeight: 31
  123. },
  124. {
  125. key: 'goalkeeperAnim',
  126. url: 'static/images/football/Goalkeeper.png',
  127. frameWidth: 55,
  128. frameHeight: 56
  129. },
  130. {
  131. key: 'playerCollision',
  132. url: 'static/images/football/QiuyuanCollision.png',
  133. frameWidth: 92.5,
  134. frameHeight: 92.5
  135. },
  136. {
  137. key: 'playerSuper',
  138. url: 'static/images/football/QiuyuanSuper.png',
  139. frameWidth: 92.5,
  140. frameHeight: 92.5
  141. },
  142. ]
  143. };
  144. // 初始化Phaser游戏
  145. const initGame = () => {
  146. // 游戏配置
  147. gameConfig.value = {
  148. type: Phaser.AUTO,
  149. width: GAME_WIDTH,
  150. height: GAME_HEIGHT,
  151. parent: 'gameCanvas',
  152. physics: {
  153. default: 'arcade',
  154. arcade: {
  155. gravity: { y: 0 },
  156. debug: false
  157. }
  158. },
  159. scene: [
  160. PreloaderScene,
  161. GameScene
  162. ]
  163. };
  164. game.value = new Phaser.Game(gameConfig.value);
  165. // 关键:使用局部变量存储事件监听器,便于后续清理
  166. scoreListener = (newScore) => {
  167. score.value = newScore;
  168. };
  169. // 添加事件监听
  170. if (game.value && game.value.events) {
  171. game.value.events.on('scoreChanged', scoreListener);
  172. }
  173. game.value.events.on('gameOver', () => {
  174. currentScene.value = 'gameover';
  175. setTimeout(() => {
  176. if (currentScene.value == 'gameover') {
  177. restartGame();
  178. }
  179. }, 2000)
  180. });
  181. };
  182. // 预加载场景
  183. class PreloaderScene extends Phaser.Scene {
  184. constructor() {
  185. super('PreloaderScene');
  186. }
  187. preload() {
  188. // 加载图片资源
  189. gameAssets.images.forEach(asset => {
  190. this.load.image(asset.key, asset.url);
  191. });
  192. // 加载精灵表
  193. gameAssets.spritesheets.forEach(asset => {
  194. this.load.spritesheet(asset.key, asset.url, {
  195. frameWidth: asset.frameWidth,
  196. frameHeight: asset.frameHeight
  197. });
  198. });
  199. // 显示加载进度
  200. const progressBar = this.add.graphics();
  201. const progressBox = this.add.graphics();
  202. progressBox.fillStyle(0x222222, 0.8);
  203. progressBox.fillRect(GAME_WIDTH / 2 - 160, GAME_HEIGHT / 2 - 25, 320, 50);
  204. const loadingText = this.make.text({
  205. x: GAME_WIDTH / 2,
  206. y: GAME_HEIGHT / 2 - 50,
  207. text: 'Loading...',
  208. style: {
  209. font: '20px monospace',
  210. fill: '#ffffff'
  211. }
  212. }).setOrigin(0.5, 0.5);
  213. const percentText = this.make.text({
  214. x: GAME_WIDTH / 2,
  215. y: GAME_HEIGHT / 2,
  216. text: '0%',
  217. style: {
  218. font: '18px monospace',
  219. fill: '#ffffff'
  220. }
  221. }).setOrigin(0.5, 0.5);
  222. this.load.on('progress', (value) => {
  223. percentText.setText(`${Math.round(value * 100)}%`);
  224. progressBar.clear();
  225. progressBar.fillStyle(0xffffff, 1);
  226. progressBar.fillRect(GAME_WIDTH / 2 - 150, GAME_HEIGHT / 2 - 15, 300 * value, 30);
  227. });
  228. this.load.on('complete', () => {
  229. progressBar.destroy();
  230. progressBox.destroy();
  231. loadingText.destroy();
  232. percentText.destroy();
  233. });
  234. }
  235. create() {
  236. // 初始化动画
  237. this.initAnimations();
  238. // 切换到游戏场景
  239. this.scene.start('GameScene');
  240. }
  241. initAnimations() {
  242. // 球员动画
  243. this.anims.create({
  244. key: 'playerLeft',
  245. frames: [
  246. { key: 'playerAnim', frame: 0 },
  247. { key: 'playerAnim', frame: 2 },
  248. { key: 'playerAnim', frame: 4 },
  249. { key: 'playerAnim', frame: 6 }
  250. ],
  251. frameRate: 10,
  252. repeat: -1
  253. });
  254. this.anims.create({
  255. key: 'playerRight',
  256. frames: [
  257. { key: 'playerAnim', frame: 1 },
  258. { key: 'playerAnim', frame: 3 },
  259. { key: 'playerAnim', frame: 5 },
  260. { key: 'playerAnim', frame: 7 }
  261. ],
  262. frameRate: 10,
  263. repeat: -1
  264. });
  265. // 足球动画
  266. this.anims.create({
  267. key: 'ballAnim',
  268. frames: this.anims.generateFrameNumbers('ballAnim', { start: 0, end: 1 }),
  269. frameRate: 5,
  270. repeat: -1
  271. });
  272. // 守门员动画
  273. this.anims.create({
  274. key: 'goalkeeperAnim',
  275. frames: this.anims.generateFrameNumbers('goalkeeperAnim', { start: 0, end: 1 }),
  276. frameRate: 3,
  277. repeat: -1
  278. });
  279. }
  280. }
  281. // 游戏主场景
  282. class GameScene extends Phaser.Scene {
  283. constructor() {
  284. super('GameScene');
  285. this.player = null;
  286. this.score = 0;
  287. this.lives = 3;
  288. this.maxLives = 5;
  289. this.speed = 6;
  290. this.acceleration = 1.9;
  291. this.level = 1;
  292. this.obstacles = [];
  293. this.powerUps = [];
  294. this.isSuper = false;
  295. this.gameActive = false;
  296. this.timer = null;
  297. this.isShooting = false; // 新增:初始化射门状态
  298. this.obstacleTimer = null; // 新增:初始化障碍物计时器
  299. this.obstacleEvent = null;
  300. this.jerseyEvent = null;
  301. this.broomEvent = null;
  302. this.shootTimeout = null; // 射门超时计时器
  303. this.shootTimeLeft = 15; // 剩余射门时间
  304. this.shootTimerText = null; // 倒计时显示文本
  305. }
  306. create() {
  307. // 创建背景
  308. this.createBackground();
  309. // 创建玩家
  310. this.createPlayer();
  311. // 创建计分板
  312. this.createHUD();
  313. // 输入控制
  314. this.initControls();
  315. // 游戏事件
  316. this.events.on('resume', () => {
  317. this.gameActive = true;
  318. });
  319. // 等待开始游戏信号
  320. this.gameActive = false;
  321. }
  322. createBackground() {
  323. // 创建滚动背景
  324. this.background = this.add.tileSprite(0, 0, GAME_WIDTH, GAME_HEIGHT, 'grass');
  325. this.background.setOrigin(0, 0);
  326. }
  327. createPlayer() {
  328. // 创建玩家
  329. //this.player = this.physics.add.sprite(GAME_WIDTH / 2, GAME_HEIGHT - 100, 'playerAnim');
  330. this.player = this.physics.add.sprite(direction.value + 46, GAME_HEIGHT - 100, 'playerAnim');
  331. this.player.setCollideWorldBounds(true);
  332. this.player.setScale(0.8);
  333. this.player.lives = this.lives;
  334. this.player.setDepth(9);
  335. }
  336. createHUD() {
  337. // 分数显示
  338. this.scoreText = this.add.text(10, 10, `分数: ${this.score}`, {
  339. fontSize: '16px',
  340. fill: '#ffffff',
  341. backgroundColor: 'rgba(0,0,0,0.5)',
  342. padding: { x: 5, y: 2 }
  343. });
  344. this.scoreText.setDepth(10);
  345. // 生命值显示
  346. this.livesText = this.add.text(GAME_WIDTH - 80, 10, `生命: ${this.lives}`, {
  347. fontSize: '16px',
  348. fill: '#ffffff',
  349. backgroundColor: 'rgba(0,0,0,0.5)',
  350. padding: { x: 5, y: 2 }
  351. });
  352. this.livesText.setDepth(10);
  353. }
  354. initControls() {
  355. // 键盘控制
  356. this.cursors = this.input.keyboard.createCursorKeys();
  357. // 触摸控制(添加垂直移动)
  358. this.input.on('pointermove', (pointer) => {
  359. if (this.gameActive && pointer.isDown && !this.isShooting) {
  360. // 水平移动
  361. this.player.x = Phaser.Math.Clamp(pointer.x, this.player.width / 2, GAME_WIDTH - this.player.width / 2);
  362. // 垂直移动(限制范围)
  363. this.player.y = Phaser.Math.Clamp(
  364. pointer.y,
  365. 0, // 最小Y值
  366. GAME_HEIGHT// 最大Y值
  367. );
  368. }
  369. });
  370. }
  371. startGame() {
  372. // 重置核心状态
  373. this.gameActive = true;
  374. this.score = 0;
  375. this.lives = 3; // 重置生命值
  376. this.maxLives = 5;
  377. this.speed = 6; // 重置基础速度
  378. this.acceleration = 1.9; // 重置加速度
  379. this.level = 1; // 重置等级
  380. this.isSuper = false;
  381. this.isShooting = false;
  382. this.obstacles = [];
  383. this.powerUps = [];
  384. // 更新UI显示
  385. this.livesText.setText(`生命: ${this.lives}`);
  386. this.updateScore();
  387. // 清理旧定时器(双重保险)
  388. if (this.timer) this.timer.remove();
  389. if (this.obstacleEvent) this.obstacleEvent.remove();
  390. if (this.jerseyEvent) this.jerseyEvent.remove();
  391. if (this.broomEvent) this.broomEvent.remove();
  392. // 重新开始生成障碍物和计时
  393. this.startSpawning();
  394. this.timer = this.time.addEvent({
  395. delay: 200,
  396. callback: () => {
  397. this.score++;
  398. this.updateScore();
  399. if (this.score % 200 === 0) {
  400. this.levelUp();
  401. }
  402. },
  403. loop: true
  404. });
  405. }
  406. startSpawning() {
  407. // 先移除旧事件(强化清除逻辑)
  408. if (this.obstacleEvent) {
  409. this.obstacleEvent.remove();
  410. this.obstacleEvent = null; // 置空引用
  411. }
  412. if (this.jerseyEvent) {
  413. this.jerseyEvent.remove();
  414. this.jerseyEvent = null; // 置空引用
  415. }
  416. if (this.broomEvent) {
  417. this.broomEvent.remove();
  418. this.broomEvent = null; // 置空引用
  419. }
  420. // 重新创建事件(使用安全的场景上下文)
  421. this.obstacleEvent = this.time.addEvent({
  422. delay: 1000 / this.level,
  423. callback: () => { if (this.gameActive) this.spawnObstacle(); },
  424. loop: true
  425. });
  426. this.jerseyEvent = this.time.addEvent({
  427. delay: 5000,
  428. callback: () => { if (this.gameActive) this.spawnPowerUp('jersey'); },
  429. loop: true
  430. });
  431. this.broomEvent = this.time.addEvent({
  432. delay: 8000,
  433. callback: () => { if (this.gameActive) this.spawnPowerUp('broom'); },
  434. loop: true
  435. });
  436. // 新增:立即生成第一个障碍物,确保倒计时结束后立即出现
  437. this.spawnObstacle();
  438. }
  439. spawnObstacle() {
  440. const x = Phaser.Math.Between(30, GAME_WIDTH - 30);
  441. const obstacle = this.physics.add.sprite(x, -50, 'pile');
  442. obstacle.setScale(0.7);
  443. // 降低速度(从原来的this.speed * this.acceleration * 10调整为)
  444. obstacle.setVelocityY(this.speed * this.acceleration * 5); // 速度减半
  445. obstacle.setDepth(8);
  446. obstacle.type = 'pile';
  447. // 碰撞检测
  448. this.physics.add.overlap(this.player, obstacle, this.handleObstacleCollision, null, this);
  449. this.obstacles.push(obstacle);
  450. // 替换原计时器代码
  451. const event = this.time.addEvent({ // 用变量接收事件引用
  452. delay: 100,
  453. loop: true,
  454. callback: () => {
  455. if (obstacle.active) {
  456. if (obstacle.y > GAME_HEIGHT + obstacle.height) {
  457. obstacle.destroy();
  458. this.obstacles = this.obstacles.filter(o => o !== obstacle);
  459. this.score++;
  460. this.updateScore();
  461. this.time.removeEvent(event); // 移除当前事件
  462. }
  463. } else {
  464. this.time.removeEvent(event);
  465. }
  466. }
  467. });
  468. }
  469. spawnPowerUp(type) {
  470. const x = Phaser.Math.Between(30, GAME_WIDTH - 30);
  471. let powerUp;
  472. if (type === 'jersey') {
  473. powerUp = this.physics.add.sprite(x, -50, 'jersey');
  474. powerUp.type = 'jersey';
  475. } else if (type === 'broom') {
  476. powerUp = this.physics.add.sprite(x, -50, 'broom');
  477. powerUp.type = 'broom';
  478. }
  479. powerUp.setScale(0.7);
  480. // 降低道具速度(从原来的this.speed * this.acceleration * 8调整为)
  481. powerUp.setVelocityY(this.speed * this.acceleration * 4); // 速度降低
  482. powerUp.setDepth(8);
  483. // 碰撞检测
  484. this.physics.add.overlap(this.player, powerUp, this.handlePowerUpCollision, null, this);
  485. this.powerUps.push(powerUp);
  486. // 延长道具生命周期
  487. this.time.addEvent({
  488. delay: 15000,
  489. callback: () => {
  490. if (powerUp.active) {
  491. powerUp.destroy();
  492. this.powerUps = this.powerUps.filter(p => p !== powerUp);
  493. }
  494. }
  495. });
  496. }
  497. handleObstacleCollision(player, obstacle) {
  498. if (this.isSuper) {
  499. // 超级状态下直接摧毁障碍物
  500. obstacle.destroy();
  501. this.obstacles = this.obstacles.filter(o => o !== obstacle);
  502. return;
  503. }
  504. // 扣除生命值
  505. player.lives--;
  506. this.lives = player.lives;
  507. this.livesText.setText(`生命: ${this.lives}`);
  508. // 显示受伤效果
  509. player.setTexture('playerCollision');
  510. this.time.addEvent({
  511. delay: 618,
  512. callback: () => {
  513. player.setTexture('playerAnim');
  514. }
  515. });
  516. // 销毁障碍物
  517. obstacle.destroy();
  518. this.obstacles = this.obstacles.filter(o => o !== obstacle);
  519. // 检查游戏是否结束
  520. if (player.lives <= 0) {
  521. this.gameOver();
  522. }
  523. }
  524. handlePowerUpCollision(player, powerUp) {
  525. if (powerUp.type === 'jersey') {
  526. // 增加生命值
  527. if (player.lives < this.maxLives) {
  528. player.lives++;
  529. this.lives = player.lives;
  530. this.livesText.setText(`生命: ${this.lives}`);
  531. }
  532. } else if (powerUp.type === 'broom') {
  533. // 激活超级状态
  534. this.activateSuperMode();
  535. }
  536. // 销毁道具
  537. powerUp.destroy();
  538. this.powerUps = this.powerUps.filter(p => p !== powerUp);
  539. }
  540. activateSuperMode() {
  541. this.isSuper = true;
  542. this.player.setTexture('playerSuper');
  543. this.acceleration = 10;
  544. // 3秒后结束超级状态
  545. this.time.addEvent({
  546. delay: 3000,
  547. callback: () => {
  548. this.isSuper = false;
  549. this.player.setTexture('playerAnim');
  550. this.acceleration = 1.9 + (this.level - 1) * 0.5;
  551. }
  552. });
  553. }
  554. levelUp() {
  555. this.level++;
  556. this.acceleration += 0.5;
  557. // 显示升级提示
  558. const levelUpText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, `Level ${this.level}!`, {
  559. fontSize: '32px',
  560. fill: '#ffff00',
  561. stroke: '#000000',
  562. strokeThickness: 2
  563. }).setOrigin(0.5);
  564. levelUpText.setDepth(10);
  565. this.time.addEvent({
  566. delay: 1000,
  567. callback: () => {
  568. levelUpText.destroy();
  569. }
  570. });
  571. }
  572. updateScore() {
  573. // 新增:检查游戏是否处于活跃状态
  574. if (!this.gameActive) return;
  575. try {
  576. if (!this.scoreText || !this.scene) return;
  577. this.scoreText.setText(`分数: ${this.score}`);
  578. if (this.game && this.game.events) {
  579. this.game.events.emit('scoreChanged', this.score);
  580. }
  581. if (this.score % 200 === 0 && this.score > 0 && !this.isShooting) {
  582. this.enterShootingMode();
  583. }
  584. } catch (error) {
  585. console.error('updateScore 错误:', error);
  586. }
  587. }
  588. enterShootingMode() {
  589. this.gameActive = false;
  590. this.isShooting = true;
  591. this.shootTimeLeft = 15; // 重置剩余时间
  592. // 暂停计分定时器
  593. if (this.timer) {
  594. this.timer.paused = true;
  595. }
  596. // 重置键盘状态
  597. this.input.keyboard.resetKeys();
  598. this.cursors.left.isDown = false;
  599. this.cursors.right.isDown = false;
  600. this.cursors.up.isDown = false;
  601. this.cursors.down.isDown = false;
  602. // 停止所有生成事件
  603. if (this.obstacleEvent) this.obstacleEvent.remove();
  604. if (this.jerseyEvent) this.jerseyEvent.remove();
  605. if (this.broomEvent) this.broomEvent.remove();
  606. // 清除现有障碍物和道具
  607. this.obstacles.forEach(obs => obs.destroy());
  608. this.obstacles = [];
  609. this.powerUps.forEach(p => p.destroy());
  610. this.powerUps = [];
  611. // 创建射门场景背景
  612. this.background.setTexture('goalBackground');
  613. // 创建球门
  614. this.goal = this.physics.add.sprite(GAME_WIDTH / 2, 100, 'goal');
  615. this.goal.setScale(0.8);
  616. this.goal.setImmovable(true);
  617. this.goal.setDepth(5);
  618. // 创建守门员
  619. this.goalkeeper = this.physics.add.sprite(GAME_WIDTH / 2, 150, 'goalkeeperAnim');
  620. this.goalkeeper.setScale(0.9);
  621. this.goalkeeper.setImmovable(true);
  622. this.goalkeeper.anims.play('goalkeeperAnim', true);
  623. this.goalkeeper.setDepth(7);
  624. // 让守门员左右移动
  625. this.tweens.add({
  626. targets: this.goalkeeper,
  627. x: [GAME_WIDTH / 2 - 50, GAME_WIDTH / 2 + 50],
  628. duration: 4000,
  629. ease: 'Sine.inOut',
  630. repeat: -1,
  631. yoyo: true
  632. });
  633. // 调整球员位置(准备射门)
  634. this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
  635. this.player.setTexture('playerShoot');
  636. this.player.setVelocity(0);
  637. // 创建足球
  638. this.ball = this.physics.add.sprite(this.player.x, this.player.y - 50, 'ballAnim');
  639. this.ball.setScale(0.8);
  640. this.ball.anims.play('ballAnim', true);
  641. this.ball.setDepth(8);
  642. // 显示射门提示和倒计时
  643. // this.shootHint = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 80, '点击或按空格键射门', {
  644. // fontSize: '16px',
  645. // fill: '#ffffff',
  646. // backgroundColor: 'rgba(0,0,0,0.5)',
  647. // padding: { x: 5, y: 2 }
  648. // }).setOrigin(0.5);
  649. // this.shootHint.setDepth(10);
  650. // 添加射门倒计时显示
  651. this.shootTimerText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 50, `剩余时间: ${this.shootTimeLeft}秒`, {
  652. fontSize: '16px',
  653. fill: '#ffffff',
  654. backgroundColor: 'rgba(0,0,0,0.5)',
  655. padding: { x: 5, y: 2 }
  656. }).setOrigin(0.5);
  657. this.shootTimerText.setDepth(10);
  658. // 设置射门超时计时器
  659. this.shootTimeout = this.time.addEvent({
  660. delay: 1000, // 每秒触发一次
  661. callback: () => {
  662. this.shootTimeLeft--;
  663. this.shootTimerText.setText(`剩余时间: ${this.shootTimeLeft}秒`);
  664. // 时间到未射门,判定失败
  665. if (this.shootTimeLeft <= 0) {
  666. this.handleShootTimeout();
  667. }
  668. },
  669. loop: true
  670. });
  671. // 射门控制
  672. this.input.keyboard.on('keydown-SPACE', this.shootBall, this);
  673. this.input.on('pointerdown', this.shootBall, this);
  674. }
  675. handleShootTimeout() {
  676. // 清除超时计时器
  677. if (this.shootTimeout) {
  678. this.shootTimeout.remove();
  679. this.shootTimeout = null;
  680. }
  681. // 禁用输入
  682. this.input.keyboard.off('keydown-SPACE', this.shootBall, this);
  683. this.input.off('pointerdown', this.shootBall, this);
  684. // 移除提示文本
  685. if (this.shootHint) {
  686. this.shootHint.destroy();
  687. this.shootHint = null;
  688. }
  689. if (this.shootTimerText) {
  690. this.shootTimerText.destroy();
  691. this.shootTimerText = null;
  692. }
  693. // 显示超时提示
  694. const timeoutText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '射门超时!', {
  695. fontSize: '24px',
  696. fill: '#f00',
  697. stroke: '#000000',
  698. strokeThickness: 2
  699. }).setOrigin(0.5);
  700. timeoutText.setDepth(10);
  701. // 2秒后返回奔跑场景
  702. this.time.addEvent({
  703. delay: 2000,
  704. callback: () => {
  705. timeoutText.destroy();
  706. this.ball.destroy();
  707. this.resetToRunningScene();
  708. // 恢复计分定时器
  709. if (this.timer) {
  710. this.timer.paused = false;
  711. }
  712. },
  713. callbackScope: this
  714. });
  715. }
  716. shootBall() {
  717. if (!this.isShooting) return;
  718. // 清除超时计时器
  719. if (this.shootTimeout) {
  720. this.shootTimeout.remove();
  721. this.shootTimeout = null;
  722. }
  723. // 移除时间显示文本
  724. if (this.shootTimerText) {
  725. this.shootTimerText.destroy();
  726. this.shootTimerText = null;
  727. }
  728. // 禁用输入
  729. this.input.keyboard.off('keydown-SPACE', this.shootBall, this);
  730. this.input.off('pointerdown', this.shootBall, this);
  731. this.input.keyboard.off('keydown-LEFT');
  732. this.input.keyboard.off('keydown-RIGHT');
  733. if (this.shootHint) {
  734. this.shootHint.destroy();
  735. this.shootHint = null;
  736. }
  737. // 计算龙门网内的目标位置
  738. const goalNetY = this.goal.y + 30;
  739. const goalNetX = this.player.x;
  740. // 创建射门动画
  741. const ballTween = this.tweens.add({
  742. targets: this.ball,
  743. x: goalNetX,
  744. y: goalNetY,
  745. duration: 1000,
  746. ease: 'Power1',
  747. onComplete: () => {
  748. if (!this.ball || !this.ball.active) return;
  749. // 判断是否成功
  750. const isSuccess = Phaser.Math.Between(0, 1) === 1;
  751. if (isSuccess) {
  752. this.score += 3;
  753. this.updateScore();
  754. this.ball.setDepth(6.5); // 成功:足球在守门员后方(网内)
  755. this.successShoot();
  756. } else {
  757. this.ball.setDepth(7.5); // 失败:足球在守门员前方
  758. this.failShoot();
  759. }
  760. ballTween.remove();
  761. }
  762. });
  763. }
  764. // 射门成功处理
  765. successShoot() {
  766. this.isShooting = false;
  767. // 显示成功提示
  768. const successText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '射门成功!+3分', {
  769. fontSize: '24px',
  770. fill: '#0f0',
  771. stroke: '#000000',
  772. strokeThickness: 2
  773. }).setOrigin(0.5);
  774. successText.setDepth(10);
  775. // 2秒后返回奔跑场景
  776. this.time.addEvent({
  777. delay: 2000,
  778. callback: () => {
  779. if (successText && successText.active) {
  780. successText.destroy();
  781. }
  782. this.ball.destroy();
  783. this.resetToRunningScene();
  784. // 恢复计分定时器
  785. if (this.timer) {
  786. this.timer.paused = false;
  787. }
  788. },
  789. callbackScope: this
  790. });
  791. }
  792. // 射门失败处理
  793. failShoot() {
  794. this.isShooting = false;
  795. // 关键修复1:解除足球的物理控制
  796. this.ball.setVelocity(0);
  797. this.ball.setImmovable(true);
  798. this.physics.world.disable(this.ball); // 完全禁用物理引擎对足球的控制
  799. // 关键修复2:调整足球位置到守门员前方固定位置,不跟随移动
  800. this.ball.setPosition(
  801. this.goalkeeper.x,
  802. this.goalkeeper.y + 20 // 固定在守门员前方20px处
  803. );
  804. // 显示失败提示
  805. const failText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT / 2, '射门失败!', {
  806. fontSize: '24px',
  807. fill: '#f00',
  808. stroke: '#000000',
  809. strokeThickness: 2
  810. }).setOrigin(0.5);
  811. failText.setDepth(10);
  812. // 2秒后返回奔跑场景
  813. this.time.addEvent({
  814. delay: 2000,
  815. callback: () => {
  816. if (failText && failText.active) {
  817. failText.destroy();
  818. }
  819. this.ball.destroy(); // 确保足球被销毁
  820. this.resetToRunningScene();
  821. // 恢复计分定时器
  822. if (this.timer) {
  823. this.timer.paused = false;
  824. }
  825. },
  826. callbackScope: this
  827. });
  828. }
  829. // 重置为奔跑场景
  830. resetToRunningScene() {
  831. // 确保清除超时计时器
  832. if (this.shootTimeout) {
  833. this.shootTimeout.remove();
  834. this.shootTimeout = null;
  835. }
  836. // 清除时间显示文本
  837. if (this.shootTimerText) {
  838. this.shootTimerText.destroy();
  839. this.shootTimerText = null;
  840. }
  841. this.clearScene();
  842. // 重置玩家状态
  843. if (this.player) {
  844. this.player.setTexture('playerAnim');
  845. this.player.setPosition(GAME_WIDTH / 2, GAME_HEIGHT - 100);
  846. this.player.setVelocity(0);
  847. }
  848. // 恢复背景和游戏状态
  849. if (this.background) this.background.setTexture('grass');
  850. this.gameActive = true;
  851. this.isShooting = false;
  852. // 确保足球被销毁
  853. if (this.ball) {
  854. this.ball.destroy();
  855. this.ball = null;
  856. }
  857. // 重新开始生成障碍物
  858. this.startSpawning();
  859. }
  860. gameOver() {
  861. this.gameActive = false;
  862. // 清理所有定时事件(增强版)
  863. // 1. 清除计分定时器
  864. if (this.timer) {
  865. this.timer.remove();
  866. this.timer.destroy(); // 彻底销毁计时器
  867. this.timer = null; // 置空引用
  868. }
  869. // 2. 清除障碍物生成计时器
  870. if (this.obstacleEvent) {
  871. this.obstacleEvent.remove();
  872. this.obstacleEvent.destroy();
  873. this.obstacleEvent = null;
  874. }
  875. // 3. 清除道具生成计时器
  876. if (this.jerseyEvent) {
  877. this.jerseyEvent.remove();
  878. this.jerseyEvent.destroy();
  879. this.jerseyEvent = null;
  880. }
  881. if (this.broomEvent) {
  882. this.broomEvent.remove();
  883. this.broomEvent.destroy();
  884. this.broomEvent = null;
  885. }
  886. // 4. 清除所有可能残留的计时器
  887. this.time.removeAllEvents();
  888. // 5. 停止所有动画和物理运动
  889. this.physics.pause();
  890. // 显示游戏结束画面
  891. this.add.image(GAME_WIDTH / 2, GAME_HEIGHT / 2, 'gameOver').setOrigin(0.5);
  892. // 触发游戏结束事件
  893. this.time.addEvent({
  894. delay: 2000,
  895. callback: () => {
  896. this.game.events.emit('gameOver');
  897. },
  898. loop: false // 确保这是一次性事件
  899. });
  900. }
  901. update() {
  902. if (!this.gameActive) return;
  903. // 关键修复:射门模式下直接返回,不执行任何移动逻辑
  904. if (this.isShooting) return;
  905. // 玩家移动逻辑(原有代码保持不变)
  906. if (this.player && this.player.active) {
  907. if (this.cursors.left.isDown) {
  908. //this.player.setVelocityX(-200);
  909. this.player.setX(direction.value + 46);
  910. if (this.player.anims.currentAnim?.key !== 'playerLeft' && !this.player.anims.paused) {
  911. this.player.anims.play('playerLeft', true);
  912. }
  913. } else if (this.cursors.right.isDown) {
  914. //this.player.setVelocityX(200);
  915. this.player.setX(direction.value + 46);
  916. if (this.player.anims.currentAnim?.key !== 'playerRight' && !this.player.anims.paused) {
  917. this.player.anims.play('playerRight', true);
  918. }
  919. } else if (this.cursors.up.isDown) {
  920. this.player.setVelocityY(-200);
  921. if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
  922. this.player.anims.play('playerLeft', true);
  923. } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
  924. this.player.anims.play('playerRight', true);
  925. } else {
  926. this.player.anims.play('playerLeft', true);
  927. }
  928. } else if (this.cursors.down.isDown) {
  929. this.player.setVelocityY(200);
  930. if (this.player.anims.currentAnim?.key === 'playerLeft' && !this.player.anims.paused) {
  931. this.player.anims.play('playerLeft', true);
  932. } else if (this.player.anims.currentAnim?.key === 'playerRight' && !this.player.anims.paused) {
  933. this.player.anims.play('playerRight', true);
  934. } else {
  935. this.player.anims.play('playerLeft', true);
  936. }
  937. } else {
  938. this.player.setVelocityX(0);
  939. this.player.setVelocityY(0);
  940. this.player.anims.stop();
  941. }
  942. }
  943. }
  944. // 添加 clearScene 方法
  945. clearScene() {
  946. // 清理射门场景的元素
  947. if (this.goal) this.goal.destroy();
  948. if (this.goalkeeper) this.goalkeeper.destroy();
  949. if (this.ball) {
  950. this.ball.destroy();
  951. this.ball = null; // 显式置空
  952. }
  953. if (this.shootHint) this.shootHint.destroy();
  954. // 清理所有障碍物和道具
  955. this.obstacles.forEach(obs => obs.destroy());
  956. this.powerUps.forEach(p => p.destroy());
  957. this.obstacles = [];
  958. this.powerUps = [];
  959. // 停止所有动画
  960. this.tweens.killAll();
  961. }
  962. // 新增:场景销毁时清理资源
  963. destroy() {
  964. // 清理所有定时器
  965. if (this.timer) this.timer.remove();
  966. if (this.obstacleEvent) this.obstacleEvent.remove();
  967. if (this.jerseyEvent) this.jerseyEvent.remove();
  968. if (this.broomEvent) this.broomEvent.remove();
  969. // 移除输入监听
  970. this.input.keyboard.off('keydown-SPACE', this.shootBall, this);
  971. this.input.off('pointerdown', this.shootBall, this);
  972. // 清理所有游戏对象
  973. this.obstacles.forEach(obs => obs.destroy());
  974. this.powerUps.forEach(p => p.destroy());
  975. if (this.goal) this.goal.destroy();
  976. if (this.goalkeeper) this.goalkeeper.destroy();
  977. if (this.ball) this.ball.destroy();
  978. // 调用父类销毁方法
  979. super.destroy();
  980. }
  981. }
  982. // 游戏控制函数
  983. const startGame = () => {
  984. currentScene.value = 'countdown';
  985. // 倒计时
  986. currentScene.value = 'countdown';
  987. let count = 3;
  988. const countdownInterval = setInterval(() => {
  989. count--;
  990. countdownNum.value = count;
  991. if (count <= 0) {
  992. clearInterval(countdownInterval);
  993. currentScene.value = 'game';
  994. // 通知游戏开始
  995. if (game.value) {
  996. const gameScene = game.value.scene.getScene('GameScene');
  997. if (gameScene) {
  998. gameScene.startGame();
  999. }
  1000. }
  1001. }
  1002. }, 1000);
  1003. };
  1004. const continueGame = () => {
  1005. startGame();
  1006. };
  1007. const restartGame = () => {
  1008. // 1. 彻底销毁旧游戏实例
  1009. if (game.value) {
  1010. // 获取当前场景并停止所有活动
  1011. const gameScene = game.value.scene.getScene('GameScene');
  1012. if (gameScene) {
  1013. gameScene.gameActive = false;
  1014. gameScene.time.removeAllEvents(); // 清除所有事件
  1015. gameScene.physics.pause(); // 暂停物理引擎
  1016. }
  1017. // 销毁游戏实例
  1018. game.value.destroy(true);
  1019. game.value = null;
  1020. }
  1021. // 2. 重置全局状态
  1022. score.value = 0;
  1023. currentScene.value = 'start';
  1024. countdownNum.value = 3;
  1025. // 3. 初始化新游戏
  1026. setTimeout(() => {
  1027. initGame();
  1028. }, 100);
  1029. };
  1030. const getTitle = (score) => {
  1031. if (score < 50) return '足球新手';
  1032. if (score < 100) return '业余球员';
  1033. if (score < 200) return '专业选手';
  1034. if (score < 300) return '足球明星';
  1035. return '足球传奇';
  1036. };
  1037. // 组件挂载时初始化游戏
  1038. onMounted(() => {
  1039. initGame();
  1040. });
  1041. // 监听场景变化
  1042. watch(currentScene, (newVal) => {
  1043. if (newVal === 'game' && game.value) {
  1044. const gameScene = game.value.scene.getScene('GameScene');
  1045. if (gameScene) {
  1046. gameScene.scene.resume();
  1047. }
  1048. }
  1049. });
  1050. /**
  1051. * 监听投篮
  1052. */
  1053. watch(
  1054. () => myThrow.value,
  1055. (newData, oldData) => {
  1056. if (oldData == undefined || myTimer.value) {
  1057. return false;
  1058. }
  1059. console.log("ppp", oldData, newData)
  1060. if (newData == 1) {
  1061. console.log("收腿准备")
  1062. } else {
  1063. if (currentScene.value === 'start') {
  1064. startGame();
  1065. }
  1066. const gameScene = game.value.scene.getScene('GameScene');
  1067. gameScene.shootBall();
  1068. console.log("踢出去了")
  1069. //加个时间以免踢出去然后收腿也算了
  1070. myTimer.value = setTimeout(() => {
  1071. clearTimeout(myTimer.value);
  1072. myTimer.value = null;
  1073. }, 500)
  1074. }
  1075. },
  1076. { immediate: true }
  1077. );
  1078. /**
  1079. * 监听跑动
  1080. */
  1081. watch(
  1082. () => direction.value,
  1083. (newData, oldData) => {
  1084. nextTick(() => {
  1085. if (!game.value) {
  1086. return false;
  1087. }
  1088. const gameScene = game.value.scene.getScene('GameScene');
  1089. if (newData > oldData) {
  1090. gameScene.cursors.left.isDown = false;
  1091. gameScene.cursors.right.isDown = true;
  1092. } else {
  1093. gameScene.cursors.left.isDown = true;
  1094. gameScene.cursors.right.isDown = false;
  1095. }
  1096. });
  1097. },
  1098. { immediate: true }
  1099. );
  1100. /**
  1101. * 初始化
  1102. */
  1103. const getInit = async () => {
  1104. console.log("触发姿态识别")
  1105. let deviceid = localStorage.getItem('deviceid') || '';
  1106. if (!deviceid) {
  1107. proxy?.$modal.msgError(`请重新登录绑定设备号后使用`);
  1108. return false;
  1109. }
  1110. bodyposeState.value = true;
  1111. if (wsState.value) {
  1112. proxy?.$modal.msgWarning(`操作过快,请稍后重试`);
  1113. setTimeout(() => {
  1114. bodyposeState.value = false;
  1115. }, 1000)
  1116. return false;
  1117. }
  1118. speckText("正在姿态识别");
  1119. proxy?.$modal.msgWarning(`正在姿态识别`);
  1120. bodyposeWs((e: any) => {
  1121. //console.log("bodyposeWS", e)
  1122. if (e?.wksid) {
  1123. wsState.value = true;
  1124. //获取设备信息
  1125. startDevice({ deviceid: deviceid });
  1126. console.log("获取设备信息")
  1127. }
  1128. if (e?.type == 'fe_device_init_result') {
  1129. //接收设备信息并发送请求
  1130. if (e?.device_info) {
  1131. deviceInfo.value = e.device_info;
  1132. getCheckBodypose();
  1133. console.log("返回设备信息,检查是否支持姿态识别")
  1134. } else {
  1135. proxy?.$modal.msgError(`设备信息缺失,请重新登录绑定设备号后使用`);
  1136. }
  1137. }
  1138. if (e?.cmd == 'check_bodyposecontroller_available') {
  1139. let handcontroller_id = deviceInfo.value.handcontroller_id;
  1140. if (e?.code == 0) {
  1141. //查看姿态识别状态,如果不处于关闭就先关闭再重新启动(可能会APP退出然后工作站还在运行的可能性)
  1142. getBodyposeState(handcontroller_id);
  1143. againNum.value = 0;
  1144. againTimer.value = null;
  1145. clearTimeout(againTimer.value);
  1146. console.log("查看姿态识别状态")
  1147. } else {
  1148. //尝试多次查询姿态识别状态
  1149. if (againNum.value <= 2) {
  1150. againTimer.value = setTimeout(() => {
  1151. getCheckBodypose();
  1152. }, 500)
  1153. againNum.value++;
  1154. } else {
  1155. let msg = "";
  1156. if (e.code == 102402) {
  1157. msg = `多次连接失败请重试,姿态识别模块被占用`;
  1158. } else {
  1159. msg = `多次连接失败请重试,姿态识别模块不可用,code:${e.code}`;
  1160. }
  1161. proxy?.$modal.msgWarning(msg);
  1162. againNum.value = 0;
  1163. againTimer.value = null;
  1164. clearTimeout(againTimer.value);
  1165. getCloseBodypose();//直接关闭
  1166. }
  1167. }
  1168. }
  1169. if (e?.cmd == 'get_bodyposecontroller_state') {
  1170. let handcontroller_id = deviceInfo.value.handcontroller_id;
  1171. //state说明: 0:关闭,3:空闲,36:工作中
  1172. if ([3, 36].includes(e.state)) {
  1173. getCloseBodypose();
  1174. proxy?.$modal.msgWarning(`请重新姿态识别`);
  1175. } else {
  1176. openBodypose(handcontroller_id);
  1177. }
  1178. }
  1179. if (e?.type == 'bodyposecontroller_result') {
  1180. let arr = e.data.result.keypoints;
  1181. let result = [];
  1182. for (let i = 0; i < arr.length; i += 3) {
  1183. result.push(arr.slice(i, i + 2));
  1184. }
  1185. //console.log("result", result)
  1186. bodyposeData.value = result;
  1187. if (boxes.value.length == 0) {
  1188. speckText("识别成功");
  1189. proxy?.$modal.msgWarning(`识别成功`);
  1190. let arr = e.data.result.boxes;
  1191. boxes.value = [{ x: arr[0], y: arr[3] }, { x: arr[0], y: arr[1] }, { x: arr[2], y: arr[1] }, { x: arr[2], y: arr[3] }]
  1192. proportion.value = (clientObj.value.height / (arr[3] - arr[1])).toFixed(2);
  1193. }
  1194. getCanvas();
  1195. }
  1196. if (e?.cmd == 'terminate_bodyposecontroller') {
  1197. if (e?.code == 0) {
  1198. closeWS();
  1199. bodyposeState.value = false;
  1200. }
  1201. }
  1202. if (e?.type == 'disconnect') {
  1203. wsState.value = false;
  1204. }
  1205. });
  1206. };
  1207. /**
  1208. * 查询姿态识别状态
  1209. */
  1210. const getCheckBodypose = () => {
  1211. let handcontroller_id = deviceInfo.value.handcontroller_id;
  1212. //检查是否支持姿态识别
  1213. checkBodypose(handcontroller_id);
  1214. };
  1215. /**
  1216. * 关闭姿态识别
  1217. */
  1218. const getCloseBodypose = () => {
  1219. let handcontroller_id = deviceInfo.value.handcontroller_id;
  1220. terminateBodypose(handcontroller_id);
  1221. bodyposeState.value = false;
  1222. speckCancel(); //停止播报
  1223. setTimeout(() => {
  1224. if (wsState.value) {
  1225. closeWS();
  1226. }
  1227. }, 3000)
  1228. };
  1229. const getCanvas = () => {
  1230. let leftA = { x: bodyposeData.value[12][0], y: bodyposeData.value[12][1] };//大腿
  1231. let leftB = { x: bodyposeData.value[14][0], y: bodyposeData.value[14][1] };//膝盖
  1232. let leftC = { x: bodyposeData.value[16][0], y: bodyposeData.value[16][1] };//脚
  1233. let rightA = { x: bodyposeData.value[11][0], y: bodyposeData.value[11][1] };//大腿
  1234. let rightB = { x: bodyposeData.value[13][0], y: bodyposeData.value[13][1] };//膝盖
  1235. let rightC = { x: bodyposeData.value[15][0], y: bodyposeData.value[15][1] };//脚
  1236. let jiaodu1 = calculateAngleAtB(leftA, leftB, leftC)
  1237. let jiaodu2 = calculateAngleAtB(rightA, rightB, rightC)
  1238. // console.log("jiaodu1",jiaodu1)
  1239. // console.log("jiaodu2",jiaodu2)
  1240. if (jiaodu1 <= 80 && jiaodu2 >= 120 || jiaodu2 <= 80 && jiaodu2 >= 120) {
  1241. myThrow.value = 1;
  1242. } else {
  1243. myThrow.value = 0;
  1244. }
  1245. const canvas: any = canvasRef.value;
  1246. const ctx = canvas.getContext('2d');
  1247. // 清空整个画布
  1248. ctx.clearRect(0, 0, canvas.width, canvas.height);
  1249. // 保存当前状态
  1250. ctx.save();
  1251. function calculateOffset(a: any, b: any) {
  1252. return {
  1253. x: b.x - a.x,
  1254. y: b.y - a.y
  1255. };
  1256. }
  1257. const pointA = { x: clientObj.value.width / 2, y: clientObj.value.height / 2 };
  1258. const pointB = { x: (boxes.value[2].x + boxes.value[0].x) / 2, y: (boxes.value[3].y + boxes.value[1].y) / 2 };
  1259. const offset = calculateOffset(pointA, pointB);
  1260. ctx.translate(-offset.x, -offset.y);
  1261. // console.log("Canvas分辨率", clientObj.value);
  1262. // console.log("人体图片四点坐标", boxes.value)
  1263. // console.log("Canvas中心", pointA);
  1264. // console.log("人体中心", pointB);
  1265. // console.log("offset", offset)
  1266. // console.log("proportion.value",proportion.value)
  1267. const originalPoints = bodyposeData.value;
  1268. // 计算缩放后坐标
  1269. const postData = originalPoints.map((point: any) => {
  1270. const newX = (point[0] - pointB.x) * proportion.value + pointB.x;
  1271. const newY = (point[1] - pointB.y) * proportion.value + pointB.y;
  1272. return [newX, newY];
  1273. });
  1274. // console.log("原始坐标:", originalPoints);
  1275. // console.log("缩放后坐标:", postData);
  1276. direction.value = postData[0][0] - offset.x - (94 / 2);//鼻子X
  1277. //绘制头部
  1278. const point1 = { x: postData[4][0], y: postData[4][1] };
  1279. const point2 = { x: postData[3][0], y: postData[3][1] };
  1280. // 计算椭圆参数
  1281. const centerX = (point1.x + point2.x) / 2; // 椭圆中心X
  1282. const centerY = (point1.y + point2.y) / 2; // 椭圆中心Y
  1283. const distance = Math.sqrt(
  1284. Math.pow(point2.x - point1.x, 2) +
  1285. Math.pow(point2.y - point1.y, 2)
  1286. ); // 两个焦点之间的距离
  1287. const radiusX = distance * 0.5; // 水平半径(可调整)
  1288. const radiusY = distance * 0.6; // 垂直半径(可调整)
  1289. // 1. 绘制填充椭圆
  1290. ctx.beginPath();
  1291. ctx.ellipse(centerX, centerY, radiusX, radiusY, 0, 0, Math.PI * 2);
  1292. ctx.fillStyle = 'red'; // 填充颜色
  1293. ctx.fill(); // 填充
  1294. // 2. 绘制边框
  1295. ctx.strokeStyle = 'red';
  1296. ctx.lineWidth = 5;
  1297. ctx.stroke();
  1298. // 绘制每个点
  1299. postData.forEach((point: any, index: number) => {
  1300. //眼睛鼻子不显示
  1301. if (![0, 1, 2].includes(index)) {
  1302. const [x, y] = point;
  1303. ctx.beginPath();
  1304. ctx.arc(x, y, 5, 0, Math.PI * 2); // 绘制半径为5的圆点
  1305. ctx.fillStyle = 'red';
  1306. ctx.fill();
  1307. ctx.lineWidth = 1;
  1308. ctx.stroke();
  1309. }
  1310. });
  1311. // 根据点关系连线
  1312. const arr = [[10, 8], [8, 6], [6, 5], [5, 7], [7, 9], [6, 12], [5, 11], [12, 11], [12, 14], [14, 16], [11, 13], [13, 15]]
  1313. arr.forEach((point: any) => {
  1314. let index1 = point[0];
  1315. let index2 = point[1];
  1316. //连线
  1317. const dian1 = { x: postData[index1][0], y: postData[index1][1] };
  1318. const dian2 = { x: postData[index2][0], y: postData[index2][1] };
  1319. // 绘制连线
  1320. ctx.beginPath();
  1321. ctx.moveTo(dian1.x, dian1.y); // 起点
  1322. ctx.lineTo(dian2.x, dian2.y); // 终点
  1323. ctx.strokeStyle = 'red'; // 线条颜色
  1324. ctx.lineWidth = 3; // 线条宽度
  1325. ctx.stroke(); // 描边
  1326. });
  1327. ctx.restore(); // 恢复状态
  1328. };
  1329. /**
  1330. * 计算B点的夹角度数(∠ABC)
  1331. * @param {Object} pointA - A点坐标 {x, y, z可选}
  1332. * @param {Object} pointB - B点坐标 {x, y, z可选}
  1333. * @param {Object} pointC - C点坐标 {x, y, z可选}
  1334. * @returns {number} B点的夹角度数(保留两位小数)
  1335. */
  1336. function calculateAngleAtB(pointA, pointB, pointC) {
  1337. // 计算向量BA和向量BC
  1338. const vectorBA = {
  1339. x: pointA.x - pointB.x,
  1340. y: pointA.y - pointB.y,
  1341. z: (pointA.z || 0) - (pointB.z || 0)
  1342. };
  1343. const vectorBC = {
  1344. x: pointC.x - pointB.x,
  1345. y: pointC.y - pointB.y,
  1346. z: (pointC.z || 0) - (pointB.z || 0)
  1347. };
  1348. // 计算点积
  1349. const dotProduct =
  1350. vectorBA.x * vectorBC.x +
  1351. vectorBA.y * vectorBC.y +
  1352. vectorBA.z * vectorBC.z;
  1353. // 计算向量BA的模长
  1354. const lengthBA = Math.sqrt(
  1355. vectorBA.x ** 2 +
  1356. vectorBA.y ** 2 +
  1357. vectorBA.z ** 2
  1358. );
  1359. // 计算向量BC的模长
  1360. const lengthBC = Math.sqrt(
  1361. vectorBC.x ** 2 +
  1362. vectorBC.y ** 2 +
  1363. vectorBC.z ** 2
  1364. );
  1365. // 防止除以零的情况
  1366. if (lengthBA === 0 || lengthBC === 0) {
  1367. throw new Error("点A、B、C不能重合");
  1368. }
  1369. // 计算余弦值
  1370. const cosine = dotProduct / (lengthBA * lengthBC);
  1371. // 由于计算误差可能导致cosine略超出[-1, 1]范围,需要修正
  1372. const clampedCosine = Math.max(Math.min(cosine, 1), -1);
  1373. // 计算弧度并转换为角度
  1374. const angleRadians = Math.acos(clampedCosine);
  1375. const angleDegrees = angleRadians * (180 / Math.PI);
  1376. // 保留两位小数并返回
  1377. return parseFloat(angleDegrees.toFixed(2));
  1378. }
  1379. onBeforeMount(() => {
  1380. clientObj.value = {
  1381. width: document.documentElement.clientWidth,
  1382. height: document.documentElement.clientHeight,
  1383. }
  1384. getInit();
  1385. });
  1386. // 组件卸载时清理事件监听
  1387. onUnmounted(() => {
  1388. if (game.value && game.value.events && scoreListener) {
  1389. game.value.events.off('scoreChanged', scoreListener);
  1390. }
  1391. });
  1392. </script>
  1393. <style scoped>
  1394. .game-container {
  1395. position: relative;
  1396. width: 100%;
  1397. height: 100vh;
  1398. margin: 0 auto;
  1399. overflow: hidden;
  1400. }
  1401. .game-canvas {
  1402. width: 100%;
  1403. height: 100%;
  1404. background-color: #000;
  1405. }
  1406. .gamestart {
  1407. position: absolute;
  1408. top: 0;
  1409. left: 0;
  1410. width: 100%;
  1411. height: 100%;
  1412. z-index: 100;
  1413. }
  1414. .start_bg {
  1415. width: 100%;
  1416. height: 100%;
  1417. object-fit: cover;
  1418. }
  1419. .btn {
  1420. width: 80%;
  1421. height: 40px;
  1422. border-radius: 5px;
  1423. text-align: center;
  1424. line-height: 40px;
  1425. color: #333;
  1426. box-shadow: 0 5px 0px #07942c;
  1427. box-shadow: 0 5px 0px rgba(0, 0, 0, 0.17);
  1428. position: absolute;
  1429. left: 50%;
  1430. margin: 0 0 0 -40%;
  1431. font-size: 18px;
  1432. }
  1433. .rule {
  1434. background: #fff;
  1435. bottom: 100px;
  1436. }
  1437. .start {
  1438. background: #ffff00;
  1439. bottom: 40px;
  1440. }
  1441. .ruleshadow {
  1442. position: absolute;
  1443. top: 0;
  1444. left: 0;
  1445. width: 100%;
  1446. height: 100%;
  1447. background-color: rgba(0, 0, 0, 0.7);
  1448. z-index: 102;
  1449. display: flex;
  1450. justify-content: center;
  1451. align-items: center;
  1452. font-size: 18px;
  1453. }
  1454. .rulebox {
  1455. width: 280px;
  1456. background-color: white;
  1457. border-radius: 10px;
  1458. padding: 20px;
  1459. position: relative;
  1460. opacity: 0;
  1461. animation: fadeIn 0.5s forwards;
  1462. }
  1463. @keyframes fadeIn {
  1464. to {
  1465. opacity: 1;
  1466. }
  1467. }
  1468. .x_rulebox {
  1469. position: absolute;
  1470. top: 10px;
  1471. right: 10px;
  1472. width: 20px;
  1473. height: 20px;
  1474. background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23000'%3E%3Cpath d='M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'/%3E%3C/svg%3E");
  1475. cursor: pointer;
  1476. }
  1477. .daoju {
  1478. margin-top: 15px;
  1479. }
  1480. .daoju_item {
  1481. display: flex;
  1482. align-items: center;
  1483. margin-bottom: 10px;
  1484. }
  1485. .daoju_1,
  1486. .daoju_2,
  1487. .daoju_3 {
  1488. display: inline-block;
  1489. width: 60px;
  1490. height: 60px;
  1491. margin-right: 10px;
  1492. background-size: contain;
  1493. background-repeat: no-repeat;
  1494. flex-shrink: 0;
  1495. }
  1496. .daoju_1 {
  1497. background-image: url("/static/images/football/Jersey.png");
  1498. }
  1499. .daoju_2 {
  1500. background-image: url("/static/images/football/Broom.png");
  1501. }
  1502. .daoju_3 {
  1503. background-image: url("/static/images/football/Pile.png");
  1504. }
  1505. .zhiyin {
  1506. position: absolute;
  1507. top: 0;
  1508. left: 0;
  1509. width: 100%;
  1510. height: 100%;
  1511. background-color: rgba(0, 0, 0, 0.7);
  1512. z-index: 100;
  1513. display: flex;
  1514. justify-content: center;
  1515. align-items: center;
  1516. }
  1517. .daojishi {
  1518. position: absolute;
  1519. width: 100%;
  1520. top: 0;
  1521. bottom: 0;
  1522. display: none;
  1523. z-index: 11;
  1524. background: url(daojishu_bg.png) repeat left top;
  1525. background-size: 100%;
  1526. }
  1527. .daojishiline {
  1528. width: 100%;
  1529. height: 95px;
  1530. position: absolute;
  1531. top: 109px;
  1532. left: 0;
  1533. background: #ffe400;
  1534. z-index: 1;
  1535. }
  1536. .daojishibox {
  1537. width: 180px;
  1538. height: 180px;
  1539. position: absolute;
  1540. left: 50%;
  1541. top: 71px;
  1542. margin: 0 0 0 -90px;
  1543. background: url(daojishibox.png) no-repeat left top;
  1544. background-size: 100%;
  1545. z-index: 2;
  1546. }
  1547. .daojishipangzi {
  1548. width: 111px;
  1549. height: 102px;
  1550. position: absolute;
  1551. left: 50%;
  1552. bottom: 50px;
  1553. margin: 0 0 0 -55px;
  1554. background: url(daojishipangzi.png) no-repeat left top;
  1555. background-size: 100%;
  1556. z-index: -1;
  1557. }
  1558. .daojishinum {
  1559. width: 100%;
  1560. text-align: center;
  1561. font-size: 80px;
  1562. color: #FFFFFF;
  1563. padding: 70px 0 0 0;
  1564. line-height: normal;
  1565. }
  1566. @keyframes countDown {
  1567. 0% {
  1568. transform: scale(3);
  1569. opacity: 0;
  1570. }
  1571. 50% {
  1572. transform: scale(1.2);
  1573. opacity: 1;
  1574. }
  1575. 100% {
  1576. transform: scale(1);
  1577. opacity: 1;
  1578. }
  1579. }
  1580. .gameend {
  1581. position: absolute;
  1582. top: 0;
  1583. left: 0;
  1584. width: 100%;
  1585. height: 100%;
  1586. background-color: rgba(0, 0, 0, 0.7);
  1587. z-index: 100;
  1588. display: flex;
  1589. flex-direction: column;
  1590. justify-content: center;
  1591. align-items: center;
  1592. color: white;
  1593. }
  1594. .game_fenshu {
  1595. font-size: 36px;
  1596. margin-bottom: 20px;
  1597. }
  1598. .chenghao {
  1599. font-size: 24px;
  1600. margin-bottom: 30px;
  1601. text-align: center;
  1602. }
  1603. .game_ogain,
  1604. .game_chakan,
  1605. .game_share {
  1606. width: 150px;
  1607. height: 40px;
  1608. line-height: 40px;
  1609. text-align: center;
  1610. background-color: #f00;
  1611. border-radius: 20px;
  1612. margin-bottom: 10px;
  1613. cursor: pointer;
  1614. font-size: 18px;
  1615. }
  1616. .game_chakan {
  1617. background-color: #666;
  1618. }
  1619. .game_share {
  1620. background-color: #008000;
  1621. }
  1622. </style>