This commit is contained in:
monsterkodi 2016-08-10 23:53:20 +02:00
parent 8dbd0bb9da
commit 9372c1f3a6
7 changed files with 503 additions and 98 deletions

396
coffee/bot.coffee Normal file
View File

@ -0,0 +1,396 @@
# 0000000 0000000 000000000
# 000 000 000 000 000
# 0000000 000 000 000
# 000 000 000 000 000
# 0000000 0000000 000
Pushable = require './pushable'
class Bot extends Pushable
constructor: () ->
@left_tire_rot = 0.0
@right_tire_rot = 0.0
@last_fume = 0
@status = new KikiBotStatus @
@move = false
@push = false
@jump = false
@shoot = false
@jump_once = false
@spiked = false
@died = false
@move_action = null
@rotate_action = null
@dir_sgn = 1.0
@addAction new KikiAction @, ACTION_NOOP, "noop", 0
@addAction new KikiAction @, ACTION_FORWARD, "move forward", 200
@addAction new KikiAction @, ACTION_CLIMB_UP, "climb up", 200
@addAction new KikiAction @, ACTION_CLIMB_DOWN, "climb down", 500
@addAction new KikiAction @, ACTION_TURN_LEFT, "turn left", 200
@addAction new KikiAction @, ACTION_TURN_RIGHT, "turn right", 200
@addAction new KikiAction @, ACTION_JUMP, "jump", 120
@addAction new KikiAction @, ACTION_JUMP_FORWARD, "jump forward", 200
@addAction new KikiAction @, ACTION_FALL_FORWARD, "fall forward", 200
@addAction new KikiAction @, ACTION_SHOOT, "shoot", 200, KikiAction::REPEAT
@getActionWithId(ACTION_FALL).setDuration 120
@addEventWithName "died"
@startTimedAction @getActionWithId ACTION_NOOP, 500
die: () ->
# timer_event.removeActionsOfObject (@)
@move = false
@jump = false
@shoot = false
@push = false
@getEventWithName("died").triggerActions()
@died = true
reset: () ->
@left_tire_rot = 0.0
@right_tire_rot = 0.0
@last_fume = 0
@direction.reset()
@orientation.reset()
@current_orientation.reset()
@rotate_orientation.reset()
@climb_orientation.reset()
@rest_orientation.reset()
@move_action = null
@move = false
@push = false
@jump = false
@shoot = false
@jump_once = false
@spiked = false
@died = false
isFalling: -> @move_action and @move_action.getId() == ACTION_FALL
initAction: (action) ->
newPos = new KikiPos @position
switch action.getId()
when ACTION_NOOP then return
when ACTION_FORWARD then newPos += @getDir()
when ACTION_CLIMB_DOWN then newPos += @getDir() + @getDown()
when ACTION_JUMP then newPos += @getUp()
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
return
else
newPos += @getDown()
break
else
KikiPushable::initAction (action)
return
# if newPos != @position
# world.objectWillMoveToPos (@, newPos, action.getDuration())
performAction: ( action ) ->
actionId = action.getId()
relTime = action.getRelativeTime()
dltTime = action.getRelativeDelta()
switch actionId
when ACTION_SHOOT
if (relTime == 0)
KikiBullet::shootFromBot (@)
when ACTION_NOOP then return
when ACTION_FORWARD
left_tire_rot += dir_sgn * dltTime
right_tire_rot += dir_sgn * dltTime
@current_position = @position + relTime * @getDir()
return
when ACTION_JUMP
@current_position = @position + Math.cos(Math.PI/2 - Math.PI/2 * relTime) * @getUp()
return
when ACTION_JUMP_FORWARD
left_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()
return
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()
return
when ACTION_FALL
if direction != KVector()
KikiPushable::performAction action
return
@current_position = @position + relTime * @getDown()
return
when ACTION_CLIMB_UP
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))
break
when ACTION_CLIMB_DOWN
left_tire_rot += dir_sgn * dltTime
right_tire_rot += dir_sgn * dltTime
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))
@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))
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
when ACTION_TURN_RIGHT, ACTION_TURN_LEFT
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))
else
orientation *= KQuaternion::rotationAroundVector(-90.0, KVector(0,1,0))
rest_orientation = KQuaternion::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))
else
left_tire_rot += dltTime
right_tire_rot += -dltTime
rotate_orientation = KQuaternion::rotationAroundVector(relTime * -90.0, KVector(0,1,0))
break
else
KikiPushable::performAction (action)
return
@current_orientation = orientation * climb_orientation * rotate_orientation * rest_orientation
finishAction: ( action ) ->
actionId = action.getId()
return if (actionId == ACTION_NOOP or actionId == ACTION_SHOOT)
if (actionId == ACTION_PUSH)
KikiPushable::finishAction (action)
return
if (actionId == ACTION_TURN_LEFT or actionId == ACTION_TURN_RIGHT)
rotate_action = null
if (move_action) # bot currently performing a move action -> store rotation in rest_orientation
rest_orientation *= rotate_orientation
rotate_orientation.reset()
else
orientation *= rotate_orientation * rest_orientation # update rotation matrix
rotate_orientation.reset()
rest_orientation.reset()
else if (actionId < ACTION_END)
move_action = null
orientation *= climb_orientation # update climb orientation
climb_orientation.reset()
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.getId() == ACTION_TURN_LEFT)
orientation *= KQuaternion::rotationAroundVector(90.0, KVector(0,1,0)) * rest_orientation
rest_orientation = KQuaternion::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))
if (actionId != ACTION_CLIMB_UP)
world.objectMovedFromPos @, @position # update world @position
@position = @current_position.round()
if (actionId != ACTION_JUMP_FORWARD and rotate_action == null) # if not jumping forward
orientation *= rest_orientation # update rotation orientation
rest_orientation.reset()
actionFinished: ( action ) ->
actionId = action.getId()
if @isDead()
die() if not @died
if actionId != ACTION_PUSH and actionId != ACTION_FALL
# dead player may only fall, nothing else
return
if spiked
@move_action = null
@startTimedAction getActionWithId(ACTION_NOOP), 0
return
if actionId == ACTION_PUSH or direction != KVector()
KikiPushable::actionFinished (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
if actionId == ACTION_JUMP_FORWARD
forwardPos = @position + @getDir()
if (world.isUnoccupiedPos(forwardPos))
# forward will be empty
if (world.isUnoccupiedPos(forwardPos - @getUp()))
# below forward will also be empty
move_action = @getActionWithId (ACTION_FALL_FORWARD)
move_action.takeRest (action)
else
move_action = @getActionWithId (ACTION_FORWARD)
playSoundAtPos(KikiSound::BOT_LAND, @getPos(), 0.25)
else # forward will not be empty
if (world.isUnoccupiedPos(position - @getUp())) # below is empty
move_action = @getActionWithId (ACTION_CLIMB_UP)
playSoundAtPos(KikiSound::BOT_LAND, @getPos(), 0.5)
else if (world.isUnoccupiedPos(position - @getUp())) # below will be empty
if (move) # sticky if moving
if (world.isUnoccupiedPos(position + @getDir()))
# forward will be empty
if (world.isOccupiedPos (position + @getDir() - @getUp()))
# below forward is solid
KikiObject * occupant = world.getOccupantAtPos(position + @getDir() - @getUp())
if occupant == null or not occupant.isSlippery()
move_action = @getActionWithId (ACTION_FORWARD)
else
KikiObject * occupant = world.getOccupantAtPos(position + @getDir())
if occupant == null or not occupant.isSlippery()
move_action = @getActionWithId (ACTION_CLIMB_UP)
if move_action == null
move_action = @getActionWithId (ACTION_FALL)
move_action.takeRest (action)
else if (actionId == ACTION_FALL or actionId == ACTION_FALL_FORWARD) # landed
if @ == player
playSound KikiSound::BOT_LAND
else
playSoundAtPos KikiSound::BOT_LAND, @getPos()
if (move_action)
timer_event.addAction move_action
return
return if rotate_action
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)
moveBot: () ->
move_action = null
KikiPos forwardPos = @position + @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 + @getUp()) # and above empty
if world.isUnoccupiedPos(forwardPos + @getUp()) and
world.isUnoccupiedPos(forwardPos) # forward and above forward also empty
move_action = @getActionWithId (ACTION_JUMP_FORWARD)
else # no space to jump forward -> jump up
move_action = @getActionWithId (ACTION_JUMP)
else if world.isUnoccupiedPos(forwardPos) # forward is empty
if world.isUnoccupiedPos(forwardPos + @getDown())
# below forward also empty
move_action = @getActionWithId ACTION_CLIMB_DOWN
else # forward down is solid
move_action = @getActionWithId ACTION_FORWARD
else # forward is not empty
moveAction = @getActionWithId ACTION_FORWARD
if push and world.mayObjectPushToPos @, forwardPos, moveAction.getDuration()
moveAction.reset()
# player in push mode and pushing object is possible
if (world.isUnoccupiedPos(forwardPos + @getDown())) # below forward is empty
move_action = @getActionWithId ACTION_CLIMB_DOWN
else
move_action = moveAction
else # just 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)
jump_once = false
if (move_action)
move_action.keepRest() # try to make subsequent actions smooth
timer_event.addAction move_action
render: () ->
radius = 0.5
tireRadius = 0.15
if (died) @getDeadColor().glColor()
else @getTireColor().glColor()
# KMatrix(current_orientation).glMultMatrix()
# glPushMatrix() # tires
# glRotated(90.0, 0.0, 1.0, 0.0)
# glTranslated(0.0, 0.0, radius-tireRadius)
# glRotated(left_tire_rot * 180.0, 0.0, 0.0, 1.0)
#
# render_tire
#
# glPopMatrix()
# glPushMatrix()
# glRotated(90.0, 0.0, 1.0, 0.0)
# glTranslated(0.0, 0.0, -(radius-tireRadius))
# glRotated(right_tire_rot * 180.0, 0.0, 0.0, 1.0)
#
# render_tire
#
# glPopMatrix()
# if not @died then @getBodyColor().glColor()
render_body
if (move_action or rotate_action) and died == false
unsigned now = getTime()
if ((int)(now - last_fume) > mapMsTime (40))
fume = new KikiBotFume()
world.addObject fume
fume.setPosition @current_position - @getCurrentDir() * 0.4
@last_fume = now
module.exports = Bot

View File

@ -32,7 +32,7 @@ module.exports =
]
create: ->
world.addObjectAtPos('KikiWall', world.decenter(0,0,-2))
world.addObjectAtPos('KikiWall', world.decenter(0,0,-4))
world.addObjectAtPos('KikiWall', world.decenter(0,0,1))
world.addObjectAtPos 'KikiWall', world.decenter 0,0,-2
world.addObjectAtPos 'KikiWall', world.decenter 0,0,-4
world.addObjectAtPos 'KikiWall', world.decenter 0,0, 1

View File

@ -5,10 +5,9 @@
# 000 000 000 000 000 000
# 000 000 000 000 000 000
Stage = require '/Users/kodi/s/ko/js/area/stage'
log = require '/Users/kodi/s/ko/js/tools/log'
KikiWorld = require './world'
Stage = require '/Users/kodi/s/ko/js/area/stage'
log = require '/Users/kodi/s/ko/js/tools/log'
World = require './world'
class Kiki extends Stage
@ -24,8 +23,8 @@ class Kiki extends Stage
@elem.style.bottom = '0'
@elem.style.background = "#004"
@view.appendChild @elem
@world = KikiWorld.init @view
log 'init KikiWorld', @view
@world = World.init @view
@elem.appendChild @world.renderer.domElement

74
coffee/pushable.coffee Normal file
View File

@ -0,0 +1,74 @@
# 00000000 000 000 0000000 000 000 0000000 0000000 000 00000000
# 000 000 000 000 000 000 000 000 000 000 000 000 000
# 00000000 000 000 0000000 000000000 000000000 0000000 000 0000000
# 000 000 000 000 000 000 000 000 000 000 000 000
# 000 0000000 0000000 000 000 000 000 0000000 0000000 00000000
class Pushable
constructor: () ->
@pusher = null
@direction = new KVector()
# addAction (new KikiAction (this, ACTION_PUSH, "push"));
# addAction (new KikiAction (this, ACTION_FALL, "fall", 40));
pushedByObjectInDirection: (object, dir, duration) ->
pushAction = @getActionWithId ACTION_PUSH
@pusher = object
@move_action = pushAction
@direction = dir
# pushAction->setDuration Controller.unmapMsTime duration
# Controller.timer_event->addAction (pushAction);
initAction (action) ->
# switch action->getId()
# when ACTION_FALL
# Controller.world->objectWillMoveToPos @, @position + @direction, action->getDuration()
performAction: (action) ->
switch action.getId()
when ACTION_PUSH, ACTION_FALL
@setCurrentPosition @position + action.getRelativeTime() * @direction
finishAction: (action) ->
switch action.getId()
when ACTION_PUSH, ACTION_FALL
@move_action = null
world.objectMovedFromPos @, @position
@setPosition @current_position
actionFinished (action) ->
actionId = action.getId()
if actionId == ACTION_PUSH or actionId == ACTION_FALL
gravityDir = @direction
if actionId == ACTION_PUSH
if @pusher instanceof KikiBot
gravityDir = pusher.getDown()
else if pusher instanceof KikiBomb
if @ instanceof KikiBot
if @direction == @getUp()
# bots don't fall through bomb splitter
@direction.reset()
return
else
gravityDir = @getDown() # bots pushed by bombs fall down
else
direction.reset()
return # objects pushed by bombs don't fall
if world.isUnoccupiedPos @position + gravityDir
@direction = gravityDir
@move_action = @getActionWithId ACTION_FALL
# Controller.timer_event->addAction (move_action)
else
@direction.reset()
# playSoundAtPos landing_sound, position
module.exports = Pushable

View File

@ -11,7 +11,15 @@ class Stone extends Item
constructor: ->
@geom = new THREE.BoxGeometry 1,1,1
@mat = new THREE.MeshBasicMaterial color: 0x0000ff
@mat = new THREE.MeshPhongMaterial
color: 0xffffff
side: THREE.FrontSide
shading: THREE.SmoothShading
transparent: true
opacity: 0.58
shininess: 0.9
@mesh = new THREE.Mesh @geom, @mat
world.scene.add @mesh
@mesh.matrixAutoUpdate = true

View File

@ -12,7 +12,14 @@ class Wall extends Item
constructor: ->
@geom = new THREE.BoxGeometry 1,1,1
@mat = new THREE.MeshBasicMaterial color: 0xff0000
@mat = new THREE.MeshPhongMaterial
color: 0x0000ff
side: THREE.FrontSide
shading: THREE.SmoothShading
transparent: true
opacity: 0.9
shininess: 0.99
@mesh = new THREE.Mesh @geom, @mat
world.scene.add @mesh
@mesh.matrixAutoUpdate = true

View File

@ -35,7 +35,7 @@ class KikiWorld
logarithmicDepthBuffer: true
autoClear: true
@renderer.setClearColor 0x008800
@renderer.setClearColor 0x000000
@renderer.setSize @view.offsetWidth, @view.offsetHeight
# 0000000 0000000 00 00 00000000 00000000 0000000
@ -60,7 +60,7 @@ class KikiWorld
# 0000000 0000000 00000000 000 000 00000000
@scene = new THREE.Scene()
@geom = new THREE.BoxGeometry 10, 10, 10
@geom = new THREE.BoxGeometry 10, 10, 10
# 000 000 0000000 000 000 000000000
# 000 000 000 000 000 000
@ -68,24 +68,13 @@ class KikiWorld
# 000 000 000 000 000 000 000
# 0000000 000 0000000 000 000 000
@sun = new THREE.PointLight 0xffff00
@sun = new THREE.PointLight 0xffffff
@sun.position.copy @camera.position
@scene.add @sun
@ambient = new THREE.AmbientLight 0x444444
@scene.add @ambient
# @material = new THREE.MeshPhongMaterial
# color: 0xff0000
# side: THREE.FrontSide
# shading: THREE.SmoothShading
# transparent: true
# opacity: 0.85
# shininess: 0
#
# @mesh = new THREE.Mesh @geom, @material
# @scene.add @mesh
#
@preview = false
@display_list = 0
@ -101,26 +90,7 @@ class KikiWorld
@debug_camera = false
@debug_cells = false
# flags[KDL_PICKHANDLER_FLAG_MOVING_ENABLED] = true
# flags[KDL_PICKHANDLER_FLAG_PROJECTION_ENABLED] = true
# flags.resize(WORLD_END)
# flags[DISPLAY_BORDER] = true
# flags[DISPLAY_DOTS] = false
# flags[DISPLAY_RASTER] = true
# flags[DISPLAY_SHADOWS] = false
@raster_size = 0.1
# KEventHandler::notification_center.addReceiverCallback((KPickHandler*)this,
# (KCallbackPtr)&KikiWorld::reinit,
# KDL_NOTIFICATION_TYPE_VIDEO_MODE_CHANGED)
#
# KEventHandler::notification_center.addReceiverCallback((KPickHandler*)this,
# (KCallbackPtr)&KikiWorld::reinit,
# KDL_NOTIFICATION_TYPE_WINDOW_SIZE_CHANGED)
# initializeTextures ()
@raster_size = 0.1
@init: (view) ->
return if world?
@ -144,7 +114,7 @@ class KikiWorld
@levelList = [
# intro
# "start",
#"steps",
"steps",
#"move", "electro", "elevate",
# "throw",
# easy
@ -181,7 +151,7 @@ class KikiWorld
@levelDict[levelName] = require "./levels/#{levelName}"
# log 'levelDict', @levelDict
log "create world in view:", view
world = new KikiWorld view
global.world = world
world.create first @levelList
@ -503,7 +473,6 @@ class KikiWorld
setSize: (size) ->
@deleteAllObjects()
@deleteDisplayList()
@cells = []
@size = new Pos size
@ -647,48 +616,10 @@ class KikiWorld
log "KikiWorld.getObjectWithName :: no object found with name #{objectName}"
null
setEditMode: (editMode) ->
@edit_mode = editMode
if @edit_mode and @edit_projection == null
edit_projection = new KLightingProjection()
@edit_projection.focusOn KVector(@size).mul 2.0
@edit_projection.setEyeDistance @max_distance*1.5
# focusOnPickedPickable: ( bool zoom ) ->
# if (edit_mode and picked_pickable)
# projection.focusOn (((KikiObject*)picked_pickable).getPosition())
setCameraMode: (mode) -> @camera_mode = clamp CAMERA_INSIDE, CAMERA_FOLLOW, mode
changeCameraMode: () -> @camera_mode = (@camera_mode+1) % (CAMERA_FOLLOW+1)
# shouldPick: () -> @edit_mode
#
# picked: () -> # reset drag deltas and start pos
# @deltas.x = @deltas.y = 0
# if @picked_pickable
# @drag_start_pos = ((KikiObject*)picked_pickable).position
#
# moved: ( const KMouseEvent & mouseEvent ) ->
# object = (KikiObject*)picked_pickable
#
# if (object == null) return
#
# KVector newPosition = drag_start_pos
#
# deltas = deltas + mouseEvent.delta
#
# getProjection().moveObjectRelativeToWindow(deltas, newPosition)
#
# # round to next integer positions and make a valid pos
# Pos newPos = getNearestValidPos(newPosition)
#
# if (getOccupantAtPos(newPos) == null and (newPos != object.getPos()))
# empty position != old position . move object
# moveObjectToPos(object, newPos)
objectMovedFromPos: (object, pos) ->
if cell = @getCellAtPos(pos)
@ -732,11 +663,8 @@ class KikiWorld
tmpObject.time = -duration
@addObjectAtPos tmpObject, object.getPos()
# --------------------------------------------------------------------------------------------------------
updateStatus: () ->
# glClearColor(colors[KikiWorld_base_color][R], colors[KikiWorld_base_color][G],
# colors[KikiWorld_base_color][B], colors[KikiWorld_base_color][A])
while @moved_objects.length
movedObject = last @moved_objects
pos = new Pos movedObject.position
@ -756,11 +684,6 @@ class KikiWorld
@setObjectAtPos movedObject, pos
@moved_objects.pop()
deleteDisplayList: () ->
if @display_list
glDeleteLists(@display_list, 1)
@display_list = 0
setObjectColor: (color_name, color) ->
if color_name == 'base'
# KikiWall::setObjectColor "base", color
@ -769,8 +692,6 @@ class KikiWorld
# KikiWall::setObjectColor "plate", color
@colors[1] = color
# Controller.world.deleteDisplayList ()
# 0000000 000000000 00000000 00000000
# 000 000 000 000 000
# 0000000 000 0000000 00000000