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

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

View file

@ -14,6 +14,8 @@ local mapX = 0
local mapY = 0 local mapY = 0
local mapSpeed = 32 local mapSpeed = 32
local someUnit
local function drawText(str, x, y) local function drawText(str, x, y)
local font = love.graphics.getFont() local font = love.graphics.getFont()
love.graphics.print(str, x - font:getWidth(str) / 2, y - font:getHeight() / 2) love.graphics.print(str, x - font:getWidth(str) / 2, y - font:getHeight() / 2)
@ -48,6 +50,8 @@ function love.load()
) )
tilemap:random(10, 10) tilemap:random(10, 10)
require "unit"
someUnit = SomeUnit:new(tilemap, 30, 30)
end end
-- 0: nothing -- 0: nothing
@ -117,13 +121,17 @@ function love.draw()
-- tilemap.offsetX = 50 + math.floor(math.cos(time) * 25) -- tilemap.offsetX = 50 + math.floor(math.cos(time) * 25)
-- tilemap.offsetY = 50 + math.floor(math.sin(time) * 25) -- tilemap.offsetY = 50 + math.floor(math.sin(time) * 25)
tilemap:update() tilemap:update()
local tilemapPosX = tilemap.offsetX -- local tilemapPosX = tilemap.offsetX
local tilemapPosY = tilemap.offsetY -- local tilemapPosY = tilemap.offsetY
local tilemapPosX = 10
local tilemapPosY = 10
tilemap:draw(tilemapPosX, tilemapPosY) tilemap:draw(tilemapPosX, tilemapPosY)
love.graphics.setColor(255, 0, 0) love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("line", tilemapPosX - 1, tilemapPosY - 1, 16 * 5 + 1, 16 * 5 + 1) love.graphics.rectangle("line", tilemapPosX - 1, tilemapPosY - 1, 16 * 5 + 1, 16 * 5 + 1)
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
someUnit:draw()
drawText('Hellorld!', 160, 100) drawText('Hellorld!', 160, 100)
if mouseState == 2 then if mouseState == 2 then

BIN
game/res/unit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 217 B

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