From 3b2aaec0c7c15f0974693a994af313b542d30078 Mon Sep 17 00:00:00 2001 From: Brett Unzaga Date: Thu, 28 Nov 2013 01:42:34 -0700 Subject: [PATCH] Moved a few things to components, and Modules. Reworking how to spawn zombies. --- js/loadScene.js | 263 +++++--------------------------------- lib/AIComponent.js | 23 +++- lib/HealthComponent.js | 26 ++++ lib/ShotgunComponent.js | 28 +++-- lib/Zombie.js | 271 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 364 insertions(+), 247 deletions(-) create mode 100644 lib/HealthComponent.js create mode 100644 lib/Zombie.js diff --git a/js/loadScene.js b/js/loadScene.js index 30dee7a..98f5cea 100644 --- a/js/loadScene.js +++ b/js/loadScene.js @@ -24,7 +24,8 @@ require([ 'goo/util/rsvp', 'lib/ShotgunComponent', 'lib/FlashlightComponent', - 'goo/math/Plane' + 'goo/math/Plane', + 'lib/Zombie' ], function ( GooRunner, @@ -53,11 +54,14 @@ require([ RSVP, ShotgunComponent, FlashlightComponent, - Plane + Plane, + Zombie ) { 'use strict'; - + Vector3.UP = Object.freeze(new Vector3(0,1,0)); + Vector3.DOWN = Object.freeze(new Vector3(0,-1,0)); + Vector3.FORWARD = Object.freeze(new Vector3(0,0,1)); function init() { // If you try to load a scene without a server, you're gonna have a bad time @@ -83,74 +87,31 @@ require([ var point; // The Loader takes care of loading data from a URL... var loader = new DynamicLoader({world: goo.world, rootPath: 'res'}); - var loader2 = new DynamicLoader({world: goo.world, rootPath: 'res'}); - var loader3 = new DynamicLoader({world: goo.world, rootPath: 'res'}); var promises = []; promises.push(loader.loadFromBundle('project.project', 'root.bundle')); - promises.push(loader.loadFromBundle('project.project', 'zombie.bundle')); - promises.push(loader2.loadFromBundle('project.project', 'zombie.bundle')); - promises.push(loader3.loadFromBundle('project.project', 'zombie.bundle')); promises.push(loader.loadFromBundle('project.project', 'Point.bundle')); + Zombie.setupRefs(promises); RSVP.all(promises) .then(function(){ initGoobers(goo); }) .then(null, function(e){ - alert (e); + console.log(e.stack); }); - - var spawnZombie = function () { - var loader_ = new DynamicLoader({world: goo.world, rootPath: 'res'}); - loader_.loadFromBundle('project.project', 'zombie.bundle').then( function() { - var z2 = loader_.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - z2.transformComponent.setTranslation(0,0,0); - z2.transformComponent.setUpdated(); - z2.setComponent(new AIComponent(z2)); - z2.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0); - z2.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1); - }); - } - var respawnZombie = function ( zx) { - if( zx.dmg > 100 ) { - zx.dmg = 0; - var p = zx.transformComponent.parent.entity; - var eac = p.animationComponent; - eac.layers[0]._steadyStates['mixamo_com__']._sourceTree._clipInstance._loopCount=-1; - //eac.transitionTo( eac.getStates()[1]); - eac.layers[0].setCurrentStateByName(eac.getStates()[1]); - zx.aIComponent.setActiveByName("Zombie-PathFind", true); - zx.transformComponent.setTranslation(50,0,0); - } - }; - - var respawnZombie2 = function () { - respawnZombie( window.z2); - respawnZombie( window.z3); - respawnZombie( window.z4); - }; - - //setInterval( respawnZombie2, 17000); - - document.addEventListener('keypress', function(e){ - if( e.keyCode == 114) { // r - //spawnZombie(); - respawnZombie2(); - } - }, false); - - function initGoobers(goo){ + function initGoobers(){ console.log(loader._configs); point = loader.getCachedObjectForRef("Point/entities/RootNode.entity"); point.transformComponent.setScale(0.03, 0.03, 0.03); point.removeFromWorld(); navMesh = generateRoomsFromMesh(loader.getCachedObjectForRef("NavMesh/entities/RootNode.entity")); - //for(var i in navMesh.vert){ - // var p = EntityUtils.clone(goo.world, point); - // p.transformComponent.setTranslation(navMesh.vert[i]); - // p.addToWorld(); - //} + Game.navMesh = navMesh; + for(var i in navMesh.vert){ + var p = EntityUtils.clone(goo.world, point); + p.transformComponent.setTranslation(navMesh.vert[i]); + p.addToWorld(); + } generateDoors(navMesh); @@ -179,44 +140,26 @@ require([ Game.userEntity.setComponent(new FlashlightComponent()); - var zombie = loader.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - //zombie.removeFromWorld(); // this breaks the parent child relationship, the parent has the animation that I need... - window.z2 = zombie; // EntityUtils.clone(goo.world, zombie); - z2.transformComponent.setTranslation(50,0,50); + //console.log(navRef); + + var z1 = Zombie.getRef(); + z1.transformComponent.setTranslation(4,0,4); + z1.transformComponent.setUpdated(); + z1.skip = false; + + var z2 = Zombie.getRef(); + z2.transformComponent.setTranslation(-4,0,-4); z2.transformComponent.setUpdated(); - z2.setComponent(new AIComponent(z2)); - z2.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0); - z2.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1); - // z2.addToWorld(); - - window.z3 = loader2.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - z3.transformComponent.setTranslation(-50,0,-50); - z3.transformComponent.setUpdated(); - z3.setComponent(new AIComponent(z3)); - z3.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0); - z3.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1); - - window.z4 = loader3.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - z4.transformComponent.setTranslation(50,0,-50); - z4.transformComponent.setUpdated(); - z4.setComponent(new AIComponent(z4)); - z4.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0); - z4.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1); + z2.skip = false; - //console.log(navRef); goo.renderer.setClearColor(0, 0, 0, 1); goo.renderer.domElement.id = 'goo'; document.body.appendChild(goo.renderer.domElement); + goo.startGameLoop(); + + } - function addZombie( loader, x, y, z ) { - var z = loader.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - z.transformComponent.setTranslation( x, y, z); - z.transformComponent.setUpdated(); - z.setComponent(new AIComponent(z)); - z.aIComponent.addBehavior({name:"Zombie-Idle", update:ZombieIdle}, 0); - z.aIComponent.addBehavior({name:"Zombie-PathFind", update:ZombiePathFind}, 1); - } var goal; var viewCam; @@ -307,7 +250,7 @@ require([ var closedList = {}; // var currentRoom; - function getPathRoomToRoom(roomStart, roomGoal){ + Game.getPathRoomToRoom = function(roomStart, roomGoal){ if(null == roomStart || null == roomGoal){return null;} // console.log("Getting Path from start:"+roomStart+" to goal:"+roomGoal); openList.push({room:roomStart, parent:null}); @@ -344,145 +287,7 @@ require([ return path; } } - Vector3.UP = Object.freeze(new Vector3(0,1,0)); - Vector3.DOWN = Object.freeze(new Vector3(0,-1,0)); - Vector3.FORWARD = Object.freeze(new Vector3(0,0,1)); - function ZombieIdle(entity, node){ - switch(node.state){ - case 0: - case null: - case undefined: - Vector3.add(entity.transformComponent.transform.translation, Vector3.UP, ray.origin); - ray.direction = Vector3.DOWN; - picking.castRay(ray, function(hit){ - if(null != hit){ - entity.room = hit.entity.navID; - // console.log("I am in room "+entity.room); - //console.log(hit); - } - }, 1); - node.state = 1; - break; - case 1: - // if player near... - break; - } - } - function ZombiePathFind(entity, node){ - switch(node.state){ - case -1: - case null: - case undefined: - Game.register("PlayerMoved", this, playerMoved); - node.dir = new Vector3(); - node.state = 0; - node.speed = 100; - break; - case 0: - break; - case 1: - Vector3.add(entity.transformComponent.transform.translation, Vector3.UP, ray.origin); - ray.direction = Vector3.DOWN; - picking.castRay(ray, function(hit){ - if(null != hit){ - //console.log("I am in room "+hit.entity.navID+" goal is "+node.curNode.room); - //entity.room = hit.entity.navID; - entity.transformComponent.transform.translation.y = hit.point.y; - } - }, 1); - - if(entity.room == node.goalRoom){ - node.state = 2; - console.log("Going to player position..."); - return; - } - if(entity.transformComponent.transform.translation.distance(node.doorPos) <= 0.1){ - //if(entity.room == node.curNode.room){ - // console.log("got to room "+node.curNode.room); - entity.room = node.curNode.room; - node.curNode = node.curNode.next; - if(node.curNode != null){ - // console.log("going to room "+node.curNode.room+" from room "+node.curNode.previous.room); - node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center; - } - } - - Vector3.sub(node.doorPos, entity.transformComponent.transform.translation, node.dir); - node.dir.normalize(); - node.dir.y = 0; - - entity.transformComponent.transform.rotation.lookAt(node.dir, Vector3.UP); - entity.transformComponent.transform.applyForwardVector(Vector3.FORWARD, node.dir); - entity.transformComponent.addTranslation(Vector3.mul(node.dir, node.speed * Time.dt )); - entity.transformComponent.setUpdated(); - - break; - case 2: - if(entity.transformComponent.transform.translation.distance(Game.userEntity.transformComponent.transform.translation) <= 1.0){ - console.log("at player position..."); - node.state = 3; - } - - Vector3.sub(Game.userEntity.transformComponent.transform.translation, entity.transformComponent.transform.translation, node.dir); - node.dir.normalize(); - node.dir.y = 0; - - entity.transformComponent.transform.rotation.lookAt(node.dir, Vector3.UP); - entity.transformComponent.transform.applyForwardVector(Vector3.FORWARD, node.dir); - entity.transformComponent.addTranslation(Vector3.mul(node.dir, node.speed * Time.dt )); - entity.transformComponent.setUpdated(); - break; - case 3: - entity.aIComponent.setActiveByName("Zombie-Idle", true); - break; - case 4: - node.goalRoom = Game.userEntity.room; - - if(node.goalRoom == entity.room){ - console.log("I am already in "+node.goalRoom); - node.state = 2; - return; - } - //console.log("(case 4)I am not in "+node.goalRoom+" getting path."); - node.path = getPathRoomToRoom(entity.room, node.goalRoom); - if(node.path != null){ - node.curNode = node.path.first; - node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center; - entity.aIComponent.setActiveByName("Zombie-Idle", false); - node.state = 1; - var eac = entity.transformComponent.parent.entity.animationComponent; - if( !entity.dmg || entity.dmg < 100) - eac.transitionTo( eac.getStates()[1]); - } - break; - } - function playerMoved(room, pos){ - // console.log("playerMoved:"+room+","+pos); - node.goalRoom = room; - - if(node.goalRoom == entity.room){ - console.log("I am already in "+node.goalRoom); - node.state = 2; - return; - } - //console.log("playerMoved():I am not in "+node.goalRoom+" getting path."); - node.path = getPathRoomToRoom(entity.room, node.goalRoom); - if(node.path != null){ - node.curNode = node.path.first; - node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center; - entity.aIComponent.setActiveByName("Zombie-Idle", false); - node.state = 1; - var eac = entity.transformComponent.parent.entity.animationComponent; - if( !entity.dmg || entity.dmg < 100) - eac.transitionTo( eac.getStates()[1]); - } - else{ - node.state = 4; - } - } - } - function generateRoomsFromMesh(navRootEntity){ var nav = {room:[], vert:{}} for(var i = 0, ilen = navRootEntity.transformComponent.children.length; i < ilen; i++){ @@ -592,10 +397,10 @@ require([ room2.door.push(door2); - //var p = EntityUtils.clone(goo.world, point); - //p.transformComponent.setTranslation(door1.center); - //p.transformComponent.setScale(0.1, 0.2, 0.1); - //p.addToWorld(); + var p = EntityUtils.clone(goo.world, point); + p.transformComponent.setTranslation(door1.center); + p.transformComponent.setScale(0.1, 0.2, 0.1); + p.addToWorld(); } } diff --git a/lib/AIComponent.js b/lib/AIComponent.js index e7f4177..5611654 100644 --- a/lib/AIComponent.js +++ b/lib/AIComponent.js @@ -8,6 +8,8 @@ function( Component, Game ){ + var self = function(entity){}; + function AIComponent(entity){ this.type = "AIComponent"; this.behavior = {}; @@ -16,7 +18,7 @@ function( }; AIComponent.prototype = Object.create(Component.prototype); AIComponent.prototype.addBehavior = function(b, priority){ - if(null != this.list[b.name]){console.error("A name for the behavior:"+b.name+" already exists!");} + if(null != this.list[b.name]){console.log(this); console.error("A name for the behavior:"+b.name+" already exists!");} var node = { next:null, previous:null, @@ -26,8 +28,8 @@ function( node[p] = b[p]; } } - node.active = node.active || true; - node.blocking = node.blocking || false; + node.active = node.active == null ? true : node.active; + node.blocking = node.blocking == null ? false : node.blocking; if(null != node.update){ if(null == this.behavior.update){ this.behavior.update = new NodeList(); @@ -83,6 +85,12 @@ function( this.list[b.name] = node; return node; }; + AIComponent.prototype.destroy = function(){ + Game.unregister("Update", this, this.update); + Game.unregister("FixedUpdate", this, this.fixedUpdate); + Game.unregister("RenderUpdate", this, this.renderUpdate); + Game.unregister("LateUpdate", this, this.lateUpdate); + }; AIComponent.prototype.removeBehavior = function(b){ if(null != b.update){ if(null != this.behavior.update){ @@ -121,14 +129,19 @@ function( } this.list = {}; }; - AIComponent.prototype.setActiveByName = function(name, active){ + AIComponent.prototype.getBehaviorByName = function(name){ + if(null == this.list[name]){console.error("No behavior with the name: "+name+" exists!");return;} + return this.list[name]; + } + + /*AIComponent.prototype.setActiveByName = function(name, active){ if(null == this.list[name]){console.error("No behavior with the name: "+name+" exists!");return;} this.list[name].active = active; }; AIComponent.prototype.setBlockingByName = function(name, blocking){ if(null == this.list[name]){console.error("No behavior with the name: "+name+" exists!");return;} this.list[name].blocking = blocking; - }; + };*/ AIComponent.prototype.update = function(){ if(null == this.behavior.update){return;} diff --git a/lib/HealthComponent.js b/lib/HealthComponent.js new file mode 100644 index 0000000..a4c74fd --- /dev/null +++ b/lib/HealthComponent.js @@ -0,0 +1,26 @@ +define([ + 'goo/entities/components/Component' +], function( + Component +){ + + function HealthComponent(ent, n0){ + this.type = "HealthComponent"; + this.entity = ent; + this.maxHealth = this.currentHealth = n0; + } + HealthComponent.prototype = Object.create(Component.prototype); + HealthComponent.prototype.applyDamage = function(n0){ + this.currentHealth -= n0; + console.log("Removing:"+n0+" health."); + console.log(this.currentHealth+"/"+this.maxHealth+" left."); + if(this.currentHealth <= 0){ + + this.entity.aIComponent.getBehaviorByName("Death").active = true; + } + } + HealthComponent.prototype.resetHealth = function(){ + this.currentHealth = this.maxHealth; + } + return HealthComponent; +}); \ No newline at end of file diff --git a/lib/ShotgunComponent.js b/lib/ShotgunComponent.js index 5bfc9f2..63318dd 100644 --- a/lib/ShotgunComponent.js +++ b/lib/ShotgunComponent.js @@ -88,19 +88,23 @@ define([ var p = entity.transformComponent.parent.entity; if( ! p.animationComponent ) return; - var eac = p.animationComponent; + //var eac = p.animationComponent; blood.spawn(md_ray.origin); + + console.log(p); + + p.transformComponent.children[0].entity.healthComponent.applyDamage(7); - if( entity.dmg) entity.dmg += 7; else entity.dmg = 7; - if( entity.dmg && entity.dmg == 7) { + //if( entity.dmg) entity.dmg += 7; else entity.dmg = 7; + //if( entity.dmg && entity.dmg == 7) { //eac.transitionTo( eac.getStates()[1]); // idle, injured_walk, uppercut_jab, dying - } - if( entity.dmg && entity.dmg > 100) { - eac.layers[0]._steadyStates['mixamo_com__']._sourceTree._clipInstance._loopCount=1; - eac.transitionTo( eac.getStates()[3]); // idle, injured_walk, uppercut_jab, dying - entity.aIComponent.setActiveByName("Zombie-PathFind", false); - } + //} + //if( entity.dmg && entity.dmg > 100) { + // eac.layers[0]._steadyStates['mixamo_com__']._sourceTree._clipInstance._loopCount=1; + // eac.transitionTo( eac.getStates()[3]); // idle, injured_walk, uppercut_jab, dying + // entity.aIComponent.setActiveByName("Zombie-PathFind", false); + //} } function randInt(max) { @@ -115,7 +119,7 @@ define([ if(true == bool0){ //console.log("bam"); if(document.pointerLockElement) { - console.log( Game.userEntity.transformComponent.transform.translation.data ); + //console.log( Game.userEntity.transformComponent.transform.translation.data ); sound.play(); shotgun.transformComponent.setRotation( 0.35, 0.1, 0); setTimeout( resetSSG, 250); @@ -124,10 +128,8 @@ define([ var x = w / 2; var y = h / 2; Game.goo.pick( x, y, function( id, depth){ - if( id < 0) - return; + if( id < 0){return;} var camera = Game.viewCam.cameraComponent.camera - for( var i=0; i<10; i++) { pellet( camera, x+randBlood(), y+randBlood(), w, h); } diff --git a/lib/Zombie.js b/lib/Zombie.js new file mode 100644 index 0000000..0a537ea --- /dev/null +++ b/lib/Zombie.js @@ -0,0 +1,271 @@ +define([ + 'goo/entities/EntityUtils', + 'lib/Game', + 'goo/math/Vector3', + 'goo/math/Ray', + 'lib/AIComponent', + 'lib/HealthComponent', + 'lib/Time', + 'goo/loaders/DynamicLoader', + 'goo/util/rsvp' +], function( + EntityUtils, + Game, + Vector3, + Ray, + AIComponent, + HealthComponent, + Time, + DynamicLoader, + RSVP +){ + "use strict"; + var zLoader = []; + + var zRef = []; + var ray = new Ray(); + var count = 0; + var health = 50; + function Zombie(){} + + //Zombie.zRef = []; + //Zombie.ray = new Ray(); + //Zombie.count = 0; + //Zombie.health = 50; + + Zombie.getRef = function(){ + if(zRef.length > 0){ + var z = zRef.shift(); + + return z; + } + } + + Zombie.setupRefs = function(promises){ + console.log("count at time of creation is "+count); + var l0 = new DynamicLoader({world: Game.goo.world, rootPath: 'res'}); + promises.push(l0.loadFromBundle('project.project', 'zombie.bundle')); + zLoader.push(l0); + + var l1 = new DynamicLoader({world: Game.goo.world, rootPath: 'res'}); + promises.push(l1.loadFromBundle('project.project', 'zombie.bundle')); + zLoader.push(l1); + + var l2 = new DynamicLoader({world: Game.goo.world, rootPath: 'res'}); + promises.push(l2.loadFromBundle('project.project', 'zombie.bundle')); + zLoader.push(l2); + + var l3 = new DynamicLoader({world: Game.goo.world, rootPath: 'res'}); + promises.push(l3.loadFromBundle('project.project', 'zombie.bundle')); + zLoader.push(l3); + + var l4 = new DynamicLoader({world: Game.goo.world, rootPath: 'res'}); + promises.push(l4.loadFromBundle('project.project', 'zombie.bundle')); + zLoader.push(l4); + + RSVP.all(promises) + .then(function(){ + for(var i = 0, ilen = zLoader.length; i < ilen;i++){ + var z = zLoader[i].getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); + z.name = "Zombie:"+(count++); + z.setComponent(new AIComponent(z)); + z.setComponent(new HealthComponent(z, health)); + z.aIComponent.addBehavior({name:"Death", update:Zombie.Death, blocking:true, active:false}, 0); + z.aIComponent.addBehavior({name:"Idle", update:Zombie.Idle, blocking:true, active:false}, 10); + z.aIComponent.addBehavior({name:"PathFind", update:Zombie.PathFind, blocking:true, active:false}, 20); + z.skip = true; + zRef.push(z); + //z.removeFromWorld(); + } + }).then(null, function(e){}); + } + + Zombie.Death = function(entity, node){ + switch(node.state){ + case -1: + Game.unregister("PlayerMoved", entity); + console.log("Starting Death State"); + var eac = entity.transformComponent.parent.entity.animationComponent; + eac.layers[0]._steadyStates['mixamo_com__']._sourceTree._clipInstance._loopCount=1; + eac.transitionTo( eac.getStates()[3]); // idle, injured_walk, uppercut_jab, dying + node.fadeTime = Time.time+5.0; + node.state = 0; + break; + case 0: + if(node.fadeTime < Time.time){ + entity.skip = true; + entity.aIComponent.getBehaviorByName("Death").active = false; + entity.aIComponent.getBehaviorByName("Idle").state = -1; + entity.aIComponent.getBehaviorByName("Idle").active = true; + entity.aIComponent.getBehaviorByName("PathFind").active = false; + entity.healthComponent.resetHealth(); + node.state = null; + zRef.push(entity); + var z = Zombie.getRef(); + z.aIComponent.getBehaviorByName("Idle").state = 0; + z.transformComponent.setTranslation((Math.random()*10)-5,0,(Math.random()*10)-5); + z.transformComponent.setUpdated(); + } + break; + + default: + node.state = -1; + break; + } + }; + Zombie.Idle = function(entity, node){ + switch(node.state){ + case -1: + // waiting to respawn + break; + case 0: + entity.skip = false; + Vector3.add(entity.transformComponent.transform.translation, Vector3.UP, ray.origin); + ray.direction = Vector3.DOWN; + Game.picking.castRay(ray, function(hit){ + if(null != hit){ + entity.room = hit.entity.navID; + } + }, 1); + var eac = entity.transformComponent.parent.entity.animationComponent; + eac.layers[0]._steadyStates['mixamo_com__']._sourceTree._clipInstance._loopCount=0; + eac.transitionTo( eac.getStates()[0]); // idle, injured_walk, uppercut_jab, dying + node.state = 1; + break; + case 1: + // if player near... + node.active = false; + entity.aIComponent.getBehaviorByName("PathFind").active = true; + break; + default: + node.state = 0; + break; + } + }; + + Zombie.FollowUser = function(entity, node){ + + }; + + Zombie.AttackUser = function(entity, node){ + + }; + + Zombie.PathFind = function(entity, node){ + function playerMoved(room, pos){ + console.log("playerMoved:"+room+","+pos); + node.goalRoom = room; + + if(node.goalRoom == entity.room){ + console.log("I am already in "+node.goalRoom); + node.state = 2; + return; + } + //console.log("playerMoved():I am not in "+node.goalRoom+" getting path."); + node.path = Game.getPathRoomToRoom(entity.room, node.goalRoom); + if(node.path != null){ + node.curNode = node.path.first; + node.doorPos = Game.navMesh.room[entity.room].door[node.curNode.door].center; + entity.aIComponent.getBehaviorByName("Idle").active = false; + node.state = 1; + var eac = entity.transformComponent.parent.entity.animationComponent; + if( !entity.dmg || entity.dmg < 100) + eac.transitionTo( eac.getStates()[1]); + } + else{ + node.state = 4; + } + } + + switch(node.state){ + case -1: + case null: + case undefined: + Game.register("PlayerMoved", entity, playerMoved); + node.dir = new Vector3(); + node.state = 0; + node.speed = 100; + console.log("now registered for PlayerMoved"); + break; + case 0: + break; + case 1: + Vector3.add(entity.transformComponent.transform.translation, Vector3.UP, ray.origin); + ray.direction = Vector3.DOWN; + Game.picking.castRay(ray, function(hit){ + if(null != hit){ + //console.log("I am in room "+hit.entity.navID+" goal is "+node.curNode.room); + //entity.room = hit.entity.navID; + entity.transformComponent.transform.translation.y = hit.point.y; + } + }, 1); + + if(entity.room == node.goalRoom){ + node.state = 2; + console.log("Going to player position..."); + return; + } + if(entity.transformComponent.transform.translation.distance(node.doorPos) <= 0.1){ + //if(entity.room == node.curNode.room){ + // console.log("got to room "+node.curNode.room); + entity.room = node.curNode.room; + node.curNode = node.curNode.next; + if(node.curNode != null){ + // console.log("going to room "+node.curNode.room+" from room "+node.curNode.previous.room); + node.doorPos = Game.navMesh.room[entity.room].door[node.curNode.door].center; + } + } + + Vector3.sub(node.doorPos, entity.transformComponent.transform.translation, node.dir); + node.dir.normalize(); + node.dir.y = 0; + + entity.transformComponent.transform.rotation.lookAt(node.dir, Vector3.UP); + entity.transformComponent.transform.applyForwardVector(Vector3.FORWARD, node.dir); + entity.transformComponent.addTranslation(Vector3.mul(node.dir, node.speed * Time.dt )); + entity.transformComponent.setUpdated(); + + break; + case 2: + if(entity.transformComponent.transform.translation.distance(Game.userEntity.transformComponent.transform.translation) <= 1.0){ + console.log("at player position..."); + node.state = 3; + } + + Vector3.sub(Game.userEntity.transformComponent.transform.translation, entity.transformComponent.transform.translation, node.dir); + node.dir.normalize(); + node.dir.y = 0; + + entity.transformComponent.transform.rotation.lookAt(node.dir, Vector3.UP); + entity.transformComponent.transform.applyForwardVector(Vector3.FORWARD, node.dir); + entity.transformComponent.addTranslation(Vector3.mul(node.dir, node.speed * Time.dt )); + entity.transformComponent.setUpdated(); + break; + case 3: + entity.aIComponent.setActiveByName("Zombie-Idle", true); + break; + case 4: + node.goalRoom = Game.userEntity.room; + + if(node.goalRoom == entity.room){ + console.log("I am already in "+node.goalRoom); + node.state = 2; + return; + } + //console.log("(case 4)I am not in "+node.goalRoom+" getting path."); + node.path = Game.getPathRoomToRoom(entity.room, node.goalRoom); + if(node.path != null){ + node.curNode = node.path.first; + node.doorPos = Game.navMesh.room[entity.room].door[node.curNode.door].center; + entity.aIComponent.setActiveByName("Zombie-Idle", false); + node.state = 1; + var eac = entity.transformComponent.parent.entity.animationComponent; + if( !entity.dmg || entity.dmg < 100) + eac.transitionTo( eac.getStates()[1]); + } + break; + }; + } + + return Zombie; +}); \ No newline at end of file