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
| let init; let herald; let particles = []; const maxParticleLength = 40; let particleId = 0;
const stage = new Stage({ verticalAcceleration: new Vector(0, 0.6), initialHorizontalVelocity: new Vector(-4, 0), horizontalAcceleration: new Vector(-0.001, 0), }); let hasIntersect = false; let prevHasIntersect = false;
function isIntersectLeft(herald, platform) { if (herald.prevPosition.x + herald.width >= platform.prevPosition.x) { return false; } if (herald.prevPosition.y + herald.height >= platform.prevPosition.y) { return true; } const { x, y } = platform.prevPosition; const prevRightBottomX = herald.width + herald.prevPosition.x; const prevRightBottomY = herald.height + herald.prevPosition.y; const tx = (x - prevRightBottomX) / -stage.horizontalVelocity.x; const ty = (y - prevRightBottomY) / herald.prevVelocity.y; return ty < tx; }
function collideDetect(herald, platforms) { if (herald.position.y > stage.height) { init(); return; } let tempHasIntersect = false; for (let i = 0; i < platforms.length; i++) { if (Rect.isIntersect(herald, platforms[i])) { tempHasIntersect = true; const platform = platforms[i]; herald.velocity = new Vector(0, 0); herald.curConJump = 0; if (isIntersectLeft(herald, platform)) { init(); return; } herald.position.y = platform.position.y - herald.height; const particleSize = 8; if (!prevHasIntersect) { for (let i = 0; i < 10; i++) { const left = Math.random() > 0.5; particles[particleId % maxParticleLength] = new Particle({ velocity: left ? new Vector(random(-4, -2), random(-6, -1)) : new Vector(random(10, 16), random(-6, -1)), mass: 1, position: left ? new Vector(herald.position.x - particleSize, herald.position.y + herald.height - particleSize) : new Vector(herald.position.x + herald.width, herald.position.y + herald.height - particleSize), width: particleSize, height: particleSize, color: randomOne([herald.color, platform.color]) }); particles[particleId++ % maxParticleLength].applyForce(new Vector(0, -2)); } } else { particles[particleId % maxParticleLength] = new Particle({ velocity: new Vector(0, random(-6, -1)), mass: 1, position: new Vector(herald.position.x - particleSize, herald.position.y + herald.height - particleSize), width: particleSize, height: particleSize, color: platform.color }); particles[particleId++ % maxParticleLength].applyForce(new Vector(0, -2)); } } } hasIntersect = tempHasIntersect; if (!prevHasIntersect && hasIntersect) { stage.horizontalVelocity.add(new Vector(-2, 0)); } if (prevHasIntersect && !hasIntersect) { stage.horizontalVelocity.add(new Vector(2, 0)); } prevHasIntersect = hasIntersect; }
init = () => { stage.reset(); herald = new Herald({ position: new Vector(160, 20), height: 24, width: 24, color: "#222f3e" }); const pm = new PlatformManager({ colors: ["#1dd1a1", "#ff6b6b", "#feca57", "#54a0ff", "#9c88ff"] }); stage.play(() => { collideDetect(herald, pm.platforms); for (let particle of particles) { particle.update(stage); particle.draw(stage); } }); stage.add(herald, pm); };
document.body.addEventListener("change", function (event) { setControlType(event.target.value); });
addListener(() => { herald.jump(); }); init();
|