diff --git a/coffee/actor.coffee b/coffee/actor.coffee index 0178935..44737a2 100644 --- a/coffee/actor.coffee +++ b/coffee/actor.coffee @@ -62,6 +62,7 @@ class Actor extends Emitter return a if action.name = name addEventWithName: (eventName) -> + log "Actor.addEventWithName eventName:#{eventName}" if @getEventWithName eventName # to be removed log "Actor.addEventWithName '#{eventName}' already in use!" return -1; # shouldn't happen anyway :-) diff --git a/coffee/cell.coffee b/coffee/cell.coffee index 42bf132..9081234 100644 --- a/coffee/cell.coffee +++ b/coffee/cell.coffee @@ -5,6 +5,8 @@ # 000 000 000 000 # 0000000 00000000 0000000 0000000 +TmpObject = require './tmpobject' + class Cell constructor: () -> @objects = [] @@ -12,7 +14,7 @@ class Cell getObjectsOfType: (clss) -> @objects.filter (o) -> o instanceof clss getObjectOfType: (clss) -> _.find @objects, (o) -> o instanceof clss - getRealObjectOfType: (clss) -> _.find @objects, (o) -> o instanceof clss or o instanceof KikiTmpObject and o.object instanceof clss + getRealObjectOfType: (clss) -> _.find @objects, (o) -> o instanceof clss or o instanceof TmpObject and o.object instanceof clss getOccupant: -> _.find @objects, (o) -> o.isSpaceEgoistic() removeObject: (object) -> diff --git a/coffee/item.coffee b/coffee/item.coffee index f82260d..1f8d280 100644 --- a/coffee/item.coffee +++ b/coffee/item.coffee @@ -8,6 +8,7 @@ log = require '/Users/kodi/s/ko/js/tools/log' Actor = require './actor' Vector = require './lib/vector' +Quat = require './lib/quaternion' Pos = require './lib/pos' class Item extends Actor @@ -29,26 +30,17 @@ class Item extends Actor isSpaceEgoistic: -> true isSlippery: -> false - setPosition: (p) -> - @position = @current_position = p - if @mesh? - log 'setPosition', @mesh.position - # @mesh.position = new THREE.Vector3 @position.x, @position.y, @position.z - # @mesh.matrixWorldNeedsUpdate = true - # @mesh.updateMatrix() - # @mesh.updateMatrixWorld true - @mesh.translateX @position.x - @mesh.translateY @position.y - @mesh.translateZ @position.z - log 'setPosition', p, @mesh.position + setPosition: (x,y,z) -> + @position = @current_position = new Vector x, y, z + @mesh?.position.copy @position getPos: -> new Pos @current_position - getPosition: -> @position - getOrientation: -> @orientation - getCurrentPosition: -> @current_position - getCurrentOrientation: -> @current_orientation - setOrientation: (q) -> @current_orientation = @orientation = q + # getPosition: -> @position + # getOrientation: -> @orientation + # getCurrentPosition: -> @current_position + # getCurrentOrientation: -> @current_orientation + setOrientation: (q) -> @current_orientation = @orientation = new Quat q setCurrentPosition: (p) -> @current_position = p setCurrentOrientation: (q) -> @current_orientation = q diff --git a/coffee/lib/matrix.coffee b/coffee/lib/matrix.coffee index 344e5c1..f0e795f 100644 --- a/coffee/lib/matrix.coffee +++ b/coffee/lib/matrix.coffee @@ -12,22 +12,18 @@ class Matrix constructor: (o) -> @matrix = [] - - if o instanceof Matrix - @copy o - - else if o instanceof Array - if o.length == 16 - for i in [0...16] - @matrix[i] = o[i] - - else if o?.x? and o?.y? and o?.z? - @initXYZ o.x, o.y, o.z - - else if o instanceof Quaternion - @initQuat o - else - @reset() + + switch + when not o? then @reset() + when o instanceof Quaternion then @initQuat o + when o instanceof Matrix then @copy o + when o instanceof Array + if o.length == 16 + for i in [0...16] + @matrix[i] = o[i] + when o?.x? and o?.y? and o?.z? + @initXYZ o.x, o.y, o.z + else @reset() initXYZ: (x,y,z) -> @matrix[0] = x.x diff --git a/coffee/perspective.coffee b/coffee/perspective.coffee index 552af0f..83828b6 100644 --- a/coffee/perspective.coffee +++ b/coffee/perspective.coffee @@ -9,7 +9,7 @@ Matrix = require './lib/matrix' class Perspective extends Matrix - constructor: (fov,near,far) -> + constructor: (fov,near=0.1,far=10000) -> @znear = near @zfar = far @fov = fov @@ -19,8 +19,9 @@ class Perspective extends Matrix @setViewport 0.0, 0.0, 1.0, 1.0 # WINDOW_SIZE_CHANGED -> updateViewport super + log "Perspective #{@fov} #{@znear} #{@zfar}" - reset: () -> + reset: -> @fov = 60.0 @eye_distance = @znear super @@ -65,7 +66,7 @@ class Perspective extends Matrix up = @getYVector() lookAt = @getLookAtPosition() - log "Perspective.apply", camPos, up, lookAt + # log "Perspective.apply", @matrix #camPos, up, lookAt camera.position.clone camPos #set camPos.x, camPos.y, camPos.z camera.up.clone up #new THREE.Vector3 up.x, up.y, up.z diff --git a/coffee/player.coffee b/coffee/player.coffee index b246d67..341a53d 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -5,6 +5,7 @@ # 000 000 000 000 000 000 000 000 # 000 0000000 000 000 000 00000000 000 000 +log = require '/Users/kodi/s/ko/js/tools/log' Bot = require './bot' Action = require './action' Timer = require './timer' @@ -49,43 +50,41 @@ class Player extends Bot # @projection.getLight().setCutoff 90.0 # @projection.getLight().setAttenuation 1.0, 0.0, 0.05 - getActionForKey: (keyName) -> - index = 0 - while actionKeyMapping[index].actionName - if keyName == actionKeyMapping[index].keyName - return actionKeyMapping[index].actionName - index++ - - return "" + # getActionForKey: (keyName) -> + # index = 0 + # while actionKeyMapping[index].actionName + # if keyName == actionKeyMapping[index].keyName + # return actionKeyMapping[index].actionName + # index += 1 + # return '' +# + # getKeyForAction: (actionName) -> + # index = 0 + # while actionKeyMapping[index].actionName + # if actionName == actionKeyMapping[index].actionName + # return actionKeyMapping[index].keyName + # index += 1 + # return '' +# + # setKeyForAction: (keyName, actionName) -> + # index = 0 + # while actionKeyMapping[index].actionName + # if actionName == actionKeyMapping[index].actionName + # actionKeyMapping[index].keyName = keyName + # index += 1 - getKeyForAction: (actionName) -> - index = 0 - while actionKeyMapping[index].actionName - if actionName == actionKeyMapping[index].actionName - return actionKeyMapping[index].keyName - index++ - - return "" - - setKeyForAction: (keyName, actionName) -> - index = 0 - while actionKeyMapping[index].actionName - if actionName == actionKeyMapping[index].actionName - actionKeyMapping[index].keyName = keyName - index++ - - recordKeyForAction: (actionName) -> - RecordingActionName = actionName - KeyRecorder.startRecordingSequence @, @setRecordedKey, 1 - - setRecordedKey: (keyName) -> - index = 0 - while actionKeyMapping[index].actionName - if keyName == actionKeyMapping[index].keyName and actionKeyMapping[index].actionName != RecordingActionName - setKeyForAction "", actionKeyMapping[index].actionName - index += 1 - setKeyForAction keyName, RecordingActionName - getEventWithName("keyset").triggerActions() + # recordKeyForAction: (actionName) -> + # RecordingActionName = actionName + # KeyRecorder.startRecordingSequence @, @setRecordedKey, 1 +# + # setRecordedKey: (keyName) -> + # index = 0 + # while actionKeyMapping[index].actionName + # if keyName == actionKeyMapping[index].keyName and actionKeyMapping[index].actionName != RecordingActionName + # setKeyForAction "", actionKeyMapping[index].actionName + # index += 1 + # setKeyForAction keyName, RecordingActionName + # getEventWithName("keyset").triggerActions() updatePosition: () -> if @move_action @@ -101,7 +100,14 @@ class Player extends Bot when Action.FALL_FORWARD @current_position = @position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getDir() + (1.0 - Math.cos(Math.PI/2 * relTime)) * -@getUp() + # 00000000 00000000 0000000 000 00000000 0000000 000000000 000 0000000 000 000 + # 000 000 000 000 000 000 000 000 000 000 000 000 000 0000 000 + # 00000000 0000000 000 000 000 0000000 000 000 000 000 000 000 0 000 + # 000 000 000 000 000 000 000 000 000 000 000 000 000 000 0000 + # 000 000 000 0000000 0000000 00000000 0000000 000 000 0000000 000 000 + getProjection: () -> + log 'getProjection' # smooth camera movement a little bit posDelta = world.getSpeed() / 10.0 @projection.setPosition ((1.0 - posDelta) * @projection.getPosition() + posDelta * @current_position) @@ -126,7 +132,14 @@ class Player extends Bot @projection + # 0000000 00000000 000 000 000 000 000 0000000 + # 000 000 000 000 000 000 0000 000 000 000 + # 0000000 0000000 000000000 000 000 0 000 000 000 + # 000 000 000 000 000 000 000 0000 000 000 + # 0000000 00000000 000 000 000 000 000 0000000 + getBehindProjection: () -> + log 'getBehindProjection' @updatePosition() @playerDir = getCurrentDir() @@ -162,7 +175,15 @@ class Player extends Bot @projection - getFollowProjection: () -> + + # 00000000 0000000 000 000 0000000 000 000 + # 000 000 000 000 000 000 000 000 0 000 + # 000000 000 000 000 000 000 000 000000000 + # 000 000 000 000 000 000 000 000 000 + # 000 0000000 0000000 0000000 0000000 00 00 + + getFollowProjection: () -> + log 'getFollowProjection' cameraPos = @projection.getPosition() # current camera position desiredDistance = 2.0 # desired distance from camera to bot @@ -253,6 +274,13 @@ class Player extends Bot @projection + + # 0000000 0000000 000000000 000 0000000 000 000 + # 000 000 000 000 000 000 000 0000 000 + # 000000000 000 000 000 000 000 000 0 000 + # 000 000 000 000 000 000 000 000 0000 + # 000 000 0000000 000 000 0000000 000 000 + initAction: (action) -> actionId = action.id switch actionId @@ -266,6 +294,17 @@ class Player extends Bot super action + finishRotateAction: () -> + if rotate_action + @rotate = false + @finishAction rotate_action + + # 00000000 00000000 00000000 00000000 0000000 00000000 00 00 + # 000 000 000 000 000 000 000 000 000 000 000 000 + # 00000000 0000000 0000000 000000 000 000 0000000 000000000 + # 000 000 000 000 000 000 000 000 000 000 0 000 + # 000 00000000 000 000 000 0000000 000 000 000 000 + performAction: (action) -> relTime = action.getRelativeTime() @@ -286,6 +325,12 @@ class Player extends Bot else super action + # 00000000 000 000 000 000 0000000 000 000 + # 000 000 0000 000 000 000 000 000 + # 000000 000 000 0 000 000 0000000 000000000 + # 000 000 000 0000 000 000 000 000 + # 000 000 000 000 000 0000000 000 000 + finishAction: (action) -> actionId = action.id @@ -294,14 +339,14 @@ class Player extends Bot @look_angle = 0.0 else if action == @move_action # move finished, update direction - dir_sgn = new_dir_sgn + @dir_sgn = @new_dir_sgn if actionId != Action.LOOK_UP and actionId != Action.LOOK_DOWN KikiBot.finishAction(action) if actionId == Action.TURN_LEFT or actionId == Action.TURN_RIGHT if rotate - rotate_action = getActionWithId rotate + @rotate_action = getActionWithId rotate rotate_action.reset() Timer.addAction rotate_action @@ -314,30 +359,37 @@ class Player extends Bot reborn: () -> # Controller.addKeyHandler @ - died = false + @died = false reset: () -> - KikiBot.reset() + super Timer.removeActionsOfObject @ @look_action = null @look_angle = 0.0 - new_dir_sgn = 1.0 - rotate = 0 + @new_dir_sgn = 1.0 + @rotate = 0 - recorder = null - playback = null + # @recorder = null + # @playback = null - saveRecorder: () -> - if @recorder - @recorder.save() - @recorder = null - - startRecorder: (file) -> - if @recorder - saveRecorder() - @recorder = new KikiRecorder file + # saveRecorder: () -> + # if @recorder + # @recorder.save() + # @recorder = null +# + # startRecorder: (file) -> + # if @recorder + # saveRecorder() + # @recorder = new KikiRecorder file + + # 000 000 00000000 000 000 + # 000 000 000 000 000 + # 0000000 0000000 00000 + # 000 000 000 000 + # 000 000 00000000 000 + handleKey: (key) -> keyName = key.getUnmodifiedName() keyHandled = -> @@ -349,11 +401,11 @@ class Player extends Bot if @move_action == null # player is currently not performing a move action # forward or backward direction - new_dir_sgn = dir_sgn = (key.getUnmodifiedName() == backward_key) ? -1 : 1 + @new_dir_sgn = @dir_sgn = (key.getUnmodifiedName() == backward_key) ? -1 : 1 moveBot() # perform new move action (depending on environment) else - new_dir_sgn = (keyName == backward_key) ? -1 : 1 + @new_dir_sgn = (keyName == backward_key) ? -1 : 1 return keyHandled() @@ -395,6 +447,12 @@ class Player extends Bot return false + # 00000000 00000000 000 00000000 0000000 0000000 00000000 + # 000 000 000 000 000 000 000 000 000 + # 0000000 0000000 000 0000000 000000000 0000000 0000000 + # 000 000 000 000 000 000 000 000 000 + # 000 000 00000000 0000000 00000000 000 000 0000000 00000000 + handleKeyRelease: (key) -> keyName = key.getUnmodifiedName() releaseHandled = -> @@ -440,6 +498,13 @@ class Player extends Bot return false + + # 0000000 000 0000000 00000000 000 0000000 000 000 + # 000 000 000 000 000 000 000 000 000 000 000 + # 000 000 000 0000000 00000000 000 000000000 00000 + # 000 000 000 000 000 000 000 000 000 + # 0000000 000 0000000 000 0000000 000 000 000 + display: () -> if world.getCameraMode() != world.CAMERA_INSIDE or world.getEditMode() render() @@ -461,10 +526,5 @@ class Player extends Bot return tireColor return colors[KikiPlayer_tire_color] - - finishRotateAction: () -> - if rotate_action - @rotate = false - @finishAction rotate_action - + module.exports = Player diff --git a/coffee/tmpobject.coffee b/coffee/tmpobject.coffee new file mode 100644 index 0000000..07cdd5a --- /dev/null +++ b/coffee/tmpobject.coffee @@ -0,0 +1,18 @@ +# 000000000 00 00 00000000 0000000 0000000 000 00000000 0000000 000000000 +# 000 000 000 000 000 000 000 000 000 000 000 000 000 +# 000 000000000 00000000 000 000 0000000 000 0000000 000 000 +# 000 000 0 000 000 000 000 000 000 000 000 000 000 000 +# 000 000 000 000 0000000 0000000 0000000 00000000 0000000 000 + +Item = require './item' + +class TmpObject extends Item + + constructor: (o) -> + @time = 0 + @object = o + super + + del: -> + +module.exports = TmpObject diff --git a/coffee/world.coffee b/coffee/world.coffee index bd9fc61..d767e89 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -13,6 +13,8 @@ Size = require './lib/size' Cell = require './cell' Light = require './light' Player = require './player' +Timer = require './timer' +TmpObject = require './tmpobject' Quaternion = require './lib/quaternion' Vector = require './lib/vector' Pos = require './lib/pos' @@ -86,6 +88,8 @@ class World @size = new Pos() @depth = -Number.MAX_SAFE_INTEGER @camera_mode = World.CAMERA_BEHIND + @camera_mode = World.CAMERA_INSIDE + @camera_mode = World.CAMERA_FOLLOW @edit_projection = null @raster_size = 0.1 @@ -96,7 +100,6 @@ class World # 0000000 000 000 0000000 00000000 initCage: -> - log "initCage size:", @size mat = new THREE.MeshPhongMaterial color: 0x440000 side: THREE.BackSide @@ -110,12 +113,6 @@ class World @cage.translateY @size.y/2-0.5 @cage.translateZ @size.z/2-0.5 @scene.add @cage - # glDisable(GL_BLEND); - # glDisable(GL_DEPTH_TEST); - # glDisable(GL_ALPHA_TEST); - # glDisable(GL_NORMALIZE); -# - # # colors[World_plate_color].glColor(); # @init: (view) -> return if world? @@ -138,7 +135,7 @@ class World @levelList = [ # intro - # "start", + "start", "steps", #"move", "electro", "elevate", # "throw", @@ -494,7 +491,6 @@ class World menu.addItem(Controller.getLocalizedString("quit"), once(Controller.quit)) setSize: (size) -> - log 'World.setSize!', size @deleteAllObjects() @cells = [] @size = new Pos size @@ -517,7 +513,7 @@ class World getOccupantAtPos: (pos) -> @getCellAtPos(pos)?.getOccupant() getRealOccupantAtPos: (pos) -> occupant = @getOccupantAtPos pos - if occupant and occupant instanceof KikiTmpObject + if occupant and occupant instanceof TmpObject occupant.object else occupant @@ -527,18 +523,16 @@ class World log "World.setObjectAtPos invalid pos:", pos return - cell = @getCellAtPos pos - log "world.setObjectAtPos", cell - + cell = @getCellAtPos pos if object.isSpaceEgoistic() and cell and cell.getOccupant() objectAtNewPos = cell.getOccupant() - if objectAtNewPos instanceof KikiTmpObject + if objectAtNewPos instanceof TmpObject if objectAtNewPos.time > 0 log "WARNING World.setObject already occupied pos:", pos # "already occupied by %s with time %d!", # object.getClassName(), pos.x, pos.y, pos.z, # cell.getOccupant().getClassName(), - # ((KikiTmpObject*)objectAtNewPos).time) + # ((TmpObject*)objectAtNewPos).time) objectAtNewPos.del() # temporary object at new pos will vanish anyway . delete it cell = @getCellAtPos pos @@ -605,12 +599,12 @@ class World @picked_pickable = null @moved_objects = [] - # if Controller.player - # Controller.player.finishRotateAction() - # @removeObject (Controller.player) # remove the player first, to keep it's state - # Controller.timer_event.removeAllActions () + if @player? + @player.finishRotateAction() + @removeObject @player # remove the player first, to keep it's state + Timer.removeAllActions() # Controller.removeKeyHandler (Controller.player) # prevent keyboard input while building world - # Controller.player.reset () + @player.reset() while @lights.length oldSize = @lights.length @@ -638,14 +632,14 @@ class World log "World.getObjectWithName :: no object found with name #{objectName}" null - setCameraMode: (mode) -> @camera_mode = clamp CAMERA_INSIDE, CAMERA_FOLLOW, mode + setCameraMode: (mode) -> @camera_mode = clamp World.CAMERA_INSIDE, World.CAMERA_FOLLOW, mode - changeCameraMode: () -> @camera_mode = (@camera_mode+1) % (CAMERA_FOLLOW+1) + changeCameraMode: () -> @camera_mode = (@camera_mode+1) % (World.CAMERA_FOLLOW+1) objectMovedFromPos: (object, pos) -> if cell = @getCellAtPos(pos) - if tmpObject = cell.getObjectOfType KikiTmpObject + if tmpObject = cell.getObjectOfType TmpObject if tmpObject.object == object tmpObject.del() @moved_objects.push object @@ -662,10 +656,10 @@ class World if cell if objectAtNewPos = cell.getOccupant() - if objectAtNewPos instanceof KikiTmpObject + if objectAtNewPos instanceof TmpObject tmpObject = objectAtNewPos - if (objectAtNewPos.time < 0 and -objectAtNewPos.time <= duration) + if objectAtNewPos.time < 0 and -objectAtNewPos.time <= duration # temporary object at new pos will vanish before object will arrive . delete it objectAtNewPos.del() else @@ -675,13 +669,13 @@ class World @unsetObject object # remove object from cell grid - tmpObject = new KikiTmpObject object # insert temporary objects at new pos + tmpObject = new TmpObject object # insert temporary objects at new pos tmpObject.setPosition pos tmpObject.time = duration @addObjectAtPos tmpObject, pos - tmpObject = new KikiTmpObject object # insert temporary objects at old pos - tmpObject.setPosition object.getPosition() + tmpObject = new TmpObject object # insert temporary objects at old pos + tmpObject.setPosition object.position tmpObject.time = -duration @addObjectAtPos tmpObject, object.getPos() @@ -695,7 +689,7 @@ class World log "World.updateStatus invalid new pos" return - if tmpObject = @getObjectOfTypeAtPos KikiTmpObject, pos + if tmpObject = @getObjectOfTypeAtPos TmpObject, pos if tmpObject.object == movedObject tmpObject.del() else @@ -864,18 +858,19 @@ class World glRectf w+l, h+l, w+t, h+t getProjection: () -> - if @projection == NULL + log "world.getProjection #{@camera_mode}" + if not @projection switch @camera_mode - when CAMERA_INSIDE then @projection = @player.getProjection() - when CAMERA_BEHIND then @projection = @player.getBehindProjection() - when CAMERA_FOLLOW then @projection = @player.getFollowProjection() + when World.CAMERA_INSIDE then @projection = @player.getProjection() + when World.CAMERA_BEHIND then @projection = @player.getBehindProjection() + when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection() @projection display: (mode) -> switch @camera_mode - when CAMERA_INSIDE then @projection = @player.getProjection() - when CAMERA_BEHIND then @projection = @player.getBehindProjection() - when CAMERA_FOLLOW then @projection = @player.getFollowProjection() + when World.CAMERA_INSIDE then @projection = @player.getProjection() + when World.CAMERA_BEHIND then @projection = @player.getBehindProjection() + when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection() @player_projection = @projection