mainLand.vue 14 KB

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