Skip to content

Instantly share code, notes, and snippets.

@a327ex
Last active July 25, 2018 22:34
Show Gist options
  • Save a327ex/03ca4594700484d6f66cea8b9a82547a to your computer and use it in GitHub Desktop.
Save a327ex/03ca4594700484d6f66cea8b9a82547a to your computer and use it in GitHub Desktop.
export Object
class Object
new: (level, x, y, opts) =>
for k, v in pairs(opts) do self[k] = v
@id = uuid!
@dead = false
@creation_time = love.timer.getTime!
@depth = 50
@level = level
@x, @y, @r, @sx, @sy = x, y, 0, 1, 1
@collision_events = {}
update: (dt) =>
if @body
@x, @y = @body\getPosition!
@r = @body\getAngle!
draw: =>
if @body
if @shape_type == 'rectangle'
love.graphics.polygon('line', @body\getWorldPoints(@shape\getPoints!))
elseif @shape_type == 'edge'
love.graphics.line(@x + @x1, @y + @y1, @x + @x2, @y + @y2)
elseif @shape_type == 'chain'
points = {@body\getWorldPoints(@shape\getPoints!)}
if @loop then love.graphics.polygon('line', points)
else
for i = 1, #points-2, 2 do love.graphics.line(points[i], points[i+1], points[i+2], points[i+3])
elseif @shape_type == 'polygon'
love.graphics.polygon('line', @body\getWorldPoints(@shape\getPoints!))
elseif @shape_type == 'circle'
love.graphics.circle('line', @x, @y, @radius)
destroy: =>
if @body
@fixture\destroy!
@sensor\destroy!
@body\destroy!
@body, @fixture, @sensor, @shape = nil, nil, nil, nil
after: (delay, action, tag) =>
if tag then tag ..= @id
after(delay, action, tag)
every: (delay, action, count, after, tag) =>
if tag then tag ..= @id
every(delay, action, count, after, tag)
decreasing_every: (delay, decrease, action, count, after, tag) =>
if tag then tag ..= @id
decreasing_every(delay, decrease, action, count, after, tag)
during: (delay, action, after, tag) =>
if tag then tag ..= @id
during(delay, action, after, tag)
tween: (delay, subject, target, method, after, tag) =>
if tag then tag ..= @id
tween(delay, subject, target, method, after, tag)
rectangle: (tag, w, h, type='static') =>
@tag = tag
@w, @h = w, h
@shape_type = 'rectangle'
@body = love.physics.newBody(@level.world, @x, @y, type)
@shape = love.physics.newRectangleShape(w, h)
@fixture = love.physics.newFixture(@body, @shape)
@fixture\setUserData(@id)
@fixture\setFilterData(collision_tags[tag].categories, collision_tags[tag].masks, 0)
@sensor = love.physics.newFixture(@body, @shape)
@sensor\setSensor(true)
@sensor\setUserData(@id)
@sensor\setFilterData(trigger_tags[tag].categories, trigger_tags[tag].masks, 0)
edge: (tag, x1, y1, x2, y2, type='static') =>
@tag = tag
@x1, @y1, @x2, @y2 = x1, y1, x2, y2
@shape_type = 'edge'
@body = love.physics.newBody(@level.world, 0, 0, type)
@shape = love.physics.newEdgeShape(x1, y1, x2, y2)
@fixture = love.physics.newFixture(@body, @shape)
@fixture\setUserData(@id)
@fixture\setFilterData(collision_tags[tag].categories, collision_tags[tag].masks, 0)
@sensor = love.physics.newFixture(@body, @shape)
@sensor\setSensor(true)
@sensor\setUserData(@id)
@sensor\setFilterData(trigger_tags[tag].categories, trigger_tags[tag].masks, 0)
chain: (tag, loop, vertices, type='static') =>
@tag = tag
@loop = loop
@vertices = vertices
@shape_type = 'chain'
@body = love.physics.newBody(@level.world, 0, 0, type)
@shape = love.physics.newChainShape(loop, vertices)
@fixture = love.physics.newFixture(@body, @shape)
@fixture\setUserData(@id)
@fixture\setFilterData(collision_tags[tag].categories, collision_tags[tag].masks, 0)
@sensor = love.physics.newFixture(@body, @shape)
@sensor\setSensor(true)
@sensor\setUserData(@id)
@sensor\setFilterData(trigger_tags[tag].categories, trigger_tags[tag].masks, 0)
polygon: (tag, vertices, type='static') =>
@tag = tag
@vertices = vertices
@shape_type = 'polygon'
@body = love.physics.newBody(@level.world, 0, 0, type)
@shape = love.physics.newPolygonShape(vertices)
@fixture = love.physics.newFixture(@body, @shape)
@fixture\setUserData(@id)
@fixture\setFilterData(collision_tags[tag].categories, collision_tags[tag].masks, 0)
@sensor = love.physics.newFixture(@body, @shape)
@sensor\setSensor(true)
@sensor\setUserData(@id)
@sensor\setFilterData(trigger_tags[tag].categories, trigger_tags[tag].masks, 0)
circle: (tag, radius, type='static') =>
@tag = tag
@radius = radius
@shape_type = 'circle'
@body = love.physics.newBody(@level.world, @x, @y, type)
@shape = love.physics.newCircleShape(radius)
@fixture = love.physics.newFixture(@body, @shape)
@fixture\setUserData(@id)
@fixture\setFilterData(collision_tags[tag].categories, collision_tags[tag].masks, 0)
@sensor = love.physics.newFixture(@body, @shape)
@sensor\setSensor(true)
@sensor\setUserData(@id)
@sensor\setFilterData(trigger_tags[tag].categories, trigger_tags[tag].masks, 0)
enter: (tag) =>
if not @collision_events[tag] then return {}
collision_events = {}
for _, e in ipairs(@collision_events[tag])
if e.type == 'enter' then table.insert(collision_events, e)
return collision_events
exit: (tag) =>
if not @collision_events[tag] then return {}
collision_events = {}
for _, e in ipairs(@collision_events[tag])
if e.type == 'exit' then table.insert(collision_events, e)
return collision_events
post: (tag) =>
if not @collision_events[tag] then return {}
collision_events = {}
for _, e in ipairs(@collision_events[tag])
if e.type == 'post' then table.insert(collision_events, e)
return collision_events
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment