homeLand.vue 8.5 KB

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