homeLand.vue 14 KB

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