I have recently started using LimeJS for my game development, and I have run into a bit of a hault.
I am making a RTS type of game. I have functions which let you chose buildings, and then click on the map to add chosen building to the map. I do not want the player to be able to place a building if it overlaps with one that is already there. How would I check this? The buildings are Sprites which in turn are added as children to a map sprite. I was thinking of using hitTest but I can't quite figure out how that function works. Example of the event handler:
goog.events.listen(field,['mousedown'],function build(e){
if(selected_ == 1){
var house = new rh.house().setPosition(e.position.x, e.position.y);
field.appendChild(house);
selected_ = -1;
houselbl.setFill('assets/storage.png');
}
if(selected_ == 2){
var blacksmith = new rh.blacksmith().setPosition(e.position.x, e.position.y);
field.appendChild(blacksmith);
selected_ = -1;
blacksmithlbl.setFill('assets/blacksmith.png');
}
if(selected_ == 3){
var lumbermill = new rh.lumbermill().setPosition(e.position.x, e.position.y);
field.appendChild(lumbermill);
selected_ = -1;
lumbermilllbl.setFill('assets/lumbermill.png');
}
});
I got it working. I used getBoundingBox on all of the children, and checked if it was safe. Example below:
goog.events.listen(field,['mousedown'],function build(e){
if(selected_ == 1){
var house = new rh.house().setPosition(e.position.x, e.position.y);
var safe=true;
for (var i=0;i<field.getNumberOfChildren();i++)
{
var box = field.getChildAt(i).getBoundingBox();
if (goog.math.Box.intersects(box,house.getBoundingBox())){
safe=false;
}
}
if (safe){
field.appendChild(house);
selected_ = -1;
houselbl.setFill('assets/storage.png');
}
}
}