Базовый класс юнита, простейший юнит

This commit is contained in:
Иван Кузьменко 2025-09-12 03:57:02 +03:00
parent e629d0ffa4
commit 9c162282a7
3 changed files with 60 additions and 2 deletions

50
game/unit.lua Normal file
View file

@ -0,0 +1,50 @@
BaseUnit = {}
BaseUnit.__index = BaseUnit
function BaseUnit:new(tilemap, x, y, size)
local this = {}
this.tilemap = tilemap
this.x = x
this.y = y
this.size = size or 16
this.sizeHalf = math.floor(this.size / 2) -- TODO: replace with Lua 5.3's integer thingies
return setmetatable(this, BaseUnit)
end
function BaseUnit:middlePoint()
return self.x + self.sizeHalf, self.y + self.sizeHalf
end
function BaseUnit:command(x, y, entity)
-- NOOP
end
function BaseUnit:draw()
-- NOOP
end
function BaseUnit:drawLimited(quad)
-- NOOP
end
function BaseUnit:__tostring()
return "BaseUnit(x=" .. self.x .. ", y=" .. self.y .. ")"
end
----------------------
SomeUnit = setmetatable({}, { __index = BaseUnit })
SomeUnit.__index = SomeUnit
local unitTexture = love.graphics.newImage("res/unit.png")
function SomeUnit:new(tilemap, x, y)
local this = BaseUnit:new(tilemap, x, y, 16)
return setmetatable(this, SomeUnit)
end
function SomeUnit:draw()
love.graphics.draw(unitTexture, self.x - self.tilemap.offsetX, self.y - self.tilemap.offsetY)
end