Skip to content

Instantly share code, notes, and snippets.

@benui-dev
Created July 8, 2012 00:20
Show Gist options
  • Save benui-dev/3068718 to your computer and use it in GitHub Desktop.
Save benui-dev/3068718 to your computer and use it in GitHub Desktop.
Example of subclassing physics objects from Löve2d's Hardoncollider, using Class-Commons. Requires HardonCollider to work.
-- Add this line to the main hardoncollider/init.lua file around line 136
-- to allow you to add preconstructed shapes.
function HC:addExistingShape(shape)
return new_shape(self, shape)
end
Sprite = require("sprite")
function love.load()
-- Effectively Sprite:new(x, y, width, height)
mySprite = common.instance(Sprite, 200, 200, 50, 200)
end
function love.draw()
-- center() is from ConvexPolygonShape
local x, y = mySprite:center()
-- Run our custom method
love.graphics.print(mySprite:hello(), x, y)
-- Implemented by ConvexPolygonShape
mySprite:draw('line')
end
-- Have to require HC because it sets up the class stuff
HC = require("hardoncollider")
Shapes = require("hardoncollider.shapes")
Polygon = require("hardoncollider.polygon")
local Sprite = {}
function Sprite:init(x, y, width, height)
-- ConvexPolygonShape requires an instance of Polygon
-- set up a square with our size. Could be set based on the image
-- in our sprite
local polygon = common.instance(Polygon,
x - width/2, y - height/2,
x + width/2, y - height/2,
x + width/2, y + height/2,
x - width/2, y + height/2)
-- Run our parent's constructor
Shapes.ConvexPolygonShape.init(self, polygon)
self.bacon = 42
end
function Sprite:hello()
return "Hey, bacon is currently " .. self.bacon
end
Sprite = common.class("Sprite", Sprite, Shapes.ConvexPolygonShape)
return Sprite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment