9 years ago by Wake
Hi there guys! i`m new with impact but i´m doing right now a tiny game for LD 33 (i know, maybe someone will ask me few days after LD but still helps me to learn! haha).
So, i have a Player that spawn a bullet with bunciness pressing "Z". Also, i have another type of bullet spawned by clicking the mouse. My problem is:
The "pressing Z" bullet is a high damage bullet, so i want to limit it to 3 bullets on screen. If there are 3 bullets on screen, the player can´t fire more, even if he press the "Z" button. If you have 2 High damage bullet on screen, you can fire one more, etc.
This is my Player code with my bullets (bullet and Granada). Sorry for the spanish notes, i´m Chilean! if you need more info, just tell me.
PS: I know that my code sucks sometimes, this is my second week with impact. I'm a 3D artist so you know, coding is new for me haha
So, i have a Player that spawn a bullet with bunciness pressing "Z". Also, i have another type of bullet spawned by clicking the mouse. My problem is:
The "pressing Z" bullet is a high damage bullet, so i want to limit it to 3 bullets on screen. If there are 3 bullets on screen, the player can´t fire more, even if he press the "Z" button. If you have 2 High damage bullet on screen, you can fire one more, etc.
This is my Player code with my bullets (bullet and Granada). Sorry for the spanish notes, i´m Chilean! if you need more info, just tell me.
PS: I know that my code sucks sometimes, this is my second week with impact. I'm a 3D artist so you know, coding is new for me haha
ig.module( 'game.entities.player' ) .requires( 'impact.entity' ) .defines(function(){ EntityPlayer = ig.Entity.extend({ //Importar animacion y fisicas del personaje (tambien salud y colisiones) animSheet: new ig.AnimationSheet ('media/graphics/player.png', 16, 16), size: {x:16, y:16}, offset:{x:2, y:0}, //_wmScalable: true, flip:false, maxVel: {x:100, y:140}, friction: {x:600, y:0}, accelGround: 400, accelAir: 300, jump: 300, gravity: 1, //Colisiones. type: ig.Entity.TYPE.A, checkAgainst: ig.Entity.TYPE.NONE, collides: ig.Entity.COLLIDES.PASSIVE, health: 100, startPosition: null, //bounciness: 0.7, //Esto es para la inmunidad luego del respawn invincible: true, invincibleDelay: true, invincibleTimer: null, weapon: 0, totalWeapons: 2, activeWeapon: 'EntityBullet', _wmDrawBox: true, _wmBoxColor: 'rgba(255, 0, 0, 0.7)', init: function (x,y, settings){ this.parent(x, y, settings); this.addAnim ('idle', 1, [5] ); this.addAnim ('run', 0.12, [0,1,2] ); this.addAnim ('shoot',1, [3] ); this.addAnim ('jump', 1, [4] ); this.addAnim ('falling', 1, [6]); this.addAnim ('shoting', 1, [3], true); this.addAnim( 'pain', 0.3, [6], true ); // Propiedades mouse this.invincibleTimer = new ig.Timer(); this.makeInvincible(); //posicion. Se indica la posicion del personaje. this.startPosition = {x:x, y:y}; }, update : function(){ this.parent(); // Esto flipea la animacion this.currentAnim.flip.x = this.flip; // Moverse de izquierda a derecha var accel = this.standing ? this.accelGround : this.accelAir; if(ig.input.state('left')){ this.accel.x = -accel; this.flip = true; } else if (ig.input.state('right')){ this.accel.x = accel; this.flip = false; } else{ this.accel.x = 0; } // Saltar if ( this.standing && ig.input.pressed('jump')){ this.vel.y = -this.jump; } //Nuevo metodo para ejecutar las animaciones, diferente al if pressed.state.released. Se usa con calculo de velocidad en vez de verificar tecla apretada WAY BETTER!!! if(this.vel.y < 0 ){ this.currentAnim = this.anims.jump; } else if( this.vel.y > 0) { this.currentAnim = this.anims.falling; } else if( this.vel.x != 0){ this.currentAnim = this.anims.run; } else { this.currentAnim = this.anims.idle; } //disparooo! no funciona el flip de la bala eso si. if( ig.input.pressed('fire') ) { var entity = ig.game.spawnEntity ( //esto spawnea la bala y la flipea EntityGranada, this.pos.x+10, this.pos.y, {flip:this.flip}); entity.currentAnim.flip.x = this.flip; //this.currentAnim = this.anims.shoting; } if (this.invincibleTimer.delta()> this.invincibleDelay){ this.invincible = false; this.currentAnim.alpha =1; } if(ig.input.pressed('cambiar')){ this.weapon ++; if(this.weapon >= this.totalWeapons) this.weapon = 0; switch (this.weapon){ case(0): this.activeWeapon = 'EntityBullet'; break; case(1): this.activeWeapon = 'EntityGranada'; break; } } // Mouse!! MOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUSEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE if( ig.input.pressed('click') ) { //Basic shoot command var mx = (ig.input.mouse.x + ig.game.screen.x); //Figures out the x coord of the mouse in the entire world var my = (ig.input.mouse.y + ig.game.screen.y); //Figures out the y coord of the mouse in the entire world var r = Math.atan2(my-this.pos.y, mx-this.pos.x); //Gives angle in radians from player's location to the mouse location, assuming directly right is 0 /* Honestly, the above should probably take into account offsets of where your gun is located, but that greatly overcomplicates this snippet since most of you are going to have different exit points for the barrel of your weapons Furthermore, each weapon might even have different exit points, so you might want to move the angle calculation into the init method of each bullet */ ig.game.spawnEntity( EntityBullet, this.pos.x, this.pos.y, {flip:this.flip, angle:r} ); //Nothing to special here, just make sure you pass the angle we calculated in } // Aca termina el MOUSEEEEEEE }, //Esto es para llamar al respawn. El player aparecera en el incio (donde la entidad estaba originalmente) kill: function(){ this.parent(); var x = this.startPosition.x; var y = this.startPosition.y; ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {callBack:function(){ig.game.spawnEntity( EntityPlayer, x, y)}} ); }, makeInvincible: function(){ this.invincible = true; this.invincibleTimer.reset(); offset = {x:2, y:0}; }, receiveDamage: function (amount, from){ if (this.invincible) return; this.parent(amount, from); }, draw: function (){ if (this.invincible) this.currentAnim.alpha = this.invincibleTimer.delta()/this.invincibleDelay * 1; this.parent(); } }); EntityBullet = ig.Entity.extend({ size: {x:8, y:5}, offset: {x:5, y:6}, animSheet: new ig.AnimationSheet ('media/graphics/items.png', 16, 16), maxVel: {x:300, y:300}, type: ig.Entity.TYPE.NONE, flip: false, checkAgainst: ig.Entity.TYPE.B, collides: ig.Entity.COLLIDES.PASSIVE, check: function( other ) { other.receiveDamage( 10, this ); this.kill(); }, init: function (x, y, settings) { this.parent (x + (settings.flip ? -4 : 8) , y+8, settings ); this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x); this.addAnim('shoot', 0.1, [11]); //MOUUUUUUUUSEEEEEEEEE var vely = Math.sin(this.angle) * this.maxVel.x; //.desiredVel is just the velocity I would want if we were going in a straight line directly out of the right of the player. I just put it as a property of the entity since I refer to it in multiple locations var velx = Math.cos(this.angle) * this.maxVel.x; /* I'm a fan of fullspeed projectiles with no acceleration so we set the velocity, max velocity and for good measure acceleration too. You might want to start with a bit of velocity and some sort of acceleration so your projectile starts off slower and speeds up for something like a rocket If that's the case, you'll want to do something like the following this.maxVel.x = whatever max you want; this.accel.x = Math.sin(this.angle) * desiredAcceleration; this.accel.y = Math.cos(this.angle) * desiredAcceleration; this.vel.x = Math.sin(this.angle) * desiredStartingVelocity; this.vel.y = Math.cos(this.angle) * desiredStartingVelocity; */ this.maxVel.x = this.vel.x = this.accel.x = velx; this.maxVel.y = this.vel.y = this.accel.y = vely; //this.activeWeapon = {flip:this.flip}); //FIN DEL MOUSE! BORRAR }, handleMovementTrace: function( res ) { this.parent( res ); if( res.collision.x || res.collision.y ){ this.kill(); } }, check: function( other ) { other.receiveDamage( 3, this ); this.kill(); } }); EntityGranada = ig.Entity.extend({ size: {x:8, y:5}, offset: {x:5, y:5}, animSheet: new ig.AnimationSheet ('media/graphics/items.png', 16,16), type: ig.Entity.TYPE.NONE, checkAgainst: ig.Entity.TYPE.B, collides: ig.Entity.COLLIDES.PASSIVE, maxVel: {x:130, y:200}, bounciness: 1, gravityFactor: 1, gravity: 1, angle: 45, bounceCounter: 2, init: function(x,y, settings){ this.parent (x + (settings.flip ? -4 : 7), y, settings); this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x); this.vel.y = -(10 + (Math.random()*100)); this.addAnim('granada', 0.1, [1,2]); //MOUUUUUUUUSEEEEEEEEE }, handleMovementTrace: function (res){ this.parent (res); if (res.collision.x || res.collision.y){ //esto es pa que rebote 3 veces! se puede cambiar a gusto la cantidad de rebotes this.bounceCounter++; if(this.bounceCounter > 4){ this.kill(); } } }, check: function (other){ other.receiveDamage (10, this); this.kill(); } }); EntityDeathExplosion = ig.Entity.extend({ lifetime: 1, callBack: null, particles: 10, init: function( x, y, settings ) { this.parent( x, y, settings ); for(var i = 0; i < this.particles; i++) ig.game.spawnEntity(EntityDeathExplosionParticlep, x, y, {colorOffset: settings.colorOffset ? settings.colorOffset : 2}); this.idleTimer = new ig.Timer(); }, update: function() { if( this.idleTimer.delta() > this.lifetime ) { this.kill(); if(this.callBack) this.callBack(); return; } } }); EntityDeathExplosionParticlep = ig.Entity.extend({ size: {x: 2, y: 2}, maxVel: {x: 160, y: 200}, lifetime: 2, fadetime: 1, bounciness: 0, vel: {x: 100, y: 30}, friction: {x:100, y: 0}, collides: ig.Entity.COLLIDES.LITE, colorOffset: 0, totalColors: 9, animSheet: new ig.AnimationSheet( 'media/particles.png', 2, 2 ), init: function( x, y, settings ) { this.parent( x, y, settings ); var frameID = Math.round(Math.random()*this.totalColors) + (this.colorOffset * (this.totalColors+1)); this.addAnim( 'idle', 0.2, [0,1,2,3,4,5,6,7] ); this.vel.x = (Math.random() * 2 - 1) * this.vel.x; this.vel.y = (Math.random() * 2 - 1) * this.vel.y; this.idleTimer = new ig.Timer(); }, update: function() { if( this.idleTimer.delta() > this.lifetime ) { this.kill(); return; } this.currentAnim.alpha = this.idleTimer.delta().map( this.lifetime - this.fadetime, this.lifetime, 1, 0 ); this.parent(); } }); });