Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Add a method map:get_entities_in_rectangle() thanks to quadtrees
Browse files Browse the repository at this point in the history
Closes #142.
  • Loading branch information
christopho committed Aug 26, 2015
1 parent c28cc4e commit aeae7cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Changes that introduce incompatibilities:
Changes that do not introduce incompatibilities:

* Add a function sol.main.get_type() (#744).
* Add a method map:get_entities_in_rectangle() (#142).
* Add a method block:get_sprite().
* entity:set_optimization_distance() is now only a hint for the engine.

Expand Down
1 change: 1 addition & 0 deletions include/solarus/lua/LuaContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ class LuaContext {
map_api_get_entities,
map_api_get_entities_count,
map_api_has_entities,
map_api_get_entities_in_rectangle,
map_api_get_hero,
map_api_set_entities_enabled,
map_api_remove_entities,
Expand Down
36 changes: 36 additions & 0 deletions src/lua/MapApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void LuaContext::register_map_module() {
{ "get_entities", map_api_get_entities },
{ "get_entities_count", map_api_get_entities_count },
{ "has_entities", map_api_has_entities },
{ "get_entities_in_rectangle", map_api_get_entities_in_rectangle },
{ "get_hero", map_api_get_hero },
{ "set_entities_enabled", map_api_set_entities_enabled },
{ "remove_entities", map_api_remove_entities },
Expand Down Expand Up @@ -1789,6 +1790,41 @@ int LuaContext::map_api_has_entities(lua_State* l) {
});
}

/**
* \brief Implementation of map:get_entities_in_rectangle().
* \param l The Lua context that is calling this function.
* \return Number of values to return to Lua.
*/
int LuaContext::map_api_get_entities_in_rectangle(lua_State* l) {

return LuaTools::exception_boundary_handle(l, [&] {
Map& map = *check_map(l, 1);
const int x = LuaTools::check_int(l, 2);
const int y = LuaTools::check_int(l, 3);
const int width = LuaTools::check_int(l, 4);
const int height = LuaTools::check_int(l, 5);

std::vector<EntityPtr> entities;
map.get_entities().get_entities_in_rectangle(
Rectangle(x, y, width, height), entities
);

lua_newtable(l);
for (auto it = entities.begin(); it != entities.end(); ++it) {
EntityPtr entity = *it;
push_entity(l, *entity);
lua_pushboolean(l, true);
lua_rawset(l, -3);
}
lua_getglobal(l, "pairs");
lua_pushvalue(l, -2);
lua_call(l, 1, 3);
// TODO factorize with get_entities()

return 3;
});
}

/**
* \brief Implementation of map:get_hero().
* \param l The Lua context that is calling this function.
Expand Down

0 comments on commit aeae7cb

Please sign in to comment.