mainLand.vue 16 KB

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