Created
July 8, 2012 00:20
-
-
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.
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
-- 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 |
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
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 |
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
-- 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