mainLand.vue 13 KB

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