Skip to content
This repository has been archived by the owner on Jan 30, 2022. It is now read-only.

Commit

Permalink
优化坦克相撞规则:判断坦克是否相撞时,只考虑前方的坦克
Browse files Browse the repository at this point in the history
  • Loading branch information
feichao93 committed Jul 9, 2018
1 parent 8bff100 commit f6855e9
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions app/utils/canTankMove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ function isTankCollidedWithRestrictedAreas(
return areas.some(subject => testCollide(subject, tankTarget, threshold))
}

// 判断 other 是否在 tank 前方
function isInFront(other: TankRecord, tank: TankRecord) {
return (
(tank.direction === 'left' && other.x < tank.x) ||
(tank.direction === 'right' && other.x > tank.x) ||
(tank.direction === 'up' && other.y < tank.y) ||
(tank.direction === 'down' && other.y > tank.y)
)
}

function isTankCollidedWithOtherTanks(
activeTanks: TanksMap,
tank: TankRecord,
Expand All @@ -72,9 +82,13 @@ function isTankCollidedWithOtherTanks(
if (tank.tankId === otherTank.tankId) {
continue
}
// 判断坦克相撞时,subject 一方需要使用预留位置
const subject = asRect(otherTank.useReservedXY())
if (testCollide(subject, tankTarget, threshhold)) {
// 判断坦克相撞时,只需要考虑当前坦克前方的其他坦克
// 且其他坦克需要使用「预留位置」
const otherReserved = otherTank.useReservedXY()
if (
isInFront(otherReserved, tank) &&
testCollide(asRect(otherReserved), tankTarget, threshhold)
) {
return true
}
}
Expand Down

0 comments on commit f6855e9

Please sign in to comment.