我已经建立了一个塔防游戏,并已经得到它,所以你可以放下塔楼,敌人来到你身边。但是,塔楼正在对其半径内的所有怪物造成伤害。我希望它能让塔只对周围的一个敌人造成伤害。如何让玩家锁定其半径内的一个敌人?
我设置了两个变量:monsterList和towerList。它们都是对象,并且我已经设置好它,以便在某个事件中,它会向指定的变量添加敌人或塔。因此,当您选择一个空白区域来放置一个塔时,它会将一个对象变量添加到列表中。当你点击下一级时,这也会发生,但它会创建可以在路径中移动的动态怪物。它看起来有点像这样:
//the variables
var monsterList = {};
var towerList = {};
//how towers or enemies are added
var Monster = {
x:10,
y:10,
type:"Red",
id:Math.random()
}
monsterList[Monster.id] = Monster;
所以,这就是我创建的怪物,并以同样的方式,根据不同的参数,对于塔。当一个怪物沿着这条路走下去并进入塔内时,塔会攻击到达的任何和所有怪物。我希望它只锁定在1范围内的怪物。这里是我的碰撞设置:
//loops through all the monsters and towers and tests if any of them are
//colliding. If they are colliding, it will decrease the monster's health by
//1/30 of the tower's damage. This is because I have an interval looping
//through this code 30 times a second. So, it deals the damage per second.
for(var key in monsterList){
for(var i in towerList){
if(Collision(towerList[i], monsterList[key])){
monsterList[key].health -= (towerList[i].damage/30);
}
if(monsterList[key].health <= 0) delete monsterList[key];
}
}
//and this is the Collision() function. It pretty much returns true if the
//monster is within the tower's range which is defined by an argument to the
//tower called radius.
function Collision(tower, monster){
return ((monster.y >= (tower.y - tower.radius*20)) &&
(monster.x >= (tower.x - tower.radius*20)) &&
(monster.y <= (tower.y + tower.height + tower.radius*20)) &&
(monster.x <= (tower.x + tower.width + tower.radius*20)));
}
谢谢你的帮助!
现有的答案似乎有问题。那么,如果你想破坏的范围有多重?最近的?最新的?最健康的?最弱的? –
@GarrGodfrey的问题很重要,请回答。你需要为每个塔处理一个'currentTarget',并且有一个'selectTarget'方法,根据你的选择策略+当前的游戏状态更新'currentTarget'。 – GameAlchemist