From fcd0407cd00991f3b311fea134df7d08b9d308fa Mon Sep 17 00:00:00 2001 From: monsterkodi Date: Sat, 13 Aug 2016 03:27:13 +0200 Subject: [PATCH] bot --- coffee/bot.coffee | 68 ++++++++++++++++---------------- coffee/lib/vector.coffee | 2 + coffee/main.coffee | 6 +++ coffee/perspective.coffee | 2 - coffee/player.coffee | 83 +++++++++++++++++++++------------------ coffee/timer.coffee | 10 ++--- coffee/world.coffee | 40 ++++++++++--------- 7 files changed, 113 insertions(+), 98 deletions(-) diff --git a/coffee/bot.coffee b/coffee/bot.coffee index c9b813e..4f03ed0 100644 --- a/coffee/bot.coffee +++ b/coffee/bot.coffee @@ -4,6 +4,7 @@ # 000 000 000 000 000 # 0000000 0000000 000 +log = require '/Users/kodi/s/ko/js/tools/log' Pushable = require './pushable' Action = require './action' Timer = require './timer' @@ -123,14 +124,14 @@ class Bot extends Pushable when Action.JUMP_FORWARD then newPos += @getUp() + @getDir() when Action.FALL_FORWARD then newPos += @getDown() + @getDir() when Action.FALL - if @direction != KVector() - KikiPushable.initAction action + if not @direction.isZero() + super action return else newPos += @getDown() break else - KikiPushable.initAction (action) + super action return # if newPos != @position @@ -144,7 +145,7 @@ class Bot extends Pushable switch actionId when Action.SHOOT if relTime == 0 - KikiBullet.shootFromBot (@) + Bullet.shootFromBot @ when Action.NOOP then return @@ -175,8 +176,8 @@ class Bot extends Pushable when Action.FALL - if @direction != KVector() - KikiPushable.performAction action + if not @direction.isZero() + super action return @current_position = @position + relTime * @getDown() return @@ -185,7 +186,7 @@ class Bot extends Pushable @left_tire_rot += dir_sgn * dltTime/2 @right_tire_rot += dir_sgn * dltTime/2 - @climb_orientation = KQuaternion.rotationAroundVector(dir_sgn * relTime * -90.0, KVector(1,0,0)) + @climb_orientation = Quaternion.rotationAroundVector(dir_sgn * relTime * -90.0, KVector(1,0,0)) break when Action.CLIMB_DOWN @@ -195,10 +196,10 @@ class Bot extends Pushable if relTime <= 0.2 @current_position = @position + (relTime/0.2)/2 * @getDir() else if (relTime >= 0.8) - @climb_orientation = KQuaternion.rotationAroundVector(dir_sgn * 90.0, KVector(1,0,0)) + @climb_orientation = Quaternion.rotationAroundVector(dir_sgn * 90.0, KVector(1,0,0)) @current_position = @position + @getDir() + (0.5+(relTime-0.8)/0.2/2) * @getDown() else - @climb_orientation = KQuaternion.rotationAroundVector(dir_sgn * (relTime-0.2)/0.6 * 90.0, KVector(1,0,0)) + @climb_orientation = Quaternion.rotationAroundVector(dir_sgn * (relTime-0.2)/0.6 * 90.0, KVector(1,0,0)) rotVec = (orientation * @climb_orientation).rotate(KVector(0.0, 1.0, 0.0)) @current_position = @position.plus @getDir().plus(@getDown()).plus(rotVec).mul 0.5 break @@ -208,25 +209,25 @@ class Bot extends Pushable if @move_action == null and relTime == 0.0 # if not performing move action and start of rotation # update @orientation now, so next move action will move in desired @direction if actionId == Action.TURN_LEFT - @orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) - @rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) + @orientation *= Quaternion.rotationAroundVector(90.0, KVector(0,1,0)) + @rest_orientation = Quaternion.rotationAroundVector(-90.0, KVector(0,1,0)) else - @orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) - @rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) + @orientation *= Quaternion.rotationAroundVector(-90.0, KVector(0,1,0)) + @rest_orientation = Quaternion.rotationAroundVector(90.0, KVector(0,1,0)) if actionId == Action.TURN_LEFT @left_tire_rot += -dltTime @right_tire_rot += dltTime - @rotate_orientation = KQuaternion.rotationAroundVector(relTime * 90.0, KVector(0,1,0)) + @rotate_orientation = Quaternion.rotationAroundVector(relTime * 90.0, KVector(0,1,0)) else @left_tire_rot += dltTime @right_tire_rot += -dltTime - @rotate_orientation = KQuaternion.rotationAroundVector(relTime * -90.0, KVector(0,1,0)) + @rotate_orientation = Quaternion.rotationAroundVector(relTime * -90.0, KVector(0,1,0)) break else - KikiPushable.performAction action + super action return @current_orientation = @orientation * @climb_orientation * @rotate_orientation * @rest_orientation @@ -238,7 +239,7 @@ class Bot extends Pushable return if actionId == Action.NOOP or actionId == Action.SHOOT if actionId == Action.PUSH - KikiPushable.finishAction action + super action return if actionId == Action.TURN_LEFT or actionId == Action.TURN_RIGHT @@ -260,11 +261,11 @@ class Bot extends Pushable if @rotate_action and actionId != Action.JUMP_FORWARD # bot is currently performing a rotation -> # take over result of rotation to prevent sliding if @rotate_action.id == Action.TURN_LEFT - @orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation - @rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) + @orientation *= Quaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation + @rest_orientation = Quaternion.rotationAroundVector(-90.0, KVector(0,1,0)) else - @orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation - @rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) + @orientation *= Quaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation + @rest_orientation = Quaternion.rotationAroundVector(90.0, KVector(0,1,0)) if actionId != Action.CLIMB_UP world.objectMovedFromPos @, @position # update world @position @@ -290,7 +291,7 @@ class Bot extends Pushable return if actionId == Action.PUSH or @direction != KVector() - KikiPushable.actionFinished (action) + super action return return if @move_action # action was not a move action -> return @@ -336,27 +337,27 @@ class Bot extends Pushable playSoundAtPos KikiSound.BOT_LAND, @getPos() if @move_action - timer_event.addAction @move_action + Timer.addAction @move_action return return if @rotate_action - if move + if @move @moveBot() else dir_sgn = 1.0 if actionId != Action.NOOP then jump_once = false # keep action chain flowing in order to detect environment changes - startTimedAction getActionWithId(Action.NOOP), 0 + @startTimedAction @getActionWithId(Action.NOOP), 0 moveBot: () -> @move_action = null - KikiPos forwardPos = @position + @getDir() + forwardPos = @position.plus @getDir() - if jump or jump_once and # jump mode or jump activated while moving - dir_sgn == 1.0 and # and moving forward - world.isUnoccupiedPos position.plus @getUp() # and above empty + if @jump or @jump_once and # jump mode or jump activated while moving + @dir_sgn == 1.0 and # and moving forward + world.isUnoccupiedPos @position.plus @getUp() # and above empty if world.isUnoccupiedPos forwardPos.plus @getUp() and world.isUnoccupiedPos forwardPos # forward and above forward also empty @move_action = @getActionWithId Action.JUMP_FORWARD @@ -370,7 +371,7 @@ class Bot extends Pushable @move_action = @getActionWithId Action.FORWARD else # forward is not empty moveAction = @getActionWithId Action.FORWARD - if push and world.mayObjectPushToPos @, forwardPos, moveAction.getDuration() + if @push and world.mayObjectPushToPos @, forwardPos, moveAction.getDuration() moveAction.reset() # player in push mode and pushing object is possible if world.isUnoccupiedPos forwardPos.plus @getDown() # below forward is empty @@ -381,13 +382,14 @@ class Bot extends Pushable @move_action = @getActionWithId Action.CLIMB_UP # reset the jump once flag (either we jumped or it's not possible to jump at current @position) - jump_once = false + @jump_once = false - if move_action + if @move_action @move_action.keepRest() # try to make subsequent actions smooth - timer_event.addAction @move_action + Timer.addAction @move_action getCurrentDir: -> @current_orientation.rotate(new Vector(0,0,1)).normal() + getDir: -> @dir_sgn * @orientation.rotate new Vector 0,0,1 render: () -> radius = 0.5 diff --git a/coffee/lib/vector.coffee b/coffee/lib/vector.coffee index 0e10fa8..754e52f 100644 --- a/coffee/lib/vector.coffee +++ b/coffee/lib/vector.coffee @@ -102,6 +102,8 @@ class Vector reset: -> @x = @y = @z = @w = 0 @ + + isZero: -> @x == @y == @z == 0 # glTranslate: () -> glTranslatef @x,@y,@z diff --git a/coffee/main.coffee b/coffee/main.coffee index 6bf65fa..cd0d510 100644 --- a/coffee/main.coffee +++ b/coffee/main.coffee @@ -50,4 +50,10 @@ class Kiki extends Stage resized: (w,h) -> @world.resized w, h + modKeyComboEventDown: (mod, key, combo, event) -> + world.modKeyComboEventDown mod, key, combo, event + + modKeyComboEventUp: (mod, key, combo, event) -> + world.modKeyComboEventUp mod, key, combo, event + module.exports = Kiki diff --git a/coffee/perspective.coffee b/coffee/perspective.coffee index 83828b6..40f4c38 100644 --- a/coffee/perspective.coffee +++ b/coffee/perspective.coffee @@ -58,8 +58,6 @@ class Perspective extends Matrix @setPosition savePos.plus @getZVector().mul @eye_distance - initProjection: -> @apply() - apply: (camera) -> camPos = @getPosition() diff --git a/coffee/player.coffee b/coffee/player.coffee index 341a53d..406cde3 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -5,7 +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' +log = require '/Users/kodi/s/ko/js/tools/log' Bot = require './bot' Action = require './action' Timer = require './timer' @@ -29,6 +29,16 @@ class Player extends Bot super + @key = + forward: 'w' + backward: 's' + left: 'a' + right: 'd' + lookUp: 'up' + lookDown: 'down' + shoot: 'space' + jump: 'command' + @look_action = null @look_angle = 0.0 @new_dir_sgn = 1.0 @@ -47,6 +57,7 @@ class Player extends Bot @projection = new Perspective 90.0 @projection.updateViewport() + # @projection.getLight().setCutoff 90.0 # @projection.getLight().setAttenuation 1.0, 0.0, 0.05 @@ -107,7 +118,6 @@ class Player extends Bot # 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) @@ -390,62 +400,60 @@ class Player extends Bot # 000 000 000 000 # 000 000 00000000 000 - handleKey: (key) -> - keyName = key.getUnmodifiedName() + modKeyComboEventDown: (mod, key, combo, event) -> + # log "player.modKeyComboEventDown mod:#{mod} key:#{key} combo:#{combo}" keyHandled = -> - @recorder?.recordKey key + # @recorder?.recordKey combo true - - if keyName == forward_key or keyName == backward_key - move = true # try to move as long as the key is not released + if combo == @key.forward or combo == @key.backward + # log 'move!' + @move = true # try to move as long as the key is not released 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 - - moveBot() # perform new move action (depending on environment) + @new_dir_sgn = @dir_sgn = (combo == @key.backward) and -1 or 1 + @moveBot() # perform new move action (depending on environment) else - @new_dir_sgn = (keyName == backward_key) ? -1 : 1 - + @new_dir_sgn = (combo == @key.backward) and -1 or 1 + return keyHandled() - if keyName == turn_left_key or keyName == turn_right_key - rotate = (keyName == turn_left_key) and Action.TURN_LEFT or Action.TURN_RIGHT + if combo == @key.turn or combo == @key.right + rotate = (combo == @key.left) and Action.TURN_LEFT or Action.TURN_RIGHT - if (@rotate_action == null and spiked == false) # player is not performing a rotation and unspiked + 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() - if key.name == jump_key - jump = true # switch to jump mode until jump_key released - jump_once = true + if combo == @key.jump + @jump = true # switch to jump mode until jump_key released + @jump_once = true return keyHandled() - if key.name == push_key + if combo == @key.push push = true return keyHandled() - if keyName == shoot_key + if combo == @key.shoot if not shoot shoot = true Timer.addAction @getActionWithId Action.SHOOT - return keyHandled() - if keyName == look_up_key or keyName == look_down_key + if combo == @key.lookUp or combo == @key.lookDown if not @look_action - @look_action = @getActionWithId (key.name == look_up_key) and Action.LOOK_UP or Action.LOOK_DOWN + @look_action = @getActionWithId (combo == @key.lookUp) and Action.LOOK_UP or Action.LOOK_DOWN @look_action.reset() Timer.addAction @look_action return keyHandled() - if keyName == view_key + if combo == @key.view world.changeCameraMode() return keyHandled() - return false + false # 00000000 00000000 000 00000000 0000000 0000000 00000000 # 000 000 000 000 000 000 000 000 000 @@ -453,22 +461,22 @@ class Player extends Bot # 000 000 000 000 000 000 000 000 000 # 000 000 00000000 0000000 00000000 000 000 0000000 00000000 - handleKeyRelease: (key) -> - keyName = key.getUnmodifiedName() + modKeyComboEventUp: (mod, key, combo, event) -> + # log "player.modKeyComboEventUp mod:#{mod} key:#{key} combo:#{combo}" releaseHandled = -> - @recorder?.recordKeyRelease key + # @recorder?.recordKeyRelease combo true - if keyName == shoot_key + if combo == @key.shoot Timer.removeAction @getActionWithId Action.SHOOT shoot = false return releaseHandled() - if keyName == forward_key or keyName == backward_key + if combo == @key.forward or combo == @key.backward move = false return releaseHandled() - if key.name == jump_key + if key.name == @key.jump jump = false if jump_once if @move_action == null and world.isUnoccupiedPos position.plus @getUp() @@ -478,26 +486,25 @@ class Player extends Bot Timer.addAction @move_action return releaseHandled() - if keyName == turn_left_key or keyName == turn_right_key + if combo == @key.left or combo == @key.right rotate = 0 return releaseHandled() - if key.name == push_key + if key.name == @key.push push = false return releaseHandled() - if keyName == look_down_key or keyName == look_up_key + if combo == @key.lookDown or combo == @key.lookUp if @look_action and @look_action.id != Action.LOOK_RESET Timer.removeAction @look_action @look_action = getActionWithId Action.LOOK_RESET Timer.addAction @look_action return releaseHandled() - if keyName == view_key + if combo == @key.view return releaseHandled() - return false - + false # 0000000 000 0000000 00000000 000 0000000 000 000 # 000 000 000 000 000 000 000 000 000 000 000 diff --git a/coffee/timer.coffee b/coffee/timer.coffee index 127d863..1aa660e 100644 --- a/coffee/timer.coffee +++ b/coffee/timer.coffee @@ -4,14 +4,12 @@ # 000 000 000 0 000 000 000 000 # 000 000 000 000 00000000 000 000 -log = require '/Users/kodi/s/ko/js/tools/log' +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 + @removeActionsOfObject: (o) -> log "removeActionsOfObject" + @addAction: (a) -> log "addAction" + @removeAction: (a) -> log "removeAction" module.exports = Timer diff --git a/coffee/world.coffee b/coffee/world.coffee index d767e89..5631e36 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -34,7 +34,7 @@ class World constructor: (@view) -> @screenSize = new Size @view.clientWidth, @view.clientHeight - log "view @screenSize #{@screenSize}" + log "view @screenSize:", @screenSize @renderer = new THREE.WebGLRenderer antialias: true @@ -113,7 +113,13 @@ class World @cage.translateY @size.y/2-0.5 @cage.translateZ @size.z/2-0.5 @scene.add @cage -# + + # l = @raster_size/2.0 + # t = 1.0 - l + # for w in [0..width] + # for h in [0..height] + # log 'wall:', w+l, h+l, w+t, h+t + @init: (view) -> return if world? @@ -539,7 +545,6 @@ class World if not cell? cell = new Cell() @cells[@posToIndex(pos)] = cell - log "world.setObjectAtPos new cell", cell object.setPosition pos cell.addObject object @@ -568,7 +573,6 @@ class World addObjectAtPos: (object, x, y, z) -> pos = new Pos x, y, z - log "world.addObjectAtPos", pos object = @newObject object @setObjectAtPos object, pos @addObject object @@ -844,19 +848,7 @@ class World displayLights: () -> for light in @lights lignt.display() - - displayWall: (width, height) -> - l = @raster_size/2.0 - t = 1.0 - l - - # if flags[DISPLAY_RASTER] == false - # l = 0.0; t = 1.0 - glNormal3f 0.0, 0.0, 1.0 - for w in [0..width] - for h in [0..height] - glRectf w+l, h+l, w+t, h+t - getProjection: () -> log "world.getProjection #{@camera_mode}" if not @projection @@ -867,13 +859,23 @@ class World @projection display: (mode) -> + log "display #{@mode}" switch @camera_mode 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.apply @camera - @player_projection = @projection - - @projection.initProjection() + # 000 000 00000000 000 000 + # 000 000 000 000 000 + # 0000000 0000000 00000 + # 000 000 000 000 + # 000 000 00000000 000 + modKeyComboEventUp: (mod, key, combo, event) -> + @player?.modKeyComboEventUp mod, key, combo, event + + modKeyComboEventDown: (mod, key, combo, event) -> + @player?.modKeyComboEventDown mod, key, combo, event + module.exports = World