homeLand.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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: 680rpx; bottom:430rpx;align-items: center;">
  9. <image class="house-image" src="/static/island/building/4.png" mode="widthFix" style="width:670rpx; position: static;" @click="onHouseClick" :animation="houseAnimationData"> </image>
  10. </view>
  11. <view style="position: absolute;width: 670rpx;left:180rpx; bottom:130rpx;align-items: center;">
  12. <image class="hall-image" src="/static/island/building/1.png" mode="widthFix" style="width:670rpx; position: static;" @click="onHallClick" :animation="hallAnimationData"> </image>
  13. </view>
  14. <!-- 主岛箭头 -->
  15. <view class="main-arrow" @click="goToMainLand" :animation="mainArrowAnimation" :style="{ opacity: mainArrowVisible ? 1 : 0 }">
  16. <image src="/static/island/main_arrow.png" mode="widthFix" style="width: 100rpx;"></image>
  17. <text class="main-text">主岛</text>
  18. </view>
  19. </view>
  20. <!-- 第一层:UI -->
  21. <view class="ui-layer">
  22. <view class="ui-content">
  23. <view class="ui-buttons">
  24. <button class="ui-button" @click="showInventory">背包</button>
  25. <button class="ui-button" @click="showCharacter">角色</button>
  26. <button class="ui-button" @click="showShop">商店</button>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- 对话框组件 -->
  31. <inventory-dialog :visible.sync="inventoryVisible" @close="onInventoryClose"></inventory-dialog>
  32. <character-dialog :visible.sync="characterVisible" @close="onCharacterClose"></character-dialog>
  33. <shop-dialog :visible.sync="shopVisible" @close="onShopClose" @buy="onShopBuy"></shop-dialog>
  34. </view>
  35. </template>
  36. <script>
  37. import InventoryDialog from '@/components/dialogs/InventoryDialog.vue'
  38. import CharacterDialog from '@/components/dialogs/CharacterDialog.vue'
  39. import ShopDialog from '@/components/dialogs/ShopDialog.vue'
  40. export default {
  41. components: {
  42. InventoryDialog,
  43. CharacterDialog,
  44. ShopDialog
  45. },
  46. data() {
  47. return {
  48. // 背景位置控制
  49. translateX: 0,
  50. startX: 0,
  51. currentX: 0,
  52. isDragging : false,
  53. // 获取屏幕宽度和背景宽度
  54. screenWidth: 0,
  55. backgroundWidth: 0,
  56. islandHeight: 0,
  57. houseAnimationData: {},
  58. hallAnimationData: {},
  59. houseAnimating: false,
  60. hallAnimating: false,
  61. baseTranslate: { x: -50, y: 50 },
  62. inventoryVisible: false,
  63. characterVisible: false,
  64. shopVisible: false,
  65. mainArrowAnimation: {},
  66. mainArrowAnimating: false,
  67. mainArrowVisible: false,
  68. }
  69. },
  70. onLoad() {
  71. let self = this;
  72. // 获取屏幕宽度
  73. uni.getSystemInfo({
  74. success: (res) => {
  75. self.screenWidth = res.windowWidth;
  76. console.log('屏幕宽度:', self.screenWidth);
  77. }
  78. });
  79. },
  80. onShow() {
  81. // this.loadData();
  82. },
  83. onReady() {
  84. // 在组件渲染完成后获取图片尺寸
  85. setTimeout(() => {
  86. this.getImageSize();
  87. // 延迟1秒后显示箭头并开始动画
  88. setTimeout(() => {
  89. this.mainArrowVisible = true;
  90. this.startMainArrowAnimation();
  91. }, 1000);
  92. }, 300);
  93. },
  94. methods: {
  95. loadData() {
  96. // 可以在这里加载其他数据
  97. },
  98. getImageSize() {
  99. const query = uni.createSelectorQuery().in(this);
  100. query.select('.island-image').boundingClientRect(data => {
  101. if (data) {
  102. // 获取岛屿图片的宽度和高度
  103. this.backgroundWidth = data.width;
  104. this.islandHeight = data.height;
  105. // 计算最大可移动距离
  106. this.maxTranslate = this.backgroundWidth - this.screenWidth;
  107. // 初始位置居中
  108. this.translateX = -this.maxTranslate / 2;
  109. // 打印调试信息
  110. console.log('屏幕宽度:', this.screenWidth);
  111. console.log('背景宽度:', this.backgroundWidth);
  112. console.log('岛屿高度:', this.islandHeight);
  113. console.log('最大可移动距离:',this.maxTranslate);
  114. console.log('岛屿data:', data);
  115. } else {
  116. console.error('未能获取岛屿图片的尺寸');
  117. }
  118. }).exec();
  119. },
  120. // 触摸开始
  121. touchStart(e) {
  122. this.startX = e.touches[0].clientX;
  123. this.currentX = this.translateX;
  124. console.log('this.startX =',this.startX);
  125. console.log('this.currentX =',this.currentX);
  126. },
  127. // 触摸移动
  128. touchMove(e) {
  129. console.log('----------- touchMove');
  130. const moveX = e.touches[0].clientX - this.startX;
  131. let newTranslateX = this.currentX + moveX;
  132. // 限制移动范围,不让背景两侧露出
  133. if (newTranslateX > 0) {
  134. newTranslateX = 0;
  135. } else if (newTranslateX < -this.maxTranslate) {
  136. newTranslateX = -this.maxTranslate;
  137. }
  138. this.translateX = newTranslateX;
  139. console.log('moveX =',moveX);
  140. console.log('this.translateX =',this.translateX);
  141. },
  142. // 触摸结束
  143. touchEnd() {
  144. console.log('----------- touchEnd');
  145. this.currentX = this.translateX;
  146. console.log('this.currentX =',this.currentX);
  147. },
  148. onmousedown(e) {
  149. console.log('----------- onmousedown');
  150. console.log('----------- e',e);
  151. this.isDragging = true;
  152. this.startX = e.clientX;
  153. this.currentX = this.translateX;
  154. mapLayer.style.cursor = 'grabbing';
  155. },
  156. onmousemove(e) {
  157. if(this.isDragging){
  158. console.log('----------- onmousemove');
  159. const moveX = e.clientX - this.startX;
  160. let newTranslateX = this.currentX + moveX;
  161. //限制移动范围,不让背景两侧露出
  162. if (newTranslateX > 0) {
  163. newTranslateX = 0;
  164. } else if (newTranslateX < -this.maxTranslate) {
  165. newTranslateX = -this.maxTranslate;
  166. }
  167. this.translateX = newTranslateX;
  168. console.log('moveX =',moveX);
  169. console.log('this.translateX =',this.translateX);
  170. }
  171. },
  172. onmouseup(e) {
  173. console.log('----------- onmouseup');
  174. this.isDragging = false;
  175. mapLayer.style.cursor = 'grab';
  176. },
  177. onHouseClick(event) {
  178. if(this.houseAnimating) return;
  179. // 处理点击事件
  180. console.log('House clicked!');
  181. // 播放房子的点击动画
  182. this.playAnimation('house');
  183. uni.showToast({
  184. title: 'House clicked!',
  185. icon: 'none'
  186. });
  187. },
  188. onHallClick(event) {
  189. if(this.hallAnimating) return;
  190. // 处理点击事件
  191. console.log('Hall clicked!');
  192. // 播放大厅的点击动画
  193. this.playAnimation('hall');
  194. uni.showToast({
  195. title: 'Hall clicked!',
  196. icon: 'none'
  197. });
  198. },
  199. playAnimation(type) {
  200. let self = this;
  201. // 根据类型设置对应的动画状态
  202. if (type === 'house') {
  203. this.houseAnimating = true;
  204. } else if (type === 'hall') {
  205. this.hallAnimating = true;
  206. }
  207. // 创建动画实例
  208. const animation = uni.createAnimation({
  209. duration: 400,
  210. timingFunction: 'ease',
  211. });
  212. // 定义果冻动画序列
  213. animation.scale(0.95).step({ duration: 100 })
  214. .scale(1.05).step({ duration: 100 })
  215. .scale(0.98).step({ duration: 100 })
  216. .scale(1).step({ duration: 100 });
  217. // 根据类型应用动画到对应的元素
  218. if (type === 'house') {
  219. this.houseAnimationData = animation.export();
  220. } else if (type === 'hall') {
  221. this.hallAnimationData = animation.export();
  222. }
  223. // 动画结束后重置状态
  224. setTimeout(() => {
  225. if (type === 'house') {
  226. self.houseAnimating = false;
  227. } else if (type === 'hall') {
  228. self.hallAnimating = false;
  229. }
  230. }, 800);
  231. },
  232. showInventory() {
  233. console.log('Opening inventory...')
  234. this.inventoryVisible = true
  235. },
  236. onInventoryClose() {
  237. console.log('Closing inventory...')
  238. this.inventoryVisible = false
  239. },
  240. showCharacter() {
  241. console.log('Opening character...')
  242. this.characterVisible = true
  243. },
  244. onCharacterClose() {
  245. console.log('Closing character...')
  246. this.characterVisible = false
  247. },
  248. showShop() {
  249. console.log('Opening shop...')
  250. this.shopVisible = true
  251. },
  252. onShopClose() {
  253. console.log('Closing shop...')
  254. this.shopVisible = false
  255. },
  256. onShopBuy(item) {
  257. console.log('Buying item:', item)
  258. },
  259. startMainArrowAnimation() {
  260. if (this.mainArrowAnimating) return;
  261. this.mainArrowAnimating = true;
  262. const animation = uni.createAnimation({
  263. duration: 1000,
  264. timingFunction: 'ease-in-out',
  265. });
  266. const animate = () => {
  267. if (!this.mainArrowAnimating) return;
  268. animation.translateY(-20).step({ duration: 1000 });
  269. this.mainArrowAnimation = animation.export();
  270. setTimeout(() => {
  271. animation.translateY(0).step({ duration: 1000 });
  272. this.mainArrowAnimation = animation.export();
  273. setTimeout(() => {
  274. if (this.mainArrowAnimating) {
  275. animate();
  276. }
  277. }, 1000);
  278. }, 1000);
  279. };
  280. animate();
  281. },
  282. goToMainLand() {
  283. uni.navigateTo({
  284. url: '/pages/isLand/mainLand'
  285. });
  286. }
  287. },
  288. beforeDestroy() {
  289. this.mainArrowAnimating = false;
  290. }
  291. }
  292. </script>
  293. <style lang="scss" scoped>
  294. @import './homeLand.scss';
  295. .ui-layer {
  296. position: fixed;
  297. top: 0;
  298. left: 0;
  299. right: 0;
  300. bottom: 0;
  301. pointer-events: none;
  302. z-index: 100;
  303. .ui-content {
  304. position: absolute;
  305. top: 20rpx;
  306. right: 20rpx;
  307. pointer-events: auto;
  308. z-index: 101;
  309. }
  310. .ui-buttons {
  311. display: flex;
  312. flex-direction: column;
  313. gap: 20rpx;
  314. .ui-button {
  315. background: rgba(255, 255, 255, 0.9);
  316. border: none;
  317. padding: 16rpx 32rpx;
  318. border-radius: 8rpx;
  319. font-size: 28rpx;
  320. color: #333;
  321. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  322. min-width: 120rpx;
  323. &:active {
  324. opacity: 0.8;
  325. transform: scale(0.95);
  326. }
  327. }
  328. }
  329. }
  330. .map-layer {
  331. position: relative;
  332. z-index: 1;
  333. cursor: grab;
  334. &:active {
  335. cursor: grabbing;
  336. }
  337. }
  338. .main-arrow {
  339. position: absolute;
  340. right: 100rpx; // 调整到右边位置
  341. top: 50%;
  342. transform: translateY(-50%);
  343. display: flex;
  344. flex-direction: column;
  345. align-items: center;
  346. cursor: pointer;
  347. z-index: 10;
  348. transition: opacity 0.5s ease-in-out; // 添加淡入效果
  349. .main-text {
  350. color: #ffffff;
  351. font-size: 28rpx;
  352. text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.5);
  353. margin-top: 10rpx;
  354. }
  355. }
  356. </style>