This commit is contained in:
monsterkodi 2016-08-12 00:07:30 +02:00
parent a777ddaeb9
commit 17dbe2b590
6 changed files with 151 additions and 120 deletions

View File

@ -6,6 +6,20 @@
class Action class Action
@NOOP = 0
@PUSH = 1
@FALL = 2
@FORWARD = 3
@CLIMB_UP = 4
@CLIMB_DOWN = 5
@TURN_LEFT = 6
@TURN_RIGHT = 7
@JUMP = 8
@JUMP_FORWARD = 9
@FALL_FORWARD = 10
@SHOOT = 11
@END = 12
constructor: (o, i, n, d, m) -> constructor: (o, i, n, d, m) ->
@action_object = o @action_object = o
@ -66,7 +80,7 @@ class Action
return @current / @getDuration() return @current / @getDuration()
getDuration: () -> getDuration: () ->
return Controller.mapMsTime @duration return world.mapMsTime @duration
performWithEvent: (event) -> performWithEvent: (event) ->
eventTime = event.getTime() eventTime = event.getTime()

View File

@ -6,10 +6,11 @@
# 000 000 0000000 000 0000000 000 000 # 000 000 0000000 000 0000000 000 000
{ {
last last
} = require '/Users/kodi/s/ko/js/tools/tools' } = require '/Users/kodi/s/ko/js/tools/tools'
log = require '/Users/kodi/s/ko/js/tools/log' log = require '/Users/kodi/s/ko/js/tools/log'
event = require 'events' Action = require './action'
_ = require 'lodash' event = require 'events'
_ = require 'lodash'
class Actor extends event class Actor extends event
@ -73,3 +74,4 @@ class Actor extends event
getEventWithId: (eventId) -> getEventWithId: (eventId) ->
return @events[eventId] return @events[eventId]
module.exports = Actor

View File

@ -27,7 +27,10 @@ class Bot extends Pushable
@right_tire_rot = 0.0 @right_tire_rot = 0.0
@last_fume = 0 @last_fume = 0
@status = new KikiBotStatus @ @moves = 0
@health = 1
@minMoves = 100
@move = false @move = false
@push = false @push = false
@jump = false @jump = false
@ -42,21 +45,24 @@ class Bot extends Pushable
@dir_sgn = 1.0 @dir_sgn = 1.0
@addAction new KikiAction @, ACTION_NOOP, "noop", 0 @addAction new KikiAction @, Action.NOOP, "noop", 0
@addAction new KikiAction @, ACTION_FORWARD, "move forward", 200 @addAction new KikiAction @, Action.FORWARD, "move forward", 200
@addAction new KikiAction @, ACTION_CLIMB_UP, "climb up", 200 @addAction new KikiAction @, Action.CLIMB_UP, "climb up", 200
@addAction new KikiAction @, ACTION_CLIMB_DOWN, "climb down", 500 @addAction new KikiAction @, Action.CLIMB_DOWN, "climb down", 500
@addAction new KikiAction @, ACTION_TURN_LEFT, "turn left", 200 @addAction new KikiAction @, Action.TURN_LEFT, "turn left", 200
@addAction new KikiAction @, ACTION_TURN_RIGHT, "turn right", 200 @addAction new KikiAction @, Action.TURN_RIGHT, "turn right", 200
@addAction new KikiAction @, ACTION_JUMP, "jump", 120 @addAction new KikiAction @, Action.JUMP, "jump", 120
@addAction new KikiAction @, ACTION_JUMP_FORWARD, "jump forward", 200 @addAction new KikiAction @, Action.JUMP_FORWARD, "jump forward", 200
@addAction new KikiAction @, ACTION_FALL_FORWARD, "fall forward", 200 @addAction new KikiAction @, Action.FALL_FORWARD, "fall forward", 200
@addAction new KikiAction @, ACTION_SHOOT, "shoot", 200, KikiAction.REPEAT @addAction new KikiAction @, Action.SHOOT, "shoot", 200, KikiAction.REPEAT
@getActionWithId(ACTION_FALL).setDuration 120 @getActionWithId(Action.FALL).setDuration 120
@addEventWithName "died" @addEventWithName "died"
@startTimedAction @getActionWithId ACTION_NOOP, 500 @startTimedAction @getActionWithId Action.NOOP, 500
addMoves: (m) -> @moves += m
addHealth: (h) -> @health = Math.max @health+h
die: () -> die: () ->
# timer_event.removeActionsOfObject (@) # timer_event.removeActionsOfObject (@)
@ -91,20 +97,20 @@ class Bot extends Pushable
@spiked = false @spiked = false
@died = false @died = false
isFalling: -> @move_action and @move_action.getId() == ACTION_FALL isFalling: -> @move_action and @move_action.getId() == Action.FALL
initAction: (action) -> initAction: (action) ->
newPos = new KikiPos @position newPos = new KikiPos @position
switch action.getId() switch action.getId()
when ACTION_NOOP then return when Action.NOOP then return
when ACTION_FORWARD then newPos += @getDir() when Action.FORWARD then newPos += @getDir()
when ACTION_CLIMB_DOWN then newPos += @getDir() + @getDown() when Action.CLIMB_DOWN then newPos += @getDir() + @getDown()
when ACTION_JUMP then newPos += @getUp() when Action.JUMP then newPos += @getUp()
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 @direction != KVector()
KikiPushable.initAction action KikiPushable.initAction action
return return
@ -124,13 +130,13 @@ class Bot extends Pushable
dltTime = action.getRelativeDelta() dltTime = action.getRelativeDelta()
switch actionId switch actionId
when ACTION_SHOOT when Action.SHOOT
if relTime == 0 if relTime == 0
KikiBullet.shootFromBot (@) KikiBullet.shootFromBot (@)
when ACTION_NOOP then return when Action.NOOP then return
when ACTION_FORWARD when Action.FORWARD
@left_tire_rot += dir_sgn * dltTime @left_tire_rot += dir_sgn * dltTime
@right_tire_rot += dir_sgn * dltTime @right_tire_rot += dir_sgn * dltTime
@ -138,24 +144,24 @@ class Bot extends Pushable
return return
when ACTION_JUMP when Action.JUMP
@current_position = @position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getUp() @current_position = @position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getUp()
return return
when ACTION_JUMP_FORWARD when Action.JUMP_FORWARD
@left_tire_rot += Math.cos(Math.PI/2 - Math.PI/2 * dltTime) @left_tire_rot += Math.cos(Math.PI/2 - Math.PI/2 * dltTime)
@right_tire_rot += Math.cos(Math.PI/2 - Math.PI/2 * dltTime) @right_tire_rot += Math.cos(Math.PI/2 - Math.PI/2 * dltTime)
@current_position = @position + (1.0 - Math.cos(Math.PI/2 * relTime)) * @getDir() + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getUp() @current_position = @position + (1.0 - Math.cos(Math.PI/2 * relTime)) * @getDir() + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getUp()
return return
when ACTION_FALL_FORWARD 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)) * @getDown() @current_position = @position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getDir() + (1.0 - Math.cos(Math.PI/2 * relTime)) * @getDown()
return return
when ACTION_FALL when Action.FALL
if @direction != KVector() if @direction != KVector()
KikiPushable.performAction action KikiPushable.performAction action
@ -163,14 +169,14 @@ class Bot extends Pushable
@current_position = @position + relTime * @getDown() @current_position = @position + relTime * @getDown()
return return
when ACTION_CLIMB_UP when Action.CLIMB_UP
@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 = KQuaternion.rotationAroundVector(dir_sgn * relTime * -90.0, KVector(1,0,0))
break break
when ACTION_CLIMB_DOWN when Action.CLIMB_DOWN
@left_tire_rot += dir_sgn * dltTime @left_tire_rot += dir_sgn * dltTime
@right_tire_rot += dir_sgn * dltTime @right_tire_rot += dir_sgn * dltTime
@ -185,18 +191,18 @@ class Bot extends Pushable
@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
when ACTION_TURN_RIGHT, ACTION_TURN_LEFT when Action.TURN_RIGHT, Action.TURN_LEFT
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 *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0))
@rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0))
else else
@orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0))
@rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) @rest_orientation = KQuaternion.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 = KQuaternion.rotationAroundVector(relTime * 90.0, KVector(0,1,0))
@ -217,13 +223,13 @@ class Bot extends Pushable
finishAction: (action) -> finishAction: (action) ->
actionId = action.getId() actionId = action.getId()
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 KikiPushable.finishAction action
return return
if actionId == ACTION_TURN_LEFT or actionId == ACTION_TURN_RIGHT if actionId == Action.TURN_LEFT or actionId == Action.TURN_RIGHT
@rotate_action = null @rotate_action = null
if move_action # bot currently performing a move action -> store rotation in @rest_orientation if move_action # bot currently performing a move action -> store rotation in @rest_orientation
@ -233,26 +239,26 @@ class Bot extends Pushable
@orientation *= @rotate_orientation * @rest_orientation # update rotation matrix @orientation *= @rotate_orientation * @rest_orientation # update rotation matrix
@rotate_orientation.reset() @rotate_orientation.reset()
@rest_orientation.reset() @rest_orientation.reset()
else if actionId < ACTION_END else if actionId < Action.END
@move_action = null @move_action = null
@orientation *= @climb_orientation # update climb @orientation @orientation *= @climb_orientation # update climb @orientation
@climb_orientation.reset() @climb_orientation.reset()
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.getId() == ACTION_TURN_LEFT if @rotate_action.getId() == Action.TURN_LEFT
@orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation @orientation *= KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) * @rest_orientation
@rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) @rest_orientation = KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0))
else else
@orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation @orientation *= KQuaternion.rotationAroundVector(-90.0, KVector(0,1,0)) * @rest_orientation
@rest_orientation = KQuaternion.rotationAroundVector(90.0, KVector(0,1,0)) @rest_orientation = KQuaternion.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
@position = @current_position.round() @position = @current_position.round()
if actionId != ACTION_JUMP_FORWARD and @rotate_action == null # if not jumping forward if actionId != Action.JUMP_FORWARD and @rotate_action == null # if not jumping forward
@orientation *= @rest_orientation # update rotation @orientation @orientation *= @rest_orientation # update rotation @orientation
@rest_orientation.reset() @rest_orientation.reset()
@ -262,37 +268,37 @@ class Bot extends Pushable
if @isDead() if @isDead()
die() if not @died die() if not @died
if actionId != ACTION_PUSH and actionId != ACTION_FALL if actionId != Action.PUSH and actionId != Action.FALL
# dead player may only fall, nothing else # dead player may only fall, nothing else
return return
if spiked if spiked
@move_action = null @move_action = null
@startTimedAction getActionWithId(ACTION_NOOP), 0 @startTimedAction getActionWithId(Action.NOOP), 0
return return
if actionId == ACTION_PUSH or @direction != KVector() if actionId == Action.PUSH or @direction != KVector()
KikiPushable.actionFinished (action) KikiPushable.actionFinished (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
# find next action depending on type of finished action and surrounding environment # find next action depending on type of finished action and surrounding environment
if actionId == ACTION_JUMP_FORWARD if actionId == Action.JUMP_FORWARD
forwardPos = @position + @getDir() forwardPos = @position + @getDir()
if world.isUnoccupiedPos forwardPos if world.isUnoccupiedPos forwardPos
# forward will be empty # forward will be empty
if world.isUnoccupiedPos forwardPos.minus @getUp() if world.isUnoccupiedPos forwardPos.minus @getUp()
# below forward will also be empty # below forward will also be empty
@move_action = @getActionWithId ACTION_FALL_FORWARD @move_action = @getActionWithId Action.FALL_FORWARD
@move_action.takeRest (action) @move_action.takeRest (action)
else else
@move_action = @getActionWithId ACTION_FORWARD @move_action = @getActionWithId Action.FORWARD
playSoundAtPos(KikiSound.BOT_LAND, @getPos(), 0.25) playSoundAtPos(KikiSound.BOT_LAND, @getPos(), 0.25)
else # forward will not be empty else # forward will not be empty
if world.isUnoccupiedPos position.minus @getUp() # below is empty if world.isUnoccupiedPos position.minus @getUp() # below is empty
@move_action = @getActionWithId ACTION_CLIMB_UP @move_action = @getActionWithId Action.CLIMB_UP
playSoundAtPos KikiSound.BOT_LAND, @getPos(), 0.5 playSoundAtPos KikiSound.BOT_LAND, @getPos(), 0.5
else if world.isUnoccupiedPos position.minus @getUp() # below will be empty else if world.isUnoccupiedPos position.minus @getUp() # below will be empty
if move # sticky if moving if move # sticky if moving
@ -302,16 +308,16 @@ class Bot extends Pushable
# below forward is solid # below forward is solid
KikiObject * occupant = world.getOccupantAtPos position.plus @getDir().minus @getUp() KikiObject * occupant = world.getOccupantAtPos position.plus @getDir().minus @getUp()
if occupant == null or not occupant.isSlippery() if occupant == null or not occupant.isSlippery()
@move_action = @getActionWithId (ACTION_FORWARD) @move_action = @getActionWithId (Action.FORWARD)
else else
KikiObject * occupant = world.getOccupantAtPos position.plus @getDir() KikiObject * occupant = world.getOccupantAtPos position.plus @getDir()
if occupant == null or not occupant.isSlippery() if occupant == null or not occupant.isSlippery()
@move_action = @getActionWithId (ACTION_CLIMB_UP) @move_action = @getActionWithId (Action.CLIMB_UP)
if @move_action == null if @move_action == null
@move_action = @getActionWithId ACTION_FALL @move_action = @getActionWithId Action.FALL
@move_action.takeRest action @move_action.takeRest action
else if actionId == ACTION_FALL or actionId == ACTION_FALL_FORWARD # landed else if actionId == Action.FALL or actionId == Action.FALL_FORWARD # landed
if @ == player if @ == player
playSound KikiSound.BOT_LAND playSound KikiSound.BOT_LAND
else else
@ -327,9 +333,9 @@ class Bot extends Pushable
@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
@ -341,26 +347,26 @@ class Bot extends Pushable
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
else # no space to jump forward -> jump up else # no space to jump forward -> jump up
@move_action = @getActionWithId ACTION_JUMP @move_action = @getActionWithId Action.JUMP
else if world.isUnoccupiedPos forwardPos # forward is empty else if world.isUnoccupiedPos forwardPos # forward is empty
if world.isUnoccupiedPos forwardPos.plus @getDown() if world.isUnoccupiedPos forwardPos.plus @getDown()
# below forward also empty # below forward also empty
@move_action = @getActionWithId ACTION_CLIMB_DOWN @move_action = @getActionWithId Action.CLIMB_DOWN
else # forward down is solid else # forward down is solid
@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
@move_action = @getActionWithId ACTION_CLIMB_DOWN @move_action = @getActionWithId Action.CLIMB_DOWN
else else
@move_action = moveAction @move_action = moveAction
else # just climb up else # just climb up
@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

View File

@ -5,7 +5,8 @@
# 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
Bot = require './bot' Bot = require './bot'
Action = require './action'
forward_key = "UP" forward_key = "UP"
backward_key = "DOWN" backward_key = "DOWN"
@ -36,9 +37,9 @@ class Player extends Bot
# @flags[KDL_KEYHANDLER_FLAG_HANDLES_RELEASE] = true # @flags[KDL_KEYHANDLER_FLAG_HANDLES_RELEASE] = true
@addAction new KikiAction @, ACTION_LOOK_UP, "look up", 220 @addAction new KikiAction @, Action.LOOK_UP, "look up", 220
@addAction new KikiAction @, ACTION_LOOK_DOWN, "look down", 220 @addAction new KikiAction @, Action.LOOK_DOWN, "look down", 220
@addAction new KikiAction @, ACTION_LOOK_RESET, "look reset", 60 @addAction new KikiAction @, Action.LOOK_RESET, "look reset", 60
@addEventWithName "keyset" @addEventWithName "keyset"
@addEventWithName "keyset failed" @addEventWithName "keyset failed"
@ -94,13 +95,13 @@ class Player extends Bot
relTime = (Controller.getTime() - move_action.getStart()) / move_action.getDuration() relTime = (Controller.getTime() - move_action.getStart()) / move_action.getDuration()
if relTime <= 1.0 if relTime <= 1.0
switch move_action.getId() switch move_action.getId()
when ACTION_FORWARD when Action.FORWARD
current_position = position + relTime * getDir() current_position = position + relTime * getDir()
when ACTION_FALL when Action.FALL
current_position = position - relTime * getUp() current_position = position - relTime * getUp()
when ACTION_JUMP_FORWARD when Action.JUMP_FORWARD
current_position = position + (1.0 - Math.cos(Math.PI/2 * relTime)) * getDir() + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * getUp() current_position = position + (1.0 - Math.cos(Math.PI/2 * relTime)) * getDir() + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * getUp()
when ACTION_FALL_FORWARD 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() current_position = position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * getDir() + (1.0 - Math.cos(Math.PI/2 * relTime)) * -getUp()
getProjection: () -> getProjection: () ->
@ -259,12 +260,12 @@ class Player extends Bot
initAction: (action) -> initAction: (action) ->
actionId = action.getId() actionId = action.getId()
switch actionId switch actionId
when ACTION_CLIMB_DOWN, ACTION_FORWARD when Action.CLIMB_DOWN, Action.FORWARD
status.addMoves 1 @status.addMoves 1
when ACTION_TURN_LEFT, ACTION_TURN_RIGHT when Action.TURN_LEFT, Action.TURN_RIGHT
Controller.sound.playSound KikiSound.BOT_MOVE Controller.sound.playSound KikiSound.BOT_MOVE
when ACTION_JUMP, ACTION_JUMP_FORWARD when Action.JUMP, Action.JUMP_FORWARD
status.addMoves actionId == ACTION_JUMP and 1 or 2 @status.addMoves actionId == Action.JUMP and 1 or 2
Controller.sound.playSound KikiSound.BOT_JUMP Controller.sound.playSound KikiSound.BOT_JUMP
KikiBot.initAction(action) KikiBot.initAction(action)
@ -273,15 +274,15 @@ class Player extends Bot
relTime = action.getRelativeTime() relTime = action.getRelativeTime()
switch action.getId() switch action.getId()
when ACTION_NOOP then return when Action.NOOP then return
when ACTION_LOOK_UP when Action.LOOK_UP
@look_angle = relTime * -90.0 @look_angle = relTime * -90.0
when ACTION_LOOK_DOWN when Action.LOOK_DOWN
@look_angle = relTime * 90.0 @look_angle = relTime * 90.0
when ACTION_LOOK_RESET when Action.LOOK_RESET
if @look_angle > 0 if @look_angle > 0
@look_angle = Math.min @look_angle, (1.0-relTime) * 90.0 @look_angle = Math.min @look_angle, (1.0-relTime) * 90.0
else else
@ -292,17 +293,17 @@ class Player extends Bot
finishAction: (action) -> finishAction: (action) ->
actionId = action.getId() actionId = action.getId()
if actionId == ACTION_LOOK_RESET if actionId == Action.LOOK_RESET
@look_action = null @look_action = null
@look_angle = 0.0 @look_angle = 0.0
else else
if action == move_action # move finished, update direction 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 if actionId != Action.LOOK_UP and actionId != Action.LOOK_DOWN
KikiBot.finishAction(action) KikiBot.finishAction(action)
if actionId == ACTION_TURN_LEFT or actionId == ACTION_TURN_RIGHT if actionId == Action.TURN_LEFT or actionId == Action.TURN_RIGHT
if rotate if rotate
rotate_action = getActionWithId rotate rotate_action = getActionWithId rotate
rotate_action.reset() rotate_action.reset()
@ -313,7 +314,7 @@ class Player extends Bot
KikiBot.die() KikiBot.die()
Controller.displayText("game over") Controller.displayText("game over")
Controller.sound.playSound (KikiSound.BOT_DEATH) Controller.sound.playSound (KikiSound.BOT_DEATH)
world.setCameraMode (KikiWorld.CAMERA_FOLLOW) world.setCameraMode (world.CAMERA_FOLLOW)
reborn: () -> reborn: () ->
Controller.addKeyHandler (this) Controller.addKeyHandler (this)
@ -361,7 +362,7 @@ class Player extends Bot
return keyHandled() return keyHandled()
if keyName == turn_left_key or keyName == turn_right_key if keyName == turn_left_key or keyName == turn_right_key
rotate = (keyName == turn_left_key) ? ACTION_TURN_LEFT : ACTION_TURN_RIGHT 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 if (rotate_action == null and spiked == false) # player is not performing a rotation and unspiked
rotate_action = getActionWithId rotate rotate_action = getActionWithId rotate
@ -381,15 +382,15 @@ class Player extends Bot
if keyName == shoot_key if keyName == shoot_key
if not shoot if not shoot
shoot = true shoot = true
Controller.timer_event.addAction (getActionWithId (ACTION_SHOOT)) Controller.timer_event.addAction @getActionWithId Action.SHOOT
return keyHandled() return keyHandled()
if keyName == look_up_key or keyName == look_down_key if keyName == look_up_key or keyName == look_down_key
if not @look_action if not @look_action
@look_action = getActionWithId ((key.name == look_up_key) ? ACTION_LOOK_UP : ACTION_LOOK_DOWN) @look_action = @getActionWithId (key.name == look_up_key) and Action.LOOK_UP or Action.LOOK_DOWN
@look_action.reset() @look_action.reset()
Controller.timer_event.addAction (@look_action) Controller.timer_event.addAction @look_action
return keyHandled() return keyHandled()
if keyName == view_key if keyName == view_key
@ -405,7 +406,7 @@ class Player extends Bot
true true
if keyName == shoot_key if keyName == shoot_key
Controller.timer_event.removeAction getActionWithId ACTION_SHOOT Controller.timer_event.removeAction getActionWithId Action.SHOOT
shoot = false shoot = false
return releaseHandled() return releaseHandled()
@ -418,8 +419,8 @@ class Player extends Bot
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()
jump_once = false jump_once = false
move_action = getActionWithId ACTION_JUMP move_action = getActionWithId Action.JUMP
Controller.sound.playSound (KikiSound.BOT_JUMP) Controller.sound.playSound KikiSound.BOT_JUMP
Controller.timer_event.addAction (move_action) Controller.timer_event.addAction (move_action)
return releaseHandled() return releaseHandled()
@ -432,9 +433,9 @@ class Player extends Bot
return releaseHandled() return releaseHandled()
if keyName == look_down_key or keyName == look_up_key if keyName == look_down_key or keyName == look_up_key
if @look_action and @look_action.getId() != ACTION_LOOK_RESET if @look_action and @look_action.getId() != Action.LOOK_RESET
Controller.timer_event.removeAction @look_action Controller.timer_event.removeAction @look_action
@look_action = getActionWithId ACTION_LOOK_RESET @look_action = getActionWithId Action.LOOK_RESET
Controller.timer_event.addAction @look_action Controller.timer_event.addAction @look_action
return releaseHandled() return releaseHandled()
@ -444,14 +445,11 @@ class Player extends Bot
return false return false
display: () -> display: () ->
if world.getCameraMode() != KikiWorld.CAMERA_INSIDE or world.getEditMode() if world.getCameraMode() != world.CAMERA_INSIDE or world.getEditMode()
# glPushMatrix()
# current_position.glTranslate()
render() render()
# glPopMatrix()
getBodyColor: () -> getBodyColor: () ->
if world.getCameraMode() == KikiWorld.CAMERA_BEHIND if world.getCameraMode() == world.CAMERA_BEHIND
# static bodyColor # static bodyColor
bodyColor = colors[KikiPlayer_base_color] bodyColor = colors[KikiPlayer_base_color]
bodyColor.setAlpha(kMin(0.7, (projection.getPosition()-current_position).length()-0.4)) bodyColor.setAlpha(kMin(0.7, (projection.getPosition()-current_position).length()-0.4))
@ -460,7 +458,7 @@ class Player extends Bot
return colors[KikiPlayer_base_color] return colors[KikiPlayer_base_color]
getTireColor: () -> getTireColor: () ->
if world.getCameraMode() == KikiWorld.CAMERA_BEHIND if world.getCameraMode() == world.CAMERA_BEHIND
# static tireColor # static tireColor
tireColor = colors[KikiPlayer_tire_color] tireColor = colors[KikiPlayer_tire_color]
tireColor.setAlpha(kMin(1.0, (projection.getPosition()-current_position).length()-0.4)) tireColor.setAlpha(kMin(1.0, (projection.getPosition()-current_position).length()-0.4))
@ -473,3 +471,4 @@ class Player extends Bot
rotate = false rotate = false
finishAction(rotate_action) finishAction(rotate_action)
module.exports = Player

View File

@ -4,21 +4,23 @@
# 000 000 000 000 000 000 000 000 000 000 000 000 # 000 000 000 000 000 000 000 000 000 000 000 000
# 000 0000000 0000000 000 000 000 000 0000000 0000000 00000000 # 000 0000000 0000000 000 000 000 000 0000000 0000000 00000000
Item = require './item' Item = require './item'
Action = require './action'
Vector = require './lib/vector'
class Pushable extends Item class Pushable extends Item
constructor: () -> constructor: () ->
super super
@pusher = null @pusher = null
@direction = new KVector() @direction = new Vector()
@addAction new KikiAction @, ACTION_PUSH, "push" @addAction new Action @, Action.PUSH, "push"
@addAction new KikiAction @, ACTION_FALL, "fall", 40 @addAction new Action @, Action.FALL, "fall", 40
pushedByObjectInDirection: (object, dir, duration) -> pushedByObjectInDirection: (object, dir, duration) ->
pushAction = @getActionWithId ACTION_PUSH pushAction = @getActionWithId Action.PUSH
@pusher = object @pusher = object
@move_action = pushAction @move_action = pushAction
@ -29,28 +31,28 @@ class Pushable extends Item
initAction: (action) -> initAction: (action) ->
# switch action->getId() # switch action->getId()
# when ACTION_FALL # when Action.FALL
# Controller.world->objectWillMoveToPos @, @position + @direction, action->getDuration() # Controller.world->objectWillMoveToPos @, @position + @direction, action->getDuration()
performAction: (action) -> performAction: (action) ->
switch action.getId() switch action.getId()
when ACTION_PUSH, ACTION_FALL when Action.PUSH, Action.FALL
@setCurrentPosition @position + action.getRelativeTime() * @direction @setCurrentPosition @position + action.getRelativeTime() * @direction
finishAction: (action) -> finishAction: (action) ->
switch action.getId() switch action.getId()
when ACTION_PUSH, ACTION_FALL when Action.PUSH, Action.FALL
@move_action = null @move_action = null
world.objectMovedFromPos @, @position world.objectMovedFromPos @, @position
@setPosition @current_position @setPosition @current_position
actionFinished (action) -> actionFinished: (action) ->
actionId = action.getId() actionId = action.getId()
if actionId == ACTION_PUSH or actionId == ACTION_FALL if actionId == Action.PUSH or actionId == Action.FALL
gravityDir = @direction gravityDir = @direction
if actionId == ACTION_PUSH if actionId == Action.PUSH
if @pusher instanceof KikiBot if @pusher instanceof KikiBot
gravityDir = pusher.getDown() gravityDir = pusher.getDown()
else if pusher instanceof KikiBomb else if pusher instanceof KikiBomb
@ -67,7 +69,7 @@ class Pushable extends Item
if world.isUnoccupiedPos @position + gravityDir if world.isUnoccupiedPos @position + gravityDir
@direction = gravityDir @direction = gravityDir
@move_action = @getActionWithId ACTION_FALL @move_action = @getActionWithId Action.FALL
# Controller.timer_event->addAction (move_action) # Controller.timer_event->addAction (move_action)
else else
@direction.reset() @direction.reset()

View File

@ -12,6 +12,7 @@ log = require "/Users/kodi/s/ko/js/tools/log"
Pos = require './lib/pos' Pos = require './lib/pos'
KikiCell = require './cell' KikiCell = require './cell'
KikiLight = require './light' KikiLight = require './light'
Player = require './player'
KQuaternion = require './lib/quaternion' KQuaternion = require './lib/quaternion'
KVector = require './lib/vector' KVector = require './lib/vector'
Pos = require './lib/pos' Pos = require './lib/pos'
@ -246,7 +247,7 @@ class KikiWorld
# ............................................................ player # ............................................................ player
# player = Controller.player player = new Player
# #
# player_dict = @dict["player"] # player_dict = @dict["player"]
# #
@ -282,8 +283,8 @@ class KikiWorld
restart: (self) -> restart: (self) ->
# restores the player status and restarts the current level # restores the player status and restarts the current level
Controller.player.getStatus().setMoves (0) @player.status.setMoves 0
Controller.player.reborn() @player.reborn()
@create() @create()
finish: (self) -> finish: (self) ->
@ -312,7 +313,7 @@ class KikiWorld
# action callback. used to exit current world # action callback. used to exit current world
if name.find ("exit") == 0 if name.find ("exit") == 0
@finish() @finish()
Controller.player.getStatus().setMoves (0) @player.status.setMoves 0
if "world" in @dict["exits"][parseInt name.slice 5] if "world" in @dict["exits"][parseInt name.slice 5]
w = @dict["exits"][parseInt name.slice 5]["world"] w = @dict["exits"][parseInt name.slice 5]["world"]
if w instanceof KikiWorld if w instanceof KikiWorld
@ -710,6 +711,13 @@ class KikiWorld
@sun.position.copy @camera.position @sun.position.copy @camera.position
@renderer.render @scene, @camera @renderer.render @scene, @camera
setSpeed: (s) -> @speed = s
getSpeed: -> @speed
mapMsTime: (unmapped) -> parseInt 10.0 * unmapped/speed
unmapMsTime: (mapped) -> parseInt mapped * speed/10.0
getRelativeTime: -> @frame_time % (10000/@speed)/(10000.0/@speed)
getRelativeDelta: -> (@frame_time - @last_time)/(10000.0/@speed)
resized: (w,h) -> resized: (w,h) ->
# log "world.resized w:#{w} h:#{h}" # log "world.resized w:#{w} h:#{h}"
@aspect = w/h @aspect = w/h