mainLand.vue 9.6 KB

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