-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.lua
67 lines (55 loc) · 1.72 KB
/
entity.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--[[
Filgrim Engine
entity.lua
A 2D platformer engine for LÖVE
By Hoover and Phil
]]
-- Prototype
Entity = {} -- Entity object prototype
-- OO Methods =================================================================
function Entity:new(x, y, width, height, onMap)
-- Constructor
local object = { x = x, y = y, width = width, height = height, map = onMap, active = true, }
setmetatable(object, { __index = Entity })
game.addEntity(object) -- Add ourselves to the global entity table.
return object
end
function Entity:type()
-- Returns the type of object this is.
return "entity"
end
-- Accessors ==================================================================
function Entity:setPosition(x, y)
self.x, self.y = x, y
end
function Entity:getPosition()
return self.x, self.y
end
function Entity:getDimensions()
return self.width, self.height
end
function Entity:setActive(flag)
self.active = flag
end
function Entity:isActive()
return self.active
end
-- Entity Methods =============================================================
function Entity:move(deltaX, deltaY)
-- Does nothing
error("Entity:move() is an abstract method. You should override it in your subclass.")
end
function Entity:update(deltaTime)
if not self.active then return end
-- TODO: Write me!
-- Check and apply gravity here.
self:move(0, 4)
end
function Entity:draw(camera)
-- Draws the entity but skips inactive ones. Pass in the camera.
-- TODO: Write me! Currently just draws a rectangle based on its position and size.
local originX, originY = camera:getOriginPosition()
local x, y = camera:getPosition()
if not self.active then return end
love.graphics.rectangle("fill", originX + self.x - x, originY + self.y - y, self.width, self.height)
end