homeLand.vue 14 KB

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