Skip to content

Commit

Permalink
body/fixture setup for actor collision
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Jul 20, 2023
1 parent bdf4d8e commit d2e606e
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
Release/
.vscode/
*.tiled-session
*.tiled-session
Scripts/
Binary file added FMOD/Desktop/Master.bank
Binary file not shown.
Binary file added FMOD/Desktop/Master.strings.bank
Binary file not shown.
Binary file modified Graphics/Animations/animation6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphics/Fonts/CommonCase.ttf
Binary file not shown.
40 changes: 20 additions & 20 deletions Tiled/Maps/Exports/sample.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5917,26 +5917,26 @@ return {
properties = {},
encoding = "lua",
data = {
0, 37, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 60, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 114, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 60, 53, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 114, 24, 34, 53, 53, 53, 53, 44, 0, 0, 0, 0, 6, 0, 0, 0, 6, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 60, 61, 61, 61, 61, 61, 34, 44, 0, 0, 0, 17, 0, 0, 56, 25, 57, 65, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 114, 24, 195, 206, 206, 206, 61, 65, 0, 56, 57, 25, 57, 58, 0, 17, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 60, 61, 206, 205, 195, 195, 34, 44, 0, 0, 17, 0, 0, 56, 25, 57, 25, 58, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 60, 61, 61, 206, 61, 61, 23, 121, 0, 0, 28, 0, 0, 0, 17, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 114, 105, 105, 117, 105, 105, 121, 0, 0, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 60, 53, 53, 53, 53, 53, 44, 0, 0, 0, 0, 37, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 60, 61, 61, 61, 61, 61, 65, 0, 0, 0, 37, 35, 34, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 60, 61, 205, 194, 205, 61, 65, 0, 0, 0, 114, 24, 194, 34, 44, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 56, 70, 206, 205, 195, 206, 61, 65, 0, 0, 0, 0, 114, 24, 195, 34, 44, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 60, 61, 205, 206, 61, 61, 65, 0, 0, 0, 0, 0, 114, 24, 205, 34, 44, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 114, 24, 205, 23, 105, 105, 121, 0, 0, 0, 0, 0, 0, 114, 24, 23, 121, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 60,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, 0, 0, 0, 0, 0, 0, 0, 0, 60,
61, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60,
61, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60,
23, 121, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 53, 53, 35,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 53, 53, 35, 61, 61, 61,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 61, 61, 206, 205, 195, 61,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 35, 194, 206, 206, 206, 205, 61,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 61, 206, 205, 206, 206, 206, 61,
65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 61, 205, 205, 194, 205, 195, 61,
65, 0, 0, 0, 0, 37, 53, 53, 53, 53, 53, 53, 53, 53, 53, 44, 0, 0, 0, 0, 0, 0, 60, 61, 194, 194, 194, 206, 195, 61,
34, 53, 53, 53, 53, 35, 61, 61, 61, 61, 61, 61, 61, 61, 61, 34, 53, 53, 53, 53, 53, 53, 35, 61, 61, 61, 61, 61, 61, 195
}
},
{
Expand Down
40 changes: 20 additions & 20 deletions Tiled/Maps/sample.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
<tileset firstgid="122" source="../Tilesets/Template/Template.tsx"/>
<layer id="1" name="Template" width="30" height="20">
<data encoding="csv">
0,37,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,60,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,114,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,60,53,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,114,24,34,53,53,53,53,44,0,0,0,0,6,0,0,0,6,0,6,0,0,0,0,0,0,0,0,0,
0,0,0,60,61,61,61,61,61,34,44,0,0,0,17,0,0,56,25,57,65,0,1,0,0,0,0,0,0,0,
0,0,0,114,24,195,206,206,206,61,65,0,56,57,25,57,58,0,17,0,17,0,0,0,0,0,0,0,0,0,
0,0,0,0,60,61,206,205,195,195,34,44,0,0,17,0,0,56,25,57,25,58,0,0,0,0,0,0,0,0,
0,0,0,0,60,61,61,206,61,61,23,121,0,0,28,0,0,0,17,0,28,0,0,0,0,0,0,0,0,0,
0,0,0,0,114,105,105,117,105,105,121,0,0,0,0,0,0,0,28,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,60,53,53,53,53,53,44,0,0,0,0,37,44,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,60,61,61,61,61,61,65,0,0,0,37,35,34,44,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,60,61,205,194,205,61,65,0,0,0,114,24,194,34,44,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,56,70,206,205,195,206,61,65,0,0,0,0,114,24,195,34,44,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,60,61,205,206,61,61,65,0,0,0,0,0,114,24,205,34,44,0,0,0,0,0,0,
0,0,0,0,0,0,0,114,24,205,23,105,105,121,0,0,0,0,0,0,114,24,23,121,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,60,61,65,0,0,0,0,0,0,0,0,0,0,114,121,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,114,105,36,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,114,121,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
23,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,24,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,35,
34,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,114,24,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
61,65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
23,121,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,53,53,35,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,53,53,35,61,61,61,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,61,206,205,195,61,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,37,35,194,206,206,206,205,61,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,206,205,206,206,206,61,
65,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60,61,205,205,194,205,195,61,
65,0,0,0,0,37,53,53,53,53,53,53,53,53,53,44,0,0,0,0,0,0,60,61,194,194,194,206,195,61,
34,53,53,53,53,35,61,61,61,61,61,61,61,61,61,34,53,53,53,53,53,53,35,61,61,61,61,61,61,195
</data>
</layer>
<objectgroup id="2" name="Objects">
Expand Down
40 changes: 37 additions & 3 deletions actor.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
Actor = inherits(AnimatedSprite, {
dx = 0, dy = 0,
xr = 0, yr = 0
xr = 0, yr = 0,
body = {}
})

local actor_mt = class(Actor)

function Actor:new(x, y, skins)
local o = AnimatedSprite:init(x, y, skins)

return setmetatable(o, actor_mt)
local s = setmetatable(o, actor_mt)

s:adddefaultbox()

return s
end

function Actor:add(x, y, skins)
table.insert(p, Actor:new(x, y, skins))
end

function Actor:addbox(w, h, xoff, yoff)
table.insert(self.body, Rectangle:new(self.x + (xoff or 0), self.y + (yoff or 0), w, h, xoff or 0, yoff or 0))
end

function Actor:adddefaultbox()
self:addbox(self.w, self.h)
end

function Actor:movex(vel)
self.xr = self.xr + vel
local move = self.xr > 0 and math.floor(self.xr) or self.xr <= 0 and math.ceil(self.xr)
Expand All @@ -25,6 +38,7 @@ function Actor:movex(vel)
while move ~= 0 do
self.x = self.x + move
move = move - sign
self:updatebody()
end
end
end
Expand All @@ -39,11 +53,31 @@ function Actor:movey(vel)
while move ~= 0 do
self.y = self.y + move
move = move - sign
self:updatebody()
end
end
end

function Actor:updatebody()
for _,box in pairs(self.body) do
box.x = self.x + box.xoff
box.y = self.y + box.yoff
end
end

function Actor:update(dt)
self:animate(dt)
self:movex(100 * dt)
end

function Actor:draw()
self:drawselfanim()
if love.keyboard.isDown("right") then
self:movex(1)
end

if showdebug then
for _,box in pairs(self.body) do
box:draw()
end
end
end
1 change: 1 addition & 0 deletions assets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ end
function loadassets()
images = loadfolder("Graphics/Sprites", "image")
animations = loadfolder("Graphics/Animations", "anim")
fonts = loadfolder("Graphics/Fonts", "font")
end
4 changes: 3 additions & 1 deletion collision.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Rectangle = {}
Rectangle = {
xoff = 0, yoff = 0
}
local rectangle_mt = class(Rectangle)

function Rectangle:new(x, y, w, h)
Expand Down
42 changes: 23 additions & 19 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ sti = require("sti")
showdebug = true
debug = 0

window = { w = 800, h = 600, scale = 1 }
window = { w = 800, h = 600, scale = 1.65 }

camera = { x = 0, y = 0 }

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

gamestate = { state = 0, main = 0, paused = 1 }

p = {}

Expand All @@ -34,40 +36,42 @@ end

function love.update(dt)
--calculate fixed timestep
local rate = speed.ideal * dt * speed.multiplier * 30
local rate = speed.target * dt * speed.multiplier * 30

updatemouse()

if love.keyboard.isDown("up") then window.scale = window.scale + rate end
if love.keyboard.isDown("down") then window.scale = window.scale - rate end
if gamestate.state == gamestate.main then

if love.keyboard.isDown("w") then camera.y = camera.y - rate * 100 end
if love.keyboard.isDown("a") then camera.x = camera.x - rate * 100 end
if love.keyboard.isDown("s") then camera.y = camera.y + rate * 100 end
if love.keyboard.isDown("d") then camera.x = camera.x + rate * 100 end

for _,v in pairs(p) do
v:update(rate)
end
if love.keyboard.isDown("up") then window.scale = window.scale + rate end
if love.keyboard.isDown("down") then window.scale = window.scale - rate end

if love.keyboard.isDown("w") then camera.y = camera.y - rate * 100 end
if love.keyboard.isDown("a") then camera.x = camera.x - rate * 100 end
if love.keyboard.isDown("s") then camera.y = camera.y + rate * 100 end
if love.keyboard.isDown("d") then camera.x = camera.x + rate * 100 end

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

debug = tostring(mouse.p) .. " " .. tostring(mouse.held)
end

mouse.op = mouse.p
end

function love.draw()
love.graphics.push()
--set drawing offset to camera position
love.graphics.translate(camera.x * window.scale, camera.y * window.scale)

love.graphics.circle("fill", mouse.x, mouse.y, 3)

--draw tiled map
map:draw(camera.x, camera.y, window.scale)

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

love.graphics.pop()
--draw mouse cursor
love.graphics.circle("fill", mouse.x, mouse.y, 3)

if showdebug then
love.graphics.print(tostring(debug))
end
Expand Down
10 changes: 9 additions & 1 deletion sprite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function Sprite:init(x, y, image)
x = x, y = y,
image = images[image]
}
o.w = o.image:getWidth()
o.h = o.image:getHeight()

return o
end
Expand Down Expand Up @@ -60,6 +62,8 @@ function AnimatedSprite:init(x, y, skins)
end

o.anim = o.skins[1]
o.w = o.anim.image:getWidth() / o.anim.frames
o.h = o.anim.image:getHeight()

return o
end
Expand Down Expand Up @@ -105,12 +109,16 @@ function AnimatedSprite:swapanimation(anim)
end
end

function AnimatedSprite:draw()
function AnimatedSprite:drawselfanim()
love.graphics.draw(
self.anim.image, self.anim.quads[self.frame],
self.x * window.scale, self.y * window.scale, self.r,
(self.flipped and -1 or (not self.flipped and 1)) * window.scale, window.scale
)
end

function AnimatedSprite:draw()
self:drawselfanim()

if self.bounds ~= nil and showdebug then
self.bounds:draw()
Expand Down

0 comments on commit d2e606e

Please sign in to comment.