chroma_solstice/entity.lua

155 lines
3.5 KiB
Lua
Raw Normal View History

2015-03-11 20:11:29 -04:00
Entity = class("Entity")
function Entity:initialize(t)
2015-03-12 03:04:09 -04:00
self.x, self.y = t.x, t.y
2015-03-14 05:36:02 -04:00
self.spriteSize = {w = t.sprite:getWidth(), h = t.sprite:getHeight()}
self.size = {w = self.spriteSize.w / 16, h = self.spriteSize.h / 16}
2015-03-15 16:25:00 -04:00
self.cellShape = t.cellShape or self:cellShapeFromBox(self.size)
2015-03-17 01:59:14 -04:00
self.name = t.name
2015-03-11 20:11:29 -04:00
self.color = Color:new(t.color)
2015-03-12 05:13:04 -04:00
self.collision = t.collision --"none", "moveable", "unmoveable"
2015-03-12 03:04:09 -04:00
self.sprite = t.sprite
2015-03-12 04:05:39 -04:00
self.shaAmount = t.sha or 1
2015-03-16 08:47:08 -04:00
self.onBump = t.onBump or function(self) return true end
2015-03-16 10:17:06 -04:00
self.tick = t.tick or function(self) return true end
2015-03-15 05:26:47 -04:00
self.drawOffset = {x = 0, y = 0}
if self.size.w < 1 or self.size.h < 1 then
self.drawOffset.x = (drawScale * 16 * self.size.w) / 2
self.drawOffset.y = (drawScale * 16 * self.size.h) / 2
end
2015-03-11 20:11:29 -04:00
end
function Entity:getCollision()
return self.collision
end
2015-03-11 20:11:29 -04:00
function Entity:atGridPos(x, y)
-- we probably don't need this anymore
2015-03-14 05:36:02 -04:00
local size = self:getGridSize()
if x >= self.x and y >= self.y and x < self.x + size.x and y < self.y + size.y then
2015-03-11 20:11:29 -04:00
return true
end
return false
end
function Entity:getOccupiedCells(coords)
--we have this offset so we can project what a movement would do
local offset = coords or {x = 0, y = 0}
local cells = {}
2015-03-15 16:25:00 -04:00
local pos = self:getGridPos()
for _, cell in ipairs(self:getCellShape()) do
local adjustedCell = {}
adjustedCell.x = pos.x + cell.x - 1 + offset.x
adjustedCell.y = pos.y + cell.y - 1 + offset.y
table.insert(cells, adjustedCell)
end
2015-03-15 16:25:00 -04:00
return cells
end
function Entity:cellShapeFromBox(box)
local cells = {}
for i = 1, box.w do
for j = 1, box.h do
table.insert(cells, {x = i, y = j})
end
end
return cells
end
2015-03-15 16:25:00 -04:00
function Entity:getCellShape()
return self.cellShape
end
2015-03-14 05:36:02 -04:00
function Entity:getDrawBox()
local drawPos = self:getDrawPos()
return {x = drawPos.x, y = drawPos.y, w = self.spriteSize.w * drawScale, h = self.spriteSize.h * drawScale}
end
2015-03-14 06:08:50 -04:00
function Entity:getGridBox()
return {x = self.x, y = self.y, w = self.size.w, h = self.size.h}
end
2015-03-11 20:11:29 -04:00
function Entity:getGridPos()
2015-03-12 03:04:09 -04:00
return {x = self.x, y = self.y}
2015-03-11 20:11:29 -04:00
end
function Entity:getDrawPos()
local gridPos = self:getGridPos()
return {
2015-03-14 05:36:02 -04:00
x = (gridPos.x - 1) * width / gridWidth + self:sha(),
y = (gridPos.y - 1) * height / gridHeight + self:sha()
2015-03-11 20:11:29 -04:00
}
2015-03-12 03:04:09 -04:00
end
2015-03-14 05:36:02 -04:00
function Entity:getGridSize()
return {x = self.size.w, y = self.size.h}
end
2015-03-16 10:17:06 -04:00
function Entity:getname()
return self.name
end
2015-03-12 05:13:04 -04:00
2015-03-16 10:17:06 -04:00
function Entity:tick()
return self.tick()
2015-03-12 05:13:04 -04:00
end
2015-03-12 03:04:09 -04:00
function Entity:draw()
love.graphics.setColor(self.color:set())
local p = self:getDrawPos()
2015-03-15 05:26:47 -04:00
love.graphics.draw(self.sprite, p.x + self.drawOffset.x, p.y + self.drawOffset.y, 0, drawScale, drawScale)
2015-03-12 03:04:09 -04:00
end
function Entity:canMove()
2015-03-15 16:25:00 -04:00
if self:getCollision() == "moveable" then
2015-03-15 07:08:35 -04:00
return true
2015-03-12 03:04:09 -04:00
end
2015-03-15 07:08:35 -04:00
return false
end
function Entity:canPassOver()
2015-03-15 16:25:00 -04:00
if self:getCollision() == "none" then
2015-03-15 07:08:35 -04:00
return true
end
return false
2015-03-12 03:04:09 -04:00
end
2015-03-16 10:17:06 -04:00
function Entity:getClass()
return self.class.name
end
function Entity:getSerializationTable()
local pos = self:getGridPos()
local table = {}
2015-03-17 01:59:14 -04:00
table.x, table.y, table.color = pos.x, pos.y, self.color:getname()
2015-03-16 10:17:06 -04:00
if self.name then table.name = self.name end
return table
end
2015-03-12 03:04:09 -04:00
function Entity:move(axis, direction)
self[axis] = self[axis] + direction
end
2015-03-15 13:54:47 -04:00
function Entity:setSprite(sprite)
self.sprite = sprite
end
2015-03-12 03:04:09 -04:00
2015-03-12 04:05:39 -04:00
function Entity:sha()
2015-03-14 05:36:02 -04:00
return math.random(-worldSha, worldSha) * .5
2015-03-16 07:39:54 -04:00
end
function Entity:bump()
2015-03-16 08:47:08 -04:00
return self:onBump()
2015-03-16 10:17:06 -04:00
end
Entity.static.positionsAreEqual = function(cell1, cell2)
if cell1.x == cell2.x and cell1.y == cell2.y then
return true
end
return false
end