Skip to content

Commit

Permalink
sophisticated the camera
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Aug 10, 2023
1 parent e9196cd commit 11c9e57
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 36 deletions.
24 changes: 24 additions & 0 deletions camera.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
camera = { x = 0, y = 0, rx = 0, ry = 0, init = false }

function caminit(map)
camera.xmin = 0
camera.xmax = map.width * map.tilewidth - window.w * (1 / window.scale)
camera.ymin = 0
camera.ymax = map.height * map.tileheight - window.h * (1 / window.scale)
camera.init = true
end

function camfollowsprite(o)
camera.x = o.x - ((window.w / 2) * (1 / window.scale))
camera.y = o.y - ((window.h / 2) * (1 / window.scale))
end

function camupdate()
if camera.init then
camera.x = math.min(math.max(camera.x, camera.xmin), camera.xmax)
camera.y = math.min(math.max(camera.y, camera.ymin), camera.ymax)
end

camera.rx = math.floor(camera.x)
camera.ry = math.floor(camera.y)
end
13 changes: 0 additions & 13 deletions class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ function class(members)
__index = members
}

local function new(_, init)
return setmetatable(init or {}, mt)
end

local function copy(obj, ...)
local newobj = obj:new(unpack(arg))
for n,v in pairs(obj) do newobj[n] = v end
return newobj
end

members.new = members.new or new
members.copy = members.copy or copy

return mt
end

Expand Down
4 changes: 2 additions & 2 deletions input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ mouse = { x = 0, y = 0, p = false, op = false, held = false }

function updatemouse()
mouse.x, mouse.y = love.mouse.getPosition()
mouse.x = mouse.x - camera.rx * window.scale
mouse.y = mouse.y - camera.ry * window.scale
mouse.x = mouse.x - camera.rx * window.scale * -1
mouse.y = mouse.y - camera.ry * window.scale * -1

mouse.p = false

Expand Down
30 changes: 14 additions & 16 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ require "assets"
require "input"
require "audio"
require "class"
require "camera"
require "collision"
require "map"
require "sprite"
require "ui"
require "actor"
require "player"
require "tools"
require "slime"
sti = require "lib/sti"
moonshine = require "lib/moonshine"

showdebug = false
showdebug = true
debug = ""

window = { w = 1050, h = 600, scale = 1.65 }

camera = { x = 0, y = 0, rx = 0, ry = 0 }

speed = { target = 1 / 60, multiplier = 1 }

gamestate = { state = 0, main = 0, paused = 1 }
Expand All @@ -28,7 +28,7 @@ physics = { gravity = 100, friction = 20 }
p = {}

function love.load()
love.window.setMode(window.w, window.h, {vsync = true})
love.window.setMode(window.w, window.h, { vsync = true })
love.window.setTitle("Love2D Project Template")

loadassets()
Expand Down Expand Up @@ -56,21 +56,19 @@ function love.update(dt)
if love.keyboard.isDown("q") then window.scale = window.scale + rate * 5 end
if love.keyboard.isDown("e") then window.scale = window.scale - rate * 5 end

if love.keyboard.isDown("s") then camera.y = camera.y - rate * 1000 end
if love.keyboard.isDown("d") then camera.x = camera.x - rate * 1000 end
if love.keyboard.isDown("w") then camera.y = camera.y + rate * 1000 end
if love.keyboard.isDown("a") then camera.x = camera.x + rate * 1000 end
if love.keyboard.isDown("s") then camera.y = camera.y + rate * 1000 end
if love.keyboard.isDown("d") then camera.x = camera.x + rate * 1000 end
if love.keyboard.isDown("w") then camera.y = camera.y - rate * 1000 end
if love.keyboard.isDown("a") then camera.x = camera.x - rate * 1000 end
else
camera.x = player.x * -1 + (window.w / 2 * (1 / window.scale))
camera.y = player.y * -1 + (window.h / 2 * (1 / window.scale))
camfollowsprite(player)
end

for _,v in pairs(p) do
v:update(rate)
end

camera.ry = math.floor(camera.y)
camera.rx = math.floor(camera.x)
camupdate()
end

mouse.op = mouse.p
Expand All @@ -84,11 +82,11 @@ end

function love.draw()
--draw tiled map
map:draw(camera.rx, camera.ry, window.scale, window.scale)
map:draw(camera.rx * -1, camera.ry * -1, window.scale, window.scale)

love.graphics.push()
--set drawing offset to camera position
love.graphics.translate(camera.rx * window.scale, camera.ry * window.scale)
love.graphics.translate(camera.rx * window.scale * -1, camera.ry * window.scale * -1)

for _,v in pairs(p) do
v:draw()
Expand All @@ -98,7 +96,7 @@ function love.draw()
love.graphics.circle("fill", mouse.x, mouse.y, 3)
love.graphics.pop()

if showdebug then
--if showdebug then
love.graphics.print(tostring(debug or 0))
end
--end
end
3 changes: 2 additions & 1 deletion map.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function loadmap(map)
local path = "Tiled/Maps/Exports/" .. map .. ".lua"
local map = sti(path)

local mapspr = Actor:new(0, 0, "animation")
local mapspr = Actor:new(0, 0, "animation", { type = "static" })
mapspr.body = {}
mapspr.visible = false

Expand All @@ -92,6 +92,7 @@ function loadmap(map)
debug = #mapspr.body

spawn(map)
caminit(map)

return map
end
Expand Down
15 changes: 12 additions & 3 deletions player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ function Player:add(x, y, skins, args)
end

function Player:update(dt)
if love.keyboard.isDown("right") then self.dx = self.dx + dt * 100 end
if love.keyboard.isDown("left") then self.dx = self.dx - dt * 100 end
if love.keyboard.isDown("up") and self:collides({ x = 0, y = 0.01 }) then self.dy = -20 end
if love.keyboard.isDown("right") then
self.dx = self.dx + dt * 100
self.flipped = false
end
if love.keyboard.isDown("left") then
self.dx = self.dx - dt * 100
self.flipped = true
end
if love.keyboard.isDown("up") and self:collides({ x = 0, y = 0.01 }) then
self.dy = -20
end

self:applyphysics(dt)
self:animate(dt)
end
Empty file added slime.lua
Empty file.
4 changes: 3 additions & 1 deletion sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ end

function AnimatedSprite:drawselfanim()
if self.visible then
local xoff = self.flipped and self.w * window.scale or (not self.flipped and 0)

love.graphics.draw(
self.anim.image, self.anim.quads[self.frame],
self.x * window.scale, self.y * window.scale, self.r,
self.x * window.scale + xoff, self.y * window.scale, self.r,
(self.flipped and -1 or (not self.flipped and 1)) * window.scale, window.scale
)
end
Expand Down

0 comments on commit 11c9e57

Please sign in to comment.