Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Clase Bullet que construye un Bullet
- * @class Bullet
- * @constructors
- * @this {Bullet}
- * @param {Number} x Posicion X en el que se encuentra
- * @param {Number} y Posicion Y en el que se encuentra
- * @param {Enemy} Enemy enemigo que tiene seleccionado
- * @param {Number} damage dano que inflije la bala
- * @param {Number} index indice para sacar la informacion del json
- * @param {String} direccion direccion que va a disprar
- * @returns {Bullet}
- */
- function Bullet(x, y, Enemy, damage, index, direccion) {
- /* * Posicion X en el que se encuentra. Por defecto 0.
- * @type Number
- */
- this.x = (x == null) ? 0 : x;
- /* * Posicion Y en el que se encuentra. Por defecto 0.
- * @type Number
- */
- this.y = (y == null) ? 0 : y;
- /* * Posicion X inicial coje el valor de X al construir el objeto.
- * @type Number
- */
- this.xi = this.x;
- /* * Posicion Y inicial coje el valor de Y al construir el objeto.
- * @type Number
- */
- this.yi = this.y;
- /**
- * Direccion donde va disparar "bot" || "top" || "left" || "right"
- * solo se usara si bulletType es "lineal" o "area".
- * @type String
- */
- this.direccion = direccion;
- /**
- * Tipo de la bala coje el valor del json tipotower.
- * @type String
- */
- this.bulletType = tipotower[index].bulletType;
- /**
- * Anchura de la bala coje el valor del json tipotower.
- * @type Number
- */
- this.width = tipotower[index].bulletSize;
- /**
- * Altura de la bala coje el valor del json tipotower.
- * @type Number
- *
- */
- this.height = tipotower[index].bulletSize;
- /**
- * Dano de la bala por defecto 0.
- * @type Number
- */
- this.damage = (damage == null) ? 0 : damage;
- /**
- * Imagen de la bala coje la src del json tipotower.
- * @type Image
- */
- this.img = new Image();
- this.img.src = tipotower[index].imgbullet;
- /**
- * Enemigo que tiene seleccionado solo lo utilizara si el bulletType
- * es "target"
- * @type Enemy
- */
- this.Enemy = Enemy;
- /**
- * Velocidad de la bala coje el valor del json tipotower.
- * @type Number
- */
- this.velocity = tipotower[index].velocity;
- this.radio = tipotower[index].radio;
- this.countradio = 0;
- /**
- * Metodo que comprueba si se ha chocado con otro cuerpo
- * @param {type} rect
- * @returns {boolean}
- */
- this.intersects = function(rect) {
- if (rect != null) {
- return(this.x <= rect.x + rect.width &&
- this.x + this.width > rect.x &&
- this.y <= rect.y + rect.height &&
- this.y + this.height > rect.y);
- }
- }
- /**
- * Metodo para mover la bala. Actualiza su posicion.
- * Si bulletType es target seguira al Enemy que tiene.
- * Si bulletType es lineal o area la bala mirara que direccion tiene
- * y se movera en esa direccion.
- * @returns {void}
- */
- this.move = function() {
- if (this.bulletType == "target") {
- var dx = Enemy.x + Enemy.width / 2 - this.x;
- var dy = Enemy.y + Enemy.height / 2 - this.y;
- var angle = Math.atan(dy / dx);
- var dxf = Math.cos(angle) * this.velocity;
- var dyf = Math.sin(angle) * this.velocity;
- if (dx < 0) {
- dxf = dxf * -1;
- dyf = dyf * -1;
- }
- this.x += dxf;
- this.y += dyf;
- } else if (this.bulletType == "lineal" || this.bulletType == "area") {
- switch (this.direccion) {
- case "top":
- this.y -= this.velocity;
- break;
- case "left":
- this.x -= this.velocity;
- break;
- case "bot":
- this.y += this.velocity;
- break;
- case "right":
- this.x += this.velocity;
- break;
- }
- this.countradio += this.velocity;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment