From 4a251fe5e2823d08f0b54539e08781ede44f75f7 Mon Sep 17 00:00:00 2001 From: monsterkodi Date: Sun, 14 Aug 2016 13:01:48 +0200 Subject: [PATCH] action --- coffee/action.coffee | 3 +-- coffee/bot.coffee | 11 ++++++----- coffee/event.coffee | 10 ++-------- coffee/item.coffee | 2 +- coffee/lib/pos.coffee | 20 ++++++++++---------- coffee/lib/vector.coffee | 5 +---- coffee/main.coffee | 6 +----- coffee/player.coffee | 20 ++++++++++++-------- coffee/world.coffee | 22 ++++++++++++++-------- 9 files changed, 48 insertions(+), 51 deletions(-) diff --git a/coffee/action.coffee b/coffee/action.coffee index d39cba5..31ee7d9 100644 --- a/coffee/action.coffee +++ b/coffee/action.coffee @@ -62,10 +62,9 @@ class Action perform: () -> @object.performAction @ finish: () -> @object.finishAction @ finished: () -> - log "Action.finished #{@name} #{@object?.actionFinished?}" + # log "Action.finished #{@name} #{@object?.actionFinished?}" @object.actionFinished @ return if @deleted - if @current == @getDuration() # if keepRest wasn't called -> reset start and current values @reset() diff --git a/coffee/bot.coffee b/coffee/bot.coffee index 72f18b6..4810e08 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -292,7 +292,7 @@ class Bot extends Pushable finishAction: (action) -> - # log "Bot.finishAction #{action.id} #{action.name}" + log "Bot.finishAction #{action.id} #{action.name}" switch action.id when Action.NOOP, Action.SHOOT @@ -300,8 +300,9 @@ class Bot extends Pushable when Action.PUSH super action return - when Action.TURN_LEFT or Action.TURN_RIGHT + when Action.TURN_LEFT, Action.TURN_RIGHT @rotate_action = null + log 'rotate_action done' if @move_action # bot currently performing a move action -> store rotation in @rest_orientation @rest_orientation = @rest_orientation.mul @rotate_orientation @rotate_orientation.reset() @@ -357,8 +358,8 @@ class Bot extends Pushable @startTimedAction @getActionWithId(Action.NOOP), 0 return - if action.id == Action.PUSH or not @direction.isZero() - log 'super action!' + if action.id == Action.PUSH #or action.id == Action.FALL # not @direction.isZero() + log 'super (Pushable) action!' super action return @@ -384,7 +385,7 @@ class Bot extends Pushable @move_action = @getActionWithId Action.CLIMB_UP world.playSound 'BOT_LAND', @getPos(), 0.5 else if world.isUnoccupiedPos @position.minus @getUp() # below will be empty - log 'below will be empty' + log 'below will be empty!' if @move # sticky if moving if world.isUnoccupiedPos @position.plus @getDir() # forward will be empty diff --git a/coffee/event.coffee b/coffee/event.coffee index b9145f8..dacf795 100644 --- a/coffee/event.coffee +++ b/coffee/event.coffee @@ -67,18 +67,12 @@ class Event @save_actions.pop() addFinishedAction: (action) -> - log "Event.addFinishedAction #{action.name} #{@finished_actions.length}" + # log "Event.addFinishedAction #{action.name} #{@finished_actions.length}" @finished_actions.push action finishActions: () -> while @finished_actions.length - log "Event.finishActions pop:#{@finished_actions.length}", last(@finished_actions).name + # log "Event.finishActions pop:#{@finished_actions.length}", last(@finished_actions).name @finished_actions.pop().finished() - # action = last @finished_actions - # action.finished() - # if @finished_actions.length and action == last @finished_actions - # log 'pop finished' - # @finished_actions.pop() - # log "Event.finishActions actions:#{@actions.length}" if @actions.length > 1 or @actions[0]?.name != 'noop' module.exports = Event diff --git a/coffee/item.coffee b/coffee/item.coffee index 914f37b..a8e5079 100644 --- a/coffee/item.coffee +++ b/coffee/item.coffee @@ -35,7 +35,7 @@ class Item extends Actor isSlippery: -> false setPosition: (x,y,z) -> - @position = new Vector x,y,z + @position = @current_position = new Vector x,y,z @mesh?.position.copy @position getPos: -> new Pos @current_position diff --git a/coffee/lib/pos.coffee b/coffee/lib/pos.coffee index c3188b4..709a140 100644 --- a/coffee/lib/pos.coffee +++ b/coffee/lib/pos.coffee @@ -13,18 +13,18 @@ class Pos constructor: (x=0, y=0, z=0) -> if (x instanceof Vector) or (x instanceof Pos) - @x = parseInt x.x - @y = parseInt x.y - @z = parseInt x.z + @x = Math.round x.x + @y = Math.round x.y + @z = Math.round x.z else if Array.isArray x - @x = parseInt x[0] - @y = parseInt x[1] - @z = parseInt x[2] + @x = Math.round x[0] + @y = Math.round x[1] + @z = Math.round x[2] else - @x = parseInt x - @y = parseInt y - @z = parseInt z - # log "Pos #{x} #{y} #{z}", @ + @x = Math.round x + @y = Math.round y + @z = Math.round z + log "Pos #{x} #{y} #{z}", @ if Number.isNaN @x throw new Error diff --git a/coffee/lib/vector.coffee b/coffee/lib/vector.coffee index 7e74dc8..d562cce 100644 --- a/coffee/lib/vector.coffee +++ b/coffee/lib/vector.coffee @@ -60,10 +60,7 @@ class Vector @ xyperp: -> new Vector -@y, @x - round: -> new Vector @rint(@x), @rint(@y), @rint(@z), @w - rint: (n) -> - if n < 0 then Math.floor(n - 0.5) - else Math.round n + round: -> new Vector Math.round(@x), Math.round(@y), Math.round(@z), @w xyangle: (v) -> thisXY = new Vector(@x, @y).normal() diff --git a/coffee/main.coffee b/coffee/main.coffee index 28e3fa7..9199875 100644 --- a/coffee/main.coffee +++ b/coffee/main.coffee @@ -23,13 +23,9 @@ class Kiki extends Stage @elem.style.bottom = '0' @elem.style.background = "#004" @view.appendChild @elem - log 'init KikiWorld', @view @world = World.init @view - @elem.appendChild @world.renderer.domElement - - log 'hello world' - + @view.focus() @animate() # 0000000 000000000 00000000 00000000 diff --git a/coffee/player.coffee b/coffee/player.coffee index 5579007..6eb2a7d 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -77,7 +77,7 @@ class Player extends Bot when Action.JUMP_FORWARD @current_position = @position.plus @getDir().mul(1.0 - Math.cos(Math.PI/2 * relTime)).plus @getUp().mul Math.cos(Math.PI/2 - Math.PI/2 * relTime) when Action.FALL_FORWARD - @current_position = @position.plus @getDir().mul(Math.cos(Math.PI/2 - Math.PI/2 * relTime)).plus @getUp().mul -(1.0 - Math.cos(Math.PI/2 * relTime)) + @current_position = @position.plus @getDir().mul(Math.cos(Math.PI/2 - Math.PI/2 * relTime)).plus @getUp().mul -(1.0 - Math.cos Math.PI/2 * relTime) # 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 @@ -85,7 +85,7 @@ class Player extends Bot # 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: () -> + getInsideProjection: () -> # smooth camera movement a little bit posDelta = world.getSpeed() / 10.0 @@ -142,7 +142,7 @@ class Player extends Bot else # smooth camera rotation a little bit lookDelta = 0.3 - newLookVector = @projection.getZVector().mul(1.0 - lookDelta).minus playerDir.mul lookDelta + newLookVector = @projection.getZVector().mul(1.0 - lookDelta).plus playerDir.mul lookDelta newLookVector.normalize() @projection.setZVector newLookVector @@ -180,11 +180,11 @@ class Player extends Bot if cameraBotDistance >= desiredDistance difference = cameraBotDistance - desiredDistance delta = difference*difference/400.0 # weight for following speed - cameraPos = cameraPos.mul(1.0 - delta).plus playerPos.mul delta + cameraPos = cameraPos.mul(1.0-delta).plus playerPos.mul delta else difference = desiredDistance - cameraBotDistance delta = difference/20.0 # weight for negative following speed - cameraPos = cameraPos.mul(1.0 - delta).plus (playerPos.plus botToCamera.normal().mul desiredDistance).mul delta + cameraPos = cameraPos.mul(1.0-delta).plus (playerPos.plus botToCamera.normal().mul desiredDistance).mul delta # ____________________________________________________ refining camera position # second, rotate around bot @@ -221,7 +221,7 @@ class Player extends Bot if botToCameraNormal.dot(playerRight) > 0 horizontalAngle = -horizontalAngle - cameraPos = playerPos.plus Quaternion.rotationAroundVector(horizontalAngle / (rotFactor * 400.0), playerUp).rotate botToCamera + cameraPos = playerPos.plus Quaternion.rotationAroundVector(horizontalAngle/(rotFactor*400.0), playerUp).rotate botToCamera botToCamera = cameraPos.minus playerPos botToCameraNormal = botToCamera.normal() @@ -235,7 +235,8 @@ class Player extends Bot # slowly adjust look direction by interpolating current and desired directions lookDelta = 2.0 - @projection.getZVector().dot botToCameraNormal lookDelta *= lookDelta / 30.0 - newLookVector = @projection.getZVector().mul(1.0 - lookDelta).plus botToCameraNormal.mul(lookDelta) + # newLookVector = @projection.getZVector().mul(1.0-lookDelta).plus botToCameraNormal.mul(lookDelta) + newLookVector = @projection.getZVector().mul(1.0-lookDelta).plus botToCameraNormal.neg().mul(lookDelta) newLookVector.normalize() # slowly adjust up vector by interpolating current and desired up vectors @@ -307,7 +308,7 @@ class Player extends Bot @look_action = null @look_angle = 0.0 else - if action.id == @move_action # move finished, update direction + if action.id == @move_action?.id # move finished, update direction @dir_sgn = @new_dir_sgn if action.id != Action.LOOK_UP and action.id != Action.LOOK_DOWN @@ -356,10 +357,13 @@ class Player extends Bot return true when @key.left, @key.right + log 'rotate', combo @rotate = (combo == @key.left) and Action.TURN_LEFT or Action.TURN_RIGHT if not @rotate_action? and not @spiked # player is not performing a rotation and unspiked @rotate_action = @getActionWithId @rotate Timer.addAction @rotate_action + else + log 'already rotating!' return true when @key.jump diff --git a/coffee/world.coffee b/coffee/world.coffee index e7254b7..32d0a50 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -158,8 +158,8 @@ class World extends Actor @levelList = [ # intro - # "start", - "steps", + "start", + # "steps", #"move", "electro", "elevate", # "throw", # easy @@ -384,6 +384,7 @@ class World extends Actor isValidPos: (pos) -> p = new Pos pos + log 'isValidPos', p p.x >= 0 and p.x < @size.x and p.y >= 0 and p.y < @size.y and p.z >= 0 and p.z < @size.z isInvalidPos: (pos) -> not @isValidPos pos @@ -555,7 +556,7 @@ class World extends Actor @initCage() getCellAtPos: (pos) -> return @cells[@posToIndex(pos)] if @isValidPos pos - getBotAtPos: (pos) -> @getObjectOfTypeAtPos KikiBot, pos + getBotAtPos: (pos) -> @getObjectOfTypeAtPos KikiBot, new Pos pos posToIndex: (pos) -> p = new Pos pos @@ -599,8 +600,10 @@ class World extends Actor cell = @getCellAtPos pos if not cell? + cellIndex = @posToIndex(pos) cell = new Cell() - @cells[@posToIndex(pos)] = cell + log "created cell at index #{cellIndex}" + @cells[cellIndex] = cell object.setPosition pos cell.addObject object @@ -634,6 +637,7 @@ class World extends Actor pos = new Pos x, y, z object = @newObject object @setObjectAtPos object, pos + # log "addObjectAtPos #{object.name}", pos @addObject object removeObject: (object) -> @@ -701,7 +705,10 @@ 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) + changeCameraMode: () -> + @camera_mode = (@camera_mode+1) % (World.CAMERA_FOLLOW+1) + log "world.changeCameraMode #{@camera_mode}" + @camera_mode # 0000000 0000000 000 00 00 0000000 000 000 00000000 # 000 000 000 000 000 000 000 000 000 000 000 000 @@ -865,7 +872,7 @@ class World extends Actor if @isInvalidPos pos return true if @getOccupantAtPos pos - log 'isOccupiedPos occupant!', pos + log "isOccupiedPos occupant: #{@getOccupantAtPos(pos).name} at pos:", pos return true mayObjectPushToPos: (object, pos, duration) -> @@ -947,10 +954,9 @@ class World extends Actor lignt.display() getProjection: () -> - # log "world.getProjection #{@camera_mode}" if not @projection switch @camera_mode - when World.CAMERA_INSIDE then @projection = @player.getProjection() + when World.CAMERA_INSIDE then @projection = @player.getInsideProjection() when World.CAMERA_BEHIND then @projection = @player.getBehindProjection() when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection() @projection