Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import React, { useState, useEffect, useRef } from "react"; import "./ChallengeArea.css"; import Popup from '../popup/Popup'; // Character image const imagesRight = ['char_right1.png', 'char_right2.png', 'char_right3.png']; const imagesLeft = ['char_left1.png', 'char_left2.png', 'char_left3.png']; const AttackRight = ['rightAtk1.png', 'rightAtk2.png', 'rightAtk3.png', 'rightAtk4.png', 'rightAtk6.png', 'rightAtk7.png', 'rightAtk8.png']; const AttackLeft = ['leftAtk1.png', 'leftAtk2.png', 'leftAtk3.png', 'leftAtk4.png', 'leftAtk6.png', 'leftAtk7.png', 'leftAtk8.png']; const stickAttackRight = ['rightStick1.png', 'rightStick2.png', 'rightStick3.png']; const stickAttackLeft = ['leftStick1.png', 'leftStick2.png', 'leftStick3.png']; const swordAttackRight = ['rightSword1.png', 'rightSword2.png', 'rightSword3.png']; const swordAttackLeft = ['leftSword1.png', 'leftSword2.png', 'leftSword3.png']; const lightingAnimationFrames = ['lighting1.png', 'lighting2.png', 'lighting3.png', 'lighting4.png', 'lighting5.png']; // Boss image const bossImagesLeft = ['wolfgoleft1.png', 'wolfgoleft2.png']; const bossImagesRight = ['wolfgoright1.png', 'wolfgoright2.png']; function ChallengeArea() { // initial state const [position, setPosition] = useState(5); const [health, setHealth] = useState(100); const [currentImage, setCurrentImage] = useState('char_right1.png'); const [direction, setDirection] = useState('right'); // get bought items const [boughtItems, ] = useState(() => { const saved = localStorage.getItem('boughtItems'); return saved ? JSON.parse(saved) : {}; }); // popup const [showPopup, setShowPopup] = useState(false); const [popupMessage, setPopupMessage] = useState({ title: '', body: '', background_color: ''}); // image index for animation const imageIndex = useRef(0); // magic state const [isMagicAttack, setIsMagicAttack] = useState(false); // state for mouse position for apply magic attack const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 }); // magic attack lighting const [showLighting, setShowLighting] = useState(false); const [lightingPosition, setLightingPosition] = useState({ x: 0, y: 0 }); const [currentLightingImage, setCurrentLightingImage] = useState(''); const lightingAnimationIndex = useRef(0); // popup const showCustomPopup = (title, body, background_color) => { setPopupMessage({ title, body, background_color }); setShowPopup(true); }; // boss state const [bossPosition, setBossPosition] = useState(95); // Initial boss position const [bossHealth, setBossHealth] = useState(100); // 新增Boss血量状态 const [currentBossImage, setCurrentBossImage] = useState('wolfgoleft1.png'); const bossImageIndex = useRef(0); // Add boss attack power const [bossAttackCooldown, setBossAttackCooldown] = useState(false); const bossCooldownTime = 1000; // Preload every pic useEffect(() => { [...imagesRight, ...imagesLeft, ...swordAttackRight, ...swordAttackLeft, ...AttackRight, ...AttackLeft, ...stickAttackRight, ...stickAttackLeft, ...bossImagesLeft, ...bossImagesRight].forEach(image => { const img = new Image(); img.src = image; }); }, []); // Animation useEffect(() => { let attackAnimationInterval; const updateImageForDirection = (dir) => { const images = dir === 'right' ? imagesRight : imagesLeft; setCurrentImage(images[imageIndex.current % images.length]); imageIndex.current++; }; // apply demage to boss const applyDamageToBoss = (damageAmount) => { setBossHealth((prevHealth) => Math.max(prevHealth - damageAmount, 0)); }; const handleKeyDown = (event) => { // check if weapon is purchased const isSwordAvailable = boughtItems['sword']; const isStickAvailable = boughtItems['stick']; const isBookAvailable = boughtItems['magicBook']; switch(event.key) { case "0": // Toggle magic attack if (isBookAvailable) { setIsMagicAttack(!isMagicAttack); const magicImage = direction === 'right' ? 'rightMagic.png' : 'leftMagic.png'; setCurrentImage(magicImage); } else { showCustomPopup("Purchase Required", "You need to purchase the Magic Book to use this attack.", "rgba(243, 97, 105, 0.7)"); } break; case "9": // Sword attack if (isSwordAvailable) { if (!attackAnimationInterval) { imageIndex.current = 0; attackAnimationInterval = setInterval(() => { const attackImages = direction === 'right' ? swordAttackRight : swordAttackLeft; setCurrentImage(attackImages[imageIndex.current % attackImages.length]); imageIndex.current++; if (imageIndex.current === attackImages.length) { clearInterval(attackAnimationInterval); attackAnimationInterval = null; applyDamageToBoss(15); } }, 150); } } else { showCustomPopup("Purchase Required", "You need to purchase the Sword to use this attack.", "rgba(243, 97, 105, 0.7)"); } break; case "8": // Stick attack if (isStickAvailable) { if (!attackAnimationInterval) { imageIndex.current = 0; attackAnimationInterval = setInterval(() => { const attackImages = direction === 'right' ? stickAttackRight : stickAttackLeft; setCurrentImage(attackImages[imageIndex.current % attackImages.length]); imageIndex.current++; if (imageIndex.current === attackImages.length) { clearInterval(attackAnimationInterval); attackAnimationInterval = null; applyDamageToBoss(10); } }, 150); } } else { showCustomPopup("Purchase Required", "You need to purchase the Stick to use this attack.", "rgba(243, 97, 105, 0.7)"); } break; case "7": // Normal attack if (!attackAnimationInterval) { imageIndex.current = 0; attackAnimationInterval = setInterval(() => { const attackImages = direction === 'right' ? AttackRight : AttackLeft; setCurrentImage(attackImages[imageIndex.current % attackImages.length]); imageIndex.current++; if (imageIndex.current === attackImages.length) { clearInterval(attackAnimationInterval); attackAnimationInterval = null; applyDamageToBoss(5); } }, 150); } break; case "a": case "ArrowLeft": setPosition(prevPosition => Math.max(prevPosition - 1, 0)); setDirection('left'); if (!attackAnimationInterval) { updateImageForDirection('left'); } break; case "d": case "ArrowRight": setPosition(prevPosition => Math.min(prevPosition + 1, 100)); setDirection('right'); if (!attackAnimationInterval) { updateImageForDirection('right'); } break; default: break; } }; const handleKeyUp = (event) => { if (["0", "7", "8", "9"].includes(event.key)) { const transitionImage = direction === 'right' ? imagesRight[0] : imagesLeft[0]; setCurrentImage(transitionImage); } if (isMagicAttack) { const magicImage = direction === 'right' ? 'rightMagic.png' : 'leftMagic.png'; setCurrentImage(magicImage); } if (attackAnimationInterval) { clearInterval(attackAnimationInterval); attackAnimationInterval = null; } }; window.addEventListener("keydown", handleKeyDown); window.addEventListener("keyup", handleKeyUp); return () => { window.removeEventListener("keydown", handleKeyDown); window.removeEventListener("keyup", handleKeyUp); if (attackAnimationInterval) { clearInterval(attackAnimationInterval); } }; }, [direction, boughtItems, isMagicAttack, position, bossHealth]); // show use-magic icon with mouse useEffect(() => { const handleMouseMove = (event) => { setMousePosition({ x: event.clientX, y: event.clientY }); }; window.addEventListener('mousemove', handleMouseMove); return () => { window.removeEventListener('mousemove', handleMouseMove); }; }, []); // Magic attack click useEffect(() => { const handleMouseClick = (event) => { if (!isMagicAttack) return; setShowLighting(true); const lightingPos = { x: event.clientX, y: event.clientY }; setLightingPosition(lightingPos); lightingAnimationIndex.current = 0; setCurrentLightingImage(lightingAnimationFrames[lightingAnimationIndex.current]); const intervalId = setInterval(() => { lightingAnimationIndex.current += 1; if (lightingAnimationIndex.current >= lightingAnimationFrames.length) { setShowLighting(false); clearInterval(intervalId); // check collison if (CharacterAttackCollision(lightingPos, bossPosition)) { setBossHealth((prevHealth) => Math.max(prevHealth - 20, 0)); } } else { setCurrentLightingImage(lightingAnimationFrames[lightingAnimationIndex.current]); } }, 100); return () => clearInterval(intervalId); }; window.addEventListener('click', handleMouseClick); return () => window.removeEventListener('click', handleMouseClick); }, [isMagicAttack, bossPosition, bossHealth]); // Boss Attack Collision detection function const BossAttackCollision = (distance) => { const collisionThreshold = 10; return Math.abs(distance) <= collisionThreshold; }; // Character Attack collision detection const CharacterAttackCollision = (lightingPosition, bossPosition) => { const gameArea = document.querySelector('.combat-background'); if (!gameArea) { console.error('Game area not found'); return false; } const gameAreaWidth = gameArea.offsetWidth; const gameAreaLeftEdge = gameArea.getBoundingClientRect().left; const lightingPositionPercentage = ((lightingPosition.x - gameAreaLeftEdge) / gameAreaWidth) * 100; const collisionThreshold = 10; const distance = Math.abs(lightingPositionPercentage - bossPosition); return distance <= collisionThreshold; }; /* ------------- Boss Logic Start ------------ */ useEffect(() => { const bossSpeed = 0.3; // boss move speed const moveBoss = () => { const distance = bossPosition - position; // update boss position if (distance < -12) { setBossPosition(bossPosition => Math.min(bossPosition + bossSpeed, 100)); setCurrentBossImage(bossImagesRight[bossImageIndex.current % bossImagesRight.length]); } else if (distance > 6) { setBossPosition(bossPosition => Math.max(bossPosition - bossSpeed, 0)); setCurrentBossImage(bossImagesLeft[bossImageIndex.current % bossImagesLeft.length]); } bossImageIndex.current = (bossImageIndex.current + 1) % bossImagesLeft.length; // set boss attack interval if (!bossAttackCooldown && BossAttackCollision(distance)) { if (bossHealth > 0){ setHealth(prevHealth => Math.max(prevHealth - 5, 0)); setBossAttackCooldown(true); setTimeout(() => { setBossAttackCooldown(false); }, bossCooldownTime); } } }; const intervalId = setInterval(moveBoss, 80); return () => clearInterval(intervalId); }, [position, bossPosition, bossAttackCooldown, bossHealth]); /* ------------- Boss Logic End ------------- */ // check victory useEffect(() => { if (bossHealth === 0) { showCustomPopup("Victory!", "Congratulations, you have defeated the boss!", "rgba(8,186,255, 0.7)"); } }, [bossHealth]); return ( <div className="challenge-page"> <Popup show={showPopup} onClose={() => setShowPopup(false)} message={popupMessage} /> <div className="combat-background"> {/* Character Health Bar */} <div className="combat-health"> <img src="/heart.png" alt="" /> <div className="combat-health-bar"> <div className="combat-health-level" style={{ width: `${health}%` }}></div> </div> <span>{health}/100</span> </div> {/* Boss Health Bar */} <div className="boss-health"> <span>{bossHealth}/100</span> <div className="boss-health-bar"> <div className="boss-health-level" style={{ width: `${bossHealth}%` }}></div> </div> <img src="/wolf-icon.png" alt="" /> </div> {/* Character */} <div className="character" style={{ left: `${position}%`, backgroundImage: `url(${currentImage})` }}></div> {/* Boss */} {bossHealth > 0 &&( <div className="boss" style={{ left: `${bossPosition}%`, backgroundImage: `url(${currentBossImage})` }}></div> )} {/* Conditional rendering for the magic icon */} {isMagicAttack && ( <img className="use-magic-icon" src="UseLighting.png" alt="Use Lighting" style={{ position: 'absolute', left: mousePosition.x, top: mousePosition.y, transform: 'translate(-50%, -51%)' }} /> )} {/* Conditional rendering for the lighting animation */} {showLighting && ( <img className="lighting" src={currentLightingImage} alt="Lighting Animation" style={{ position: 'absolute', left: lightingPosition.x, top: lightingPosition.y, transform: 'translate(-50%, -80%)' }} /> )} </div> <div className="explain-text"> <span>Move Left <div className="keyboard-icon">A</div>/<div className="keyboard-icon">{"<"}-</div> Move Right <div className="keyboard-icon">D</div>/<div className="keyboard-icon">-{">"}</div></span> <span>Normal Attack <div className="keyboard-icon">7</div> Stick Attack <div className="keyboard-icon">8</div> Sword Attack <div className="keyboard-icon">9</div></span> <span>Open Magic State<div className="keyboard-icon">0</div>Magic Attack<div className="keyboard-icon">Mouse Click</div></span> </div> </div> ); } export default ChallengeArea; |