mainLand.vue 15 KB

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