From ca721ec8acdc487ad66eb7a8c483403237c3f3fb Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Fri, 26 Jul 2019 11:53:10 +0200 Subject: [PATCH] feat(camera+map): add a way to add padding to map limits --- CHANGELOG.md | 2 ++ gamecore/modules/world/camera/init.lua | 4 ++-- gamecore/modules/world/maps/parent.lua | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5de3de..9788772 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **core:** add a way to activate easily debug mode directly +- **camera+maps:** add a way to add padding to map limits + ### Changed - **world:** extract map module from the world module diff --git a/gamecore/modules/world/camera/init.lua b/gamecore/modules/world/camera/init.lua index 7640d02..49487ff 100644 --- a/gamecore/modules/world/camera/init.lua +++ b/gamecore/modules/world/camera/init.lua @@ -274,8 +274,8 @@ function CameraSystem:limitView(id) local posy = self.views.list[id].y local minx = worldx + self.views.width / 2 local miny = worldy + self.views.height / 2 - local maxx = worldw - minx - local maxy = worldh - miny + local maxx = worldw - self.views.width / 2 + local maxy = worldh - self.views.height / 2 self.views.list[id].x = utils.math.between(posx, minx, maxx) self.views.list[id].y = utils.math.between(posy, miny, maxy) diff --git a/gamecore/modules/world/maps/parent.lua b/gamecore/modules/world/maps/parent.lua index 17207a5..a1b445d 100644 --- a/gamecore/modules/world/maps/parent.lua +++ b/gamecore/modules/world/maps/parent.lua @@ -11,6 +11,7 @@ function ParentMap:new(world, r, g, b) local b = b or 128 self.backgroundColor = {r, g, b} + self:setPadding() self:register() end @@ -18,6 +19,16 @@ function ParentMap:register() self.world.map = self end +function ParentMap:setPadding(x1, y1, x2, y2) + local padding = {} + padding.x1 = x1 or 0 + padding.y1 = y1 or padding.x1 + padding.x2 = x2 or padding.x1 + padding.y2 = y2 or padding.y1 + + self.padding = padding +end + -- UPDATE FUNCTION -- Update or modify the map @@ -62,13 +73,18 @@ function ParentMap:getBackgroundColor() return self.backgroundColor[1]/256, self.backgroundColor[2]/256, self.backgroundColor[3]/256 end +function ParentMap:getPadding() + return self.padding.x1, self.padding.y1, self.padding.x2, self.padding.y2 +end + function ParentMap:getDimensions() return core.screen:getDimensions() end function ParentMap:getBox() + local x1, y1, x2, y2 = self:getPadding() local w, h = self:getDimensions() - return 0, 0, w, h + return -x1, -y1, w+x2, h+y2 end function ParentMap:drawBackgroundColor()