Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by dungeonmaster

I got some questions for my game click to play from drhayes. Here are some answers.

Q1: How do you make the towers fire on things in range? Is there a second, invisible entity that the creeps collide with to say "I'm in range of this gun"?

A: I completely disabled all the collisions and checks :) On each update of the tower entity, I check for enemies in range by going through all the enemies in the game.

On every 10 update cycle (That means for 60 FPS => 6 times per second) the following code runs for each tower entity:

	//Select the first target in the list that is in the range.
	selectTarget: function (targetList) {
		if (!targetList) {
			return false;
		}
		this.target = null;
		for (var i = 0;i<targetList.length;i++) {
			if (MyLib.IsInRange(
				this.center,targetList[i].center,
				this.stats[this.level][EntityTower.RANGE],
				targetList[i].size.x/2 ))
			{
				this.target = targetList[i];
				return true;
			}
		}
		return false;
	},

targetlist is an array containing all the enemy entities in the game.

Q2: Are you relying on bullet/creep collisions to do damage, or are you faking it (e.g. creep takes 3 damage per second when in range of tower) and showing animations to match?

A: When a cannon tower fires, a new bullet entity is created with the information of the target and the damage it carries. After that on every update of the bullet entity, it sets it speed directly for its target and checks if it arrived near it. If so, it deals the damage and kills itself. Here is the code:

	this.center.x = this.pos.x+this.centerOffset;
	this.center.y = this.pos.y+this.centerOffset;
	var deltax = this.target.center.x - this.center.x;
	var deltay = this.target.center.y - this.center.y;
	var deltaH = Math.sqrt(deltax*deltax+deltay*deltay);
	this.vel.x = deltax/deltaH * 800;
	this.vel.y = deltay/deltaH * 800;
	var precision = ig.global.tilesize/4;
	if (Math.abs(deltax)<precision && Math.abs(deltay)<precision) {
		this.target.receiveDamage(this.damage,this);
		ig.game.spawnEntity(EntityExplosion,this.center.x+Math.random()*10-5,this.center.y+Math.random()*10-5,{
				vel:{
					x:this.target.vel.x,
					y:this.target.vel.y
				}});
		this.kill();
		ig.game.sortEntitiesDeferred();
	}

I guess this would be much more saner if I used the original collision code from impact but this works fine for me.

1 decade ago by dungeonmaster

btw. here is the function that check the range. I've added them in my main.js.

MyLib = {};
MyLib.SquareSum = function(a,b) {
	return (a*a+b*b);
};
//pos: {x,y}, range:number
MyLib.IsInRange = function(pos1,pos2,range,totalRadius) {
	if (MyLib.SquareSum((pos1.x-pos2.x),(pos1.y-pos2.y))<(range+totalRadius)*(range+totalRadius)){
		return true;
	} else {return false;}
};

1 decade ago by drhayes

Thanks for the info.

When I was thinking about it my primary worry was the bullets missing (which doesn't happen in TD games). Nice solution!
Page 1 of 1
« first « previous next › last »