From 23cd85bf046059dea168395e85c716786383fbc8 Mon Sep 17 00:00:00 2001 From: monsterkodi Date: Mon, 15 Aug 2016 22:50:46 +0200 Subject: [PATCH] light --- coffee/action.coffee | 5 ++ coffee/bullet.coffee | 21 +++--- coffee/gate.coffee | 127 +++++++++++++++++++++++++++++++++++++ coffee/item.coffee | 1 + coffee/levels/steps.coffee | 2 +- coffee/light.coffee | 53 +++++++--------- coffee/main.coffee | 9 ++- coffee/player.coffee | 4 +- coffee/switch.coffee | 113 +++++++++++++++++++++++++++++++++ coffee/wall.coffee | 14 ++-- coffee/world.coffee | 108 ++++++++++++++++--------------- 11 files changed, 348 insertions(+), 109 deletions(-) create mode 100644 coffee/gate.coffee create mode 100644 coffee/switch.coffee diff --git a/coffee/action.coffee b/coffee/action.coffee index 442164f..b2851ad 100644 --- a/coffee/action.coffee +++ b/coffee/action.coffee @@ -12,6 +12,8 @@ class Action @NOOP = 0 @PUSH = 1 @EXPLODE = 1 + @TOGGLE = 1 + @ROTATE = 2 @FLY = 2 @FALL = 2 @FORWARD = 3 @@ -121,6 +123,9 @@ class Action if @mode == Action.CONTINUOUS log 'Action.CONTINUOUS' @current = @rest + @start = eventTime + @last = 0 + @rest = 0 return event.removeAction @ if @mode == Action.ONCE diff --git a/coffee/bullet.coffee b/coffee/bullet.coffee index f7aeacb..e3ba533 100644 --- a/coffee/bullet.coffee +++ b/coffee/bullet.coffee @@ -18,12 +18,12 @@ class Bullet extends Item geom = new THREE.SphereGeometry 1, 16, 16 mat = new THREE.MeshPhongMaterial - color: 0x222266 + color: 0x2222ff side: THREE.FrontSide shading: THREE.SmoothShading transparent: true - opacity: 0.9 - shininess: 0.99 + opacity: 0.8 + shininess: 5 @mesh = new THREE.Mesh geom, mat @mesh.scale.set @size, @size, @size super @@ -36,7 +36,7 @@ class Bullet extends Item bullet.direction = bot.getCurrentDir() bullet.setPosition bot.position.plus bullet.direction.mul 1/2.0 bullet.src_object = bot - log 'shootFromBot', bullet.direction, bullet.position + # log 'shootFromBot', bullet.direction, bullet.position world.playSound 'BULLET_SHOT', bot.getPos() return if bullet.hitObjectAtPos bullet.position.plus bullet.direction.mul 1/2.0 @@ -61,12 +61,13 @@ class Bullet extends Item if hitObject != @src_object if hitObject? hitObject.bulletImpact() - if hitObject instanceof Mutant and not hitObject.isDead() - world.playSound 'BULLET_HIT_MUTANT', pos - else if hitObject == world.player - world.playSound 'BULLET_HIT_PLAYER', pos - else - world.playSound 'BULLET_HIT_OBJECT', pos + world.playSound hitObject.bulletHitSound?() ? 'BULLET_HIT_OBJECT' + # if hitObject instanceof Mutant and not hitObject.isDead() + # world.playSound 'BULLET_HIT_MUTANT', pos + # else if hitObject == world.player + # world.playSound 'BULLET_HIT_PLAYER', pos + # else + # world.playSound 'BULLET_HIT_OBJECT', pos else world.playSound 'BULLET_HIT_WALL', pos Timer.addAction @getActionWithId Action.EXPLODE diff --git a/coffee/gate.coffee b/coffee/gate.coffee new file mode 100644 index 0000000..7c01eac --- /dev/null +++ b/coffee/gate.coffee @@ -0,0 +1,127 @@ +# 0000000 0000000 000000000 00000000 +# 000 000 000 000 000 +# 000 0000 000000000 000 0000000 +# 000 000 000 000 000 000 +# 0000000 000 000 000 00000000 + +log = require "/Users/kodi/s/ko/js/tools/log" + +Switch = require './switch' +Action = require './action' + +class Gate extends Switch + + constructor: (active) -> + super active + @ENTER_EVENT = @addEventWithName "enter" + @value = 0.0 + @getActionWithId(Action.ROTATE).duration = 3000 + @sound_on = 'GATE_OPEN' + @sound_off = 'GATE_CLOSE' + + createMesh: () -> super + # log 'gate.createMesh' + + newCellMate: (object) -> + if object == world.player and @active + world.playSound 'GATE_WARP' + @events[@ENTER_EVENT].triggerActions() + + renderBar: (r,b,h) -> + # glBegin(GL_QUAD_STRIP); + # glNormal3f(0,1,0); + # glVertex3f(-r, h, -r); glVertex3f(-b, h, -b); + # glVertex3f( r, h, -r); glVertex3f( b, h, -b); + # glVertex3f( r, h, r); glVertex3f( b, h, b); + # glVertex3f(-r, h, r); glVertex3f(-b, h, b); + # glVertex3f(-r, h, -r); glVertex3f(-b, h, -b); + # glEnd(); + # glBegin(GL_QUAD_STRIP); + # glNormal3f(0,-1,0); + # glVertex3f(-b, -h, -b); glVertex3f(-r, -h, -r); + # glVertex3f( b, -h, -b); glVertex3f( r, -h, -r); + # glVertex3f( b, -h, b); glVertex3f( r, -h, r); + # glVertex3f(-b, -h, b); glVertex3f(-r, -h, r); + # glVertex3f(-b, -h, -b); glVertex3f(-r, -h, -r); + # glEnd(); + # glBegin(GL_QUADS); + # glNormal3f(0,0,-1); + # glVertex3f(-r, -h, -r); glVertex3f(-r, h, -r); + # glVertex3f( r, h, -r); glVertex3f( r, -h, -r); + # glNormal3f(1,0,0); + # glVertex3f( r, -h, -r); glVertex3f( r, h, -r); + # glVertex3f( r, h, r); glVertex3f( r, -h, r); + # glNormal3f(0,0,1); + # glVertex3f( r, -h, r); glVertex3f( r, h, r); + # glVertex3f(-r, h, r); glVertex3f(-r, -h, r); + # glNormal3f(-1,0,0); + # glVertex3f(-r, -h, r); glVertex3f(-r, h, r); + # glVertex3f(-r, h, -r); glVertex3f(-r, -h, -r); + # glNormal3f(0,0,1); + # glVertex3f(-b, h, -b); glVertex3f(-b, -h, -b); + # glVertex3f( b, -h, -b); glVertex3f( b, h, -b); + # glNormal3f(-1,0,0); + # glVertex3f( b, h, -b); glVertex3f( b, -h, -b); + # glVertex3f( b, -h, b); glVertex3f( b, h, b); + # glNormal3f(0,0,-1); + # glVertex3f( b, h, b); glVertex3f( b, -h, b); + # glVertex3f(-b, -h, b); glVertex3f(-b, h, b); + # glNormal3f(1,0,0); + # glVertex3f(-b, h, b); glVertex3f(-b, -h, b); + # glVertex3f(-b, -h, -b); glVertex3f(-b, h, -b); + # glEnd(); + + render: () -> + # KColor gate_color = colors[KikiGate_base_color]; + # if (active == false) + # gate_color.setAlpha (gate_color.getAlpha()/4.0); +# + # gate_color.glColor(); +# + # float v = sin(DEG2RAD(angle)); + # float av = kAbs(v); + # float b = 0.29 + av * 0.1; + # float h = 0.1 - av * 0.05; + # float r = 0.49; + # float t = v * (0.49 - h); + # float s = 1.0 - av * 0.5; +# + # glEnable(GL_POLYGON_OFFSET_FILL); + # glPushMatrix(); + # glScalef(s, 1.0, s); + # glTranslatef (0, t, 0); + # glPolygonOffset(0.1, 0.1); + # renderBar (r, b, h); + # glTranslatef (0, -2*t, 0); + # glPolygonOffset(0.2, 0.2); + # renderBar (r, b, h); + # glPopMatrix(); +# + # glPushMatrix(); + # glRotatef (90, 1.0, 0.0, 0.0); + # glScalef(s, 1.0, s); + # glTranslatef (0, t, 0); + # glPolygonOffset(0.3, 0.3); + # renderBar (r, b, h); + # glTranslatef (0, -2*t, 0); + # glPolygonOffset(0.4, 0.4); + # renderBar (r, b, h); + # glPopMatrix(); +# + # glPushMatrix(); + # glRotatef (-90, 0.0, 0.0, 1.0); + # glScalef(s, 1.0, s); + # glTranslatef (0, t, 0); + # glPolygonOffset(0.5, 0.5); + # renderBar (r, b, h); + # glTranslatef (0, -2*t, 0); + # glPolygonOffset(0.6, 0.6); + # renderBar (r, b, h); + # glPopMatrix(); + # glDisable(GL_POLYGON_OFFSET_FILL); +# + # if (active) + # colors[KikiGate_sphere_color].glColor(); + # kDisplaySolidSphere(0.20); + +module.exports = Gate diff --git a/coffee/item.coffee b/coffee/item.coffee index a9a59b8..3498390 100644 --- a/coffee/item.coffee +++ b/coffee/item.coffee @@ -16,6 +16,7 @@ class Item extends Actor constructor: -> super @name = @constructor.name + @createMesh?() world.scene.add @mesh if @mesh? @position = new Vector @current_position = new Vector diff --git a/coffee/levels/steps.coffee b/coffee/levels/steps.coffee index 2529200..ae07e98 100644 --- a/coffee/levels/steps.coffee +++ b/coffee/levels/steps.coffee @@ -21,7 +21,7 @@ module.exports = to turn, press "$key(turn left)" or "$key(turn right)" """ player: - coordinates: [3,0,6] + coordinates: [4,4,8] nostatus: 0 exits: [ name: "exit" diff --git a/coffee/light.coffee b/coffee/light.coffee index 506b6d2..82cec03 100644 --- a/coffee/light.coffee +++ b/coffee/light.coffee @@ -9,38 +9,33 @@ Item = require './item' class Light extends Item constructor: (pos, radius) -> - @radius = radius ? 4.0 - @setPosition pos if pos? - @setup() - # Controller.world->addObject @ if pos? - - setup: -> - @halo_radius = 1.0 - @quadratic_attenuation = 1.0/(@radius*@radius) - + @radius = radius ? 4 + @intensity = 1 # @ambient_color = colors[KikiLight_base_color] # @diffuse_color = colors[KikiLight_diffuse_color] # @specular_color = colors[KikiLight_specular_color] + @point = new THREE.PointLight 0xffffff, @intensity, @radius, 2 + geom = new THREE.SphereGeometry 0.3, 16, 16 + mat = new THREE.MeshLambertMaterial + color: 0xffffff + side: THREE.FrontSide + shading: THREE.SmoothShading + transparent: true + opacity: 0.7 + emissive: 0xffff00 + emissiveIntensity: 0.9 + + @mesh = new THREE.Mesh geom, mat + world.scene.add @point + @setPosition pos if pos? + super - @initialize() - - setPosition: (pos) -> - # KLight::setPosition (KVector(pos[X], pos[Y], pos[Z], 1.0)) - super pos - - display: () -> - # if (light_number == 0) return -# - # KLight::setPosition (KVector(position[X], position[Y], position[Z], 1.0)); -# - # glDepthMask (false); - # glPushMatrix(); - # colors[KikiLight_halo_color].glColor(); - # position.glTranslate(); -# - # KikiBillBoard::displayTextureWithSize - # (Controller.world->getTextureId (KikiWorld::TEXTURE_GRADIENT), 1.0); - # glPopMatrix(); - # glDepthMask (true); + del: -> + world.scene.remove @point + super + setPosition: (pos) -> + super pos + @point.position.copy @position + module.exports = Light diff --git a/coffee/main.coffee b/coffee/main.coffee index 9199875..bb962ef 100644 --- a/coffee/main.coffee +++ b/coffee/main.coffee @@ -11,7 +11,9 @@ World = require './world' class Kiki extends Stage - constructor: (@view) -> super @view + constructor: (@view) -> + super @view + @view.focus() start: -> @@ -37,11 +39,12 @@ class Kiki extends Stage animationStep: (step) => @world.step step reset: -> - @elem.style.display = 'block' @resume() + @start() stop: -> - @elem.style.display = 'none' + World.deinit() + @elem.remove() @pause() resized: (w,h) -> @world.resized w, h diff --git a/coffee/player.coffee b/coffee/player.coffee index 3b4f7f8..bb4eaa0 100644 --- a/coffee/player.coffee +++ b/coffee/player.coffee @@ -39,8 +39,8 @@ class Player extends Bot right: 'd' lookUp: 'up' lookDown: 'down' - shoot: 'space' - jump: 'enter' + shoot: 'enter' + jump: 'space' view: 'c' @look_action = null diff --git a/coffee/switch.coffee b/coffee/switch.coffee new file mode 100644 index 0000000..5f9bda6 --- /dev/null +++ b/coffee/switch.coffee @@ -0,0 +1,113 @@ +# 0000000 000 000 000 000000000 0000000 000 000 +# 000 000 0 000 000 000 000 000 000 +# 0000000 000000000 000 000 000 000000000 +# 000 000 000 000 000 000 000 000 +# 0000000 00 00 000 000 0000000 000 000 + +log = require "/Users/kodi/s/ko/js/tools/log" +Quaternion = require './lib/quaternion' +Vector = require './lib/vector' +Action = require './action' +Light = require './light' +Item = require './item' + +class Switch extends Item + + constructor: (active=false) -> + + super + + @angle = 0 + @light = null + @active = null + @sound_on = 'SWITCH_ON' + @sound_off = 'SWITCH_OFF' + + @SWITCH_OFF_EVENT = @addEventWithName "off" + @SWITCH_ON_EVENT = @addEventWithName "on" + @SWITCHED_EVENT = @addEventWithName "switched" + + @addAction new Action @, Action.TOGGLE, "toggle", 0 + @addAction new Action @, Action.ROTATE, "rotation", 2000, Action.CONTINUOUS + + @setActive active + + createMesh: () -> + log 'switch createMesh' + torusRadius = 0.05 + t1 = new THREE.TorusGeometry 0.5-torusRadius, torusRadius, 16, 32 + @mat = new THREE.MeshPhongMaterial + color: 0x0000ff + side: THREE.FrontSide + shading: THREE.SmoothShading + transparent: true + opacity: 0.9 + shininess: 0.99 + @mesh = new THREE.Mesh t1, @mat + + t2 = new THREE.TorusGeometry 0.5-torusRadius, torusRadius, 16, 32 + t3 = new THREE.TorusGeometry 0.5-torusRadius, torusRadius, 16, 32 + t2.rotateY Vector.DEG2RAD 90 + t3.rotateX Vector.DEG2RAD 90 + t2.merge t3 + @tors = new THREE.Mesh t2, @mat + @mesh.add @tors + @mesh + + bulletImpact: -> @setActive not @active + + del: () -> @light?.del() + + lightDeleted: () -> @light = null + + setActive: (status) -> + log "switch #{@name} active:#{status}" + if @active != status + @active = status + + if @active + # start the orbit rotation + @startTimedAction @getActionWithId Action.ROTATE + world.playSound @sound_on + @events[@SWITCH_ON_EVENT].triggerActions() + # log 'createLight at pos:', @position + @light = new Light @position, 10.0 + @light.on 'deleted', @lightDeleted + else + @stopAction @getActionWithId Action.ROTATE + + world.playSound @sound_off + @events[@SWITCH_OFF_EVENT].triggerActions() + + if @light + @light.del() + @light = null + + @events[@SWITCHED_EVENT].triggerActions() + + setPosition: (pos) -> + # log "setPosition #{@light?} pos:", pos + super pos + @light?.setPosition @position + + animate: (f) -> + @angle += f * 360 + @mesh.quaternion.copy Quaternion.rotationAroundVector @angle, new Vector 0,1,0 + @tors.quaternion.copy Quaternion.rotationAroundVector @angle/2, new Vector 0,0,1 + # @tort.quaternion.copy Quaternion.rotationAroundVector @angle/2, new Vector 0,0,1 + + performAction: (action) -> + + if action.id == Action.TOGGLE + @toggle() + log "Switch.performAction 'toggle'" + else + @animate action.getRelativeDelta() + + # render: () -> + + # if (active) + # colors[KikiSwitch_sphere_color].glColor() + # kDisplaySolidSphere(0.3); + +module.exports = Switch diff --git a/coffee/wall.coffee b/coffee/wall.coffee index ea72eef..87c28e7 100644 --- a/coffee/wall.coffee +++ b/coffee/wall.coffee @@ -17,18 +17,14 @@ class Wall extends Item side: THREE.FrontSide shading: THREE.SmoothShading transparent: true - opacity: 0.9 - shininess: 0.99 - + opacity: 0.85 + shininess: 5 @mesh = new THREE.Mesh @geom, @mat - world.scene.add @mesh - @mesh.matrixAutoUpdate = true - - s = 0.45 - d = 0.5 - super + # s = 0.45 + # d = 0.5 + # glDisable(GL_LIGHTING); # colors[KikiWall_base_color].glColor(); # glDepthMask(false); diff --git a/coffee/world.coffee b/coffee/world.coffee index 6d5e4e4..6b7e728 100644 --- a/coffee/world.coffee +++ b/coffee/world.coffee @@ -13,6 +13,7 @@ log = require "/Users/kodi/s/ko/js/tools/log" Pos = require './lib/pos' Size = require './lib/size' Cell = require './cell' +Gate = require './gate' Light = require './light' Player = require './player' Cage = require './cage' @@ -109,19 +110,36 @@ class World extends Actor @cells = [] @size = new Pos() @depth = -Number.MAX_SAFE_INTEGER - + + @deinit: () -> + world = null + @init: (view) -> return if world? + + @initGlobal() + + log "create world in view:", view + world = new World view + world.name = 'world' + global.world = world + Timer.init() + world.create first @levelList + world + + @initGlobal: () -> + + return if @levelList.length global.rot0 = new Quaternion() - global.rotz180 = Quaternion.rotationAroundVector(180, Vector(0,0,1)) - global.rotz90 = Quaternion.rotationAroundVector(90, Vector(0,0,1)) - global.roty270 = Quaternion.rotationAroundVector(270, Vector(0,1,0)) - global.roty180 = Quaternion.rotationAroundVector(180, Vector(0,1,0)) - global.roty90 = Quaternion.rotationAroundVector(90, Vector(0,1,0)) - global.roty0 = Quaternion.rotationAroundVector(0, Vector(0,1,0)) - global.rotx180 = Quaternion.rotationAroundVector(180, Vector(1,0,0)) - global.rotx90 = Quaternion.rotationAroundVector(90, Vector(1,0,0)) + global.rotz180 = Quaternion.rotationAroundVector 180, new Vector 0,0,1 + global.rotz90 = Quaternion.rotationAroundVector 90, new Vector 0,0,1 + global.roty270 = Quaternion.rotationAroundVector 270, new Vector 0,1,0 + global.roty180 = Quaternion.rotationAroundVector 180, new Vector 0,1,0 + global.roty90 = Quaternion.rotationAroundVector 90, new Vector 0,1,0 + global.roty0 = Quaternion.rotationAroundVector 0, new Vector 0,1,0 + global.rotx180 = Quaternion.rotationAroundVector 180, new Vector 1,0,0 + global.rotx90 = Quaternion.rotationAroundVector 90, new Vector 1,0,0 # 000 00000000 000 000 00000000 000 0000000 # 000 000 000 000 000 000 000 @@ -164,15 +182,6 @@ class World extends Actor # import the levels for levelName in @levelList @levelDict[levelName] = require "./levels/#{levelName}" - - # log 'levelDict', @levelDict - log "create world in view:", view - world = new World view - world.name = 'world' - global.world = world - Timer.init() - world.create first @levelList - world # 0000000 00000000 00000000 0000000 000000000 00000000 # 000 000 000 000 000 000 000 000 @@ -224,24 +233,20 @@ class World extends Actor # ............................................................ exits - if @dict.exits? and false - log "exits" + if @dict.exits? + log "exits", @dict.exits exit_id = 0 for entry in @dict.exits exit_gate = new Gate entry["active"] - if "name" in entry - name = entry["name"] - else - name = "exit "+str(exit_id) - exit_gate.setName name + exit_gate.name = entry["name"] ? "exit #{exit_id}" - exit_action = once "exit " + str(exit_id) - delay_action = once (a=exit_action) -> Timer.addAction a + # exit_action = @once "exit #{exit_id}" + # delay_action = @once (a=exit_action) -> Timer.addAction a # exit_gate.getEventWithName("enter").addAction(delay_action) if entry.position? pos = @decenter entry.position - else if "coordinates" in entry + else if entry.coordinates? pos = new Pos entry.coordinates @addObjectAtPos exit_gate, pos exit_id += 1 @@ -249,26 +254,23 @@ class World extends Actor # ............................................................ creation if @dict.create? - log "create" if _.isFunction @dict.create + log "@dict.create function" @dict.create() - # else + else + log "@dict.create not a function!" # exec @dict["create"] in globals() # ............................................................ player @player = new Player - player_dict = @dict.player # log "player_dict", player_dict - if player_dict.orientation? - @player.setOrientation player_dict.orientation - else - @player.setOrientation roty90 + @player.setOrientation @dict.player.orientation ? roty90 - if player_dict.position? - @addObjectAtPos @player, @decenter player_dict.position - else if player_dict.coordinates? - @addObjectAtPos @player, new Pos player_dict.coordinates + if @dict.player.position? + @addObjectAtPos @player, @decenter @dict.player.position + else if @dict.player.coordinates? + @addObjectAtPos @player, new Pos @dict.player.coordinates # if player_dict.nostatus? # if player_dict.nostatus or @preview @@ -289,13 +291,13 @@ class World extends Actor # ............................................................ init # @init() # tell the world that we are finished - restart: (self) -> + restart: () -> # restores the player status and restarts the current level @player.status.setMoves 0 @player.reborn() @create() - finish: (self) -> + finish: () -> # saves the current level status in highscore file highscore.levelFinished world.level_name, Controller.player.getStatus().getMoves() @@ -305,24 +307,20 @@ class World extends Actor # 000 000 000 000 000 000 000 000 000 000 000 000 000 000 # 000 000 00000000 0000000 00000000 000 000 0000000 000 000 000 00000000 000 000 - resetPlayer: (self) -> + resetPlayer: () -> # reset the player to it's original position and orientation - - player_dict = @dict["player"] - player = Controller.getPlayer() - - if "reset orientation" in player_dict - player.setOrientation player_dict["reset orientation"] - else if "orientation" in player_dict - player.setOrientation player_dict["orientation"] + log 'world.resetPlayer', @dict.player + if @dict.player.resetOrientation? + @player.setOrientation @dict.player.resetOrientation + else if @dict.player.orientation? + @player.setOrientation @dict.player.orientation else - player.setOrientation rot0 + @player.setOrientation rot0 - if "reset position" in player_dict - world.moveObjectToPos player, world.decenter(player_dict["reset position"]) + if @dict.player.resetPosition? + world.moveObjectToPos @player, world.decenter @dict.player.resetPosition else - world.moveObjectToPos player, world.decenter(player_dict["position"]) - + world.moveObjectToPos @player, world.decenter @dict.player.position # 0000000 0000000 000000000 000 0000000 000 000 # 000 000 000 000 000 000 000 0000 000