mainLand.vue 11 KB

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