mainLand.vue 13 KB

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