diff --git a/coffee/cage.coffee b/coffee/cage.coffee new file mode 100644 index 0000000..c817b80 --- /dev/null +++ b/coffee/cage.coffee @@ -0,0 +1,201 @@ +# 0000000 0000000 0000000 00000000 +# 000 000 000 000 000 +# 000 000000000 000 0000 0000000 +# 000 000 000 000 000 000 +# 0000000 000 000 0000000 00000000 + +class Cage + + constructor: (@size, @gap) -> + + cageMat = new THREE.MeshPhongMaterial + color: 0x880000 + side: THREE.FrontSide + shading: THREE.SmoothShading + shininess: 10 + emissive: 0x880000 + emissiveIntensity: 0.02 + + rasterMat = new THREE.MeshPhongMaterial + color: 0x880000 + side: THREE.FrontSide + shading: THREE.SmoothShading + shininess: 20 + + geom = @wallTiles @gap + + @cage = new THREE.Mesh geom, cageMat + @cage.translateX -0.5 + @cage.translateY -0.5 + @cage.translateZ -0.5 + world.scene.add @cage + + geom = @wallTiles 0 + + @raster = new THREE.Mesh geom, rasterMat + @raster.translateX -0.5 + @raster.translateY -0.5 + @raster.translateZ -0.5 + world.scene.add @raster + + del: -> + world.scene.remove @raster + world.scene.remove @cage + + wallTiles: (raster=0) -> + + faces = @size.x * @size.y * 2 + @size.x * @size.z * 2 + @size.y * @size.z * 2 + triangles = faces * 2 + positions = new Float32Array triangles * 3 * 3 + normals = new Float32Array triangles * 3 * 3 + + s = 1-raster + o = raster + i = -1 + offset = raster/10 + + z = offset + n = 1 + for x in [0...@size.x] + for y in [0...@size.y] + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + + z = @size.z - offset + n = -1 + for x in [0...@size.x] + for y in [0...@size.y] + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z ; normals[i] = n + + y = offset + n = 1 + for x in [0...@size.x] + for z in [0...@size.z] + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + + y = @size.y - offset + n = -1 + for x in [0...@size.x] + for z in [0...@size.z] + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x+o; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x+s; normals[i] = 0 + positions[i+=1] = y ; normals[i] = n + positions[i+=1] = z+o; normals[i] = 0 + + x = offset + n = 1 + for y in [0...@size.y] + for z in [0...@size.z] + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + + x = @size.x-offset + n = -1 + for y in [0...@size.y] + for z in [0...@size.z] + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+o; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+o; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + positions[i+=1] = x ; normals[i] = n + positions[i+=1] = y+s; normals[i] = 0 + positions[i+=1] = z+s; normals[i] = 0 + + geom = new THREE.BufferGeometry + geom.addAttribute 'position', new THREE.BufferAttribute positions, 3 + geom.addAttribute 'normal', new THREE.BufferAttribute normals, 3 + geom + +module.exports = Cage diff --git a/coffee/player.coffee b/coffee/player.coffee index 8e663e0..049329d 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -76,15 +76,18 @@ class Player extends Bot # smooth camera movement a little bit posDelta = world.getSpeed() / 10.0 playerDir = @getCurrentDir() - camPos = @current_position.plus playerDir.mul 0.4 - @projection.setPosition @projection.getPosition().mul(1.0 - posDelta).plus camPos.mul posDelta playerUp = @current_orientation.rotate(new Vector(0,1,0)).normal() + camPos = @current_position.plus playerDir.mul 0.4*(1-Math.abs(@look_angle)/90) + if @look_angle < 0 + camPos.add playerUp.mul -2*@look_angle/90 + @projection.setPosition @projection.getPosition().mul(1.0-posDelta).plus camPos.mul posDelta if @look_angle # player is looking up or down - @projection.setXVector playerUp.cross(playerDir).normal() - @look_rot = Quaternion.rotationAroundVector @look_angle, @projection.getXVector() - @projection.setYVector @look_rot.rotate playerUp - @projection.setZVector @look_rot.rotate playerDir.neg() + log "look_angle #{@look_angle}" + @projection.setXVector playerDir.cross(playerUp).normal() + rot = Quaternion.rotationAroundVector @look_angle, @projection.getXVector() + @projection.setYVector rot.rotate playerUp + @projection.setZVector rot.rotate playerDir #.neg() else # smooth camera rotation a little bit lookDelta = (2.0 - @projection.getZVector().dot playerDir) * world.getSpeed() / 50.0 @@ -113,18 +116,22 @@ class Player extends Bot botToCamera.normalize() min_f = Math.min world.getWallDistanceForRay(@current_position, botToCamera), min_f - cameraPos = @current_position.plus botToCamera.mul Math.max min_f, 0.72 - cameraPos = world.getInsideWallPosWithDelta cameraPos, 0.2 + camPos = @current_position.plus botToCamera.mul Math.max(min_f, 0.72) * (1-Math.abs(@look_angle)/90) + if @look_angle < 0 + camPos.add playerUp.mul -2*@look_angle/90 + + camPos = world.getInsideWallPosWithDelta camPos, 0.2 # smooth camera movement a little bit posDelta = 0.2 - @projection.setPosition @projection.getPosition().mul(1.0 - posDelta).plus cameraPos.mul posDelta + @projection.setPosition @projection.getPosition().mul(1.0 - posDelta).plus camPos.mul posDelta if @look_angle - @projection.setXVector playerUp.cross(playerDir).normal() - look_rot = Quaternion.rotationAroundVector @look_angle, @projection.getXVector() - @projection.setYVector look_rot.rotate playerUp - @projection.setZVector look_rot.rotate playerDir.neg() + log "look_angle #{@look_angle}" + @projection.setXVector playerDir.cross(playerUp).normal() + rot = Quaternion.rotationAroundVector @look_angle, @projection.getXVector() + @projection.setYVector rot.rotate playerUp + @projection.setZVector rot.rotate playerDir #.neg() else # smooth camera rotation a little bit lookDelta = 0.3 diff --git a/coffee/world.coffee b/coffee/world.coffee index d410e22..248eddf 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -15,6 +15,7 @@ Size = require './lib/size' Cell = require './cell' Light = require './light' Player = require './player' +Cage = require './cage' Timer = require './timer' Actor = require './actor' TmpObject = require './tmpobject' @@ -109,54 +110,6 @@ class World extends Actor @size = new Pos() @depth = -Number.MAX_SAFE_INTEGER - # 0000000 0000000 0000000 00000000 - # 000 000 000 000 000 - # 000 000000000 000 0000 0000000 - # 000 000 000 000 000 000 - # 0000000 000 000 0000000 00000000 - - initCage: -> - boxMat = new THREE.MeshPhongMaterial - color: 0x333333 - side: THREE.BackSide - shading: THREE.SmoothShading - transparent: true - opacity: 0.5 - shininess: 20 - - boxGeom = new THREE.BoxGeometry @size.x*1.01, @size.y*1.01, @size.z*1.01 - - mat = new THREE.MeshPhongMaterial - color: 0x880000 - side: THREE.FrontSide - shading: THREE.SmoothShading - transparent: false - opacity: 0.5 - shininess: 10 - - geom = new THREE.BufferGeometry - - faces = @size.x * @size.y * 2 + @size.x * @size.z * 2 + @size.y * @size.z * 2 - triangles = faces * 2 - positions = new Float32Array triangles * 3 * 3 - normals = new Float32Array triangles * 3 * 3 - @wallTiles positions, normals, @raster_size - geom.addAttribute 'position', new THREE.BufferAttribute positions, 3 - geom.addAttribute 'normal', new THREE.BufferAttribute normals, 3 - - @cage = new THREE.Mesh geom, mat - @cage.translateX -0.5 - @cage.translateY -0.5 - @cage.translateZ -0.5 - @scene.add @cage - - # @box = new THREE.Mesh boxGeom, boxMat - # @box.translateX @size.x/2-0.5 - # @box.translateY @size.y/2-0.5 - # @box.translateZ @size.z/2-0.5 - # @scene.add @box - - @init: (view) -> return if world? @@ -572,7 +525,8 @@ class World extends Actor @size = new Pos size # calcuate max distance (for position relative sound) @max_distance = Math.max(@size.x, Math.max(@size.y, @size.z)) # heuristic of a heuristic :-) - @initCage() + @cage?.del() + @cage = new Cage @size, @raster_size getCellAtPos: (pos) -> return @cells[@posToIndex(pos)] if @isValidPos pos getBotAtPos: (pos) -> @getObjectOfTypeAtPos KikiBot, new Pos pos @@ -990,149 +944,4 @@ class World extends Actor modKeyComboEventDown: (mod, key, combo, event) -> @player?.modKeyComboEventDown mod, key, combo, event - wallTiles: (positions, normals, raster=0) -> - s = 1-raster - o = raster - i = -1 - - z = 0 - n = 1 - for x in [0...@size.x] - for y in [0...@size.y] - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - - z = @size.z - n = -1 - for x in [0...@size.x] - for y in [0...@size.y] - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z ; normals[i] = n - - y = 0 - n = 1 - for x in [0...@size.x] - for z in [0...@size.z] - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - - y = @size.y - n = -1 - for x in [0...@size.x] - for z in [0...@size.z] - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x+o; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x+s; normals[i] = 0 - positions[i+=1] = y ; normals[i] = n - positions[i+=1] = z+o; normals[i] = 0 - - x = 0 - n = 1 - for y in [0...@size.y] - for z in [0...@size.z] - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - - x = @size.x - n = -1 - for y in [0...@size.y] - for z in [0...@size.z] - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+o; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+o; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - positions[i+=1] = x ; normals[i] = n - positions[i+=1] = y+s; normals[i] = 0 - positions[i+=1] = z+s; normals[i] = 0 - - module.exports = World