homeLand.vue 14 KB

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