homeLand.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <template>
  2. <view class="home-land-container">
  3. <!-- 第三层:背景 -->
  4. <view class="background-layer"></view>
  5. <!-- 第二层:地图 -->
  6. <view class="map-layer" id="mapLayer" :style="{ transform: `translateX(${translateX}px)` }" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd" @mousedown="onmousedown" @mousemove="onmousemove" @mouseup="onmouseup">
  7. <image class="island-image" src="/static/island/island.png" mode="widthFix" style="width:1536rpx; bottom: 0rpx;left: 0rpx; position: absolute;"></image>
  8. <view style="position: absolute;width: 670rpx;left: 340rpx; bottom:320rpx;align-items: center;">
  9. <image class="house-image" src="/static/island/building/home1.png" mode="widthFix" style="width:670rpx; position: static;" @click="onHouseClick" :animation="houseAnimationData"> </image>
  10. </view>
  11. <view style="position: absolute;width: 300rpx;left:280rpx; bottom:200rpx;align-items: center;">
  12. <image class="table-image" src="/static/island/building/workTable.png" mode="widthFix" style="width:670rpx; position: static;" @click="onHallClick" :animation="hallAnimationData"> </image>
  13. </view>
  14. <view style="position: absolute;width:200rpx;left:660rpx; bottom:260rpx;align-items: center;">
  15. <image class="mailBox-image" src="/static/island/building/mailBox.png" mode="widthFix" style="width:670rpx; position: static;" > </image>
  16. </view>
  17. <!-- 主岛箭头 -->
  18. <view class="main-arrow" @click="goToMainLand" :animation="mainArrowAnimation" :style="{ opacity: mainArrowVisible ? 1 : 0 }">
  19. <image src="/static/island/main_arrow.png" mode="widthFix" style="width: 100rpx;"></image>
  20. <text class="main-text">主岛</text>
  21. </view>
  22. </view>
  23. <!-- 第一层:UI -->
  24. <view class="ui-layer">
  25. <view class="ui-content">
  26. <!-- 添加货币计数器 -->
  27. <view class="currency-display" >
  28. <view class="currency-item">
  29. <image src="/static/island/UI/wd_icon_coin.png" mode="widthFix" class="currency-icon"></image>
  30. <text class="currency-value">9999</text>
  31. </view>
  32. <view class="currency-item">
  33. <image src="/static/island/UI/wd_icon_xingyuan.png" mode="widthFix" class="currency-icon"></image>
  34. <text class="currency-value">9999</text>
  35. </view>
  36. </view>
  37. <view class="ui-buttons">
  38. <button class="ui-button" @click="showInventory">背包</button>
  39. <button class="ui-button" @click="showCharacter">角色</button>
  40. <button class="ui-button" @click="showShop">商店</button>
  41. <button class="ui-button" @click="showTask">任务</button>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- 对话框组件 -->
  46. <inventory-dialog :visible.sync="inventoryVisible" @close="onInventoryClose"></inventory-dialog>
  47. <character-dialog :visible.sync="characterVisible" @close="onCharacterClose"></character-dialog>
  48. <shop-dialog :visible.sync="shopVisible" @close="onShopClose" @buy="onShopBuy"></shop-dialog>
  49. <task-dialog class="task-dialog" :visible.sync="taskVisible" @close="onTaskClose"></task-dialog>
  50. <!-- 引导对话组件 -->
  51. <talk-guide
  52. :guide-data="guideData"
  53. :player-name="playerName"
  54. @guide-complete="onGuideComplete"
  55. ></talk-guide>
  56. </view>
  57. </template>
  58. <script>
  59. import InventoryDialog from '@/components/dialogs/InventoryDialog.vue'
  60. import CharacterDialog from '@/components/dialogs/CharacterDialog.vue'
  61. import ShopDialog from '@/components/dialogs/ShopDialog.vue'
  62. import TaskDialog from './TaskDialog.vue'
  63. import TalkGuide from './talkGuide.vue'
  64. export default {
  65. components: {
  66. InventoryDialog,
  67. CharacterDialog,
  68. ShopDialog,
  69. TaskDialog,
  70. TalkGuide
  71. },
  72. data() {
  73. return {
  74. // 背景位置控制
  75. translateX: 0,
  76. startX: 0,
  77. currentX: 0,
  78. isDragging : false,
  79. // 获取屏幕宽度和背景宽度
  80. screenWidth: 0,
  81. backgroundWidth: 0,
  82. islandHeight: 0,
  83. houseAnimationData: {},
  84. hallAnimationData: {},
  85. houseAnimating: false,
  86. hallAnimating: false,
  87. baseTranslate: { x: -50, y: 50 },
  88. inventoryVisible: false,
  89. characterVisible: false,
  90. shopVisible: false,
  91. mainArrowAnimation: {},
  92. mainArrowAnimating: false,
  93. mainArrowVisible: false,
  94. taskVisible: false,
  95. playerName: '梦幻',
  96. guideData: [
  97. {
  98. characterImage: '/static/island/npc.png',
  99. characterName: '罗宾',
  100. text: '这位大人,这里太大了吧....',
  101. position: 'left',
  102. isMirror: true
  103. },
  104. {
  105. characterImage: '/static/island/building/1.png',
  106. characterName: '我',
  107. text: '好的,我马上上会教付清',
  108. position: 'right',
  109. isMirror: false
  110. },
  111. {
  112. characterImage: '/static/island/building/1.png',
  113. characterName: '我',
  114. text: '很奇怪不是吗?',
  115. position: 'right',
  116. isMirror: false
  117. },
  118. {
  119. characterImage: '/static/island/npc.png',
  120. characterName: '罗宾',
  121. text: '怎么回事',
  122. position: 'left',
  123. isMirror: true
  124. }
  125. ]
  126. }
  127. },
  128. onLoad() {
  129. let self = this;
  130. // 获取屏幕宽度
  131. uni.getSystemInfo({
  132. success: (res) => {
  133. self.screenWidth = res.windowWidth;
  134. console.log('屏幕宽度:', self.screenWidth);
  135. }
  136. });
  137. },
  138. onShow() {
  139. // this.loadData();
  140. },
  141. onReady() {
  142. // 在组件渲染完成后获取图片尺寸
  143. setTimeout(() => {
  144. this.getImageSize();
  145. // 延迟1秒后显示箭头并开始动画
  146. setTimeout(() => {
  147. this.mainArrowVisible = true;
  148. this.startMainArrowAnimation();
  149. }, 1000);
  150. }, 300);
  151. },
  152. methods: {
  153. loadData() {
  154. // 可以在这里加载其他数据
  155. },
  156. getImageSize() {
  157. const query = uni.createSelectorQuery().in(this);
  158. query.select('.island-image').boundingClientRect(data => {
  159. if (data) {
  160. // 获取岛屿图片的宽度和高度
  161. this.backgroundWidth = data.width;
  162. this.islandHeight = data.height;
  163. // 计算最大可移动距离
  164. this.maxTranslate = this.backgroundWidth - this.screenWidth;
  165. // 初始位置居中
  166. this.translateX = -this.maxTranslate / 2;
  167. // 打印调试信息
  168. console.log('屏幕宽度:', this.screenWidth);
  169. console.log('背景宽度:', this.backgroundWidth);
  170. console.log('岛屿高度:', this.islandHeight);
  171. console.log('最大可移动距离:',this.maxTranslate);
  172. console.log('岛屿data:', data);
  173. } else {
  174. console.error('未能获取岛屿图片的尺寸');
  175. }
  176. }).exec();
  177. },
  178. // 触摸开始
  179. touchStart(e) {
  180. this.startX = e.touches[0].clientX;
  181. this.currentX = this.translateX;
  182. console.log('this.startX =',this.startX);
  183. console.log('this.currentX =',this.currentX);
  184. },
  185. // 触摸移动
  186. touchMove(e) {
  187. console.log('----------- touchMove');
  188. const moveX = e.touches[0].clientX - this.startX;
  189. let newTranslateX = this.currentX + moveX;
  190. // 限制移动范围,不让背景两侧露出
  191. if (newTranslateX > 0) {
  192. newTranslateX = 0;
  193. } else if (newTranslateX < -this.maxTranslate) {
  194. newTranslateX = -this.maxTranslate;
  195. }
  196. this.translateX = newTranslateX;
  197. console.log('moveX =',moveX);
  198. console.log('this.translateX =',this.translateX);
  199. },
  200. // 触摸结束
  201. touchEnd() {
  202. console.log('----------- touchEnd');
  203. this.currentX = this.translateX;
  204. console.log('this.currentX =',this.currentX);
  205. },
  206. onmousedown(e) {
  207. console.log('----------- onmousedown');
  208. console.log('----------- e',e);
  209. this.isDragging = true;
  210. this.startX = e.clientX;
  211. this.currentX = this.translateX;
  212. mapLayer.style.cursor = 'grabbing';
  213. },
  214. onmousemove(e) {
  215. if(this.isDragging){
  216. console.log('----------- onmousemove');
  217. const moveX = e.clientX - this.startX;
  218. let newTranslateX = this.currentX + moveX;
  219. //限制移动范围,不让背景两侧露出
  220. if (newTranslateX > 0) {
  221. newTranslateX = 0;
  222. } else if (newTranslateX < -this.maxTranslate) {
  223. newTranslateX = -this.maxTranslate;
  224. }
  225. this.translateX = newTranslateX;
  226. console.log('moveX =',moveX);
  227. console.log('this.translateX =',this.translateX);
  228. }
  229. },
  230. onmouseup(e) {
  231. console.log('----------- onmouseup');
  232. this.isDragging = false;
  233. mapLayer.style.cursor = 'grab';
  234. },
  235. onHouseClick(event) {
  236. if(this.houseAnimating) return;
  237. // 处理点击事件
  238. console.log('House clicked!');
  239. // 播放房子的点击动画
  240. this.playAnimation('house');
  241. uni.showToast({
  242. title: 'House clicked!',
  243. icon: 'none'
  244. });
  245. },
  246. onHallClick(event) {
  247. if(this.hallAnimating) return;
  248. // 处理点击事件
  249. console.log('Hall clicked!');
  250. // 播放大厅的点击动画
  251. this.playAnimation('hall');
  252. uni.showToast({
  253. title: 'Hall clicked!',
  254. icon: 'none'
  255. });
  256. },
  257. playAnimation(type) {
  258. let self = this;
  259. // 根据类型设置对应的动画状态
  260. if (type === 'house') {
  261. this.houseAnimating = true;
  262. } else if (type === 'hall') {
  263. this.hallAnimating = true;
  264. }
  265. // 创建动画实例
  266. const animation = uni.createAnimation({
  267. duration: 400,
  268. timingFunction: 'ease',
  269. });
  270. // 定义果冻动画序列
  271. animation.scale(0.95).step({ duration: 100 })
  272. .scale(1.05).step({ duration: 100 })
  273. .scale(0.98).step({ duration: 100 })
  274. .scale(1).step({ duration: 100 });
  275. // 根据类型应用动画到对应的元素
  276. if (type === 'house') {
  277. this.houseAnimationData = animation.export();
  278. } else if (type === 'hall') {
  279. this.hallAnimationData = animation.export();
  280. }
  281. // 动画结束后重置状态
  282. setTimeout(() => {
  283. if (type === 'house') {
  284. self.houseAnimating = false;
  285. } else if (type === 'hall') {
  286. self.hallAnimating = false;
  287. }
  288. }, 800);
  289. },
  290. showInventory() {
  291. console.log('Opening inventory...')
  292. this.inventoryVisible = true
  293. },
  294. onInventoryClose() {
  295. console.log('Closing inventory...')
  296. this.inventoryVisible = false
  297. },
  298. showCharacter() {
  299. console.log('Opening character...')
  300. this.characterVisible = true
  301. },
  302. onCharacterClose() {
  303. console.log('Closing character...')
  304. this.characterVisible = false
  305. },
  306. showShop() {
  307. console.log('Opening shop...')
  308. this.shopVisible = true
  309. },
  310. onShopClose() {
  311. console.log('Closing shop...')
  312. this.shopVisible = false
  313. },
  314. onShopBuy(item) {
  315. console.log('Buying item:', item)
  316. },
  317. showTask() {
  318. console.log('Opening task board...')
  319. this.taskVisible = true
  320. },
  321. onTaskClose() {
  322. console.log('Closing task board...')
  323. this.taskVisible = false
  324. },
  325. startMainArrowAnimation() {
  326. if (this.mainArrowAnimating) return;
  327. this.mainArrowAnimating = true;
  328. const animation = uni.createAnimation({
  329. duration: 1000,
  330. timingFunction: 'ease-in-out',
  331. });
  332. const animate = () => {
  333. if (!this.mainArrowAnimating) return;
  334. animation.translateY(-20).step({ duration: 1000 });
  335. this.mainArrowAnimation = animation.export();
  336. setTimeout(() => {
  337. animation.translateY(0).step({ duration: 1000 });
  338. this.mainArrowAnimation = animation.export();
  339. setTimeout(() => {
  340. if (this.mainArrowAnimating) {
  341. animate();
  342. }
  343. }, 1000);
  344. }, 1000);
  345. };
  346. animate();
  347. },
  348. goToMainLand() {
  349. uni.navigateTo({
  350. url: '/pages/isLand/mainLand'
  351. });
  352. },
  353. onGuideComplete() {
  354. console.log('引导完成')
  355. // 这里可以添加引导完成后的逻辑
  356. uni.setStorageSync('isGuideCompleted', true)
  357. }
  358. },
  359. beforeDestroy() {
  360. this.mainArrowAnimating = false;
  361. }
  362. }
  363. </script>
  364. <style lang="scss" scoped>
  365. @import './homeLand.scss';
  366. .ui-layer {
  367. position: fixed;
  368. top: 0;
  369. left: 0;
  370. right: 0;
  371. bottom: 0;
  372. pointer-events: none;
  373. z-index: 100;
  374. .ui-content {
  375. position: absolute;
  376. top: 20rpx;
  377. right: 20rpx;
  378. pointer-events: auto;
  379. z-index: 101;
  380. }
  381. .ui-buttons {
  382. display: flex;
  383. flex-direction: column;
  384. gap: 20rpx;
  385. .ui-button {
  386. background: rgba(255, 255, 255, 0.9);
  387. border: none;
  388. padding: 16rpx 32rpx;
  389. border-radius: 8rpx;
  390. font-size: 28rpx;
  391. color: #333;
  392. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  393. min-width: 120rpx;
  394. &:active {
  395. opacity: 0.8;
  396. transform: scale(0.95);
  397. }
  398. }
  399. }
  400. }
  401. .map-layer {
  402. position: relative;
  403. z-index: 1;
  404. cursor: grab;
  405. &:active {
  406. cursor: grabbing;
  407. }
  408. }
  409. .main-arrow {
  410. position: absolute;
  411. right: 100rpx; // 调整到右边位置
  412. top: 50%;
  413. transform: translateY(-50%);
  414. display: flex;
  415. flex-direction: column;
  416. align-items: center;
  417. cursor: pointer;
  418. z-index: 10;
  419. transition: opacity 0.5s ease-in-out; // 添加淡入效果
  420. .main-text {
  421. color: #ffffff;
  422. font-size: 28rpx;
  423. text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.5);
  424. margin-top: 10rpx;
  425. }
  426. }
  427. .task-dialog{
  428. ::v-deep.dialog-content{
  429. background: transparent !important;
  430. }
  431. }
  432. </style>