Skip to content

Instantly share code, notes, and snippets.

@dtturcotte
Last active September 21, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtturcotte/9a26e996da623a13dfc7a97cf52be1a7 to your computer and use it in GitHub Desktop.
Save dtturcotte/9a26e996da623a13dfc7a97cf52be1a7 to your computer and use it in GitHub Desktop.

Player Entity:

	pointerdown : function (e) {
		if (e.button === 0) {
			this.angle = this.angleToPoint(new me.Vector2d(e.gameX - 30, e.gameY + 30));

			// Projectile
			var params = {
				poolID: 'projectile',
				x: this.pos.x + 30,
				y: this.pos.y + 20,
				data: {
					name: 'fire',
					type: 'AttackEntity',
					resource_name: 'fireball',
					image: 'fireball',
					width: 64,
					height: 64,
					framewidth: 64,
					frameheight: 64,
					angle: this.angle
				}
			};

			var newProjectile = me.pool.pull(params.poolID, params.x, params.y, params.data);
			newProjectile.z = 39;
			me.game.world.addChild(newProjectile);
		}
	},

Attack Entity:

game.AttackEntity = me.Entity.extend({
	init: function(x, y, settings) {

		this._super(me.Entity, 'init', [x, y , settings]);

		this.angle = settings.angle;
		this.poolID = settings.poolID;
		this.speedMultiplier = 4;

		// save the area size defined in Tiled
		var width = settings.width;
		var height = settings.height;

		this.body.addShape(new me.Rect(0, 0, settings.width, settings.height));
		this.body.setVelocity(4, 4);

		this.name = settings.name;
		this.hitTarget = false;
		this.alwaysUpdate = true;
		this.image = settings.image;

		var x = this.pos.x;
		var y = this.pos.y;

		// adjust the size setting information to match the sprite size
		// so that the entity object is created with the right size
		settings.framewidth = settings.width = 64;
		settings.frameheight = settings.height = 64;

		this.resource_name = settings.resource_name;

		this.renderable.addAnimation("fire",  [0, 1, 2]);

		this.renderable.setCurrentAnimation(this.name);

		console.log('Angle', this.angle);
		this.body.vel.set(Math.cos(this.angle) * this.speedMultiplier, -Math.sin(this.angle) * this.speedMultiplier);
		console.log('Calculations:', Math.cos(-this.angle), Math.sin(-this.angle), this.body.vel);
	},

	update: function(delta) {

		if (!this.inViewport) {
			me.game.world.removeChild(this);
		}

		this.body.update(delta);
		if (!me.collision.check(this)) {}

		return (this._super(me.Entity, 'update', [delta]) || this.body.vel.x !== 0 || this.body.vel.y !== 0);
	},


	onCollision : function (response, collidedObject) {

		if (collidedObject.type === 'minion' && !this.hitTarget) {
			this.hitTarget = true;
			me.game.viewport.shake(10, 500, me.game.viewport.AXIS.BOTH);
			me.game.world.removeChild(this);
		}

		if (collidedObject.type === 'AttackEntity') {
			return false;
		}

		// Ignore collision if hits player's interaction box
		if ((response.a.type === 'PlayerEntity')) {
			return false;
		}
		return true;
	}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment