homeLand.vue 11 KB

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