Game Development Reference
In-Depth Information
hx, hy = cx + max_distance, cy + max_distance
38
# Fast, rough results
39
results = @tree . query_range(
40
AxisAlignedBoundingBox . new( [ cx, cy ] , [ hx, hy ] ))
41
# Sift through to select fine-grained results
42
results . select do | o |
43
o != object &&
44
Utils . distance_between(
45
o . x, o . y, object . x, object . y) <= max_distance
46
47 end
48 end
49
50 def query_range (box)
51 @tree . query_range(box)
52 end
53 end
An old fashioned array of all objects is still used, because we still need to loop through
everything and invoke GameObject#update . ObjectPool#query_range was
introduced to quickly grab objects that have to be rendered on screen, and
ObjectPool#nearby now queries tree and measures distances only on rough result set.
This is how we will render things from now on:
class PlayState < GameState
# ...
def draw
cam_x = @camera . x
cam_y = @camera . y
off_x = $window . width / 2 - cam_x
off_y = $window . height / 2 - cam_y
viewport = @camera . viewport
x1, x2, y1, y2 = viewport
box = AxisAlignedBoundingBox . new(
[ x1 + (x2 - x1) / 2 , y1 + (y2 - y1) / 2] ,
[ x1 - Map :: TILE_SIZE , y1 - Map :: TILE_SIZE ] )
$window . translate(off_x, off_y) do
zoom = @camera . zoom
$window . scale(zoom, zoom, cam_x, cam_y) do
@map . draw(viewport)
@object_pool . query_range(box) . map do | o |
o . draw(viewport)
end
end
Search WWH ::




Custom Search