| Server IP : 104.21.80.248 / Your IP : 172.71.28.156 Web Server : Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30 System : Windows NT WIN-ECQAAA40806 6.2 build 9200 (Windows Server 2012 Standard Edition) i586 User : SYSTEM ( 0) PHP Version : 5.6.30 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Inetpub/www/game/train/ |
Upload File : |
document.addEventListener('DOMContentLoaded', () => {
const selection = document.getElementById('train-selection');
const trackContainer = document.getElementById('track-container');
const scenery = document.getElementById('scenery');
const runButton = document.getElementById('run-button');
const stopButton = document.getElementById('stop-button');
let draggedItem = null;
// --- การลากและวาง (Drag and Drop) ---
selection.addEventListener('dragstart', (e) => {
if (e.target.classList.contains('train-part')) {
draggedItem = e.target;
setTimeout(() => {
e.target.style.display = 'none'; // ซ่อนชั่วคราว
}, 0);
}
});
selection.addEventListener('dragend', (e) => {
if (e.target.classList.contains('train-part')) {
setTimeout(() => {
draggedItem.style.display = 'inline-block'; // แสดงกลับมา
draggedItem = null;
}, 0);
}
});
trackContainer.addEventListener('dragover', (e) => {
e.preventDefault(); // อนุญาตให้วางได้
});
trackContainer.addEventListener('drop', (e) => {
e.preventDefault();
if (draggedItem) {
// ตรวจสอบว่ามีหัวรถจักรหรือยัง
const hasLocomotive = trackContainer.querySelector('.locomotive');
const isLocomotive = draggedItem.id === 'locomotive-template';
if (!hasLocomotive && !isLocomotive) {
alert('ต้องวางหัวรถจักรก่อน!');
return;
}
if (hasLocomotive && isLocomotive) {
alert('มีหัวรถจักรได้แค่ 1 อัน!');
return;
}
const newPart = draggedItem.cloneNode(true);
newPart.classList.remove('train-part');
newPart.classList.add('train-part-placed');
if (isLocomotive) {
newPart.classList.add('locomotive');
trackContainer.prepend(newPart); // วางหัวรถจักรไว้ข้างหน้าสุด
} else {
trackContainer.appendChild(newPart);
}
}
});
// --- การควบคุมรถไฟ ---
runButton.addEventListener('click', () => {
if (trackContainer.children.length > 0) {
scenery.classList.add('scenery-running');
// ทำให้ไม่สามารถลากวางได้ตอนรถไฟวิ่ง
selection.style.pointerEvents = 'none';
trackContainer.style.pointerEvents = 'none';
} else {
alert('ยังไม่มีรถไฟบนราง!');
}
});
stopButton.addEventListener('click', () => {
scenery.classList.remove('scenery-running');
// ทำให้กลับมาลากวางได้
selection.style.pointerEvents = 'auto';
trackContainer.style.pointerEvents = 'auto';
});
});