Created
November 7, 2014 19:32
-
-
Save nickjanssen/5ac79857613ef1be9cbc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('components.scene.wield-item', ['ces', 'three', 'engine.texture-loader']) | |
.config(function ($componentsProvider) { | |
'use strict'; | |
$componentsProvider.addComponentData({ | |
'wieldItem': { | |
item: 14 | |
} | |
}); | |
}) | |
.factory('WielItemSystem', function (System, THREE, TextureLoader) { | |
'use strict'; | |
var WielItemSystem = System.extend({ | |
addedToWorld: function (world) { | |
var sys = this; | |
sys._super(world); | |
world.entityAdded('wieldItem').add(function (entity) { | |
var wieldItemData = entity.getComponent('wieldItem'), | |
wieldItem; | |
var planeGeo = new THREE.PlaneGeometry(1.0, 1.0, 1, 1); | |
wieldItem = new THREE.Mesh(planeGeo, new THREE.MeshLambertMaterial()); | |
wieldItem.material.side = THREE.DoubleSide; | |
wieldItem.geometry.dynamic = true; | |
if (wieldItemData.item) { | |
TextureLoader.load('assets/images/items/' + wieldItemData.item + '.png') | |
.then(function (texture) { | |
// texture.needsUpdate = true; | |
wieldItem.material.map = texture; | |
wieldItem.material.needsUpdate = true; | |
wieldItem.geometry.buffersNeedUpdate = true; | |
wieldItem.geometry.uvsNeedUpdate = true; | |
wieldItem.material.transparent = true; | |
}); | |
} | |
entity.renderOffset = new THREE.Vector3(); | |
wieldItemData.weaponPivot = new THREE.Object3D(); | |
wieldItemData.weaponPivot.add(wieldItem); | |
wieldItemData.wieldItem = wieldItem; | |
entity.parent.add(wieldItemData.weaponPivot); | |
}); | |
}, | |
update: function (dt) { | |
var world = this.world; | |
var rigidBodies = this.world.getEntities('wieldItem'); | |
rigidBodies.forEach(function (entity) { | |
var wieldItemComponent = entity.getComponent('wieldItem'); | |
var spriteSheetComponent = entity.getScript('/scripts/built-in/sprite-sheet.js'); | |
if (spriteSheetComponent) { | |
var rtd = THREE.Math.radToDeg; | |
// Reset everything first | |
var wp = wieldItemComponent.weaponPivot; | |
var wi = wieldItemComponent.wieldItem; | |
wp.position.set(0,0,0); | |
wi.position.set(0,0,0); | |
wi.rotation.set(0,0,0); | |
wi.scale.set(0.7,0.7,0.7); | |
if (_.contains([0], spriteSheetComponent.dirIndex)) { | |
// wi.rotation.y += rtd(180); | |
} | |
if (spriteSheetComponent.dirIndex === 0) { | |
wp.position.setZ(-0.2); | |
wp.position.setX(0.55); | |
// wi.rotation.y += rtd(10); | |
// wi.rotateZ(Math.PI/4); | |
} | |
// TODO this logic is copied from the look-at-camera script | |
// this should probably me merged somehow | |
var entitiesWithCamera = world.getEntities('camera'); | |
if (entitiesWithCamera.length) { | |
var activeCamera = entitiesWithCamera[0].getComponent('camera').camera; | |
wp.position.copy(entity.position); | |
var parent = entity.parent; | |
var camWorldPos = new THREE.Vector3(); | |
camWorldPos.setFromMatrixPosition(activeCamera.matrixWorld); | |
wp.lookAt(camWorldPos, wp.position, wp.up); | |
} | |
} | |
}); | |
} | |
}); | |
return WielItemSystem; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment