Last active
November 11, 2017 10:11
-
-
Save qaisjp/774960a454be262267453f26eeb3b9d0 to your computer and use it in GitHub Desktop.
sort of lua for camera logic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Code written by Qais Patankar | |
-- Placed under the public domain, feel free to use this in your project | |
-- | |
-- Code is also not real Lua code, it's pseudocode-ish, for easy public consumption. | |
-- | |
-- This zooms out the camera if players are far apart. | |
-- All comments were written at time of Gist creation (2017-11-11). | |
local zoomAccuracy = 0.05 | |
local maxZoom = 0.25 | |
local minZoom = zoomAccuracy + 0.1 | |
-- determine if the player is "offscreen" but keep a small | |
-- buffer (a safezone) so that players don't appear to be | |
-- onscreen if they are right next to the edge of the screen. | |
-- | |
function player:IsInSafeZone(self, zoom) | |
-- STUB: this code in this function was not written by me | |
-- so I have not shared it. general idea shared below | |
-- return true if the player is onscreen | |
-- return false if the player is offscreen | |
-- | |
-- you can return false if the player is almost offscreen | |
end | |
function onUpdate(dt) | |
local rightmostPlayer, leftmostPlayer | |
-- rightmostPlayer: player on one end of the screen | |
-- leftmostPlayer: player on the opposite end of the screen | |
-- so these are the "edge-most" players | |
for player in players do | |
if not player.dead then | |
if not leftmostPlayer then | |
-- Set the initial players if the player variables above have not be seen set | |
-- (we only need to check if one was not set, because if then the other will not have been set either) | |
leftmostPlayer = player | |
rightmostPlayer = player | |
else | |
-- "current player" = context in loop | |
-- finds leftmostPlayer (or top-most) | |
if player.x < leftmostPlayer.x or (player.x == leftmostPlayer.x and player.y < leftmostPlayer.y) then | |
leftmostPlayer = player; | |
end | |
-- finds rightmostPlayer (or bottom-most) | |
if player.x > rightmostPlayer or (rightmostPlayer.x == player.x and player.y > rightmostPlayer.y) then | |
rightmostPlayer = player; | |
end | |
end | |
end | |
end | |
-- (at this point we have the edge-most players) | |
-- lets follow the rightmost player | |
camera:follow(rightmostPlayer) | |
local zoom = camera:getZoom(); -- get the target zoom (see line 51) | |
if leftmostPlayer:IsInSafeZone(zoom) then | |
-- The player is inside the safezone, there is the possibility of zooming in. | |
while ( leftmostPlayer:IsInSafeZone(zoom + zoomAccuracy) ) and (zoom < maxZoom) do | |
zoom = zoom + zoomAccuracy | |
end | |
else | |
-- Not inside the safezone, so zoom out until inside. | |
while ( not leftmostPlayer:IsInSafeZone(zoom) ) and (zoom > minZoom) do | |
zoom = zoom - zoomAccuracy | |
end | |
end | |
-- This progressively zooms the camera to the target zoom, rather than snapping | |
-- | |
camera:setZoom(zoom - (camera:getZoom() - zoom) * dt * 2) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment