3 changed files with 86 additions and 0 deletions
@ -0,0 +1,59 @@ |
|||
-- |
|||
-- classic |
|||
-- |
|||
-- Copyright (c) 2014, rxi |
|||
-- |
|||
-- This module is free software; you can redistribute it and/or modify it under |
|||
-- the terms of the MIT license. See LICENSE for details. |
|||
-- |
|||
|
|||
local Object = {} |
|||
Object.__index = Object |
|||
|
|||
function Object:new() end |
|||
|
|||
function Object:extend() |
|||
local cls = {} |
|||
for k, v in pairs(self) do |
|||
if k:find("__") == 1 then |
|||
cls[k] = v |
|||
end |
|||
end |
|||
cls.__index = cls |
|||
cls.super = self |
|||
setmetatable(cls, self) |
|||
return cls |
|||
end |
|||
|
|||
function Object:implement(...) |
|||
for _, cls in pairs({ ... }) do |
|||
for k, v in pairs(cls) do |
|||
if self[k] == nil and type(v) == "function" then |
|||
self[k] = v |
|||
end |
|||
end |
|||
end |
|||
end |
|||
|
|||
function Object:is(T) |
|||
local mt = getmetatable(self) |
|||
while mt do |
|||
if mt == T then |
|||
return true |
|||
end |
|||
mt = getmetatable(mt) |
|||
end |
|||
return false |
|||
end |
|||
|
|||
function Object:__tostring() |
|||
return "Object" |
|||
end |
|||
|
|||
function Object:__call(...) |
|||
local obj = setmetatable({}, self) |
|||
obj:new(...) |
|||
return obj |
|||
end |
|||
|
|||
return Object |
|||
@ -0,0 +1,12 @@ |
|||
function love.load() |
|||
Object = require("classic") |
|||
require("player") |
|||
|
|||
player = Player() |
|||
end |
|||
|
|||
function love.update(dt) end |
|||
|
|||
function love.draw() |
|||
player:draw() |
|||
end |
|||
@ -0,0 +1,15 @@ |
|||
--! file: player.lua |
|||
Player = Object:extend() |
|||
|
|||
function Player:new() |
|||
self.x = 100 |
|||
self.y = 100 |
|||
self.health = 100 |
|||
end |
|||
|
|||
function Player:update(dt) end |
|||
|
|||
function Player:draw() |
|||
local vert = { self.x, self.y, (self.x - 50), (self.y - 50), (self.x + 50), (self.y - 50) } |
|||
love.graphics.polygon("fill", vert) |
|||
end |
|||
Loading…
Reference in new issue