summaryrefslogtreecommitdiff
path: root/entity.lua
diff options
context:
space:
mode:
Diffstat (limited to 'entity.lua')
-rw-r--r--entity.lua78
1 files changed, 78 insertions, 0 deletions
diff --git a/entity.lua b/entity.lua
new file mode 100644
index 0000000..3619993
--- /dev/null
+++ b/entity.lua
@@ -0,0 +1,78 @@
+local Entity = {}
+Entity.__index = Entity
+
+function Entity:new(x, y, width, height, physicsWidth, physicsHeight)
+ local self = setmetatable({}, Entity)
+ self.x = x
+ self.y = y
+ self.width = width
+ self.height = height
+ self.physicsWidth = physicsWidth or width
+ self.physicsHeight = physicsHeight or height
+ return self
+end
+
+function Entity:update(dt)
+end
+
+function Entity:draw()
+end
+
+function Entity:enablePhysics(world, bodyType)
+ bodyType = bodyType or "dynamic"
+ if not world then error("love.physics.World is required") end
+ local cx = self.x + self.physicsWidth / 2
+ local cy = self.y + self.height - self.physicsHeight / 2
+ self.body = love.physics.newBody(world, cx, cy, bodyType)
+ local shape = love.physics.newRectangleShape(self.physicsWidth, self.physicsHeight)
+ self.fixture = love.physics.newFixture(self.body, shape, 1)
+ if self.fixture.setFriction then self.fixture:setFriction(0.3) end
+ if self.fixture.setRestitution then self.fixture:setRestitution(0) end
+ if self.fixture.setUserData then self.fixture:setUserData(self) end
+ self.contact = { floor = 0, wall = 0, ceiling = 0 }
+end
+
+function Entity:syncFromPhysicsBody()
+ if not self.body then return end
+ self.x = self.body:getX() - self.physicsWidth / 2
+ self.y = self.body:getY() - self.height + self.physicsHeight / 2
+end
+
+function Entity:getPhysicsBody()
+ return self.body
+end
+
+function Entity:getPhysicsFixture()
+ return self.fixture
+end
+
+function Entity:setPropertiesFromOptions(options)
+ options = options or {}
+ self.properties = {}
+ if options.properties then
+ for k, v in pairs(options.properties) do
+ self.properties[k] = v
+ end
+ end
+
+ for k, v in pairs(options) do
+ if self[k] == nil then
+ self[k] = v
+ end
+ end
+
+ return self
+end
+
+function Entity:has(key)
+ return self.properties and self.properties[key] ~= nil
+end
+
+function Entity:get(key, default)
+ if self.properties and self.properties[key] ~= nil then
+ return self.properties[key]
+ end
+ return default
+end
+
+return Entity \ No newline at end of file