From bcd77d48038d81f646f8329b0b41e95c06f8d299 Mon Sep 17 00:00:00 2001 From: Raymond Hulha Date: Wed, 20 Nov 2013 18:37:17 +0100 Subject: [PATCH] Woot ! Got the animation and blood working: Added walk animation on player move Added Blood class Added pellet logic to ShotgunComponent Added health point deduction to ShotgunComponent Added death animation activation on death --- js/loadScene.js | 10 ++++-- lib/Blood.js | 57 ++++++++++++++++++++++++++++++ lib/ShotgunComponent.js | 76 +++++++++++++++++++++++++++++++++++----- res/flare.png | Bin 0 -> 530 bytes 4 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 lib/Blood.js create mode 100644 res/flare.png diff --git a/js/loadScene.js b/js/loadScene.js index 654ce26..fbe3bc4 100644 --- a/js/loadScene.js +++ b/js/loadScene.js @@ -124,13 +124,15 @@ require([ Game.userEntity.setComponent(new FlashlightComponent()); var zombie = loader.getCachedObjectForRef("zombie_idle/entities/Zombie_Geo_0.entity"); - zombie.removeFromWorld(); - var z2 = EntityUtils.clone(goo.world, zombie); + //zombie.removeFromWorld(); // this breaks the parent child relationship, the parent has the animation that I need... + var z2 = zombie; // EntityUtils.clone(goo.world, zombie); z2.transformComponent.setTranslation(-2,0,2); 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(); + // z2.addToWorld(); + Game.zombie = zombie; + Game.zombieRoot = zombie.transformComponent.parent.entity; goo.renderer.domElement.id = 'goo'; document.body.appendChild(goo.renderer.domElement); @@ -342,6 +344,8 @@ require([ node.doorPos = navMesh.room[entity.room].door[node.curNode.door].center; entity.aIComponent.setActiveByName("Zombie-Idle", false); node.state = 1; + var eac = Game.zombieRoot.animationComponent; + eac.transitionTo( eac.getStates()[1]); } } diff --git a/lib/Blood.js b/lib/Blood.js new file mode 100644 index 0000000..665f33a --- /dev/null +++ b/lib/Blood.js @@ -0,0 +1,57 @@ + +define(['goo/entities/EntityUtils', 'goo/math/Vector3', 'goo/renderer/TextureCreator', 'goo/renderer/Material', 'goo/renderer/shaders/ShaderLib', + 'goo/particles/ParticleUtils', 'goo/entities/components/ParticleComponent'], function( EntityUtils, Vector3, TextureCreator, Material, ShaderLib, ParticleUtils, ParticleComponent) { + 'use strict'; + + function Blood( goo) { + + this.goo = goo; + + var particleTex = new TextureCreator().loadTexture2D('../res/flare.png'); + particleTex.generateMipmaps = true; + + //texture.wrapS = 'EdgeClamp'; + //texture.wrapT = 'EdgeClamp'; + + var material = this.material = Material.createMaterial(ShaderLib.particles); + material.setTexture('DIFFUSE_MAP', particleTex); + material.blendState.blending = 'AlphaBlending'; // 'AdditiveBlending'; + material.cullState.enabled = false; + material.depthState.write = false; + material.renderQueue = 2001; + + this.config = { + //particleCount : 200, + timeline : [ + {timeOffset: 0.00, color: [1, 0, 0, 0.5], size: 0.3, spin: 0, mass: 0}, + //{timeOffset: 0.25, color: [1, 0, 0, 0.5], size: 50.0}, + //{timeOffset: 0.25, color: [1, 0, 0, 0.5], size: 100.0}, + {timeOffset: 0.25, color: [1, 0, 0, 0], size: 3.0,} + ], + emitters : [{ + totalParticlesToSpawn : 1, + releaseRatePerSecond : 5, + minLifetime : 1.25, + maxLifetime : 1.25, + getEmissionVelocity : function (particle/*, particleEntity*/) { + var vec3 = particle.velocity; + return ParticleUtils.getRandomVelocityOffY(vec3, 0, Math.PI * 15 / 180, 5); + } + }] + }; + + } + + Blood.prototype.spawn = function( pos) { + var particleComponent = new ParticleComponent(this.config); + //particleComponent.emitters[0].influences.push(ParticleUtils.createConstantForce(new Vector3(0, -20, 0))); + + var entity = EntityUtils.createTypicalEntity( this.goo.world, this.material, particleComponent.meshData, [pos.x,pos.y,pos.z]); + entity.meshRendererComponent.isPickable = false; + entity.setComponent(particleComponent); + entity.addToWorld(); + return entity; + } + + return Blood; +}); diff --git a/lib/ShotgunComponent.js b/lib/ShotgunComponent.js index e22a1ed..652e0b4 100644 --- a/lib/ShotgunComponent.js +++ b/lib/ShotgunComponent.js @@ -8,6 +8,10 @@ define([ 'goo/renderer/Material', 'goo/addons/howler/systems/HowlerSystem', 'goo/addons/howler/components/HowlerComponent', + 'goo/math/Ray', + 'goo/math/Vector3', + 'lib/Blood' + ], function( Component, Game, @@ -17,14 +21,17 @@ define([ ShapeCreator, Material, HowlerSystem, - HowlerComponent + HowlerComponent, + Ray, + Vector3, + Blood ) { 'use strict'; var shotgun; var sound; function ShotgunComponent(){ this.type = "ShotgunComponent"; - sound = new Howl({urls: ["res/sounds/ssg.ogg", "res/sounds/ssg.mp3"], volume:1.0}); + sound = new Howl({urls: ["res/sounds/ssg.ogg", "res/sounds/ssg.mp3"], volume:0.3}); var mesh = ShapeCreator.createCylinder( 30, 2); var mat = Material.createMaterial( ShaderLib.simpleLit, 'BoxMaterial'); @@ -54,7 +61,58 @@ define([ function resetSSG() { shotgun.transformComponent.setRotation( 0.15, 0.1, 0); - }; + } + + var blood; + + var pickingStore = {}; + var md_pos = new Vector3(); + var md_dir = new Vector3(); + var md_ray = new Ray(); + function pellet( camera, x, y, w, h) { + + blood = blood || new Blood(Game.goo); + + Game.goo.renderer.pick( x, y, pickingStore, camera); + if( pickingStore.id == -1) + return; + camera.getPickRay( x, y, w, h, md_ray); + md_ray.direction.mul( pickingStore.depth); + md_ray.origin.add( md_ray.direction); + + var entity = Game.goo.world.entityManager.getEntityById(pickingStore.id); + + console.log(entity); + + if( ! entity.transformComponent.parent ) + return; + + + var p = entity.transformComponent.parent.entity; + if( ! p.animationComponent ) + return; + var eac = p.animationComponent; + + blood.spawn(md_ray.origin); + + 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 + Game.zombie.aIComponent.setActiveByName("Zombie-PathFind", false); + } + } + + function randInt(max) { + return Math.floor(Math.random()*max); + } + function randBlood() { + return randInt(200)-100; + } + function mouseButton1(bool0){ //console.log(bool0); if(true == bool0){ @@ -70,14 +128,14 @@ define([ Game.goo.pick( x, y, function( id, depth){ if( id < 0) return; - console.log( depth); - - var pos = Game.viewCam.cameraComponent.camera.getWorldCoordinates( x, y, w, h, depth); - //blood.spawn([pos.x,pos.y,pos.z]); - var entity = Game.goo.world.entityManager.getEntityById(id); + var camera = Game.viewCam.cameraComponent.camera + + for( var i=0; i<10; i++) { + pellet( camera, x+randBlood(), y+randBlood(), w, h); + } }); } } - }; + } return ShotgunComponent; }); \ No newline at end of file diff --git a/res/flare.png b/res/flare.png new file mode 100644 index 0000000000000000000000000000000000000000..1eedb4f5a62107ba0efb190ff42db917576bd4b8 GIT binary patch literal 530 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}Y)RhkE*aZ~g-O$3 z%4eA!ta%OG2@C#8Cny?eC#)n} zS*0d4k)!*AY{kXTJTuH47X-PqzItevv3CQnxYj=Rv&`&M*v~hrczPLsUK_zO-)C5l4Y1v=T SVb>3g9R^QVKbLh*2~7YNfX_1k literal 0 HcmV?d00001