Click here to Skip to main content
15,886,110 members
Articles / Web Development / HTML5

Visual Concealment for Games using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
14 Feb 2013CPOL11 min read 27.2K   333   14  
Obscuring out-of-sight areas in games using HTML5 and JavaScript
function Player() {
	Body.call(this);
	this.standardForce = 20000;
	this.fireLossFactor = 0.01;	
}

Player.prototype = new Body();
Player.prototype.constructor = Player;

Player.prototype.processInput = function(totalElapsed, elapsed, level, entities, keyboard) {
	
	if (keyboard.pressed.forward)
		this.forces.push(Vector2D.mul(this.lookDirection, this.standardForce / (1 + this.size)));

	if (keyboard.pressed.backward)
		this.forces.push(Vector2D.mul(this.lookDirection, -(this.standardForce / 2) / (1 + this.size)));
	
	if (keyboard.pressed.left || keyboard.pressed.right) {
		var perpendicular = Vector2D.cross(this.lookDirection);
		if (keyboard.pressed.left)
			this.forces.push(Vector2D.mul(perpendicular, 0.25 * this.standardForce / (1 + this.size)));
		else
			this.forces.push(Vector2D.mul(perpendicular, -0.25 * this.standardForce / (1 + this.size)));
	}

	if (keyboard.pressed.turnleft)
		this.lookDirection = Vector2D.rotate(this.lookDirection, -(32 * elapsed / (1 + this.size)));
	
	if (keyboard.pressed.turnright)
		this.lookDirection = Vector2D.rotate(this.lookDirection,  (32 * elapsed / (1 + this.size)));
	
	if (keyboard.pressed.fire) {
	    var bullet = new Bullet(this.size * this.fireLossFactor);
	    this.size *= 1 - this.fireLossFactor;
	    bullet.lookDirection = new Vector2D(this.lookDirection.x, this.lookDirection.y);
	    bullet.position = Vector2D.add(Vector2D.mul(bullet.lookDirection, this.size), new Vector2D(this.position.x, this.position.y));
	    bullet.forces.push(Vector2D.mul(bullet.lookDirection, 50000));
	    this.additionalEntities.push(bullet);
	}
	
	if (this.size < 5) {
        for(var j = 0; j < 200; ++j) {
            var fragment = new ExplosionFragment(3 + Math.random() * 4);
            fragment.position = this.position;
            fragment.velocity = Vector2D.rotate(new Vector2D(Math.random() * 1000, 0), Math.random() * TWO_PI);
            this.additionalEntities.push(fragment);
            this.isDead = true;
        }
	    
	}
	    
	    
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Article videos
Oakmead Apps Android Games

21 Feb 2014: Best VB.NET Article of January 2014 - Second Prize
18 Oct 2013: Best VB.NET article of September 2013
23 Jun 2012: Best C++ article of May 2012
20 Apr 2012: Best VB.NET article of March 2012
22 Feb 2010: Best overall article of January 2010
22 Feb 2010: Best C# article of January 2010

Comments and Discussions