Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- body {
- margin: 0;
- overflow: hidden;
- font-family: 'Prompt', sans-serif;
- }
- #gameCanvas {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- }
- .start-screen {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
- display: flex;
- flex-direction: column;
- justify-content: center;
- align-items: center;
- z-index: 100;
- }
- .planet-info {
- position: fixed;
- bottom: 20px;
- left: 20px;
- background: rgba(15, 23, 42, 0.8);
- border-radius: 10px;
- padding: 15px;
- color: white;
- max-width: 300px;
- transform: translateY(150%);
- transition: transform 0.3s ease-out;
- z-index: 10;
- }
- .planet-info.visible {
- transform: translateY(0);
- }
- .controls-info {
- position: fixed;
- top: 20px;
- right: 20px;
- background: rgba(15, 23, 42, 0.8);
- border-radius: 10px;
- padding: 15px;
- color: white;
- z-index: 10;
- }
- .orbit-info {
- position: fixed;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- background: rgba(15, 23, 42, 0.9);
- border-radius: 10px;
- padding: 20px;
- color: white;
- max-width: 500px;
- z-index: 20;
- display: none;
- }
- .mini-map {
- position: fixed;
- bottom: 20px;
- right: 20px;
- width: 150px;
- height: 150px;
- background: rgba(15, 23, 42, 0.8);
- border-radius: 50%;
- z-index: 10;
- overflow: hidden;
- }
- .star {
- position: absolute;
- background-color: white;
- border-radius: 50%;
- }
- .button-pulse {
- animation: pulse 2s infinite;
- }
- @keyframes pulse {
- 0% {
- box-shadow: 0 0 0 0 rgba(79, 209, 197, 0.7);
- }
- 70% {
- box-shadow: 0 0 0 15px rgba(79, 209, 197, 0);
- }
- 100% {
- box-shadow: 0 0 0 0 rgba(79, 209, 197, 0);
- }
- }
- .gravity-indicator {
- position: fixed;
- top: 20px;
- left: 20px;
- background: rgba(15, 23, 42, 0.8);
- border-radius: 10px;
- padding: 10px 15px;
- color: white;
- z-index: 10;
- display: none;
- }
- .gravity-indicator.active {
- display: block;
- }
- .gravity-bar {
- width: 100%;
- height: 10px;
- background: rgba(255, 255, 255, 0.2);
- border-radius: 5px;
- margin-top: 5px;
- overflow: hidden;
- }
- .gravity-bar-fill {
- height: 100%;
- background: linear-gradient(90deg, #4fd1c5 0%, #3182ce 100%);
- border-radius: 5px;
- width: 0%;
- transition: width 0.3s ease;
- }
- .boost-button {
- position: fixed;
- bottom: 20px;
- left: 50%;
- transform: translateX(-50%);
- background: linear-gradient(90deg, #f43f5e 0%, #ec4899 100%);
- color: white;
- border: none;
- border-radius: 50px;
- padding: 12px 30px;
- font-size: 18px;
- font-weight: 600;
- cursor: pointer;
- z-index: 10;
- transition: all 0.3s ease;
- box-shadow: 0 4px 10px rgba(236, 72, 153, 0.5);
- }
- .boost-button:hover {
- transform: translateX(-50%) translateY(-3px);
- box-shadow: 0 6px 15px rgba(236, 72, 153, 0.6);
- }
- .boost-button:active {
- transform: translateX(-50%) translateY(-1px);
- }
- .boost-button.boosting {
- background: linear-gradient(90deg, #f97316 0%, #f43f5e 100%);
- animation: boosting 0.5s infinite alternate;
- }
- @keyframes boosting {
- 0% {
- box-shadow: 0 0 15px rgba(249, 115, 22, 0.7);
- }
- 100% {
- box-shadow: 0 0 25px rgba(244, 63, 94, 0.9);
- }
- }
- .boost-meter {
- position: fixed;
- bottom: 80px;
- left: 50%;
- transform: translateX(-50%);
- width: 200px;
- height: 8px;
- background: rgba(255, 255, 255, 0.2);
- border-radius: 4px;
- overflow: hidden;
- z-index: 10;
- }
- .boost-meter-fill {
- height: 100%;
- background: linear-gradient(90deg, #f43f5e 0%, #ec4899 100%);
- border-radius: 4px;
- width: 100%;
- transition: width 0.3s ease;
- }
- .boost-meter-fill.depleting {
- background: linear-gradient(90deg, #f97316 0%, #f43f5e 100%);
- }
- .boost-meter-fill.recharging {
- background: linear-gradient(90deg, #4ade80 0%, #2dd4bf 100%);
- }
- .engine-glow {
- animation: glow 1s infinite alternate;
- }
- @keyframes glow {
- 0% {
- opacity: 0.5;
- scale: 0.9;
- }
- 100% {
- opacity: 0.8;
- scale: 1.1;
- }
- }
- .boost-glow {
- animation: boostGlow 0.2s infinite alternate;
- }
- @keyframes boostGlow {
- 0% {
- opacity: 0.7;
- scale: 1.2;
- }
- 100% {
- opacity: 1;
- scale: 1.5;
- }
- }
- </style>
- <div class="start-screen" id="startScreen">
- <div class="relative w-full h-full">
- <!-- Stars background -->
- <div id="stars" class="absolute inset-0 overflow-hidden"></div>
- <div class="absolute inset-0 flex flex-col items-center justify-center">
- <div class="text-center">
- <h1 class="text-5xl md:text-7xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-teal-400 to-blue-500 mb-6">นักสำรวจระบบสุริยะ</h1>
- <p class="text-xl md:text-2xl text-gray-300 mb-8">ผจญภัยไปในอวกาศและสำรวจดาวเคราะห์ในระบบสุริยะของเรา</p>
- <button id="startButton" class="bg-gradient-to-r from-teal-500 to-blue-600 text-white text-xl font-medium py-3 px-8 rounded-full hover:from-teal-600 hover:to-blue-700 transition-all duration-300 button-pulse">
- เริ่มการสำรวจ
- </button>
- <div class="mt-12 text-gray-400">
- <p class="mb-2">การควบคุม:</p>
- <div class="grid grid-cols-2 gap-4 max-w-md mx-auto text-sm">
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">W</span> เคลื่อนที่ไปข้างหน้า
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">S</span> เคลื่อนที่ถอยหลัง
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">A</span> หมุนซ้าย
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">D</span> หมุนขวา
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">Q</span> เคลื่อนที่ขึ้น
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">E</span> เคลื่อนที่ลง
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">I</span> แสดงข้อมูลวงโคจร
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">SPACE</span> เร่งความเร็ว
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="planet-info" id="planetInfo">
- <h2 class="text-xl font-bold text-teal-400 mb-2" id="planetName"></h2>
- <p id="planetDescription" class="text-sm"></p>
- <div class="mt-3 pt-3 border-t border-gray-600">
- <div class="grid grid-cols-2 gap-2 text-xs">
- <div>
- <span class="text-gray-400">รัศมี:</span>
- <span id="planetRadius" class="text-white"></span>
- </div>
- <div>
- <span class="text-gray-400">ระยะห่างจากดวงอาทิตย์:</span>
- <span id="planetDistance" class="text-white"></span>
- </div>
- <div>
- <span class="text-gray-400">อุณหภูมิเฉลี่ย:</span>
- <span id="planetTemp" class="text-white"></span>
- </div>
- <div>
- <span class="text-gray-400">แรงโน้มถ่วง:</span>
- <span id="planetGravity" class="text-white"></span>
- </div>
- </div>
- </div>
- </div>
- <div class="controls-info">
- <div class="text-sm">
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">W</span> เคลื่อนที่ไปข้างหน้า</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">S</span> เคลื่อนที่ถอยหลัง</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">A</span> หมุนซ้าย</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">D</span> หมุนขวา</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">Q</span> เคลื่อนที่ขึ้น</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">E</span> เคลื่อนที่ลง</div>
- <div class="mb-2"><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">I</span> แสดงข้อมูลวงโคจร</div>
- <div><span class="inline-block bg-gray-700 px-2 py-1 rounded mr-2">SPACE</span> เร่งความเร็ว</div>
- </div>
- </div>
- <div class="orbit-info" id="orbitInfo">
- <div class="flex justify-between items-center mb-4">
- <h2 class="text-xl font-bold text-teal-400">กลไกการโคจรและระยะทาง</h2>
- <button id="closeOrbitInfo" class="text-gray-400 hover:text-white">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
- <line x1="18" y1="6" x2="6" y2="18"></line>
- <line x1="6" y1="6" x2="18" y2="18"></line>
- </svg>
- </button>
- </div>
- <div class="space-y-4">
- <p>ดาวเคราะห์โคจรรอบดวงอาทิตย์ในวงรีเนื่องจากแรงโน้มถ่วง โดยดาวเคราะห์ที่อยู่ใกล้ดวงอาทิตย์จะโคจรเร็วกว่าดาวเคราะห์ที่อยู่ไกล</p>
- <div class="bg-gray-800 p-3 rounded-lg">
- <h3 class="font-medium text-teal-400 mb-2">ระยะทางเฉลี่ยจากดวงอาทิตย์:</h3>
- <ul class="space-y-1 text-sm">
- <li>ดาวพุธ: 57.9 ล้านกิโลเมตร (0.39 AU)</li>
- <li>ดาวศุกร์: 108.2 ล้านกิโลเมตร (0.72 AU)</li>
- <li>โลก: 149.6 ล้านกิโลเมตร (1.00 AU)</li>
- <li>ดาวอังคาร: 227.9 ล้านกิโลเมตร (1.52 AU)</li>
- <li>ดาวพฤหัสบดี: 778.5 ล้านกิโลเมตร (5.20 AU)</li>
- <li>ดาวเสาร์: 1,434 ล้านกิโลเมตร (9.58 AU)</li>
- <li>ดาวยูเรนัส: 2,871 ล้านกิโลเมตร (19.22 AU)</li>
- <li>ดาวเนปจูน: 4,495 ล้านกิโลเมตร (30.05 AU)</li>
- </ul>
- <p class="text-xs text-gray-400 mt-2">AU (Astronomical Unit) คือหน่วยวัดระยะทางในอวกาศ โดย 1 AU เท่ากับระยะทางเฉลี่ยจากโลกถึงดวงอาทิตย์</p>
- </div>
- <div class="bg-gray-800 p-3 rounded-lg">
- <h3 class="font-medium text-teal-400 mb-2">กฎของเคปเลอร์:</h3>
- <ol class="list-decimal list-inside space-y-1 text-sm">
- <li>วงโคจรของดาวเคราะห์เป็นวงรี โดยมีดวงอาทิตย์อยู่ที่จุดโฟกัสหนึ่ง</li>
- <li>เส้นที่ลากจากดวงอาทิตย์ถึงดาวเคราะห์จะกวาดพื้นที่เท่ากันในเวลาที่เท่ากัน</li>
- <li>กำลังสองของคาบการโคจรแปรผันตรงกับกำลังสามของระยะทางเฉลี่ยจากดวงอาทิตย์</li>
- </ol>
- </div>
- </div>
- </div>
- <div class="mini-map" id="miniMap"></div>
- <div class="gravity-indicator" id="gravityIndicator">
- <div class="flex justify-between items-center">
- <span class="text-sm font-medium">แรงโน้มถ่วง:</span>
- <span class="text-sm" id="gravityValue">0%</span>
- </div>
- <div class="gravity-bar">
- <div class="gravity-bar-fill" id="gravityBarFill"></div>
- </div>
- </div>
- <button class="boost-button" id="boostButton">เร่งความเร็ว</button>
- <div class="boost-meter">
- <div class="boost-meter-fill" id="boostMeterFill"></div>
- </div>
- <script>
- // ข้อมูลดาวเคราะห์
- const planetData = {
- "Sun": {
- name: "ดวงอาทิตย์ (Sun)",
- description: "ดวงอาทิตย์เป็นดาวฤกษ์ที่อยู่ศูนย์กลางของระบบสุริยะ มีมวลคิดเป็น 99.86% ของมวลทั้งหมดในระบบสุริยะ",
- radius: "696,340 กิโลเมตร",
- distance: "0 กิโลเมตร",
- temperature: "5,500°C (พื้นผิว)",
- gravity: "274 m/s²",
- color: 0xffcc00,
- size: 5,
- position: [0, 0, 0],
- orbitRadius: 0,
- orbitSpeed: 0,
- gravityStrength: 100
- },
- "Mercury": {
- name: "ดาวพุธ (Mercury)",
- description: "ดาวเคราะห์ที่เล็กที่สุดและอยู่ใกล้ดวงอาทิตย์มากที่สุดในระบบสุริยะ มีพื้นผิวเต็มไปด้วยหลุมอุกกาบาต",
- radius: "2,440 กิโลเมตร",
- distance: "57.9 ล้านกิโลเมตร",
- temperature: "-173°C ถึง 427°C",
- gravity: "3.7 m/s²",
- color: 0xbebebe,
- size: 0.4,
- position: [10, 0, 0],
- orbitRadius: 10,
- orbitSpeed: 0.04,
- gravityStrength: 10
- },
- "Venus": {
- name: "ดาวศุกร์ (Venus)",
- description: "ดาวเคราะห์ที่มีขนาดและมวลใกล้เคียงกับโลก แต่มีบรรยากาศหนาแน่นที่ประกอบด้วยคาร์บอนไดออกไซด์ ทำให้เกิดปรากฏการณ์เรือนกระจกรุนแรง",
- radius: "6,052 กิโลเมตร",
- distance: "108.2 ล้านกิโลเมตร",
- temperature: "462°C",
- gravity: "8.87 m/s²",
- color: 0xe6e6e6,
- size: 0.9,
- position: [15, 0, 0],
- orbitRadius: 15,
- orbitSpeed: 0.03,
- gravityStrength: 20
- },
- "Earth": {
- name: "โลก (Earth)",
- description: "บ้านของเรา เป็นดาวเคราะห์เพียงดวงเดียวที่ทราบว่ามีสิ่งมีชีวิตอาศัยอยู่ มีน้ำในสถานะของเหลวและบรรยากาศที่เหมาะสมต่อการดำรงชีวิต",
- radius: "6,371 กิโลเมตร",
- distance: "149.6 ล้านกิโลเมตร",
- temperature: "-88°C ถึง 58°C",
- gravity: "9.8 m/s²",
- color: 0x2b82c9,
- size: 1,
- position: [20, 0, 0],
- orbitRadius: 20,
- orbitSpeed: 0.025,
- gravityStrength: 25
- },
- "Mars": {
- name: "ดาวอังคาร (Mars)",
- description: "ดาวเคราะห์สีแดง มีบรรยากาศบางเบาและพื้นผิวที่แห้งแล้ง มีภูเขาไฟที่ใหญ่ที่สุดในระบบสุริยะคือ Olympus Mons",
- radius: "3,390 กิโลเมตร",
- distance: "227.9 ล้านกิโลเมตร",
- temperature: "-153°C ถึง 20°C",
- gravity: "3.71 m/s²",
- color: 0xc1440e,
- size: 0.5,
- position: [25, 0, 0],
- orbitRadius: 25,
- orbitSpeed: 0.02,
- gravityStrength: 15
- },
- "Jupiter": {
- name: "ดาวพฤหัสบดี (Jupiter)",
- description: "ดาวเคราะห์ที่ใหญ่ที่สุดในระบบสุริยะ เป็นดาวเคราะห์ก๊าซยักษ์ที่มีจุดแดงใหญ่ซึ่งเป็นพายุที่มีอายุมากกว่า 300 ปี",
- radius: "69,911 กิโลเมตร",
- distance: "778.5 ล้านกิโลเมตร",
- temperature: "-145°C",
- gravity: "24.79 m/s²",
- color: 0xd8ca9d,
- size: 2.5,
- position: [35, 0, 0],
- orbitRadius: 35,
- orbitSpeed: 0.01,
- gravityStrength: 60
- },
- "Saturn": {
- name: "ดาวเสาร์ (Saturn)",
- description: "ดาวเคราะห์ที่มีวงแหวนที่สวยงามและโดดเด่น วงแหวนประกอบด้วยน้ำแข็งและฝุ่นที่โคจรรอบดาวเสาร์",
- radius: "58,232 กิโลเมตร",
- distance: "1,434 ล้านกิโลเมตร",
- temperature: "-178°C",
- gravity: "10.44 m/s²",
- color: 0xead6b8,
- size: 2.2,
- position: [45, 0, 0],
- orbitRadius: 45,
- orbitSpeed: 0.008,
- gravityStrength: 50
- },
- "Uranus": {
- name: "ดาวยูเรนัส (Uranus)",
- description: "ดาวเคราะห์ก๊าซยักษ์ที่มีแกนหมุนเอียงมากจนเหมือนกลิ้งไปตามวงโคจร มีสีฟ้าอมเขียวจากก๊าซมีเทนในบรรยากาศ",
- radius: "25,362 กิโลเมตร",
- distance: "2,871 ล้านกิโลเมตร",
- temperature: "-224°C",
- gravity: "8.69 m/s²",
- color: 0x75c1e0,
- size: 1.8,
- position: [55, 0, 0],
- orbitRadius: 55,
- orbitSpeed: 0.006,
- gravityStrength: 40
- },
- "Neptune": {
- name: "ดาวเนปจูน (Neptune)",
- description: "ดาวเคราะห์ที่อยู่ไกลที่สุด (หลังจากที่ดาวพลูโตถูกจัดเป็นดาวเคราะห์แคระ) มีสีน้ำเงินเข้มและพายุรุนแรงที่สุดในระบบสุริยะ",
- radius: "24,622 กิโลเมตร",
- distance: "4,495 ล้านกิโลเมตร",
- temperature: "-218°C",
- gravity: "11.15 m/s²",
- color: 0x3b55ce,
- size: 1.7,
- position: [65, 0, 0],
- orbitRadius: 65,
- orbitSpeed: 0.005,
- gravityStrength: 45
- }
- };
- // สร้างดาวบนหน้าเริ่มต้น
- function createStars() {
- const starsContainer = document.getElementById('stars');
- const starCount = 200;
- for (let i = 0; i < starCount; i++) {
- const star = document.createElement('div');
- star.className = 'star';
- // สุ่มขนาดและตำแหน่ง
- const size = Math.random() * 3;
- const left = Math.random() * 100;
- const top = Math.random() * 100;
- star.style.width = `${size}px`;
- star.style.height = `${size}px`;
- star.style.left = `${left}%`;
- star.style.top = `${top}%`;
- star.style.opacity = Math.random();
- starsContainer.appendChild(star);
- }
- }
- createStars();
- // ตัวแปรสำหรับ Three.js
- let scene, camera, renderer, spaceship;
- let planets = {};
- let orbits = {};
- let miniMapScene, miniMapCamera, miniMapRenderer;
- let keys = {};
- let currentPlanet = null;
- let velocity = new THREE.Vector3();
- let gravityInfluence = false;
- // ตัวแปรสำหรับระบบเร่งความเร็ว
- let boostEnergy = 100;
- let isBoosting = false;
- let boostCooldown = false;
- let engineGlow, boostGlow;
- // เริ่มเกม
- document.getElementById('startButton').addEventListener('click', function() {
- document.getElementById('startScreen').style.display = 'none';
- initGame();
- });
- // ปิดข้อมูลวงโคจร
- document.getElementById('closeOrbitInfo').addEventListener('click', function() {
- document.getElementById('orbitInfo').style.display = 'none';
- });
- // ฟังก์ชันเริ่มเกม
- function initGame() {
- initThreeJS();
- createSpaceship();
- createPlanets();
- createOrbits();
- createStarfield();
- createMiniMap();
- setupBoostSystem();
- // ตรวจจับการกดปุ่ม
- window.addEventListener('keydown', function(e) {
- keys[e.key.toLowerCase()] = true;
- // แสดงข้อมูลวงโคจร
- if (e.key.toLowerCase() === 'i') {
- const orbitInfo = document.getElementById('orbitInfo');
- orbitInfo.style.display = orbitInfo.style.display === 'block' ? 'none' : 'block';
- }
- // เร่งความเร็ว
- if (e.key === ' ' && !boostCooldown && boostEnergy > 0) {
- activateBoost();
- }
- });
- window.addEventListener('keyup', function(e) {
- keys[e.key.toLowerCase()] = false;
- // หยุดเร่งความเร็ว
- if (e.key === ' ') {
- deactivateBoost();
- }
- });
- animate();
- }
- // ฟังก์ชันเริ่มต้น Three.js
- function initThreeJS() {
- // สร้าง Scene
- scene = new THREE.Scene();
- scene.background = new THREE.Color(0x000510);
- // สร้าง Camera
- camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
- camera.position.set(0, 10, 30);
- camera.lookAt(0, 0, 0);
- // สร้าง Renderer
- renderer = new THREE.WebGLRenderer({ antialias: true });
- renderer.setSize(window.innerWidth, window.innerHeight);
- document.getElementById('gameCanvas').appendChild(renderer.domElement);
- // สร้างแสง
- const ambientLight = new THREE.AmbientLight(0x333333);
- scene.add(ambientLight);
- const sunLight = new THREE.PointLight(0xffffff, 2, 300);
- sunLight.position.set(0, 0, 0);
- scene.add(sunLight);
- // ปรับขนาดหน้าจอเมื่อมีการเปลี่ยนแปลง
- window.addEventListener('resize', function() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize(window.innerWidth, window.innerHeight);
- if (miniMapRenderer) {
- miniMapRenderer.setSize(150, 150);
- }
- });
- }
- // สร้างยานอวกาศ
- function createSpaceship() {
- // สร้างยานอวกาศด้วย SVG
- spaceship = new THREE.Group();
- // ตัวยาน
- const bodyGeometry = new THREE.ConeGeometry(0.5, 2, 8);
- const bodyMaterial = new THREE.MeshPhongMaterial({ color: 0x888888 });
- const body = new THREE.Mesh(bodyGeometry, bodyMaterial);
- body.rotation.x = Math.PI / 2;
- spaceship.add(body);
- // ปีก
- const wingGeometry = new THREE.BoxGeometry(2, 0.1, 0.7);
- const wingMaterial = new THREE.MeshPhongMaterial({ color: 0x3182ce });
- const wings = new THREE.Mesh(wingGeometry, wingMaterial);
- wings.position.z = -0.5;
- spaceship.add(wings);
- // หน้าต่างยาน
- const windowGeometry = new THREE.SphereGeometry(0.2, 16, 16);
- const windowMaterial = new THREE.MeshPhongMaterial({ color: 0x4fd1c5, emissive: 0x4fd1c5, emissiveIntensity: 0.5 });
- const window = new THREE.Mesh(windowGeometry, windowMaterial);
- window.position.y = 0.3;
- window.position.z = 0.5;
- spaceship.add(window);
- // เครื่องยนต์
- const engineGeometry = new THREE.CylinderGeometry(0.2, 0.3, 0.5, 8);
- const engineMaterial = new THREE.MeshPhongMaterial({ color: 0x555555 });
- const engine = new THREE.Mesh(engineGeometry, engineMaterial);
- engine.position.z = -1;
- engine.rotation.x = Math.PI / 2;
- spaceship.add(engine);
- // ไฟเครื่องยนต์ปกติ
- engineGlow = new THREE.Mesh(
- new THREE.ConeGeometry(0.2, 0.8, 8),
- new THREE.MeshBasicMaterial({
- color: 0xff6600,
- transparent: true,
- opacity: 0.7
- })
- );
- engineGlow.position.z = -1.5;
- engineGlow.rotation.x = -Math.PI / 2;
- engineGlow.userData.defaultScale = new THREE.Vector3(1, 1, 1);
- spaceship.add(engineGlow);
- // ไฟเร่งความเร็ว
- boostGlow = new THREE.Mesh(
- new THREE.ConeGeometry(0.3, 1.2, 8),
- new THREE.MeshBasicMaterial({
- color: 0xff0066,
- transparent: true,
- opacity: 0
- })
- );
- boostGlow.position.z = -1.8;
- boostGlow.rotation.x = -Math.PI / 2;
- spaceship.add(boostGlow);
- // ปรับขนาดและตำแหน่งยาน
- spaceship.scale.set(0.5, 0.5, 0.5);
- spaceship.position.set(0, 0, 25);
- scene.add(spaceship);
- // เก็บข้อมูลยานอวกาศ
- spaceship.userData = {
- velocity: new THREE.Vector3(),
- rotationVelocity: new THREE.Vector3(),
- speed: 0.1,
- rotationSpeed: 0.03,
- boostSpeed: 0.3
- };
- }
- // สร้างดาวเคราะห์
- function createPlanets() {
- for (const [key, planet] of Object.entries(planetData)) {
- const geometry = new THREE.SphereGeometry(planet.size, 32, 32);
- let material;
- if (key === "Sun") {
- // สร้างดวงอาทิตย์ให้มีการเรืองแสง
- material = new THREE.MeshBasicMaterial({
- color: planet.color,
- emissive: planet.color,
- emissiveIntensity: 1
- });
- } else {
- material = new THREE.MeshPhongMaterial({ color: planet.color });
- }
- const mesh = new THREE.Mesh(geometry, material);
- mesh.position.set(...planet.position);
- scene.add(mesh);
- planets[key] = {
- mesh: mesh,
- data: planet
- };
- // สร้างวงแหวนสำหรับดาวเสาร์
- if (key === "Saturn") {
- const ringGeometry = new THREE.RingGeometry(planet.size + 0.5, planet.size + 2, 32);
- const ringMaterial = new THREE.MeshBasicMaterial({
- color: 0xc2a278,
- side: THREE.DoubleSide,
- transparent: true,
- opacity: 0.8
- });
- const ring = new THREE.Mesh(ringGeometry, ringMaterial);
- ring.rotation.x = Math.PI / 2;
- mesh.add(ring);
- }
- // สร้างบรรยากาศสำหรับโลก
- if (key === "Earth") {
- const atmosphereGeometry = new THREE.SphereGeometry(planet.size + 0.05, 32, 32);
- const atmosphereMaterial = new THREE.MeshPhongMaterial({
- color: 0x4fc3f7,
- transparent: true,
- opacity: 0.3
- });
- const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
- mesh.add(atmosphere);
- }
- // สร้างพื้นผิวพิเศษสำหรับดาวพฤหัสบดี
- if (key === "Jupiter") {
- const bands = new THREE.Object3D();
- for (let i = 0; i < 5; i++) {
- const bandGeometry = new THREE.TorusGeometry(planet.size * (0.85 + i * 0.03), 0.05, 16, 100);
- const bandMaterial = new THREE.MeshBasicMaterial({
- color: i % 2 === 0 ? 0xd8ca9d : 0xc2a278,
- transparent: true,
- opacity: 0.7
- });
- const band = new THREE.Mesh(bandGeometry, bandMaterial);
- band.rotation.x = Math.PI / 2;
- bands.add(band);
- }
- mesh.add(bands);
- }
- }
- }
- // สร้างวงโคจร
- function createOrbits() {
- for (const [key, planet] of Object.entries(planetData)) {
- if (planet.orbitRadius > 0) {
- const orbitGeometry = new THREE.BufferGeometry();
- const points = [];
- for (let i = 0; i <= 100; i++) {
- const angle = (i / 100) * Math.PI * 2;
- points.push(
- new THREE.Vector3(
- Math.cos(angle) * planet.orbitRadius,
- 0,
- Math.sin(angle) * planet.orbitRadius
- )
- );
- }
- orbitGeometry.setFromPoints(points);
- const orbitMaterial = new THREE.LineBasicMaterial({
- color: 0x444444,
- transparent: true,
- opacity: 0.3
- });
- const orbit = new THREE.Line(orbitGeometry, orbitMaterial);
- scene.add(orbit);
- orbits[key] = orbit;
- }
- }
- }
- // สร้างดาวในอวกาศ
- function createStarfield() {
- const starsGeometry = new THREE.BufferGeometry();
- const starsMaterial = new THREE.PointsMaterial({
- color: 0xffffff,
- size: 0.1,
- transparent: true
- });
- const starsVertices = [];
- for (let i = 0; i < 10000; i++) {
- const x = (Math.random() - 0.5) * 2000;
- const y = (Math.random() - 0.5) * 2000;
- const z = (Math.random() - 0.5) * 2000;
- starsVertices.push(x, y, z);
- }
- starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
- const starField = new THREE.Points(starsGeometry, starsMaterial);
- scene.add(starField);
- }
- // สร้างแผนที่ขนาดเล็ก
- function createMiniMap() {
- miniMapScene = new THREE.Scene();
- miniMapScene.background = new THREE.Color(0x000510);
- miniMapCamera = new THREE.OrthographicCamera(-70, 70, 70, -70, 1, 1000);
- miniMapCamera.position.set(0, 100, 0);
- miniMapCamera.lookAt(0, 0, 0);
- miniMapRenderer = new THREE.WebGLRenderer({ antialias: true });
- miniMapRenderer.setSize(150, 150);
- document.getElementById('miniMap').appendChild(miniMapRenderer.domElement);
- // สร้างดาวเคราะห์ในแผนที่ขนาดเล็ก
- for (const [key, planet] of Object.entries(planetData)) {
- const geometry = new THREE.SphereGeometry(planet.size * 0.5, 16, 16);
- const material = new THREE.MeshBasicMaterial({ color: planet.color });
- const mesh = new THREE.Mesh(geometry, material);
- mesh.position.set(...planet.position);
- miniMapScene.add(mesh);
- }
- // สร้างตัวแทนยานอวกาศในแผนที่ขนาดเล็ก
- const shipGeometry = new THREE.SphereGeometry(0.5, 8, 8);
- const shipMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
- const shipMesh = new THREE.Mesh(shipGeometry, shipMaterial);
- miniMapScene.add(shipMesh);
- // เก็บตัวแทนยานอวกาศไว้ใช้ในการอัปเดตตำแหน่ง
- miniMapScene.userData.shipMesh = shipMesh;
- }
- // ตั้งค่าระบบเร่งความเร็ว
- function setupBoostSystem() {
- const boostButton = document.getElementById('boostButton');
- const boostMeterFill = document.getElementById('boostMeterFill');
- // อัปเดตแถบพลังงาน
- updateBoostMeter();
- // เพิ่มการตรวจจับการคลิกปุ่มเร่งความเร็ว
- boostButton.addEventListener('mousedown', function() {
- if (!boostCooldown && boostEnergy > 0) {
- activateBoost();
- }
- });
- boostButton.addEventListener('mouseup', function() {
- deactivateBoost();
- });
- boostButton.addEventListener('mouseleave', function() {
- deactivateBoost();
- });
- // เพิ่มการตรวจจับการสัมผัสสำหรับอุปกรณ์มือถือ
- boostButton.addEventListener('touchstart', function(e) {
- e.preventDefault();
- if (!boostCooldown && boostEnergy > 0) {
- activateBoost();
- }
- });
- boostButton.addEventListener('touchend', function(e) {
- e.preventDefault();
- deactivateBoost();
- });
- }
- // เปิดใช้งานการเร่งความเร็ว
- function activateBoost() {
- if (boostCooldown || boostEnergy <= 0) return;
- isBoosting = true;
- // เปลี่ยนสถานะปุ่ม
- const boostButton = document.getElementById('boostButton');
- boostButton.classList.add('boosting');
- boostButton.textContent = 'กำลังเร่งความเร็ว!';
- // เปลี่ยนสถานะแถบพลังงาน
- const boostMeterFill = document.getElementById('boostMeterFill');
- boostMeterFill.classList.add('depleting');
- boostMeterFill.classList.remove('recharging');
- // เปิดใช้งานเอฟเฟกต์ไฟเร่งความเร็ว
- boostGlow.material.opacity = 0.9;
- // เริ่มลดพลังงาน
- if (!spaceship.userData.boostInterval) {
- spaceship.userData.boostInterval = setInterval(function() {
- if (isBoosting && boostEnergy > 0) {
- boostEnergy = Math.max(0, boostEnergy - 2);
- updateBoostMeter();
- if (boostEnergy <= 0) {
- deactivateBoost();
- startBoostCooldown();
- }
- }
- }, 100);
- }
- }
- // ปิดใช้งานการเร่งความเร็ว
- function deactivateBoost() {
- if (!isBoosting) return;
- isBoosting = false;
- // เปลี่ยนสถานะปุ่ม
- const boostButton = document.getElementById('boostButton');
- boostButton.classList.remove('boosting');
- boostButton.textContent = 'เร่งความเร็ว';
- // เปลี่ยนสถานะแถบพลังงาน
- const boostMeterFill = document.getElementById('boostMeterFill');
- boostMeterFill.classList.remove('depleting');
- if (boostEnergy < 100 && !boostCooldown) {
- boostMeterFill.classList.add('recharging');
- }
- // ปิดใช้งานเอฟเฟกต์ไฟเร่งความเร็ว
- boostGlow.material.opacity = 0;
- // หยุดลดพลังงาน
- if (spaceship.userData.boostInterval) {
- clearInterval(spaceship.userData.boostInterval);
- spaceship.userData.boostInterval = null;
- }
- // เริ่มชาร์จพลังงาน
- if (!spaceship.userData.rechargeInterval && !boostCooldown) {
- spaceship.userData.rechargeInterval = setInterval(function() {
- if (!isBoosting && boostEnergy < 100) {
- boostEnergy = Math.min(100, boostEnergy + 0.5);
- updateBoostMeter();
- if (boostEnergy >= 100) {
- const boostMeterFill = document.getElementById('boostMeterFill');
- boostMeterFill.classList.remove('recharging');
- clearInterval(spaceship.userData.rechargeInterval);
- spaceship.userData.rechargeInterval = null;
- }
- }
- }, 100);
- }
- }
- // เริ่มคูลดาวน์หลังจากใช้พลังงานหมด
- function startBoostCooldown() {
- boostCooldown = true;
- const boostButton = document.getElementById('boostButton');
- boostButton.textContent = 'กำลังชาร์จ...';
- boostButton.disabled = true;
- boostButton.style.opacity = '0.5';
- boostButton.style.cursor = 'not-allowed';
- setTimeout(function() {
- boostCooldown = false;
- boostButton.textContent = 'เร่งความเร็ว';
- boostButton.disabled = false;
- boostButton.style.opacity = '1';
- boostButton.style.cursor = 'pointer';
- // เริ่มชาร์จพลังงาน
- const boostMeterFill = document.getElementById('boostMeterFill');
- boostMeterFill.classList.add('recharging');
- if (!spaceship.userData.rechargeInterval) {
- spaceship.userData.rechargeInterval = setInterval(function() {
- if (!isBoosting && boostEnergy < 100) {
- boostEnergy = Math.min(100, boostEnergy + 0.5);
- updateBoostMeter();
- if (boostEnergy >= 100) {
- boostMeterFill.classList.remove('recharging');
- clearInterval(spaceship.userData.rechargeInterval);
- spaceship.userData.rechargeInterval = null;
- }
- }
- }, 100);
- }
- }, 3000);
- }
- // อัปเดตแถบพลังงาน
- function updateBoostMeter() {
- const boostMeterFill = document.getElementById('boostMeterFill');
- boostMeterFill.style.width = `${boostEnergy}%`;
- }
- // ฟังก์ชันอนิเมชัน
- function animate() {
- requestAnimationFrame(animate);
- // อัปเดตตำแหน่งดาวเคราะห์
- updatePlanets();
- // ควบคุมยานอวกาศ
- controlSpaceship();
- // คำนวณแรงโน้มถ่วง
- calculateGravity();
- // ตรวจสอบการเข้าใกล้ดาวเคราะห์
- checkPlanetProximity();
- // อัปเดตแผนที่ขนาดเล็ก
- updateMiniMap();
- // อัปเดตเอฟเฟกต์ไฟเครื่องยนต์
- updateEngineEffects();
- // เรนเดอร์ฉาก
- renderer.render(scene, camera);
- miniMapRenderer.render(miniMapScene, miniMapCamera);
- }
- // อัปเดตตำแหน่งดาวเคราะห์
- function updatePlanets() {
- for (const [key, planet] of Object.entries(planets)) {
- if (key !== "Sun") {
- const orbitRadius = planet.data.orbitRadius;
- const orbitSpeed = planet.data.orbitSpeed;
- const time = Date.now() * 0.001;
- const x = Math.cos(time * orbitSpeed) * orbitRadius;
- const z = Math.sin(time * orbitSpeed) * orbitRadius;
- planet.mesh.position.set(x, 0, z);
- // อัปเดตตำแหน่งในแผนที่ขนาดเล็ก
- const miniPlanet = miniMapScene.children.find(child =>
- child instanceof THREE.Mesh &&
- child.geometry instanceof THREE.SphereGeometry &&
- Math.abs(child.position.x - planet.mesh.position.x) < 0.1 &&
- Math.abs(child.position.z - planet.mesh.position.z) < 0.1
- );
- if (miniPlanet) {
- miniPlanet.position.copy(planet.mesh.position);
- }
- }
- // หมุนดาวเคราะห์
- planet.mesh.rotation.y += 0.005;
- }
- }
- // ควบคุมยานอวกาศ
- function controlSpaceship() {
- // กำหนดความเร็วตามสถานะการเร่งความเร็ว
- const moveSpeed = isBoosting ? spaceship.userData.boostSpeed : spaceship.userData.speed;
- const rotateSpeed = spaceship.userData.rotationSpeed;
- // หมุนซ้าย-ขวา
- if (keys['a']) {
- spaceship.rotation.y += rotateSpeed;
- }
- if (keys['d']) {
- spaceship.rotation.y -= rotateSpeed;
- }
- // หมุนขึ้น-ลง
- if (keys['q']) {
- spaceship.position.y += moveSpeed;
- }
- if (keys['e']) {
- spaceship.position.y -= moveSpeed;
- }
- // เคลื่อนที่ไปข้างหน้า-ถอยหลัง
- const direction = new THREE.Vector3(0, 0, -1);
- direction.applyQuaternion(spaceship.quaternion);
- if (keys['w']) {
- spaceship.position.addScaledVector(direction, moveSpeed);
- }
- if (keys['s']) {
- spaceship.position.addScaledVector(direction, -moveSpeed);
- }
- // เร่งความเร็วด้วยปุ่ม Space
- if (keys[' '] && !boostCooldown && boostEnergy > 0) {
- activateBoost();
- } else if (!keys[' '] && isBoosting) {
- deactivateBoost();
- }
- // อัปเดตตำแหน่งกล้อง
- const cameraOffset = new THREE.Vector3(0, 1, 5);
- cameraOffset.applyQuaternion(spaceship.quaternion);
- camera.position.copy(spaceship.position).add(cameraOffset);
- camera.lookAt(spaceship.position);
- // อัปเดตตำแหน่งยานอวกาศในแผนที่ขนาดเล็ก
- if (miniMapScene.userData.shipMesh) {
- miniMapScene.userData.shipMesh.position.set(
- spaceship.position.x,
- 0,
- spaceship.position.z
- );
- }
- }
- // คำนวณแรงโน้มถ่วง
- function calculateGravity() {
- let maxGravity = 0;
- let closestPlanetName = null;
- for (const [key, planet] of Object.entries(planets)) {
- const distance = spaceship.position.distanceTo(planet.mesh.position);
- const gravityRadius = planet.data.size * 10;
- if (distance < gravityRadius) {
- // ลดผลกระทบของแรงโน้มถ่วงเมื่อเร่งความเร็ว
- const gravityMultiplier = isBoosting ? 0.3 : 1.0;
- const gravityStrength = planet.data.gravityStrength * (1 - distance / gravityRadius) * gravityMultiplier;
- if (gravityStrength > maxGravity) {
- maxGravity = gravityStrength;
- closestPlanetName = key;
- }
- }
- }
- // แสดงตัวบ่งชี้แรงโน้มถ่วง
- const gravityIndicator = document.getElementById('gravityIndicator');
- const gravityValue = document.getElementById('gravityValue');
- const gravityBarFill = document.getElementById('gravityBarFill');
- if (maxGravity > 0) {
- gravityIndicator.classList.add('active');
- gravityValue.textContent = `${Math.round(maxGravity)}% (${planets[closestPlanetName].data.name})`;
- gravityBarFill.style.width = `${maxGravity}%`;
- // ใช้แรงโน้มถ่วงดึงยานอวกาศ
- const direction = new THREE.Vector3();
- direction.subVectors(planets[closestPlanetName].mesh.position, spaceship.position).normalize();
- spaceship.position.addScaledVector(direction, maxGravity * 0.0005);
- } else {
- gravityIndicator.classList.remove('active');
- }
- }
- // ตรวจสอบการเข้าใกล้ดาวเคราะห์
- function checkPlanetProximity() {
- let closestPlanet = null;
- let closestDistance = Infinity;
- for (const [key, planet] of Object.entries(planets)) {
- const distance = spaceship.position.distanceTo(planet.mesh.position);
- // ตรวจสอบระยะห่างจากดาวเคราะห์
- if (distance < closestDistance) {
- closestDistance = distance;
- closestPlanet = planet;
- }
- }
- // แสดงข้อมูลดาวเคราะห์เมื่อเข้าใกล้
- const planetInfo = document.getElementById('planetInfo');
- if (closestDistance < 10 && closestPlanet !== currentPlanet) {
- currentPlanet = closestPlanet;
- document.getElementById('planetName').textContent = closestPlanet.data.name;
- document.getElementById('planetDescription').textContent = closestPlanet.data.description;
- document.getElementById('planetRadius').textContent = closestPlanet.data.radius;
- document.getElementById('planetDistance').textContent = closestPlanet.data.distance;
- document.getElementById('planetTemp').textContent = closestPlanet.data.temperature;
- document.getElementById('planetGravity').textContent = closestPlanet.data.gravity;
- planetInfo.classList.add('visible');
- } else if (closestDistance >= 10 && planetInfo.classList.contains('visible')) {
- planetInfo.classList.remove('visible');
- currentPlanet = null;
- }
- }
- // อัปเดตแผนที่ขนาดเล็ก
- function updateMiniMap() {
- // อัปเดตมุมมองของกล้องในแผนที่ขนาดเล็ก
- miniMapCamera.position.set(0, 100, 0);
- miniMapCamera.lookAt(0, 0, 0);
- }
- // อัปเดตเอฟเฟกต์ไฟเครื่องยนต์
- function updateEngineEffects() {
- // อนิเมชันไฟเครื่องยนต์ปกติ
- if (engineGlow) {
- if (keys['w']) {
- engineGlow.material.opacity = 0.7;
- engineGlow.scale.z = 1 + Math.sin(Date.now() * 0.01) * 0.2;
- } else {
- engineGlow.material.opacity = 0.4;
- engineGlow.scale.z = 0.8 + Math.sin(Date.now() * 0.005) * 0.1;
- }
- }
- // อนิเมชันไฟเร่งความเร็ว
- if (boostGlow && isBoosting) {
- boostGlow.scale.z = 1.2 + Math.sin(Date.now() * 0.02) * 0.3;
- boostGlow.scale.x = 1 + Math.sin(Date.now() * 0.03) * 0.2;
- boostGlow.scale.y = 1 + Math.cos(Date.now() * 0.03) * 0.2;
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement