Files
o2o-weather/frontend/src/b/cloudMerge.ts
T

121 lines
4.7 KiB
TypeScript

import Matter from 'matter-js'
/** 작은 것부터 큰 것 순서. 같은 단계 둘이 닿으면 다음 단계로 합쳐지고, 해 둘은 사라지며 보너스 */
export const LEVELS = [
{ icon: 'muggy', name: '물방울', r: 15, color: '#d9f0ff' },
{ icon: 'snow', name: '눈송이', r: 21, color: '#e4ecff' },
{ icon: 'cloud', name: '구름', r: 28, color: '#f1f3f8' },
{ icon: 'rain', name: '비구름', r: 36, color: '#dcdcff' },
{ icon: 'umbrella', name: '우산', r: 45, color: '#eadcff' },
{ icon: 'sun_cloud', name: '해구름', r: 55, color: '#fff1c9' },
{ icon: 'moon', name: '달', r: 66, color: '#ffe6b3' },
{ icon: 'sun', name: '해', r: 78, color: '#ffd2a1' },
] as const
export const W = 360
export const H = 540
/** 이 선 위에 쌓인 채로 LOSE_MS 넘게 있으면 끝 */
export const LINE_Y = 96
export const DROP_Y = 46
const LOSE_MS = 1600
const WALL = 40
/** 떨어지는 중에만 바람을 받는다. 초속 1m 당 가로 힘 */
const WIND_FORCE = 0.000045
const MAX_WIND = 12
/** 처음 떨어뜨리는 건 작은 4단계 중에서 */
const SPAWN_MAX = 3
const SUN_BONUS = 66
/** 라운드마다 이만큼 점수를 더 내면 다음 라운드 */
export const roundTarget = (round: number) => 200 + 80 * (round - 1)
export const pointsFor = (level: number) => ((level + 1) * (level + 2)) / 2
export type Piece = Matter.Body & { level: number; falling: boolean; merged: boolean; overSince: number | null }
export type GameEvents = { onScore: (score: number) => void; onOver: () => void }
/** 바람 방향은 날짜마다 정해 하루 동안 같게: 홀수 날은 오른쪽, 짝수 날은 왼쪽 */
export const windDir = (day: number) => (day % 2 ? 1 : -1)
export const randomLevel = () => Math.floor(Math.random() * (SPAWN_MAX + 1))
export function createGame(windMs: number, dir: number, ev: GameEvents) {
const engine = Matter.Engine.create({ gravity: { x: 0, y: 1, scale: 0.0012 } })
const wind = Math.min(Math.max(windMs, 0), MAX_WIND) * dir
const opts = { isStatic: true, friction: 0.6 }
Matter.Composite.add(engine.world, [
Matter.Bodies.rectangle(W / 2, H + WALL / 2, W + WALL * 2, WALL, opts),
Matter.Bodies.rectangle(-WALL / 2, H / 2, WALL, H * 2, opts),
Matter.Bodies.rectangle(W + WALL / 2, H / 2, WALL, H * 2, opts),
])
let score = 0
let over = false
const make = (level: number, x: number, y: number) => {
const b = Matter.Bodies.circle(x, y, LEVELS[level].r, { restitution: 0.15, friction: 0.4, frictionAir: 0.012, density: 0.0016 }) as Piece
b.level = level
b.falling = false
b.merged = false
b.overSince = null
Matter.Composite.add(engine.world, b)
return b
}
const pieces = () => engine.world.bodies.filter((b): b is Piece => 'level' in b)
Matter.Events.on(engine, 'beforeUpdate', () => {
for (const p of pieces()) if (p.falling) Matter.Body.applyForce(p, p.position, { x: p.mass * wind * WIND_FORCE, y: 0 })
})
Matter.Events.on(engine, 'collisionStart', (e) => {
for (const { bodyA, bodyB } of e.pairs) {
const a = bodyA as Piece
const b = bodyB as Piece
if ('level' in a) a.falling = false
if ('level' in b) b.falling = false
if (!('level' in a) || !('level' in b) || a.level !== b.level || a.merged || b.merged) continue
a.merged = b.merged = true
Matter.Composite.remove(engine.world, [a, b])
const last = a.level === LEVELS.length - 1
score += last ? SUN_BONUS : pointsFor(a.level + 1)
if (!last) {
const c = make(a.level + 1, (a.position.x + b.position.x) / 2, (a.position.y + b.position.y) / 2)
Matter.Body.setVelocity(c, { x: 0, y: -1 })
}
ev.onScore(score)
}
})
const step = (dt: number, now: number) => {
if (over) return
Matter.Engine.update(engine, Math.min(dt, 1000 / 60))
for (const p of pieces()) {
const above = !p.falling && p.position.y - LEVELS[p.level].r < LINE_Y
p.overSince = above ? (p.overSince ?? now) : null
if (p.overSince != null && now - p.overSince > LOSE_MS) {
over = true
ev.onOver()
return
}
}
}
const drop = (level: number, x: number) => {
if (over) return
const r = LEVELS[level].r
const p = make(level, Math.min(Math.max(x, r), W - r), DROP_Y)
p.falling = true
}
/** 위험선 근처에 있는지: 선을 빨갛게 보여 줄 때 */
const danger = () => pieces().some((p) => !p.falling && p.position.y - LEVELS[p.level].r < LINE_Y + 40)
return { step, drop, pieces, danger, wind, destroy: () => Matter.Engine.clear(engine) }
}
export type Game = ReturnType<typeof createGame>
const BEST_KEY = 'cloudMerge.best'
export const loadBest = () => Number(localStorage.getItem(BEST_KEY)) || 0
export const saveBest = (v: number) => localStorage.setItem(BEST_KEY, String(v))