mainLand.vue 17 KB

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