homeLand.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <view class="home-land-container">
  3. <!-- 第三层:背景 -->
  4. <view class="background-layer"></view>
  5. <!-- 第二层:地图 -->
  6. <view class="map-layer" id="mapLayer" :style="{ transform: `translateX(${translateX}px)` }" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @mousedown="onmousedown" @mousemove="onmousemove" @mouseup="onmouseup">
  7. <image class="island-image" src="/static/island/island.png" mode="widthFix" style="width:1536rpx; bottom: 0rpx;left: 0rpx; position: absolute;"></image>
  8. <view style="position: absolute;width: 670rpx;left: 340rpx; bottom:320rpx;align-items: center;">
  9. <image class="house-image" src="/static/island/building/home1.png" mode="widthFix" style="width:670rpx; position: static;" @click="onHouseClick" :animation="houseAnimationData"> </image>
  10. </view>
  11. <view style="position: absolute;width: 300rpx;left:280rpx; bottom:200rpx;align-items: center;">
  12. <image class="table-image" src="/static/island/building/workTable.png" mode="widthFix" style="width:670rpx; position: static;" @click="onTableClick" :animation="tableAnimationData"> </image>
  13. </view>
  14. <view style="position: absolute;width:200rpx;left:660rpx; bottom:260rpx;align-items: center;">
  15. <image class="mailBox-image" src="/static/island/building/mailBox.png" mode="widthFix" style="width:670rpx; position: static;" > </image>
  16. </view>
  17. <view style="position: absolute;width:200rpx;left:1000rpx; bottom:450rpx;align-items: center;">
  18. <image class="taskBoard-image" src="/static/island/building/taskBoard.png" mode="widthFix" style="width:670rpx; position: static;" @click="showTask" :animation="taskAnimationData"> </image>
  19. </view>
  20. <!-- 主岛箭头 -->
  21. <view class="main-arrow" @click="goToMainLand" :animation="mainArrowAnimation" :style="{ opacity: mainArrowVisible ? 1 : 0 }">
  22. <!-- <image src="/static/island/main_arrow.png" mode="widthFix" style="width: 100rpx;"></image> -->
  23. <view class="arrow" ></view>
  24. <text class="main-text">主岛</text>
  25. </view>
  26. </view>
  27. <!-- 第一层:UI -->
  28. <view class="ui-layer">
  29. <view class="ui-content">
  30. <!-- 添加货币计数器 -->
  31. <view class="currency-display" >
  32. <view class="currency-item">
  33. <image src="/static/island/UI/wd_icon_coin.png" mode="widthFix" class="currency-icon"></image>
  34. <text class="currency-value">{{userInfo.num_gmd}}</text>
  35. </view>
  36. <view class="currency-item">
  37. <image src="/static/island/UI/wd_icon_xingyuan.png" mode="widthFix" class="currency-icon"></image>
  38. <text class="currency-value">{{userInfo.num_gmg}}</text>
  39. </view>
  40. </view>
  41. <view class="ui-buttons">
  42. <image src="/static/island/icon_backpack.png" mode="widthFix" style="width:100rpx" @click="showInventory"></image>
  43. </view>
  44. </view>
  45. </view>
  46. <!-- 对话框组件 -->
  47. <backpack-dialog :visible.sync="inventoryVisible" @close="onInventoryClose"></backpack-dialog>
  48. <character-dialog :visible.sync="characterVisible" @close="onCharacterClose"></character-dialog>
  49. <shop-dialog :visible.sync="shopVisible" @close="onShopClose" @buy="onShopBuy"></shop-dialog>
  50. <task-dialog class="task-dialog" :visible.sync="taskVisible" @close="onTaskClose" type="main"></task-dialog>
  51. <!-- 引导对话组件 -->
  52. <talk-guide
  53. :guide-data="guideData"
  54. :player-name="playerName"
  55. @guide-complete="onGuideComplete"
  56. ></talk-guide>
  57. <!-- 制造台对话框组件 -->
  58. <crafting-dialog :visible.sync="craftingVisible" @close="craftingVisible = false"></crafting-dialog>
  59. <!-- 引导管理器 -->
  60. <guide-manager ref="guideManager"></guide-manager>
  61. </view>
  62. </template>
  63. <script>
  64. import BackpackDialog from '@/components/dialogs/BackpackDialog.vue'
  65. import CharacterDialog from '@/components/dialogs/CharacterDialog.vue'
  66. import ShopDialog from '@/components/dialogs/ShopDialog.vue'
  67. import TaskDialog from './TaskDialog.vue'
  68. import TalkGuide from './talkGuide.vue'
  69. import CraftingDialog from '@/components/dialogs/CraftingDialog.vue'
  70. import GuideManager from '@/components/guide/GuideManager.vue'
  71. export default {
  72. components: {
  73. BackpackDialog,
  74. CharacterDialog,
  75. ShopDialog,
  76. TaskDialog,
  77. TalkGuide,
  78. CraftingDialog,
  79. GuideManager
  80. },
  81. data() {
  82. return {
  83. // 背景位置控制
  84. translateX: -141,
  85. startX: 0,
  86. currentX: 0,
  87. isDragging : false,
  88. // 获取屏幕宽度和背景宽度
  89. screenWidth: 0,
  90. backgroundWidth: 0,
  91. islandHeight: 0,
  92. houseAnimationData: {},
  93. tableAnimationData: {},
  94. taskAnimationData: {},
  95. houseAnimating: false,
  96. tableAnimating: false,
  97. taskAnimating: false,
  98. baseTranslate: { x: -50, y: 50 },
  99. inventoryVisible: false,
  100. characterVisible: false,
  101. shopVisible: false,
  102. mainArrowAnimation: {},
  103. mainArrowAnimating: false,
  104. mainArrowVisible: false,
  105. taskVisible: false,
  106. playerName: '梦幻',
  107. guideData: [
  108. {
  109. characterImage: '/static/island/npc.png',
  110. characterName: '罗宾',
  111. text: '这位大人,这里太大了吧....',
  112. position: 'left',
  113. isMirror: true
  114. },
  115. {
  116. characterImage: '/static/island/building/1.png',
  117. characterName: '我',
  118. text: '好的,我马上上会教付清',
  119. position: 'right',
  120. isMirror: false
  121. },
  122. {
  123. characterImage: '/static/island/building/1.png',
  124. characterName: '我',
  125. text: '很奇怪不是吗?',
  126. position: 'right',
  127. isMirror: false
  128. },
  129. {
  130. characterImage: '/static/island/npc.png',
  131. characterName: '罗宾',
  132. text: '怎么回事',
  133. position: 'left',
  134. isMirror: true
  135. }
  136. ],
  137. craftingVisible: false,
  138. guideManager: null,
  139. userInfo: {
  140. num_gmd: 0,
  141. num_gmg: 0,
  142. },
  143. moneyTimer: null, // 添加定时器变量
  144. }
  145. },
  146. onLoad() {
  147. let self = this;
  148. // 获取屏幕宽度
  149. uni.getSystemInfo({
  150. success: (res) => {
  151. self.screenWidth = res.windowWidth;
  152. console.log('屏幕宽度:', self.screenWidth);
  153. }
  154. });
  155. this.getUserMoney();
  156. // 启动定时器,每2秒更新一次铃钱
  157. this.moneyTimer = setInterval(() => {
  158. this.getUserMoney();
  159. }, 2000);
  160. },
  161. onShow() {
  162. // 检查是否需要显示引导
  163. this.checkAndShowGuide();
  164. },
  165. onReady() {
  166. // 在组件渲染完成后获取图片尺寸
  167. setTimeout(() => {
  168. this.getImageSize();
  169. // 延迟1秒后显示箭头并开始动画
  170. setTimeout(() => {
  171. this.mainArrowVisible = true;
  172. this.startMainArrowAnimation();
  173. }, 1000);
  174. }, 300);
  175. },
  176. onUnload() {
  177. // 清除定时器
  178. if (this.moneyTimer) {
  179. clearInterval(this.moneyTimer);
  180. this.moneyTimer = null;
  181. }
  182. },
  183. methods: {
  184. loadData() {
  185. // 可以在这里加载其他数据
  186. },
  187. getImageSize() {
  188. const query = uni.createSelectorQuery().in(this);
  189. query.select('.island-image').boundingClientRect(data => {
  190. if (data) {
  191. // 获取岛屿图片的宽度和高度
  192. this.backgroundWidth = data.width;
  193. this.islandHeight = data.height;
  194. // 计算最大可移动距离
  195. this.maxTranslate = this.backgroundWidth - this.screenWidth;
  196. // 初始位置居中
  197. // this.translateX = -this.maxTranslate / 2;
  198. // this.translateX = 0;
  199. // 打印调试信息
  200. // console.log('屏幕宽度:', this.screenWidth);
  201. // console.log('背景宽度:', this.backgroundWidth);
  202. // console.log('岛屿高度:', this.islandHeight);
  203. // console.log('最大可移动距离:',this.maxTranslate);
  204. // console.log('岛屿data:', data);
  205. } else {
  206. console.error('未能获取岛屿图片的尺寸');
  207. }
  208. }).exec();
  209. },
  210. // 触摸开始
  211. touchStart(e) {
  212. this.startX = e.touches[0].clientX;
  213. this.currentX = this.translateX;
  214. console.log('this.startX =',this.startX);
  215. console.log('this.currentX =',this.currentX);
  216. },
  217. // 触摸移动
  218. touchMove(e) {
  219. console.log('----------- touchMove');
  220. const moveX = e.touches[0].clientX - this.startX;
  221. let newTranslateX = this.currentX + moveX;
  222. // 限制移动范围,不让背景两侧露出
  223. if (newTranslateX > 0) {
  224. newTranslateX = 0;
  225. } else if (newTranslateX < -this.maxTranslate) {
  226. newTranslateX = -this.maxTranslate;
  227. }
  228. this.translateX = newTranslateX;
  229. console.log('moveX =',moveX);
  230. console.log('this.translateX =',this.translateX);
  231. },
  232. // 触摸结束
  233. touchEnd() {
  234. console.log('----------- touchEnd');
  235. this.currentX = this.translateX;
  236. console.log('this.currentX =',this.currentX);
  237. },
  238. onmousedown(e) {
  239. console.log('----------- onmousedown');
  240. console.log('----------- e',e);
  241. this.isDragging = true;
  242. this.startX = e.clientX;
  243. this.currentX = this.translateX;
  244. mapLayer.style.cursor = 'grabbing';
  245. },
  246. onmousemove(e) {
  247. if(this.isDragging){
  248. console.log('----------- onmousemove');
  249. const moveX = e.clientX - this.startX;
  250. let newTranslateX = this.currentX + moveX;
  251. //限制移动范围,不让背景两侧露出
  252. if (newTranslateX > 0) {
  253. newTranslateX = 0;
  254. } else if (newTranslateX < -this.maxTranslate) {
  255. newTranslateX = -this.maxTranslate;
  256. }
  257. this.translateX = newTranslateX;
  258. console.log('moveX =',moveX);
  259. console.log('this.translateX =',this.translateX);
  260. }
  261. },
  262. onmouseup(e) {
  263. console.log('----------- onmouseup');
  264. this.isDragging = false;
  265. mapLayer.style.cursor = 'grab';
  266. },
  267. onHouseClick(event) {
  268. if(this.houseAnimating) return;
  269. // 处理点击事件
  270. console.log('House clicked!');
  271. // 播放房子的点击动画
  272. this.playAnimation('house');
  273. // uni.showToast({
  274. // title: 'House clicked!',
  275. // icon: 'none'
  276. // });
  277. },
  278. onTableClick(event) {
  279. if(this.tableAnimating) return;
  280. console.log('Table clicked!');
  281. // 播放大厅的点击动画
  282. this.playAnimation('table');
  283. // 显示制造台界面
  284. this.craftingVisible = true;
  285. },
  286. onTaskClick(event) {
  287. if(this.taskAnimating) return;
  288. // 处理点击事件
  289. console.log('Task clicked!');
  290. // 播放大厅的点击动画
  291. this.playAnimation('task');
  292. // uni.showToast({
  293. // title: 'task clicked!',
  294. // icon: 'none'
  295. // });
  296. },
  297. playAnimation(type) {
  298. let self = this;
  299. // 根据类型设置对应的动画状态
  300. if (type === 'house') {
  301. this.houseAnimating = true;
  302. } else if (type === 'table') {
  303. this.tableAnimating = true;
  304. } else if (type === 'task') {
  305. this.taskAnimating = true;
  306. }
  307. // 创建动画实例
  308. const animation = uni.createAnimation({
  309. duration: 400,
  310. timingFunction: 'ease',
  311. });
  312. // 定义果冻动画序列
  313. animation.scale(0.95).step({ duration: 100 })
  314. .scale(1.05).step({ duration: 100 })
  315. .scale(0.98).step({ duration: 100 })
  316. .scale(1).step({ duration: 100 });
  317. // 根据类型应用动画到对应的元素
  318. if (type === 'house') {
  319. this.houseAnimationData = animation.export();
  320. } else if (type === 'table') {
  321. this.tableAnimationData = animation.export();
  322. } else if (type === 'task') {
  323. this.taskAnimationData = animation.export();
  324. }
  325. // 动画结束后重置状态
  326. setTimeout(() => {
  327. if (type === 'house') {
  328. self.houseAnimating = false;
  329. } else if (type === 'table') {
  330. self.tableAnimating = false;
  331. } else if (type === 'task') {
  332. self.taskAnimating = false;
  333. }
  334. }, 800);
  335. },
  336. showInventory() {
  337. console.log('Opening inventory...')
  338. this.inventoryVisible = true
  339. },
  340. onInventoryClose() {
  341. console.log('Closing inventory...')
  342. this.inventoryVisible = false
  343. },
  344. showCharacter() {
  345. console.log('Opening character...')
  346. this.characterVisible = true
  347. },
  348. onCharacterClose() {
  349. console.log('Closing character...')
  350. this.characterVisible = false
  351. },
  352. showShop() {
  353. console.log('Opening shop...')
  354. this.shopVisible = true
  355. },
  356. onShopClose() {
  357. console.log('Closing shop...')
  358. this.shopVisible = false
  359. },
  360. onShopBuy(item) {
  361. console.log('Buying item:', item)
  362. },
  363. showTask() {
  364. console.log('Opening task board...')
  365. this.taskVisible = true
  366. },
  367. onTaskClose() {
  368. console.log('Closing task board...')
  369. this.taskVisible = false
  370. },
  371. startMainArrowAnimation() {
  372. if (this.mainArrowAnimating) return;
  373. this.mainArrowAnimating = true;
  374. const animation = uni.createAnimation({
  375. duration: 1000,
  376. timingFunction: 'ease-in-out',
  377. });
  378. const animate = () => {
  379. if (!this.mainArrowAnimating) return;
  380. animation.translateY(-10).step({ duration: 1000 });
  381. this.mainArrowAnimation = animation.export();
  382. setTimeout(() => {
  383. animation.translateY(0).step({ duration: 1000 });
  384. this.mainArrowAnimation = animation.export();
  385. setTimeout(() => {
  386. if (this.mainArrowAnimating) {
  387. animate();
  388. }
  389. }, 1000);
  390. }, 1000);
  391. };
  392. animate();
  393. },
  394. goToMainLand() {
  395. uni.navigateTo({
  396. url: '/pages/isLand/mainLand'
  397. });
  398. },
  399. onGuideComplete() {
  400. console.log('引导完成')
  401. // 这里可以添加引导完成后的逻辑
  402. uni.setStorageSync('isGuideCompleted', true)
  403. },
  404. // 检查并显示引导
  405. checkAndShowGuide() {
  406. // 延迟执行以确保DOM已经渲染
  407. setTimeout(() => {
  408. this.$refs.guideManager && this.$refs.guideManager.startGuide('homeLand');
  409. }, 500);
  410. },
  411. // 获取用户铃钱
  412. getUserMoney() {
  413. uni.request({
  414. url: this.$apiHost + '/User/getinfo',
  415. method: 'GET',
  416. data: {
  417. uuid: getApp().globalData.uuid
  418. },
  419. header: {
  420. 'Content-Type': 'application/x-www-form-urlencoded',
  421. 'sign': getApp().globalData.headerSign,
  422. },
  423. success: (res) => {
  424. if (res.data) {
  425. console.log("res.getUserMoney", res.data)
  426. this.userInfo = res.data;
  427. }
  428. }
  429. })
  430. },
  431. },
  432. beforeDestroy() {
  433. this.mainArrowAnimating = false;
  434. }
  435. }
  436. </script>
  437. <style lang="scss" scoped>
  438. @import './homeLand.scss';
  439. .ui-layer {
  440. position: fixed;
  441. top: 0;
  442. left: 0;
  443. right: 0;
  444. bottom: 0;
  445. pointer-events: none;
  446. z-index: 100;
  447. .ui-content {
  448. position: absolute;
  449. top: 20rpx;
  450. right: 20rpx;
  451. pointer-events: auto;
  452. z-index: 101;
  453. }
  454. .ui-buttons {
  455. display: flex;
  456. flex-direction: column;
  457. gap: 20rpx;
  458. .ui-button {
  459. background: rgba(255, 255, 255, 0.9);
  460. border: none;
  461. padding: 16rpx 32rpx;
  462. border-radius: 8rpx;
  463. font-size: 28rpx;
  464. color: #333;
  465. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  466. min-width: 120rpx;
  467. &:active {
  468. opacity: 0.8;
  469. transform: scale(0.95);
  470. }
  471. }
  472. }
  473. }
  474. .map-layer {
  475. position: relative;
  476. z-index: 1;
  477. cursor: grab;
  478. &:active {
  479. cursor: grabbing;
  480. }
  481. }
  482. .main-arrow {
  483. // background: rgba(255, 255, 255, 0.9);
  484. position: absolute;
  485. height: 190rpx;
  486. right: 50rpx;
  487. bottom: 20rpx; // 改为 bottom 定位
  488. display: flex;
  489. flex-direction: column;
  490. align-items: center;
  491. cursor: pointer;
  492. z-index: 10;
  493. transition: opacity 0.5s ease-in-out;
  494. .arrow {
  495. content: '';
  496. display: block;
  497. width: 60rpx;
  498. height: 60rpx;
  499. background: url('/static/island/arrow.png') no-repeat center center;
  500. background-size: contain;
  501. transform: rotate(270deg);
  502. }
  503. .main-text {
  504. color: #ffffff;
  505. font-size: 32rpx;
  506. text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.5);
  507. margin-top: 10rpx;
  508. }
  509. }
  510. .task-dialog{
  511. ::v-deep.dialog-content{
  512. background: transparent !important;
  513. }
  514. }
  515. </style>