diff --git a/coffee/face.coffee b/coffee/face.coffee index 7b4afd4..8f7f2f3 100644 --- a/coffee/face.coffee +++ b/coffee/face.coffee @@ -16,7 +16,7 @@ class Face @NY = 4 @NZ = 5 - @orientationForFace: (face) -> + @orientation: (face) -> switch face % 6 when 0 then return Quaternion.rot_90_Y when 1 then return Quaternion.rot_270_X @@ -25,12 +25,15 @@ class Face when 4 then return Quaternion.rot_90_X when 5 then return Quaternion.rot_180_X - @normalVectorForFace: (face) -> + @normal: (face) -> o = (face < 3) and 1 or -1 switch face % 3 when 0 then return new Vector o, 0, 0 when 1 then return new Vector 0, o, 0 when 2 then return new Vector 0, 0, o new Vector + + @orientationForFace: (face) -> @orientation face + @normalVectorForFace: (face) -> @normal face module.exports = Face diff --git a/coffee/gear.coffee b/coffee/gear.coffee index e13a3af..9b4b288 100644 --- a/coffee/gear.coffee +++ b/coffee/gear.coffee @@ -24,14 +24,15 @@ class Gear extends Valve @mesh.add new THREE.Mesh Geom.valve(), Material.plate @mesh.receiveShadow = true - getNeighborGears: -> + neighborGears: -> dirs = Gear.neighbors[@face % 3] pos = @getPos() gears = [] for i in [0...4] neighbor = world.getOccupantAtPos pos.plus new Pos dirs[i] + # log "gear.neighborGears #{neighbor?} #{neighbor instanceof Gear} #{neighbor?.face}", pos.plus new Pos dirs[i] if neighbor? and neighbor instanceof Gear - if neighbor.face == face + if neighbor.face == @face gears.push neighbor gears @@ -49,22 +50,30 @@ class Gear extends Valve @updateActive() updateActive: -> - @setActive false - for gear in @getNeighborGears() + # log "gear.updateActive #{@active}" + for gear in @neighborGears() + log "gear.updateActive neighbor active #{gear.active}" if gear.active @setActive true return + @setActive false setActive: (active) -> if @active != active @active = active world.playSound @active and 'GEAR_ON' or 'GEAR_OFF' - - for gear in @getNeighborGears() + if @active + @startTimedAction @getActionWithId Action.ROTATE + else + @stopAction @getActionWithId Action.ROTATE + log "gear.setActive neighborGears #{@neighborGears().length}" + for gear in @neighborGears() if @active + log 'gear.setActive activate neighbor' gear.setActive true else + log 'gear.setActive update neighbor' gear.updateActive() module.exports = Gear diff --git a/coffee/generator.coffee b/coffee/generator.coffee index f1bdbbc..15707dc 100644 --- a/coffee/generator.coffee +++ b/coffee/generator.coffee @@ -11,8 +11,7 @@ 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 +20,6 @@ class Generator extends Gear activateWires: -> wires = world.getObjectsOfTypeAtPos Wire, @getPos() - for wire in wires wire.setActive active diff --git a/coffee/levels/test.coffee b/coffee/levels/test.coffee index 7307086..86d2845 100644 --- a/coffee/levels/test.coffee +++ b/coffee/levels/test.coffee @@ -38,11 +38,11 @@ module.exports = # 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 - return 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), 6, 4, 0 + return world.addObjectAtPos new Gear(Face.NZ), 6, 5, 4 world.addObjectAtPos new Gear(Face.NZ), 4, 5, 4 diff --git a/coffee/motorgear.coffee b/coffee/motorgear.coffee index 6cff650..72ba8e4 100644 --- a/coffee/motorgear.coffee +++ b/coffee/motorgear.coffee @@ -4,9 +4,13 @@ # 000 0 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 # 000 000 0000000 000 0000000 000 000 0000000 00000000 000 000 000 000 -Gear = require './gear' -Geom = require './geom' -Material = require './material' +log = require '/Users/kodi/s/ko/js/tools/log' +Gear = require './gear' +Geom = require './geom' +Face = require './face' +Material = require './material' +Quaternion = require './lib/quaternion' +MotorCylinder = require './motorcylinder' class MotorGear extends Gear @@ -15,8 +19,19 @@ class MotorGear extends Gear @setActive true createMesh: -> - @mesh = new THREE.Mesh Geom.gear(), Material.gear - @mesh.add new THREE.Mesh Geom.motor(), Material.plate + @mesh = new THREE.Mesh Geom.motor(), Material.plate + @gear = new THREE.Mesh Geom.gear(), Material.gear + @mesh.add @gear @mesh.receiveShadow = true + + 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 + + updateActive: -> + pos = @position.plus Face.normal @face + # log "MotorGear.updateActive #{@active}", pos, world.getOccupantAtPos(pos) instanceof MotorCylinder + @setActive world.getOccupantAtPos(pos) instanceof MotorCylinder module.exports = MotorGear diff --git a/coffee/sound.coffee b/coffee/sound.coffee index 4357dfc..1d125ed 100644 --- a/coffee/sound.coffee +++ b/coffee/sound.coffee @@ -53,10 +53,13 @@ class Sound src: ["#{__dirname}/../sound/#{v.file}"] volume: v.volume @sounds[k].pannerAttr - maxDistance: 10 - refDistance: 1 - rolloffFactor: 4 - distanceModel: 'exponential' + coneInnerAngle: 360 + coneOuterAngle: 360 + coneOuterGain: 0 + maxDistance: 10 + refDistance: 1 + rolloffFactor: 4 + distanceModel: 'exponential' @setMatrix: (m) -> p = m.getPosition() @@ -67,7 +70,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 3251904..2c0f502 100644 --- a/coffee/wire.coffee +++ b/coffee/wire.coffee @@ -54,21 +54,21 @@ class Wire extends Item plane.translate 0, -w/2, -s+o geom.merge plane - @wire = new THREE.Mesh geom, Material.wire + @wire = new THREE.Mesh geom, Material.wire @mesh = new THREE.Mesh Geom.wire(), Material.plate @mesh.add @wire @mesh.receiveShadow = true - @mesh.position.copy Face.normalVectorForFace(@face).mul -(0.5+o) - @mesh.quaternion.copy Face.orientationForFace @face + @mesh.position.copy Face.normal(@face).mul -(0.5+o) + @mesh.quaternion.copy Face.orientation @face updateActive: -> - for wire in @getNeighborWires() + for wire in @neighborWires() @setActive true if wire.active setActive: (active) -> if @active != active @active = active - neighbors = @getNeighborWires() + neighbors = @neighborWires() active_neighbor = false if @active @@ -87,9 +87,9 @@ class Wire extends Item @events[@active and @SWITCH_ON_EVENT or @SWITCH_OFF_EVENT].triggerActions() @events[@SWITCHED_EVENT].triggerActions() - getNeighborWires: -> + neighborWires: -> wires = [] - points = @getConnectionPoints() + points = @connectionPoints() neighbor_dirs = [] rot = Face.orientationForFace @face @@ -114,14 +114,14 @@ class Wire extends Item neighbors = world.getObjectsOfTypeAtPos Wire, @position.plus neighbor_dirs[i] for iter in neighbors continue if iter == @ - neighbor_points = iter.getConnectionPoints() + neighbor_points = iter.connectionPoints() for point in points for neighbor_point in neighbor_points if (neighbor_point.minus point).length() < 0.1 wires.push iter wires - getConnectionPoints: -> + connectionPoints: -> points = [] to_border = Face.normalVectorForFace(@face).mul 0.5 rot = Face.orientationForFace @face diff --git a/coffee/world.coffee b/coffee/world.coffee index 7981fea..a956177 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -243,22 +243,8 @@ class World extends Actor else if @dict.player.coordinates? @addObjectAtPos @player, new Pos @dict.player.coordinates - # if player_dict.nostatus? - # if player_dict.nostatus or @preview - # @player_status.hide() - # else - # @player_status.show() - # else - # if @preview - # Controller.player_status.hide() - # else - # Controller.player_status.show() -# @getProjection().setPosition new Vector 0,0,0 - # @player.getStatus().setMinMoves (highscore.levelParMoves (@level_name)) - # @player.getStatus().setMoves (0) - # ............................................................ init # @init() # tell the world that we are finished @@ -301,7 +287,6 @@ class World extends Actor exitLevel: (action) => log "world.exitLevel", action @finish() - # @player.status.setMoves 0 # exitIndex = parseInt action.name?.slice 5 # log "world.exitLevel exitIndex:#{exitIndex}" # if @dict.exits[exitIndex]?.world? @@ -311,10 +296,7 @@ class World extends Actor log "world.level_index #{world.level_index} nextLevel #{World.levels.list[world.level_index+1]}" world.create World.levels.list[world.level_index+1] - activate: (objectName) -> - # activates object with name objectName - object = @getObjectWithName objectName - object?.setActive? 1 + activate: (objectName) -> @getObjectWithName(objectName)?.setActive? true decenter: (x,y,z) -> new Pos(x,y,z).plus @size.div 2 @@ -493,7 +475,6 @@ class World extends Actor moveObjectToPos: (object, pos) -> return false if @isInvalidPos(pos) or @isOccupiedPos(pos) - @unsetObject object @setObjectAtPos object, pos world.playSound 'BOT_LAND' @@ -517,7 +498,6 @@ class World extends Actor object.del() deleteAllObjects: () -> - # log 'world.deleteAllObjects' Timer.removeAllActions() if @player? @@ -551,10 +531,7 @@ class World extends Actor setCameraMode: (mode) -> @camera_mode = clamp World.CAMERA_INSIDE, World.CAMERA_FOLLOW, mode - changeCameraMode: () -> - @camera_mode = (@camera_mode+1) % (World.CAMERA_FOLLOW+1) - # log "world.changeCameraMode #{@camera_mode}" - @camera_mode + changeCameraMode: -> @camera_mode = (@camera_mode+1) % (World.CAMERA_FOLLOW+1) # 0000000 0000000 000 00 00 0000000 000 000 00000000 # 000 000 000 000 000 000 000 000 000 000 000 000 @@ -766,7 +743,6 @@ class World extends Actor if o instanceof Light o.initialize() - # Spikes::initialize() # Text::reinit() # 000 000 00000000 000 00000000