나는 그 팀이 인수에 전달되는 위치를 볼 수 있다면 내 FogOfWar 클래스에 다음 함수, 그것은 계산이 :전쟁의 안개를 계산하는 가장 빠른 방법은 무엇입니까?
내 문제가package mwoke {
public class FogOfWar {
public var game:Game;
public var team:int;
private var cache:Vector.<CachePos> = new Vector.<CachePos>();
private var chars:Vector.<Character> = new Vector.<Character>();
public function FogOfWar($game:Game, $team:int) {
game = $game;
team = $team;
}
public function think():void {
cache.length = 0;
chars.length = 0;
var ent:Character;
for(var i:int = 0; i < game.entities.length;++i) {
if(game.entities[i] == null) continue;
if(!(game.entities[i] is Character)) continue;
ent = Character(game.entities[i]);
if(ent.team != team) continue;
if(ent.vision <= 0.0) continue;
chars.push(ent);
}
}
public function canSee(x:Number, y:Number):Boolean {
if(x < game.minX) return false;
if(y < game.minY) return false;
if(x >= game.maxX) return false;
if(y >= game.maxY) return false;
for each(var v:CachePos in cache) {
if(v.x == x && v.y == y) {
return v.result;
}
}
var res:Boolean = false;
for each(var ent:Character in chars) {
if(Game.distance(x, y, ent.x, ent.y) < ent.vision) {
res = true;
break;
}
}
var tmp:CachePos = new CachePos();
tmp.x = x;
tmp.y = y;
tmp.result = res;
cache.push(tmp);
return res;
}
}
}
internal class CachePos {
public var x:Number;
public var y:Number;
public var result:Boolean;
}
, 그, 그것을 할 수있는 게임 만 3 문자 초당 4000 번만 실행하면 그리 그리 크지 않습니다.
캐시를 구현할 수 있으므로 가장 일반적인 위치를 캐시하지만 최적화를 위해 수행 할 수있는 작업은 무엇입니까?
Game.distance 방법은 어떨까요? 충분히 빠릅니까? – vulkanino
그것은'Math.sqrt ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))''이고 옵티마이 저를 사용하여 인라인됩니다. –
모든 문자를 벡터이므로 모든 엔티티를 반복 할 필요가 없습니다. –