diff --git a/coffee/bot.coffee b/coffee/bot.coffee index 60ad7cb..132a2c1 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -60,7 +60,7 @@ class Bot extends Pushable transparent: true opacity: 1 shininess: 4 - alphaTest: 0.1 + # alphaTest: 0.1 @leftTire = new THREE.Mesh geom, @tireMat @leftTire.position.set 0.35,0,0 diff --git a/coffee/gear.coffee b/coffee/gear.coffee index 9b4b288..5c1539c 100644 --- a/coffee/gear.coffee +++ b/coffee/gear.coffee @@ -51,12 +51,12 @@ class Gear extends Valve updateActive: -> # log "gear.updateActive #{@active}" + @setActive false for gear in @neighborGears() - log "gear.updateActive neighbor active #{gear.active}" + # log "gear.updateActive neighbor active #{gear.active}" if gear.active @setActive true return - @setActive false setActive: (active) -> if @active != active @@ -67,13 +67,13 @@ class Gear extends Valve @startTimedAction @getActionWithId Action.ROTATE else @stopAction @getActionWithId Action.ROTATE - log "gear.setActive neighborGears #{@neighborGears().length}" + # log "gear.setActive neighborGears #{@neighborGears().length}" for gear in @neighborGears() if @active - log 'gear.setActive activate neighbor' + # log 'gear.setActive activate neighbor' gear.setActive true else - log 'gear.setActive update neighbor' + # log 'gear.setActive update neighbor' gear.updateActive() module.exports = Gear diff --git a/coffee/generator.coffee b/coffee/generator.coffee index 15707dc..e36de81 100644 --- a/coffee/generator.coffee +++ b/coffee/generator.coffee @@ -11,7 +11,8 @@ Material = require './material' class Generator extends Gear - constructor: (face) -> super face + constructor: (face) -> + super face createMesh: -> @mesh = new THREE.Mesh Geom.generator(), Material.plate @@ -21,7 +22,7 @@ class Generator extends Gear activateWires: -> wires = world.getObjectsOfTypeAtPos Wire, @getPos() for wire in wires - wire.setActive active + wire.setActive @active setActive: (active) -> if @active != active diff --git a/coffee/geom.coffee b/coffee/geom.coffee index 2dc212c..934749f 100644 --- a/coffee/geom.coffee +++ b/coffee/geom.coffee @@ -66,7 +66,7 @@ class Geom geom @motor: -> - quads = 11 + quads = 12 triangles = quads*2+4 positions = new Float32Array triangles*9 normals = new Float32Array triangles*9 @@ -453,7 +453,8 @@ class Geom 0.167, -0.5, 0.29, -0.167, -0.5, 0.29, -0.5, -0.5, 0.027, 0.5, -0.5, 0.027, -0.167, 0.5, 0.29, 0.167, 0.5, 0.29, 0.5, 0.5, 0.027, -0.5, 0.5, 0.027, 0.167, 0.5, 0.29, -0.167, 0.5, 0.29, -0.5, 0.167, 0.29, 0.5, 0.167, 0.29, - 0.5, -0.167, 0.29, 0.5, -0.5, 0.027, 0.5, 0.5, 0.027, 0.5, 0.167, 0.290 + 0.5, -0.167, 0.29, 0.5, -0.5, 0.027, 0.5, 0.5, 0.027, 0.5, 0.167, 0.290, + -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5 ] # 0000000 00000000 000 000 00000000 00000000 0000000 000000000 0000000 00000000 diff --git a/coffee/levels/test.coffee b/coffee/levels/test.coffee index 86d2845..d03fd5b 100644 --- a/coffee/levels/test.coffee +++ b/coffee/levels/test.coffee @@ -9,7 +9,7 @@ module.exports = name: "test" size: [11,11,11] player: - coordinates: [5,5,1] + coordinates: [5,7,1] exits: [ name: "exit" active: 0 @@ -20,28 +20,30 @@ module.exports = s = world.size {Gear,Generator,MotorCylinder,MotorGear,Face,Wall,Wire,WireStone,Stone} = require '../items' - # world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 7, 5, 0 - # world.addObjectAtPos new Wire(Face.NZ, Wire.ALL), 7, 5, 0 - # world.addObjectAtPos new Wire(Face.Y, Wire.ALL), 7, 5, 0 - # world.addObjectAtPos new Wire(Face.NY, Wire.ALL), 7, 5, 0 - # world.addObjectAtPos new Wire(Face.X, Wire.ALL), 7, 5, 0 - # world.addObjectAtPos new Wire(Face.NX, Wire.ALL), 7, 5, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 5, 7, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 5, 8, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 6, 7, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 5, 6, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 4, 7, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 5, 8, 0 + world.addObjectAtPos new Wire(Face.Z, Wire.ALL), 5, 5, 0 - world.addObjectAtPos new Generator(Face.Z), 5, 7, 0 - world.addObjectAtPos new Stone(), 6, 7, 0 - world.addObjectAtPos new Stone(), 6, 7, 2 - world.addObjectAtPos new WireStone(), 6, 7, 1 - world.addObjectAtPos new WireStone(), 3, 7, 1 + world.addObjectAtPos new Generator(Face.Z), 5, 6, 0 + # world.addObjectAtPos new MotorGear(Face.NZ), 8, 7, 0 + # world.addObjectAtPos new Stone(), 6, 7, 0 + # world.addObjectAtPos new Stone(), 6, 7, 2 + # world.addObjectAtPos new WireStone(), 6, 7, 1 + # world.addObjectAtPos new WireStone(), 3, 7, 1 - world.addObjectAtPos new MotorCylinder(Face.Z), 4, 5, 1 - world.addObjectAtPos new MotorGear(Face.Z), 4, 5, 0 - # world.addObjectAtPos new Gear(Face.Z), 6, 5, 0 + world.addObjectAtPos new Gear(Face.Z), 6, 5, 0 # world.addObjectAtPos new Gear(Face.Z), 5, 6, 0 # world.addObjectAtPos new Gear(Face.Z), 5, 4, 0 - world.addObjectAtPos new Gear(Face.Z), 6, 6, 0 + # world.addObjectAtPos new Gear(Face.Z), 6, 6, 0 world.addObjectAtPos new Gear(Face.Z), 4, 4, 0 - world.addObjectAtPos new Gear(Face.Z), 4, 6, 0 + # world.addObjectAtPos new Gear(Face.Z), 4, 6, 0 world.addObjectAtPos new Gear(Face.Z), 6, 4, 0 + world.addObjectAtPos new MotorCylinder(Face.Z), 4, 5, 1 + world.addObjectAtPos new MotorGear(Face.Z), 4, 5, 0 return world.addObjectAtPos new Gear(Face.NZ), 6, 5, 4 diff --git a/coffee/material.coffee b/coffee/material.coffee index 65ef572..75ac4ad 100644 --- a/coffee/material.coffee +++ b/coffee/material.coffee @@ -18,6 +18,12 @@ module.exports = side: THREE.DoubleSide shading: THREE.SmoothShading shininess: 40 + + wire_plate: new THREE.MeshPhongMaterial + color: 0x880000 + side: THREE.DoubleSide + shading: THREE.SmoothShading + shininess: 10 raster: new THREE.MeshPhongMaterial color: 0x880000 diff --git a/coffee/motorcylinder.coffee b/coffee/motorcylinder.coffee index a3224ef..f352559 100644 --- a/coffee/motorcylinder.coffee +++ b/coffee/motorcylinder.coffee @@ -4,24 +4,27 @@ # 000 0 000 000 000 000 000 000 000 000 000 000 000 000 000 0000 000 000 000 000 000 # 000 000 0000000 000 0000000 000 000 0000000 000 0000000 000 000 000 0000000 00000000 000 000 -log = require '/Users/kodi/s/ko/js/tools/log' -Item = require './item' -Action = require './action' -Face = require './face' -Geom = require './geom' -Material = require './material' +Item = require './item' +Action = require './action' +Face = require './face' +Geom = require './geom' +Pushable = require './pushable' +Material = require './material' -class MotorCylinder extends Item +class MotorCylinder extends Pushable #Item isSpaceEgoistic: -> true - constructor: (face) -> - super + constructor: (@face) -> + super() @value = 0.0 @active = false @orientation = Face.orientationForFace @face @addAction new Action @, Action.TUCKER, "tucker", 500, Action.REPEAT - @setActive true + + setPosition: (pos) -> + super pos + @updateActive() createMesh: -> @@ -30,8 +33,10 @@ class MotorCylinder extends Item @mesh.add @kolben @mesh.receiveShadow = true - updateMesh: -> @kolben.position.set 0, 0, -0.5 * Math.sin(@value) - + updateMesh: -> + @kolben.position.set 0, 0, -0.5 * Math.sin @value + @mesh.quaternion.copy Face.orientation @face + setActive: (active) -> if @active != active @active = active @@ -40,15 +45,41 @@ class MotorCylinder extends Item else @stopAction @getActionWithId Action.TUCKER + initAction: (action) -> + if action.id in [Action.PUSH, Action.FALL] + @setActive false + pos = @position.minus Face.normal @face + occupant = world.getOccupantAtPos pos + MotorGear = require './motorgear' + isGear = occupant instanceof MotorGear and occupant.face == @face + log "initAction isGear #{isGear}" + occupant.setActive false if isGear + super action + performAction: (action) -> if action.id == Action.TUCKER relTime = action.getRelativeTime() @value = if relTime > 0.5 then 1.0 - relTime else relTime @value *= 2 @updateMesh() + return + super action finishAction: (action) -> if action.id == Action.TUCKER world.playSound 'MOTOR', @getPos() + return + super action + if action.id in [Action.PUSH, Action.FALL] + @updateActive() + updateActive: -> + pos = @position.minus Face.normal @face + occupant = world.getOccupantAtPos pos + MotorGear = require './motorgear' + isGear = occupant instanceof MotorGear and occupant.face == @face + # log "isGear #{isGear}" + @setActive isGear + occupant.setActive true if isGear + module.exports = MotorCylinder diff --git a/coffee/motorgear.coffee b/coffee/motorgear.coffee index 72ba8e4..0f95a85 100644 --- a/coffee/motorgear.coffee +++ b/coffee/motorgear.coffee @@ -8,15 +8,19 @@ log = require '/Users/kodi/s/ko/js/tools/log' Gear = require './gear' Geom = require './geom' Face = require './face' +Action = require './action' Material = require './material' Quaternion = require './lib/quaternion' MotorCylinder = require './motorcylinder' class MotorGear extends Gear - constructor: (face) -> - super face - @setActive true + constructor: (@face) -> + super @face + + setPosition: (pos) -> + super pos + @updateActive() createMesh: -> @mesh = new THREE.Mesh Geom.motor(), Material.plate @@ -24,14 +28,27 @@ class MotorGear extends Gear @mesh.add @gear @mesh.receiveShadow = true + initAction: (action) -> + # log "MotorGear.initAction action #{action.name}" + if action.id in [Action.PUSH, Action.FALL] + pos = @position.plus Face.normal @face + occupant = world.getOccupantAtPos pos + isCylinder = occupant instanceof MotorCylinder and occupant.face == @face + occupant.setActive false if isCylinder + super action + updateMesh: -> # log "Valve.updateMesh #{@angle} #{@face}" rot = Quaternion.rotationAroundVector (@clockwise and 1 or -1) * @angle, 0,0,1 - @gear.quaternion.copy Face.orientationForFace(@face).mul rot + @gear.quaternion.copy rot #Face.orientationForFace(@face).mul rot + @mesh.quaternion.copy Face.orientation @face updateActive: -> pos = @position.plus Face.normal @face # log "MotorGear.updateActive #{@active}", pos, world.getOccupantAtPos(pos) instanceof MotorCylinder - @setActive world.getOccupantAtPos(pos) instanceof MotorCylinder + occupant = world.getOccupantAtPos pos + isCylinder = occupant instanceof MotorCylinder and occupant.face == @face and not occupant.move_action + @setActive isCylinder + occupant.setActive true if isCylinder module.exports = MotorGear diff --git a/coffee/player.coffee b/coffee/player.coffee index 7078b07..78a0ae9 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -164,9 +164,10 @@ class Player extends Bot botToCamera = camPos.minus playerPos botToCameraNormal = botToCamera.normal() - # if camera below bot, rotate up - if botToCameraNormal.dot(playerUp) < 0 # calculate angle between player to camera vector and player up vector - verticalAngle = Vector.RAD2DEG Math.acos(clamp(-1.0, 1.0, botToCameraNormal.dot playerUp)) + # rotate camera vertically + verticalAngle = Vector.RAD2DEG Math.acos(clamp(-1.0, 1.0, botToCameraNormal.dot playerUp)) + if verticalAngle > 45 + # log "verticalAngle #{verticalAngle}" rotQuat = Quaternion.rotationAroundVector(verticalAngle/400.0, botToCameraNormal.cross(playerUp)) botToCamera = rotQuat.rotate botToCamera botToCameraNormal = botToCamera.normal() diff --git a/coffee/sound.coffee b/coffee/sound.coffee index 1d125ed..c5acbe3 100644 --- a/coffee/sound.coffee +++ b/coffee/sound.coffee @@ -53,13 +53,14 @@ class Sound src: ["#{__dirname}/../sound/#{v.file}"] volume: v.volume @sounds[k].pannerAttr - coneInnerAngle: 360 - coneOuterAngle: 360 - coneOuterGain: 0 - maxDistance: 10 - refDistance: 1 - rolloffFactor: 4 - distanceModel: 'exponential' + coneInnerAngle: 360 + coneOuterAngle: 360 + coneOuterGain: 0 + maxDistance: 10 + refDistance: 1 + rolloffFactor: 4 + distanceModel: 'exponential' + panningModel: 'HRTF' @setMatrix: (m) -> p = m.getPosition() @@ -70,7 +71,7 @@ class Sound @play: (sound, pos, time) -> pos ?= world.player?.current_position - log "Sound.play #{sound} #{time}", pos + # log "Sound.play #{sound} #{time}", pos id = @sounds[sound].play() @sounds[sound].pos pos.x, pos.y, pos.z, id if pos? diff --git a/coffee/wire.coffee b/coffee/wire.coffee index 2c0f502..c9d38e8 100644 --- a/coffee/wire.coffee +++ b/coffee/wire.coffee @@ -7,6 +7,7 @@ Item = require './item' Geom = require './geom' Face = require './face' +Gate = require './gate' Vector = require './lib/vector' Material = require './material' @@ -21,9 +22,11 @@ class Wire extends Item @ALL =15 constructor: (@face=Face.Z, @connections=Wire.ALL) -> - super + @glow = null @active = false @value = 1.0 + + super @SWITCH_OFF_EVENT = @addEventWithName "off" @SWITCH_ON_EVENT = @addEventWithName "on" @@ -53,9 +56,9 @@ class Wire extends Item plane = new THREE.PlaneGeometry h, w plane.translate 0, -w/2, -s+o geom.merge plane - + @wire = new THREE.Mesh geom, Material.wire - @mesh = new THREE.Mesh Geom.wire(), Material.plate + @mesh = new THREE.Mesh Geom.wire(), Material.wire_plate @mesh.add @wire @mesh.receiveShadow = true @mesh.position.copy Face.normal(@face).mul -(0.5+o) @@ -68,21 +71,63 @@ class Wire extends Item setActive: (active) -> if @active != active @active = active + log "wire active #{active}" neighbors = @neighborWires() + + for wire in neighbors + wire.setActive @active + + # active_neighbor = false + # if @active + # for wire in neighbors + # if wire.active + # active_neighbor = true + # break +# + # for wire in wires + # wire.setActive active - active_neighbor = false + gate = world.getObjectOfTypeAtPos Gate, @getPos() + gate?.setActive @active + if @active - for wire in neighbors - if wire.active - active_neighbor = true - break - - for wire in wires - wire.setActive active - - cellSwitch = world.getObjectOfTypeAtPos KikiSwitch, @getPos() - if cellSwitch? - cellSwitch.setActive active + if not @glow? + map = new THREE.TextureLoader().load "#{__dirname}/../img/wire.png" + # map.offset.set -0.5, -0.5 + # map.repeat.set 2, 2 + material = new THREE.SpriteMaterial + map: map + color: 0xffff00 + transparent: false + # opacity: 0.95 + blending: THREE.AdditiveBlending + fog: true + id: 999 + lights: true + # side: THREE.DoubleSide + # depthTest: false + # depthWrite: true + + @glow = new THREE.Sprite material + # @glow.scale.set 0.1, 0.1, 0.1 + log 'glow position', @position + @glow.position.set 0, 0, -0.3 + @glow.scale.set .5, .5, 1 + @glow.renderOrder = 999 + # @glow.position.normalize() + # @glow.position.multiplyScalar 2 + @mesh.add @glow + + # @glow2 = new THREE.Sprite material + # @glow2.scale.set 1, 1, 1 + # @glow2.renderOrder = 999 + # @glow2.position.set @position.x, @position.y, @position.z-0.3 + # world.scene.add @glow2 + else if @glow + @mesh.remove @glow + log 'remove glow' + # @world.scene.remove @glow + @glow = null @events[@active and @SWITCH_ON_EVENT or @SWITCH_OFF_EVENT].triggerActions() @events[@SWITCHED_EVENT].triggerActions() diff --git a/coffee/world.coffee b/coffee/world.coffee index a956177..bcb6edb 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -69,14 +69,14 @@ class World extends Actor @renderer = new THREE.WebGLRenderer antialias: true - logarithmicDepthBuffer: true + logarithmicDepthBuffer: false autoClear: true sortObjects: true - + @renderer.setClearColor 0x000000 @renderer.setSize @view.offsetWidth, @view.offsetHeight @renderer.shadowMap.type = THREE.PCFSoftShadowMap - + # 0000000 0000000 00 00 00000000 00000000 0000000 # 000 000 000 000 000 000 000 000 000 000 # 000 000000000 000000000 0000000 0000000 000000000 @@ -140,7 +140,7 @@ class World extends Actor return if @levels? Sound.init() - + global.log = log global.rot0 = Quaternion.rot_0 global.rotx90 = Quaternion.rot_90_X global.roty90 = Quaternion.rot_90_Y @@ -409,7 +409,7 @@ class World extends Actor getObjectsOfType: (clss) -> @objects.filter (o) -> o instanceof clss getObjectsOfTypeAtPos: (clss, pos) -> @getCellAtPos(pos)?.getObjectsOfType(clss) ? [] - getObjectOfTypeAtPos: (clss, pos) -> @getCellAtPos(pos)?.getRealObjectOfType(clss) ? [] + getObjectOfTypeAtPos: (clss, pos) -> @getCellAtPos(pos)?.getRealObjectOfType(clss) getOccupantAtPos: (pos) -> @getCellAtPos(pos)?.getOccupant() getRealOccupantAtPos: (pos) -> occupant = @getOccupantAtPos pos diff --git a/img/glow.png b/img/glow.png new file mode 100644 index 0000000..8d54d5b Binary files /dev/null and b/img/glow.png differ diff --git a/img/shot03.png b/img/shot03.png new file mode 100644 index 0000000..f3afd45 Binary files /dev/null and b/img/shot03.png differ diff --git a/img/wire.png b/img/wire.png new file mode 100644 index 0000000..a762a31 Binary files /dev/null and b/img/wire.png differ diff --git a/img/wire2.png b/img/wire2.png new file mode 100644 index 0000000..c7a2417 Binary files /dev/null and b/img/wire2.png differ