1 decade ago by Jerczu
Its somewhat inspired by the Xtype entities.
var sinOff = Math.sin(this.children[i].oscillationTimer.delta()+this.children[i].oscillationOffset)*50;
if you want to increase the speed of oscillation Multiply the value inside the sin in the line mentioned above.
var sinOff = Math.sin(this.children[i].oscillationTimer.delta()+this.children[i].oscillationOffset)*50;
if you want to increase the speed of oscillation Multiply the value inside the sin in the line mentioned above.
ig.module( 'game.entities.modular' ) .requires( 'impact.entity' ) .defines(function(){ EntityModular = ig.Entity.extend({ size: {x: 20, y: 20}, maxVel: {x: 100, y: 100}, friction: {x: 150, y: 0}, gravityFactor:0, type: ig.Entity.TYPE.B, // Evil enemy group checkAgainst: ig.Entity.TYPE.A, // Check against friendly collides: ig.Entity.COLLIDES.PASSIVE, health: 100, speed: 14, flip: false, armOffsets:[ {x:-80,y:10,timerOffset:3}, {x:-60,y:10,timerOffset:2}, {x:-40,y:10,timerOffset:1}, {x:-20,y:10,timerOffset:0}, {x:20,y:10,timerOffset:0}, {x:40,y:10,timerOffset:1}, {x:60,y:10,timerOffset:2}, {x:80,y:10,timerOffset:3} ], children:[], oscillationTimer: new ig.Timer(), animSheet: new ig.AnimationSheet( 'media/entities/testmulti/center.png', 20, 20 ), init: function( x, y, settings ) { this.parent( x, y, settings ); this.addAnim('idle', 1, [0]); for(var i = 0;i<this.armOffsets.length;i++){ this.addModule(i,EntityEnemyArm); } this.vel.x = 10; }, update: function() { this.parent(); this.vel.x = 10; this.updateChildren(); }, addModule: function (offset,entityType) { var ap = this.armOffsets[offset]; var c = this.addChild(entityType, this.pos.x+ap.x, this.pos.y, { angle: (ap.angle * Math.PI) / 180,oscillationOffset:ap.timerOffset }); if(offset == 0 || offset == this.armOffsets.length-1){ this.attachGuns(offset); } return c; }, attachGuns: function(offset){ var child = this.children[offset]; child.children.push(ig.game.spawnEntity(EntityEnemyGun,child.pos.x,child.pos.y-20)); }, addChild: function (entityClass, x, y, settings) { var c = ig.game.spawnEntity(entityClass, x, y, settings); this.children.push(c); return c; }, updateChildren: function () { if (!this.children.length) return; for (var i = 0; i < this.children.length; i++) { var sinOff = Math.sin(this.children[i].oscillationTimer.delta()+this.children[i].oscillationOffset)*50; var child = this.children[i]; child.pos.y = this.pos.y + (sinOff);//*Math.sin(sv);<-extra shake child.vel.x = this.vel.x; if(child.children.length){ for(var j= 0;j<child.children.length;j++){ var gun = child.children[j]; gun.pos.y = child.pos.y-20; gun.vel.x = child.vel.x; } } } }, receiveDamage:function(amount,from){ }, handleMovementTrace: function( res ) { this.parent( res ); }, spawnGibs: function(amount,x,y) { }, check: function( other ) { //other.receiveDamage( 10, this ); } }); EntityEnemyArm = ig.Entity.extend({ size: {x: 20, y: 20}, maxVel: {x: 0, y: 100}, friction: {x: 150, y: 0}, type:ig.Entity.TYPE.NONE, collides: ig.Entity.COLLIDES.PASSIVE, children:[], ang:0, oscillationOffset:0, oscillationTimer: new ig.Timer(), animSheet: new ig.AnimationSheet( 'media/entities/testmulti/arm.png', 20, 20 ), init: function(x,y,settings){ this.parent(x,y,settings); this.addAnim('idle', 1, [0]); this.oscillationOffset = settings.oscillationOffset; this.oscillationTimer.set(Math.random()*2); this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x); }, handleMovementTrace: function( res ) { this.parent( res ); }, update: function() { this.parent(); //console.log("called"); }, check: function( other ) { } }); EntityEnemyGun = EntityEnemyArm.extend({ animSheet: new ig.AnimationSheet('media/entities/testmulti/gun.png',20,20), bullets:10, firingTimer: null, reloadTime: 3, init: function(x,y,settings){ this.parent(x,y,settings); this.firingTimer = new ig.Timer(Math.random() * this.reloadTime * 2); }, update: function() { this.parent(); if (this.firingTimer.delta() > 0) { var angleIncrease = 360 / (this.bullets - 1); this.currentAnim.angle += 1; var initAngle = 0; for (var i = 0; i < this.bullets; i++) { var angle = initAngle * Math.PI / 180; var x = this.pos.x + 18; var y = this.pos.y + 18; ig.game.spawnEntity(EntityEnemyBullet, x, y, { angle: angle }); initAngle += angleIncrease; } this.firingTimer.set(this.reloadTime); }else{ this.currentAnim.angle += 0.5; } } }); EntityEnemyBullet = ig.Entity.extend({ size: { x: 8, y: 8 }, offset: { x: 28, y: 28 }, maxVel: {x: 100, y: 100}, animSheet: new ig.AnimationSheet('media/entities/testmulti/bullet.png',20,20), health: 10, speed: 10, angle:0, maxSpeed: 160, type: ig.Entity.TYPE.NONE, init: function(x,y,settings){ this.parent(x,y,settings); this.addAnim('idle', 1, [0]); this.angle = settings.angle; }, update: function () { this.parent(); this.speed = Math.min(this.maxSpeed, this.speed + ig.system.tick * 100); this.vel.x = Math.cos(this.angle) * this.speed; this.vel.y = Math.sin(this.angle) * this.speed; }, handleMovementTrace: function( res ) { this.parent( res ); if( res.collision.y || res.collision.x ) { this.kill(); } } }); });
1 decade ago by Jerczu
Oh yeah if you want your arm elements to follow the sine wave of the oscillation you can do in updateChildren
This should rotate the box in right direction although its not tested and its still WIP
child.currentAnim.angle = sinOff.toRad();
This should rotate the box in right direction although its not tested and its still WIP
1 decade ago by xOpti
This is working great for me. Just one little issue though. The modules that I spawn seem to lag behind. I spawn the main body, then add legs (weird, I know. but needed for some game mechanics later on) and then spawn a gun. When I'm moving around, the modules can't quite keep up and are 2 - 3 pixels behind the main body. Once stopped, they line up again.
Any way to avoid this?
Any way to avoid this?
ig.module( 'game.entities.player' ) .requires( 'impact.entity' ) .defines(function(){ EntityPlayer = ig.Entity.extend({ size: {x:32,y:32}, animSheet: new ig.AnimationSheet( 'media/character.png', 32, 32 ), speed: 75, children: [], direction: null, init: function( x, y, settings ){ this.parent( x, y, settings ); this.addAnim( 'walkUp', 0.1, [42] ); /* removed the other directions to shorten */ this.addModule( EntityPlayerLegs ); this.addModule( EntityPlayerGun ); }, addModule: function (entityType) { var c = this.addChild(entityType, this.pos.x, this.pos.y); return c; }, addChild: function (entityClass, x, y) { var c = ig.game.spawnEntity(entityClass, x, y, {daddy:this}); this.children.push(c); return c; }, update: function(){ if( ig.input.state('up') && ig.input.state('right') ) { this.vel.y = -this.speed; this.vel.x = this.speed; this.direction = 'walkUpRight'; } /* removed the other directions to shorten */ if( this.direction == 'walkUpRight' && this.vel.y < 0 && this.vel.x > 0 ) { this.currentAnim = this.anims.walkUpRight; } /* removed the other directions to shorten */ this.parent(); } }); EntityPlayerLegs = ig.Entity.extend({ name: 'playerLegs', animSheet: new ig.AnimationSheet('media/character.png',32,32), direction: null, daddy: null, init: function(x,y,settings){ this.parent(x,y,settings); // Leg Animation this.addAnim( 'walkUp', 0.1, [20,21,22,23,30,31,32,33] ); /* removed the other directions to shorten */ }, update: function() { this.parent(); this.direction = this.daddy.direction; this.pos.x = this.daddy.pos.x; this.pos.y = this.daddy.pos.y; if( this.direction == 'walkUpRight') { this.currentAnim = this.anims.walkUp; } /* removed the other directions to shorten */ } }); EntityPlayerGun = ig.Entity.extend({ name: 'playerGun', animSheet: new ig.AnimationSheet('media/character.png',32,32), direction: null, daddy: null, init: function(x,y,settings){ this.parent(x,y,settings); // Equiped Gun Animation this.addAnim( 'walkUp', 0.1, [47] ); /* removed the other directions to shorten */ }, update: function() { this.parent(); this.direction = this.daddy.direction; this.pos.x = this.daddy.pos.x; this.pos.y = this.daddy.pos.y; if( this.direction == 'walkUpRight') { this.currentAnim = this.anims.walkUpRight; } /* removed the other directions to shorten */ } }); });
1 decade ago by Jerczu
You need to update your children in the parent update function call.
this is what updateChildren() does in the parent entity.
update: function() { this.parent(); this.vel.x = 10; this.updateChildren(); },
this is what updateChildren() does in the parent entity.
updateChildren: function () { if (!this.children.length) return; for (var i = 0; i < this.children.length; i++) { var sinOff = Math.sin(this.children[i].oscillationTimer.delta()+this.children[i].oscillationOffset)*50; var child = this.children[i]; child.pos.y = this.pos.y + (sinOff);//*Math.sin(sv);<-extra shake child.vel.x = this.vel.x; if(child.children.length){ for(var j= 0;j<child.children.length;j++){ var gun = child.children[j]; gun.pos.y = child.pos.y-20; gun.vel.x = child.vel.x; } } } },
1 decade ago by typedef_struct
I posted an alternate explanation/solution here: http://impactjs.com/forums/help/modular-entities-lagging-behind-owner-entity/page/1#post20040
Page 1 of 1
« first
« previous
next ›
last »