diff --git a/coffee/bot.coffee b/coffee/bot.coffee index d7c9c40..c9b813e 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -6,6 +6,9 @@ Pushable = require './pushable' Action = require './action' +Timer = require './timer' +Vector = require './lib/vector' +Quaternion = require './lib/quaternion' class Bot extends Pushable @@ -13,6 +16,13 @@ class Bot extends Pushable super + @direction = new Quaternion + @orientation = new Quaternion + @current_orientation = new Quaternion + @rotate_orientation = new Quaternion + @climb_orientation = new Quaternion + @rest_orientation = new Quaternion + @geom = new THREE.SphereGeometry 1, 32, 32 @mat = new THREE.MeshPhongMaterial color: 0x0000ff @@ -39,14 +49,13 @@ class Bot extends Pushable @jump = false @shoot = false @jump_once = false - @spiked = false @died = false - @move_action = null - @rotate_action = null + @move_action = null + @rotate_action = null - @dir_sgn = 1.0 + @dir_sgn = 1.0 @addAction new Action @, Action.NOOP, "noop", 0 @addAction new Action @, Action.FORWARD, "move forward", 200 @@ -68,7 +77,7 @@ class Bot extends Pushable addHealth: (h) -> @health = Math.max @health+h die: () -> - # timer_event.removeActionsOfObject (@) + Timer.removeActionsOfObject @ @move = false @jump = false @@ -378,6 +387,8 @@ class Bot extends Pushable @move_action.keepRest() # try to make subsequent actions smooth timer_event.addAction @move_action + getCurrentDir: -> @current_orientation.rotate(new Vector(0,0,1)).normal() + render: () -> radius = 0.5 tireRadius = 0.15 diff --git a/coffee/lib/matrix.coffee b/coffee/lib/matrix.coffee index 198dd38..344e5c1 100644 --- a/coffee/lib/matrix.coffee +++ b/coffee/lib/matrix.coffee @@ -4,9 +4,13 @@ # 000 0 000 000 000 000 000 000 000 000 000 # 000 000 000 000 000 000 000 000 000 000 +Quaternion = require './quaternion' +Vector = require './vector' + class Matrix constructor: (o) -> + @matrix = [] if o instanceof Matrix @@ -123,7 +127,7 @@ class Matrix @matrix[14] += @matrix[2]*v.x+@matrix[6]*v.y+@matrix[10]*v.z @matrix[15] += @matrix[3]*v.x+@matrix[7]*v.y+@matrix[11]*v.z - @rotation (x,y,z) -> new Matrix().rotate x,y,z + @rotation: (x,y,z) -> new Matrix().rotate x,y,z rotate: (x,y,z) -> rx = Vector.DEG2RAD x @@ -201,12 +205,9 @@ class Matrix for k in [0...3] jj[k] = k+1 - fac = m[ii[0]][jj[0]] * (m[ii[1]][jj[1]]*m[ii[2]][jj[2]] - - m[ii[1]][jj[2]]*m[ii[2]][jj[1]]) - fac -= m[ii[0]][jj[1]] * (m[ii[1]][jj[0]]*m[ii[2]][jj[2]] - - m[ii[1]][jj[2]]*m[ii[2]][jj[0]]) - fac += m[ii[0]][jj[2]] * (m[ii[1]][jj[0]]*m[ii[2]][jj[1]] - - m[ii[1]][jj[1]]*m[ii[2]][jj[0]]) + fac = m[ii[0]][jj[0]] * (m[ii[1]][jj[1]]*m[ii[2]][jj[2]] - m[ii[1]][jj[2]]*m[ii[2]][jj[1]]) + fac -= m[ii[0]][jj[1]] * (m[ii[1]][jj[0]]*m[ii[2]][jj[2]] - m[ii[1]][jj[2]]*m[ii[2]][jj[0]]) + fac += m[ii[0]][jj[2]] * (m[ii[1]][jj[0]]*m[ii[2]][jj[1]] - m[ii[1]][jj[1]]*m[ii[2]][jj[0]]) k = i+j if k != (k/2)*2 # compute sign diff --git a/coffee/lib/pos.coffee b/coffee/lib/pos.coffee index b15447a..46eb54d 100644 --- a/coffee/lib/pos.coffee +++ b/coffee/lib/pos.coffee @@ -16,7 +16,9 @@ class Pos @y = parseInt x.y @z = parseInt x.z else if Array.isArray x - [@x, @y, @z] = x + @x = parseInt x[0] + @y = parseInt x[1] + @z = parseInt x[2] else @x = parseInt x @y = parseInt y diff --git a/coffee/lib/quaternion.coffee b/coffee/lib/quaternion.coffee index 840559d..0e447cc 100644 --- a/coffee/lib/quaternion.coffee +++ b/coffee/lib/quaternion.coffee @@ -78,8 +78,6 @@ class Quaternion length: -> Math.sqrt @w*@w + @x*@x + @y*@y + @z*@z eql: (q) -> @w==q.w and @x=q.x and @y==q.y and @z==q.z - # glRotate () const { KMatrix(*this).glMultMatrix(); } - # Quaternion & operator += ( float f ) { w += f; return(*this); } # Quaternion & operator -= ( float f ) { w -= f; return(*this); } # Quaternion & operator *= ( float f ) { w *= f; x *= f; y *= f; z *= f; return(*this); } @@ -88,18 +86,18 @@ class Quaternion mul: (quatOrScalar) -> if quatOrScalar instanceof Quaternion quat = quatOrScalar - A = (@w + @x)*(quat.w + quat.x) - B = (@z - @y)*(quat.y - quat.z) - C = (@w - @x)*(quat.y + quat.z) - D = (@y + @z)*(quat.w - quat.x) - E = (@x + @z)*(quat.x + quat.y) - F = (@x - @z)*(quat.x - quat.y) - G = (@w + @y)*(quat.w - quat.z) - H = (@w - @y)*(quat.w + quat.z) - new Quaternion B +(-E - F + G + H)/2, - A - (E + F + G + H)/2, - C + (E - F + G - H)/2, - D + (E - F - G + H)/2 + A = (@w + @x) * (quat.w + quat.x) + B = (@z - @y) * (quat.y - quat.z) + C = (@w - @x) * (quat.y + quat.z) + D = (@y + @z) * (quat.w - quat.x) + E = (@x + @z) * (quat.x + quat.y) + F = (@x - @z) * (quat.x - quat.y) + G = (@w + @y) * (quat.w - quat.z) + H = (@w - @y) * (quat.w + quat.z) + new Quaternion B + (-E - F + G + H)/2, + A - (E + F + G + H)/2, + C + (E - F + G - H)/2, + D + (E - F - G + H)/2 else new Quaternion @w*f, @x*f, @y*f, z*f diff --git a/coffee/perspective.coffee b/coffee/perspective.coffee index 57349bd..552af0f 100644 --- a/coffee/perspective.coffee +++ b/coffee/perspective.coffee @@ -4,7 +4,8 @@ # 000 000 000 000 000 000 000 000 000 000 000 000 # 000 00000000 000 000 0000000 000 00000000 0000000 000 000 0 00000000 -Matrix = require './matrix' +log = require '/Users/kodi/s/ko/js/tools/log' +Matrix = require './lib/matrix' class Perspective extends Matrix @@ -58,15 +59,18 @@ class Perspective extends Matrix initProjection: -> @apply() - apply: -> - # glViewport(vp[0], vp[1], vp[2], vp[3]); - # gluPerspective (fov, getCurrentAspectRatio(), znear, zfar); - # glMatrixMode(GL_MODELVIEW); - # glLoadIdentity(); - # KVector lookAt = getLookAtPosition(); - # gluLookAt( matrix[TX], matrix[TY], matrix[TZ], - # lookAt[X], lookAt[Y], lookAt[Z], - # matrix[4], matrix[5], matrix[6]); + apply: (camera) -> + + camPos = @getPosition() + up = @getYVector() + lookAt = @getLookAtPosition() + + log "Perspective.apply", 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 + camera.lookAt new THREE.Vector3 lookAt.x, lookAt.y, lookAt.z + if @light? pos = @getPosition().plus @light_offset @light.setDirection -@getZVector() @@ -94,11 +98,12 @@ class Perspective extends Matrix getLookAtPosition: -> @getZVector().mul(-@eye_distance).plus @getPosition() updateViewport: -> - ss = @world.screenSize() + ss = world.screenSize + vp = [] vp[0] = @viewport[0] * ss.w + @border[0] vp[1] = @viewport[1] * ss.h + @border[1] - vp[2] = @viewport[2] * ss.w - (@border[0]+@border[2]) - vp[3] = @viewport[3] * ss.h - (@border[1]+@border[3]) + vp[2] = @viewport[2] * ss.w - @border[0] - @border[2] + vp[3] = @viewport[3] * ss.h - @border[1] - @border[3] getCurrentAspectRatio: -> vps = @getViewportSize() @@ -111,7 +116,7 @@ class Perspective extends Matrix @updateViewport(); setViewport: (l, b, w, h) -> - @viewport[0] = [l,b,w,h] + @viewport = [l,b,w,h] @updateViewport() setFov: (fov) -> @fov = Math.max(2.0, Math.min fov, 175.0) diff --git a/coffee/player.coffee b/coffee/player.coffee index f674313..b246d67 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -7,6 +7,9 @@ Bot = require './bot' Action = require './action' +Timer = require './timer' +Vector = require './lib/vector' +Perspective = require './perspective' forward_key = "UP" backward_key = "DOWN" @@ -19,8 +22,6 @@ look_up_key = "HOME" look_down_key = "END" view_key = "PAGEDOWN" -# KikiActionKey actionKeyMapping[] = - class Player extends Bot constructor: -> @@ -35,8 +36,6 @@ class Player extends Bot @recorder = null @playback = null - # @flags[KDL_KEYHANDLER_FLAG_HANDLES_RELEASE] = true - @addAction new Action @, Action.LOOK_UP, "look up", 220 @addAction new Action @, Action.LOOK_DOWN, "look down", 220 @addAction new Action @, Action.LOOK_RESET, "look reset", 60 @@ -45,13 +44,11 @@ class Player extends Bot @addEventWithName "keyset failed" @addEventWithName "landed" - # @projection = new KLightingProjection (90.0) - # @projection.updateViewport() - # @projection.getLight().setCutoff (90.0) - # @projection.getLight().setAttenuation (1.0, 0.0, 0.05) + @projection = new Perspective 90.0 + @projection.updateViewport() + # @projection.getLight().setCutoff 90.0 + # @projection.getLight().setAttenuation 1.0, 0.0, 0.05 - # Controller.player_status->setStatus status - getActionForKey: (keyName) -> index = 0 while actionKeyMapping[index].actionName @@ -92,7 +89,7 @@ class Player extends Bot updatePosition: () -> if @move_action - relTime = (Controller.getTime() - @move_action.getStart()) / @move_action.getDuration() + relTime = (world.getTime() - @move_action.getStart()) / @move_action.getDuration() if relTime <= 1.0 switch @move_action.id when Action.FORWARD @@ -110,30 +107,30 @@ class Player extends Bot @projection.setPosition ((1.0 - posDelta) * @projection.getPosition() + posDelta * @current_position) playerDir = @getCurrentDir() - playerUp = @current_orientation.rotate(new KVector(0,1,0)).normal() + playerUp = @current_orientation.rotate(new Vector(0,1,0)).normal() if @look_angle # player is looking up or down - projection.setXVector playerUp.cross(@playerDir).normal() - @look_rot = KQuaternion.rotationAroundVector @look_angle, projection.getXVector() - projection.setYVector @look_rot.rotate @playerUp - projection.setZVector @look_rot.rotate -@playerDir + @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 else # smooth camera rotation a little bit - lookDelta = (2.0 - projection.getZVector() * @playerDir) * world.getSpeed() / 50.0 - newLookVector = (1.0 - lookDelta) * projection.getZVector() - lookDelta * @playerDir + lookDelta = (2.0 - @projection.getZVector().dot playerDir) * world.getSpeed() / 50.0 + newLookVector = @projection.getZVector().mul(1.0 - lookDelta).minus playerDir.mul lookDelta newLookVector.normalize() - projection.setXVector playerUp.cross(newLookVector).normal() - projection.setYVector playerUp - projection.setZVector newLookVector + @projection.setXVector playerUp.cross(newLookVector).normal() + @projection.setYVector playerUp + @projection.setZVector newLookVector - return projection + @projection getBehindProjection: () -> @updatePosition() @playerDir = getCurrentDir() - @playerUp = current_orientation.rotate(KVector(0,1,0)).normal() + @playerUp = @current_orientation.rotate(new Vector(0,1,0)).normal() # find a valid camera position botToCamera = (playerUp - 2 * playerDir) @@ -156,7 +153,7 @@ class Player extends Bot else # smooth camera rotation a little bit lookDelta = 0.3 - KVector newLookVector =(1.0 - lookDelta) * @projection.getZVector() - lookDelta * playerDir + newLookVector = @projection.getZVector().mul((1.0 - lookDelta)).minus @playerDir.mul lookDelta newLookVector.normalize() @projection.setZVector(newLookVector) @@ -169,86 +166,85 @@ class Player extends Bot cameraPos = @projection.getPosition() # current camera position desiredDistance = 2.0 # desired distance from camera to bot - updatePosition() + @updatePosition() playerPos = @current_position # desired look pos - playerDir = getCurrentDir() - playerUp = current_orientation.rotate(KVector(0,1,0)).normal() + playerDir = @getCurrentDir() + playerUp = current_orientation.rotate(new Vector(0,1,0)).normal() playerRight = playerDir.cross(playerUp).normal() - # .................................................................. camera follows bot + # ____________________________________________________ camera follows bot # first, adjust distance from camera to bot - botToCamera = cameraPos - playerPos # vector from bot to current pos + botToCamera = cameraPos.minus playerPos # vector from bot to current pos cameraBotDistance = botToCamera.length() # distance from camera to bot if cameraBotDistance >= desiredDistance difference = cameraBotDistance - desiredDistance - delta = (difference*difference)/400.0 # weight for following speed - cameraPos = (1.0 - delta) * cameraPos + delta * playerPos + delta = difference*difference/400.0 # weight for following speed + cameraPos = cameraPos.mul(1.0 - delta).plus playerPos.mul delta else difference = desiredDistance - cameraBotDistance delta = difference/20.0 # weight for negative following speed - cameraPos = (1.0 - delta) * cameraPos + delta * (playerPos + desiredDistance * botToCamera.normal()) + cameraPos = cameraPos.mul(1.0 - delta).plus (playerPos.plus botToCamera.normal().mul desiredDistance).mul delta - # .................................................................. refining camera position + # ____________________________________________________ refining camera position # second, rotate around bot - botToCamera = cameraPos - playerPos - KVector botToCameraNormal = botToCamera.normal() + botToCamera = cameraPos.minus playerPos + botToCameraNormal = botToCamera.normal() - # .................................................................. try view bot from above + # ____________________________________________________ try view bot from above # if camera below bot, rotate up - if (botToCameraNormal * playerUp < 0) + if botToCameraNormal.dot(playerUp) < 0 # calculate angle between player to camera vector and player up vector - verticalAngle = RAD2DEG (Math.acos(kMinMax(-1.0, 1.0, botToCameraNormal * playerUp))) - 90.0 - cameraPos = playerPos + KQuaternion.rotationAroundVector(verticalAngle/40.0, botToCameraNormal.cross(playerUp)).rotate(botToCamera) + verticalAngle = Vector.RAD2DEG Math.acos(kMinMax(-1.0, 1.0, botToCameraNormal.dot playerUp)) - 90.0 + cameraPos = playerPos.plus Quaternion.rotationAroundVector(verticalAngle/40.0, botToCameraNormal.cross(playerUp)).rotate botToCamera - botToCamera = cameraPos - playerPos - botToCameraNormal = (cameraPos - playerPos).normal() + botToCamera = cameraPos.minus playerPos + botToCameraNormal = botToCamera.normal() rot_factor = 1.0 wall_distance = world.getWallDistanceForPos (playerPos + botToCamera) if wall_distance < 0.5 - # .................................................................. apiercing walls - + # ____________________________________________________ apiercing walls if (wall_distance < 0.2) cameraPos = world.getInsideWallPosWithDelta cameraPos, 0.2 - botToCamera = cameraPos - playerPos - botToCameraNormal = (cameraPos - playerPos).normal() + botToCamera = cameraPos.minus playerPos + botToCameraNormal = botToCamera.normal() rot_factor = 0.5 / (wall_distance-0.2) - # .................................................................. try view bot from behind + # ____________________________________________________ try view bot from behind # calculate horizontal angle between bot orientation and vector to camera - mappedToXZ ((botToCamera - playerUp * (botToCamera * playerUp)).normal()) - horizontalAngle = RAD2DEG (Math.acos(kMinMax(-1.0, 1.0, -playerDir * mappedToXZ))) - if botToCameraNormal * playerRight > 0 + mappedToXZ = (botToCamera.minus playerUp.mul(botToCamera.dot playerUp)).normal() + horizontalAngle = Vector.RAD2DEG Math.acos(kMinMax(-1.0, 1.0, -playerDir.dot mappedToXZ)) + if botToCameraNormal.dot(playerRight) > 0 horizontalAngle = -horizontalAngle - cameraPos = playerPos + KQuaternion.rotationAroundVector(horizontalAngle / (rot_factor * 400.0), playerUp).rotate botToCamera + cameraPos = playerPos.plus Quaternion.rotationAroundVector(horizontalAngle / (rot_factor * 400.0), playerUp).rotate botToCamera - botToCamera = cameraPos - playerPos + botToCamera = cameraPos.minus playerPos botToCameraNormal = botToCamera.normal() - # .................................................................. finally, set the position + # ____________________________________________________ finally, set the position @projection.setPosition cameraPos - # .................................................................. refining camera orientation + # ____________________________________________________ refining camera orientation # slowly adjust look direction by interpolating current and desired directions - lookDelta = 2.0 - @projection.getZVector() * botToCameraNormal + lookDelta = 2.0 - @projection.getZVector().dot botToCameraNormal lookDelta *= lookDelta / 30.0 - KVector newLookVector = (1.0 - lookDelta) * @projection.getZVector() + lookDelta * botToCameraNormal + newLookVector = @projection.getZVector().mul(1.0 - lookDelta).plus botToCameraNormal.mul(lookDelta) newLookVector.normalize() # slowly adjust up vector by interpolating current and desired up vectors - upDelta = 2.0 - @projection.getYVector() * playerUp + upDelta = 2.0 - @projection.getYVector().dot playerUp upDelta *= upDelta / 100.0 - KVector newRightVector = ((1.0 - upDelta) * @projection.getYVector() + upDelta * playerUp).cross(newLookVector) + KVector newRightVector = (@projection.getYVector().mul(1.0 - upDelta).plus playerUp.mul(upDelta)).cross newLookVector newRightVector.normalize() - KVector newUpVector = newLookVector.cross(newRightVector).normal() + newUpVector = newLookVector.cross(newRightVector).normal() # finished interpolations, update camera matrix @projection.setZVector newLookVector @@ -263,12 +259,12 @@ class Player extends Bot when Action.CLIMB_DOWN, Action.FORWARD @status.addMoves 1 when Action.TURN_LEFT, Action.TURN_RIGHT - Controller.sound.playSound KikiSound.BOT_MOVE + sound.playSound KikiSound.BOT_MOVE when Action.JUMP, Action.JUMP_FORWARD @status.addMoves actionId == Action.JUMP and 1 or 2 - Controller.sound.playSound KikiSound.BOT_JUMP + sound.playSound KikiSound.BOT_JUMP - KikiBot.initAction(action) + super action performAction: (action) -> relTime = action.getRelativeTime() @@ -288,7 +284,7 @@ class Player extends Bot else @look_angle = Math.max @look_angle, (1.0-relTime) * -90.0 else - KikiBot.performAction action + super action finishAction: (action) -> actionId = action.id @@ -307,22 +303,22 @@ class Player extends Bot if rotate rotate_action = getActionWithId rotate rotate_action.reset() - Controller.timer_event.addAction rotate_action + Timer.addAction rotate_action die: () -> # Controller.removeKeyHandler @ super # Controller.displayText "game over" - # Controller.sound.playSound KikiSound.BOT_DEATH + # sound.playSound KikiSound.BOT_DEATH world.setCameraMode world.CAMERA_FOLLOW reborn: () -> - Controller.addKeyHandler @ + # Controller.addKeyHandler @ died = false reset: () -> KikiBot.reset() - Controller.timer_event.removeActionsOfObject @ + Timer.removeActionsOfObject @ @look_action = null @look_angle = 0.0 @@ -364,9 +360,9 @@ class Player extends Bot if keyName == turn_left_key or keyName == turn_right_key rotate = (keyName == turn_left_key) and Action.TURN_LEFT or Action.TURN_RIGHT - if (rotate_action == null and spiked == false) # player is not performing a rotation and unspiked - rotate_action = getActionWithId rotate - Controller.timer_event.addAction rotate_action + if (@rotate_action == null and spiked == false) # player is not performing a rotation and unspiked + @rotate_action = getActionWithId rotate + Timer.addAction @rotate_action return keyHandled() @@ -382,7 +378,7 @@ class Player extends Bot if keyName == shoot_key if not shoot shoot = true - Controller.timer_event.addAction @getActionWithId Action.SHOOT + Timer.addAction @getActionWithId Action.SHOOT return keyHandled() @@ -390,7 +386,7 @@ class Player extends Bot if not @look_action @look_action = @getActionWithId (key.name == look_up_key) and Action.LOOK_UP or Action.LOOK_DOWN @look_action.reset() - Controller.timer_event.addAction @look_action + Timer.addAction @look_action return keyHandled() if keyName == view_key @@ -406,7 +402,7 @@ class Player extends Bot true if keyName == shoot_key - Controller.timer_event.removeAction getActionWithId Action.SHOOT + Timer.removeAction @getActionWithId Action.SHOOT shoot = false return releaseHandled() @@ -420,8 +416,8 @@ class Player extends Bot if @move_action == null and world.isUnoccupiedPos position.plus @getUp() jump_once = false @move_action = getActionWithId Action.JUMP - Controller.sound.playSound KikiSound.BOT_JUMP - Controller.timer_event.addAction @move_action + sound.playSound KikiSound.BOT_JUMP + Timer.addAction @move_action return releaseHandled() if keyName == turn_left_key or keyName == turn_right_key @@ -434,9 +430,9 @@ class Player extends Bot if keyName == look_down_key or keyName == look_up_key if @look_action and @look_action.id != Action.LOOK_RESET - Controller.timer_event.removeAction @look_action + Timer.removeAction @look_action @look_action = getActionWithId Action.LOOK_RESET - Controller.timer_event.addAction @look_action + Timer.addAction @look_action return releaseHandled() if keyName == view_key @@ -467,8 +463,8 @@ class Player extends Bot return colors[KikiPlayer_tire_color] finishRotateAction: () -> - if rotate_action - rotate = false - finishAction(rotate_action) + if rotate_action + @rotate = false + @finishAction rotate_action module.exports = Player diff --git a/coffee/timer.coffee b/coffee/timer.coffee new file mode 100644 index 0000000..127d863 --- /dev/null +++ b/coffee/timer.coffee @@ -0,0 +1,17 @@ +# 000000000 000 00 00 00000000 00000000 +# 000 000 000 000 000 000 000 +# 000 000 000000000 0000000 0000000 +# 000 000 000 0 000 000 000 000 +# 000 000 000 000 00000000 000 000 + +log = require '/Users/kodi/s/ko/js/tools/log' + +class Timer + + constructor: () -> + + removeActionsOfObject: (o) -> log "removeActionsOfObject", o + addAction: (a) -> log "addAction", a + removeAction: (a) -> log "removeAction", a + +module.exports = Timer diff --git a/coffee/world.coffee b/coffee/world.coffee index 1aa2bf3..bd9fc61 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -9,6 +9,7 @@ first, last} = require "/Users/kodi/s/ko/js/tools/tools" log = require "/Users/kodi/s/ko/js/tools/log" Pos = require './lib/pos' +Size = require './lib/size' Cell = require './cell' Light = require './light' Player = require './player' @@ -30,6 +31,9 @@ class World constructor: (@view) -> + @screenSize = new Size @view.clientWidth, @view.clientHeight + log "view @screenSize #{@screenSize}" + @renderer = new THREE.WebGLRenderer antialias: true logarithmicDepthBuffer: true @@ -60,7 +64,6 @@ class World # 0000000 0000000 00000000 000 000 00000000 @scene = new THREE.Scene() - @geom = new THREE.BoxGeometry 10, 10, 10 # 000 000 0000000 000 000 000000000 # 000 000 000 000 000 000 @@ -74,10 +77,8 @@ class World @ambient = new THREE.AmbientLight 0x444444 @scene.add @ambient - # - @preview = false - - @display_list = 0 + + @preview = false @objects = [] @lights = [] @moved_objects = [] @@ -86,12 +87,36 @@ class World @depth = -Number.MAX_SAFE_INTEGER @camera_mode = World.CAMERA_BEHIND @edit_projection = null - @edit_mode = false - @debug_camera = false - @debug_cells = false - @raster_size = 0.1 + + # 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: -> + log "initCage size:", @size + mat = new THREE.MeshPhongMaterial + color: 0x440000 + side: THREE.BackSide + shading: THREE.SmoothShading + transparent: true + opacity: 0.5 + shininess: 0.99 + geom = new THREE.BoxGeometry @size.x, @size.y, @size.z + @cage = new THREE.Mesh geom, mat + @cage.translateX @size.x/2-0.5 + @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? @@ -245,26 +270,24 @@ class World # ............................................................ player - player = new Player -# - # player_dict = @dict["player"] -# - # if "orientation" in player_dict - # player.setOrientation (player_dict["orientation"]) - # else - # player.setOrientation (roty90) -# - # if "position" in player_dict - # @addObjectAtPos player, @decenter(player_dict["position"]) - # else if "coordinates" in player_dict - # pos = player_dict["coordinates"] - # @addObjectAtPos player, Pos(pos[0], pos[1], pos[2]) + @player = new Player + player_dict = @dict.player + log "player_dict", player_dict + if player_dict.orientation? + @player.setOrientation player_dict.orientation + else + @player.setOrientation roty90 - # if "nostatus" in player_dict - # if player_dict["nostatus"] or @preview - # Controller.player_status.hide() + if player_dict.position? + @addObjectAtPos @player, @decenter player_dict.position + else if player_dict.coordinates? + @addObjectAtPos @player, new Pos player_dict.coordinates + + # if player_dict.nostatus? + # if player_dict.nostatus or @preview + # @player_status.hide() # else - # Controller.player_status.show() + # @player_status.show() # else # if @preview # Controller.player_status.hide() @@ -273,8 +296,8 @@ class World # # @getProjection().setPosition(KVector()) - # Controller.player.getStatus().setMinMoves (highscore.levelParMoves (@level_name)) - # Controller.player.getStatus().setMoves (0) + # @player.getStatus().setMinMoves (highscore.levelParMoves (@level_name)) + # @player.getStatus().setMoves (0) # ............................................................ init # @init() # tell the world that we are finished @@ -471,13 +494,13 @@ class World menu.addItem(Controller.getLocalizedString("quit"), once(Controller.quit)) setSize: (size) -> + log 'World.setSize!', size @deleteAllObjects() @cells = [] - @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() getCellAtPos: (pos) -> return @cells[@posToIndex(pos)] if @isValidPos pos getBotAtPos: (pos) -> @getObjectOfTypeAtPos KikiBot, pos @@ -698,14 +721,17 @@ class World # 0000000 000 00000000 000 step: (step) -> - if true + if false quat = @camera.quaternion.clone() quat.multiply (new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(1,0,0), step.dsecs*0.2) quat.multiply (new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(0,1,0), step.dsecs*0.1) - center = @decenter 0,0,0 + # center = @decenter 0,0,0 + center = @size.div 2 + log center @camera.position.set(center.x,center.y,center.z+@dist).applyQuaternion quat @camera.quaternion.copy quat + @player.getProjection().apply @camera @sun.position.copy @camera.position @renderer.render @scene, @camera @@ -733,6 +759,7 @@ class World @camera?.aspect = @aspect @camera?.updateProjectionMatrix() @renderer?.setSize w,h + @screenSize = new Size w,h getNearestValidPos: (pos) -> new KikiPos Math.min(size.x-1, Math.max(pos.x, 0)), @@ -854,32 +881,4 @@ class World @projection.initProjection() - # glDisable(GL_BLEND); - # glDisable(GL_DEPTH_TEST); - # glDisable(GL_ALPHA_TEST); - # glDisable(GL_NORMALIZE); -# - # # colors[World_plate_color].glColor(); -# - # glTranslatef(-0.5, -0.5, -0.5); -# - # displayWall(size.x, size.y); # xy+z - # glRotatef(180.0, 0.0, 1.0, 0.0); - # glTranslatef(-size.x, 0.0, -size.z); - # displayWall(size.x, size.y); # xy-z -# - # glRotatef(90.0, 1.0, 0.0, 0.0); # xz-y - # glTranslatef(0.0, 0.0, -size.y); - # displayWall(size.x, size.z); - # glRotatef(180.0, 0.0, 1.0, 0.0); # xz+y - # glTranslatef(-size.x, 0.0, -size.y); - # displayWall(size.x, size.z); -# - # glRotatef(-90.0, 0.0, 1.0, 0.0); # yz+x - # glTranslatef(0.0, 0.0, -size.x); - # displayWall(size.y, size.z); - # glRotatef(180.0, 1.0, 0.0, 0.0); # yz-x - # glTranslatef(0.0, -size.z, -size.x); - # displayWall(size.y, size.z); - module.exports = World