This commit is contained in:
monsterkodi 2016-08-13 03:27:13 +02:00
parent 438be70529
commit fcd0407cd0
7 changed files with 113 additions and 98 deletions

View File

@ -4,6 +4,7 @@
# 000 000 000 000 000 # 000 000 000 000 000
# 0000000 0000000 000 # 0000000 0000000 000
log = require '/Users/kodi/s/ko/js/tools/log'
Pushable = require './pushable' Pushable = require './pushable'
Action = require './action' Action = require './action'
Timer = require './timer' Timer = require './timer'
@ -123,14 +124,14 @@ class Bot extends Pushable
when Action.JUMP_FORWARD then newPos += @getUp() + @getDir() when Action.JUMP_FORWARD then newPos += @getUp() + @getDir()
when Action.FALL_FORWARD then newPos += @getDown() + @getDir() when Action.FALL_FORWARD then newPos += @getDown() + @getDir()
when Action.FALL when Action.FALL
if @direction != KVector() if not @direction.isZero()
KikiPushable.initAction action super action
return return
else else
newPos += @getDown() newPos += @getDown()
break break
else else
KikiPushable.initAction (action) super action
return return
# if newPos != @position # if newPos != @position
@ -144,7 +145,7 @@ class Bot extends Pushable
switch actionId switch actionId
when Action.SHOOT when Action.SHOOT
if relTime == 0 if relTime == 0
KikiBullet.shootFromBot (@) Bullet.shootFromBot @
when Action.NOOP then return when Action.NOOP then return
@ -175,8 +176,8 @@ class Bot extends Pushable
when Action.FALL when Action.FALL
if @direction != KVector() if not @direction.isZero()
KikiPushable.performAction action super action
return return
@current_position = @position + relTime * @getDown() @current_position = @position + relTime * @getDown()
return return
@ -185,7 +186,7 @@ class Bot extends Pushable
@left_tire_rot += dir_sgn * dltTime/2 @left_tire_rot += dir_sgn * dltTime/2
@right_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 break
when Action.CLIMB_DOWN when Action.CLIMB_DOWN
@ -195,10 +196,10 @@ class Bot extends Pushable
if relTime <= 0.2 if relTime <= 0.2
@current_position = @position + (relTime/0.2)/2 * @getDir() @current_position = @position + (relTime/0.2)/2 * @getDir()
else if (relTime >= 0.8) 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() @current_position = @position + @getDir() + (0.5+(relTime-0.8)/0.2/2) * @getDown()
else 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)) rotVec = (orientation * @climb_orientation).rotate(KVector(0.0, 1.0, 0.0))
@current_position = @position.plus @getDir().plus(@getDown()).plus(rotVec).mul 0.5 @current_position = @position.plus @getDir().plus(@getDown()).plus(rotVec).mul 0.5
break 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 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 # update @orientation now, so next move action will move in desired @direction
if actionId == Action.TURN_LEFT if actionId == Action.TURN_LEFT
@orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) @orientation *= Quaternion.rotationAroundVector(90.0, KVector(0,1,0))
@rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @rest_orientation = Quaternion.rotationAroundVector(-90.0, KVector(0,1,0))
else else
@orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @orientation *= Quaternion.rotationAroundVector(-90.0, KVector(0,1,0))
@rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) @rest_orientation = Quaternion.rotationAroundVector(90.0, KVector(0,1,0))
if actionId == Action.TURN_LEFT if actionId == Action.TURN_LEFT
@left_tire_rot += -dltTime @left_tire_rot += -dltTime
@right_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 else
@left_tire_rot += dltTime @left_tire_rot += dltTime
@right_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 break
else else
KikiPushable.performAction action super action
return return
@current_orientation = @orientation * @climb_orientation * @rotate_orientation * @rest_orientation @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 return if actionId == Action.NOOP or actionId == Action.SHOOT
if actionId == Action.PUSH if actionId == Action.PUSH
KikiPushable.finishAction action super action
return return
if actionId == Action.TURN_LEFT or actionId == Action.TURN_RIGHT 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 -> if @rotate_action and actionId != Action.JUMP_FORWARD # bot is currently performing a rotation ->
# take over result of rotation to prevent sliding # take over result of rotation to prevent sliding
if @rotate_action.id == Action.TURN_LEFT if @rotate_action.id == Action.TURN_LEFT
@orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation @orientation *= Quaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation
@rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @rest_orientation = Quaternion.rotationAroundVector(-90.0, KVector(0,1,0))
else else
@orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation @orientation *= Quaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation
@rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) @rest_orientation = Quaternion.rotationAroundVector(90.0, KVector(0,1,0))
if actionId != Action.CLIMB_UP if actionId != Action.CLIMB_UP
world.objectMovedFromPos @, @position # update world @position world.objectMovedFromPos @, @position # update world @position
@ -290,7 +291,7 @@ class Bot extends Pushable
return return
if actionId == Action.PUSH or @direction != KVector() if actionId == Action.PUSH or @direction != KVector()
KikiPushable.actionFinished (action) super action
return return
return if @move_action # action was not a move 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() playSoundAtPos KikiSound.BOT_LAND, @getPos()
if @move_action if @move_action
timer_event.addAction @move_action Timer.addAction @move_action
return return
return if @rotate_action return if @rotate_action
if move if @move
@moveBot() @moveBot()
else else
dir_sgn = 1.0 dir_sgn = 1.0
if actionId != Action.NOOP then jump_once = false if actionId != Action.NOOP then jump_once = false
# keep action chain flowing in order to detect environment changes # keep action chain flowing in order to detect environment changes
startTimedAction getActionWithId(Action.NOOP), 0 @startTimedAction @getActionWithId(Action.NOOP), 0
moveBot: () -> moveBot: () ->
@move_action = null @move_action = null
KikiPos forwardPos = @position + @getDir() forwardPos = @position.plus @getDir()
if jump or jump_once and # jump mode or jump activated while moving if @jump or @jump_once and # jump mode or jump activated while moving
dir_sgn == 1.0 and # and moving forward @dir_sgn == 1.0 and # and moving forward
world.isUnoccupiedPos position.plus @getUp() # and above empty world.isUnoccupiedPos @position.plus @getUp() # and above empty
if world.isUnoccupiedPos forwardPos.plus @getUp() and if world.isUnoccupiedPos forwardPos.plus @getUp() and
world.isUnoccupiedPos forwardPos # forward and above forward also empty world.isUnoccupiedPos forwardPos # forward and above forward also empty
@move_action = @getActionWithId Action.JUMP_FORWARD @move_action = @getActionWithId Action.JUMP_FORWARD
@ -370,7 +371,7 @@ class Bot extends Pushable
@move_action = @getActionWithId Action.FORWARD @move_action = @getActionWithId Action.FORWARD
else # forward is not empty else # forward is not empty
moveAction = @getActionWithId Action.FORWARD moveAction = @getActionWithId Action.FORWARD
if push and world.mayObjectPushToPos @, forwardPos, moveAction.getDuration() if @push and world.mayObjectPushToPos @, forwardPos, moveAction.getDuration()
moveAction.reset() moveAction.reset()
# player in push mode and pushing object is possible # player in push mode and pushing object is possible
if world.isUnoccupiedPos forwardPos.plus @getDown() # below forward is empty if world.isUnoccupiedPos forwardPos.plus @getDown() # below forward is empty
@ -381,13 +382,14 @@ class Bot extends Pushable
@move_action = @getActionWithId Action.CLIMB_UP @move_action = @getActionWithId Action.CLIMB_UP
# reset the jump once flag (either we jumped or it's not possible to jump at current @position) # 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 @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() getCurrentDir: -> @current_orientation.rotate(new Vector(0,0,1)).normal()
getDir: -> @dir_sgn * @orientation.rotate new Vector 0,0,1
render: () -> render: () ->
radius = 0.5 radius = 0.5

View File

@ -103,6 +103,8 @@ class Vector
@x = @y = @z = @w = 0 @x = @y = @z = @w = 0
@ @
isZero: -> @x == @y == @z == 0
# glTranslate: () -> glTranslatef @x,@y,@z # glTranslate: () -> glTranslatef @x,@y,@z
@DEG2RAD: (d) -> Math.PI*d/180.0 @DEG2RAD: (d) -> Math.PI*d/180.0

View File

@ -50,4 +50,10 @@ class Kiki extends Stage
resized: (w,h) -> @world.resized w, h 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 module.exports = Kiki

View File

@ -58,8 +58,6 @@ class Perspective extends Matrix
@setPosition savePos.plus @getZVector().mul @eye_distance @setPosition savePos.plus @getZVector().mul @eye_distance
initProjection: -> @apply()
apply: (camera) -> apply: (camera) ->
camPos = @getPosition() camPos = @getPosition()

View File

@ -5,7 +5,7 @@
# 000 000 000 000 000 000 000 000 # 000 000 000 000 000 000 000 000
# 000 0000000 000 000 000 00000000 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' Bot = require './bot'
Action = require './action' Action = require './action'
Timer = require './timer' Timer = require './timer'
@ -29,6 +29,16 @@ class Player extends Bot
super super
@key =
forward: 'w'
backward: 's'
left: 'a'
right: 'd'
lookUp: 'up'
lookDown: 'down'
shoot: 'space'
jump: 'command'
@look_action = null @look_action = null
@look_angle = 0.0 @look_angle = 0.0
@new_dir_sgn = 1.0 @new_dir_sgn = 1.0
@ -47,6 +57,7 @@ class Player extends Bot
@projection = new Perspective 90.0 @projection = new Perspective 90.0
@projection.updateViewport() @projection.updateViewport()
# @projection.getLight().setCutoff 90.0 # @projection.getLight().setCutoff 90.0
# @projection.getLight().setAttenuation 1.0, 0.0, 0.05 # @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 # 000 000 000 0000000 0000000 00000000 0000000 000 000 0000000 000 000
getProjection: () -> getProjection: () ->
log 'getProjection'
# smooth camera movement a little bit # smooth camera movement a little bit
posDelta = world.getSpeed() / 10.0 posDelta = world.getSpeed() / 10.0
@projection.setPosition ((1.0 - posDelta) * @projection.getPosition() + posDelta * @current_position) @projection.setPosition ((1.0 - posDelta) * @projection.getPosition() + posDelta * @current_position)
@ -390,62 +400,60 @@ class Player extends Bot
# 000 000 000 000 # 000 000 000 000
# 000 000 00000000 000 # 000 000 00000000 000
handleKey: (key) -> modKeyComboEventDown: (mod, key, combo, event) ->
keyName = key.getUnmodifiedName() # log "player.modKeyComboEventDown mod:#{mod} key:#{key} combo:#{combo}"
keyHandled = -> keyHandled = ->
@recorder?.recordKey key # @recorder?.recordKey combo
true true
if combo == @key.forward or combo == @key.backward
if keyName == forward_key or keyName == backward_key # log 'move!'
move = true # try to move as long as the key is not released @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 if @move_action == null # player is currently not performing a move action
# forward or backward direction # forward or backward direction
@new_dir_sgn = @dir_sgn = (key.getUnmodifiedName() == backward_key) ? -1 : 1 @new_dir_sgn = @dir_sgn = (combo == @key.backward) and -1 or 1
@moveBot() # perform new move action (depending on environment)
moveBot() # perform new move action (depending on environment)
else else
@new_dir_sgn = (keyName == backward_key) ? -1 : 1 @new_dir_sgn = (combo == @key.backward) and -1 or 1
return keyHandled() return keyHandled()
if keyName == turn_left_key or keyName == turn_right_key if combo == @key.turn or combo == @key.right
rotate = (keyName == turn_left_key) and Action.TURN_LEFT or Action.TURN_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 @rotate_action = getActionWithId rotate
Timer.addAction @rotate_action Timer.addAction @rotate_action
return keyHandled() return keyHandled()
if key.name == jump_key if combo == @key.jump
jump = true # switch to jump mode until jump_key released @jump = true # switch to jump mode until jump_key released
jump_once = true @jump_once = true
return keyHandled() return keyHandled()
if key.name == push_key if combo == @key.push
push = true push = true
return keyHandled() return keyHandled()
if keyName == shoot_key if combo == @key.shoot
if not shoot if not shoot
shoot = true shoot = true
Timer.addAction @getActionWithId Action.SHOOT Timer.addAction @getActionWithId Action.SHOOT
return keyHandled() return keyHandled()
if keyName == look_up_key or keyName == look_down_key if combo == @key.lookUp or combo == @key.lookDown
if not @look_action 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() @look_action.reset()
Timer.addAction @look_action Timer.addAction @look_action
return keyHandled() return keyHandled()
if keyName == view_key if combo == @key.view
world.changeCameraMode() world.changeCameraMode()
return keyHandled() return keyHandled()
return false false
# 00000000 00000000 000 00000000 0000000 0000000 00000000 # 00000000 00000000 000 00000000 0000000 0000000 00000000
# 000 000 000 000 000 000 000 000 000 # 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 000 000 000 000 000 000 000
# 000 000 00000000 0000000 00000000 000 000 0000000 00000000 # 000 000 00000000 0000000 00000000 000 000 0000000 00000000
handleKeyRelease: (key) -> modKeyComboEventUp: (mod, key, combo, event) ->
keyName = key.getUnmodifiedName() # log "player.modKeyComboEventUp mod:#{mod} key:#{key} combo:#{combo}"
releaseHandled = -> releaseHandled = ->
@recorder?.recordKeyRelease key # @recorder?.recordKeyRelease combo
true true
if keyName == shoot_key if combo == @key.shoot
Timer.removeAction @getActionWithId Action.SHOOT Timer.removeAction @getActionWithId Action.SHOOT
shoot = false shoot = false
return releaseHandled() return releaseHandled()
if keyName == forward_key or keyName == backward_key if combo == @key.forward or combo == @key.backward
move = false move = false
return releaseHandled() return releaseHandled()
if key.name == jump_key if key.name == @key.jump
jump = false jump = false
if jump_once if jump_once
if @move_action == null and world.isUnoccupiedPos position.plus @getUp() if @move_action == null and world.isUnoccupiedPos position.plus @getUp()
@ -478,26 +486,25 @@ class Player extends Bot
Timer.addAction @move_action Timer.addAction @move_action
return releaseHandled() return releaseHandled()
if keyName == turn_left_key or keyName == turn_right_key if combo == @key.left or combo == @key.right
rotate = 0 rotate = 0
return releaseHandled() return releaseHandled()
if key.name == push_key if key.name == @key.push
push = false push = false
return releaseHandled() 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 if @look_action and @look_action.id != Action.LOOK_RESET
Timer.removeAction @look_action Timer.removeAction @look_action
@look_action = getActionWithId Action.LOOK_RESET @look_action = getActionWithId Action.LOOK_RESET
Timer.addAction @look_action Timer.addAction @look_action
return releaseHandled() return releaseHandled()
if keyName == view_key if combo == @key.view
return releaseHandled() return releaseHandled()
return false false
# 0000000 000 0000000 00000000 000 0000000 000 000 # 0000000 000 0000000 00000000 000 0000000 000 000
# 000 000 000 000 000 000 000 000 000 000 000 # 000 000 000 000 000 000 000 000 000 000 000

View File

@ -4,14 +4,12 @@
# 000 000 000 0 000 000 000 000 # 000 000 000 0 000 000 000 000
# 000 000 000 000 00000000 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 class Timer
constructor: () -> @removeActionsOfObject: (o) -> log "removeActionsOfObject"
@addAction: (a) -> log "addAction"
removeActionsOfObject: (o) -> log "removeActionsOfObject", o @removeAction: (a) -> log "removeAction"
addAction: (a) -> log "addAction", a
removeAction: (a) -> log "removeAction", a
module.exports = Timer module.exports = Timer

View File

@ -34,7 +34,7 @@ class World
constructor: (@view) -> constructor: (@view) ->
@screenSize = new Size @view.clientWidth, @view.clientHeight @screenSize = new Size @view.clientWidth, @view.clientHeight
log "view @screenSize #{@screenSize}" log "view @screenSize:", @screenSize
@renderer = new THREE.WebGLRenderer @renderer = new THREE.WebGLRenderer
antialias: true antialias: true
@ -113,7 +113,13 @@ class World
@cage.translateY @size.y/2-0.5 @cage.translateY @size.y/2-0.5
@cage.translateZ @size.z/2-0.5 @cage.translateZ @size.z/2-0.5
@scene.add @cage @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) -> @init: (view) ->
return if world? return if world?
@ -539,7 +545,6 @@ class World
if not cell? if not cell?
cell = new Cell() cell = new Cell()
@cells[@posToIndex(pos)] = cell @cells[@posToIndex(pos)] = cell
log "world.setObjectAtPos new cell", cell
object.setPosition pos object.setPosition pos
cell.addObject object cell.addObject object
@ -568,7 +573,6 @@ class World
addObjectAtPos: (object, x, y, z) -> addObjectAtPos: (object, x, y, z) ->
pos = new Pos x, y, z pos = new Pos x, y, z
log "world.addObjectAtPos", pos
object = @newObject object object = @newObject object
@setObjectAtPos object, pos @setObjectAtPos object, pos
@addObject object @addObject object
@ -845,18 +849,6 @@ class World
for light in @lights for light in @lights
lignt.display() 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: () -> getProjection: () ->
log "world.getProjection #{@camera_mode}" log "world.getProjection #{@camera_mode}"
if not @projection if not @projection
@ -867,13 +859,23 @@ class World
@projection @projection
display: (mode) -> display: (mode) ->
log "display #{@mode}"
switch @camera_mode switch @camera_mode
when World.CAMERA_INSIDE then @projection = @player.getProjection() when World.CAMERA_INSIDE then @projection = @player.getProjection()
when World.CAMERA_BEHIND then @projection = @player.getBehindProjection() when World.CAMERA_BEHIND then @projection = @player.getBehindProjection()
when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection() when World.CAMERA_FOLLOW then @projection = @player.getFollowProjection()
@projection.apply @camera
@player_projection = @projection # 000 000 00000000 000 000
# 000 000 000 000 000
# 0000000 0000000 00000
# 000 000 000 000
# 000 000 00000000 000
@projection.initProjection() 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 module.exports = World