chroma_solstice/entity.lua
2015-03-12 04:05:39 -04:00

60 lines
No EOL
1.3 KiB
Lua

Entity = class("Entity")
function Entity:initialize(t)
self.x, self.y = t.x, t.y
self.gridSize = {x = t.sizeX, y = t.sizeY}
self.color = Color:new(t.color)
self.collision = t.collision --either "none", "moveable", "unmoveable"
self.sprite = t.sprite
self.shaAmount = t.sha or 1
end
function Entity:atGridPos(x, y)
if x >= self.x and x <= self.x + self.gridSize.x and
y >= self.y and y <= self.gridSize.y then
return true
end
return false
end
function Entity:getGridPos()
return {x = self.x, y = self.y}
end
function Entity:getDrawPos()
local gridPos = self:getGridPos()
return {
x = (gridPos.x - 1) * width / gridWidth,
y = (gridPos.y - 1) * height / gridHeight
}
end
function Entity:draw()
love.graphics.setColor(self.color:set())
local p = self:getDrawPos()
love.graphics.draw(self.sprite, p.x + self:sha(), p.y + self:sha(), 0, drawScale, drawScale)
end
function Entity:isAlignedWithPlayerMovement(axis, pos)
if self:getGridPos()[axis] == pos then
return true
end
return false
end
function Entity:canMove()
if self.collision == "unmoveable" then
return false
end
return true
end
function Entity:move(axis, direction)
self[axis] = self[axis] + direction
end
function Entity:sha()
return math.random(-self.shaAmount, self.shaAmount) * .5
end