~nasser/lospec-2

ff8d7629b843fb639886807a6f273554386c18aa — Ramsey Nasser 6 months ago 8f385f2
Add level progression
5 files changed, 81 insertions(+), 39 deletions(-)

M blend/level.blend
M blend/level.blend1
A blend/level.glb
M gltf/level.glb
M main.js
M blend/level.blend => blend/level.blend +0 -0
M blend/level.blend1 => blend/level.blend1 +0 -0
A blend/level.glb => blend/level.glb +0 -0
M gltf/level.glb => gltf/level.glb +0 -0
M main.js => main.js +81 -39
@@ 103,15 103,8 @@ new THREE.TextureLoader().load(ls16.palette, palette => {
    composer.addPass(pass)
})

const sun = new THREE.DirectionalLight('white', 1.5)
sun.position.set(-50, 100, 70)
// SCHED.add(function* () {
//     while(true) {
//         sun.position.y += INPUT.now.time.delta
//         sun.lookAt(new THREE.Vector3(0, 0, 0))
//         yield
//     }
// })
const sun = new THREE.DirectionalLight('white', .75)
sun.position.set(-50, 200, 70)
sun.castShadow = true
sun.shadow.normalBias = 10
sun.shadow.camera.left = -500


@@ 119,6 112,16 @@ sun.shadow.camera.right = 500
sun.shadow.camera.top = -500
sun.shadow.camera.bottom = 500
scene.add(sun)

const sun2 = new THREE.DirectionalLight('blue', .275)
sun2.position.set(-50, 200, -70)
sun2.castShadow = true
sun2.shadow.normalBias = 10
sun2.shadow.camera.left = -500
sun2.shadow.camera.right = 500
sun2.shadow.camera.top = -500
sun2.shadow.camera.bottom = 500
scene.add(sun2)
scene.add(new THREE.AmbientLight('white', 1))

const helper = new THREE.CameraHelper(sun.shadow.camera)


@@ 178,36 181,42 @@ const boundaries = []
//     [310, 10],
// ])

new GLTFLoader().load(level, levelScene => {
    console.log('level', levelScene.scene)
    levelScene.scene.traverse(object => {
const levels = []

function loadLevel(levelScene, offset = new THREE.Vector2()) {
    console.log('loading level', levelScene)
    // velocityZones.length = 0
    // boundaries.length = 0
    goalZones.length = 0

    levelScene.traverse(object => {
        if (object.userData.boundary) {
            const coordinates = object.geometry.attributes.position.array

            const from = new THREE.Vector2(coordinates[0], coordinates[2])
            const from = new THREE.Vector2(coordinates[0] + offset.x, coordinates[2])
            const to = new THREE.Vector2()
            for (let i = 3; i < coordinates.length; i += 3) {
                to.x = coordinates[i + 0]
                to.y = coordinates[i + 2]
                boundaries.push({ from: from.clone(), to: to.clone() })
                boundaries.push({ from: from.clone().add(offset), to: to.clone().add(offset), exit: object.userData.exit, enabled: true })
                from.copy(to)
            }
        } else if (object.userData.velocity) {
            velocityZones.push({
                box: new THREE.Box2(
                    new THREE.Vector2(object.position.x - object.scale.x, object.position.z - object.scale.z),
                    new THREE.Vector2(object.position.x + object.scale.x, object.position.z + object.scale.z),
                    new THREE.Vector2(object.position.x - object.scale.x, object.position.z - object.scale.z).add(offset),
                    new THREE.Vector2(object.position.x + object.scale.x, object.position.z + object.scale.z).add(offset),
                ),
                velocity: new THREE.Vector2(object.userData.direction[0], object.userData.direction[1]).normalize()
            })
        } else if (object.userData.goal) {
            goalZones.push({
                box: new THREE.Box2(
                    new THREE.Vector2(object.position.x - object.scale.x, object.position.z - object.scale.z),
                    new THREE.Vector2(object.position.x + object.scale.x, object.position.z + object.scale.z),
                    new THREE.Vector2(object.position.x - object.scale.x, object.position.z - object.scale.z).add(offset),
                    new THREE.Vector2(object.position.x + object.scale.x, object.position.z + object.scale.z).add(offset),
                )
            })
        } else if (object.parent === levelScene.scene) {
        } else if (object.parent === levelScene) {
            const o = object.clone()
            o.castShadow = true
            o.receiveShadow = true


@@ 217,14 226,49 @@ new GLTFLoader().load(level, levelScene => {
                    oo.receiveShadow = true
                }
            })
            console.log('add', o)
            o.position.x += offset.x
            o.position.z += offset.y
            scene.add(o)
        } else {
            console.log('skip', object)
        }
    })
}

new GLTFLoader().load(level, file => {
    for (const level of file.scene.children) {
        levels.push(level)
    }
    SCHED.add(levelProgression)
})

function levelFinished() {
    for (const zone of goalZones)
        if (zone.complete) return true
    return false
}

function* levelProgression() {
    const exitVelocity = new THREE.Vector2(0, -1)
    // levels.shift()
    // levels.shift()
    for (let i = 0; i < levels.length; i++) {
        loadLevel(levels[i], new THREE.Vector2(0, i * -390))
        while (!levelFinished())
            yield
        console.log('done')
        for (const zone of goalZones) {
            velocityZones.push({
                box: zone.box,
                velocity: exitVelocity
            })
        }
        goalZones.length = 0
        for (const segment of boundaries) {
            if (segment.exit)
                segment.enabled = false
        }
    }
}

let sheepMesh = null
let dogMesh = null



@@ 241,15 285,17 @@ new GLTFLoader().load(art, artScene => {

SCHED.add(function* () {
    const blue = new THREE.Color(0x2929ff)
    const white = new THREE.Color('white')
    const red = new THREE.Color('red')
    const green = new THREE.Color(0x0aff0a)
    const darkGreen = new THREE.Color(0x007062)
    const orange = new THREE.Color(0xff8f00)
    const center = new THREE.Vector2()
    while (true) {
        // yield
        // continue
        yield
        continue
        for (const segment of boundaries)
            GIZMOS.line(toVector3(segment.from), toVector3(segment.to), blue)
            GIZMOS.line(toVector3(segment.from), toVector3(segment.to), !segment.enabled ? white : (segment.exit ? red : blue))
        for (const zone of velocityZones) {
            GIZMOS.line(toVector3(zone.box.min), toVector3(new THREE.Vector2(zone.box.min.x, zone.box.max.y)), green)
            GIZMOS.line(toVector3(new THREE.Vector2(zone.box.min.x, zone.box.max.y)), toVector3(zone.box.max), green)


@@ 278,7 324,7 @@ const dog = {
}

const sheepies = []
for (let i = 0; i < 70; i++) {
for (let i = 0; i < 20; i++) {
    sheepies.push({
        position: new THREE.Vector2(50 + Math.random() * 80, Math.random() * 80),
        velocity: new THREE.Vector2()


@@ 302,9 348,13 @@ function* oneSheep(sheep) {
    const matrix = new THREE.Matrix4()
    while (!sheepMesh) yield
    const mesh = sheepMesh.clone()
    scene.add(mesh)
    const line = new THREE.Line3()
    const closest = new THREE.Vector3()
    SCHED.add(function* () {
        yield* coro.seconds(1 + Math.random())
        scene.add(mesh)
    })


    while (true) {
        const oldPosition = new THREE.Vector3(sheep.position.x, 0, sheep.position.y)


@@ 350,6 400,7 @@ function* oneSheep(sheep) {

        // avoid terrain
        for (const segment of boundaries) {
            if (!segment.enabled) continue
            line.set(toVector3(segment.from), toVector3(segment.to))
            line.closestPointToPoint(toVector3(sheep.position), true, closest)
            const closest2 = toVector2(closest)


@@ 385,6 436,7 @@ for (const sheep of sheepies) {
SCHED.add(function* sheep() {
    while (true) {
        for (const zone of goalZones) {
            if (zone.complete) continue
            zone.complete = true
            for (const sheep of sheepies) {
                if (!zone.box.containsPoint(sheep.position)) {


@@ 472,18 524,8 @@ SCHED.add(function* dogMovement() {

        // avoid terrain
        for (const segment of boundaries) {
            if (intersect(segment.from, segment.to, dog.position, cursor.position, cursor.position)) {
                // console.log('bump')
                // cursor.position = segment.to
            }
            // line.set(toVector3(segment.from), toVector3(segment.to))
            // line.closestPointToPoint(toVector3(mesh.position), true, closest)
            // const closest2 = toVector2(closest)
            // const segmentDistance = mesh.position.distanceTo(closest2)
            // if (segmentDistance < 10) {
            //     const avoidSegment = new THREE.Vector2()
            //     avoidSegment.add(mesh.position.clone().sub(closest2).multiplyScalar(10 / segmentDistance))
            // }
            if (!segment.enabled) continue
            intersect(segment.from, segment.to, dog.position, cursor.position, cursor.position)
        }

        dog.position.lerp(cursor.position, INPUT.now.time.delta * 8)